]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_metadata.h
97d4f0487606df09e275eb1da952f54ed2e4875f
[ceph.git] / ceph / src / rgw / rgw_metadata.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
3
4 #ifndef CEPH_RGW_METADATA_H
5 #define CEPH_RGW_METADATA_H
6
7 #include <string>
8 #include <utility>
9 #include <boost/optional.hpp>
10
11 #include "include/types.h"
12 #include "rgw_common.h"
13 #include "rgw_period_history.h"
14 #include "rgw_mdlog_types.h"
15 #include "cls/version/cls_version_types.h"
16 #include "cls/log/cls_log_types.h"
17 #include "common/RefCountedObj.h"
18 #include "common/ceph_time.h"
19 #include "services/svc_meta_be.h"
20
21
22 namespace rgw { namespace sal {
23 class RGWRadosStore;
24 } }
25 class RGWCoroutine;
26 class JSONObj;
27 struct RGWObjVersionTracker;
28
29 struct obj_version;
30
31
32 class RGWMetadataObject {
33 protected:
34 obj_version objv;
35 ceph::real_time mtime;
36 std::map<string, bufferlist> *pattrs{nullptr};
37
38 public:
39 RGWMetadataObject() {}
40 virtual ~RGWMetadataObject() {}
41 obj_version& get_version();
42 real_time& get_mtime() { return mtime; }
43 void set_pattrs(std::map<string, bufferlist> *_pattrs) {
44 pattrs = _pattrs;
45 }
46 std::map<string, bufferlist> *get_pattrs() {
47 return pattrs;
48 }
49
50 virtual void dump(Formatter *f) const {}
51 };
52
53 class RGWMetadataManager;
54
55 class RGWMetadataHandler {
56 friend class RGWMetadataManager;
57
58 protected:
59 CephContext *cct;
60
61 public:
62 RGWMetadataHandler() {}
63 virtual ~RGWMetadataHandler() {}
64 virtual string get_type() = 0;
65
66 void base_init(CephContext *_cct) {
67 cct = _cct;
68 }
69
70 virtual RGWMetadataObject *get_meta_obj(JSONObj *jo, const obj_version& objv, const ceph::real_time& mtime) = 0;
71
72 virtual int get(string& entry, RGWMetadataObject **obj, optional_yield) = 0;
73 virtual int put(string& entry,
74 RGWMetadataObject *obj,
75 RGWObjVersionTracker& objv_tracker,
76 optional_yield, RGWMDLogSyncType type,
77 bool from_remote_zone) = 0;
78 virtual int remove(string& entry, RGWObjVersionTracker& objv_tracker, optional_yield) = 0;
79
80 virtual int mutate(const string& entry,
81 const ceph::real_time& mtime,
82 RGWObjVersionTracker *objv_tracker,
83 optional_yield y,
84 RGWMDLogStatus op_type,
85 std::function<int()> f) = 0;
86
87 virtual int list_keys_init(const string& marker, void **phandle) = 0;
88 virtual int list_keys_next(void *handle, int max, list<string>& keys, bool *truncated) = 0;
89 virtual void list_keys_complete(void *handle) = 0;
90
91 virtual string get_marker(void *handle) = 0;
92
93 virtual int get_shard_id(const string& entry, int *shard_id) {
94 *shard_id = 0;
95 return 0;
96 }
97 virtual int attach(RGWMetadataManager *manager);
98 };
99
100 class RGWMetadataHandler_GenericMetaBE : public RGWMetadataHandler {
101 friend class RGWSI_MetaBackend;
102 friend class RGWMetadataManager;
103 friend class Put;
104
105 public:
106 class Put;
107
108 protected:
109 RGWSI_MetaBackend_Handler *be_handler;
110
111 virtual int do_get(RGWSI_MetaBackend_Handler::Op *op, string& entry, RGWMetadataObject **obj, optional_yield y) = 0;
112 virtual int do_put(RGWSI_MetaBackend_Handler::Op *op, string& entry, RGWMetadataObject *obj,
113 RGWObjVersionTracker& objv_tracker, optional_yield y,
114 RGWMDLogSyncType type, bool from_remote_zone) = 0;
115 virtual int do_put_operate(Put *put_op);
116 virtual int do_remove(RGWSI_MetaBackend_Handler::Op *op, string& entry, RGWObjVersionTracker& objv_tracker, optional_yield y) = 0;
117
118 public:
119 RGWMetadataHandler_GenericMetaBE() {}
120
121 void base_init(CephContext *_cct,
122 RGWSI_MetaBackend_Handler *_be_handler) {
123 RGWMetadataHandler::base_init(_cct);
124 be_handler = _be_handler;
125 }
126
127 RGWSI_MetaBackend_Handler *get_be_handler() {
128 return be_handler;
129 }
130
131 class Put {
132 protected:
133 RGWMetadataHandler_GenericMetaBE *handler;
134 RGWSI_MetaBackend_Handler::Op *op;
135 string& entry;
136 RGWMetadataObject *obj;
137 RGWObjVersionTracker& objv_tracker;
138 RGWMDLogSyncType apply_type;
139 optional_yield y;
140 bool from_remote_zone{false};
141
142 int get(RGWMetadataObject **obj) {
143 return handler->do_get(op, entry, obj, y);
144 }
145 public:
146 Put(RGWMetadataHandler_GenericMetaBE *_handler, RGWSI_MetaBackend_Handler::Op *_op,
147 string& _entry, RGWMetadataObject *_obj,
148 RGWObjVersionTracker& _objv_tracker, optional_yield _y,
149 RGWMDLogSyncType _type, bool from_remote_zone);
150
151 virtual ~Put() {}
152
153 virtual int put_pre() {
154 return 0;
155 }
156 virtual int put() {
157 return 0;
158 }
159 virtual int put_post() {
160 return 0;
161 }
162 virtual int finalize() {
163 return 0;
164 }
165 };
166
167 int get(string& entry, RGWMetadataObject **obj, optional_yield) override;
168 int put(string& entry, RGWMetadataObject *obj, RGWObjVersionTracker& objv_tracker, optional_yield, RGWMDLogSyncType type, bool from_remote_zone) override;
169 int remove(string& entry, RGWObjVersionTracker& objv_tracker, optional_yield) override;
170
171 int mutate(const string& entry,
172 const ceph::real_time& mtime,
173 RGWObjVersionTracker *objv_tracker,
174 optional_yield y,
175 RGWMDLogStatus op_type,
176 std::function<int()> f) override;
177
178 int get_shard_id(const string& entry, int *shard_id) override;
179
180 int list_keys_init(const std::string& marker, void **phandle) override;
181 int list_keys_next(void *handle, int max, std::list<string>& keys, bool *truncated) override;
182 void list_keys_complete(void *handle) override;
183
184 std::string get_marker(void *handle) override;
185
186 /**
187 * Compare an incoming versus on-disk tag/version+mtime combo against
188 * the sync mode to see if the new one should replace the on-disk one.
189 *
190 * @return true if the update should proceed, false otherwise.
191 */
192 static bool check_versions(bool exists,
193 const obj_version& ondisk, const real_time& ondisk_time,
194 const obj_version& incoming, const real_time& incoming_time,
195 RGWMDLogSyncType sync_mode) {
196 switch (sync_mode) {
197 case APPLY_UPDATES:
198 if ((ondisk.tag != incoming.tag) ||
199 (ondisk.ver >= incoming.ver))
200 return false;
201 break;
202 case APPLY_NEWER:
203 if (ondisk_time >= incoming_time)
204 return false;
205 break;
206 case APPLY_EXCLUSIVE:
207 if (exists)
208 return false;
209 break;
210 case APPLY_ALWAYS: //deliberate fall-thru -- we always apply!
211 default: break;
212 }
213 return true;
214 }
215 };
216
217 class RGWMetadataTopHandler;
218
219 class RGWMetadataManager {
220 friend class RGWMetadataHandler;
221
222 CephContext *cct;
223 RGWSI_Meta *meta_svc;
224 map<string, RGWMetadataHandler *> handlers;
225 std::unique_ptr<RGWMetadataTopHandler> md_top_handler;
226
227 int find_handler(const string& metadata_key, RGWMetadataHandler **handler, string& entry);
228 int register_handler(RGWMetadataHandler *handler);
229
230 public:
231 RGWMetadataManager(RGWSI_Meta *_meta_svc);
232 ~RGWMetadataManager();
233
234 RGWMetadataHandler *get_handler(const string& type);
235
236 int get(string& metadata_key, Formatter *f, optional_yield y);
237 int put(string& metadata_key, bufferlist& bl, optional_yield y,
238 RGWMDLogSyncType sync_mode,
239 bool from_remote_zone,
240 obj_version *existing_version = NULL);
241 int remove(string& metadata_key, optional_yield y);
242
243 int mutate(const string& metadata_key,
244 const ceph::real_time& mtime,
245 RGWObjVersionTracker *objv_tracker,
246 optional_yield y,
247 RGWMDLogStatus op_type,
248 std::function<int()> f);
249
250 int list_keys_init(const string& section, void **phandle);
251 int list_keys_init(const string& section, const string& marker, void **phandle);
252 int list_keys_next(void *handle, int max, list<string>& keys, bool *truncated);
253 void list_keys_complete(void *handle);
254
255 string get_marker(void *handle);
256
257 void dump_log_entry(cls_log_entry& entry, Formatter *f);
258
259 void get_sections(list<string>& sections);
260
261 void parse_metadata_key(const string& metadata_key, string& type, string& entry);
262
263 int get_shard_id(const string& section, const string& key, int *shard_id);
264 };
265
266 class RGWMetadataHandlerPut_SObj : public RGWMetadataHandler_GenericMetaBE::Put
267 {
268 protected:
269 std::unique_ptr<RGWMetadataObject> oo;
270 RGWMetadataObject *old_obj{nullptr};
271 bool exists{false};
272
273 public:
274 RGWMetadataHandlerPut_SObj(RGWMetadataHandler_GenericMetaBE *handler, RGWSI_MetaBackend_Handler::Op *op,
275 string& entry, RGWMetadataObject *obj, RGWObjVersionTracker& objv_tracker,
276 optional_yield y,
277 RGWMDLogSyncType type, bool from_remote_zone);
278 ~RGWMetadataHandlerPut_SObj();
279
280 int put_pre() override;
281 int put() override;
282 virtual int put_check() {
283 return 0;
284 }
285 virtual int put_checked();
286 virtual void encode_obj(bufferlist *bl) {}
287 };
288
289
290 #endif