]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_lc.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rgw / rgw_lc.h
1 #ifndef CEPH_RGW_LC_H
2 #define CEPH_RGW_LC_H
3
4 #include <map>
5 #include <string>
6 #include <iostream>
7 #include <include/types.h>
8
9 #include "common/debug.h"
10
11 #include "include/types.h"
12 #include "include/rados/librados.hpp"
13 #include "common/Mutex.h"
14 #include "common/Cond.h"
15 #include "common/Thread.h"
16 #include "rgw_common.h"
17 #include "rgw_rados.h"
18 #include "rgw_multi.h"
19 #include "cls/rgw/cls_rgw_types.h"
20
21 #include <atomic>
22
23 using namespace std;
24 #define HASH_PRIME 7877
25 #define MAX_ID_LEN 255
26 static string lc_oid_prefix = "lc";
27 static string lc_index_lock_name = "lc_process";
28
29 extern const char* LC_STATUS[];
30
31 typedef enum {
32 lc_uninitial = 0,
33 lc_processing,
34 lc_failed,
35 lc_complete,
36 }LC_BUCKET_STATUS;
37
38 class LCExpiration
39 {
40 protected:
41 string days;
42 public:
43 LCExpiration() {}
44 ~LCExpiration() {}
45
46 void encode(bufferlist& bl) const {
47 ENCODE_START(2, 2, bl);
48 ::encode(days, bl);
49 ENCODE_FINISH(bl);
50 }
51 void decode(bufferlist::iterator& bl) {
52 DECODE_START_LEGACY_COMPAT_LEN(2, 2, 2, bl);
53 ::decode(days, bl);
54 DECODE_FINISH(bl);
55 }
56 void dump(Formatter *f) const;
57 // static void generate_test_instances(list<ACLOwner*>& o);
58 void set_days(const string& _days) { days = _days; }
59 int get_days() {return atoi(days.c_str()); }
60 bool empty() const{
61 return days.empty();
62 }
63 };
64 WRITE_CLASS_ENCODER(LCExpiration)
65
66 class LCRule
67 {
68 protected:
69 string id;
70 string prefix;
71 string status;
72 LCExpiration expiration;
73 LCExpiration noncur_expiration;
74 LCExpiration mp_expiration;
75
76 public:
77
78 LCRule(){};
79 ~LCRule(){};
80
81 bool get_id(string& _id) {
82 _id = id;
83 return true;
84 }
85
86 string& get_status() {
87 return status;
88 }
89
90 string& get_prefix() {
91 return prefix;
92 }
93
94 LCExpiration& get_expiration() {
95 return expiration;
96 }
97
98 LCExpiration& get_noncur_expiration() {
99 return noncur_expiration;
100 }
101
102 LCExpiration& get_mp_expiration() {
103 return mp_expiration;
104 }
105
106 void set_id(string*_id) {
107 id = *_id;
108 }
109
110 void set_prefix(string*_prefix) {
111 prefix = *_prefix;
112 }
113
114 void set_status(string*_status) {
115 status = *_status;
116 }
117
118 void set_expiration(LCExpiration*_expiration) {
119 expiration = *_expiration;
120 }
121
122 void set_noncur_expiration(LCExpiration*_noncur_expiration) {
123 noncur_expiration = *_noncur_expiration;
124 }
125
126 void set_mp_expiration(LCExpiration* _mp_expiration) {
127 mp_expiration = *_mp_expiration;
128 }
129
130 bool validate();
131
132 void encode(bufferlist& bl) const {
133 ENCODE_START(3, 1, bl);
134 ::encode(id, bl);
135 ::encode(prefix, bl);
136 ::encode(status, bl);
137 ::encode(expiration, bl);
138 ::encode(noncur_expiration, bl);
139 ::encode(mp_expiration, bl);
140 ENCODE_FINISH(bl);
141 }
142 void decode(bufferlist::iterator& bl) {
143 DECODE_START_LEGACY_COMPAT_LEN(3, 1, 1, bl);
144 ::decode(id, bl);
145 ::decode(prefix, bl);
146 ::decode(status, bl);
147 ::decode(expiration, bl);
148 if (struct_v >=2) {
149 ::decode(noncur_expiration, bl);
150 }
151 if (struct_v >= 3) {
152 ::decode(mp_expiration, bl);
153 }
154 DECODE_FINISH(bl);
155 }
156
157 };
158 WRITE_CLASS_ENCODER(LCRule)
159
160 struct lc_op
161 {
162 bool status;
163 int expiration;
164 int noncur_expiration;
165 int mp_expiration;
166
167 lc_op() : status(false), expiration(0), noncur_expiration(0), mp_expiration(0) {}
168
169 };
170
171 class RGWLifecycleConfiguration
172 {
173 protected:
174 CephContext *cct;
175 map<string, lc_op> prefix_map;
176 multimap<string, LCRule> rule_map;
177 bool _add_rule(LCRule *rule);
178 public:
179 RGWLifecycleConfiguration(CephContext *_cct) : cct(_cct) {}
180 RGWLifecycleConfiguration() : cct(NULL) {}
181
182 void set_ctx(CephContext *ctx) {
183 cct = ctx;
184 }
185
186 virtual ~RGWLifecycleConfiguration() {}
187
188 // int get_perm(string& id, int perm_mask);
189 // int get_group_perm(ACLGroupTypeEnum group, int perm_mask);
190 void encode(bufferlist& bl) const {
191 ENCODE_START(1, 1, bl);
192 ::encode(rule_map, bl);
193 ENCODE_FINISH(bl);
194 }
195 void decode(bufferlist::iterator& bl) {
196 DECODE_START_LEGACY_COMPAT_LEN(1, 1, 1, bl);
197 ::decode(rule_map, bl);
198 multimap<string, LCRule>::iterator iter;
199 for (iter = rule_map.begin(); iter != rule_map.end(); ++iter) {
200 LCRule& rule = iter->second;
201 _add_rule(&rule);
202 }
203 DECODE_FINISH(bl);
204 }
205 void dump(Formatter *f) const;
206 // static void generate_test_instances(list<RGWAccessControlList*>& o);
207
208 void add_rule(LCRule* rule);
209
210 int check_and_add_rule(LCRule* rule);
211
212 bool validate();
213
214 multimap<string, LCRule>& get_rule_map() { return rule_map; }
215 map<string, lc_op>& get_prefix_map() { return prefix_map; }
216 /*
217 void create_default(string id, string name) {
218 ACLGrant grant;
219 grant.set_canon(id, name, RGW_PERM_FULL_CONTROL);
220 add_grant(&grant);
221 }
222 */
223 };
224 WRITE_CLASS_ENCODER(RGWLifecycleConfiguration)
225
226 class RGWLC {
227 CephContext *cct;
228 RGWRados *store;
229 int max_objs;
230 string *obj_names;
231 std::atomic<bool> down_flag = { false };
232 string cookie;
233
234 class LCWorker : public Thread {
235 CephContext *cct;
236 RGWLC *lc;
237 Mutex lock;
238 Cond cond;
239
240 public:
241 LCWorker(CephContext *_cct, RGWLC *_lc) : cct(_cct), lc(_lc), lock("LCWorker") {}
242 void *entry() override;
243 void stop();
244 bool should_work(utime_t& now);
245 int schedule_next_start_time(utime_t& start, utime_t& now);
246 };
247
248 public:
249 LCWorker *worker;
250 RGWLC() : cct(NULL), store(NULL), worker(NULL) {}
251 ~RGWLC() {
252 stop_processor();
253 finalize();
254 }
255
256 void initialize(CephContext *_cct, RGWRados *_store);
257 void finalize();
258
259 int process();
260 int process(int index, int max_secs);
261 bool if_already_run_today(time_t& start_date);
262 int list_lc_progress(const string& marker, uint32_t max_entries, map<string, int> *progress_map);
263 int bucket_lc_prepare(int index);
264 int bucket_lc_process(string& shard_id);
265 int bucket_lc_post(int index, int max_lock_sec, pair<string, int >& entry, int& result);
266 bool going_down();
267 void start_processor();
268 void stop_processor();
269
270 private:
271 int remove_expired_obj(RGWBucketInfo& bucket_info, rgw_obj_key obj_key, bool remove_indeed = true);
272 bool obj_has_expired(double timediff, int days);
273 int handle_multipart_expiration(RGWRados::Bucket *target, const map<string, lc_op>& prefix_map);
274 };
275
276
277
278 #endif