-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprogress.h
126 lines (106 loc) · 3.8 KB
/
progress.h
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
#ifndef __PROGRESS_H__
#define __PROGRESS_H__
#include "util.h"
#include "raft.pb.h"
#include "logger.h"
using namespace std;
using namespace raftpb;
struct inflights
{
// the starting index in the buffer
int start_;
// number of inflights in the buffer
int count_;
// the size of the buffer
int size_;
// buffer contains the index of the last entry
// inside one message.
vector<uint64_t> buffer_;
Logger* logger_;
void add(uint64_t infight);
void growBuf();
void freeTo(uint64_t to);
void freeFirstOne();
bool full();
void reset();
int count();
inflights(int size, Logger *logger)
: start_(0),
count_(0),
size_(size),
logger_(logger)
{
buffer_.resize(size);
}
~inflights()
{
}
};
enum ProgressState
{
ProgressStateProbe = 0,
ProgressStateReplicate = 1,
ProgressStateSnapshot = 2
};
// Progress represents a follower progress in the view of the leader. Leader maintains
// progresses of all followers, and sends entries to the follower based on its progress.
struct Progress
{
uint64_t match_, next_;
// State defines how the leader should interact with the follower.
//
// When in ProgressStateProbe, leader sends at most one replication message
// per heartbeat interval. It also probes actual progress of the follower.
//
// When in ProgressStateReplicate, leader optimistically increases next
// to the latest entry sent after sending replication message. This is
// an optimized state for fast replicating log entries to the follower.
//
// When in ProgressStateSnapshot, leader should have sent out snapshot
// before and stops sending any replication message.
ProgressState state_;
// Paused is used in ProgressStateProbe.
// When Paused is true, raft should pause sending replication message to this peer.
bool paused_;
// PendingSnapshot is used in ProgressStateSnapshot.
// If there is a pending snapshot, the pendingSnapshot will be set to the
// index of the snapshot. If pendingSnapshot is set, the replication process of
// this Progress will be paused. raft will not resend snapshot until the pending one
// is reported to be failed.
uint64_t pendingSnapshot_;
// RecentActive is true if the progress is recently active. Receiving any messages
// from the corresponding follower indicates the progress is active.
// RecentActive can be reset to false after an election timeout.
bool recentActive_;
// inflights is a sliding window for the inflight messages.
// Each inflight message contains one or more log entries.
// The max number of entries per message is defined in raft config as MaxSizePerMsg.
// Thus inflight effectively limits both the number of inflight messages
// and the bandwidth each Progress can use.
// When inflights is full, no more message should be sent.
// When a leader sends out a message, the index of the last
// entry should be added to inflights. The index MUST be added
// into inflights in order.
// When a leader receives a reply, the previous inflights should
// be freed by calling inflights.freeTo with the index of the last
// received entry.
inflights ins_;
Logger* logger_;
const char* stateString();
void resetState(ProgressState state);
void becomeProbe();
void becomeReplicate();
void becomeSnapshot(uint64_t snapshoti);
bool maybeUpdate(uint64_t n);
void optimisticUpdate(uint64_t n);
bool maybeDecrTo(uint64_t rejected, uint64_t last);
void snapshotFailure();
void pause();
void resume();
bool isPaused();
bool needSnapshotAbort();
string String();
Progress(uint64_t next, int maxInfilght, Logger *logger);
~Progress();
};
#endif