-
Notifications
You must be signed in to change notification settings - Fork 950
/
replica.cc
1175 lines (975 loc) · 39.4 KB
/
replica.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 2022, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//
#include "server/replica.h"
#include <chrono>
#include "absl/strings/match.h"
extern "C" {
#include "redis/rdb.h"
}
#include <absl/cleanup/cleanup.h>
#include <absl/flags/flag.h>
#include <absl/functional/bind_front.h>
#include <absl/strings/escaping.h>
#include <absl/strings/str_cat.h>
#include <absl/strings/strip.h>
#include <boost/asio/ip/tcp.hpp>
#include <memory>
#include <utility>
#include "base/logging.h"
#include "facade/dragonfly_connection.h"
#include "facade/redis_parser.h"
#include "server/error.h"
#include "server/journal/executor.h"
#include "server/journal/serializer.h"
#include "server/main_service.h"
#include "server/rdb_load.h"
#include "strings/human_readable.h"
ABSL_FLAG(int, replication_acks_interval, 3000, "Interval between acks in milliseconds.");
ABSL_FLAG(bool, enable_multi_shard_sync, false,
"Execute multi shards commands on replica synchronized");
ABSL_FLAG(int, master_connect_timeout_ms, 20000,
"Timeout for establishing connection to a replication master");
ABSL_FLAG(int, master_reconnect_timeout_ms, 1000,
"Timeout for re-establishing connection to a replication master");
ABSL_FLAG(bool, replica_partial_sync, true,
"Use partial sync to reconnect when a replica connection is interrupted.");
ABSL_FLAG(bool, replica_reconnect_on_master_restart, false,
"When in replica mode, and master restarts, break replication from master.");
ABSL_DECLARE_FLAG(int32_t, port);
namespace dfly {
using namespace std;
using namespace util;
using namespace boost::asio;
using namespace facade;
using absl::GetFlag;
using absl::StrCat;
namespace {
constexpr unsigned kRdbEofMarkSize = 40;
// Distribute flow indices over all available threads (shard_set pool size).
vector<vector<unsigned>> Partition(unsigned num_flows) {
vector<vector<unsigned>> partition(shard_set->pool()->size());
for (unsigned i = 0; i < num_flows; ++i) {
partition[i % partition.size()].push_back(i);
}
return partition;
}
} // namespace
Replica::Replica(string host, uint16_t port, Service* se, std::string_view id,
std::optional<cluster::SlotRange> slot_range)
: ProtocolClient(std::move(host), port), service_(*se), id_{id}, slot_range_(slot_range) {
proactor_ = ProactorBase::me();
}
Replica::~Replica() {
sync_fb_.JoinIfNeeded();
acks_fb_.JoinIfNeeded();
}
static const char kConnErr[] = "could not connect to master: ";
error_code Replica::Start(ConnectionContext* cntx) {
VLOG(1) << "Starting replication";
ProactorBase* mythread = ProactorBase::me();
CHECK(mythread);
auto check_connection_error = [this, &cntx](error_code ec, const char* msg) -> error_code {
if (cntx_.IsCancelled()) {
cntx->SendError("replication cancelled");
return std::make_error_code(errc::operation_canceled);
}
if (ec) {
cntx->SendError(absl::StrCat(msg, ec.message()));
cntx_.Cancel();
}
return ec;
};
// 0. Set basic error handler that is reponsible for cleaning up on errors.
// Can return an error only if replication was cancelled immediately.
auto err = cntx_.SwitchErrorHandler([this](const auto& ge) { this->DefaultErrorHandler(ge); });
RETURN_ON_ERR(check_connection_error(err, "replication cancelled"));
// 1. Resolve dns.
VLOG(1) << "Resolving master DNS";
error_code ec = ResolveHostDns();
RETURN_ON_ERR(check_connection_error(ec, "could not resolve master dns"));
// 2. Connect socket.
VLOG(1) << "Connecting to master";
ec = ConnectAndAuth(absl::GetFlag(FLAGS_master_connect_timeout_ms) * 1ms, &cntx_);
RETURN_ON_ERR(check_connection_error(ec, kConnErr));
// 3. Greet.
VLOG(1) << "Greeting";
state_mask_.store(R_ENABLED | R_TCP_CONNECTED);
ec = Greet();
RETURN_ON_ERR(check_connection_error(ec, "could not greet master "));
// 4. Spawn main coordination fiber.
sync_fb_ = fb2::Fiber("main_replication", &Replica::MainReplicationFb, this);
cntx->SendOk();
return {};
}
void Replica::EnableReplication(ConnectionContext* cntx) {
VLOG(1) << "Enabling replication";
state_mask_.store(R_ENABLED); // set replica state to enabled
sync_fb_ = MakeFiber(&Replica::MainReplicationFb, this); // call replication fiber
cntx->SendOk();
}
void Replica::Stop() {
VLOG(1) << "Stopping replication";
// Stops the loop in MainReplicationFb.
proactor_->Await([this] {
cntx_.Cancel(); // Context is fully resposible for cleanup.
state_mask_.store(0); // Specifically ~R_ENABLED.
});
// Make sure the replica fully stopped and did all cleanup,
// so we can freely release resources (connections).
sync_fb_.JoinIfNeeded();
acks_fb_.JoinIfNeeded();
}
void Replica::Pause(bool pause) {
VLOG(1) << "Pausing replication";
Proactor()->Await([&] { is_paused_ = pause; });
}
std::error_code Replica::TakeOver(std::string_view timeout, bool save_flag) {
VLOG(1) << "Taking over";
std::error_code ec;
auto takeOverCmd = absl::StrCat("TAKEOVER ", timeout, (save_flag ? " SAVE" : ""));
Proactor()->Await([this, &ec, cmd = std::move(takeOverCmd)] { ec = SendNextPhaseRequest(cmd); });
// If we successfully taken over, return and let server_family stop the replication.
return ec;
}
void Replica::MainReplicationFb() {
VLOG(1) << "Main replication fiber started";
// Switch shard states to replication.
SetShardStates(true);
error_code ec;
while (state_mask_.load() & R_ENABLED) {
// Discard all previous errors and set default error handler.
cntx_.Reset([this](const GenericError& ge) { this->DefaultErrorHandler(ge); });
// 1. Connect socket.
if ((state_mask_.load() & R_TCP_CONNECTED) == 0) {
ThisFiber::SleepFor(500ms);
if (is_paused_)
continue;
ec = ResolveHostDns();
if (ec) {
LOG(ERROR) << "Error resolving dns to " << server().host << " " << ec;
continue;
}
// Give a lower timeout for connect, because we're
ec = ConnectAndAuth(absl::GetFlag(FLAGS_master_reconnect_timeout_ms) * 1ms, &cntx_);
if (ec) {
LOG(ERROR) << "Error connecting to " << server().Description() << " " << ec;
continue;
}
VLOG(1) << "Replica socket connected";
state_mask_.fetch_or(R_TCP_CONNECTED);
continue;
}
// 2. Greet.
if ((state_mask_.load() & R_GREETED) == 0) {
ec = Greet();
if (ec) {
LOG(INFO) << "Error greeting " << server().Description() << " " << ec << " "
<< ec.message();
state_mask_.fetch_and(R_ENABLED);
continue;
}
state_mask_.fetch_or(R_GREETED);
continue;
}
// 3. Initiate full sync
if ((state_mask_.load() & R_SYNC_OK) == 0) {
if (HasDflyMaster())
ec = InitiateDflySync();
else
ec = InitiatePSync();
if (ec) {
LOG(WARNING) << "Error syncing with " << server().Description() << " " << ec << " "
<< ec.message();
state_mask_.fetch_and(R_ENABLED); // reset all flags besides R_ENABLED
continue;
}
state_mask_.fetch_or(R_SYNC_OK);
continue;
}
// 4. Start stable state sync.
DCHECK(state_mask_.load() & R_SYNC_OK);
if (HasDflyMaster())
ec = ConsumeDflyStream();
else
ec = ConsumeRedisStream();
auto state = state_mask_.fetch_and(R_ENABLED);
if (state & R_ENABLED) { // replication was not stopped.
LOG(WARNING) << "Error stable sync with " << server().Description() << " " << ec << " "
<< ec.message();
}
}
// Wait for unblocking cleanup to finish.
cntx_.JoinErrorHandler();
// Revert shard states to normal state.
SetShardStates(false);
VLOG(1) << "Main replication fiber finished";
}
error_code Replica::Greet() {
ResetParser(false);
VLOG(1) << "greeting message handling";
// Corresponds to server.repl_state == REPL_STATE_CONNECTING state in redis
RETURN_ON_ERR(SendCommandAndReadResponse("PING")); // optional.
PC_RETURN_ON_BAD_RESPONSE(CheckRespIsSimpleReply("PONG"));
// Corresponds to server.repl_state == REPL_STATE_SEND_HANDSHAKE condition in replication.c
auto port = absl::GetFlag(FLAGS_port);
RETURN_ON_ERR(SendCommandAndReadResponse(StrCat("REPLCONF listening-port ", port)));
PC_RETURN_ON_BAD_RESPONSE(CheckRespIsSimpleReply("OK"));
// Corresponds to server.repl_state == REPL_STATE_SEND_CAPA
RETURN_ON_ERR(SendCommandAndReadResponse("REPLCONF capa eof capa psync2"));
PC_RETURN_ON_BAD_RESPONSE(CheckRespIsSimpleReply("OK"));
// Announce that we are the dragonfly client.
// Note that we currently do not support dragonfly->redis replication.
RETURN_ON_ERR(SendCommandAndReadResponse("REPLCONF capa dragonfly"));
PC_RETURN_ON_BAD_RESPONSE(CheckRespFirstTypes({RespExpr::STRING}));
if (LastResponseArgs().size() == 1) { // Redis
PC_RETURN_ON_BAD_RESPONSE(CheckRespIsSimpleReply("OK"));
} else if (LastResponseArgs().size() >= 3) { // it's dragonfly master.
PC_RETURN_ON_BAD_RESPONSE(!HandleCapaDflyResp());
if (auto ec = ConfigureDflyMaster(); ec)
return ec;
} else {
PC_RETURN_ON_BAD_RESPONSE(false);
}
state_mask_.fetch_or(R_GREETED);
return error_code{};
}
std::error_code Replica::HandleCapaDflyResp() {
// Response is: <master_repl_id, syncid, num_shards [, version]>
if (!CheckRespFirstTypes({RespExpr::STRING, RespExpr::STRING, RespExpr::INT64}) ||
LastResponseArgs()[0].GetBuf().size() != CONFIG_RUN_ID_SIZE)
return make_error_code(errc::bad_message);
int64 param_num_flows = get<int64_t>(LastResponseArgs()[2].u);
if (param_num_flows <= 0 || param_num_flows > 1024) {
// sanity check, we support upto 1024 shards.
// It's not that we can not support more but it's probably highly unlikely that someone
// will run dragonfly with more than 1024 cores.
LOG(ERROR) << "Invalid flow count " << param_num_flows;
return make_error_code(errc::bad_message);
}
// If we're syncing a different replication ID, drop the saved LSNs.
string_view master_repl_id = ToSV(LastResponseArgs()[0].GetBuf());
if (master_context_.master_repl_id != master_repl_id) {
if (absl::GetFlag(FLAGS_replica_reconnect_on_master_restart) &&
!master_context_.master_repl_id.empty()) {
LOG(ERROR) << "Encountered different master repl id (" << master_repl_id << " vs "
<< master_context_.master_repl_id << ")";
state_mask_.store(0);
return make_error_code(errc::connection_aborted);
}
last_journal_LSNs_.reset();
}
master_context_.master_repl_id = master_repl_id;
master_context_.dfly_session_id = ToSV(LastResponseArgs()[1].GetBuf());
num_df_flows_ = param_num_flows;
if (LastResponseArgs().size() >= 4) {
PC_RETURN_ON_BAD_RESPONSE(LastResponseArgs()[3].type == RespExpr::INT64);
master_context_.version = DflyVersion(get<int64_t>(LastResponseArgs()[3].u));
}
VLOG(1) << "Master id: " << master_context_.master_repl_id
<< ", sync id: " << master_context_.dfly_session_id << ", num journals: " << num_df_flows_
<< ", version: " << unsigned(master_context_.version);
return error_code{};
}
std::error_code Replica::ConfigureDflyMaster() {
// We need to send this because we may require to use this for cluster commands.
// this reason to send this here is that in other context we can get an error reply
// since we are budy with the replication
RETURN_ON_ERR(SendCommandAndReadResponse(StrCat("REPLCONF CLIENT-ID ", id_)));
if (!CheckRespIsSimpleReply("OK")) {
LOG(WARNING) << "Bad REPLCONF CLIENT-ID response";
}
// Tell the master our version if it supports REPLCONF CLIENT-VERSION
if (master_context_.version > DflyVersion::VER0) {
RETURN_ON_ERR(
SendCommandAndReadResponse(StrCat("REPLCONF CLIENT-VERSION ", DflyVersion::CURRENT_VER)));
PC_RETURN_ON_BAD_RESPONSE(CheckRespIsSimpleReply("OK"));
}
return error_code{};
}
error_code Replica::InitiatePSync() {
base::IoBuf io_buf{128};
// Corresponds to server.repl_state == REPL_STATE_SEND_PSYNC
string id("?"); // corresponds to null master id and null offset
int64_t offs = -1;
if (!master_context_.master_repl_id.empty()) { // in case we synced before
id = master_context_.master_repl_id; // provide the replication offset and master id
// TBD: for incremental sync send repl_offs_, not supported yet.
// offs = repl_offs_;
}
RETURN_ON_ERR(SendCommand(StrCat("PSYNC ", id, " ", offs)));
// Master may delay sync response with "repl_diskless_sync_delay"
PSyncResponse repl_header;
RETURN_ON_ERR(ParseReplicationHeader(&io_buf, &repl_header));
string* token = absl::get_if<string>(&repl_header.fullsync);
size_t snapshot_size = SIZE_MAX;
if (!token) {
snapshot_size = absl::get<size_t>(repl_header.fullsync);
}
TouchIoTime();
// we get token for diskless redis replication. For disk based replication
// we get the snapshot size.
if (snapshot_size || token != nullptr) {
LOG(INFO) << "Starting full sync with Redis master";
state_mask_.fetch_or(R_SYNCING);
io::PrefixSource ps{io_buf.InputBuffer(), Sock()};
// Set LOADING state.
service_.RequestLoadingState();
absl::Cleanup cleanup = [this]() { service_.RemoveLoadingState(); };
if (slot_range_.has_value()) {
JournalExecutor{&service_}.FlushSlots(slot_range_.value());
} else {
JournalExecutor{&service_}.FlushAll();
}
RdbLoader loader(NULL);
loader.set_source_limit(snapshot_size);
// TODO: to allow registering callbacks within loader to send '\n' pings back to master.
// Also to allow updating last_io_time_.
error_code ec = loader.Load(&ps);
RETURN_ON_ERR(ec);
VLOG(1) << "full sync completed";
if (token) {
uint8_t buf[kRdbEofMarkSize];
io::PrefixSource chained(loader.Leftover(), &ps);
VLOG(1) << "Before reading from chained stream";
io::Result<size_t> eof_res = chained.Read(io::MutableBytes{buf});
CHECK(eof_res && *eof_res == kRdbEofMarkSize);
VLOG(1) << "Comparing token " << ToSV(buf);
// TODO: handle gracefully...
CHECK_EQ(0, memcmp(token->data(), buf, kRdbEofMarkSize));
CHECK(chained.UnusedPrefix().empty());
} else {
CHECK_EQ(0u, loader.Leftover().size());
CHECK_EQ(snapshot_size, loader.bytes_read());
}
CHECK(ps.UnusedPrefix().empty());
io_buf.ConsumeInput(io_buf.InputLen());
TouchIoTime();
} else {
LOG(INFO) << "Re-established sync with Redis master with ID=" << id;
}
state_mask_.fetch_and(~R_SYNCING);
state_mask_.fetch_or(R_SYNC_OK);
// There is a data race condition in Redis-master code, where "ACK 0" handler may be
// triggered before Redis is ready to transition to the streaming state and it silenty ignores
// "ACK 0". We reduce the chance it happens with this delay.
ThisFiber::SleepFor(50ms);
return error_code{};
}
// Initialize and start sub-replica for each flow.
error_code Replica::InitiateDflySync() {
auto start_time = absl::Now();
// Initialize MultiShardExecution.
multi_shard_exe_.reset(new MultiShardExecution());
// Initialize shard flows.
shard_flows_.resize(num_df_flows_);
for (unsigned i = 0; i < num_df_flows_; ++i) {
shard_flows_[i].reset(
new DflyShardReplica(server(), master_context_, i, &service_, multi_shard_exe_));
}
// Blocked on until all flows got full sync cut.
BlockingCounter sync_block{num_df_flows_};
// Switch to new error handler that closes flow sockets.
auto err_handler = [this, sync_block](const auto& ge) mutable {
// Unblock this function.
sync_block->Cancel();
// Make sure the flows are not in a state transition
lock_guard lk{flows_op_mu_};
// Unblock all sockets.
DefaultErrorHandler(ge);
for (auto& flow : shard_flows_)
flow->Cancel();
};
RETURN_ON_ERR(cntx_.SwitchErrorHandler(std::move(err_handler)));
// Make sure we're in LOADING state.
service_.RequestLoadingState();
// Start full sync flows.
state_mask_.fetch_or(R_SYNCING);
absl::Cleanup cleanup = [this]() {
// We do the following operations regardless of outcome.
JoinDflyFlows();
service_.RemoveLoadingState();
state_mask_.fetch_and(~R_SYNCING);
last_journal_LSNs_.reset();
};
std::string_view sync_type = "full";
{
// Going out of the way to avoid using std::vector<bool>...
auto is_full_sync = std::make_unique<bool[]>(num_df_flows_);
auto partition = Partition(num_df_flows_);
CHECK(!last_journal_LSNs_ || last_journal_LSNs_->size() == shard_flows_.size());
auto shard_cb = [&](unsigned index, auto*) {
for (auto id : partition[index]) {
auto ec = shard_flows_[id]->StartSyncFlow(sync_block, &cntx_,
last_journal_LSNs_.has_value()
? std::optional((*last_journal_LSNs_)[id])
: std::nullopt);
if (ec.has_value())
is_full_sync[id] = ec.value();
else
cntx_.ReportError(ec.error());
}
};
// Lock to prevent the error handler from running instantly
// while the flows are in a mixed state.
lock_guard lk{flows_op_mu_};
shard_set->pool()->AwaitFiberOnAll(std::move(shard_cb));
size_t num_full_flows =
std::accumulate(is_full_sync.get(), is_full_sync.get() + num_df_flows_, 0);
if (num_full_flows == num_df_flows_) {
if (slot_range_.has_value()) {
JournalExecutor{&service_}.FlushSlots(slot_range_.value());
} else {
JournalExecutor{&service_}.FlushAll();
}
RdbLoader::PerformPreLoad(&service_);
} else if (num_full_flows == 0) {
sync_type = "partial";
} else {
last_journal_LSNs_.reset();
cntx_.ReportError(std::make_error_code(errc::state_not_recoverable),
"Won't do a partial sync: some flows must fully resync");
}
}
RETURN_ON_ERR(cntx_.GetError());
// Send DFLY SYNC.
if (auto ec = SendNextPhaseRequest("SYNC"); ec) {
return cntx_.ReportError(ec);
}
LOG(INFO) << "Started " << sync_type << " sync with " << server().Description();
// Wait for all flows to receive full sync cut.
// In case of an error, this is unblocked by the error handler.
VLOG(1) << "Waiting for all full sync cut confirmations";
sync_block->Wait();
// Check if we woke up due to cancellation.
if (cntx_.IsCancelled())
return cntx_.GetError();
RdbLoader::PerformPostLoad(&service_);
// Send DFLY STARTSTABLE.
if (auto ec = SendNextPhaseRequest("STARTSTABLE"); ec) {
return cntx_.ReportError(ec);
}
// Joining flows and resetting state is done by cleanup.
double seconds = double(absl::ToInt64Milliseconds(absl::Now() - start_time)) / 1000;
LOG(INFO) << sync_type << " sync finished in " << strings::HumanReadableElapsedTime(seconds);
return cntx_.GetError();
}
error_code Replica::ConsumeRedisStream() {
base::IoBuf io_buf(16_KB);
io::NullSink null_sink; // we never reply back on the commands.
ConnectionContext conn_context{&null_sink, nullptr};
conn_context.is_replicating = true;
conn_context.journal_emulated = true;
conn_context.skip_acl_validation = true;
ResetParser(true);
// Master waits for this command in order to start sending replication stream.
RETURN_ON_ERR(SendCommand("REPLCONF ACK 0"));
VLOG(1) << "Before reading repl-log";
// Redis sends either pings every "repl_ping_slave_period" time inside replicationCron().
// or, alternatively, write commands stream coming from propagate() function.
// Replica connection must send "REPLCONF ACK xxx" in order to make sure that master replication
// buffer gets disposed of already processed commands, this is done in a separate fiber.
error_code ec;
LOG(INFO) << "Transitioned into stable sync";
// Set new error handler.
auto err_handler = [this](const auto& ge) {
// Trigger ack-fiber
replica_waker_.notifyAll();
DefaultErrorHandler(ge);
};
RETURN_ON_ERR(cntx_.SwitchErrorHandler(std::move(err_handler)));
facade::CmdArgVec args_vector;
acks_fb_ = fb2::Fiber("redis_acks", &Replica::RedisStreamAcksFb, this);
while (true) {
auto response = ReadRespReply(&io_buf, /*copy_msg=*/false);
if (!response.has_value()) {
VLOG(1) << "ConsumeRedisStream finished";
cntx_.ReportError(response.error());
acks_fb_.JoinIfNeeded();
return response.error();
}
if (!LastResponseArgs().empty()) {
VLOG(2) << "Got command " << absl::CHexEscape(ToSV(LastResponseArgs()[0].GetBuf()))
<< "\n consumed: " << response->total_read;
if (LastResponseArgs()[0].GetBuf()[0] == '\r') {
for (const auto& arg : LastResponseArgs()) {
LOG(INFO) << absl::CHexEscape(ToSV(arg.GetBuf()));
}
}
facade::RespExpr::VecToArgList(LastResponseArgs(), &args_vector);
CmdArgList arg_list{args_vector.data(), args_vector.size()};
service_.DispatchCommand(arg_list, &conn_context);
}
io_buf.ConsumeInput(response->left_in_buffer);
repl_offs_ += response->total_read;
replica_waker_.notify(); // Notify to trigger ACKs.
}
}
error_code Replica::ConsumeDflyStream() {
// Set new error handler that closes flow sockets.
auto err_handler = [this](const auto& ge) {
// Make sure the flows are not in a state transition
lock_guard lk{flows_op_mu_};
DefaultErrorHandler(ge);
for (auto& flow : shard_flows_) {
flow->Cancel();
}
multi_shard_exe_->CancelAllBlockingEntities();
};
RETURN_ON_ERR(cntx_.SwitchErrorHandler(std::move(err_handler)));
LOG(INFO) << "Transitioned into stable sync";
// Transition flows into stable sync.
{
auto partition = Partition(num_df_flows_);
auto shard_cb = [&](unsigned index, auto*) {
const auto& local_ids = partition[index];
for (unsigned id : local_ids) {
auto ec = shard_flows_[id]->StartStableSyncFlow(&cntx_);
if (ec)
cntx_.ReportError(ec);
}
};
// Lock to prevent error handler from running on mixed state.
lock_guard lk{flows_op_mu_};
shard_set->pool()->AwaitFiberOnAll(std::move(shard_cb));
}
JoinDflyFlows();
last_journal_LSNs_.emplace();
for (auto& flow : shard_flows_) {
last_journal_LSNs_->push_back(flow->JournalExecutedCount());
}
LOG(INFO) << "Exit stable sync";
// The only option to unblock is to cancel the context.
CHECK(cntx_.GetError());
return cntx_.GetError();
}
void Replica::JoinDflyFlows() {
for (auto& flow : shard_flows_) {
flow->JoinFlow();
}
}
void Replica::SetShardStates(bool replica) {
shard_set->RunBriefInParallel([replica](EngineShard* shard) { shard->SetReplica(replica); });
}
error_code Replica::SendNextPhaseRequest(string_view kind) {
// Ask master to start sending replication stream
string request = StrCat("DFLY ", kind, " ", master_context_.dfly_session_id);
VLOG(1) << "Sending: " << request;
RETURN_ON_ERR(SendCommandAndReadResponse(request));
PC_RETURN_ON_BAD_RESPONSE(CheckRespIsSimpleReply("OK"));
return std::error_code{};
}
io::Result<bool> DflyShardReplica::StartSyncFlow(BlockingCounter sb, Context* cntx,
std::optional<LSN> lsn) {
using nonstd::make_unexpected;
DCHECK(!master_context_.master_repl_id.empty() && !master_context_.dfly_session_id.empty());
RETURN_ON_ERR_T(make_unexpected,
ConnectAndAuth(absl::GetFlag(FLAGS_master_connect_timeout_ms) * 1ms, &cntx_));
VLOG(1) << "Sending on flow " << master_context_.master_repl_id << " "
<< master_context_.dfly_session_id << " " << flow_id_;
std::string cmd = StrCat("DFLY FLOW ", master_context_.master_repl_id, " ",
master_context_.dfly_session_id, " ", flow_id_);
// Try to negotiate a partial sync if possible.
if (lsn.has_value() && master_context_.version > DflyVersion::VER1 &&
absl::GetFlag(FLAGS_replica_partial_sync)) {
absl::StrAppend(&cmd, " ", *lsn);
}
ResetParser(/*server_mode=*/false);
leftover_buf_.emplace(128);
RETURN_ON_ERR_T(make_unexpected, SendCommand(cmd));
auto read_resp = ReadRespReply(&*leftover_buf_);
if (!read_resp.has_value()) {
return make_unexpected(read_resp.error());
}
PC_RETURN_ON_BAD_RESPONSE_T(make_unexpected,
CheckRespFirstTypes({RespExpr::STRING, RespExpr::STRING}));
string_view flow_directive = ToSV(LastResponseArgs()[0].GetBuf());
string eof_token;
PC_RETURN_ON_BAD_RESPONSE_T(make_unexpected,
flow_directive == "FULL" || flow_directive == "PARTIAL");
bool is_full_sync = flow_directive == "FULL";
eof_token = ToSV(LastResponseArgs()[1].GetBuf());
leftover_buf_->ConsumeInput(read_resp->left_in_buffer);
// We can not discard io_buf because it may contain data
// besides the response we parsed. Therefore we pass it further to ReplicateDFFb.
sync_fb_ = fb2::Fiber("shard_full_sync", &DflyShardReplica::FullSyncDflyFb, this,
std::move(eof_token), sb, cntx);
return is_full_sync;
}
error_code DflyShardReplica::StartStableSyncFlow(Context* cntx) {
DCHECK(!master_context_.master_repl_id.empty() && !master_context_.dfly_session_id.empty());
ProactorBase* mythread = ProactorBase::me();
CHECK(mythread);
if (!Sock()->IsOpen()) {
return std::make_error_code(errc::io_error);
}
sync_fb_ =
fb2::Fiber("shard_stable_sync_read", &DflyShardReplica::StableSyncDflyReadFb, this, cntx);
if (use_multi_shard_exe_sync_) {
execution_fb_ =
fb2::Fiber("shard_stable_sync_exec", &DflyShardReplica::StableSyncDflyExecFb, this, cntx);
}
return std::error_code{};
}
void DflyShardReplica::FullSyncDflyFb(std::string eof_token, BlockingCounter bc, Context* cntx) {
DCHECK(leftover_buf_);
io::PrefixSource ps{leftover_buf_->InputBuffer(), Sock()};
RdbLoader loader(&service_);
loader.SetFullSyncCutCb([bc, ran = false]() mutable {
if (!ran) {
bc->Dec();
ran = true;
}
});
// Load incoming rdb stream.
if (std::error_code ec = loader.Load(&ps); ec) {
cntx->ReportError(ec, "Error loading rdb format");
return;
}
// Try finding eof token.
io::PrefixSource chained_tail{loader.Leftover(), &ps};
if (!eof_token.empty()) {
unique_ptr<uint8_t[]> buf{new uint8_t[eof_token.size()]};
io::Result<size_t> res =
chained_tail.ReadAtLeast(io::MutableBytes{buf.get(), eof_token.size()}, eof_token.size());
if (!res || *res != eof_token.size()) {
cntx->ReportError(std::make_error_code(errc::protocol_error),
"Error finding eof token in stream");
return;
}
}
// Keep loader leftover.
io::Bytes unused = chained_tail.UnusedPrefix();
if (unused.size() > 0) {
leftover_buf_.emplace(unused.size());
leftover_buf_->WriteAndCommit(unused.data(), unused.size());
} else {
leftover_buf_.reset();
}
if (auto jo = loader.journal_offset(); jo.has_value()) {
this->journal_rec_executed_.store(*jo);
} else {
if (master_context_.version > DflyVersion::VER0)
cntx->ReportError(std::make_error_code(errc::protocol_error),
"Error finding journal offset in stream");
}
VLOG(1) << "FullSyncDflyFb finished after reading " << loader.bytes_read() << " bytes";
}
void DflyShardReplica::StableSyncDflyReadFb(Context* cntx) {
// Check leftover from full sync.
io::Bytes prefix{};
if (leftover_buf_ && leftover_buf_->InputLen() > 0) {
prefix = leftover_buf_->InputBuffer();
}
io::PrefixSource ps{prefix, Sock()};
JournalReader reader{&ps, 0};
DCHECK_GE(journal_rec_executed_, 1u);
TransactionReader tx_reader{use_multi_shard_exe_sync_,
journal_rec_executed_.load(std::memory_order_relaxed) - 1};
if (master_context_.version > DflyVersion::VER0) {
acks_fb_ = fb2::Fiber("shard_acks", &DflyShardReplica::StableSyncDflyAcksFb, this, cntx);
}
while (!cntx->IsCancelled()) {
shard_replica_waker_.await([&]() {
return ((trans_data_queue_.size() < kYieldAfterItemsInQueue) || cntx->IsCancelled());
});
if (cntx->IsCancelled())
break;
auto tx_data = tx_reader.NextTxData(&reader, cntx);
if (!tx_data)
break;
last_io_time_ = Proactor()->GetMonotonicTimeNs();
if (tx_data->opcode == journal::Op::LSN) {
// Do nothing
} else if (tx_data->opcode == journal::Op::PING) {
force_ping_ = true;
journal_rec_executed_.fetch_add(1, std::memory_order_relaxed);
} else if (tx_data->opcode == journal::Op::EXEC) {
if (use_multi_shard_exe_sync_) {
InsertTxDataToShardResource(std::move(*tx_data));
} else {
// On no shard sync mode we execute multi commands once they are recieved, therefor when
// receiving exec opcode, we only increase the journal counting.
DCHECK_EQ(tx_data->commands.size(), 0u);
journal_rec_executed_.fetch_add(1, std::memory_order_relaxed);
}
} else {
if (use_multi_shard_exe_sync_) {
InsertTxDataToShardResource(std::move(*tx_data));
} else {
ExecuteTxWithNoShardSync(std::move(*tx_data), cntx);
}
}
shard_replica_waker_.notifyAll();
}
}
void Replica::RedisStreamAcksFb() {
constexpr size_t kAckRecordMaxInterval = 1024;
std::chrono::duration ack_time_max_interval =
1ms * absl::GetFlag(FLAGS_replication_acks_interval);
std::string ack_cmd;
auto next_ack_tp = std::chrono::steady_clock::now();
while (!cntx_.IsCancelled()) {
VLOG(2) << "Sending an ACK with offset=" << repl_offs_;
ack_cmd = absl::StrCat("REPLCONF ACK ", repl_offs_);
next_ack_tp = std::chrono::steady_clock::now() + ack_time_max_interval;
if (auto ec = SendCommand(ack_cmd); ec) {
cntx_.ReportError(ec);
break;
}
ack_offs_ = repl_offs_;
replica_waker_.await_until(
[&]() { return repl_offs_ > ack_offs_ + kAckRecordMaxInterval || cntx_.IsCancelled(); },
next_ack_tp);
}
}
void DflyShardReplica::StableSyncDflyAcksFb(Context* cntx) {
constexpr size_t kAckRecordMaxInterval = 1024;
std::chrono::duration ack_time_max_interval =
1ms * absl::GetFlag(FLAGS_replication_acks_interval);
std::string ack_cmd;
auto next_ack_tp = std::chrono::steady_clock::now();
uint64_t current_offset;
while (!cntx->IsCancelled()) {
// Handle ACKs with the master. PING opcodes from the master mean we should immediately
// answer.
current_offset = journal_rec_executed_.load(std::memory_order_relaxed);
VLOG(1) << "Sending an ACK with offset=" << current_offset << " forced=" << force_ping_;
ack_cmd = absl::StrCat("REPLCONF ACK ", current_offset);
force_ping_ = false;
next_ack_tp = std::chrono::steady_clock::now() + ack_time_max_interval;
if (auto ec = SendCommand(ack_cmd); ec) {
cntx->ReportError(ec);
break;
}
ack_offs_ = current_offset;
shard_replica_waker_.await_until(
[&]() {
return journal_rec_executed_.load(std::memory_order_relaxed) >
ack_offs_ + kAckRecordMaxInterval ||
force_ping_ || cntx->IsCancelled();
},
next_ack_tp);
}
}
DflyShardReplica::DflyShardReplica(ServerContext server_context, MasterContext master_context,
uint32_t flow_id, Service* service,
std::shared_ptr<MultiShardExecution> multi_shard_exe)
: ProtocolClient(server_context),
service_(*service),
master_context_(master_context),
multi_shard_exe_(multi_shard_exe),
flow_id_(flow_id) {
use_multi_shard_exe_sync_ = GetFlag(FLAGS_enable_multi_shard_sync);
executor_ = std::make_unique<JournalExecutor>(service);
}
DflyShardReplica::~DflyShardReplica() {
JoinFlow();
}
void DflyShardReplica::ExecuteTxWithNoShardSync(TransactionData&& tx_data, Context* cntx) {
if (cntx->IsCancelled()) {
return;
}
bool was_insert = tx_data.IsGlobalCmd() &&
multi_shard_exe_->InsertTxToSharedMap(tx_data.txid, tx_data.shard_cnt);
ExecuteTx(std::move(tx_data), was_insert, cntx);
}
void DflyShardReplica::InsertTxDataToShardResource(TransactionData&& tx_data) {
bool was_insert = false;
if (tx_data.shard_cnt > 1) {
was_insert = multi_shard_exe_->InsertTxToSharedMap(tx_data.txid, tx_data.shard_cnt);
}
VLOG(2) << "txid: " << tx_data.txid << " pushed to queue";
trans_data_queue_.emplace(std::move(tx_data), was_insert);
}
void DflyShardReplica::StableSyncDflyExecFb(Context* cntx) {
while (!cntx->IsCancelled()) {
shard_replica_waker_.await(
[&]() { return (!trans_data_queue_.empty() || cntx->IsCancelled()); });
if (cntx->IsCancelled()) {
return;
}
DCHECK(!trans_data_queue_.empty());
auto& data = trans_data_queue_.front();
ExecuteTx(std::move(data.first), data.second, cntx);
trans_data_queue_.pop();
shard_replica_waker_.notifyAll();
}
}
void DflyShardReplica::ExecuteTx(TransactionData&& tx_data, bool inserted_by_me, Context* cntx) {
if (cntx->IsCancelled()) {
return;
}
if (tx_data.shard_cnt <= 1 || (!use_multi_shard_exe_sync_ && !tx_data.IsGlobalCmd())) {
VLOG(2) << "Execute cmd without sync between shards. txid: " << tx_data.txid;
executor_->Execute(tx_data.dbid, absl::MakeSpan(tx_data.commands));
journal_rec_executed_.fetch_add(tx_data.journal_rec_count, std::memory_order_relaxed);
return;
}
auto& multi_shard_data = multi_shard_exe_->Find(tx_data.txid);
VLOG(2) << "Execute txid: " << tx_data.txid << " waiting for data in all shards";
// Wait until shards flows got transaction data and inserted to map.
// This step enforces that replica will execute multi shard commands that finished on master
// and replica recieved all the commands from all shards.
multi_shard_data.block->Wait();
// Check if we woke up due to cancellation.
if (cntx_.IsCancelled())
return;
VLOG(2) << "Execute txid: " << tx_data.txid << " block wait finished";
if (tx_data.IsGlobalCmd()) {
VLOG(2) << "Execute txid: " << tx_data.txid << " global command execution";
// Wait until all shards flows get to execution step of this transaction.
multi_shard_data.barrier.Wait();
// Check if we woke up due to cancellation.
if (cntx_.IsCancelled())
return;