| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- package rafttest
- import (
- "log"
- "time"
- "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
- "github.com/coreos/etcd/raft"
- "github.com/coreos/etcd/raft/raftpb"
- )
- type node struct {
- raft.Node
- id uint64
- paused bool
- nt network
- stopc chan struct{}
- // stable
- storage *raft.MemoryStorage
- state raftpb.HardState
- }
- func startNode(id uint64, peers []raft.Peer, nt network) *node {
- st := raft.NewMemoryStorage()
- rn := raft.StartNode(id, peers, 10, 1, st)
- n := &node{
- Node: rn,
- id: id,
- storage: st,
- nt: nt,
- }
- n.start()
- return n
- }
- func (n *node) start() {
- n.stopc = make(chan struct{})
- ticker := time.Tick(5 * time.Millisecond)
- go func() {
- for {
- select {
- case <-ticker:
- n.Tick()
- case rd := <-n.Ready():
- if !raft.IsEmptyHardState(rd.HardState) {
- n.state = rd.HardState
- n.storage.SetHardState(n.state)
- }
- n.storage.Append(rd.Entries)
- go func() {
- for _, m := range rd.Messages {
- n.nt.send(m)
- }
- }()
- n.Advance()
- case m := <-n.nt.recv():
- n.Step(context.TODO(), m)
- case <-n.stopc:
- n.Stop()
- log.Printf("raft.%d: stop", n.id)
- n.Node = nil
- close(n.stopc)
- return
- }
- }
- }()
- }
- // stop stops the node. stop a stopped node might panic.
- // All in memory state of node is discarded.
- // All stable MUST be unchanged.
- func (n *node) stop() {
- n.nt.disconnect(n.id)
- n.stopc <- struct{}{}
- // wait for the shutdown
- <-n.stopc
- }
- // restart restarts the node. restart a started node
- // blocks and might affect the future stop operation.
- func (n *node) restart() {
- // wait for the shutdown
- <-n.stopc
- n.Node = raft.RestartNode(n.id, 10, 1, n.storage, 0)
- n.start()
- n.nt.connect(n.id)
- }
- // pause pauses the node.
- // The paused node buffers the received messages and replies
- // all of them when it resumes.
- func (n *node) pause() {
- panic("unimplemented")
- }
- // resume resumes the paused node.
- func (n *node) resume() {
- panic("unimplemented")
- }
- func (n *node) isPaused() bool {
- return n.paused
- }
|