Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / cls / log / cls_log_client.cc
1 #include <errno.h>
2
3 #include "cls/log/cls_log_ops.h"
4 #include "include/rados/librados.hpp"
5 #include "include/compat.h"
6
7
8 using namespace librados;
9
10
11
12 void cls_log_add(librados::ObjectWriteOperation& op, list<cls_log_entry>& entries, bool monotonic_inc)
13 {
14   bufferlist in;
15   cls_log_add_op call;
16   call.entries = entries;
17   ::encode(call, in);
18   op.exec("log", "add", in);
19 }
20
21 void cls_log_add(librados::ObjectWriteOperation& op, cls_log_entry& entry)
22 {
23   bufferlist in;
24   cls_log_add_op call;
25   call.entries.push_back(entry);
26   ::encode(call, in);
27   op.exec("log", "add", in);
28 }
29
30 void cls_log_add_prepare_entry(cls_log_entry& entry, const utime_t& timestamp,
31                  const string& section, const string& name, bufferlist& bl)
32 {
33   entry.timestamp = timestamp;
34   entry.section = section;
35   entry.name = name;
36   entry.data = bl;
37 }
38
39 void cls_log_add(librados::ObjectWriteOperation& op, const utime_t& timestamp,
40                  const string& section, const string& name, bufferlist& bl)
41 {
42   cls_log_entry entry;
43
44   cls_log_add_prepare_entry(entry, timestamp, section, name, bl);
45   cls_log_add(op, entry);
46 }
47
48 void cls_log_trim(librados::ObjectWriteOperation& op, const utime_t& from_time, const utime_t& to_time,
49                   const string& from_marker, const string& to_marker)
50 {
51   bufferlist in;
52   cls_log_trim_op call;
53   call.from_time = from_time;
54   call.to_time = to_time;
55   call.from_marker = from_marker;
56   call.to_marker = to_marker;
57   ::encode(call, in);
58   op.exec("log", "trim", in);
59 }
60
61 int cls_log_trim(librados::IoCtx& io_ctx, const string& oid, const utime_t& from_time, const utime_t& to_time,
62                  const string& from_marker, const string& to_marker)
63 {
64   bool done = false;
65
66   do {
67     ObjectWriteOperation op;
68
69     cls_log_trim(op, from_time, to_time, from_marker, to_marker);
70
71     int r = io_ctx.operate(oid, &op);
72     if (r == -ENODATA)
73       done = true;
74     else if (r < 0)
75       return r;
76
77   } while (!done);
78
79
80   return 0;
81 }
82
83 class LogListCtx : public ObjectOperationCompletion {
84   list<cls_log_entry> *entries;
85   string *marker;
86   bool *truncated;
87 public:
88   LogListCtx(list<cls_log_entry> *_entries, string *_marker, bool *_truncated) :
89                                       entries(_entries), marker(_marker), truncated(_truncated) {}
90   void handle_completion(int r, bufferlist& outbl) override {
91     if (r >= 0) {
92       cls_log_list_ret ret;
93       try {
94         bufferlist::iterator iter = outbl.begin();
95         ::decode(ret, iter);
96         if (entries)
97           *entries = std::move(ret.entries);
98         if (truncated)
99           *truncated = ret.truncated;
100         if (marker)
101           *marker = std::move(ret.marker);
102       } catch (buffer::error& err) {
103         // nothing we can do about it atm
104       }
105     }
106   }
107 };
108
109 void cls_log_list(librados::ObjectReadOperation& op, utime_t& from, utime_t& to,
110                   const string& in_marker, int max_entries,
111                   list<cls_log_entry>& entries,
112                   string *out_marker, bool *truncated)
113 {
114   bufferlist inbl;
115   cls_log_list_op call;
116   call.from_time = from;
117   call.to_time = to;
118   call.marker = in_marker;
119   call.max_entries = max_entries;
120
121   ::encode(call, inbl);
122
123   op.exec("log", "list", inbl, new LogListCtx(&entries, out_marker, truncated));
124 }
125
126 class LogInfoCtx : public ObjectOperationCompletion {
127   cls_log_header *header;
128 public:
129   explicit LogInfoCtx(cls_log_header *_header) : header(_header) {}
130   void handle_completion(int r, bufferlist& outbl) override {
131     if (r >= 0) {
132       cls_log_info_ret ret;
133       try {
134         bufferlist::iterator iter = outbl.begin();
135         ::decode(ret, iter);
136         if (header)
137           *header = ret.header;
138       } catch (buffer::error& err) {
139         // nothing we can do about it atm
140       }
141     }
142   }
143 };
144
145 void cls_log_info(librados::ObjectReadOperation& op, cls_log_header *header)
146 {
147   bufferlist inbl;
148   cls_log_info_op call;
149
150   ::encode(call, inbl);
151
152   op.exec("log", "info", inbl, new LogInfoCtx(header));
153 }
154