-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprogress.cc
More file actions
164 lines (137 loc) · 3.77 KB
/
Copy pathprogress.cc
File metadata and controls
164 lines (137 loc) · 3.77 KB
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
#include <algorithm>
#include <cassert>
#include "progress.h"
#include "log_utils.h"
#include "hassert.h"
namespace raft {
bool Progress::MaybeDecreaseNext(
uint64_t rejected_index, uint64_t rejected_hint)
{
if (ProgressState::REPLICATE == state_) {
if (rejected_index <= matched_) {
return false;
}
assert(rejected_index > matched_);
next_ = matched_ + 1;
curr_max_size_ = std::max<uint32_t>(1, curr_max_size_ >> 1);
logerr("TEST: logid %lu next_ %lu matched_ %lu curr_max_size_ %u",
logid_, next_, matched_, curr_max_size_);
return true;
}
// no need expential proble..
assert(ProgressState::PROBE == state_);
// what if rejected_index from follower missing ??
// printf ( "rejected_index %lu rejected_hint %lu next_ %lu matched_ %lu\n",
// rejected_index, rejected_hint, next_, matched_ );
if (rejected_index != next_ - 1) {
return false; // stale reject msg;
}
assert(rejected_index == next_ - 1);
// may lead to slow probe !!!
// : rejected_index, rejected_index-1, rejected_index-2...
next_ = std::min(rejected_index, rejected_hint + 1);
next_ = std::max(uint64_t{1}, next_);
assert(0 < next_);
next_ = std::max(next_, matched_+1); // never below matched !!
hassert(matched_ + 1 <= next_, "logid %lu matched_ %lu next_ %lu rejected_index %lu rejected_hint %lu",
logid_, matched_, next_, rejected_index, rejected_hint);
pause_ = false; // resume
logerr("TEST: logid %lu next_ %lu matched_ %lu",
logid_, next_, matched_);
return true;
}
void Progress::Tick()
{
++no_progress_tick_;
}
bool Progress::NeedResetByHBAppRsp()
{
if (matched_ + 1 == next_) {
no_progress_tick_ = 0;
return false;
}
hassert(matched_ + 1 < next_, "logid %lu matched_ %lu next_ %lu no_progress_tick_ %u",
logid_, matched_, next_, no_progress_tick_);
return no_progress_tick_ >= 5;
}
void Progress::ResetByHBAppRsp()
{
assert(matched_ + 1 < next_);
BecomeProbe();
pause_ = false;
no_progress_tick_ = 0;
}
bool Progress::MaybeUpdate(uint64_t new_matched_index)
{
bool update = false;
if (new_matched_index > matched_) {
matched_ = new_matched_index;
update = true;
pause_ = false;
no_progress_tick_ = 0;
curr_max_size_ =
std::min(max_max_size_, curr_max_size_ << 1);
}
if (new_matched_index + 1 > next_) {
next_ = new_matched_index + 1;
logerr("TEST: logid %lu next_ %lu matched_ %lu", logid_, next_, matched_);
}
return update;
}
void Progress::UpdateNext(uint64_t new_next)
{
assert(0 < new_next);
assert(next_ < new_next);
logerr("TEST: logid %lu next_ %lu matched_ %lu new_next %lu", logid_, next_, matched_, new_next);
next_ = new_next;
}
void Progress::BecomeReplicate()
{
if (ProgressState::PROBE == state_) {
state_ = ProgressState::REPLICATE;
next_ = matched_ + 1;
logerr("TEST: logid %lu next_ %lu matched_ %lu", logid_, next_, matched_);
pause_ = false;
}
assert(false == pause_);
assert(ProgressState::REPLICATE == state_);
}
void Progress::BecomeProbe()
{
if (ProgressState::REPLICATE == state_) {
state_ = ProgressState::PROBE;
next_ = matched_ + 1;
logerr("TEST: logid %lu next_ %lu matched_ %lu", logid_, next_, matched_);
pause_ = false;
}
assert(ProgressState::PROBE == state_);
}
void Progress::PostActive(bool active)
{
active_ = active;
}
void Progress::Reset(uint64_t new_next, bool can_vote)
{
assert(0 < new_next);
state_ = ProgressState::REPLICATE;
next_ = new_next;
matched_ = 0;
if (1 < next_) {
state_ = ProgressState::PROBE;
}
can_vote_ = can_vote;
pause_ = false;
active_ = true;
logerr("TEST: logid %lu next_ %lu matched_ %lu", logid_, next_, matched_);
}
void Progress::Pause()
{
pause_ = true;
}
// test helper function
void Progress::SetNext(uint64_t new_next)
{
next_ = new_next;
logerr("TEST: logid %lu next_ %lu matched_ %lu", logid_, next_, matched_);
}
} // namespace raft;