/
replica_state.cc
1296 lines (1105 loc) · 47 KB
/
replica_state.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
// The following only applies to changes made to this file as part of YugaByte development.
//
// Portions Copyright (c) YugaByte, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations
// under the License.
//
#include <algorithm>
#include <gflags/gflags.h>
#include "yb/consensus/consensus.h"
#include "yb/consensus/log_util.h"
#include "yb/consensus/quorum_util.h"
#include "yb/consensus/replica_state.h"
#include "yb/gutil/map-util.h"
#include "yb/gutil/strings/join.h"
#include "yb/gutil/strings/strcat.h"
#include "yb/gutil/strings/substitute.h"
#include "yb/util/debug/trace_event.h"
#include "yb/util/flag_tags.h"
#include "yb/util/logging.h"
#include "yb/util/opid.h"
#include "yb/util/status.h"
#include "yb/util/tostring.h"
#include "yb/util/trace.h"
#include "yb/util/thread_restrictions.h"
#include "yb/util/enums.h"
using namespace std::literals;
DEFINE_int32(inject_delay_commit_pre_voter_to_voter_secs, 0,
"Amount of time to delay commit of a PRE_VOTER to VOTER transition. To be used for "
"unit testing purposes only.");
TAG_FLAG(inject_delay_commit_pre_voter_to_voter_secs, unsafe);
TAG_FLAG(inject_delay_commit_pre_voter_to_voter_secs, hidden);
namespace yb {
namespace consensus {
using std::string;
using strings::Substitute;
using strings::SubstituteAndAppend;
namespace {
const int kBitsPerPackedRole = 3;
static_assert(0 <= RaftPeerPB_Role_Role_MIN, "RaftPeerPB_Role_Role_MIN must be non-negative.");
static_assert(RaftPeerPB_Role_Role_MAX < (1 << kBitsPerPackedRole),
"RaftPeerPB_Role_Role_MAX must fit in kBitsPerPackedRole bits.");
ReplicaState::PackedRoleAndTerm PackRoleAndTerm(RaftPeerPB::Role role, int64_t term) {
// Ensure we've had no more than 2305843009213693952 terms in this tablet.
CHECK_LT(term, 1ull << (8 * sizeof(ReplicaState::PackedRoleAndTerm) - kBitsPerPackedRole));
return to_underlying(role) | (term << kBitsPerPackedRole);
}
int64_t UnpackTerm(ReplicaState::PackedRoleAndTerm role_and_term) {
return role_and_term >> kBitsPerPackedRole;
}
RaftPeerPB::Role UnpackRole(ReplicaState::PackedRoleAndTerm role_and_term) {
return static_cast<RaftPeerPB::Role>(role_and_term & ((1 << kBitsPerPackedRole) - 1));
}
} // anonymous namespace
//////////////////////////////////////////////////
// ReplicaState
//////////////////////////////////////////////////
ReplicaState::ReplicaState(ConsensusOptions options, string peer_uuid,
std::unique_ptr<ConsensusMetadata> cmeta,
ReplicaOperationFactory* operation_factory,
SafeOpIdWaiter* safe_op_id_waiter,
RetryableRequests* retryable_requests)
: options_(std::move(options)),
peer_uuid_(std::move(peer_uuid)),
cmeta_(std::move(cmeta)),
operation_factory_(operation_factory),
safe_op_id_waiter_(safe_op_id_waiter) {
CHECK(cmeta_) << "ConsensusMeta passed as NULL";
if (retryable_requests) {
retryable_requests_ = std::move(*retryable_requests);
}
CHECK(leader_state_cache_.is_lock_free());
StoreRoleAndTerm(cmeta_->active_role(), cmeta_->current_term());
// Actually we don't need this lock, but GetActiveRoleUnlocked checks that we are holding the
// lock.
auto lock = LockForRead();
CoarseTimePoint now;
RefreshLeaderStateCacheUnlocked(&now);
}
ReplicaState::~ReplicaState() {
}
Status ReplicaState::StartUnlocked(const OpId& last_id_in_wal) {
DCHECK(IsLocked());
// Our last persisted term can be higher than the last persisted operation
// (i.e. if we called an election) but reverse should never happen.
CHECK_LE(last_id_in_wal.term(), GetCurrentTermUnlocked()) << LogPrefix()
<< "The last op in the WAL with id " << OpIdToString(last_id_in_wal)
<< " has a term (" << last_id_in_wal.term() << ") that is greater "
<< "than the latest recorded term, which is " << GetCurrentTermUnlocked();
next_index_ = last_id_in_wal.index() + 1;
last_received_op_id_.CopyFrom(last_id_in_wal);
state_ = kRunning;
return Status::OK();
}
Status ReplicaState::LockForStart(UniqueLock* lock) const {
ThreadRestrictions::AssertWaitAllowed();
UniqueLock l(update_lock_);
CHECK_EQ(state_, kInitialized) << "Illegal state for Start()."
<< " Replica is not in kInitialized state";
lock->swap(l);
return Status::OK();
}
bool ReplicaState::IsLocked() const {
std::unique_lock<std::mutex> lock(update_lock_, std::try_to_lock);
return !lock.owns_lock();
}
ReplicaState::UniqueLock ReplicaState::LockForRead() const {
ThreadRestrictions::AssertWaitAllowed();
return UniqueLock(update_lock_);
}
Status ReplicaState::LockForReplicate(UniqueLock* lock, const ReplicateMsg& msg) const {
DCHECK(!msg.has_id()) << "Should not have an ID yet: " << msg.ShortDebugString();
CHECK(msg.has_op_type()); // TODO: better checking?
return LockForReplicate(lock);
}
Status ReplicaState::LockForReplicate(UniqueLock* lock) const {
ThreadRestrictions::AssertWaitAllowed();
UniqueLock l(update_lock_);
if (PREDICT_FALSE(state_ != kRunning)) {
return STATUS(IllegalState, "Replica not in running state");
}
lock->swap(l);
return Status::OK();
}
Status ReplicaState::CheckIsActiveLeaderAndHasLease() const {
UniqueLock l(update_lock_);
if (PREDICT_FALSE(state_ != kRunning)) {
return STATUS(IllegalState, "Replica not in running state");
}
return CheckActiveLeaderUnlocked(LeaderLeaseCheckMode::NEED_LEASE);
}
Status ReplicaState::LockForMajorityReplicatedIndexUpdate(UniqueLock* lock) const {
TRACE_EVENT0("consensus", "ReplicaState::LockForMajorityReplicatedIndexUpdate");
ThreadRestrictions::AssertWaitAllowed();
UniqueLock l(update_lock_);
if (PREDICT_FALSE(state_ != kRunning)) {
return STATUS(IllegalState, "Replica not in running state");
}
if (PREDICT_FALSE(GetActiveRoleUnlocked() != RaftPeerPB::LEADER)) {
return STATUS(IllegalState, "Replica not LEADER");
}
lock->swap(l);
return Status::OK();
}
LeaderState ReplicaState::GetLeaderState() const {
auto cache = leader_state_cache_.load(std::memory_order_acquire);
CoarseTimePoint now = CoarseMonoClock::Now();
if (now >= cache.expire_at) {
auto lock = LockForRead();
return RefreshLeaderStateCacheUnlocked(&now);
}
LeaderState result = {cache.status()};
if (result.status == LeaderStatus::LEADER_AND_READY) {
result.term = cache.extra_value();
} else {
if (result.status == LeaderStatus::LEADER_BUT_OLD_LEADER_MAY_HAVE_LEASE) {
result.remaining_old_leader_lease = MonoDelta::FromMicroseconds(cache.extra_value());
}
result.MakeNotReadyLeader(result.status);
}
return result;
}
LeaderState ReplicaState::GetLeaderStateUnlocked(
LeaderLeaseCheckMode lease_check_mode, CoarseTimePoint* now) const {
LeaderState result;
if (GetActiveRoleUnlocked() != RaftPeerPB::LEADER) {
return result.MakeNotReadyLeader(LeaderStatus::NOT_LEADER);
}
if (!leader_no_op_committed_) {
// This will cause the client to retry on the same server (won't try to find the new leader).
return result.MakeNotReadyLeader(LeaderStatus::LEADER_BUT_NO_OP_NOT_COMMITTED);
}
const auto lease_status = lease_check_mode != LeaderLeaseCheckMode::DONT_NEED_LEASE
? GetLeaderLeaseStatusUnlocked(&result.remaining_old_leader_lease, now)
: LeaderLeaseStatus::HAS_LEASE;
switch (lease_status) {
case LeaderLeaseStatus::OLD_LEADER_MAY_HAVE_LEASE:
// Will retry on the same server.
VLOG(1) << "Old leader lease might still be active for "
<< result.remaining_old_leader_lease.ToString();
return result.MakeNotReadyLeader(
LeaderStatus::LEADER_BUT_OLD_LEADER_MAY_HAVE_LEASE);
case LeaderLeaseStatus::NO_MAJORITY_REPLICATED_LEASE:
// Will retry to look up the leader, because it might have changed.
return result.MakeNotReadyLeader(
LeaderStatus::LEADER_BUT_NO_MAJORITY_REPLICATED_LEASE);
case LeaderLeaseStatus::HAS_LEASE:
result.status = LeaderStatus::LEADER_AND_READY;
result.term = GetCurrentTermUnlocked();
return result;
}
FATAL_INVALID_ENUM_VALUE(LeaderLeaseStatus, lease_status);
}
Status ReplicaState::CheckActiveLeaderUnlocked(LeaderLeaseCheckMode lease_check_mode) const {
auto state = GetLeaderStateUnlocked(lease_check_mode);
if (state.status == LeaderStatus::NOT_LEADER) {
ConsensusStatePB cstate = ConsensusStateUnlocked(CONSENSUS_CONFIG_ACTIVE);
return STATUS_FORMAT(IllegalState,
"Replica $0 is not leader of this config. Role: $1. Consensus state: $2",
peer_uuid_, RaftPeerPB::Role_Name(GetActiveRoleUnlocked()), cstate);
}
return state.CreateStatus();
}
Status ReplicaState::LockForConfigChange(UniqueLock* lock) const {
TRACE_EVENT0("consensus", "ReplicaState::LockForConfigChange");
ThreadRestrictions::AssertWaitAllowed();
UniqueLock l(update_lock_);
// Can only change the config on running replicas.
if (PREDICT_FALSE(state_ != kRunning)) {
return STATUS(IllegalState, "Unable to lock ReplicaState for config change",
Substitute("State = $0", state_));
}
lock->swap(l);
return Status::OK();
}
Status ReplicaState::LockForUpdate(UniqueLock* lock) const {
TRACE_EVENT0("consensus", "ReplicaState::LockForUpdate");
ThreadRestrictions::AssertWaitAllowed();
UniqueLock l(update_lock_);
if (PREDICT_FALSE(state_ != kRunning)) {
return STATUS(IllegalState, "Replica not in running state");
}
lock->swap(l);
return Status::OK();
}
Status ReplicaState::LockForShutdown(UniqueLock* lock) {
TRACE_EVENT0("consensus", "ReplicaState::LockForShutdown");
ThreadRestrictions::AssertWaitAllowed();
UniqueLock l(update_lock_);
if (state_ != kShuttingDown && state_ != kShutDown) {
state_ = kShuttingDown;
}
lock->swap(l);
return Status::OK();
}
Status ReplicaState::ShutdownUnlocked() {
DCHECK(IsLocked());
CHECK_EQ(state_, kShuttingDown);
state_ = kShutDown;
return Status::OK();
}
ConsensusStatePB ReplicaState::ConsensusStateUnlocked(ConsensusConfigType type) const {
return cmeta_->ToConsensusStatePB(type);
}
RaftPeerPB::Role ReplicaState::GetActiveRoleUnlocked() const {
DCHECK(IsLocked());
return cmeta_->active_role();
}
bool ReplicaState::IsConfigChangePendingUnlocked() const {
DCHECK(IsLocked());
return cmeta_->has_pending_config();
}
Status ReplicaState::CheckNoConfigChangePendingUnlocked() const {
DCHECK(IsLocked());
if (IsConfigChangePendingUnlocked()) {
return STATUS(IllegalState,
Substitute("RaftConfig change currently pending. Only one is allowed at a time.\n"
" Committed config: $0.\n Pending config: $1",
GetCommittedConfigUnlocked().ShortDebugString(),
GetPendingConfigUnlocked().ShortDebugString()));
}
return Status::OK();
}
Status ReplicaState::SetPendingConfigUnlocked(const RaftConfigPB& new_config) {
DCHECK(IsLocked());
RETURN_NOT_OK_PREPEND(VerifyRaftConfig(new_config, UNCOMMITTED_QUORUM),
"Invalid config to set as pending");
CHECK(!cmeta_->has_pending_config())
<< "Attempt to set pending config while another is already pending! "
<< "Existing pending config: " << cmeta_->pending_config().ShortDebugString() << "; "
<< "Attempted new pending config: " << new_config.ShortDebugString();
cmeta_->set_pending_config(new_config);
CoarseTimePoint now;
RefreshLeaderStateCacheUnlocked(&now);
return Status::OK();
}
Status ReplicaState::ClearPendingConfigUnlocked() {
DCHECK(IsLocked());
if (!cmeta_->has_pending_config()) {
LOG(WARNING) << "Attempt to clear a non-existent pending config."
<< "Existing committed config: " << cmeta_->committed_config().ShortDebugString();
return STATUS(IllegalState, "Attempt to clear a non-existent pending config.");
}
cmeta_->clear_pending_config();
CoarseTimePoint now;
RefreshLeaderStateCacheUnlocked(&now);
return Status::OK();
}
const RaftConfigPB& ReplicaState::GetPendingConfigUnlocked() const {
DCHECK(IsLocked());
CHECK(IsConfigChangePendingUnlocked()) << "No pending config";
return cmeta_->pending_config();
}
Status ReplicaState::SetCommittedConfigUnlocked(const RaftConfigPB& committed_config) {
TRACE_EVENT0("consensus", "ReplicaState::SetCommittedConfigUnlocked");
DCHECK(IsLocked());
DCHECK(committed_config.IsInitialized());
RETURN_NOT_OK_PREPEND(VerifyRaftConfig(committed_config, COMMITTED_QUORUM),
"Invalid config to set as committed");
// Compare committed with pending configuration, ensure they are the same.
// Pending will not have an opid_index, so ignore that field.
DCHECK(cmeta_->has_pending_config());
RaftConfigPB config_no_opid = committed_config;
config_no_opid.clear_opid_index();
const RaftConfigPB& pending_config = GetPendingConfigUnlocked();
// Quorums must be exactly equal, even w.r.t. peer ordering.
CHECK_EQ(GetPendingConfigUnlocked().SerializeAsString(), config_no_opid.SerializeAsString())
<< Substitute("New committed config must equal pending config, but does not. "
"Pending config: $0, committed config: $1",
pending_config.ShortDebugString(), committed_config.ShortDebugString());
cmeta_->set_committed_config(committed_config);
cmeta_->clear_pending_config();
CoarseTimePoint now;
RefreshLeaderStateCacheUnlocked(&now);
CHECK_OK(cmeta_->Flush());
return Status::OK();
}
const RaftConfigPB& ReplicaState::GetCommittedConfigUnlocked() const {
DCHECK(IsLocked());
return cmeta_->committed_config();
}
const RaftConfigPB& ReplicaState::GetActiveConfigUnlocked() const {
DCHECK(IsLocked());
return cmeta_->active_config();
}
bool ReplicaState::IsOpCommittedOrPending(const OpId& op_id, bool* term_mismatch) {
DCHECK(IsLocked());
*term_mismatch = false;
if (cmeta_ == nullptr) {
LOG(FATAL) << "cmeta_ cannot be NULL";
}
int64_t committed_index = GetCommittedOpIdUnlocked().index();
if (op_id.index() <= committed_index) {
return true;
}
int64_t last_received_index = GetLastReceivedOpIdUnlocked().index();
if (op_id.index() > last_received_index) {
return false;
}
scoped_refptr<ConsensusRound> round = GetPendingOpByIndexOrNullUnlocked(op_id.index());
if (round == nullptr) {
LOG_WITH_PREFIX(ERROR)
<< "Consensus round not found for op id " << op_id << ": "
<< "committed_index=" << committed_index << ", "
<< "last_received_index=" << last_received_index << ", "
<< "tablet: " << options_.tablet_id << ", current state: "
<< ToStringUnlocked();
DumpPendingOperationsUnlocked();
CHECK(false);
}
if (round->id().term() != op_id.term()) {
*term_mismatch = true;
return false;
}
return true;
}
Status ReplicaState::SetCurrentTermUnlocked(int64_t new_term) {
TRACE_EVENT1("consensus", "ReplicaState::SetCurrentTermUnlocked",
"term", new_term);
DCHECK(IsLocked());
if (PREDICT_FALSE(new_term <= GetCurrentTermUnlocked())) {
return STATUS(IllegalState,
Substitute("Cannot change term to a term that is lower than or equal to the current one. "
"Current: $0, Proposed: $1", GetCurrentTermUnlocked(), new_term));
}
cmeta_->set_current_term(new_term);
cmeta_->clear_voted_for();
// OK to flush before clearing the leader, because the leader UUID is not part of
// ConsensusMetadataPB.
CHECK_OK(cmeta_->Flush());
ClearLeaderUnlocked();
// No need to call StoreRoleAndTerm here, because ClearLeaderUnlocked already calls it.
last_received_op_id_current_leader_ = MinimumOpId();
return Status::OK();
}
const int64_t ReplicaState::GetCurrentTermUnlocked() const {
DCHECK(IsLocked());
return cmeta_->current_term();
}
void ReplicaState::SetLeaderUuidUnlocked(const std::string& uuid) {
DCHECK(IsLocked());
cmeta_->set_leader_uuid(uuid);
StoreRoleAndTerm(cmeta_->active_role(), cmeta_->current_term());
CoarseTimePoint now;
RefreshLeaderStateCacheUnlocked(&now);
}
const string& ReplicaState::GetLeaderUuidUnlocked() const {
DCHECK(IsLocked());
return cmeta_->leader_uuid();
}
const bool ReplicaState::HasVotedCurrentTermUnlocked() const {
DCHECK(IsLocked());
return cmeta_->has_voted_for();
}
Status ReplicaState::SetVotedForCurrentTermUnlocked(const std::string& uuid) {
TRACE_EVENT1("consensus", "ReplicaState::SetVotedForCurrentTermUnlocked",
"uuid", uuid);
DCHECK(IsLocked());
cmeta_->set_voted_for(uuid);
CHECK_OK(cmeta_->Flush());
return Status::OK();
}
const std::string& ReplicaState::GetVotedForCurrentTermUnlocked() const {
DCHECK(IsLocked());
DCHECK(cmeta_->has_voted_for());
return cmeta_->voted_for();
}
ReplicaOperationFactory* ReplicaState::GetReplicaOperationFactoryUnlocked() const {
return operation_factory_;
}
const string& ReplicaState::GetPeerUuid() const {
return peer_uuid_;
}
const ConsensusOptions& ReplicaState::GetOptions() const {
return options_;
}
void ReplicaState::DumpPendingOperationsUnlocked() {
DCHECK(IsLocked());
LOG_WITH_PREFIX(INFO) << "Dumping " << pending_operations_.size()
<< " pending operations.";
for (const auto &operation : pending_operations_) {
LOG_WITH_PREFIX(INFO) << operation.second->replicate_msg()->ShortDebugString();
}
}
Status ReplicaState::CancelPendingOperations() {
{
ThreadRestrictions::AssertWaitAllowed();
UniqueLock lock(update_lock_);
if (state_ != kShuttingDown) {
return STATUS(IllegalState, "Can only wait for pending commits on kShuttingDown state.");
}
if (pending_operations_.empty()) {
return Status::OK();
}
LOG_WITH_PREFIX(INFO) << "Trying to abort " << pending_operations_.size()
<< " pending operations.";
auto abort_status = STATUS(Aborted, "Operation aborted");
for (const auto& operation : pending_operations_) {
const scoped_refptr<ConsensusRound>& round = operation.second;
// We cancel only operations whose applies have not yet been triggered.
LOG_WITH_PREFIX(INFO) << "Aborting operation as it isn't in flight: "
<< operation.second->replicate_msg()->ShortDebugString();
NotifyReplicationFinishedUnlocked(round, abort_status, yb::OpId::kUnknownTerm);
}
}
return Status::OK();
}
Status ReplicaState::AbortOpsAfterUnlocked(int64_t new_preceding_idx) {
DCHECK(IsLocked());
LOG_WITH_PREFIX(INFO)
<< "Aborting all operations after (but not including): "
<< new_preceding_idx << ". Current State: " << ToStringUnlocked();
DCHECK_GE(new_preceding_idx, 0);
OpId new_preceding;
auto iter = pending_operations_.lower_bound(new_preceding_idx);
// Either the new preceding id is in the pendings set or it must be equal to the
// committed index since we can't truncate already committed operations.
if (iter != pending_operations_.end() && (*iter).first == new_preceding_idx) {
new_preceding = (*iter).second->replicate_msg()->id();
++iter;
} else {
CHECK_EQ(new_preceding_idx, last_committed_index_.index());
new_preceding = last_committed_index_;
}
// This is the same as UpdateLastReceivedOpIdUnlocked() but we do it
// here to avoid the bounds check, since we're breaking monotonicity.
last_received_op_id_ = new_preceding;
last_received_op_id_current_leader_ = last_received_op_id_;
next_index_ = new_preceding.index() + 1;
auto abort_status = STATUS(Aborted, "Operation aborted by new leader");
for (auto it = iter; it != pending_operations_.end(); ++it) {
const scoped_refptr<ConsensusRound>& round = it->second;
LOG_WITH_PREFIX(INFO) << "Aborting uncommitted operation due to leader change: "
<< round->replicate_msg()->id();
NotifyReplicationFinishedUnlocked(round, abort_status, yb::OpId::kUnknownTerm);
}
// Clear entries from pendings.
pending_operations_.erase(iter, pending_operations_.end());
return Status::OK();
}
Status ReplicaState::AddPendingOperation(const scoped_refptr<ConsensusRound>& round) {
DCHECK(IsLocked());
auto op_type = round->replicate_msg()->op_type();
if (PREDICT_FALSE(state_ != kRunning)) {
// Special case when we're configuring and this is a config change, refuse
// everything else.
// TODO: Don't require a NO_OP to get to kRunning state
if (op_type != NO_OP) {
return STATUS(IllegalState, "Cannot trigger prepare. Replica is not in kRunning state.");
}
}
// When we do not have a hybrid time leader lease we allow 2 operation types to be added to RAFT.
// NO_OP - because even empty heartbeat messages could be used to obtain the lease.
// CHANGE_CONFIG_OP - because we should be able to update consensus even w/o lease.
// Both of them are safe, since they don't affect user reads or writes.
if (GetActiveRoleUnlocked() == RaftPeerPB::LEADER &&
op_type != NO_OP &&
op_type != CHANGE_CONFIG_OP) {
auto lease_status = GetHybridTimeLeaseStatusAtUnlocked(
HybridTime(round->replicate_msg()->hybrid_time()).GetPhysicalValueMicros());
static_assert(LeaderLeaseStatus_ARRAYSIZE == 3, "Please update logic below to adapt new state");
if (lease_status == LeaderLeaseStatus::OLD_LEADER_MAY_HAVE_LEASE) {
return STATUS_FORMAT(LeaderHasNoLease,
"Old leader may have hybrid time lease, while adding: $0",
OperationType_Name(op_type));
}
lease_status = GetLeaderLeaseStatusUnlocked(nullptr);
if (lease_status == LeaderLeaseStatus::OLD_LEADER_MAY_HAVE_LEASE) {
return STATUS_FORMAT(LeaderHasNoLease,
"Old leader may have lease, while adding: $0",
OperationType_Name(op_type));
}
}
// Mark pending configuration.
if (PREDICT_FALSE(op_type == CHANGE_CONFIG_OP)) {
DCHECK(round->replicate_msg()->change_config_record().has_old_config());
DCHECK(round->replicate_msg()->change_config_record().old_config().has_opid_index());
DCHECK(round->replicate_msg()->change_config_record().has_new_config());
DCHECK(!round->replicate_msg()->change_config_record().new_config().has_opid_index());
if (GetActiveRoleUnlocked() != RaftPeerPB::LEADER) {
const RaftConfigPB& old_config = round->replicate_msg()->change_config_record().old_config();
const RaftConfigPB& new_config = round->replicate_msg()->change_config_record().new_config();
// The leader has to mark the configuration as pending before it gets here
// because the active configuration affects the replication queue.
// Do one last sanity check.
Status s = CheckNoConfigChangePendingUnlocked();
if (PREDICT_FALSE(!s.ok())) {
s = s.CloneAndAppend(Format("New config: $0", new_config));
LOG_WITH_PREFIX(INFO) << s;
return s;
}
// Check if the pending Raft config has an OpId less than the committed
// config. If so, this is a replay at startup in which the COMMIT
// messages were delayed.
const RaftConfigPB& committed_config = GetCommittedConfigUnlocked();
if (round->replicate_msg()->id().index() > committed_config.opid_index()) {
CHECK_OK(SetPendingConfigUnlocked(new_config));
} else {
LOG_WITH_PREFIX(INFO)
<< "Ignoring setting pending config change with OpId "
<< round->replicate_msg()->id() << " because the committed config has OpId index "
<< committed_config.opid_index() << ". The config change we are ignoring is: "
<< "Old config: { " << old_config.ShortDebugString() << " }. "
<< "New config: { " << new_config.ShortDebugString() << " }";
}
}
} else if (op_type == WRITE_OP) {
if (!retryable_requests_.Register(round)) {
return STATUS(AlreadyPresent, "Duplicate request");
}
}
InsertOrDie(&pending_operations_, round->replicate_msg()->id().index(), round);
return Status::OK();
}
scoped_refptr<ConsensusRound> ReplicaState::GetPendingOpByIndexOrNullUnlocked(int64_t index) {
DCHECK(IsLocked());
return FindPtrOrNull(pending_operations_, index);
}
Status ReplicaState::UpdateMajorityReplicatedUnlocked(const OpId& majority_replicated,
OpId* committed_index,
bool* committed_index_changed) {
DCHECK(IsLocked());
DCHECK(majority_replicated.IsInitialized());
DCHECK(last_committed_index_.IsInitialized());
if (PREDICT_FALSE(state_ == kShuttingDown || state_ == kShutDown)) {
return STATUS(ServiceUnavailable, "Cannot trigger apply. Replica is shutting down.");
}
if (PREDICT_FALSE(state_ != kRunning)) {
return STATUS(IllegalState, "Cannot trigger apply. Replica is not in kRunning state.");
}
// If the last committed operation was in the current term (the normal case)
// then 'committed_index' is simply equal to majority replicated.
if (last_committed_index_.term() == GetCurrentTermUnlocked()) {
RETURN_NOT_OK(AdvanceCommittedIndexUnlocked(majority_replicated,
committed_index_changed));
committed_index->CopyFrom(last_committed_index_);
return Status::OK();
}
// If the last committed operation is not in the current term (such as when
// we change leaders) but 'majority_replicated' is then we can advance the
// 'committed_index' too.
if (majority_replicated.term() == GetCurrentTermUnlocked()) {
OpId previous = last_committed_index_;
RETURN_NOT_OK(AdvanceCommittedIndexUnlocked(majority_replicated,
committed_index_changed));
committed_index->CopyFrom(last_committed_index_);
LOG_WITH_PREFIX(INFO)
<< "Advanced the committed_index across terms."
<< " Last committed operation was: " << previous.ShortDebugString()
<< " New committed index is: " << last_committed_index_.ShortDebugString();
return Status::OK();
}
committed_index->CopyFrom(last_committed_index_);
YB_LOG_EVERY_N_SECS(WARNING, 1) << LogPrefix()
<< "Can't advance the committed index across term boundaries"
<< " until operations from the current term are replicated."
<< " Last committed operation was: " << last_committed_index_.ShortDebugString() << ","
<< " New majority replicated is: " << majority_replicated.ShortDebugString() << ","
<< " Current term is: " << GetCurrentTermUnlocked();
return Status::OK();
}
void ReplicaState::SetLastCommittedIndexUnlocked(const OpId& committed_index) {
DCHECK(IsLocked());
CHECK_GE(last_received_op_id_.index(), committed_index.index());
last_committed_index_ = committed_index;
}
void ReplicaState::StoreRoleAndTerm(RaftPeerPB::Role role, int64_t term) {
role_and_term_.store(PackRoleAndTerm(role, term), std::memory_order_release);
}
Status ReplicaState::InitCommittedIndexUnlocked(const OpId& committed_index) {
if (!OpIdEquals(last_committed_index_, MinimumOpId())) {
return STATUS_FORMAT(
IllegalState,
"Committed index already initialized to: $0, tried to set $1",
last_committed_index_,
committed_index);
}
if (!pending_operations_.empty() &&
committed_index.index() >= pending_operations_.begin()->first) {
auto status = ApplyPendingOperationsUnlocked(pending_operations_.begin(), committed_index);
if (!status.ok()) {
return status;
}
}
SetLastCommittedIndexUnlocked(committed_index);
return Status::OK();
}
Status ReplicaState::CheckOperationExist(const OpId& committed_index,
IndexToRoundMap::iterator* end_iter) {
auto prev = pending_operations_.upper_bound(committed_index.index());
if (prev == pending_operations_.begin()) {
return STATUS_FORMAT(
NotFound,
"No pending entries before committed index: $0 => $1, stack: $2, pending: $3",
last_committed_index_,
committed_index,
GetStackTrace(),
pending_operations_);
}
*end_iter = prev;
--prev;
const auto prev_id = prev->second->id();
if (!OpIdEquals(prev_id, committed_index)) {
return STATUS_FORMAT(NotFound,
"No pending entry with committed index: $0 => $1, stack: $2, pending: $3",
last_committed_index_,
committed_index,
GetStackTrace(),
pending_operations_);
}
return Status::OK();
}
Status ReplicaState::AdvanceCommittedIndexUnlocked(const OpId& committed_index,
bool *committed_index_changed) {
DCHECK(IsLocked());
if (committed_index_changed) {
*committed_index_changed = false;
}
// If we already committed up to (or past) 'id' return.
// This can happen in the case that multiple UpdateConsensus() calls end
// up in the RPC queue at the same time, and then might get interleaved out
// of order.
if (last_committed_index_.index() >= committed_index.index()) {
VLOG_WITH_PREFIX(1)
<< "Already marked ops through " << last_committed_index_ << " as committed. "
<< "Now trying to mark " << committed_index << " which would be a no-op.";
return Status::OK();
}
if (pending_operations_.empty()) {
VLOG_WITH_PREFIX(1) << "No operations to mark as committed up to: "
<< committed_index.ShortDebugString();
return STATUS_SUBSTITUTE(
NotFound,
"No pending entries, requested to advance last committed OpId from $0 to $1, "
"last received: $2",
last_committed_index_.ShortDebugString(),
committed_index.ShortDebugString(),
last_received_op_id_.ShortDebugString());
}
// Start at the operation after the last committed one.
auto iter = pending_operations_.upper_bound(last_committed_index_.index());
CHECK(iter != pending_operations_.end());
auto status = ApplyPendingOperationsUnlocked(iter, committed_index);
if (!status.ok()) {
return status;
}
if (committed_index_changed) {
*committed_index_changed = true;
}
return Status::OK();
}
Status ReplicaState::ApplyPendingOperationsUnlocked(IndexToRoundMap::iterator iter,
const OpId &committed_index) {
DCHECK(IsLocked());
VLOG_WITH_PREFIX(1)
<< "Last triggered apply was: " << last_committed_index_.ShortDebugString()
<< " Starting to apply from log index: " << (*iter).first;
// Stop at the operation after the last one we must commit. This iterator by definition points to
// the first entry greater than the committed index, so the entry preceding that must have the
// OpId equal to committed_index.
IndexToRoundMap::iterator end_iter;
auto status = CheckOperationExist(committed_index, &end_iter);
if (!status.ok()) {
return status;
}
OpId prev_id = last_committed_index_;
yb::OpId max_allowed_op_id;
if (!safe_op_id_waiter_) {
max_allowed_op_id.index = std::numeric_limits<int64_t>::max();
}
auto leader_term = GetLeaderStateUnlocked().term;
while (iter != end_iter) {
scoped_refptr<ConsensusRound> round = (*iter).second; // Make a copy.
DCHECK(round);
const OpId& current_id = round->id();
if (PREDICT_TRUE(!OpIdEquals(prev_id, MinimumOpId()))) {
CHECK_OK(CheckOpInSequence(prev_id, current_id));
}
auto type = round->replicate_msg()->op_type();
// For write operations we block rocksdb flush, until appropriate records are written to the
// log file. So we could apply them before adding to log.
if (type != OperationType::WRITE_OP &&
(current_id.index() > max_allowed_op_id.index ||
current_id.term() > max_allowed_op_id.term)) {
max_allowed_op_id = safe_op_id_waiter_->WaitForSafeOpIdToApply(yb::OpId::FromPB(current_id));
DCHECK_GE(max_allowed_op_id.index, current_id.index());
DCHECK_GE(max_allowed_op_id.term, current_id.term());
}
pending_operations_.erase(iter++);
// Set committed configuration.
if (PREDICT_FALSE(type == OperationType::CHANGE_CONFIG_OP)) {
ApplyConfigChangeUnlocked(round);
}
prev_id.CopyFrom(round->id());
NotifyReplicationFinishedUnlocked(round, Status::OK(), leader_term);
}
SetLastCommittedIndexUnlocked(committed_index);
return Status::OK();
}
void ReplicaState::ApplyConfigChangeUnlocked(const ConsensusRoundPtr& round) {
DCHECK(round->replicate_msg()->change_config_record().has_old_config());
DCHECK(round->replicate_msg()->change_config_record().has_new_config());
RaftConfigPB old_config = round->replicate_msg()->change_config_record().old_config();
RaftConfigPB new_config = round->replicate_msg()->change_config_record().new_config();
DCHECK(old_config.has_opid_index());
DCHECK(!new_config.has_opid_index());
const OpId& current_id = round->id();
if (PREDICT_FALSE(FLAGS_inject_delay_commit_pre_voter_to_voter_secs)) {
bool is_transit_to_voter =
CountVotersInTransition(old_config) > CountVotersInTransition(new_config);
if (is_transit_to_voter) {
LOG_WITH_PREFIX(INFO)
<< "Commit skipped as inject_delay_commit_pre_voter_to_voter_secs flag is set to true.\n"
<< " Old config: { " << old_config.ShortDebugString() << " }.\n"
<< " New config: { " << new_config.ShortDebugString() << " }";
SleepFor(MonoDelta::FromSeconds(FLAGS_inject_delay_commit_pre_voter_to_voter_secs));
}
}
new_config.set_opid_index(current_id.index());
// Check if the pending Raft config has an OpId less than the committed
// config. If so, this is a replay at startup in which the COMMIT
// messages were delayed.
const RaftConfigPB& committed_config = GetCommittedConfigUnlocked();
if (new_config.opid_index() > committed_config.opid_index()) {
LOG_WITH_PREFIX(INFO)
<< "Committing config change with OpId "
<< current_id << ". Old config: { " << old_config.ShortDebugString() << " }. "
<< "New config: { " << new_config.ShortDebugString() << " }";
CHECK_OK(SetCommittedConfigUnlocked(new_config));
} else {
LOG_WITH_PREFIX(INFO)
<< "Ignoring commit of config change with OpId "
<< current_id << " because the committed config has OpId index "
<< committed_config.opid_index() << ". The config change we are ignoring is: "
<< "Old config: { " << old_config.ShortDebugString() << " }. "
<< "New config: { " << new_config.ShortDebugString() << " }";
}
}
const OpId& ReplicaState::GetCommittedOpIdUnlocked() const {
DCHECK(IsLocked());
return last_committed_index_;
}
RestartSafeCoarseMonoClock& ReplicaState::Clock() {
return retryable_requests_.Clock();
}
RetryableRequestsCounts ReplicaState::TEST_CountRetryableRequests() {
auto lock = LockForRead();
return retryable_requests_.TEST_Counts();
}
bool ReplicaState::AreCommittedAndCurrentTermsSameUnlocked() const {
int64_t term = GetCurrentTermUnlocked();
const OpId &opid = GetCommittedOpIdUnlocked();
if (opid.term() != term) {
LOG(INFO) << "committed term=" << opid.term() << ", current term=" << term;
return false;
}
return true;
}
void ReplicaState::UpdateLastReceivedOpIdUnlocked(const OpId& op_id) {
DCHECK(IsLocked());
auto* trace = Trace::CurrentTrace();
DCHECK_LE(OpIdCompare(last_received_op_id_, op_id), 0)
<< LogPrefix() << ": "
<< "Previously received OpId: " << last_received_op_id_.ShortDebugString()
<< ", updated OpId: " << op_id.ShortDebugString()
<< ", Trace:" << std::endl << (trace ? trace->DumpToString(true) : "No trace found");
last_received_op_id_ = op_id;
last_received_op_id_current_leader_ = last_received_op_id_;
next_index_ = op_id.index() + 1;
}
const OpId& ReplicaState::GetLastReceivedOpIdUnlocked() const {
DCHECK(IsLocked());
return last_received_op_id_;
}
const OpId& ReplicaState::GetLastReceivedOpIdCurLeaderUnlocked() const {
DCHECK(IsLocked());
return last_received_op_id_current_leader_;
}
OpId ReplicaState::GetLastPendingOperationOpIdUnlocked() const {
DCHECK(IsLocked());
return pending_operations_.empty()
? MinimumOpId() : (--pending_operations_.end())->second->id();
}
void ReplicaState::NewIdUnlocked(OpId* id) {
DCHECK(IsLocked());
id->set_term(GetCurrentTermUnlocked());
id->set_index(next_index_++);
}
void ReplicaState::CancelPendingOperation(const OpId& id, bool should_exist) {
OpId previous = id;
previous.set_index(previous.index() - 1);
DCHECK(IsLocked());
CHECK_EQ(GetCurrentTermUnlocked(), id.term());
CHECK_EQ(next_index_, id.index() + 1);
next_index_ = id.index();