1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "include/int_types.h"
5 #include "include/buffer.h"
6 #include "include/encoding.h"
7 #include "common/errno.h"
8 #include "objclass/objclass.h"
9 #include "cls/journal/cls_journal_types.h"
20 static const uint64_t MAX_KEYS_READ
= 64;
22 static const std::string HEADER_KEY_ORDER
= "order";
23 static const std::string HEADER_KEY_SPLAY_WIDTH
= "splay_width";
24 static const std::string HEADER_KEY_POOL_ID
= "pool_id";
25 static const std::string HEADER_KEY_MINIMUM_SET
= "minimum_set";
26 static const std::string HEADER_KEY_ACTIVE_SET
= "active_set";
27 static const std::string HEADER_KEY_NEXT_TAG_TID
= "next_tag_tid";
28 static const std::string HEADER_KEY_NEXT_TAG_CLASS
= "next_tag_class";
29 static const std::string HEADER_KEY_CLIENT_PREFIX
= "client_";
30 static const std::string HEADER_KEY_TAG_PREFIX
= "tag_";
32 std::string
to_hex(uint64_t value
) {
33 std::ostringstream oss
;
34 oss
<< std::setw(16) << std::setfill('0') << std::hex
<< value
;
38 std::string
key_from_client_id(const std::string
&client_id
) {
39 return HEADER_KEY_CLIENT_PREFIX
+ client_id
;
42 std::string
key_from_tag_tid(uint64_t tag_tid
) {
43 return HEADER_KEY_TAG_PREFIX
+ to_hex(tag_tid
);
46 uint64_t tag_tid_from_key(const std::string
&key
) {
47 std::istringstream
iss(key
);
49 iss
.ignore(HEADER_KEY_TAG_PREFIX
.size()) >> std::hex
>> id
;
54 int read_key(cls_method_context_t hctx
, const string
&key
, T
*t
,
55 bool ignore_enoent
= false) {
57 int r
= cls_cxx_map_get_val(hctx
, key
, &bl
);
58 if (r
== -ENOENT
&& ignore_enoent
) {
61 CLS_ERR("failed to get omap key: %s", key
.c_str());
66 bufferlist::iterator iter
= bl
.begin();
68 } catch (const buffer::error
&err
) {
69 CLS_ERR("failed to decode input parameters: %s", err
.what());
76 int write_key(cls_method_context_t hctx
, const string
&key
, const T
&t
) {
80 int r
= cls_cxx_map_set_val(hctx
, key
, &bl
);
82 CLS_ERR("failed to set omap key: %s", key
.c_str());
88 int remove_key(cls_method_context_t hctx
, const string
&key
) {
89 int r
= cls_cxx_map_remove_key(hctx
, key
);
90 if (r
< 0 && r
!= -ENOENT
) {
91 CLS_ERR("failed to remove key: %s", key
.c_str());
97 int expire_tags(cls_method_context_t hctx
, const std::string
*skip_client_id
) {
99 std::string skip_client_key
;
100 if (skip_client_id
!= nullptr) {
101 skip_client_key
= key_from_client_id(*skip_client_id
);
104 uint64_t minimum_tag_tid
= std::numeric_limits
<uint64_t>::max();
105 std::string last_read
= HEADER_KEY_CLIENT_PREFIX
;
108 std::map
<std::string
, bufferlist
> vals
;
109 int r
= cls_cxx_map_get_vals(hctx
, last_read
, HEADER_KEY_CLIENT_PREFIX
,
110 MAX_KEYS_READ
, &vals
, &more
);
111 if (r
< 0 && r
!= -ENOENT
) {
112 CLS_ERR("failed to retrieve registered clients: %s",
113 cpp_strerror(r
).c_str());
117 for (auto &val
: vals
) {
118 // if we are removing a client, skip its commit positions
119 if (val
.first
== skip_client_key
) {
123 cls::journal::Client client
;
124 bufferlist::iterator iter
= val
.second
.begin();
126 ::decode(client
, iter
);
127 } catch (const buffer::error
&err
) {
128 CLS_ERR("error decoding registered client: %s",
133 for (auto object_position
: client
.commit_position
.object_positions
) {
134 minimum_tag_tid
= MIN(minimum_tag_tid
, object_position
.tag_tid
);
138 last_read
= vals
.rbegin()->first
;
142 // cannot expire tags if a client hasn't committed yet
143 if (minimum_tag_tid
== std::numeric_limits
<uint64_t>::max()) {
147 // compute the minimum in-use tag for each class
148 std::map
<uint64_t, uint64_t> minimum_tag_class_to_tids
;
149 typedef enum { TAG_PASS_CALCULATE_MINIMUMS
,
151 TAG_PASS_DONE
} TagPass
;
152 int tag_pass
= TAG_PASS_CALCULATE_MINIMUMS
;
153 last_read
= HEADER_KEY_TAG_PREFIX
;
155 std::map
<std::string
, bufferlist
> vals
;
156 int r
= cls_cxx_map_get_vals(hctx
, last_read
, HEADER_KEY_TAG_PREFIX
,
157 MAX_KEYS_READ
, &vals
, &more
);
158 if (r
< 0 && r
!= -ENOENT
) {
159 CLS_ERR("failed to retrieve tags: %s", cpp_strerror(r
).c_str());
163 for (auto &val
: vals
) {
164 cls::journal::Tag tag
;
165 bufferlist::iterator iter
= val
.second
.begin();
168 } catch (const buffer::error
&err
) {
169 CLS_ERR("error decoding tag: %s", val
.first
.c_str());
173 if (tag
.tid
!= tag_tid_from_key(val
.first
)) {
174 CLS_ERR("tag tid mismatched: %s", val
.first
.c_str());
178 if (tag_pass
== TAG_PASS_CALCULATE_MINIMUMS
) {
179 minimum_tag_class_to_tids
[tag
.tag_class
] = tag
.tid
;
180 } else if (tag_pass
== TAG_PASS_SCRUB
&&
181 tag
.tid
< minimum_tag_class_to_tids
[tag
.tag_class
]) {
182 r
= remove_key(hctx
, val
.first
);
188 if (tag
.tid
>= minimum_tag_tid
) {
189 // no need to check for tag classes beyond this point
196 if (tag_pass
!= TAG_PASS_DONE
&& !more
) {
197 last_read
= HEADER_KEY_TAG_PREFIX
;
199 } else if (!vals
.empty()) {
200 last_read
= vals
.rbegin()->first
;
202 } while (tag_pass
!= TAG_PASS_DONE
);
206 int get_client_list_range(cls_method_context_t hctx
,
207 std::set
<cls::journal::Client
> *clients
,
208 std::string start_after
, uint64_t max_return
) {
209 std::string last_read
;
210 if (!start_after
.empty()) {
211 last_read
= key_from_client_id(start_after
);
214 std::map
<std::string
, bufferlist
> vals
;
216 int r
= cls_cxx_map_get_vals(hctx
, last_read
, HEADER_KEY_CLIENT_PREFIX
,
217 max_return
, &vals
, &more
);
219 CLS_ERR("failed to retrieve omap values: %s", cpp_strerror(r
).c_str());
223 for (std::map
<std::string
, bufferlist
>::iterator it
= vals
.begin();
224 it
!= vals
.end(); ++it
) {
226 bufferlist::iterator iter
= it
->second
.begin();
228 cls::journal::Client client
;
229 ::decode(client
, iter
);
230 clients
->insert(client
);
231 } catch (const buffer::error
&err
) {
232 CLS_ERR("could not decode client '%s': %s", it
->first
.c_str(),
241 int find_min_commit_position(cls_method_context_t hctx
,
242 cls::journal::ObjectSetPosition
*minset
) {
245 std::string start_after
= "";
246 uint64_t tag_tid
= 0, entry_tid
= 0;
249 std::set
<cls::journal::Client
> batch
;
251 r
= get_client_list_range(hctx
, &batch
, start_after
, cls::journal::JOURNAL_MAX_RETURN
);
252 if ((r
< 0) || batch
.empty()) {
256 start_after
= batch
.rbegin()->id
;
258 // update the (minimum) commit position from this batch of clients
259 for(std::set
<cls::journal::Client
>::iterator it
= batch
.begin();
260 it
!= batch
.end(); ++it
) {
261 cls::journal::ObjectSetPosition object_set_position
= (*it
).commit_position
;
262 if (object_set_position
.object_positions
.empty()) {
263 *minset
= cls::journal::ObjectSetPosition();
266 cls::journal::ObjectPosition first
= object_set_position
.object_positions
.front();
268 // least tag_tid (or least entry_tid for matching tag_tid)
269 if (!valid
|| (tag_tid
> first
.tag_tid
) || ((tag_tid
== first
.tag_tid
) && (entry_tid
> first
.entry_tid
))) {
270 tag_tid
= first
.tag_tid
;
271 entry_tid
= first
.entry_tid
;
272 *minset
= cls::journal::ObjectSetPosition(object_set_position
);
277 // got the last batch, we're done
278 if (batch
.size() < cls::journal::JOURNAL_MAX_RETURN
) {
286 } // anonymous namespace
290 * @param order (uint8_t) - bits to shift to compute the object max size
291 * @param splay width (uint8_t) - number of active journal objects
294 * @returns 0 on success, negative error code on failure
296 int journal_create(cls_method_context_t hctx
, bufferlist
*in
, bufferlist
*out
) {
301 bufferlist::iterator iter
= in
->begin();
302 ::decode(order
, iter
);
303 ::decode(splay_width
, iter
);
304 ::decode(pool_id
, iter
);
305 } catch (const buffer::error
&err
) {
306 CLS_ERR("failed to decode input parameters: %s", err
.what());
310 bufferlist stored_orderbl
;
311 int r
= cls_cxx_map_get_val(hctx
, HEADER_KEY_ORDER
, &stored_orderbl
);
313 CLS_ERR("journal already exists");
315 } else if (r
!= -ENOENT
) {
319 r
= write_key(hctx
, HEADER_KEY_ORDER
, order
);
324 r
= write_key(hctx
, HEADER_KEY_SPLAY_WIDTH
, splay_width
);
329 r
= write_key(hctx
, HEADER_KEY_POOL_ID
, pool_id
);
334 uint64_t object_set
= 0;
335 r
= write_key(hctx
, HEADER_KEY_ACTIVE_SET
, object_set
);
340 r
= write_key(hctx
, HEADER_KEY_MINIMUM_SET
, object_set
);
346 r
= write_key(hctx
, HEADER_KEY_NEXT_TAG_TID
, tag_id
);
351 r
= write_key(hctx
, HEADER_KEY_NEXT_TAG_CLASS
, tag_id
);
364 * @returns 0 on success, negative error code on failure
366 int journal_get_order(cls_method_context_t hctx
, bufferlist
*in
,
369 int r
= read_key(hctx
, HEADER_KEY_ORDER
, &order
);
374 ::encode(order
, *out
);
384 * @returns 0 on success, negative error code on failure
386 int journal_get_splay_width(cls_method_context_t hctx
, bufferlist
*in
,
389 int r
= read_key(hctx
, HEADER_KEY_SPLAY_WIDTH
, &splay_width
);
394 ::encode(splay_width
, *out
);
404 * @returns 0 on success, negative error code on failure
406 int journal_get_pool_id(cls_method_context_t hctx
, bufferlist
*in
,
409 int r
= read_key(hctx
, HEADER_KEY_POOL_ID
, &pool_id
);
414 ::encode(pool_id
, *out
);
423 * object set (uint64_t)
424 * @returns 0 on success, negative error code on failure
426 int journal_get_minimum_set(cls_method_context_t hctx
, bufferlist
*in
,
428 uint64_t minimum_set
;
429 int r
= read_key(hctx
, HEADER_KEY_MINIMUM_SET
, &minimum_set
);
434 ::encode(minimum_set
, *out
);
440 * @param object set (uint64_t)
443 * @returns 0 on success, negative error code on failure
445 int journal_set_minimum_set(cls_method_context_t hctx
, bufferlist
*in
,
449 bufferlist::iterator iter
= in
->begin();
450 ::decode(object_set
, iter
);
451 } catch (const buffer::error
&err
) {
452 CLS_ERR("failed to decode input parameters: %s", err
.what());
456 uint64_t current_active_set
;
457 int r
= read_key(hctx
, HEADER_KEY_ACTIVE_SET
, ¤t_active_set
);
462 if (current_active_set
< object_set
) {
463 CLS_ERR("active object set earlier than minimum: %" PRIu64
464 " < %" PRIu64
, current_active_set
, object_set
);
468 uint64_t current_minimum_set
;
469 r
= read_key(hctx
, HEADER_KEY_MINIMUM_SET
, ¤t_minimum_set
);
474 if (object_set
== current_minimum_set
) {
476 } else if (object_set
< current_minimum_set
) {
477 CLS_ERR("object number earlier than current object: %" PRIu64
" < %" PRIu64
,
478 object_set
, current_minimum_set
);
482 r
= write_key(hctx
, HEADER_KEY_MINIMUM_SET
, object_set
);
494 * object set (uint64_t)
495 * @returns 0 on success, negative error code on failure
497 int journal_get_active_set(cls_method_context_t hctx
, bufferlist
*in
,
500 int r
= read_key(hctx
, HEADER_KEY_ACTIVE_SET
, &active_set
);
505 ::encode(active_set
, *out
);
511 * @param object set (uint64_t)
514 * @returns 0 on success, negative error code on failure
516 int journal_set_active_set(cls_method_context_t hctx
, bufferlist
*in
,
520 bufferlist::iterator iter
= in
->begin();
521 ::decode(object_set
, iter
);
522 } catch (const buffer::error
&err
) {
523 CLS_ERR("failed to decode input parameters: %s", err
.what());
527 uint64_t current_minimum_set
;
528 int r
= read_key(hctx
, HEADER_KEY_MINIMUM_SET
, ¤t_minimum_set
);
533 if (current_minimum_set
> object_set
) {
534 CLS_ERR("minimum object set later than active: %" PRIu64
535 " > %" PRIu64
, current_minimum_set
, object_set
);
539 uint64_t current_active_set
;
540 r
= read_key(hctx
, HEADER_KEY_ACTIVE_SET
, ¤t_active_set
);
545 if (object_set
== current_active_set
) {
547 } else if (object_set
< current_active_set
) {
548 CLS_ERR("object number earlier than current object: %" PRIu64
" < %" PRIu64
,
549 object_set
, current_active_set
);
553 r
= write_key(hctx
, HEADER_KEY_ACTIVE_SET
, object_set
);
562 * @param id (string) - unique client id
565 * cls::journal::Client
566 * @returns 0 on success, negative error code on failure
568 int journal_get_client(cls_method_context_t hctx
, bufferlist
*in
,
572 bufferlist::iterator iter
= in
->begin();
574 } catch (const buffer::error
&err
) {
575 CLS_ERR("failed to decode input parameters: %s", err
.what());
579 std::string
key(key_from_client_id(id
));
580 cls::journal::Client client
;
581 int r
= read_key(hctx
, key
, &client
);
586 ::encode(client
, *out
);
592 * @param id (string) - unique client id
593 * @param data (bufferlist) - opaque data associated to client
596 * @returns 0 on success, negative error code on failure
598 int journal_client_register(cls_method_context_t hctx
, bufferlist
*in
,
603 bufferlist::iterator iter
= in
->begin();
605 ::decode(data
, iter
);
606 } catch (const buffer::error
&err
) {
607 CLS_ERR("failed to decode input parameters: %s", err
.what());
612 int r
= read_key(hctx
, HEADER_KEY_ORDER
, &order
);
617 std::string
key(key_from_client_id(id
));
618 bufferlist stored_clientbl
;
619 r
= cls_cxx_map_get_val(hctx
, key
, &stored_clientbl
);
621 CLS_ERR("duplicate client id: %s", id
.c_str());
623 } else if (r
!= -ENOENT
) {
627 cls::journal::ObjectSetPosition minset
;
628 r
= find_min_commit_position(hctx
, &minset
);
632 cls::journal::Client
client(id
, data
, minset
);
633 r
= write_key(hctx
, key
, client
);
642 * @param id (string) - unique client id
643 * @param data (bufferlist) - opaque data associated to client
646 * @returns 0 on success, negative error code on failure
648 int journal_client_update_data(cls_method_context_t hctx
, bufferlist
*in
,
653 bufferlist::iterator iter
= in
->begin();
655 ::decode(data
, iter
);
656 } catch (const buffer::error
&err
) {
657 CLS_ERR("failed to decode input parameters: %s", err
.what());
661 std::string
key(key_from_client_id(id
));
662 cls::journal::Client client
;
663 int r
= read_key(hctx
, key
, &client
);
669 r
= write_key(hctx
, key
, client
);
678 * @param id (string) - unique client id
679 * @param state (uint8_t) - client state
682 * @returns 0 on success, negative error code on failure
684 int journal_client_update_state(cls_method_context_t hctx
, bufferlist
*in
,
687 cls::journal::ClientState state
;
690 bufferlist::iterator iter
= in
->begin();
693 ::decode(state_raw
, iter
);
694 state
= static_cast<cls::journal::ClientState
>(state_raw
);
695 } catch (const buffer::error
&err
) {
696 CLS_ERR("failed to decode input parameters: %s", err
.what());
700 std::string
key(key_from_client_id(id
));
701 cls::journal::Client client
;
702 int r
= read_key(hctx
, key
, &client
);
707 client
.state
= state
;
708 r
= write_key(hctx
, key
, client
);
717 * @param id (string) - unique client id
720 * @returns 0 on success, negative error code on failure
722 int journal_client_unregister(cls_method_context_t hctx
, bufferlist
*in
,
726 bufferlist::iterator iter
= in
->begin();
728 } catch (const buffer::error
&err
) {
729 CLS_ERR("failed to decode input parameters: %s", err
.what());
733 std::string
key(key_from_client_id(id
));
735 int r
= cls_cxx_map_get_val(hctx
, key
, &bl
);
737 CLS_ERR("client is not registered: %s", id
.c_str());
741 r
= cls_cxx_map_remove_key(hctx
, key
);
743 CLS_ERR("failed to remove omap key: %s", key
.c_str());
747 // prune expired tags
748 r
= expire_tags(hctx
, &id
);
757 * @param client_id (uint64_t) - unique client id
758 * @param commit_position (ObjectSetPosition)
761 * @returns 0 on success, negative error code on failure
763 int journal_client_commit(cls_method_context_t hctx
, bufferlist
*in
,
766 cls::journal::ObjectSetPosition commit_position
;
768 bufferlist::iterator iter
= in
->begin();
770 ::decode(commit_position
, iter
);
771 } catch (const buffer::error
&err
) {
772 CLS_ERR("failed to decode input parameters: %s", err
.what());
777 int r
= read_key(hctx
, HEADER_KEY_SPLAY_WIDTH
, &splay_width
);
781 if (commit_position
.object_positions
.size() > splay_width
) {
782 CLS_ERR("too many object positions");
786 std::string
key(key_from_client_id(id
));
787 cls::journal::Client client
;
788 r
= read_key(hctx
, key
, &client
);
793 if (client
.commit_position
== commit_position
) {
797 client
.commit_position
= commit_position
;
798 r
= write_key(hctx
, key
, client
);
807 * @param start_after (string)
808 * @param max_return (uint64_t)
811 * clients (set<cls::journal::Client>) - collection of registered clients
812 * @returns 0 on success, negative error code on failure
814 int journal_client_list(cls_method_context_t hctx
, bufferlist
*in
,
816 std::string start_after
;
819 bufferlist::iterator iter
= in
->begin();
820 ::decode(start_after
, iter
);
821 ::decode(max_return
, iter
);
822 } catch (const buffer::error
&err
) {
823 CLS_ERR("failed to decode input parameters: %s", err
.what());
827 std::set
<cls::journal::Client
> clients
;
828 int r
= get_client_list_range(hctx
, &clients
, start_after
, max_return
);
832 ::encode(clients
, *out
);
841 * @returns 0 on success, negative error code on failure
843 int journal_get_next_tag_tid(cls_method_context_t hctx
, bufferlist
*in
,
846 int r
= read_key(hctx
, HEADER_KEY_NEXT_TAG_TID
, &tag_tid
);
851 ::encode(tag_tid
, *out
);
857 * @param tag_tid (uint64_t)
861 * @returns 0 on success, negative error code on failure
863 int journal_get_tag(cls_method_context_t hctx
, bufferlist
*in
,
867 bufferlist::iterator iter
= in
->begin();
868 ::decode(tag_tid
, iter
);
869 } catch (const buffer::error
&err
) {
870 CLS_ERR("failed to decode input parameters: %s", err
.what());
874 std::string
key(key_from_tag_tid(tag_tid
));
875 cls::journal::Tag tag
;
876 int r
= read_key(hctx
, key
, &tag
);
887 * @param tag_tid (uint64_t)
888 * @param tag_class (uint64_t)
889 * @param data (bufferlist)
892 * @returns 0 on success, negative error code on failure
894 int journal_tag_create(cls_method_context_t hctx
, bufferlist
*in
,
900 bufferlist::iterator iter
= in
->begin();
901 ::decode(tag_tid
, iter
);
902 ::decode(tag_class
, iter
);
903 ::decode(data
, iter
);
904 } catch (const buffer::error
&err
) {
905 CLS_ERR("failed to decode input parameters: %s", err
.what());
909 std::string
key(key_from_tag_tid(tag_tid
));
910 bufferlist stored_tag_bl
;
911 int r
= cls_cxx_map_get_val(hctx
, key
, &stored_tag_bl
);
913 CLS_ERR("duplicate tag id: %" PRIu64
, tag_tid
);
915 } else if (r
!= -ENOENT
) {
919 // verify tag tid ordering
920 uint64_t next_tag_tid
;
921 r
= read_key(hctx
, HEADER_KEY_NEXT_TAG_TID
, &next_tag_tid
);
925 if (tag_tid
!= next_tag_tid
) {
926 CLS_LOG(5, "out-of-order tag sequence: %" PRIu64
, tag_tid
);
930 uint64_t next_tag_class
;
931 r
= read_key(hctx
, HEADER_KEY_NEXT_TAG_CLASS
, &next_tag_class
);
936 if (tag_class
== cls::journal::Tag::TAG_CLASS_NEW
) {
937 // allocate a new tag class
938 tag_class
= next_tag_class
;
939 r
= write_key(hctx
, HEADER_KEY_NEXT_TAG_CLASS
, tag_class
+ 1);
944 // verify tag class range
945 if (tag_class
>= next_tag_class
) {
946 CLS_ERR("out-of-sequence tag class: %" PRIu64
, tag_class
);
951 // prune expired tags
952 r
= expire_tags(hctx
, nullptr);
957 // update tag tid sequence
958 r
= write_key(hctx
, HEADER_KEY_NEXT_TAG_TID
, tag_tid
+ 1);
963 // write tag structure
964 cls::journal::Tag
tag(tag_tid
, tag_class
, data
);
965 key
= key_from_tag_tid(tag_tid
);
966 r
= write_key(hctx
, key
, tag
);
975 * @param start_after_tag_tid (uint64_t) - first tag tid
976 * @param max_return (uint64_t) - max tags to return
977 * @param client_id (std::string) - client id filter
978 * @param tag_class (boost::optional<uint64_t> - optional tag class filter
981 * std::set<cls::journal::Tag> - collection of tags
982 * @returns 0 on success, negative error code on failure
984 int journal_tag_list(cls_method_context_t hctx
, bufferlist
*in
,
986 uint64_t start_after_tag_tid
;
988 std::string client_id
;
989 boost::optional
<uint64_t> tag_class(0);
991 // handle compiler false positive about use-before-init
992 tag_class
= boost::none
;
994 bufferlist::iterator iter
= in
->begin();
995 ::decode(start_after_tag_tid
, iter
);
996 ::decode(max_return
, iter
);
997 ::decode(client_id
, iter
);
998 ::decode(tag_class
, iter
);
999 } catch (const buffer::error
&err
) {
1000 CLS_ERR("failed to decode input parameters: %s", err
.what());
1004 // calculate the minimum tag within client's commit position
1005 uint64_t minimum_tag_tid
= std::numeric_limits
<uint64_t>::max();
1006 cls::journal::Client client
;
1007 int r
= read_key(hctx
, key_from_client_id(client_id
), &client
);
1012 for (auto object_position
: client
.commit_position
.object_positions
) {
1013 minimum_tag_tid
= MIN(minimum_tag_tid
, object_position
.tag_tid
);
1016 // compute minimum tags in use per-class
1017 std::set
<cls::journal::Tag
> tags
;
1018 std::map
<uint64_t, uint64_t> minimum_tag_class_to_tids
;
1019 typedef enum { TAG_PASS_CALCULATE_MINIMUMS
,
1021 TAG_PASS_DONE
} TagPass
;
1022 int tag_pass
= (minimum_tag_tid
== std::numeric_limits
<uint64_t>::max() ?
1023 TAG_PASS_LIST
: TAG_PASS_CALCULATE_MINIMUMS
);
1024 std::string last_read
= HEADER_KEY_TAG_PREFIX
;
1026 std::map
<std::string
, bufferlist
> vals
;
1028 r
= cls_cxx_map_get_vals(hctx
, last_read
, HEADER_KEY_TAG_PREFIX
,
1029 MAX_KEYS_READ
, &vals
, &more
);
1030 if (r
< 0 && r
!= -ENOENT
) {
1031 CLS_ERR("failed to retrieve tags: %s", cpp_strerror(r
).c_str());
1035 for (auto &val
: vals
) {
1036 cls::journal::Tag tag
;
1037 bufferlist::iterator iter
= val
.second
.begin();
1039 ::decode(tag
, iter
);
1040 } catch (const buffer::error
&err
) {
1041 CLS_ERR("error decoding tag: %s", val
.first
.c_str());
1045 if (tag_pass
== TAG_PASS_CALCULATE_MINIMUMS
) {
1046 minimum_tag_class_to_tids
[tag
.tag_class
] = tag
.tid
;
1048 // completed calculation of tag class minimums
1049 if (tag
.tid
>= minimum_tag_tid
) {
1054 } else if (tag_pass
== TAG_PASS_LIST
) {
1055 if (start_after_tag_tid
!= 0 && tag
.tid
<= start_after_tag_tid
) {
1059 if (tag
.tid
>= minimum_tag_class_to_tids
[tag
.tag_class
] &&
1060 (!tag_class
|| *tag_class
== tag
.tag_class
)) {
1063 if (tags
.size() >= max_return
) {
1064 tag_pass
= TAG_PASS_DONE
;
1069 if (tag_pass
!= TAG_PASS_DONE
&& !more
) {
1070 last_read
= HEADER_KEY_TAG_PREFIX
;
1072 } else if (!vals
.empty()) {
1073 last_read
= vals
.rbegin()->first
;
1075 } while (tag_pass
!= TAG_PASS_DONE
);
1077 ::encode(tags
, *out
);
1083 * @param soft_max_size (uint64_t)
1086 * @returns 0 if object size less than max, negative error code otherwise
1088 int journal_object_guard_append(cls_method_context_t hctx
, bufferlist
*in
,
1090 uint64_t soft_max_size
;
1092 bufferlist::iterator iter
= in
->begin();
1093 ::decode(soft_max_size
, iter
);
1094 } catch (const buffer::error
&err
) {
1095 CLS_ERR("failed to decode input parameters: %s", err
.what());
1101 int r
= cls_cxx_stat(hctx
, &size
, &mtime
);
1105 CLS_ERR("failed to stat object: %s", cpp_strerror(r
).c_str());
1109 if (size
>= soft_max_size
) {
1110 CLS_LOG(5, "journal object full: %" PRIu64
" >= %" PRIu64
,
1111 size
, soft_max_size
);
1119 CLS_LOG(20, "Loaded journal class!");
1121 cls_handle_t h_class
;
1122 cls_method_handle_t h_journal_create
;
1123 cls_method_handle_t h_journal_get_order
;
1124 cls_method_handle_t h_journal_get_splay_width
;
1125 cls_method_handle_t h_journal_get_pool_id
;
1126 cls_method_handle_t h_journal_get_minimum_set
;
1127 cls_method_handle_t h_journal_set_minimum_set
;
1128 cls_method_handle_t h_journal_get_active_set
;
1129 cls_method_handle_t h_journal_set_active_set
;
1130 cls_method_handle_t h_journal_get_client
;
1131 cls_method_handle_t h_journal_client_register
;
1132 cls_method_handle_t h_journal_client_update_data
;
1133 cls_method_handle_t h_journal_client_update_state
;
1134 cls_method_handle_t h_journal_client_unregister
;
1135 cls_method_handle_t h_journal_client_commit
;
1136 cls_method_handle_t h_journal_client_list
;
1137 cls_method_handle_t h_journal_get_next_tag_tid
;
1138 cls_method_handle_t h_journal_get_tag
;
1139 cls_method_handle_t h_journal_tag_create
;
1140 cls_method_handle_t h_journal_tag_list
;
1141 cls_method_handle_t h_journal_object_guard_append
;
1143 cls_register("journal", &h_class
);
1145 /// methods for journal.$journal_id objects
1146 cls_register_cxx_method(h_class
, "create",
1147 CLS_METHOD_RD
| CLS_METHOD_WR
,
1148 journal_create
, &h_journal_create
);
1149 cls_register_cxx_method(h_class
, "get_order",
1151 journal_get_order
, &h_journal_get_order
);
1152 cls_register_cxx_method(h_class
, "get_splay_width",
1154 journal_get_splay_width
, &h_journal_get_splay_width
);
1155 cls_register_cxx_method(h_class
, "get_pool_id",
1157 journal_get_pool_id
, &h_journal_get_pool_id
);
1158 cls_register_cxx_method(h_class
, "get_minimum_set",
1160 journal_get_minimum_set
,
1161 &h_journal_get_minimum_set
);
1162 cls_register_cxx_method(h_class
, "set_minimum_set",
1163 CLS_METHOD_RD
| CLS_METHOD_WR
,
1164 journal_set_minimum_set
,
1165 &h_journal_set_minimum_set
);
1166 cls_register_cxx_method(h_class
, "get_active_set",
1168 journal_get_active_set
,
1169 &h_journal_get_active_set
);
1170 cls_register_cxx_method(h_class
, "set_active_set",
1171 CLS_METHOD_RD
| CLS_METHOD_WR
,
1172 journal_set_active_set
,
1173 &h_journal_set_active_set
);
1175 cls_register_cxx_method(h_class
, "get_client",
1177 journal_get_client
, &h_journal_get_client
);
1178 cls_register_cxx_method(h_class
, "client_register",
1179 CLS_METHOD_RD
| CLS_METHOD_WR
,
1180 journal_client_register
, &h_journal_client_register
);
1181 cls_register_cxx_method(h_class
, "client_update_data",
1182 CLS_METHOD_RD
| CLS_METHOD_WR
,
1183 journal_client_update_data
,
1184 &h_journal_client_update_data
);
1185 cls_register_cxx_method(h_class
, "client_update_state",
1186 CLS_METHOD_RD
| CLS_METHOD_WR
,
1187 journal_client_update_state
,
1188 &h_journal_client_update_state
);
1189 cls_register_cxx_method(h_class
, "client_unregister",
1190 CLS_METHOD_RD
| CLS_METHOD_WR
,
1191 journal_client_unregister
,
1192 &h_journal_client_unregister
);
1193 cls_register_cxx_method(h_class
, "client_commit",
1194 CLS_METHOD_RD
| CLS_METHOD_WR
,
1195 journal_client_commit
, &h_journal_client_commit
);
1196 cls_register_cxx_method(h_class
, "client_list",
1198 journal_client_list
, &h_journal_client_list
);
1200 cls_register_cxx_method(h_class
, "get_next_tag_tid",
1202 journal_get_next_tag_tid
,
1203 &h_journal_get_next_tag_tid
);
1204 cls_register_cxx_method(h_class
, "get_tag",
1206 journal_get_tag
, &h_journal_get_tag
);
1207 cls_register_cxx_method(h_class
, "tag_create",
1208 CLS_METHOD_RD
| CLS_METHOD_WR
,
1209 journal_tag_create
, &h_journal_tag_create
);
1210 cls_register_cxx_method(h_class
, "tag_list",
1212 journal_tag_list
, &h_journal_tag_list
);
1214 /// methods for journal_data.$journal_id.$object_id objects
1215 cls_register_cxx_method(h_class
, "guard_append",
1216 CLS_METHOD_RD
| CLS_METHOD_WR
,
1217 journal_object_guard_append
,
1218 &h_journal_object_guard_append
);