Skip to content

Instantly share code, notes, and snippets.

@fahadullah
Last active February 13, 2016 21:12
Show Gist options
  • Save fahadullah/e49a4578094f4efd7746 to your computer and use it in GitHub Desktop.
Save fahadullah/e49a4578094f4efd7746 to your computer and use it in GitHub Desktop.
func TestRaftSnapshotTrailingLogs(t *testing.T) {
// Make the cluster
conf := inmemConfig(t)
// Don't keep any log entries after snapshot
conf.TrailingLogs = 0
c := MakeCluster(1, t, conf)
defer c.Close()
// Commit a lot of things
leader := c.Leader()
var future Future
for i := 0; i < 100; i++ {
future = leader.Apply([]byte(fmt.Sprintf("test%d", i)), 0)
}
// Wait for the last future to apply
if err := future.Error(); err != nil {
t.Fatalf("err: %v", err)
}
// Take a snapshot
snapFuture := leader.Snapshot()
if err := snapFuture.Error(); err != nil {
t.Fatalf("err: %v", err)
}
// Check for snapshot
if snaps, _ := leader.snapshots.List(); len(snaps) != 1 {
t.Fatalf("should have a snapshot")
}
// Logs should be trimmed
if idx, _ := leader.logs.FirstIndex(); idx != 102 {
t.Fatalf("should trim logs to 102: %d", idx)
}
// Shutdown
shutdown := leader.Shutdown()
if err := shutdown.Error(); err != nil {
t.Fatalf("err: %v", err)
}
// Restart the Raft
r := leader
r, err := NewRaft(r.conf, r.fsm, r.logs, r.stable,
r.snapshots, r.peerStore, r.trans)
if err != nil {
t.Fatalf("err: %v", err)
}
}
=== RUN TestRaftSnapshotTrailingLogs
--- FAIL: TestRaftSnapshotTrailingLogs (0.07s)
raft_test.go:188: [WARN] Fully Connecting
raft_test.go:92: [INFO] raft: Node at 49a3e505-fa74-71dc-6cad-335e6b8ea6ff [Follower] entering Follower state
raft_test.go:92: [WARN] raft: Heartbeat timeout reached, starting election
raft_test.go:92: [INFO] raft: Node at 49a3e505-fa74-71dc-6cad-335e6b8ea6ff [Candidate] entering Candidate state
raft_test.go:92: [DEBUG] raft: Votes needed: 1
raft_test.go:92: [DEBUG] raft: Vote granted from 49a3e505-fa74-71dc-6cad-335e6b8ea6ff. Tally: 1
raft_test.go:92: [INFO] raft: Election won. Tally: 1
raft_test.go:92: [INFO] raft: Node at 49a3e505-fa74-71dc-6cad-335e6b8ea6ff [Leader] entering Leader state
raft_test.go:92: [INFO] raft: Disabling EnableSingleNode (bootstrap)
raft_test.go:92: [DEBUG] raft: Node 49a3e505-fa74-71dc-6cad-335e6b8ea6ff updated peer set (2): [49a3e505-fa74-71dc-6cad-335e6b8ea6ff]
raft_test.go:1750: Last index: 1
raft_test.go:1762: Last index: 101
raft_test.go:92: [INFO] raft: Starting snapshot up to 101
raft_test.go:92: [INFO] snapshot: Creating new snapshot at /tmp/raft837379019/snapshots/1-101-1455397689044.tmp
raft_test.go:92: [INFO] raft: Compacting logs from 1 to 101
raft_test.go:92: [INFO] raft: Snapshot to 101 complete
raft_test.go:1791: err: failed to get last log: log not found
FAIL
exit status 1
FAIL github.com/hashicorp/raft 0.076s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment