etcd: add clusterid to participant

This commit is contained in:
Xiang Li 2014-07-20 12:13:27 -07:00 committed by Yicheng Qin
parent 13ec81c87f
commit 155bd09902
2 changed files with 18 additions and 0 deletions

View File

@ -416,6 +416,13 @@ func waitCluster(t *testing.T, es []*Server) {
}
}
}
clusterId := es[0].p.node.ClusterId()
for i, e := range es {
if e.p.node.ClusterId() != clusterId {
t.Errorf("#%d: clusterId = %x, want %x", i, e.p.node.ClusterId(), clusterId)
}
}
}
func waitMode(mode int64, e *Server) {

View File

@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"path"
"sync"
@ -58,6 +59,7 @@ var (
type participant struct {
id int64
clusterId int64
pubAddr string
raftPubAddr string
seeds map[string]bool
@ -83,6 +85,7 @@ type participant struct {
func newParticipant(id int64, pubAddr string, raftPubAddr string, client *v2client, peerHub *peerHub, tickDuration time.Duration) *participant {
p := &participant{
id: id,
clusterId: -1,
pubAddr: pubAddr,
raftPubAddr: raftPubAddr,
tickDuration: tickDuration,
@ -120,6 +123,7 @@ func (p *participant) run() int64 {
if len(seeds) == 0 {
log.Println("starting a bootstrap node")
p.node.Campaign()
p.node.InitCluster(genId())
p.node.Add(p.id, p.raftPubAddr, []byte(p.pubAddr))
p.apply(p.node.Next())
} else {
@ -280,6 +284,8 @@ func (p *participant) apply(ents []raft.Entry) {
continue
}
p.v2apply(offset+int64(i), ent)
case raft.ClusterInit:
p.clusterId = p.node.ClusterId()
case raft.AddNode:
cfg := new(raft.Config)
if err := json.Unmarshal(ent.Data, cfg); err != nil {
@ -343,3 +349,8 @@ func (p *participant) join() {
}
log.Println("fail to join the cluster")
}
func genId() int64 {
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
return r.Int63()
}