Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / osd / ECMsgTypes.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2013 Inktank Storage, Inc.
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15 #include "ECMsgTypes.h"
16
17 void ECSubWrite::encode(bufferlist &bl) const
18 {
19   ENCODE_START(4, 1, bl);
20   ::encode(from, bl);
21   ::encode(tid, bl);
22   ::encode(reqid, bl);
23   ::encode(soid, bl);
24   ::encode(stats, bl);
25   ::encode(t, bl);
26   ::encode(at_version, bl);
27   ::encode(trim_to, bl);
28   ::encode(log_entries, bl);
29   ::encode(temp_added, bl);
30   ::encode(temp_removed, bl);
31   ::encode(updated_hit_set_history, bl);
32   ::encode(roll_forward_to, bl);
33   ::encode(backfill, bl);
34   ENCODE_FINISH(bl);
35 }
36
37 void ECSubWrite::decode(bufferlist::iterator &bl)
38 {
39   DECODE_START(4, bl);
40   ::decode(from, bl);
41   ::decode(tid, bl);
42   ::decode(reqid, bl);
43   ::decode(soid, bl);
44   ::decode(stats, bl);
45   ::decode(t, bl);
46   ::decode(at_version, bl);
47   ::decode(trim_to, bl);
48   ::decode(log_entries, bl);
49   ::decode(temp_added, bl);
50   ::decode(temp_removed, bl);
51   if (struct_v >= 2) {
52     ::decode(updated_hit_set_history, bl);
53   }
54   if (struct_v >= 3) {
55     ::decode(roll_forward_to, bl);
56   } else {
57     roll_forward_to = trim_to;
58   }
59   if (struct_v >= 4) {
60     ::decode(backfill, bl);
61   } else {
62     // The old protocol used an empty transaction to indicate backfill
63     backfill = t.empty();
64   }
65   DECODE_FINISH(bl);
66 }
67
68 std::ostream &operator<<(
69   std::ostream &lhs, const ECSubWrite &rhs)
70 {
71   lhs << "ECSubWrite(tid=" << rhs.tid
72       << ", reqid=" << rhs.reqid
73       << ", at_version=" << rhs.at_version
74       << ", trim_to=" << rhs.trim_to
75       << ", roll_forward_to=" << rhs.roll_forward_to;
76   if (rhs.updated_hit_set_history)
77     lhs << ", has_updated_hit_set_history";
78   if (rhs.backfill)
79     lhs << ", backfill";
80   return lhs <<  ")";
81 }
82
83 void ECSubWrite::dump(Formatter *f) const
84 {
85   f->dump_unsigned("tid", tid);
86   f->dump_stream("reqid") << reqid;
87   f->dump_stream("at_version") << at_version;
88   f->dump_stream("trim_to") << trim_to;
89   f->dump_stream("roll_forward_to") << roll_forward_to;
90   f->dump_bool("has_updated_hit_set_history",
91       static_cast<bool>(updated_hit_set_history));
92   f->dump_bool("backfill", backfill);
93 }
94
95 void ECSubWrite::generate_test_instances(list<ECSubWrite*> &o)
96 {
97   o.push_back(new ECSubWrite());
98   o.back()->tid = 1;
99   o.back()->at_version = eversion_t(2, 100);
100   o.back()->trim_to = eversion_t(1, 40);
101   o.push_back(new ECSubWrite());
102   o.back()->tid = 4;
103   o.back()->reqid = osd_reqid_t(entity_name_t::CLIENT(123), 1, 45678);
104   o.back()->at_version = eversion_t(10, 300);
105   o.back()->trim_to = eversion_t(5, 42);
106   o.push_back(new ECSubWrite());
107   o.back()->tid = 9;
108   o.back()->reqid = osd_reqid_t(entity_name_t::CLIENT(123), 1, 45678);
109   o.back()->at_version = eversion_t(10, 300);
110   o.back()->trim_to = eversion_t(5, 42);
111   o.back()->roll_forward_to = eversion_t(8, 250);
112 }
113
114 void ECSubWriteReply::encode(bufferlist &bl) const
115 {
116   ENCODE_START(1, 1, bl);
117   ::encode(from, bl);
118   ::encode(tid, bl);
119   ::encode(last_complete, bl);
120   ::encode(committed, bl);
121   ::encode(applied, bl);
122   ENCODE_FINISH(bl);
123 }
124
125 void ECSubWriteReply::decode(bufferlist::iterator &bl)
126 {
127   DECODE_START(1, bl);
128   ::decode(from, bl);
129   ::decode(tid, bl);
130   ::decode(last_complete, bl);
131   ::decode(committed, bl);
132   ::decode(applied, bl);
133   DECODE_FINISH(bl);
134 }
135
136 std::ostream &operator<<(
137   std::ostream &lhs, const ECSubWriteReply &rhs)
138 {
139   return lhs
140     << "ECSubWriteReply(tid=" << rhs.tid
141     << ", last_complete=" << rhs.last_complete
142     << ", committed=" << rhs.committed
143     << ", applied=" << rhs.applied << ")";
144 }
145
146 void ECSubWriteReply::dump(Formatter *f) const
147 {
148   f->dump_unsigned("tid", tid);
149   f->dump_stream("last_complete") << last_complete;
150   f->dump_bool("committed", committed);
151   f->dump_bool("applied", applied);
152 }
153
154 void ECSubWriteReply::generate_test_instances(list<ECSubWriteReply*>& o)
155 {
156   o.push_back(new ECSubWriteReply());
157   o.back()->tid = 20;
158   o.back()->last_complete = eversion_t(100, 2000);
159   o.back()->committed = true;
160   o.push_back(new ECSubWriteReply());
161   o.back()->tid = 80;
162   o.back()->last_complete = eversion_t(50, 200);
163   o.back()->applied = true;
164 }
165
166 void ECSubRead::encode(bufferlist &bl, uint64_t features) const
167 {
168   if ((features & CEPH_FEATURE_OSD_FADVISE_FLAGS) == 0) {
169     ENCODE_START(1, 1, bl);
170     ::encode(from, bl);
171     ::encode(tid, bl);
172     map<hobject_t, list<pair<uint64_t, uint64_t> >> tmp;
173     for (map<hobject_t, list<boost::tuple<uint64_t, uint64_t, uint32_t> >>::const_iterator m = to_read.begin();
174           m != to_read.end(); ++m) {
175       list<pair<uint64_t, uint64_t> > tlist;
176       for (list<boost::tuple<uint64_t, uint64_t, uint32_t> >::const_iterator l = m->second.begin();
177             l != m->second.end(); ++l) {
178         tlist.push_back(std::make_pair(l->get<0>(), l->get<1>()));
179       }
180       tmp[m->first] = tlist;
181     }
182     ::encode(tmp, bl);
183     ::encode(attrs_to_read, bl);
184     ENCODE_FINISH(bl);
185     return;
186   }
187
188   ENCODE_START(2, 2, bl);
189   ::encode(from, bl);
190   ::encode(tid, bl);
191   ::encode(to_read, bl);
192   ::encode(attrs_to_read, bl);
193   ENCODE_FINISH(bl);
194 }
195
196 void ECSubRead::decode(bufferlist::iterator &bl)
197 {
198   DECODE_START(2, bl);
199   ::decode(from, bl);
200   ::decode(tid, bl);
201   if (struct_v == 1) {
202     map<hobject_t, list<pair<uint64_t, uint64_t> >>tmp;
203     ::decode(tmp, bl);
204     for (map<hobject_t, list<pair<uint64_t, uint64_t> >>::const_iterator m = tmp.begin();
205           m != tmp.end(); ++m) {
206       list<boost::tuple<uint64_t, uint64_t, uint32_t> > tlist;
207       for (list<pair<uint64_t, uint64_t> > ::const_iterator l = m->second.begin();
208             l != m->second.end(); ++l) {
209         tlist.push_back(boost::make_tuple(l->first, l->second, 0));
210       }
211       to_read[m->first] = tlist;
212     }
213   } else {
214     ::decode(to_read, bl);
215   }
216   ::decode(attrs_to_read, bl);
217   DECODE_FINISH(bl);
218 }
219
220 std::ostream &operator<<(
221   std::ostream &lhs, const ECSubRead &rhs)
222 {
223   return lhs
224     << "ECSubRead(tid=" << rhs.tid
225     << ", to_read=" << rhs.to_read
226     << ", attrs_to_read=" << rhs.attrs_to_read << ")";
227 }
228
229 void ECSubRead::dump(Formatter *f) const
230 {
231   f->dump_stream("from") << from;
232   f->dump_unsigned("tid", tid);
233   f->open_array_section("objects");
234   for (map<hobject_t, list<boost::tuple<uint64_t, uint64_t, uint32_t> >>::const_iterator i =
235          to_read.begin();
236        i != to_read.end();
237        ++i) {
238     f->open_object_section("object");
239     f->dump_stream("oid") << i->first;
240     f->open_array_section("extents");
241     for (list<boost::tuple<uint64_t, uint64_t, uint32_t> >::const_iterator j =
242            i->second.begin();
243          j != i->second.end();
244          ++j) {
245       f->open_object_section("extent");
246       f->dump_unsigned("off", j->get<0>());
247       f->dump_unsigned("len", j->get<1>());
248       f->dump_unsigned("flags", j->get<2>());
249       f->close_section();
250     }
251     f->close_section();
252     f->close_section();
253   }
254   f->close_section();
255
256   f->open_array_section("object_attrs_requested");
257   for (set<hobject_t>::const_iterator i = attrs_to_read.begin();
258        i != attrs_to_read.end();
259        ++i) {
260     f->open_object_section("object");
261     f->dump_stream("oid") << *i;
262     f->close_section();
263   }
264   f->close_section();
265 }
266
267 void ECSubRead::generate_test_instances(list<ECSubRead*>& o)
268 {
269   hobject_t hoid1(sobject_t("asdf", 1));
270   hobject_t hoid2(sobject_t("asdf2", CEPH_NOSNAP));
271   o.push_back(new ECSubRead());
272   o.back()->from = pg_shard_t(2, shard_id_t(-1));
273   o.back()->tid = 1;
274   o.back()->to_read[hoid1].push_back(boost::make_tuple(100, 200, 0));
275   o.back()->to_read[hoid1].push_back(boost::make_tuple(400, 600, 0));
276   o.back()->to_read[hoid2].push_back(boost::make_tuple(400, 600, 0));
277   o.back()->attrs_to_read.insert(hoid1);
278   o.push_back(new ECSubRead());
279   o.back()->from = pg_shard_t(2, shard_id_t(-1));
280   o.back()->tid = 300;
281   o.back()->to_read[hoid1].push_back(boost::make_tuple(300, 200, 0));
282   o.back()->to_read[hoid2].push_back(boost::make_tuple(400, 600, 0));
283   o.back()->to_read[hoid2].push_back(boost::make_tuple(2000, 600, 0));
284   o.back()->attrs_to_read.insert(hoid2);
285 }
286
287 void ECSubReadReply::encode(bufferlist &bl) const
288 {
289   ENCODE_START(1, 1, bl);
290   ::encode(from, bl);
291   ::encode(tid, bl);
292   ::encode(buffers_read, bl);
293   ::encode(attrs_read, bl);
294   ::encode(errors, bl);
295   ENCODE_FINISH(bl);
296 }
297
298 void ECSubReadReply::decode(bufferlist::iterator &bl)
299 {
300   DECODE_START(1, bl);
301   ::decode(from, bl);
302   ::decode(tid, bl);
303   ::decode(buffers_read, bl);
304   ::decode(attrs_read, bl);
305   ::decode(errors, bl);
306   DECODE_FINISH(bl);
307 }
308
309 std::ostream &operator<<(
310   std::ostream &lhs, const ECSubReadReply &rhs)
311 {
312   return lhs
313     << "ECSubReadReply(tid=" << rhs.tid
314     << ", attrs_read=" << rhs.attrs_read.size()
315     << ")";
316 }
317
318 void ECSubReadReply::dump(Formatter *f) const
319 {
320   f->dump_stream("from") << from;
321   f->dump_unsigned("tid", tid);
322   f->open_array_section("buffers_read");
323   for (map<hobject_t, list<pair<uint64_t, bufferlist> >>::const_iterator i =
324          buffers_read.begin();
325        i != buffers_read.end();
326        ++i) {
327     f->open_object_section("object");
328     f->dump_stream("oid") << i->first;
329     f->open_array_section("data");
330     for (list<pair<uint64_t, bufferlist> >::const_iterator j =
331            i->second.begin();
332          j != i->second.end();
333          ++j) {
334       f->open_object_section("extent");
335       f->dump_unsigned("off", j->first);
336       f->dump_unsigned("buf_len", j->second.length());
337       f->close_section();
338     }
339     f->close_section();
340     f->close_section();
341   }
342   f->close_section();
343
344   f->open_array_section("attrs_returned");
345   for (map<hobject_t, map<string, bufferlist>>::const_iterator i =
346          attrs_read.begin();
347        i != attrs_read.end();
348        ++i) {
349     f->open_object_section("object_attrs");
350     f->dump_stream("oid") << i->first;
351     f->open_array_section("attrs");
352     for (map<string, bufferlist>::const_iterator j = i->second.begin();
353          j != i->second.end();
354          ++j) {
355       f->open_object_section("attr");
356       f->dump_string("attr", j->first);
357       f->dump_unsigned("val_len", j->second.length());
358       f->close_section();
359     }
360     f->close_section();
361     f->close_section();
362   }
363   f->close_section();
364
365   f->open_array_section("errors");
366   for (map<hobject_t, int>::const_iterator i = errors.begin();
367        i != errors.end();
368        ++i) {
369     f->open_object_section("error_pair");
370     f->dump_stream("oid") << i->first;
371     f->dump_int("error", i->second);
372     f->close_section();
373   }
374   f->close_section();
375 }
376
377 void ECSubReadReply::generate_test_instances(list<ECSubReadReply*>& o)
378 {
379   hobject_t hoid1(sobject_t("asdf", 1));
380   hobject_t hoid2(sobject_t("asdf2", CEPH_NOSNAP));
381   bufferlist bl;
382   bl.append_zero(100);
383   bufferlist bl2;
384   bl2.append_zero(200);
385   o.push_back(new ECSubReadReply());
386   o.back()->from = pg_shard_t(2, shard_id_t(-1));
387   o.back()->tid = 1;
388   o.back()->buffers_read[hoid1].push_back(make_pair(20, bl));
389   o.back()->buffers_read[hoid1].push_back(make_pair(2000, bl2));
390   o.back()->buffers_read[hoid2].push_back(make_pair(0, bl));
391   o.back()->attrs_read[hoid1]["foo"] = bl;
392   o.back()->attrs_read[hoid1]["_"] = bl2;
393   o.push_back(new ECSubReadReply());
394   o.back()->from = pg_shard_t(2, shard_id_t(-1));
395   o.back()->tid = 300;
396   o.back()->buffers_read[hoid2].push_back(make_pair(0, bl2));
397   o.back()->attrs_read[hoid2]["foo"] = bl;
398   o.back()->attrs_read[hoid2]["_"] = bl2;
399   o.back()->errors[hoid1] = -2;
400 }