2016-01-20 03:30:31 +03:00
|
|
|
// Copyright 2016 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-02-29 22:27:56 +03:00
|
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
2016-02-24 05:57:41 +03:00
|
|
|
"github.com/coreos/etcd/clientv3/concurrency"
|
2016-01-20 03:30:31 +03:00
|
|
|
)
|
|
|
|
|
2016-02-01 08:42:39 +03:00
|
|
|
// TestElectionWait tests if followers can correctly wait for elections.
|
2016-01-20 03:30:31 +03:00
|
|
|
func TestElectionWait(t *testing.T) {
|
2016-01-29 06:07:35 +03:00
|
|
|
clus := NewClusterV3(t, &ClusterConfig{Size: 3})
|
2016-01-20 03:30:31 +03:00
|
|
|
defer clus.Terminate(t)
|
2016-02-24 05:57:41 +03:00
|
|
|
defer dropSessionLease(clus)
|
2016-01-20 03:30:31 +03:00
|
|
|
|
|
|
|
leaders := 3
|
|
|
|
followers := 3
|
|
|
|
|
|
|
|
electedc := make(chan string)
|
|
|
|
nextc := []chan struct{}{}
|
|
|
|
|
|
|
|
// wait for all elections
|
|
|
|
donec := make(chan struct{})
|
|
|
|
for i := 0; i < followers; i++ {
|
|
|
|
nextc = append(nextc, make(chan struct{}))
|
|
|
|
go func(ch chan struct{}) {
|
|
|
|
for j := 0; j < leaders; j++ {
|
2016-02-29 22:27:56 +03:00
|
|
|
b := concurrency.NewElection(context.TODO(), clus.RandClient(), "test-election")
|
|
|
|
cctx, cancel := context.WithCancel(context.TODO())
|
|
|
|
defer cancel()
|
|
|
|
s, ok := <-b.Observe(cctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("could not observe election; channel closed")
|
2016-01-20 03:30:31 +03:00
|
|
|
}
|
2016-02-29 22:27:56 +03:00
|
|
|
electedc <- string(s.Kvs[0].Value)
|
2016-01-20 03:30:31 +03:00
|
|
|
// wait for next election round
|
|
|
|
<-ch
|
|
|
|
}
|
|
|
|
donec <- struct{}{}
|
|
|
|
}(nextc[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// elect some leaders
|
|
|
|
for i := 0; i < leaders; i++ {
|
|
|
|
go func() {
|
2016-02-29 22:27:56 +03:00
|
|
|
e := concurrency.NewElection(context.TODO(), clus.RandClient(), "test-election")
|
2016-01-20 03:30:31 +03:00
|
|
|
ev := fmt.Sprintf("electval-%v", time.Now().UnixNano())
|
2016-02-29 22:27:56 +03:00
|
|
|
if err := e.Campaign(context.TODO(), ev); err != nil {
|
2016-01-20 03:30:31 +03:00
|
|
|
t.Fatalf("failed volunteer (%v)", err)
|
|
|
|
}
|
|
|
|
// wait for followers to accept leadership
|
|
|
|
for j := 0; j < followers; j++ {
|
|
|
|
s := <-electedc
|
|
|
|
if s != ev {
|
|
|
|
t.Errorf("wrong election value got %s, wanted %s", s, ev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// let next leader take over
|
|
|
|
if err := e.Resign(); err != nil {
|
|
|
|
t.Fatalf("failed resign (%v)", err)
|
|
|
|
}
|
|
|
|
// tell followers to start listening for next leader
|
|
|
|
for j := 0; j < followers; j++ {
|
|
|
|
nextc[j] <- struct{}{}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait on followers
|
|
|
|
for i := 0; i < followers; i++ {
|
|
|
|
<-donec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestElectionFailover tests that an election will
|
|
|
|
func TestElectionFailover(t *testing.T) {
|
2016-01-29 06:07:35 +03:00
|
|
|
clus := NewClusterV3(t, &ClusterConfig{Size: 3})
|
2016-01-20 03:30:31 +03:00
|
|
|
defer clus.Terminate(t)
|
2016-02-24 05:57:41 +03:00
|
|
|
defer dropSessionLease(clus)
|
2016-01-20 03:30:31 +03:00
|
|
|
|
2016-02-29 22:27:56 +03:00
|
|
|
cctx, cancel := context.WithCancel(context.TODO())
|
|
|
|
defer cancel()
|
|
|
|
|
2016-01-20 03:30:31 +03:00
|
|
|
// first leader (elected)
|
2016-02-29 22:27:56 +03:00
|
|
|
e := concurrency.NewElection(context.TODO(), clus.clients[0], "test-election")
|
|
|
|
if err := e.Campaign(context.TODO(), "foo"); err != nil {
|
2016-01-20 03:30:31 +03:00
|
|
|
t.Fatalf("failed volunteer (%v)", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check first leader
|
2016-02-29 22:27:56 +03:00
|
|
|
resp, ok := <-e.Observe(cctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("could not wait for first election; channel closed")
|
2016-01-20 03:30:31 +03:00
|
|
|
}
|
2016-02-29 22:27:56 +03:00
|
|
|
s := string(resp.Kvs[0].Value)
|
2016-01-20 03:30:31 +03:00
|
|
|
if s != "foo" {
|
|
|
|
t.Fatalf("wrong election result. got %s, wanted foo", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// next leader
|
|
|
|
electedc := make(chan struct{})
|
|
|
|
go func() {
|
2016-02-29 22:27:56 +03:00
|
|
|
ee := concurrency.NewElection(context.TODO(), clus.clients[1], "test-election")
|
|
|
|
if eer := ee.Campaign(context.TODO(), "bar"); eer != nil {
|
2016-01-28 22:42:58 +03:00
|
|
|
t.Fatal(eer)
|
2016-01-20 03:30:31 +03:00
|
|
|
}
|
|
|
|
electedc <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// invoke leader failover
|
2016-02-24 05:57:41 +03:00
|
|
|
session, serr := concurrency.NewSession(clus.clients[0])
|
|
|
|
if serr != nil {
|
|
|
|
t.Fatal(serr)
|
|
|
|
}
|
2016-02-29 22:27:56 +03:00
|
|
|
if err := session.Close(); err != nil {
|
2016-01-20 03:30:31 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check new leader
|
2016-02-29 22:27:56 +03:00
|
|
|
e = concurrency.NewElection(context.TODO(), clus.clients[2], "test-election")
|
|
|
|
resp, ok = <-e.Observe(cctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("could not wait for second election; channel closed")
|
2016-01-20 03:30:31 +03:00
|
|
|
}
|
2016-02-29 22:27:56 +03:00
|
|
|
s = string(resp.Kvs[0].Value)
|
2016-01-20 03:30:31 +03:00
|
|
|
if s != "bar" {
|
|
|
|
t.Fatalf("wrong election result. got %s, wanted bar", s)
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:27:56 +03:00
|
|
|
// leader must ack election (otherwise, Campaign may see closed conn)
|
2016-01-20 03:30:31 +03:00
|
|
|
<-electedc
|
|
|
|
}
|