Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / os / Transaction.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3
4 #include "ObjectStore.h"
5 #include "common/Formatter.h"
6
7 void ObjectStore::Transaction::dump(ceph::Formatter *f)
8 {
9   f->open_array_section("ops");
10   iterator i = begin();
11   int op_num = 0;
12   bool stop_looping = false;
13   while (i.have_op() && !stop_looping) {
14     Transaction::Op *op = i.decode_op();
15     f->open_object_section("op");
16     f->dump_int("op_num", op_num);
17
18     switch (op->op) {
19     case Transaction::OP_NOP:
20       f->dump_string("op_name", "nop");
21       break;
22     case Transaction::OP_TOUCH:
23       {
24         coll_t cid = i.get_cid(op->cid);
25         ghobject_t oid = i.get_oid(op->oid);
26         f->dump_string("op_name", "touch");
27         f->dump_stream("collection") << cid;
28         f->dump_stream("oid") << oid;
29       }
30       break;
31       
32     case Transaction::OP_WRITE:
33       {
34         coll_t cid = i.get_cid(op->cid);
35         ghobject_t oid = i.get_oid(op->oid);
36         uint64_t off = op->off;
37         uint64_t len = op->len;
38         bufferlist bl;
39         i.decode_bl(bl);
40         f->dump_string("op_name", "write");
41         f->dump_stream("collection") << cid;
42         f->dump_stream("oid") << oid;
43         f->dump_unsigned("length", len);
44         f->dump_unsigned("offset", off);
45         f->dump_unsigned("bufferlist length", bl.length());
46       }
47       break;
48       
49     case Transaction::OP_ZERO:
50       {
51         coll_t cid = i.get_cid(op->cid);
52         ghobject_t oid = i.get_oid(op->oid);
53         uint64_t off = op->off;
54         uint64_t len = op->len;
55         f->dump_string("op_name", "zero");
56         f->dump_stream("collection") << cid;
57         f->dump_stream("oid") << oid;
58         f->dump_unsigned("offset", off);
59         f->dump_unsigned("length", len);
60       }
61       break;
62       
63     case Transaction::OP_TRIMCACHE:
64       {
65         // deprecated, no-op
66         f->dump_string("op_name", "trim_cache");
67       }
68       break;
69       
70     case Transaction::OP_TRUNCATE:
71       {
72         coll_t cid = i.get_cid(op->cid);
73         ghobject_t oid = i.get_oid(op->oid);
74         uint64_t off = op->off;
75         f->dump_string("op_name", "truncate");
76         f->dump_stream("collection") << cid;
77         f->dump_stream("oid") << oid;
78         f->dump_unsigned("offset", off);
79       }
80       break;
81       
82     case Transaction::OP_REMOVE:
83       {
84         coll_t cid = i.get_cid(op->cid);
85         ghobject_t oid = i.get_oid(op->oid);
86         f->dump_string("op_name", "remove");
87         f->dump_stream("collection") << cid;
88         f->dump_stream("oid") << oid;
89       }
90       break;
91       
92     case Transaction::OP_SETATTR:
93       {
94         coll_t cid = i.get_cid(op->cid);
95         ghobject_t oid = i.get_oid(op->oid);
96         string name = i.decode_string();
97         bufferlist bl;
98         i.decode_bl(bl);
99         f->dump_string("op_name", "setattr");
100         f->dump_stream("collection") << cid;
101         f->dump_stream("oid") << oid;
102         f->dump_string("name", name);
103         f->dump_unsigned("length", bl.length());
104       }
105       break;
106       
107     case Transaction::OP_SETATTRS:
108       {
109         coll_t cid = i.get_cid(op->cid);
110         ghobject_t oid = i.get_oid(op->oid);
111         map<string, bufferptr> aset;
112         i.decode_attrset(aset);
113         f->dump_string("op_name", "setattrs");
114         f->dump_stream("collection") << cid;
115         f->dump_stream("oid") << oid;
116         f->open_object_section("attr_lens");
117         for (map<string,bufferptr>::iterator p = aset.begin();
118             p != aset.end(); ++p) {
119           f->dump_unsigned(p->first.c_str(), p->second.length());
120         }
121         f->close_section();
122       }
123       break;
124
125     case Transaction::OP_RMATTR:
126       {
127         coll_t cid = i.get_cid(op->cid);
128         ghobject_t oid = i.get_oid(op->oid);
129         string name = i.decode_string();
130         f->dump_string("op_name", "rmattr");
131         f->dump_stream("collection") << cid;
132         f->dump_stream("oid") << oid;
133         f->dump_string("name", name);
134       }
135       break;
136
137     case Transaction::OP_RMATTRS:
138       {
139         coll_t cid = i.get_cid(op->cid);
140         ghobject_t oid = i.get_oid(op->oid);
141         f->dump_string("op_name", "rmattrs");
142         f->dump_stream("collection") << cid;
143         f->dump_stream("oid") << oid;
144       }
145       break;
146       
147     case Transaction::OP_CLONE:
148       {
149         coll_t cid = i.get_cid(op->cid);
150         ghobject_t oid = i.get_oid(op->oid);
151         ghobject_t noid = i.get_oid(op->dest_oid);
152         f->dump_string("op_name", "clone");
153         f->dump_stream("collection") << cid;
154         f->dump_stream("src_oid") << oid;
155         f->dump_stream("dst_oid") << noid;
156       }
157       break;
158
159     case Transaction::OP_CLONERANGE:
160       {
161         coll_t cid = i.get_cid(op->cid);
162         ghobject_t oid = i.get_oid(op->oid);
163         ghobject_t noid = i.get_oid(op->dest_oid);
164         uint64_t off = op->off;
165         uint64_t len = op->len;
166         f->dump_string("op_name", "clonerange");
167         f->dump_stream("collection") << cid;
168         f->dump_stream("src_oid") << oid;
169         f->dump_stream("dst_oid") << noid;
170         f->dump_unsigned("offset", off);
171         f->dump_unsigned("len", len);
172       }
173       break;
174
175     case Transaction::OP_CLONERANGE2:
176       {
177         coll_t cid = i.get_cid(op->cid);
178         ghobject_t oid = i.get_oid(op->oid);
179         ghobject_t noid = i.get_oid(op->dest_oid);
180         uint64_t srcoff = op->off;
181         uint64_t len = op->len;
182         uint64_t dstoff = op->dest_off;
183         f->dump_string("op_name", "clonerange2");
184         f->dump_stream("collection") << cid;
185         f->dump_stream("src_oid") << oid;
186         f->dump_stream("dst_oid") << noid;
187         f->dump_unsigned("src_offset", srcoff);
188         f->dump_unsigned("len", len);
189         f->dump_unsigned("dst_offset", dstoff);
190       }
191       break;
192
193     case Transaction::OP_MKCOLL:
194       {
195         coll_t cid = i.get_cid(op->cid);
196         f->dump_string("op_name", "mkcoll");
197         f->dump_stream("collection") << cid;
198       }
199       break;
200
201     case Transaction::OP_COLL_HINT:
202       {
203         coll_t cid = i.get_cid(op->cid);
204         uint32_t type = op->hint_type;
205         f->dump_string("op_name", "coll_hint");
206         f->dump_stream("collection") << cid;
207         f->dump_unsigned("type", type);
208         bufferlist hint;
209         i.decode_bl(hint);
210         bufferlist::iterator hiter = hint.begin();
211         if (type == Transaction::COLL_HINT_EXPECTED_NUM_OBJECTS) {
212           uint32_t pg_num;
213           uint64_t num_objs;
214           ::decode(pg_num, hiter);
215           ::decode(num_objs, hiter);
216           f->dump_unsigned("pg_num", pg_num);
217           f->dump_unsigned("expected_num_objects", num_objs);
218         }
219       }
220       break;
221
222     case Transaction::OP_COLL_SET_BITS:
223       {
224         coll_t cid = i.get_cid(op->cid);
225         f->dump_string("op_name", "coll_set_bits");
226         f->dump_stream("collection") << cid;
227         f->dump_unsigned("bits", op->split_bits);
228       }
229       break;
230
231     case Transaction::OP_RMCOLL:
232       {
233         coll_t cid = i.get_cid(op->cid);
234         f->dump_string("op_name", "rmcoll");
235         f->dump_stream("collection") << cid;
236       }
237       break;
238
239     case Transaction::OP_COLL_ADD:
240       {
241         coll_t ocid = i.get_cid(op->cid);
242         coll_t ncid = i.get_cid(op->dest_cid);
243         ghobject_t oid = i.get_oid(op->oid);
244         f->dump_string("op_name", "collection_add");
245         f->dump_stream("src_collection") << ocid;
246         f->dump_stream("dst_collection") << ncid;
247         f->dump_stream("oid") << oid;
248       }
249       break;
250
251     case Transaction::OP_COLL_REMOVE:
252        {
253         coll_t cid = i.get_cid(op->cid);
254         ghobject_t oid = i.get_oid(op->oid);
255         f->dump_string("op_name", "collection_remove");
256         f->dump_stream("collection") << cid;
257         f->dump_stream("oid") << oid;
258        }
259       break;
260
261     case Transaction::OP_COLL_MOVE:
262        {
263         coll_t ocid = i.get_cid(op->cid);
264         coll_t ncid = i.get_cid(op->dest_cid);
265         ghobject_t oid = i.get_oid(op->oid);
266         f->open_object_section("collection_move");
267         f->dump_stream("src_collection") << ocid;
268         f->dump_stream("dst_collection") << ncid;
269         f->dump_stream("oid") << oid;
270         f->close_section();
271        }
272       break;
273
274     case Transaction::OP_COLL_SETATTR:
275       {
276         coll_t cid = i.get_cid(op->cid);
277         string name = i.decode_string();
278         bufferlist bl;
279         i.decode_bl(bl);
280         f->dump_string("op_name", "collection_setattr");
281         f->dump_stream("collection") << cid;
282         f->dump_string("name", name);
283         f->dump_unsigned("length", bl.length());
284       }
285       break;
286
287     case Transaction::OP_COLL_RMATTR:
288       {
289         coll_t cid = i.get_cid(op->cid);
290         string name = i.decode_string();
291         f->dump_string("op_name", "collection_rmattr");
292         f->dump_stream("collection") << cid;
293         f->dump_string("name", name);
294       }
295       break;
296
297     case Transaction::OP_STARTSYNC:
298       f->dump_string("op_name", "startsync");
299       break;
300
301     case Transaction::OP_COLL_RENAME:
302       {
303         f->dump_string("op_name", "collection_rename");
304       }
305       break;
306
307     case Transaction::OP_OMAP_CLEAR:
308       {
309         coll_t cid = i.get_cid(op->cid);
310         ghobject_t oid = i.get_oid(op->oid);
311         f->dump_string("op_name", "omap_clear");
312         f->dump_stream("collection") << cid;
313         f->dump_stream("oid") << oid;
314       }
315       break;
316
317     case Transaction::OP_OMAP_SETKEYS:
318       {
319         coll_t cid = i.get_cid(op->cid);
320         ghobject_t oid = i.get_oid(op->oid);
321         map<string, bufferlist> aset;
322         i.decode_attrset(aset);
323         f->dump_string("op_name", "omap_setkeys");
324         f->dump_stream("collection") << cid;
325         f->dump_stream("oid") << oid;
326         f->open_object_section("attr_lens");
327         for (map<string, bufferlist>::iterator p = aset.begin();
328             p != aset.end(); ++p) {
329           f->dump_unsigned(p->first.c_str(), p->second.length());
330         }
331         f->close_section();
332       }
333       break;
334
335     case Transaction::OP_OMAP_RMKEYS:
336       {
337         coll_t cid = i.get_cid(op->cid);
338         ghobject_t oid = i.get_oid(op->oid);
339         set<string> keys;
340         i.decode_keyset(keys);
341         f->dump_string("op_name", "omap_rmkeys");
342         f->dump_stream("collection") << cid;
343         f->dump_stream("oid") << oid;
344       }
345       break;
346
347     case Transaction::OP_OMAP_SETHEADER:
348       {
349         coll_t cid = i.get_cid(op->cid);
350         ghobject_t oid = i.get_oid(op->oid);
351         bufferlist bl;
352         i.decode_bl(bl);
353         f->dump_string("op_name", "omap_setheader");
354         f->dump_stream("collection") << cid;
355         f->dump_stream("oid") << oid;
356         f->dump_stream("header_length") << bl.length();
357       }
358       break;
359
360     case Transaction::OP_SPLIT_COLLECTION:
361       {
362         coll_t cid = i.get_cid(op->cid);
363         uint32_t bits = op->split_bits;
364         uint32_t rem = op->split_rem;
365         coll_t dest = i.get_cid(op->dest_cid);
366         f->dump_string("op_name", "op_split_collection_create");
367         f->dump_stream("collection") << cid;
368         f->dump_stream("bits") << bits;
369         f->dump_stream("rem") << rem;
370         f->dump_stream("dest") << dest;
371       }
372       break;
373
374     case Transaction::OP_SPLIT_COLLECTION2:
375       {
376         coll_t cid = i.get_cid(op->cid);
377         uint32_t bits = op->split_bits;
378         uint32_t rem = op->split_rem;
379         coll_t dest = i.get_cid(op->dest_cid);
380         f->dump_string("op_name", "op_split_collection");
381         f->dump_stream("collection") << cid;
382         f->dump_stream("bits") << bits;
383         f->dump_stream("rem") << rem;
384         f->dump_stream("dest") << dest;
385       }
386       break;
387
388     case Transaction::OP_OMAP_RMKEYRANGE:
389       {
390         coll_t cid = i.get_cid(op->cid);
391         ghobject_t oid = i.get_oid(op->oid);
392         string first, last;
393         first = i.decode_string();
394         last = i.decode_string();
395         f->dump_string("op_name", "op_omap_rmkeyrange");
396         f->dump_stream("collection") << cid;
397         f->dump_stream("oid") << oid;
398         f->dump_string("first", first);
399         f->dump_string("last", last);
400       }
401       break;
402
403     case Transaction::OP_COLL_MOVE_RENAME:
404       {
405         coll_t old_cid = i.get_cid(op->cid);
406         ghobject_t old_oid = i.get_oid(op->oid);
407         coll_t new_cid = i.get_cid(op->dest_cid);
408         ghobject_t new_oid = i.get_oid(op->dest_oid);
409         f->dump_string("op_name", "op_coll_move_rename");
410         f->dump_stream("old_collection") << old_cid;
411         f->dump_stream("old_oid") << old_oid;
412         f->dump_stream("new_collection") << new_cid;
413         f->dump_stream("new_oid") << new_oid;
414       }
415       break;
416
417     case Transaction::OP_TRY_RENAME:
418       {
419         coll_t cid = i.get_cid(op->cid);
420         ghobject_t old_oid = i.get_oid(op->oid);
421         ghobject_t new_oid = i.get_oid(op->dest_oid);
422         f->dump_string("op_name", "op_coll_move_rename");
423         f->dump_stream("collection") << cid;
424         f->dump_stream("old_oid") << old_oid;
425         f->dump_stream("new_oid") << new_oid;
426       }
427       break;
428         
429     case Transaction::OP_SETALLOCHINT:
430       {
431         coll_t cid = i.get_cid(op->cid);
432         ghobject_t oid = i.get_oid(op->oid);
433         uint64_t expected_object_size = op->expected_object_size;
434         uint64_t expected_write_size = op->expected_write_size;
435         f->dump_string("op_name", "op_setallochint");
436         f->dump_stream("collection") << cid;
437         f->dump_stream("oid") << oid;
438         f->dump_stream("expected_object_size") << expected_object_size;
439         f->dump_stream("expected_write_size") << expected_write_size;
440       }
441       break;
442
443     default:
444       f->dump_string("op_name", "unknown");
445       f->dump_unsigned("op_code", op->op);
446       stop_looping = true;
447       break;
448     }
449     f->close_section();
450     op_num++;
451   }
452   f->close_section();
453 }
454
455 #pragma GCC diagnostic ignored "-Wpragmas"
456 #pragma GCC diagnostic push
457 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
458
459 void ObjectStore::Transaction::generate_test_instances(list<ObjectStore::Transaction*>& o)
460 {
461   o.push_back(new Transaction);
462
463   Transaction *t = new Transaction;
464   t->nop();
465   o.push_back(t);
466   
467   t = new Transaction;
468   coll_t c(spg_t(pg_t(1,2), shard_id_t::NO_SHARD));
469   coll_t c2(spg_t(pg_t(4,5), shard_id_t::NO_SHARD));
470   ghobject_t o1(hobject_t("obj", "", 123, 456, -1, ""));
471   ghobject_t o2(hobject_t("obj2", "", 123, 456, -1, ""));
472   ghobject_t o3(hobject_t("obj3", "", 123, 456, -1, ""));
473   t->touch(c, o1);
474   bufferlist bl;
475   bl.append("some data");
476   t->write(c, o1, 1, bl.length(), bl);
477   t->zero(c, o1, 22, 33);
478   t->truncate(c, o1, 99);
479   t->remove(c, o1);
480   o.push_back(t);
481
482   t = new Transaction;
483   t->setattr(c, o1, "key", bl);
484   map<string,bufferptr> m;
485   m["a"] = buffer::copy("this", 4);
486   m["b"] = buffer::copy("that", 4);
487   t->setattrs(c, o1, m);
488   t->rmattr(c, o1, "b");
489   t->rmattrs(c, o1);
490
491   t->clone(c, o1, o2);
492   t->clone(c, o1, o3);
493   t->clone_range(c, o1, o2, 1, 12, 99);
494
495   t->create_collection(c, 12);
496   t->collection_move_rename(c, o2, c2, o3);
497   t->remove_collection(c);
498   o.push_back(t);  
499 }
500
501 #pragma GCC diagnostic pop
502 #pragma GCC diagnostic warning "-Wpragmas"