1 // -*- mode:C; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
6 #include "include/utime.h"
7 #include "objclass/objclass.h"
9 #include "cls_user_ops.h"
14 static int write_entry(cls_method_context_t hctx
, const string
& key
, const cls_user_bucket_entry
& entry
)
19 int ret
= cls_cxx_map_set_val(hctx
, key
, &bl
);
26 static int remove_entry(cls_method_context_t hctx
, const string
& key
)
28 int ret
= cls_cxx_map_remove_key(hctx
, key
);
35 static void get_key_by_bucket_name(const string
& bucket_name
, string
*key
)
40 static int get_existing_bucket_entry(cls_method_context_t hctx
, const string
& bucket_name
,
41 cls_user_bucket_entry
& entry
)
43 if (bucket_name
.empty()) {
48 get_key_by_bucket_name(bucket_name
, &key
);
51 int rc
= cls_cxx_map_get_val(hctx
, key
, &bl
);
53 CLS_LOG(10, "could not read entry %s", key
.c_str());
57 auto iter
= bl
.cbegin();
59 } catch (buffer::error
& err
) {
60 CLS_LOG(0, "ERROR: failed to decode entry %s", key
.c_str());
67 static int read_header(cls_method_context_t hctx
, cls_user_header
*header
)
71 int ret
= cls_cxx_map_read_header(hctx
, &bl
);
75 if (bl
.length() == 0) {
76 *header
= cls_user_header();
82 } catch (buffer::error
& err
) {
83 CLS_LOG(0, "ERROR: failed to decode user header");
90 static void add_header_stats(cls_user_stats
*stats
, cls_user_bucket_entry
& entry
)
92 stats
->total_entries
+= entry
.count
;
93 stats
->total_bytes
+= entry
.size
;
94 stats
->total_bytes_rounded
+= entry
.size_rounded
;
97 static void dec_header_stats(cls_user_stats
*stats
, cls_user_bucket_entry
& entry
)
99 stats
->total_bytes
-= entry
.size
;
100 stats
->total_bytes_rounded
-= entry
.size_rounded
;
101 stats
->total_entries
-= entry
.count
;
104 static void apply_entry_stats(const cls_user_bucket_entry
& src_entry
, cls_user_bucket_entry
*target_entry
)
106 target_entry
->size
= src_entry
.size
;
107 target_entry
->size_rounded
= src_entry
.size_rounded
;
108 target_entry
->count
= src_entry
.count
;
111 static int cls_user_set_buckets_info(cls_method_context_t hctx
, bufferlist
*in
, bufferlist
*out
)
113 auto in_iter
= in
->cbegin();
115 cls_user_set_buckets_op op
;
118 } catch (buffer::error
& err
) {
119 CLS_LOG(1, "ERROR: cls_user_add_op(): failed to decode op");
123 cls_user_header header
;
124 int ret
= read_header(hctx
, &header
);
126 CLS_LOG(0, "ERROR: failed to read user info header ret=%d", ret
);
130 for (list
<cls_user_bucket_entry
>::iterator iter
= op
.entries
.begin();
131 iter
!= op
.entries
.end(); ++iter
) {
132 cls_user_bucket_entry
& update_entry
= *iter
;
136 get_key_by_bucket_name(update_entry
.bucket
.name
, &key
);
138 cls_user_bucket_entry entry
;
139 ret
= get_existing_bucket_entry(hctx
, key
, entry
);
141 if (ret
== -ENOENT
) {
143 continue; /* racing bucket removal */
145 entry
= update_entry
;
149 // bucket id may have changed (ie reshard)
150 entry
.bucket
.bucket_id
= update_entry
.bucket
.bucket_id
;
151 // creation date may have changed (ie delete/recreate bucket)
152 entry
.creation_time
= update_entry
.creation_time
;
156 CLS_LOG(0, "ERROR: get_existing_bucket_entry() key=%s returned %d", key
.c_str(), ret
);
158 } else if (ret
>= 0 && entry
.user_stats_sync
) {
159 dec_header_stats(&header
.stats
, entry
);
162 CLS_LOG(20, "storing entry for key=%s size=%lld count=%lld",
163 key
.c_str(), (long long)update_entry
.size
, (long long)update_entry
.count
);
165 // sync entry stats when not an op.add, as when the case is op.add if its a
166 // new entry we already have copied update_entry earlier, OTOH, for an existing entry
167 // we end up clobbering the existing stats for the bucket
169 apply_entry_stats(update_entry
, &entry
);
171 entry
.user_stats_sync
= true;
173 ret
= write_entry(hctx
, key
, entry
);
177 add_header_stats(&header
.stats
, entry
);
182 CLS_LOG(20, "header: total bytes=%lld entries=%lld", (long long)header
.stats
.total_bytes
, (long long)header
.stats
.total_entries
);
184 if (header
.last_stats_update
< op
.time
)
185 header
.last_stats_update
= op
.time
;
189 ret
= cls_cxx_map_write_header(hctx
, &bl
);
196 static int cls_user_complete_stats_sync(cls_method_context_t hctx
, bufferlist
*in
, bufferlist
*out
)
198 auto in_iter
= in
->cbegin();
200 cls_user_complete_stats_sync_op op
;
203 } catch (buffer::error
& err
) {
204 CLS_LOG(1, "ERROR: cls_user_add_op(): failed to decode op");
208 cls_user_header header
;
209 int ret
= read_header(hctx
, &header
);
211 CLS_LOG(0, "ERROR: failed to read user info header ret=%d", ret
);
215 if (header
.last_stats_sync
< op
.time
)
216 header
.last_stats_sync
= op
.time
;
222 ret
= cls_cxx_map_write_header(hctx
, &bl
);
229 static int cls_user_remove_bucket(cls_method_context_t hctx
, bufferlist
*in
, bufferlist
*out
)
231 auto in_iter
= in
->cbegin();
233 cls_user_remove_bucket_op op
;
236 } catch (buffer::error
& err
) {
237 CLS_LOG(1, "ERROR: cls_user_add_op(): failed to decode op");
241 cls_user_header header
;
242 int ret
= read_header(hctx
, &header
);
244 CLS_LOG(0, "ERROR: failed to read user info header ret=%d", ret
);
250 get_key_by_bucket_name(op
.bucket
.name
, &key
);
252 cls_user_bucket_entry entry
;
253 ret
= get_existing_bucket_entry(hctx
, key
, entry
);
254 if (ret
== -ENOENT
) {
255 return 0; /* idempotent removal */
258 CLS_LOG(0, "ERROR: get existing bucket entry, key=%s ret=%d", key
.c_str(), ret
);
262 CLS_LOG(20, "removing entry at %s", key
.c_str());
264 ret
= remove_entry(hctx
, key
);
268 if (!entry
.user_stats_sync
) {
272 dec_header_stats(&header
.stats
, entry
);
274 CLS_LOG(20, "header: total bytes=%lld entries=%lld", (long long)header
.stats
.total_bytes
, (long long)header
.stats
.total_entries
);
278 return cls_cxx_map_write_header(hctx
, &bl
);
281 static int cls_user_list_buckets(cls_method_context_t hctx
, bufferlist
*in
, bufferlist
*out
)
283 auto in_iter
= in
->cbegin();
285 cls_user_list_buckets_op op
;
288 } catch (buffer::error
& err
) {
289 CLS_LOG(1, "ERROR: cls_user_list_op(): failed to decode op");
293 map
<string
, bufferlist
> keys
;
295 const string
& from_index
= op
.marker
;
296 const string
& to_index
= op
.end_marker
;
297 const bool to_index_valid
= !to_index
.empty();
299 #define MAX_ENTRIES 1000
300 size_t max_entries
= op
.max_entries
;
301 if (max_entries
> MAX_ENTRIES
)
302 max_entries
= MAX_ENTRIES
;
305 cls_user_list_buckets_ret ret
;
307 int rc
= cls_cxx_map_get_vals(hctx
, from_index
, match_prefix
, max_entries
, &keys
, &ret
.truncated
);
311 CLS_LOG(20, "from_index=%s to_index=%s match_prefix=%s",
314 match_prefix
.c_str());
316 list
<cls_user_bucket_entry
>& entries
= ret
.entries
;
317 map
<string
, bufferlist
>::iterator iter
= keys
.begin();
321 for (; iter
!= keys
.end(); ++iter
) {
322 const string
& index
= iter
->first
;
325 if (to_index_valid
&& to_index
.compare(index
) <= 0) {
326 ret
.truncated
= false;
330 bufferlist
& bl
= iter
->second
;
331 auto biter
= bl
.cbegin();
333 cls_user_bucket_entry e
;
335 entries
.push_back(e
);
336 } catch (buffer::error
& err
) {
337 CLS_LOG(0, "ERROR: cls_user_list: could not decode entry, index=%s", index
.c_str());
350 static int cls_user_get_header(cls_method_context_t hctx
, bufferlist
*in
, bufferlist
*out
)
352 auto in_iter
= in
->cbegin();
354 cls_user_get_header_op op
;
357 } catch (buffer::error
& err
) {
358 CLS_LOG(1, "ERROR: cls_user_get_header_op(): failed to decode op");
362 cls_user_get_header_ret op_ret
;
364 int ret
= read_header(hctx
, &op_ret
.header
);
368 encode(op_ret
, *out
);
373 /// A method to reset the user.buckets header stats in accordance to
374 /// the values seen in the user.buckets omap keys. This is not be
375 /// equivalent to --sync-stats which also re-calculates the stats for
377 static int cls_user_reset_stats(cls_method_context_t hctx
,
378 bufferlist
*in
, bufferlist
*out
/*ignore*/)
380 cls_user_reset_stats_op op
;
383 auto bliter
= in
->cbegin();
385 } catch (buffer::error
& err
) {
386 CLS_LOG(0, "ERROR: %s failed to decode op", __func__
);
390 cls_user_header header
;
391 bool truncated
= false;
392 string from_index
, prefix
;
394 map
<string
, bufferlist
> keys
;
395 int rc
= cls_cxx_map_get_vals(hctx
, from_index
, prefix
, MAX_ENTRIES
,
398 CLS_LOG(0, "ERROR: %s failed to retrieve omap key-values", __func__
);
401 CLS_LOG(20, "%s: read %lu key-values, truncated=%d",
402 __func__
, keys
.size(), truncated
);
404 for (const auto& kv
: keys
) {
405 cls_user_bucket_entry e
;
408 auto bliter
= bl
.cbegin();
410 } catch (buffer::error
& err
) {
411 CLS_LOG(0, "ERROR: %s failed to decode bucket entry for %s",
412 __func__
, kv
.first
.c_str());
415 add_header_stats(&header
.stats
, e
);
420 header
.last_stats_update
= op
.time
;
423 CLS_LOG(20, "%s: updating header", __func__
);
424 return cls_cxx_map_write_header(hctx
, &bl
);
429 CLS_LOG(1, "Loaded user class!");
431 cls_handle_t h_class
;
432 cls_method_handle_t h_user_set_buckets_info
;
433 cls_method_handle_t h_user_complete_stats_sync
;
434 cls_method_handle_t h_user_remove_bucket
;
435 cls_method_handle_t h_user_list_buckets
;
436 cls_method_handle_t h_user_get_header
;
437 cls_method_handle_t h_user_reset_stats
;
439 cls_register("user", &h_class
);
442 cls_register_cxx_method(h_class
, "set_buckets_info", CLS_METHOD_RD
| CLS_METHOD_WR
,
443 cls_user_set_buckets_info
, &h_user_set_buckets_info
);
444 cls_register_cxx_method(h_class
, "complete_stats_sync", CLS_METHOD_RD
| CLS_METHOD_WR
,
445 cls_user_complete_stats_sync
, &h_user_complete_stats_sync
);
446 cls_register_cxx_method(h_class
, "remove_bucket", CLS_METHOD_RD
| CLS_METHOD_WR
, cls_user_remove_bucket
, &h_user_remove_bucket
);
447 cls_register_cxx_method(h_class
, "list_buckets", CLS_METHOD_RD
, cls_user_list_buckets
, &h_user_list_buckets
);
448 cls_register_cxx_method(h_class
, "get_header", CLS_METHOD_RD
, cls_user_get_header
, &h_user_get_header
);
449 cls_register_cxx_method(h_class
, "reset_user_stats", CLS_METHOD_RD
| CLS_METHOD_WR
, cls_user_reset_stats
, &h_user_reset_stats
);