/
repair.cc
1395 lines (1291 loc) · 60.2 KB
/
repair.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 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#include "repair.hh"
#include "range_split.hh"
#include "atomic_cell_hash.hh"
#include "streaming/stream_plan.hh"
#include "streaming/stream_state.hh"
#include "gms/inet_address.hh"
#include "db/config.hh"
#include "service/storage_service.hh"
#include "service/priority_manager.hh"
#include "message/messaging_service.hh"
#include "sstables/sstables.hh"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/cxx11/any_of.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <boost/range/adaptor/map.hpp>
#include <cryptopp/sha.h>
#include <seastar/core/gate.hh>
#include <seastar/util/defer.hh>
static logging::logger rlogger("repair");
class repair_info {
public:
seastar::sharded<database>& db;
sstring keyspace;
dht::token_range_vector ranges;
std::vector<sstring> cfs;
int id;
shard_id shard;
std::vector<sstring> data_centers;
std::vector<sstring> hosts;
size_t nr_failed_ranges = 0;
bool aborted = false;
// Map of peer -> <cf, ranges>
std::unordered_map<gms::inet_address, std::unordered_map<sstring, dht::token_range_vector>> ranges_need_repair_in;
std::unordered_map<gms::inet_address, std::unordered_map<sstring, dht::token_range_vector>> ranges_need_repair_out;
// FIXME: this "100" needs to be a parameter.
uint64_t target_partitions = 100;
// This affects how many ranges we put in a stream plan. The more the more
// memory we use to store the ranges in memory. However, it can reduce the
// total number of stream_plan we use for the repair.
size_t sub_ranges_to_stream = 10 * 1024;
size_t sp_index = 0;
size_t current_sub_ranges_nr_in = 0;
size_t current_sub_ranges_nr_out = 0;
int ranges_index = 0;
// Only allow one stream_plan in flight
semaphore sp_parallelism_semaphore{1};
lw_shared_ptr<streaming::stream_plan> _sp_in;
lw_shared_ptr<streaming::stream_plan> _sp_out;
public:
repair_info(seastar::sharded<database>& db_,
const sstring& keyspace_,
const dht::token_range_vector& ranges_,
const std::vector<sstring>& cfs_,
int id_,
const std::vector<sstring>& data_centers_,
const std::vector<sstring>& hosts_)
: db(db_)
, keyspace(keyspace_)
, ranges(ranges_)
, cfs(cfs_)
, id(id_)
, shard(engine().cpu_id())
, data_centers(data_centers_)
, hosts(hosts_) {
}
future<> do_streaming() {
size_t ranges_in = 0;
size_t ranges_out = 0;
_sp_in = make_lw_shared<streaming::stream_plan>(sprint("repair-in-id-%d-shard-%d-index-%d", id, shard, sp_index));
_sp_out = make_lw_shared<streaming::stream_plan>(sprint("repair-out-id-%d-shard-%d-index-%d", id, shard, sp_index));
for (auto& x : ranges_need_repair_in) {
auto& peer = x.first;
for (auto& y : x.second) {
auto& cf = y.first;
auto& stream_ranges = y.second;
ranges_in += stream_ranges.size();
_sp_in->request_ranges(peer, keyspace, std::move(stream_ranges), {cf});
}
}
ranges_need_repair_in.clear();
current_sub_ranges_nr_in = 0;
for (auto& x : ranges_need_repair_out) {
auto& peer = x.first;
for (auto& y : x.second) {
auto& cf = y.first;
auto& stream_ranges = y.second;
ranges_out += stream_ranges.size();
_sp_out->transfer_ranges(peer, keyspace, std::move(stream_ranges), {cf});
}
}
ranges_need_repair_out.clear();
current_sub_ranges_nr_out = 0;
if (ranges_in || ranges_out) {
rlogger.info("Start streaming for repair id={}, shard={}, index={}, ranges_in={}, ranges_out={}", id, shard, sp_index, ranges_in, ranges_out);
}
sp_index++;
return _sp_in->execute().discard_result().then([this, sp_in = _sp_in, sp_out = _sp_out] {
return _sp_out->execute().discard_result();
}).handle_exception([this] (auto ep) {
rlogger.warn("repair's stream failed: {}", ep);
return make_exception_future(ep);
}).finally([this] {
_sp_in = {};
_sp_out = {};
});
}
void check_failed_ranges() {
if (nr_failed_ranges) {
rlogger.info("repair {} on shard {} failed - {} ranges failed", id, shard, nr_failed_ranges);
throw std::runtime_error(sprint("repair %d on shard %d failed to do checksum for %d sub ranges", id, shard, nr_failed_ranges));
} else {
rlogger.info("repair {} on shard {} completed successfully", id, shard);
}
}
future<> request_transfer_ranges(const sstring& cf,
const ::dht::token_range& range,
const std::vector<gms::inet_address>& neighbors_in,
const std::vector<gms::inet_address>& neighbors_out) {
rlogger.debug("Add cf {}, range {}, current_sub_ranges_nr_in {}, current_sub_ranges_nr_out {}", cf, range, current_sub_ranges_nr_in, current_sub_ranges_nr_out);
return seastar::with_semaphore(sp_parallelism_semaphore, 1, [this, cf, range, neighbors_in, neighbors_out] {
for (const auto& peer : neighbors_in) {
ranges_need_repair_in[peer][cf].emplace_back(range);
current_sub_ranges_nr_in++;
}
for (const auto& peer : neighbors_out) {
ranges_need_repair_out[peer][cf].emplace_back(range);
current_sub_ranges_nr_out++;
}
if (current_sub_ranges_nr_in >= sub_ranges_to_stream || current_sub_ranges_nr_out >= sub_ranges_to_stream) {
return do_streaming();
}
return make_ready_future<>();
});
}
void abort() {
if (_sp_in) {
_sp_in->abort();
}
if (_sp_out) {
_sp_out->abort();
}
aborted = true;
}
void check_in_abort() {
if (aborted) {
throw std::runtime_error(sprint("repair id %d is aborted on shard %d", id, shard));
}
}
};
template <typename T1, typename T2>
inline
static std::ostream& operator<<(std::ostream& os, const std::unordered_map<T1, T2>& v) {
bool first = true;
os << "{";
for (auto&& elem : v) {
if (!first) {
os << ", ";
} else {
first = false;
}
os << elem.first << "=" << elem.second;
}
os << "}";
return os;
}
static std::vector<sstring> list_column_families(const database& db, const sstring& keyspace) {
std::vector<sstring> ret;
for (auto &&e : db.get_column_families_mapping()) {
if (e.first.first == keyspace) {
ret.push_back(e.first.second);
}
}
return ret;
}
template<typename Collection, typename T>
void remove_item(Collection& c, T& item) {
auto it = std::find(c.begin(), c.end(), item);
if (it != c.end()) {
c.erase(it);
}
}
// Return all of the neighbors with whom we share the provided range.
static std::vector<gms::inet_address> get_neighbors(database& db,
const sstring& ksname, query::range<dht::token> range,
const std::vector<sstring>& data_centers,
const std::vector<sstring>& hosts) {
keyspace& ks = db.find_keyspace(ksname);
auto& rs = ks.get_replication_strategy();
dht::token tok = range.end() ? range.end()->value() : dht::maximum_token();
auto ret = rs.get_natural_endpoints(tok);
remove_item(ret, utils::fb_utilities::get_broadcast_address());
if (!data_centers.empty()) {
auto dc_endpoints_map = service::get_local_storage_service().get_token_metadata().get_topology().get_datacenter_endpoints();
std::unordered_set<gms::inet_address> dc_endpoints;
for (const sstring& dc : data_centers) {
auto it = dc_endpoints_map.find(dc);
if (it == dc_endpoints_map.end()) {
std::vector<sstring> dcs;
for (const auto& e : dc_endpoints_map) {
dcs.push_back(e.first);
}
throw std::runtime_error(sprint("Unknown data center '%s'. "
"Known data centers: %s", dc, dcs));
}
for (const auto& endpoint : it->second) {
dc_endpoints.insert(endpoint);
}
}
// We require, like Cassandra does, that the current host must also
// be part of the repair
if (!dc_endpoints.count(utils::fb_utilities::get_broadcast_address())) {
throw std::runtime_error("The current host must be part of the repair");
}
// The resulting list of nodes is the intersection of the nodes in the
// listed data centers, and the (range-dependent) list of neighbors.
std::unordered_set<gms::inet_address> neighbor_set(ret.begin(), ret.end());
ret.clear();
for (const auto& endpoint : dc_endpoints) {
if (neighbor_set.count(endpoint)) {
ret.push_back(endpoint);
}
}
} else if (!hosts.empty()) {
bool found_me = false;
std::unordered_set<gms::inet_address> neighbor_set(ret.begin(), ret.end());
ret.clear();
for (const sstring& host : hosts) {
gms::inet_address endpoint;
try {
endpoint = gms::inet_address(host);
} catch(...) {
throw std::runtime_error(sprint("Unknown host specified: %s", host));
}
if (endpoint == utils::fb_utilities::get_broadcast_address()) {
found_me = true;
} else if (neighbor_set.count(endpoint)) {
ret.push_back(endpoint);
// If same host is listed twice, don't add it again later
neighbor_set.erase(endpoint);
}
// Nodes which aren't neighbors for this range are ignored.
// This allows the user to give a list of "good" nodes, where
// for each different range, only the subset of nodes actually
// holding a replica of the given range is used. This,
// however, means the user is never warned if one of the nodes
// on the list isn't even part of the cluster.
}
// We require, like Cassandra does, that the current host must also
// be listed on the "-hosts" option - even those we don't want it in
// the returned list:
if (!found_me) {
throw std::runtime_error("The current host must be part of the repair");
}
if (ret.size() < 1) {
auto me = utils::fb_utilities::get_broadcast_address();
auto others = rs.get_natural_endpoints(tok);
remove_item(others, me);
throw std::runtime_error(sprint("Repair requires at least two "
"endpoints that are neighbors before it can continue, "
"the endpoint used for this repair is %s, other "
"available neighbors are %s but these neighbors were not "
"part of the supplied list of hosts to use during the "
"repair (%s).", me, others, hosts));
}
}
return ret;
#if 0
// Origin's ActiveRepairService.getNeighbors() also verifies that the
// requested range fits into a local range
StorageService ss = StorageService.instance;
Map<Range<Token>, List<InetAddress>> replicaSets = ss.getRangeToAddressMap(keyspaceName);
Range<Token> rangeSuperSet = null;
for (Range<Token> range : ss.getLocalRanges(keyspaceName))
{
if (range.contains(toRepair))
{
rangeSuperSet = range;
break;
}
else if (range.intersects(toRepair))
{
throw new IllegalArgumentException("Requested range intersects a local range but is not fully contained in one; this would lead to imprecise repair");
}
}
if (rangeSuperSet == null || !replicaSets.containsKey(rangeSuperSet))
return Collections.emptySet();
#endif
}
// The repair_tracker tracks ongoing repair operations and their progress.
// A repair which has already finished successfully is dropped from this
// table, but a failed repair will remain in the table forever so it can
// be queried about more than once (FIXME: reconsider this. But note that
// failed repairs should be rare anwyay).
// This object is not thread safe, and must be used by only one cpu.
class tracker {
private:
// Each repair_start() call returns a unique int which the user can later
// use to follow the status of this repair with repair_status().
// We can't use the number 0 - if repair_start() returns 0, it means it
// decide quickly that there is nothing to repair.
int _next_repair_command = 1;
// Note that there are no "SUCCESSFUL" entries in the "status" map:
// Successfully-finished repairs are those with id < _next_repair_command
// but aren't listed as running or failed the status map.
std::unordered_map<int, repair_status> _status;
// Used to allow shutting down repairs in progress, and waiting for them.
seastar::gate _gate;
// Set when the repair service is being shutdown
std::atomic_bool _shutdown alignas(seastar::cache_line_size);
// Map repair id into repair_info. The vector has smp::count elements, each
// element will be accessed by only one shard.
std::vector<std::unordered_map<int, lw_shared_ptr<repair_info>>> _repairs;
public:
tracker() : _shutdown(false) {
}
void start(int id) {
_gate.enter();
_status[id] = repair_status::RUNNING;
}
void done(int id, bool succeeded) {
if (succeeded) {
_status.erase(id);
} else {
_status[id] = repair_status::FAILED;
}
_gate.leave();
}
repair_status get(int id) {
if (id >= _next_repair_command) {
throw std::runtime_error(sprint("unknown repair id %d", id));
}
auto it = _status.find(id);
if (it == _status.end()) {
return repair_status::SUCCESSFUL;
} else {
return it->second;
}
}
int next_repair_command() {
return _next_repair_command++;
}
future<> shutdown() {
_shutdown.store(true, std::memory_order_relaxed);
return _gate.close();
}
void check_in_shutdown() {
if (_shutdown.load(std::memory_order_relaxed)) {
throw std::runtime_error(sprint("Repair service is being shutdown"));
}
}
void init_repair_info() {
if (_repairs.size() != smp::count) {
_repairs.resize(smp::count);
}
}
void add_repair_info(int id, lw_shared_ptr<repair_info> ri) {
init_repair_info();
_repairs[engine().cpu_id()].emplace(id, ri);
}
void remove_repair_info(int id) {
init_repair_info();
_repairs[engine().cpu_id()].erase(id);
}
lw_shared_ptr<repair_info> get_repair_info(int id) {
init_repair_info();
auto it = _repairs[engine().cpu_id()].find(id);
if (it != _repairs[engine().cpu_id()].end()) {
return it->second;
}
return {};
}
std::vector<int> get_active() const {
std::vector<int> res;
boost::push_back(res, _status | boost::adaptors::filtered([] (auto& x) {
return x.second == repair_status::RUNNING;
}) | boost::adaptors::map_keys);
return res;
}
size_t nr_running_repair_jobs() {
size_t count = 0;
if (engine().cpu_id() != 0) {
return count;
}
for (auto& x : _status) {
auto& status = x.second;
if (status == repair_status::RUNNING) {
count++;
}
}
return count;
}
void abort_all_repairs() {
init_repair_info();
size_t count = nr_running_repair_jobs();
for (auto& x : _repairs[engine().cpu_id()]) {
auto& ri = x.second;
ri->abort();
}
rlogger.info0("Aborted {} repair job(s)", count);
}
};
static tracker repair_tracker;
static void check_in_shutdown() {
repair_tracker.check_in_shutdown();
}
class sha256_hasher {
CryptoPP::SHA256 hash{};
public:
void update(const char* ptr, size_t length) {
// In Crypto++ v6, the `byte` typedef has been moved to CryptoPP:: namespace
// We bring the namespace in to make the same code work for both 5.x and 6.x versions
using namespace CryptoPP;
static_assert(sizeof(char) == sizeof(byte), "Assuming lengths will be the same");
hash.Update(reinterpret_cast<const byte*>(ptr), length * sizeof(byte));
}
void finalize(std::array<uint8_t, 32>& digest) {
static_assert(CryptoPP::SHA256::DIGESTSIZE == std::tuple_size<std::remove_reference_t<decltype(digest)>>::value * sizeof(digest[0]),
"digest size");
hash.Final(reinterpret_cast<unsigned char*>(digest.data()));
}
};
class partition_hasher {
const schema& _schema;
sha256_hasher _hasher;
partition_checksum _checksum;
bound_view::compare _cmp;
range_tombstone_list _rt_list;
bool _inside_range_tombstone = false;
private:
void consume_cell(const column_definition& col, const atomic_cell_or_collection& cell) {
feed_hash(_hasher, col.name());
feed_hash(_hasher, col.type->name());
feed_hash(_hasher, cell, col);
}
void consume_range_tombstone_start(const range_tombstone& rt) {
feed_hash(_hasher, rt.start, _schema);
feed_hash(_hasher, rt.start_kind);
feed_hash(_hasher, rt.tomb);
}
void consume_range_tombstone_end(const range_tombstone& rt) {
feed_hash(_hasher, rt.end, _schema);
feed_hash(_hasher, rt.end_kind);
}
void pop_rt_front() {
auto& rt = *_rt_list.tombstones().begin();
_rt_list.tombstones().erase(_rt_list.begin());
current_deleter<range_tombstone>()(&rt);
}
void consume_range_tombstones_until(const clustering_row& cr) {
while (!_rt_list.empty()) {
auto it = _rt_list.begin();
if (_inside_range_tombstone) {
if (_cmp(it->end_bound(), cr.key())) {
consume_range_tombstone_end(*it);
_inside_range_tombstone = false;
pop_rt_front();
} else {
break;
}
} else {
if (_cmp(it->start_bound(), cr.key())) {
consume_range_tombstone_start(*it);
_inside_range_tombstone = true;
} else {
break;
}
}
}
}
void consume_range_tombstones_until_end() {
if (_inside_range_tombstone) {
consume_range_tombstone_end(*_rt_list.begin());
pop_rt_front();
}
for (auto&& rt : _rt_list) {
consume_range_tombstone_start(rt);
consume_range_tombstone_end(rt);
}
_rt_list.clear();
_inside_range_tombstone = false;
}
public:
explicit partition_hasher(const schema& s)
: _schema(s), _cmp(s), _rt_list(s) { }
void consume_new_partition(const dht::decorated_key& dk) {
feed_hash(_hasher, dk.key(), _schema);
}
stop_iteration consume(tombstone t) {
feed_hash(_hasher, t);
return stop_iteration::no;
}
stop_iteration consume(const static_row& sr) {
sr.cells().for_each_cell([&] (column_id id, const atomic_cell_or_collection& cell) {
auto&& col = _schema.static_column_at(id);
consume_cell(col, cell);
});
return stop_iteration::no;
}
stop_iteration consume(const clustering_row& cr) {
consume_range_tombstones_until(cr);
feed_hash(_hasher, cr.key(), _schema);
feed_hash(_hasher, cr.tomb());
feed_hash(_hasher, cr.marker());
cr.cells().for_each_cell([&] (column_id id, const atomic_cell_or_collection& cell) {
auto&& col = _schema.regular_column_at(id);
consume_cell(col, cell);
});
return stop_iteration::no;
}
stop_iteration consume(range_tombstone&& rt) {
_rt_list.apply(_schema, std::move(rt));
return stop_iteration::no;
}
stop_iteration consume_end_of_partition() {
consume_range_tombstones_until_end();
std::array<uint8_t, 32> digest;
_hasher.finalize(digest);
_hasher = { };
_checksum.add(partition_checksum(digest));
return stop_iteration::no;
}
partition_checksum consume_end_of_stream() {
return std::move(_checksum);
}
};
future<partition_checksum> partition_checksum::compute_legacy(flat_mutation_reader mr)
{
auto s = mr.schema();
return do_with(std::move(mr),
partition_checksum(), [] (auto& reader, auto& checksum) {
return repeat([&reader, &checksum] () {
return read_mutation_from_flat_mutation_reader(reader).then([&checksum] (auto mopt) {
if (!mopt) {
return stop_iteration::yes;
}
std::array<uint8_t, 32> digest;
sha256_hasher h;
feed_hash(h, *mopt);
h.finalize(digest);
checksum.add(partition_checksum(digest));
return stop_iteration::no;
});
}).then([&checksum] {
return checksum;
});
});
}
future<partition_checksum> partition_checksum::compute_streamed(flat_mutation_reader m)
{
return do_with(std::move(m), [] (auto& m) {
return m.consume(partition_hasher(*m.schema()));
});
}
future<partition_checksum> partition_checksum::compute(flat_mutation_reader m, repair_checksum hash_version)
{
switch (hash_version) {
case repair_checksum::legacy: return compute_legacy(std::move(m));
case repair_checksum::streamed: return compute_streamed(std::move(m));
default: throw std::runtime_error(sprint("Unknown hash version: %d", static_cast<int>(hash_version)));
}
}
static inline unaligned<uint64_t>& qword(std::array<uint8_t, 32>& b, int n) {
return *unaligned_cast<uint64_t>(b.data() + 8 * n);
}
static inline const unaligned<uint64_t>& qword(const std::array<uint8_t, 32>& b, int n) {
return *unaligned_cast<uint64_t>(b.data() + 8 * n);
}
void partition_checksum::add(const partition_checksum& other) {
static_assert(std::tuple_size<decltype(_digest)>::value == 32, "digest size");
// Hopefully the following trickery is faster than XOR'ing 32 separate bytes
qword(_digest, 0) = qword(_digest, 0) ^ qword(other._digest, 0);
qword(_digest, 1) = qword(_digest, 1) ^ qword(other._digest, 1);
qword(_digest, 2) = qword(_digest, 2) ^ qword(other._digest, 2);
qword(_digest, 3) = qword(_digest, 3) ^ qword(other._digest, 3);
}
bool partition_checksum::operator==(const partition_checksum& other) const {
static_assert(std::tuple_size<decltype(_digest)>::value == 32, "digest size");
return qword(_digest, 0) == qword(other._digest, 0) &&
qword(_digest, 1) == qword(other._digest, 1) &&
qword(_digest, 2) == qword(other._digest, 2) &&
qword(_digest, 3) == qword(other._digest, 3);
}
const std::array<uint8_t, 32>& partition_checksum::digest() const {
return _digest;
}
std::ostream& operator<<(std::ostream& out, const partition_checksum& c) {
auto save_flags = out.flags();
out << std::hex << std::setfill('0');
for (auto b : c._digest) {
out << std::setw(2) << (unsigned int)b;
}
out.flags(save_flags);
return out;
}
// Calculate the checksum of the data held *on this shard* of a column family,
// in the given token range.
// All parameters to this function are constant references, and the caller
// must ensure they live as long as the future returned by this function is
// not resolved.
// FIXME: Both master and slave will typically call this on consecutive ranges
// so it would be useful to have this code cache its stopping point or have
// some object live throughout the operation. Moreover, it makes sense to to
// vary the collection of sstables used throught a long repair.
static future<partition_checksum> checksum_range_shard(database &db,
const sstring& keyspace_name, const sstring& cf_name,
const dht::partition_range_vector& prs, repair_checksum hash_version) {
auto& cf = db.find_column_family(keyspace_name, cf_name);
auto reader = cf.make_streaming_reader(cf.schema(), prs);
return partition_checksum::compute(std::move(reader), hash_version);
}
// It is counter-productive to allow a large number of range checksum
// operations to proceed in parallel (on the same shard), because the read
// operation can already parallelize itself as much as needed, and doing
// multiple reads in parallel just adds a lot of memory overheads.
// So checksum_parallelism_semaphore is used to limit this parallelism,
// and should be set to 1, or another small number.
//
// Note that checksumming_parallelism_semaphore applies not just in the
// repair master, but also in the slave: The repair slave may receive many
// checksum requests in parallel, but will only work on one or a few
// (checksum_parallelism_semaphore) at once.
static thread_local semaphore checksum_parallelism_semaphore(2);
// Calculate the checksum of the data held on all shards of a column family,
// in the given token range.
// In practice, we only need to consider one or two shards which intersect the
// given "range". This is because the token ring has nodes*vnodes tokens,
// dividing the token space into nodes*vnodes ranges, with "range" being one
// of those. This number is big (vnodes = 256 by default). At the same time,
// sharding divides the token space into relatively few large ranges, one per
// thread.
// Watch out: All parameters to this function are constant references, and the
// caller must ensure they live as line as the future returned by this
// function is not resolved.
future<partition_checksum> checksum_range(seastar::sharded<database> &db,
const sstring& keyspace, const sstring& cf,
const ::dht::token_range& range, repair_checksum hash_version) {
auto& schema = db.local().find_column_family(keyspace, cf).schema();
auto shard_ranges = dht::split_range_to_shards(dht::to_partition_range(range), *schema);
return do_with(partition_checksum(), std::move(shard_ranges), [&db, &keyspace, &cf, hash_version] (auto& result, auto& shard_ranges) {
return parallel_for_each(shard_ranges, [&db, &keyspace, &cf, &result, hash_version] (auto& shard_range) {
auto& shard = shard_range.first;
auto& prs = shard_range.second;
return db.invoke_on(shard, [keyspace, cf, prs = std::move(prs), hash_version] (database& db) mutable {
return do_with(std::move(keyspace), std::move(cf), std::move(prs), [&db, hash_version] (auto& keyspace, auto& cf, auto& prs) {
return seastar::with_semaphore(checksum_parallelism_semaphore, 1, [&db, hash_version, &keyspace, &cf, &prs] {
return checksum_range_shard(db, keyspace, cf, prs, hash_version);
});
});
}).then([&result] (partition_checksum sum) {
result.add(sum);
});
}).then([&result] {
return make_ready_future<partition_checksum>(result);
});
});
}
// parallelism_semaphore limits the number of parallel ongoing checksum
// comparisons. This could mean, for example, that this number of checksum
// requests have been sent to other nodes and we are waiting for them to
// return so we can compare those to our own checksums. This limit can be
// set fairly high because the outstanding comparisons take only few
// resources. In particular, we do NOT do this number of file reads in
// parallel because file reads have large memory overhads (read buffers,
// partitions, etc.) - the number of concurrent reads is further limited
// by an additional semaphore checksum_parallelism_semaphore (see above).
//
// FIXME: This would be better of in a repair service, or even a per-shard
// repair instance holding all repair state. However, since we are anyway
// considering ditching those semaphores for a more fine grained resource-based
// solution, let's do the simplest thing here and change it later
constexpr int parallelism = 100;
static thread_local semaphore parallelism_semaphore(parallelism);
static future<uint64_t> estimate_partitions(seastar::sharded<database>& db, const sstring& keyspace,
const sstring& cf, const dht::token_range& range) {
return db.map_reduce0(
[keyspace, cf, range] (auto& db) {
// FIXME: column_family should have a method to estimate the number of
// partitions (and of course it should use cardinality estimation bitmaps,
// not trivial sum). We shouldn't have this ugly code here...
// FIXME: If sstables are shared, they will be accounted more than
// once. However, shared sstables should exist for a short-time only.
auto sstables = db.find_column_family(keyspace, cf).get_sstables();
return boost::accumulate(*sstables, uint64_t(0),
[&range] (uint64_t x, auto&& sst) { return x + sst->estimated_keys_for_range(range); });
},
uint64_t(0),
std::plus<uint64_t>()
);
}
// Repair a single cf in a single local range.
// Comparable to RepairJob in Origin.
static future<> repair_cf_range(repair_info& ri,
sstring cf, ::dht::token_range range,
const std::vector<gms::inet_address>& neighbors) {
if (neighbors.empty()) {
// Nothing to do in this case...
return make_ready_future<>();
}
ri.check_in_abort();
return estimate_partitions(ri.db, ri.keyspace, cf, range).then([&ri, cf, range, &neighbors] (uint64_t estimated_partitions) {
range_splitter ranges(range, estimated_partitions, ri.target_partitions);
return do_with(seastar::gate(), true, std::move(cf), std::move(ranges),
[&ri, &neighbors] (auto& completion, auto& success, const auto& cf, auto& ranges) {
return do_until([&ranges] () { return !ranges.has_next(); },
[&ranges, &ri, &completion, &success, &neighbors, &cf] () {
auto range = ranges.next();
check_in_shutdown();
ri.check_in_abort();
return seastar::get_units(parallelism_semaphore, 1).then([&ri, &completion, &success, &neighbors, &cf, range] (auto signal_sem) {
auto checksum_type = service::get_local_storage_service().cluster_supports_large_partitions()
? repair_checksum::streamed : repair_checksum::legacy;
// Ask this node, and all neighbors, to calculate checksums in
// this range. When all are done, compare the results, and if
// there are any differences, sync the content of this range.
std::vector<future<partition_checksum>> checksums;
checksums.reserve(1 + neighbors.size());
checksums.push_back(checksum_range(ri.db, ri.keyspace, cf, range, checksum_type));
for (auto&& neighbor : neighbors) {
checksums.push_back(
netw::get_local_messaging_service().send_repair_checksum_range(
netw::msg_addr{neighbor}, ri.keyspace, cf, range, checksum_type));
}
completion.enter();
auto leave = defer([&completion] { completion.leave(); });
when_all(checksums.begin(), checksums.end()).then(
[&ri, &cf, range, &neighbors, &success]
(std::vector<future<partition_checksum>> checksums) {
// If only some of the replicas of this range are alive,
// we set success=false so repair will fail, but we can
// still do our best to repair available replicas.
std::vector<gms::inet_address> live_neighbors;
std::vector<partition_checksum> live_neighbors_checksum;
for (unsigned i = 0; i < checksums.size(); i++) {
if (checksums[i].failed()) {
rlogger.warn(
"Checksum of range {} on {} failed: {}",
range,
(i ? neighbors[i-1] :
utils::fb_utilities::get_broadcast_address()),
checksums[i].get_exception());
success = false;
ri.nr_failed_ranges++;
// Do not break out of the loop here, so we can log
// (and discard) all the exceptions.
} else if (i > 0) {
live_neighbors.push_back(neighbors[i - 1]);
live_neighbors_checksum.push_back(checksums[i].get0());
}
}
if (checksums[0].failed() || live_neighbors.empty()) {
return make_ready_future<>();
}
// If one of the available checksums is different, repair
// all the neighbors which returned a checksum.
auto checksum0 = checksums[0].get0();
std::vector<gms::inet_address> live_neighbors_in(live_neighbors);
std::vector<gms::inet_address> live_neighbors_out(live_neighbors);
std::unordered_map<partition_checksum, std::vector<gms::inet_address>> checksum_map;
for (size_t idx = 0 ; idx < live_neighbors.size(); idx++) {
checksum_map[live_neighbors_checksum[idx]].emplace_back(live_neighbors[idx]);
}
auto node_reducer = [] (std::vector<gms::inet_address>& live_neighbors_in_or_out,
std::vector<gms::inet_address>& nodes_with_same_checksum, size_t nr_nodes_to_keep) {
// nodes_with_same_checksum contains two types of nodes:
// 1) the nodes we want to remove from live_neighbors_in_or_out.
// 2) the nodes, nr_nodes_to_keep in number, not to remove from
// live_neighbors_in_or_out
auto nr_nodes = nodes_with_same_checksum.size();
if (nr_nodes <= nr_nodes_to_keep) {
return;
}
if (nr_nodes_to_keep == 0) {
// All nodes in nodes_with_same_checksum will be removed from live_neighbors_in_or_out
} else if (nr_nodes_to_keep == 1) {
auto node_is_remote = [] (gms::inet_address ip) { return !service::get_local_storage_service().is_local_dc(ip); };
boost::partition(nodes_with_same_checksum, node_is_remote);
nodes_with_same_checksum.resize(nr_nodes - nr_nodes_to_keep);
} else {
throw std::runtime_error(sprint("nr_nodes_to_keep = {}, but it can only be 1 or 0", nr_nodes_to_keep));
}
// Now, nodes_with_same_checksum contains nodes we want to remove, remove it from live_neighbors_in_or_out
auto it = boost::range::remove_if(live_neighbors_in_or_out, [&nodes_with_same_checksum] (const auto& ip) {
return boost::algorithm::any_of_equal(nodes_with_same_checksum, ip);
});
live_neighbors_in_or_out.erase(it, live_neighbors_in_or_out.end());
};
// Reduce in traffic
for (auto& item : checksum_map) {
auto& sum = item.first;
auto nodes_with_same_checksum = item.second;
// If remote nodes have the same checksum, fetch only from one of them
size_t nr_nodes_to_fetch = 1;
// If remote nodes have zero checksum or have the same
// checksum as local checksum, do not fetch from them at all
if (sum == partition_checksum() || sum == checksum0) {
nr_nodes_to_fetch = 0;
}
// E.g.,
// Local Remote1 Remote2 Remote3
// 5 5 5 5 : IN: 0
// 5 5 5 0 : IN: 0
// 5 5 0 0 : IN: 0
// 5 0 0 0 : IN: 0
// 0 5 5 5 : IN: 1
// 0 5 5 0 : IN: 1
// 0 5 0 0 : IN: 1
// 0 0 0 0 : IN: 0
// 3 5 5 3 : IN: 1
// 3 5 3 3 : IN: 1
// 3 3 3 3 : IN: 0
// 3 5 4 3 : IN: 2
node_reducer(live_neighbors_in, nodes_with_same_checksum, nr_nodes_to_fetch);
}
// Reduce out traffic
if (live_neighbors_in.empty()) {
for (auto& item : checksum_map) {
auto& sum = item.first;
auto nodes_with_same_checksum = item.second;
// Skip to send to the nodes with the same checksum as local node
// E.g.,
// Local Remote1 Remote2 Remote3
// 5 5 5 5 : IN: 0 OUT: 0 SKIP_OUT: Remote1, Remote2, Remote3
// 5 5 5 0 : IN: 0 OUT: 1 SKIP_OUT: Remote1, Remote2
// 5 5 0 0 : IN: 0 OUT: 2 SKIP_OUT: Remote1
// 5 0 0 0 : IN: 0 OUT: 3 SKIP_OUT: None
// 0 0 0 0 : IN: 0 OUT: 0 SKIP_OUT: Remote1, Remote2, Remote3
if (sum == checksum0) {
size_t nr_nodes_to_send = 0;
node_reducer(live_neighbors_out, nodes_with_same_checksum, nr_nodes_to_send);
}
}
} else if (live_neighbors_in.size() == 1 && checksum0 == partition_checksum()) {
for (auto& item : checksum_map) {
auto& sum = item.first;
auto nodes_with_same_checksum = item.second;
// Skip to send to the nodes with none zero checksum
// E.g.,
// Local Remote1 Remote2 Remote3
// 0 5 5 5 : IN: 1 OUT: 0 SKIP_OUT: Remote1, Remote2, Remote3
// 0 5 5 0 : IN: 1 OUT: 1 SKIP_OUT: Remote1, Remote2
// 0 5 0 0 : IN: 1 OUT: 2 SKIP_OUT: Remote1
if (sum != checksum0) {
size_t nr_nodes_to_send = 0;
node_reducer(live_neighbors_out, nodes_with_same_checksum, nr_nodes_to_send);
}
}
}
if (!(live_neighbors_in.empty() && live_neighbors_out.empty())) {
rlogger.debug("Found differing range {} on nodes {}, in = {}, out = {}", range,
live_neighbors, live_neighbors_in, live_neighbors_out);
ri.check_in_abort();
return ri.request_transfer_ranges(cf, range, live_neighbors_in, live_neighbors_out);
}
return make_ready_future<>();
}).handle_exception([&ri, &success, &cf, range, leave = std::move(leave),
signal_sem = std::move(signal_sem)] (std::exception_ptr eptr) {
// Something above (e.g., request_transfer_ranges) failed. We could
// stop the repair immediately, or let it continue with
// other ranges (at the moment, we do the latter). But in
// any case, we need to remember that the repair failed to
// tell the caller.
success = false;
ri.nr_failed_ranges++;
rlogger.warn("Failed sync of range {}: {}", range, eptr);
});
});
}).finally([&success, &completion] {
return completion.close().then([&success] {
if (!success) {
rlogger.warn("Checksum or sync of partial range failed");
}
// We probably want the repair contiunes even if some
// ranges fail to do the checksum. We need to set the
// per-repair success flag to false and report after the
// streaming is done.
return make_ready_future<>();
});
});
});
});
}
// Repair a single local range, multiple column families.
// Comparable to RepairSession in Origin
static future<> repair_range(repair_info& ri, const dht::token_range& range) {
auto id = utils::UUID_gen::get_time_UUID();
return do_with(get_neighbors(ri.db.local(), ri.keyspace, range, ri.data_centers, ri.hosts), [&ri, range, id] (const auto& neighbors) {
rlogger.debug("[repair #{}] new session: will sync {} on range {} for {}.{}", id, neighbors, range, ri.keyspace, ri.cfs);
return do_for_each(ri.cfs.begin(), ri.cfs.end(), [&ri, &neighbors, range] (auto&& cf) {
return repair_cf_range(ri, cf, range, neighbors);
});
});
}
static dht::token_range_vector get_ranges_for_endpoint(
database& db, sstring keyspace, gms::inet_address ep) {
auto& rs = db.find_keyspace(keyspace).get_replication_strategy();
return rs.get_ranges(ep);
}
static dht::token_range_vector get_local_ranges(
database& db, sstring keyspace) {
return get_ranges_for_endpoint(db, keyspace, utils::fb_utilities::get_broadcast_address());
}
static dht::token_range_vector get_primary_ranges_for_endpoint(
database& db, sstring keyspace, gms::inet_address ep) {
auto& rs = db.find_keyspace(keyspace).get_replication_strategy();
return rs.get_primary_ranges(ep);
}