|
@@ -121,13 +121,13 @@ func newParticipant(id int64, pubAddr string, raftPubAddr string, client *v2clie
|
|
|
func (p *participant) run() int64 {
|
|
func (p *participant) run() int64 {
|
|
|
seeds := p.peerHub.getSeeds()
|
|
seeds := p.peerHub.getSeeds()
|
|
|
if len(seeds) == 0 {
|
|
if len(seeds) == 0 {
|
|
|
- log.Println("starting a bootstrap node")
|
|
|
|
|
|
|
+ log.Printf("participant.run id=%x action=bootstrap\n", p.id)
|
|
|
p.node.Campaign()
|
|
p.node.Campaign()
|
|
|
p.node.InitCluster(genId())
|
|
p.node.InitCluster(genId())
|
|
|
p.node.Add(p.id, p.raftPubAddr, []byte(p.pubAddr))
|
|
p.node.Add(p.id, p.raftPubAddr, []byte(p.pubAddr))
|
|
|
p.apply(p.node.Next())
|
|
p.apply(p.node.Next())
|
|
|
} else {
|
|
} else {
|
|
|
- log.Println("joining cluster via peers", seeds)
|
|
|
|
|
|
|
+ log.Printf("participant.run id=%x action=join seeds=\"%v\"\n", p.id, seeds)
|
|
|
p.join()
|
|
p.join()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -167,14 +167,14 @@ func (p *participant) run() int64 {
|
|
|
case <-v2SyncTicker.C:
|
|
case <-v2SyncTicker.C:
|
|
|
node.Sync()
|
|
node.Sync()
|
|
|
case <-p.stopc:
|
|
case <-p.stopc:
|
|
|
- log.Printf("Participant %x stopped\n", p.id)
|
|
|
|
|
|
|
+ log.Printf("participant.stop id=%x\n", p.id)
|
|
|
return stopMode
|
|
return stopMode
|
|
|
}
|
|
}
|
|
|
p.apply(node.Next())
|
|
p.apply(node.Next())
|
|
|
p.send(node.Msgs())
|
|
p.send(node.Msgs())
|
|
|
if node.IsRemoved() {
|
|
if node.IsRemoved() {
|
|
|
- log.Printf("Participant %x return\n", p.id)
|
|
|
|
|
p.stop()
|
|
p.stop()
|
|
|
|
|
+ log.Printf("participant.end id=%x\n", p.id)
|
|
|
return standbyMode
|
|
return standbyMode
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -195,6 +195,7 @@ func (p *participant) raftHandler() http.Handler {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (p *participant) add(id int64, raftPubAddr string, pubAddr string) error {
|
|
func (p *participant) add(id int64, raftPubAddr string, pubAddr string) error {
|
|
|
|
|
+ log.Printf("participant.add id=%x nodeId=%x raftPubAddr=%s pubAddr=%s\n", p.id, id, raftPubAddr, pubAddr)
|
|
|
pp := path.Join(v2machineKVPrefix, fmt.Sprint(id))
|
|
pp := path.Join(v2machineKVPrefix, fmt.Sprint(id))
|
|
|
|
|
|
|
|
_, err := p.Get(pp, false, false)
|
|
_, err := p.Get(pp, false, false)
|
|
@@ -202,12 +203,13 @@ func (p *participant) add(id int64, raftPubAddr string, pubAddr string) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
if v, ok := err.(*etcdErr.Error); !ok || v.ErrorCode != etcdErr.EcodeKeyNotFound {
|
|
if v, ok := err.(*etcdErr.Error); !ok || v.ErrorCode != etcdErr.EcodeKeyNotFound {
|
|
|
|
|
+ log.Printf("participant.add id=%x getErr=\"%v\"\n", p.id, err)
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
w, err := p.Watch(pp, true, false, 0)
|
|
w, err := p.Watch(pp, true, false, 0)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.Println("add error:", err)
|
|
|
|
|
|
|
+ log.Printf("participant.add id=%x watchErr=\"%v\"\n", p.id, err)
|
|
|
return tmpErr
|
|
return tmpErr
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -215,7 +217,7 @@ func (p *participant) add(id int64, raftPubAddr string, pubAddr string) error {
|
|
|
case p.addNodeC <- raft.Config{NodeId: id, Addr: raftPubAddr, Context: []byte(pubAddr)}:
|
|
case p.addNodeC <- raft.Config{NodeId: id, Addr: raftPubAddr, Context: []byte(pubAddr)}:
|
|
|
default:
|
|
default:
|
|
|
w.Remove()
|
|
w.Remove()
|
|
|
- log.Println("unable to send out addNode proposal")
|
|
|
|
|
|
|
+ log.Printf("participant.add id=%x proposeErr=\"unable to send out addNode proposal\"\n", p.id)
|
|
|
return tmpErr
|
|
return tmpErr
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -224,11 +226,11 @@ func (p *participant) add(id int64, raftPubAddr string, pubAddr string) error {
|
|
|
if v.Action == store.Set {
|
|
if v.Action == store.Set {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
- log.Println("add error: action =", v.Action)
|
|
|
|
|
|
|
+ log.Printf("participant.add id=%x watchErr=\"unexpected action\" action=%s\n", p.id, v.Action)
|
|
|
return tmpErr
|
|
return tmpErr
|
|
|
case <-time.After(6 * defaultHeartbeat * p.tickDuration):
|
|
case <-time.After(6 * defaultHeartbeat * p.tickDuration):
|
|
|
w.Remove()
|
|
w.Remove()
|
|
|
- log.Println("add error: wait timeout")
|
|
|
|
|
|
|
+ log.Printf("participant.add id=%x watchErr=timeout\n", p.id)
|
|
|
return tmpErr
|
|
return tmpErr
|
|
|
case <-p.stopc:
|
|
case <-p.stopc:
|
|
|
return stopErr
|
|
return stopErr
|
|
@@ -236,6 +238,7 @@ func (p *participant) add(id int64, raftPubAddr string, pubAddr string) error {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (p *participant) remove(id int64) error {
|
|
func (p *participant) remove(id int64) error {
|
|
|
|
|
+ log.Printf("participant.remove id=%x nodeId=%x\n", p.id, id)
|
|
|
pp := path.Join(v2machineKVPrefix, fmt.Sprint(id))
|
|
pp := path.Join(v2machineKVPrefix, fmt.Sprint(id))
|
|
|
|
|
|
|
|
v, err := p.Get(pp, false, false)
|
|
v, err := p.Get(pp, false, false)
|
|
@@ -246,7 +249,7 @@ func (p *participant) remove(id int64) error {
|
|
|
select {
|
|
select {
|
|
|
case p.removeNodeC <- raft.Config{NodeId: id}:
|
|
case p.removeNodeC <- raft.Config{NodeId: id}:
|
|
|
default:
|
|
default:
|
|
|
- log.Println("unable to send out removeNode proposal")
|
|
|
|
|
|
|
+ log.Printf("participant.remove id=%x proposeErr=\"unable to send out removeNode proposal\"\n", p.id)
|
|
|
return tmpErr
|
|
return tmpErr
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -254,7 +257,7 @@ func (p *participant) remove(id int64) error {
|
|
|
// removal target is self
|
|
// removal target is self
|
|
|
w, err := p.Watch(pp, true, false, v.Index()+1)
|
|
w, err := p.Watch(pp, true, false, v.Index()+1)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.Println("remove error:", err)
|
|
|
|
|
|
|
+ log.Printf("participant.remove id=%x watchErr=\"%v\"\n", p.id, err)
|
|
|
return tmpErr
|
|
return tmpErr
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -263,11 +266,11 @@ func (p *participant) remove(id int64) error {
|
|
|
if v.Action == store.Delete {
|
|
if v.Action == store.Delete {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
- log.Println("remove error: action =", v.Action)
|
|
|
|
|
|
|
+ log.Printf("participant.remove id=%x watchErr=\"unexpected action\" action=%s\n", p.id, v.Action)
|
|
|
return tmpErr
|
|
return tmpErr
|
|
|
case <-time.After(6 * defaultHeartbeat * p.tickDuration):
|
|
case <-time.After(6 * defaultHeartbeat * p.tickDuration):
|
|
|
w.Remove()
|
|
w.Remove()
|
|
|
- log.Println("remove error: wait timeout")
|
|
|
|
|
|
|
+ log.Printf("participant.remove id=%x watchErr=timeout\n", p.id)
|
|
|
return tmpErr
|
|
return tmpErr
|
|
|
case <-p.stopc:
|
|
case <-p.stopc:
|
|
|
return stopErr
|
|
return stopErr
|
|
@@ -286,35 +289,36 @@ func (p *participant) apply(ents []raft.Entry) {
|
|
|
p.v2apply(offset+int64(i), ent)
|
|
p.v2apply(offset+int64(i), ent)
|
|
|
case raft.ClusterInit:
|
|
case raft.ClusterInit:
|
|
|
p.clusterId = p.node.ClusterId()
|
|
p.clusterId = p.node.ClusterId()
|
|
|
|
|
+ log.Printf("participant.cluster.setId id=%x clusterId=%x\n", p.id, p.clusterId)
|
|
|
case raft.AddNode:
|
|
case raft.AddNode:
|
|
|
cfg := new(raft.Config)
|
|
cfg := new(raft.Config)
|
|
|
if err := json.Unmarshal(ent.Data, cfg); err != nil {
|
|
if err := json.Unmarshal(ent.Data, cfg); err != nil {
|
|
|
- log.Println(err)
|
|
|
|
|
|
|
+ log.Printf("participant.cluster.addNode id=%x UnmarshalErr=\"%v\"\n", p.id, err)
|
|
|
break
|
|
break
|
|
|
}
|
|
}
|
|
|
peer, err := p.peerHub.add(cfg.NodeId, cfg.Addr)
|
|
peer, err := p.peerHub.add(cfg.NodeId, cfg.Addr)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.Println(err)
|
|
|
|
|
|
|
+ log.Printf("participant.cluster.addNode id=%x peerAddErr=\"%v\"\n", p.id, err)
|
|
|
break
|
|
break
|
|
|
}
|
|
}
|
|
|
peer.participate()
|
|
peer.participate()
|
|
|
- log.Printf("Add Node %x %v %v\n", cfg.NodeId, cfg.Addr, string(cfg.Context))
|
|
|
|
|
pp := path.Join(v2machineKVPrefix, fmt.Sprint(cfg.NodeId))
|
|
pp := path.Join(v2machineKVPrefix, fmt.Sprint(cfg.NodeId))
|
|
|
p.Store.Set(pp, false, fmt.Sprintf("raft=%v&etcd=%v", cfg.Addr, string(cfg.Context)), store.Permanent)
|
|
p.Store.Set(pp, false, fmt.Sprintf("raft=%v&etcd=%v", cfg.Addr, string(cfg.Context)), store.Permanent)
|
|
|
|
|
+ log.Printf("participant.cluster.addNode id=%x nodeId=%x addr=%s context=%s\n", p.id, cfg.NodeId, cfg.Addr, cfg.Context)
|
|
|
case raft.RemoveNode:
|
|
case raft.RemoveNode:
|
|
|
cfg := new(raft.Config)
|
|
cfg := new(raft.Config)
|
|
|
if err := json.Unmarshal(ent.Data, cfg); err != nil {
|
|
if err := json.Unmarshal(ent.Data, cfg); err != nil {
|
|
|
- log.Println(err)
|
|
|
|
|
|
|
+ log.Printf("participant.cluster.removeNode id=%x UnmarshalErr=\"%v\"\n", p.id, err)
|
|
|
break
|
|
break
|
|
|
}
|
|
}
|
|
|
- log.Printf("Remove Node %x\n", cfg.NodeId)
|
|
|
|
|
peer, err := p.peerHub.peer(cfg.NodeId)
|
|
peer, err := p.peerHub.peer(cfg.NodeId)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.Fatal("cannot get the added peer:", err)
|
|
|
|
|
|
|
+ log.Fatal("participant.apply getPeerErr=\"%v\"", err)
|
|
|
}
|
|
}
|
|
|
peer.idle()
|
|
peer.idle()
|
|
|
pp := path.Join(v2machineKVPrefix, fmt.Sprint(cfg.NodeId))
|
|
pp := path.Join(v2machineKVPrefix, fmt.Sprint(cfg.NodeId))
|
|
|
p.Store.Delete(pp, false, false)
|
|
p.Store.Delete(pp, false, false)
|
|
|
|
|
+ log.Printf("participant.cluster.removeNode id=%x nodeId=%x\n", p.id, cfg.NodeId)
|
|
|
default:
|
|
default:
|
|
|
panic("unimplemented")
|
|
panic("unimplemented")
|
|
|
}
|
|
}
|
|
@@ -324,7 +328,7 @@ func (p *participant) apply(ents []raft.Entry) {
|
|
|
func (p *participant) send(msgs []raft.Message) {
|
|
func (p *participant) send(msgs []raft.Message) {
|
|
|
for i := range msgs {
|
|
for i := range msgs {
|
|
|
if err := p.peerHub.send(msgs[i]); err != nil {
|
|
if err := p.peerHub.send(msgs[i]); err != nil {
|
|
|
- log.Println("send:", err)
|
|
|
|
|
|
|
+ log.Printf("participant.send id=%x err=\"%v\"\n", p.id, err)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -342,12 +346,11 @@ func (p *participant) join() {
|
|
|
if err := p.client.AddMachine(seed, fmt.Sprint(p.id), info); err == nil {
|
|
if err := p.client.AddMachine(seed, fmt.Sprint(p.id), info); err == nil {
|
|
|
return
|
|
return
|
|
|
} else {
|
|
} else {
|
|
|
- log.Println(err)
|
|
|
|
|
|
|
+ log.Printf("participant.join id=%x addMachineErr=\"%v\"\n", p.id, err)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
}
|
|
}
|
|
|
- log.Println("fail to join the cluster")
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func genId() int64 {
|
|
func genId() int64 {
|