@@ -264,6 +264,12 @@ func (e *Election) campaignLoop(ctx context.Context, session *concurrency.Sessio
264264 e .l .Debug ("begin to campaign" , zap .Stringer ("current member" , e .info ))
265265
266266 err2 := elec .Campaign (ctx2 , e .infoStr )
267+ failpoint .Inject ("mockCapaignSucceedButReturnErr" , func () {
268+ if err2 == nil {
269+ err2 = errors .New ("mock campaign succeed but return error" )
270+ time .Sleep (time .Second )
271+ }
272+ })
267273 if err2 != nil {
268274 // because inner commit may return undetermined error, we try to delete the election key manually
269275 deleted , err3 := e .ClearSessionIfNeeded (ctx , e .ID ())
@@ -282,6 +288,7 @@ func (e *Election) campaignLoop(ctx context.Context, session *concurrency.Sessio
282288 var (
283289 leaderKey string
284290 leaderInfo * CampaignerInfo
291+ revision int64
285292 )
286293 eleObserveCh := elec .Observe (ctx2 )
287294
@@ -300,6 +307,7 @@ func (e *Election) campaignLoop(ctx context.Context, session *concurrency.Sessio
300307 e .l .Info ("get response from election observe" , zap .String ("key" , string (resp .Kvs [0 ].Key )), zap .String ("value" , string (resp .Kvs [0 ].Value )))
301308 leaderKey = string (resp .Kvs [0 ].Key )
302309 leaderInfo , err = getCampaignerInfo (resp .Kvs [0 ].Value )
310+ revision = resp .Header .Revision
303311 if err != nil {
304312 // this should never happened
305313 e .l .Error ("fail to get leader information" , zap .String ("value" , string (resp .Kvs [0 ].Value )), zap .Error (err ))
@@ -330,7 +338,7 @@ func (e *Election) campaignLoop(ctx context.Context, session *concurrency.Sessio
330338
331339 e .l .Info ("become leader" , zap .Stringer ("current member" , e .info ))
332340 e .notifyLeader (ctx , leaderInfo ) // become the leader now
333- e .watchLeader (ctx , session , leaderKey , elec )
341+ e .watchLeader (ctx , session , leaderKey , elec , revision )
334342 e .l .Info ("retire from leader" , zap .Stringer ("current member" , e .info ))
335343 e .notifyLeader (ctx , nil ) // need to re-campaign
336344 oldLeaderID = ""
@@ -359,8 +367,8 @@ func (e *Election) notifyLeader(ctx context.Context, leaderInfo *CampaignerInfo)
359367 }
360368}
361369
362- func (e * Election ) watchLeader (ctx context.Context , session * concurrency.Session , key string , elec * concurrency.Election ) {
363- e .l .Debug ("watch leader key" , zap .String ("key" , key ))
370+ func (e * Election ) watchLeader (ctx context.Context , session * concurrency.Session , key string , elec * concurrency.Election , revision int64 ) {
371+ e .l .Debug ("watch leader key" , zap .String ("key" , key ), zap . Int64 ( "revision" , revision ), zap . Stringer ( "current member" , e . info ) )
364372
365373 e .campaignMu .Lock ()
366374 e .resignCh = make (chan struct {})
@@ -374,7 +382,7 @@ func (e *Election) watchLeader(ctx context.Context, session *concurrency.Session
374382
375383 wCtx , cancel := context .WithCancel (ctx )
376384 defer cancel ()
377- wch := e .cli .Watch (wCtx , key )
385+ wch := e .cli .Watch (wCtx , key , clientv3 . WithRev ( revision + 1 ) )
378386
379387 for {
380388 if e .evictLeader .Load () {
0 commit comments