forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_metadata.cc
1288 lines (1084 loc) · 44.1 KB
/
token_metadata.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include "token_metadata.hh"
#include <optional>
#include "locator/snitch_base.hh"
#include "locator/abstract_replication_strategy.hh"
#include "locator/tablets.hh"
#include "log.hh"
#include "partition_range_compat.hh"
#include <unordered_map>
#include <algorithm>
#include <boost/icl/interval.hpp>
#include <boost/icl/interval_map.hpp>
#include <seastar/core/coroutine.hh>
#include <seastar/coroutine/maybe_yield.hh>
#include <boost/range/adaptors.hpp>
#include <seastar/core/smp.hh>
#include "utils/stall_free.hh"
#include "utils/fb_utilities.hh"
namespace locator {
static logging::logger tlogger("token_metadata");
template <typename C, typename V>
static void remove_by_value(C& container, V value) {
for (auto it = container.begin(); it != container.end();) {
if (it->second == value) {
it = container.erase(it);
} else {
it++;
}
}
}
class token_metadata_impl final {
public:
using inet_address = gms::inet_address;
private:
/**
* Maintains token to endpoint map of every node in the cluster.
* Each Token is associated with exactly one Address, but each Address may have
* multiple tokens. Hence, the BiMultiValMap collection.
*/
// FIXME: have to be BiMultiValMap
std::unordered_map<token, inet_address> _token_to_endpoint_map;
// Track the unique set of nodes in _token_to_endpoint_map
std::unordered_set<inet_address> _normal_token_owners;
std::unordered_map<token, inet_address> _bootstrap_tokens;
std::unordered_set<inet_address> _leaving_endpoints;
// The map between the existing node to be replaced and the replacing node
std::unordered_map<inet_address, inet_address> _replacing_endpoints;
std::optional<topology_change_info> _topology_change_info;
std::vector<token> _sorted_tokens;
tablet_metadata _tablets;
topology _topology;
token_metadata::read_new_t _read_new = token_metadata::read_new_t::no;
long _ring_version = 0;
static thread_local long _static_ring_version;
// Zero means that token_metadata versions are not supported,
// this will be used in RPC handling to decide whether we
// need to apply fencing or not.
// The initial valid version is 1;
token_metadata::version_t _version = 0;
token_metadata::version_tracker_t _version_tracker;
// Note: if any member is added to this class
// clone_async() must be updated to copy that member.
void sort_tokens();
const tablet_metadata& tablets() const { return _tablets; }
void set_tablets(tablet_metadata&& tablets) {
_tablets = std::move(tablets);
invalidate_cached_rings();
}
struct shallow_copy {};
public:
token_metadata_impl(shallow_copy, const token_metadata_impl& o) noexcept
: _topology(topology::config{})
{}
token_metadata_impl(token_metadata::config cfg) noexcept : _topology(std::move(cfg.topo_cfg)) {};
token_metadata_impl(const token_metadata_impl&) = delete; // it's too huge for direct copy, use clone_async()
token_metadata_impl(token_metadata_impl&&) noexcept = default;
const std::vector<token>& sorted_tokens() const;
future<> update_normal_tokens(std::unordered_set<token> tokens, inet_address endpoint);
const token& first_token(const token& start) const;
size_t first_token_index(const token& start) const;
std::optional<inet_address> get_endpoint(const token& token) const;
std::vector<token> get_tokens(const inet_address& addr) const;
const std::unordered_map<token, inet_address>& get_token_to_endpoint() const {
return _token_to_endpoint_map;
}
const std::unordered_set<inet_address>& get_leaving_endpoints() const {
return _leaving_endpoints;
}
const std::unordered_map<token, inet_address>& get_bootstrap_tokens() const {
return _bootstrap_tokens;
}
void update_topology(inet_address ep, endpoint_dc_rack dr, std::optional<node::state> opt_st) {
_topology.add_or_update_endpoint(ep, std::move(dr), std::move(opt_st));
}
/**
* Creates an iterable range of the sorted tokens starting at the token next
* after the given one.
*
* @param start A token that will define the beginning of the range
*
* @return The requested range (see the description above)
*/
boost::iterator_range<token_metadata::tokens_iterator> ring_range(const token& start) const;
boost::iterator_range<token_metadata::tokens_iterator> ring_range(dht::ring_position_view pos) const;
topology& get_topology() {
return _topology;
}
const topology& get_topology() const {
return _topology;
}
void debug_show() const;
/**
* Store an end-point to host ID mapping. Each ID must be unique, and
* cannot be changed after the fact.
*
* @param hostId
* @param endpoint
*/
void update_host_id(const host_id& host_id, inet_address endpoint);
/** Return the unique host ID for an end-point. */
host_id get_host_id(inet_address endpoint) const;
/// Return the unique host ID for an end-point or nullopt if not found.
std::optional<host_id> get_host_id_if_known(inet_address endpoint) const;
/** Return the end-point for a unique host ID */
std::optional<inet_address> get_endpoint_for_host_id(host_id) const;
/** @return a copy of the endpoint-to-id map for read-only operations */
std::unordered_map<inet_address, host_id> get_endpoint_to_host_id_map_for_reading() const;
void add_bootstrap_token(token t, inet_address endpoint);
void add_bootstrap_tokens(std::unordered_set<token> tokens, inet_address endpoint);
void remove_bootstrap_tokens(std::unordered_set<token> tokens);
void add_leaving_endpoint(inet_address endpoint);
void del_leaving_endpoint(inet_address endpoint);
public:
void remove_endpoint(inet_address endpoint);
bool is_normal_token_owner(inet_address endpoint) const;
bool is_leaving(inet_address endpoint) const;
// Is this node being replaced by another node
bool is_being_replaced(inet_address endpoint) const;
// Is any node being replaced by another node
bool is_any_node_being_replaced() const;
void add_replacing_endpoint(inet_address existing_node, inet_address replacing_node);
void del_replacing_endpoint(inet_address existing_node);
public:
/**
* Create a full copy of token_metadata_impl using asynchronous continuations.
* The caller must ensure that the cloned object will not change if
* the function yields.
*/
future<std::unique_ptr<token_metadata_impl>> clone_async() const noexcept;
/**
* Create a copy of TokenMetadata with only tokenToEndpointMap. That is, pending ranges,
* bootstrap tokens and leaving endpoints are not included in the copy.
* The caller must ensure that the cloned object will not change if
* the function yields.
*/
future<std::unique_ptr<token_metadata_impl>> clone_only_token_map(bool clone_sorted_tokens = true) const noexcept;
/**
* Create a copy of TokenMetadata with tokenToEndpointMap reflecting situation after all
* current leave operations have finished.
*
* @return new token metadata
*/
future<std::unique_ptr<token_metadata_impl>> clone_after_all_left() const noexcept {
return clone_only_token_map(false).then([this] (std::unique_ptr<token_metadata_impl> all_left_metadata) {
for (auto endpoint : _leaving_endpoints) {
all_left_metadata->remove_endpoint(endpoint);
}
all_left_metadata->sort_tokens();
return all_left_metadata;
});
}
/**
* Destroy the token_metadata members using continuations
* to prevent reactor stalls.
*/
future<> clear_gently() noexcept;
public:
dht::token_range_vector get_primary_ranges_for(std::unordered_set<token> tokens) const;
dht::token_range_vector get_primary_ranges_for(token right) const;
static boost::icl::interval<token>::interval_type range_to_interval(range<dht::token> r);
static range<dht::token> interval_to_range(boost::icl::interval<token>::interval_type i);
public:
future<> update_topology_change_info(dc_rack_fn& get_dc_rack);
const std::optional<topology_change_info>& get_topology_change_info() const {
return _topology_change_info;
}
public:
token get_predecessor(token t) const;
// Returns nodes that are officially part of the ring. It does not include
// node that is still joining the cluster, e.g., a node that is still
// streaming data before it finishes the bootstrap process and turns into
// NORMAL status.
const std::unordered_set<inet_address>& get_all_endpoints() const noexcept {
return _normal_token_owners;
}
/* Returns the number of different endpoints that own tokens in the ring.
* Bootstrapping tokens are not taken into account. */
size_t count_normal_token_owners() const;
private:
future<> update_normal_token_owners();
public:
// returns empty vector if keyspace_name not found.
inet_address_vector_topology_change pending_endpoints_for(const token& token, const sstring& keyspace_name) const;
std::optional<inet_address_vector_replica_set> endpoints_for_reading(const token& token, const sstring& keyspace_name) const;
void set_read_new(token_metadata::read_new_t read_new) {
_read_new = read_new;
}
public:
/** @return an endpoint to token multimap representation of tokenToEndpointMap (a copy) */
std::multimap<inet_address, token> get_endpoint_to_token_map_for_reading() const;
/**
* @return a (stable copy, won't be modified) Token to Endpoint map for all the normal and bootstrapping nodes
* in the cluster.
*/
std::map<token, inet_address> get_normal_and_bootstrapping_token_to_endpoint_map() const;
long get_ring_version() const {
return _ring_version;
}
void invalidate_cached_rings() {
_ring_version = ++_static_ring_version;
tlogger.debug("ring_version={}", _ring_version);
}
token_metadata::version_t get_version() const {
return _version;
}
void set_version(token_metadata::version_t version) {
if (version <= 0) {
on_internal_error(tlogger,
format("token_metadata_impl::set_version: invalid new version {}", version));
}
if (version < _version) {
on_internal_error(tlogger,
format("token_metadata_impl::set_version: new version can't be smaller than the previous one, "
"new version {}, previous version {}", version, _version));
}
_version = version;
}
void set_version_tracker(token_metadata::version_tracker_t tracker) {
_version_tracker = std::move(tracker);
}
friend class token_metadata;
};
thread_local long token_metadata_impl::_static_ring_version;
token_metadata::tokens_iterator::tokens_iterator(const token& start, const token_metadata_impl* token_metadata)
: _token_metadata(token_metadata) {
_cur_it = _token_metadata->sorted_tokens().begin() + _token_metadata->first_token_index(start);
_remaining = _token_metadata->sorted_tokens().size();
}
bool token_metadata::tokens_iterator::operator==(const tokens_iterator& it) const {
return _remaining == it._remaining;
}
const token& token_metadata::tokens_iterator::operator*() const {
return *_cur_it;
}
token_metadata::tokens_iterator& token_metadata::tokens_iterator::operator++() {
++_cur_it;
if (_cur_it == _token_metadata->sorted_tokens().end()) {
_cur_it = _token_metadata->sorted_tokens().begin();
}
--_remaining;
return *this;
}
host_id token_metadata::get_my_id() const {
return get_host_id(utils::fb_utilities::get_broadcast_address());
}
inline
boost::iterator_range<token_metadata::tokens_iterator>
token_metadata_impl::ring_range(const token& start) const {
auto begin = token_metadata::tokens_iterator(start, this);
auto end = token_metadata::tokens_iterator();
return boost::make_iterator_range(begin, end);
}
future<std::unique_ptr<token_metadata_impl>> token_metadata_impl::clone_async() const noexcept {
auto ret = co_await clone_only_token_map();
ret->_bootstrap_tokens.reserve(_bootstrap_tokens.size());
for (const auto& p : _bootstrap_tokens) {
ret->_bootstrap_tokens.emplace(p);
co_await coroutine::maybe_yield();
}
ret->_leaving_endpoints = _leaving_endpoints;
ret->_replacing_endpoints = _replacing_endpoints;
ret->_ring_version = _ring_version;
ret->_version = _version;
co_return ret;
}
future<std::unique_ptr<token_metadata_impl>> token_metadata_impl::clone_only_token_map(bool clone_sorted_tokens) const noexcept {
auto ret = std::make_unique<token_metadata_impl>(shallow_copy{}, *this);
ret->_token_to_endpoint_map.reserve(_token_to_endpoint_map.size());
for (const auto& p : _token_to_endpoint_map) {
ret->_token_to_endpoint_map.emplace(p);
co_await coroutine::maybe_yield();
}
ret->_normal_token_owners = _normal_token_owners;
ret->_topology = co_await _topology.clone_gently();
if (clone_sorted_tokens) {
ret->_sorted_tokens = _sorted_tokens;
co_await coroutine::maybe_yield();
}
ret->_tablets = _tablets;
ret->_read_new = _read_new;
co_return ret;
}
future<> token_metadata_impl::clear_gently() noexcept {
co_await utils::clear_gently(_token_to_endpoint_map);
co_await utils::clear_gently(_normal_token_owners);
co_await utils::clear_gently(_bootstrap_tokens);
co_await utils::clear_gently(_leaving_endpoints);
co_await utils::clear_gently(_replacing_endpoints);
co_await utils::clear_gently(_sorted_tokens);
co_await _topology.clear_gently();
co_await _tablets.clear_gently();
co_return;
}
void token_metadata_impl::sort_tokens() {
std::vector<token> sorted;
sorted.reserve(_token_to_endpoint_map.size());
for (auto&& i : _token_to_endpoint_map) {
sorted.push_back(i.first);
}
std::sort(sorted.begin(), sorted.end());
_sorted_tokens = std::move(sorted);
}
const tablet_metadata& token_metadata::tablets() const {
return _impl->tablets();
}
void token_metadata::set_tablets(tablet_metadata tm) {
_impl->set_tablets(std::move(tm));
}
const std::vector<token>& token_metadata_impl::sorted_tokens() const {
return _sorted_tokens;
}
std::vector<token> token_metadata_impl::get_tokens(const inet_address& addr) const {
std::vector<token> res;
for (auto&& i : _token_to_endpoint_map) {
if (i.second == addr) {
res.push_back(i.first);
}
}
std::sort(res.begin(), res.end());
return res;
}
future<> token_metadata_impl::update_normal_tokens(std::unordered_set<token> tokens, inet_address endpoint) {
if (tokens.empty()) {
co_return;
}
if (!_topology.has_endpoint(endpoint)) {
on_internal_error(tlogger, format("token_metadata_impl: {} must be a member of topology to update normal tokens", endpoint));
}
bool should_sort_tokens = false;
// Phase 1: erase all tokens previously owned by the endpoint.
for(auto it = _token_to_endpoint_map.begin(), ite = _token_to_endpoint_map.end(); it != ite;) {
co_await coroutine::maybe_yield();
if(it->second == endpoint) {
auto tokit = tokens.find(it->first);
if (tokit == tokens.end()) {
// token no longer owned by endpoint
it = _token_to_endpoint_map.erase(it);
continue;
}
// token ownership did not change,
// no further update needed for it.
tokens.erase(tokit);
}
++it;
}
// Phase 2:
// a. ...
// b. update pending _bootstrap_tokens and _leaving_endpoints
// c. update _token_to_endpoint_map with the new endpoint->token mappings
// - set `should_sort_tokens` if new tokens were added
remove_by_value(_bootstrap_tokens, endpoint);
_leaving_endpoints.erase(endpoint);
invalidate_cached_rings();
for (const token& t : tokens)
{
co_await coroutine::maybe_yield();
auto prev = _token_to_endpoint_map.insert(std::pair<token, inet_address>(t, endpoint));
should_sort_tokens |= prev.second; // new token inserted -> sort
if (prev.first->second != endpoint) {
tlogger.debug("Token {} changing ownership from {} to {}", t, prev.first->second, endpoint);
prev.first->second = endpoint;
}
}
co_await update_normal_token_owners();
// New tokens were added to _token_to_endpoint_map
// so re-sort all tokens.
if (should_sort_tokens) {
sort_tokens();
}
co_return;
}
size_t token_metadata_impl::first_token_index(const token& start) const {
if (_sorted_tokens.empty()) {
auto msg = format("sorted_tokens is empty in first_token_index!");
tlogger.error("{}", msg);
throw std::runtime_error(msg);
}
auto it = std::lower_bound(_sorted_tokens.begin(), _sorted_tokens.end(), start);
if (it == _sorted_tokens.end()) {
return 0;
} else {
return std::distance(_sorted_tokens.begin(), it);
}
}
const token& token_metadata_impl::first_token(const token& start) const {
return _sorted_tokens[first_token_index(start)];
}
std::optional<inet_address> token_metadata_impl::get_endpoint(const token& token) const {
auto it = _token_to_endpoint_map.find(token);
if (it == _token_to_endpoint_map.end()) {
return std::nullopt;
} else {
return it->second;
}
}
void token_metadata_impl::debug_show() const {
auto reporter = std::make_shared<timer<lowres_clock>>();
reporter->set_callback ([reporter, this] {
fmt::print("Endpoint -> Token\n");
for (auto x : _token_to_endpoint_map) {
fmt::print("inet_address={}, token={}\n", x.second, x.first);
}
fmt::print("Sorted Token\n");
for (auto x : _sorted_tokens) {
fmt::print("token={}\n", x);
}
});
reporter->arm_periodic(std::chrono::seconds(1));
}
void token_metadata_impl::update_host_id(const host_id& host_id, inet_address endpoint) {
_topology.add_or_update_endpoint(endpoint, host_id);
}
host_id token_metadata_impl::get_host_id(inet_address endpoint) const {
if (const auto* node = _topology.find_node(endpoint)) [[likely]] {
return node->host_id();
} else {
throw std::runtime_error(format("host_id for endpoint {} is not found", endpoint));
}
}
std::optional<host_id> token_metadata_impl::get_host_id_if_known(inet_address endpoint) const {
if (const auto* node = _topology.find_node(endpoint)) [[likely]] {
return node->host_id();
} else {
return std::nullopt;
}
}
std::optional<inet_address> token_metadata_impl::get_endpoint_for_host_id(host_id host_id) const {
if (const auto* node = _topology.find_node(host_id)) [[likely]] {
return node->endpoint();
} else {
return std::nullopt;
}
}
std::unordered_map<inet_address, host_id> token_metadata_impl::get_endpoint_to_host_id_map_for_reading() const {
const auto& nodes = _topology.get_nodes_by_endpoint();
std::unordered_map<inet_address, host_id> map;
map.reserve(nodes.size());
for (const auto& [endpoint, node] : nodes) {
map[endpoint] = node->host_id();
}
return map;
}
bool token_metadata_impl::is_normal_token_owner(inet_address endpoint) const {
return _normal_token_owners.contains(endpoint);
}
void token_metadata_impl::add_bootstrap_token(token t, inet_address endpoint) {
std::unordered_set<token> tokens{t};
add_bootstrap_tokens(tokens, endpoint);
}
boost::iterator_range<token_metadata::tokens_iterator>
token_metadata_impl::ring_range(const dht::ring_position_view start) const {
return ring_range(start.token());
}
void token_metadata_impl::add_bootstrap_tokens(std::unordered_set<token> tokens, inet_address endpoint) {
for (auto t : tokens) {
auto old_endpoint = _bootstrap_tokens.find(t);
if (old_endpoint != _bootstrap_tokens.end() && (*old_endpoint).second != endpoint) {
auto msg = format("Bootstrap Token collision between {} and {} (token {}", (*old_endpoint).second, endpoint, t);
throw std::runtime_error(msg);
}
auto old_endpoint2 = _token_to_endpoint_map.find(t);
if (old_endpoint2 != _token_to_endpoint_map.end() && (*old_endpoint2).second != endpoint) {
auto msg = format("Bootstrap Token collision between {} and {} (token {}", (*old_endpoint2).second, endpoint, t);
throw std::runtime_error(msg);
}
}
std::erase_if(_bootstrap_tokens, [endpoint] (const std::pair<token, inet_address>& n) { return n.second == endpoint; });
for (auto t : tokens) {
_bootstrap_tokens[t] = endpoint;
}
}
void token_metadata_impl::remove_bootstrap_tokens(std::unordered_set<token> tokens) {
if (tokens.empty()) {
tlogger.warn("tokens is empty in remove_bootstrap_tokens!");
return;
}
for (auto t : tokens) {
_bootstrap_tokens.erase(t);
}
}
bool token_metadata_impl::is_leaving(inet_address endpoint) const {
return _leaving_endpoints.contains(endpoint);
}
bool token_metadata_impl::is_being_replaced(inet_address endpoint) const {
return _replacing_endpoints.contains(endpoint);
}
bool token_metadata_impl::is_any_node_being_replaced() const {
return !_replacing_endpoints.empty();
}
void token_metadata_impl::remove_endpoint(inet_address endpoint) {
remove_by_value(_bootstrap_tokens, endpoint);
remove_by_value(_token_to_endpoint_map, endpoint);
_normal_token_owners.erase(endpoint);
_topology.remove_endpoint(endpoint);
_leaving_endpoints.erase(endpoint);
del_replacing_endpoint(endpoint);
invalidate_cached_rings();
}
token token_metadata_impl::get_predecessor(token t) const {
auto& tokens = sorted_tokens();
auto it = std::lower_bound(tokens.begin(), tokens.end(), t);
if (it == tokens.end() || *it != t) {
auto msg = format("token error in get_predecessor!");
tlogger.error("{}", msg);
throw std::runtime_error(msg);
}
if (it == tokens.begin()) {
// If the token is the first element, its preprocessor is the last element
return tokens.back();
} else {
return *(--it);
}
}
dht::token_range_vector token_metadata_impl::get_primary_ranges_for(std::unordered_set<token> tokens) const {
dht::token_range_vector ranges;
ranges.reserve(tokens.size() + 1); // one of the ranges will wrap
for (auto right : tokens) {
auto left = get_predecessor(right);
::compat::unwrap_into(
wrapping_range<token>(range_bound<token>(left, false), range_bound<token>(right)),
dht::token_comparator(),
[&] (auto&& rng) { ranges.push_back(std::move(rng)); });
}
return ranges;
}
dht::token_range_vector token_metadata_impl::get_primary_ranges_for(token right) const {
return get_primary_ranges_for(std::unordered_set<token>{right});
}
boost::icl::interval<token>::interval_type
token_metadata_impl::range_to_interval(range<dht::token> r) {
bool start_inclusive = false;
bool end_inclusive = false;
token start = dht::minimum_token();
token end = dht::maximum_token();
if (r.start()) {
start = r.start()->value();
start_inclusive = r.start()->is_inclusive();
}
if (r.end()) {
end = r.end()->value();
end_inclusive = r.end()->is_inclusive();
}
if (start_inclusive == false && end_inclusive == false) {
return boost::icl::interval<token>::open(std::move(start), std::move(end));
} else if (start_inclusive == false && end_inclusive == true) {
return boost::icl::interval<token>::left_open(std::move(start), std::move(end));
} else if (start_inclusive == true && end_inclusive == false) {
return boost::icl::interval<token>::right_open(std::move(start), std::move(end));
} else {
return boost::icl::interval<token>::closed(std::move(start), std::move(end));
}
}
range<dht::token>
token_metadata_impl::interval_to_range(boost::icl::interval<token>::interval_type i) {
bool start_inclusive;
bool end_inclusive;
auto bounds = i.bounds().bits();
if (bounds == boost::icl::interval_bounds::static_open) {
start_inclusive = false;
end_inclusive = false;
} else if (bounds == boost::icl::interval_bounds::static_left_open) {
start_inclusive = false;
end_inclusive = true;
} else if (bounds == boost::icl::interval_bounds::static_right_open) {
start_inclusive = true;
end_inclusive = false;
} else if (bounds == boost::icl::interval_bounds::static_closed) {
start_inclusive = true;
end_inclusive = true;
} else {
throw std::runtime_error("Invalid boost::icl::interval<token> bounds");
}
return range<dht::token>({{i.lower(), start_inclusive}}, {{i.upper(), end_inclusive}});
}
future<> token_metadata_impl::update_topology_change_info(dc_rack_fn& get_dc_rack) {
if (_bootstrap_tokens.empty() && _leaving_endpoints.empty() && _replacing_endpoints.empty()) {
co_await utils::clear_gently(_topology_change_info);
_topology_change_info.reset();
co_return;
}
// true if there is a node replaced with the same IP
bool replace_with_same_endpoint = false;
// target_token_metadata incorporates all the changes from leaving, bootstrapping and replacing
auto target_token_metadata = co_await clone_only_token_map(false);
{
// construct new_normal_tokens based on _bootstrap_tokens and _replacing_endpoints
std::unordered_map<inet_address, std::unordered_set<token>> new_normal_tokens;
if (!_replacing_endpoints.empty()) {
for (const auto& [token, inet_address]: _token_to_endpoint_map) {
const auto it = _replacing_endpoints.find(inet_address);
if (it == _replacing_endpoints.end()) {
continue;
}
new_normal_tokens[it->second].insert(token);
}
for (const auto& [replace_from, replace_to]: _replacing_endpoints) {
if (replace_from == replace_to) {
replace_with_same_endpoint = true;
} else {
target_token_metadata->remove_endpoint(replace_from);
}
}
}
for (const auto& [token, inet_address]: _bootstrap_tokens) {
new_normal_tokens[inet_address].insert(token);
}
// apply new_normal_tokens
for (auto& [endpoint, tokens]: new_normal_tokens) {
target_token_metadata->update_topology(endpoint, get_dc_rack(endpoint), node::state::normal);
co_await target_token_metadata->update_normal_tokens(std::move(tokens), endpoint);
}
// apply leaving endpoints
for (const auto& endpoint: _leaving_endpoints) {
target_token_metadata->remove_endpoint(endpoint);
}
target_token_metadata->sort_tokens();
}
// We require a distinct token_metadata instance when replace_from equals replace_to,
// as it ensures the node is included in pending_ranges.
// Otherwise, the node would be excluded from both pending_ranges and
// get_natural_endpoints_without_node_being_replaced,
// causing the coordinator to overlook it entirely.
std::unique_ptr<token_metadata_impl> base_token_metadata;
if (replace_with_same_endpoint) {
base_token_metadata = co_await clone_only_token_map(false);
for (const auto& [replace_from, replace_to]: _replacing_endpoints) {
if (replace_from == replace_to) {
base_token_metadata->remove_endpoint(replace_from);
}
}
base_token_metadata->sort_tokens();
}
// merge tokens from token_to_endpoint and bootstrap_tokens,
// preserving tokens of leaving endpoints
auto all_tokens = std::vector<dht::token>();
all_tokens.reserve(sorted_tokens().size() + get_bootstrap_tokens().size());
all_tokens.resize(sorted_tokens().size());
std::copy(begin(sorted_tokens()), end(sorted_tokens()), begin(all_tokens));
for (const auto& p: get_bootstrap_tokens()) {
all_tokens.push_back(p.first);
}
std::sort(begin(all_tokens), end(all_tokens));
auto prev_value = std::move(_topology_change_info);
_topology_change_info.emplace(token_metadata(std::move(target_token_metadata)),
base_token_metadata ? std::optional(token_metadata(std::move(base_token_metadata))): std::nullopt,
std::move(all_tokens),
_read_new);
co_await utils::clear_gently(prev_value);
}
size_t token_metadata_impl::count_normal_token_owners() const {
return _normal_token_owners.size();
}
future<> token_metadata_impl::update_normal_token_owners() {
std::unordered_set<inet_address> eps;
for (auto [t, ep]: _token_to_endpoint_map) {
eps.insert(ep);
co_await coroutine::maybe_yield();
}
_normal_token_owners = std::move(eps);
}
void token_metadata_impl::add_leaving_endpoint(inet_address endpoint) {
_leaving_endpoints.emplace(endpoint);
}
void token_metadata_impl::del_leaving_endpoint(inet_address endpoint) {
_leaving_endpoints.erase(endpoint);
}
void token_metadata_impl::add_replacing_endpoint(inet_address existing_node, inet_address replacing_node) {
tlogger.info("Added node {} as pending replacing endpoint which replaces existing node {}",
replacing_node, existing_node);
_replacing_endpoints[existing_node] = replacing_node;
}
void token_metadata_impl::del_replacing_endpoint(inet_address existing_node) {
if (_replacing_endpoints.contains(existing_node)) {
tlogger.info("Removed node {} as pending replacing endpoint which replaces existing node {}",
_replacing_endpoints[existing_node], existing_node);
}
_replacing_endpoints.erase(existing_node);
}
std::map<token, inet_address> token_metadata_impl::get_normal_and_bootstrapping_token_to_endpoint_map() const {
std::map<token, inet_address> ret(_token_to_endpoint_map.begin(), _token_to_endpoint_map.end());
ret.insert(_bootstrap_tokens.begin(), _bootstrap_tokens.end());
return ret;
}
std::multimap<inet_address, token> token_metadata_impl::get_endpoint_to_token_map_for_reading() const {
std::multimap<inet_address, token> cloned;
for (const auto& x : _token_to_endpoint_map) {
cloned.emplace(x.second, x.first);
}
return cloned;
}
topology_change_info::topology_change_info(token_metadata target_token_metadata_,
std::optional<token_metadata> base_token_metadata_,
std::vector<dht::token> all_tokens_,
token_metadata::read_new_t read_new_)
: target_token_metadata(std::move(target_token_metadata_))
, base_token_metadata(std::move(base_token_metadata_))
, all_tokens(std::move(all_tokens_))
, read_new(read_new_)
{
}
future<> topology_change_info::clear_gently() {
co_await utils::clear_gently(target_token_metadata);
co_await utils::clear_gently(base_token_metadata);
co_await utils::clear_gently(all_tokens);
}
token_metadata::token_metadata(std::unique_ptr<token_metadata_impl> impl)
: _impl(std::move(impl)) {
}
token_metadata::token_metadata(config cfg)
: _impl(std::make_unique<token_metadata_impl>(std::move(cfg))) {
}
token_metadata::~token_metadata() = default;
token_metadata::token_metadata(token_metadata&&) noexcept = default;
token_metadata& token_metadata::token_metadata::operator=(token_metadata&&) noexcept = default;
const std::vector<token>&
token_metadata::sorted_tokens() const {
return _impl->sorted_tokens();
}
future<>
token_metadata::update_normal_tokens(std::unordered_set<token> tokens, inet_address endpoint) {
return _impl->update_normal_tokens(std::move(tokens), endpoint);
}
const token&
token_metadata::first_token(const token& start) const {
return _impl->first_token(start);
}
size_t
token_metadata::first_token_index(const token& start) const {
return _impl->first_token_index(start);
}
std::optional<inet_address>
token_metadata::get_endpoint(const token& token) const {
return _impl->get_endpoint(token);
}
std::vector<token>
token_metadata::get_tokens(const inet_address& addr) const {
return _impl->get_tokens(addr);
}
const std::unordered_map<token, inet_address>&
token_metadata::get_token_to_endpoint() const {
return _impl->get_token_to_endpoint();
}
const std::unordered_set<inet_address>&
token_metadata::get_leaving_endpoints() const {
return _impl->get_leaving_endpoints();
}
const std::unordered_map<token, inet_address>&
token_metadata::get_bootstrap_tokens() const {
return _impl->get_bootstrap_tokens();
}
void
token_metadata::update_topology(inet_address ep, endpoint_dc_rack dr, std::optional<node::state> opt_st) {
_impl->update_topology(ep, std::move(dr), std::move(opt_st));
}
boost::iterator_range<token_metadata::tokens_iterator>
token_metadata::ring_range(const token& start) const {
return _impl->ring_range(start);
}
boost::iterator_range<token_metadata::tokens_iterator>
token_metadata::ring_range(dht::ring_position_view start) const {
return _impl->ring_range(start);
}
class token_metadata_ring_splitter : public locator::token_range_splitter {
token_metadata_ptr _tmptr;
boost::iterator_range<token_metadata::tokens_iterator> _range;
public:
token_metadata_ring_splitter(token_metadata_ptr tmptr)
: _tmptr(std::move(tmptr))
, _range(_tmptr->sorted_tokens().empty() // ring_range() throws if the ring is empty
? boost::make_iterator_range(token_metadata::tokens_iterator(), token_metadata::tokens_iterator())
: _tmptr->ring_range(dht::minimum_token()))
{ }
void reset(dht::ring_position_view pos) override {
_range = _tmptr->ring_range(pos);
}
std::optional<dht::token> next_token() override {
if (_range.empty()) {
return std::nullopt;
}
auto t = *_range.begin();
_range.drop_front();
return t;
}
};
std::unique_ptr<locator::token_range_splitter> make_splitter(token_metadata_ptr tmptr) {
return std::make_unique<token_metadata_ring_splitter>(std::move(tmptr));
}
topology&
token_metadata::get_topology() {
return _impl->get_topology();
}
const topology&
token_metadata::get_topology() const {
return _impl->get_topology();
}
void
token_metadata::debug_show() const {
_impl->debug_show();
}
void
token_metadata::update_host_id(const host_id& host_id, inet_address endpoint) {
_impl->update_host_id(host_id, endpoint);
}
host_id
token_metadata::get_host_id(inet_address endpoint) const {
return _impl->get_host_id(endpoint);
}
std::optional<host_id>
token_metadata::get_host_id_if_known(inet_address endpoint) const {
return _impl->get_host_id_if_known(endpoint);
}
std::optional<token_metadata::inet_address>
token_metadata::get_endpoint_for_host_id(host_id host_id) const {
return _impl->get_endpoint_for_host_id(host_id);
}