Skip to content

Commit 7d7266d

Browse files
committed
concurrency: set keyPrefix when calling ResumeElection()
This change allows users to use Observe() after resuming an election as Observe() requires a valid keyPrefix to function properly.
1 parent dedae6e commit 7d7266d

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

clientv3/concurrency/election.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func NewElection(s *Session, pfx string) *Election {
4848
// ResumeElection initializes an election with a known leader.
4949
func ResumeElection(s *Session, pfx string, leaderKey string, leaderRev int64) *Election {
5050
return &Election{
51+
keyPrefix: pfx,
5152
session: s,
5253
leaderKey: leaderKey,
5354
leaderRev: leaderRev,

clientv3/concurrency/election_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright 2018 The etcd Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package concurrency_test
16+
17+
import (
18+
"context"
19+
"log"
20+
"testing"
21+
22+
"time"
23+
24+
"strings"
25+
26+
"go.etcd.io/etcd/clientv3"
27+
"go.etcd.io/etcd/clientv3/concurrency"
28+
)
29+
30+
func TestResumeElection(t *testing.T) {
31+
const prefix = "/resume-election/"
32+
33+
cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
34+
if err != nil {
35+
log.Fatal(err)
36+
}
37+
defer cli.Close()
38+
39+
s, err := concurrency.NewSession(cli)
40+
if err != nil {
41+
log.Fatal(err)
42+
}
43+
defer s.Close()
44+
45+
e := concurrency.NewElection(s, prefix)
46+
47+
// Entire test should never take more than 10 seconds
48+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
49+
defer cancel()
50+
51+
// Become leader
52+
if err := e.Campaign(ctx, "candidate1"); err != nil {
53+
t.Fatalf("Campaign() returned non nil err: %s", err)
54+
}
55+
56+
// Get the leadership details of the current election
57+
leader, err := e.Leader(ctx)
58+
if err != nil {
59+
t.Fatalf("Leader() returned non nil err: %s", err)
60+
}
61+
62+
// Recreate the election
63+
e = concurrency.ResumeElection(s, prefix,
64+
string(leader.Kvs[0].Key), leader.Kvs[0].CreateRevision)
65+
66+
respChan := make(chan *clientv3.GetResponse)
67+
go func() {
68+
o := e.Observe(ctx)
69+
respChan <- nil
70+
for {
71+
select {
72+
case resp, ok := <-o:
73+
if !ok {
74+
t.Fatal("Observe() channel closed prematurely")
75+
}
76+
// Ignore any observations that candidate1 was elected
77+
if string(resp.Kvs[0].Value) == "candidate1" {
78+
continue
79+
}
80+
respChan <- &resp
81+
return
82+
}
83+
}
84+
}()
85+
86+
// Wait until observe goroutine is running
87+
<-respChan
88+
89+
// Put some random data to generate a change event, this put should be
90+
// ignored by Observe() because it is not under the election prefix.
91+
_, err = cli.Put(ctx, "foo", "bar")
92+
if err != nil {
93+
t.Fatalf("Put('foo') returned non nil err: %s", err)
94+
}
95+
96+
// Resign as leader
97+
if err := e.Resign(ctx); err != nil {
98+
t.Fatalf("Resign() returned non nil err: %s", err)
99+
}
100+
101+
// Elect a different candidate
102+
if err := e.Campaign(ctx, "candidate2"); err != nil {
103+
t.Fatalf("Campaign() returned non nil err: %s", err)
104+
}
105+
106+
// Wait for observed leader change
107+
resp := <-respChan
108+
109+
kv := resp.Kvs[0]
110+
if !strings.HasPrefix(string(kv.Key), prefix) {
111+
t.Errorf("expected observed election to have prefix '%s' got '%s'", prefix, string(kv.Key))
112+
}
113+
if string(kv.Value) != "candidate2" {
114+
t.Errorf("expected new leader to be 'candidate1' got '%s'", string(kv.Value))
115+
}
116+
}

0 commit comments

Comments
 (0)