-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
raft: add raft test suite #2203
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package rafttest | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/coreos/etcd/raft/raftpb" | ||
) | ||
|
||
type network interface { | ||
send(m raftpb.Message) | ||
recv() chan raftpb.Message | ||
// drop message at given rate (1.0 drops all messages) | ||
drop(from, to uint64, rate float64) | ||
// delay message for (0, d] randomly at given rate (1.0 delay all messages) | ||
// do we need rate here? | ||
delay(from, to uint64, d time.Duration, rate float64) | ||
} | ||
|
||
type raftNetwork struct { | ||
recvQueues map[uint64]chan raftpb.Message | ||
} | ||
|
||
func newRaftNetwork(nodes ...uint64) *raftNetwork { | ||
pn := &raftNetwork{ | ||
recvQueues: make(map[uint64]chan raftpb.Message, 0), | ||
} | ||
|
||
for _, n := range nodes { | ||
pn.recvQueues[n] = make(chan raftpb.Message, 1024) | ||
} | ||
return pn | ||
} | ||
|
||
func (rn *raftNetwork) nodeNetwork(id uint64) *nodeNetwork { | ||
return &nodeNetwork{id: id, raftNetwork: rn} | ||
} | ||
|
||
func (rn *raftNetwork) send(m raftpb.Message) { | ||
to := rn.recvQueues[m.To] | ||
if to == nil { | ||
panic("sent to nil") | ||
} | ||
to <- m | ||
} | ||
|
||
func (rn *raftNetwork) recvFrom(from uint64) chan raftpb.Message { | ||
fromc := rn.recvQueues[from] | ||
if fromc == nil { | ||
panic("recv from nil") | ||
} | ||
return fromc | ||
} | ||
|
||
func (rn *raftNetwork) drop(from, to uint64, rate float64) { | ||
panic("unimplemented") | ||
} | ||
|
||
func (rn *raftNetwork) delay(from, to uint64, d time.Duration, rate float64) { | ||
panic("unimplemented") | ||
} | ||
|
||
type nodeNetwork struct { | ||
id uint64 | ||
*raftNetwork | ||
} | ||
|
||
func (nt *nodeNetwork) send(m raftpb.Message) { | ||
nt.raftNetwork.send(m) | ||
} | ||
|
||
func (nt *nodeNetwork) recv() chan raftpb.Message { | ||
return nt.recvFrom(nt.id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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 | ||
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, | ||
storage: st, | ||
nt: nt, | ||
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.Append(rd.Entries) | ||
go func() { | ||
for _, m := range rd.Messages { | ||
nt.send(m) | ||
} | ||
}() | ||
n.Advance() | ||
case m := <-n.nt.recv(): | ||
n.Step(context.TODO(), m) | ||
case <-n.stopc: | ||
log.Printf("raft.%d: stop", id) | ||
return | ||
} | ||
} | ||
}() | ||
return n | ||
} | ||
|
||
func (n *node) stop() { close(n.stopc) } | ||
|
||
// restart restarts the node with the given delay. | ||
// All in memory state of node is reset to initialized state. | ||
// All stable MUST be unchanged. | ||
func (n *node) restart(delay time.Duration) { | ||
panic("unimplemented") | ||
} | ||
|
||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package rafttest | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" | ||
"github.com/coreos/etcd/raft" | ||
) | ||
|
||
func TestBasicProgress(t *testing.T) { | ||
peers := []raft.Peer{{1, nil}, {2, nil}, {3, nil}, {4, nil}, {5, nil}} | ||
nt := newRaftNetwork(1, 2, 3, 4, 5) | ||
|
||
nodes := make([]*node, 0) | ||
|
||
for i := 1; i <= 5; i++ { | ||
n := startNode(uint64(i), peers, nt.nodeNetwork(uint64(i))) | ||
nodes = append(nodes, n) | ||
} | ||
|
||
time.Sleep(50 * time.Millisecond) | ||
for i := 0; i < 1000; i++ { | ||
nodes[0].Propose(context.TODO(), []byte("somedata")) | ||
} | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
for _, n := range nodes { | ||
n.stop() | ||
if n.state.Commit < 1000 { | ||
t.Errorf("commit = %d, want > 1000", n.state.Commit) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a
duplicate(from, to uint64, d time.Duration, rate float64)
option as well? (messages are sent a second time after (0, d] seconds)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, we could. but actually i think it is the not the network, but the raft node that generates the duplicate messages. When do you think a network layer would duplicate a message in reality?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, I guess all the duplicate-message scenarios I can come up with are actually the application retrying.