@@ -39,6 +39,9 @@ pub struct Progress {
39
39
/// RecentActive can be reset to false after an election timeout.
40
40
pub recent_active : bool ,
41
41
42
+ /// This is true if the progress lags behind the leader too much.
43
+ pub lag : bool ,
44
+
42
45
/// Inflights is a sliding window for the inflight messages.
43
46
/// When inflights is full, no more message should be sent.
44
47
/// When a leader sends out a message, the index of the last
@@ -66,6 +69,7 @@ impl Progress {
66
69
pending_snapshot : 0 ,
67
70
pending_request_snapshot : 0 ,
68
71
recent_active : false ,
72
+ lag : false ,
69
73
ins : Inflights :: new ( ins_size) ,
70
74
commit_group_id : 0 ,
71
75
committed_index : 0 ,
@@ -87,6 +91,7 @@ impl Progress {
87
91
self . pending_snapshot = 0 ;
88
92
self . pending_request_snapshot = INVALID_INDEX ;
89
93
self . recent_active = false ;
94
+ self . lag = false ;
90
95
self . ins . reset ( ) ;
91
96
}
92
97
@@ -133,10 +138,14 @@ impl Progress {
133
138
134
139
/// Returns false if the given n index comes from an outdated message.
135
140
/// Otherwise it updates the progress and returns true.
136
- pub fn maybe_update ( & mut self , n : u64 ) -> bool {
141
+ /// If the matched has catched up with last_index, reset the lag flag.
142
+ pub fn maybe_update ( & mut self , n : u64 , last_index : u64 ) -> bool {
137
143
let need_update = self . matched < n;
138
144
if need_update {
139
145
self . matched = n;
146
+ if self . lag && self . matched + 20 > last_index {
147
+ self . lag = false ;
148
+ }
140
149
self . resume ( ) ;
141
150
} ;
142
151
@@ -287,7 +296,7 @@ mod tests {
287
296
p. maybe_decr_to ( 1 , 1 , INVALID_INDEX ) ;
288
297
assert ! ( !p. paused, "paused= true, want false" ) ;
289
298
p. paused = true ;
290
- p. maybe_update ( 2 ) ;
299
+ p. maybe_update ( 2 , 2 ) ;
291
300
assert ! ( !p. paused, "paused= true, want false" ) ;
292
301
}
293
302
@@ -357,7 +366,7 @@ mod tests {
357
366
for ( i, & ( update, wm, wn, wok) ) in tests. iter ( ) . enumerate ( ) {
358
367
let mut p = Progress :: new ( prev_n, 256 ) ;
359
368
p. matched = prev_m;
360
- let ok = p. maybe_update ( update) ;
369
+ let ok = p. maybe_update ( update, prev_n ) ;
361
370
if ok != wok {
362
371
panic ! ( "#{}: ok= {}, want {}" , i, ok, wok) ;
363
372
}
@@ -368,6 +377,16 @@ mod tests {
368
377
panic ! ( "#{}: next= {}, want {}" , i, p. next_idx, wn) ;
369
378
}
370
379
}
380
+ let mut p = Progress :: new ( 5 , 256 ) ;
381
+ p. matched = prev_m;
382
+ p. lag = true ;
383
+ let ok = p. maybe_update ( prev_m + 1 , prev_m + 10 ) ;
384
+ if !ok {
385
+ panic ! ( "ok= {}" , ok) ;
386
+ }
387
+ if p. lag {
388
+ panic ! ( "lag= {}" , p. lag) ;
389
+ }
371
390
}
372
391
373
392
#[ test]
0 commit comments