diff --git a/contrib/raftexample/raft.go b/contrib/raftexample/raft.go index c53c6f1f58a9..9b0a86ccca53 100644 --- a/contrib/raftexample/raft.go +++ b/contrib/raftexample/raft.go @@ -56,7 +56,7 @@ type raftNode struct { // raft backing for the commit/error channel node raft.Node - raftStorage *raft.MemoryStorage + raftStorage raft.Storage wal *wal.WAL snapshotter *raftsnap.Snapshotter @@ -230,7 +230,7 @@ func (rc *raftNode) replayWAL() *wal.WAL { if err != nil { log.Fatalf("raftexample: failed to read WAL (%v)", err) } - rc.raftStorage = raft.NewMemoryStorage() + rc.raftStorage = raft.NewStorage() if snapshot != nil { rc.raftStorage.ApplySnapshot(*snapshot) } diff --git a/etcdserver/raft.go b/etcdserver/raft.go index ff11349fae46..8dcdfb28d721 100644 --- a/etcdserver/raft.go +++ b/etcdserver/raft.go @@ -121,7 +121,7 @@ type raftNodeConfig struct { // to check if msg receiver is removed from cluster isIDRemoved func(id uint64) bool raft.Node - raftStorage *raft.MemoryStorage + raftStorage raft.Storage storage Storage heartbeat time.Duration // for logging // transport specifies the transport to send and receive msgs to members. @@ -380,7 +380,7 @@ func advanceTicksForElection(n raft.Node, electionTicks int) { } } -func startNode(cfg ServerConfig, cl *membership.RaftCluster, ids []types.ID) (id types.ID, n raft.Node, s *raft.MemoryStorage, w *wal.WAL) { +func startNode(cfg ServerConfig, cl *membership.RaftCluster, ids []types.ID) (id types.ID, n raft.Node, s raft.Storage, w *wal.WAL) { var err error member := cl.MemberByName(cfg.Name) metadata := pbutil.MustMarshal( @@ -402,7 +402,7 @@ func startNode(cfg ServerConfig, cl *membership.RaftCluster, ids []types.ID) (id } id = member.ID plog.Infof("starting member %s in cluster %s", id, cl.ID()) - s = raft.NewMemoryStorage() + s = raft.NewStorage() c := &raft.Config{ ID: uint64(id), ElectionTick: cfg.ElectionTicks, @@ -421,7 +421,7 @@ func startNode(cfg ServerConfig, cl *membership.RaftCluster, ids []types.ID) (id return id, n, s, w } -func restartNode(cfg ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *membership.RaftCluster, raft.Node, *raft.MemoryStorage, *wal.WAL) { +func restartNode(cfg ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *membership.RaftCluster, raft.Node, raft.Storage, *wal.WAL) { var walsnap walpb.Snapshot if snapshot != nil { walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term @@ -431,7 +431,7 @@ func restartNode(cfg ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *member plog.Infof("restarting member %s in cluster %s at commit index %d", id, cid, st.Commit) cl := membership.NewCluster("") cl.SetID(cid) - s := raft.NewMemoryStorage() + s := raft.NewStorage() if snapshot != nil { s.ApplySnapshot(*snapshot) } @@ -455,7 +455,7 @@ func restartNode(cfg ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *member return id, cl, n, s, w } -func restartAsStandaloneNode(cfg ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *membership.RaftCluster, raft.Node, *raft.MemoryStorage, *wal.WAL) { +func restartAsStandaloneNode(cfg ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *membership.RaftCluster, raft.Node, raft.Storage, *wal.WAL) { var walsnap walpb.Snapshot if snapshot != nil { walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term @@ -487,7 +487,7 @@ func restartAsStandaloneNode(cfg ServerConfig, snapshot *raftpb.Snapshot) (types plog.Printf("forcing restart of member %s in cluster %s at commit index %d", id, cid, st.Commit) cl := membership.NewCluster("") cl.SetID(cid) - s := raft.NewMemoryStorage() + s := raft.NewStorage() if snapshot != nil { s.ApplySnapshot(*snapshot) } diff --git a/etcdserver/raft_test.go b/etcdserver/raft_test.go index 757826cc9e1e..94653fea0b53 100644 --- a/etcdserver/raft_test.go +++ b/etcdserver/raft_test.go @@ -156,7 +156,7 @@ func TestStopRaftWhenWaitingForApplyDone(t *testing.T) { r := newRaftNode(raftNodeConfig{ Node: n, storage: mockstorage.NewStorageRecorder(""), - raftStorage: raft.NewMemoryStorage(), + raftStorage: raft.NewStorage(), transport: rafthttp.NewNopTransporter(), }) srv := &EtcdServer{r: *r} @@ -183,7 +183,7 @@ func TestConfgChangeBlocksApply(t *testing.T) { r := newRaftNode(raftNodeConfig{ Node: n, storage: mockstorage.NewStorageRecorder(""), - raftStorage: raft.NewMemoryStorage(), + raftStorage: raft.NewStorage(), transport: rafthttp.NewNopTransporter(), }) srv := &EtcdServer{r: *r} diff --git a/etcdserver/server.go b/etcdserver/server.go index 79d17425871f..bca3a77e9512 100644 --- a/etcdserver/server.go +++ b/etcdserver/server.go @@ -261,7 +261,7 @@ func NewServer(cfg ServerConfig) (srv *EtcdServer, err error) { var ( w *wal.WAL n raft.Node - s *raft.MemoryStorage + s raft.Storage id types.ID cl *membership.RaftCluster ) diff --git a/etcdserver/server_test.go b/etcdserver/server_test.go index 3c51dcc5127f..d3440c5a773c 100644 --- a/etcdserver/server_test.go +++ b/etcdserver/server_test.go @@ -172,7 +172,7 @@ func TestApplyRepeat(t *testing.T) { cl.AddMember(&membership.Member{ID: 1234}) r := newRaftNode(raftNodeConfig{ Node: n, - raftStorage: raft.NewMemoryStorage(), + raftStorage: raft.NewStorage(), storage: mockstorage.NewStorageRecorder(""), transport: rafthttp.NewNopTransporter(), }) @@ -679,7 +679,7 @@ func TestDoProposal(t *testing.T) { r := newRaftNode(raftNodeConfig{ Node: newNodeCommitter(), storage: mockstorage.NewStorageRecorder(""), - raftStorage: raft.NewMemoryStorage(), + raftStorage: raft.NewStorage(), transport: rafthttp.NewNopTransporter(), }) srv := &EtcdServer{ @@ -849,7 +849,7 @@ func TestSyncTrigger(t *testing.T) { tk := &time.Ticker{C: st} r := newRaftNode(raftNodeConfig{ Node: n, - raftStorage: raft.NewMemoryStorage(), + raftStorage: raft.NewStorage(), transport: rafthttp.NewNopTransporter(), storage: mockstorage.NewStorageRecorder(""), }) @@ -903,7 +903,7 @@ func TestSnapshot(t *testing.T) { os.RemoveAll(tmpPath) }() - s := raft.NewMemoryStorage() + s := raft.NewStorage() s.Append([]raftpb.Entry{{Index: 1}}) st := mockstore.NewRecorderStream() p := mockstorage.NewStorageRecorderStream("") @@ -972,7 +972,7 @@ func TestSnapshotOrdering(t *testing.T) { t.Fatalf("couldn't make snap dir (%v)", err) } - rs := raft.NewMemoryStorage() + rs := raft.NewStorage() p := mockstorage.NewStorageRecorderStream(testdir) tr, snapDoneC := rafthttp.NewSnapTransporter(snapdir) r := newRaftNode(raftNodeConfig{ @@ -1039,7 +1039,7 @@ func TestTriggerSnap(t *testing.T) { p := mockstorage.NewStorageRecorderStream("") r := newRaftNode(raftNodeConfig{ Node: newNodeCommitter(), - raftStorage: raft.NewMemoryStorage(), + raftStorage: raft.NewStorage(), storage: p, transport: rafthttp.NewNopTransporter(), }) @@ -1098,7 +1098,7 @@ func TestConcurrentApplyAndSnapshotV3(t *testing.T) { t.Fatalf("Couldn't make snap dir (%v)", err) } - rs := raft.NewMemoryStorage() + rs := raft.NewStorage() tr, snapDoneC := rafthttp.NewSnapTransporter(testdir) r := newRaftNode(raftNodeConfig{ isIDRemoved: func(id uint64) bool { return cl.IsIDRemoved(types.ID(id)) }, @@ -1187,7 +1187,7 @@ func TestAddMember(t *testing.T) { cl.SetStore(st) r := newRaftNode(raftNodeConfig{ Node: n, - raftStorage: raft.NewMemoryStorage(), + raftStorage: raft.NewStorage(), storage: mockstorage.NewStorageRecorder(""), transport: rafthttp.NewNopTransporter(), }) @@ -1228,7 +1228,7 @@ func TestRemoveMember(t *testing.T) { cl.AddMember(&membership.Member{ID: 1234}) r := newRaftNode(raftNodeConfig{ Node: n, - raftStorage: raft.NewMemoryStorage(), + raftStorage: raft.NewStorage(), storage: mockstorage.NewStorageRecorder(""), transport: rafthttp.NewNopTransporter(), }) @@ -1268,7 +1268,7 @@ func TestUpdateMember(t *testing.T) { cl.AddMember(&membership.Member{ID: 1234}) r := newRaftNode(raftNodeConfig{ Node: n, - raftStorage: raft.NewMemoryStorage(), + raftStorage: raft.NewStorage(), storage: mockstorage.NewStorageRecorder(""), transport: rafthttp.NewNopTransporter(), }) diff --git a/raft/README.md b/raft/README.md index 6ae005c95218..79d52d94ed6d 100644 --- a/raft/README.md +++ b/raft/README.md @@ -57,7 +57,7 @@ The primary object in raft is a Node. Either start a Node from scratch using raf To start a three-node cluster ```go - storage := raft.NewMemoryStorage() + storage := raft.NewStorage() c := &Config{ ID: 0x01, ElectionTick: 10, @@ -87,7 +87,7 @@ To allow a new node to join this cluster, do not pass in any peers. First, add t To restart a node from previous state: ```go - storage := raft.NewMemoryStorage() + storage := raft.NewStorage() // Recover the in-memory storage from persistent snapshot, state and entries. storage.ApplySnapshot(snapshot) diff --git a/raft/doc.go b/raft/doc.go index b55c591ff5d7..e028208dffbe 100644 --- a/raft/doc.go +++ b/raft/doc.go @@ -32,7 +32,7 @@ using raft.StartNode or start a Node from some initial state using raft.RestartN To start a node from scratch: - storage := raft.NewMemoryStorage() + storage := raft.NewStorage() c := &Config{ ID: 0x01, ElectionTick: 10, @@ -45,7 +45,7 @@ To start a node from scratch: To restart a node from previous state: - storage := raft.NewMemoryStorage() + storage := raft.NewStorage() // recover the in-memory storage from persistent // snapshot, state and entries. diff --git a/raft/log_test.go b/raft/log_test.go index f80e41ce2ab6..76f0cfcf5ad4 100644 --- a/raft/log_test.go +++ b/raft/log_test.go @@ -45,7 +45,7 @@ func TestFindConflict(t *testing.T) { } for i, tt := range tests { - raftLog := newLog(NewMemoryStorage(), raftLogger) + raftLog := newLog(newMemoryStorage(), raftLogger) raftLog.append(previousEnts...) gconflict := raftLog.findConflict(tt.ents) @@ -57,7 +57,7 @@ func TestFindConflict(t *testing.T) { func TestIsUpToDate(t *testing.T) { previousEnts := []pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}} - raftLog := newLog(NewMemoryStorage(), raftLogger) + raftLog := newLog(newMemoryStorage(), raftLogger) raftLog.append(previousEnts...) tests := []struct { lastIndex uint64 @@ -123,7 +123,7 @@ func TestAppend(t *testing.T) { } for i, tt := range tests { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.Append(previousEnts) raftLog := newLog(storage, raftLogger) @@ -236,7 +236,7 @@ func TestLogMaybeAppend(t *testing.T) { } for i, tt := range tests { - raftLog := newLog(NewMemoryStorage(), raftLogger) + raftLog := newLog(newMemoryStorage(), raftLogger) raftLog.append(previousEnts...) raftLog.committed = commit func() { @@ -280,7 +280,7 @@ func TestCompactionSideEffects(t *testing.T) { lastIndex := uint64(1000) unstableIndex := uint64(750) lastTerm := lastIndex - storage := NewMemoryStorage() + storage := newMemoryStorage() for i = 1; i <= unstableIndex; i++ { storage.Append([]pb.Entry{{Term: uint64(i), Index: uint64(i)}}) } @@ -356,7 +356,7 @@ func TestHasNextEnts(t *testing.T) { {5, false}, } for i, tt := range tests { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.ApplySnapshot(snap) raftLog := newLog(storage, raftLogger) raftLog.append(ents...) @@ -389,7 +389,7 @@ func TestNextEnts(t *testing.T) { {5, nil}, } for i, tt := range tests { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.ApplySnapshot(snap) raftLog := newLog(storage, raftLogger) raftLog.append(ents...) @@ -417,7 +417,7 @@ func TestUnstableEnts(t *testing.T) { for i, tt := range tests { // append stable entries to storage - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.Append(previousEnts[:tt.unstable-1]) // append unstable entries to raftlog @@ -459,7 +459,7 @@ func TestCommitTo(t *testing.T) { } } }() - raftLog := newLog(NewMemoryStorage(), raftLogger) + raftLog := newLog(newMemoryStorage(), raftLogger) raftLog.append(previousEnts...) raftLog.committed = commit raftLog.commitTo(tt.commit) @@ -482,7 +482,7 @@ func TestStableTo(t *testing.T) { {3, 1, 1}, // bad index } for i, tt := range tests { - raftLog := newLog(NewMemoryStorage(), raftLogger) + raftLog := newLog(newMemoryStorage(), raftLogger) raftLog.append([]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}}...) raftLog.stableTo(tt.stablei, tt.stablet) if raftLog.unstable.offset != tt.wunstable { @@ -517,7 +517,7 @@ func TestStableToWithSnap(t *testing.T) { {snapi - 1, snapt + 1, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1}, } for i, tt := range tests { - s := NewMemoryStorage() + s := newMemoryStorage() s.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: snapi, Term: snapt}}) raftLog := newLog(s, raftLogger) raftLog.append(tt.newEnts...) @@ -553,7 +553,7 @@ func TestCompaction(t *testing.T) { } }() - storage := NewMemoryStorage() + storage := newMemoryStorage() for i := uint64(1); i <= tt.lastIndex; i++ { storage.Append([]pb.Entry{{Index: i}}) } @@ -581,7 +581,7 @@ func TestLogRestore(t *testing.T) { index := uint64(1000) term := uint64(1000) snap := pb.SnapshotMetadata{Index: index, Term: term} - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.ApplySnapshot(pb.Snapshot{Metadata: snap}) raftLog := newLog(storage, raftLogger) @@ -605,7 +605,7 @@ func TestLogRestore(t *testing.T) { func TestIsOutOfBounds(t *testing.T) { offset := uint64(100) num := uint64(100) - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset}}) l := newLog(storage, raftLogger) for i := uint64(1); i <= num; i++ { @@ -688,7 +688,7 @@ func TestTerm(t *testing.T) { offset := uint64(100) num := uint64(100) - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset, Term: 1}}) l := newLog(storage, raftLogger) for i = 1; i < num; i++ { @@ -718,7 +718,7 @@ func TestTermWithUnstableSnapshot(t *testing.T) { storagesnapi := uint64(100) unstablesnapi := storagesnapi + 5 - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: storagesnapi, Term: 1}}) l := newLog(storage, raftLogger) l.restore(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: unstablesnapi, Term: 1}}) @@ -752,7 +752,7 @@ func TestSlice(t *testing.T) { half := offset + num/2 halfe := pb.Entry{Index: half, Term: half} - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset}}) for i = 1; i < num/2; i++ { storage.Append([]pb.Entry{{Index: offset + i, Term: offset + i}}) diff --git a/raft/node_bench_test.go b/raft/node_bench_test.go index c3fc89f3f834..358234cfc98f 100644 --- a/raft/node_bench_test.go +++ b/raft/node_bench_test.go @@ -25,7 +25,7 @@ func BenchmarkOneNode(b *testing.B) { defer cancel() n := newNode() - s := NewMemoryStorage() + s := newMemoryStorage() r := newTestRaft(1, []uint64{1}, 10, 1, s) go n.run(r) diff --git a/raft/node_test.go b/raft/node_test.go index f884f3319a5f..81737d4d4a59 100644 --- a/raft/node_test.go +++ b/raft/node_test.go @@ -115,7 +115,7 @@ func TestNodePropose(t *testing.T) { } n := newNode() - s := NewMemoryStorage() + s := newMemoryStorage() r := newTestRaft(1, []uint64{1}, 10, 1, s) go n.run(r) n.Campaign(context.TODO()) @@ -155,7 +155,7 @@ func TestNodeReadIndex(t *testing.T) { wrs := []ReadState{{Index: uint64(1), RequestCtx: []byte("somedata")}} n := newNode() - s := NewMemoryStorage() + s := newMemoryStorage() r := newTestRaft(1, []uint64{1}, 10, 1, s) r.readStates = wrs @@ -195,9 +195,9 @@ func TestNodeReadIndex(t *testing.T) { // TestDisableProposalForwarding ensures that proposals are not forwarded to // the leader when DisableProposalForwarding is true. func TestDisableProposalForwarding(t *testing.T) { - r1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - r2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - cfg3 := newTestConfig(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + r1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + r2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + cfg3 := newTestConfig(3, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) cfg3.DisableProposalForwarding = true r3 := newRaft(cfg3) nt := newNetwork(r1, r2, r3) @@ -227,9 +227,9 @@ func TestDisableProposalForwarding(t *testing.T) { // TestNodeReadIndexToOldLeader ensures that raftpb.MsgReadIndex to old leader // gets forwarded to the new leader and 'send' method does not attach its term. func TestNodeReadIndexToOldLeader(t *testing.T) { - r1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - r2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - r3 := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + r1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + r2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + r3 := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) nt := newNetwork(r1, r2, r3) @@ -292,7 +292,7 @@ func TestNodeProposeConfig(t *testing.T) { } n := newNode() - s := NewMemoryStorage() + s := newMemoryStorage() r := newTestRaft(1, []uint64{1}, 10, 1, s) go n.run(r) n.Campaign(context.TODO()) @@ -330,7 +330,7 @@ func TestNodeProposeConfig(t *testing.T) { // not affect the later propose to add new node. func TestNodeProposeAddDuplicateNode(t *testing.T) { n := newNode() - s := NewMemoryStorage() + s := newMemoryStorage() r := newTestRaft(1, []uint64{1}, 10, 1, s) go n.run(r) n.Campaign(context.TODO()) @@ -406,7 +406,7 @@ func TestNodeProposeAddDuplicateNode(t *testing.T) { // who is the current leader. func TestBlockProposal(t *testing.T) { n := newNode() - r := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1}, 10, 1, newMemoryStorage()) go n.run(r) defer n.Stop() @@ -437,7 +437,7 @@ func TestBlockProposal(t *testing.T) { // elapsed of the underlying raft state machine. func TestNodeTick(t *testing.T) { n := newNode() - s := NewMemoryStorage() + s := newMemoryStorage() r := newTestRaft(1, []uint64{1}, 10, 1, s) go n.run(r) elapsed := r.electionElapsed @@ -457,7 +457,7 @@ func TestNodeTick(t *testing.T) { // processing, and that it is idempotent func TestNodeStop(t *testing.T) { n := newNode() - s := NewMemoryStorage() + s := newMemoryStorage() r := newTestRaft(1, []uint64{1}, 10, 1, s) donec := make(chan struct{}) @@ -540,7 +540,7 @@ func TestNodeStart(t *testing.T) { MustSync: true, }, } - storage := NewMemoryStorage() + storage := newMemoryStorage() c := &Config{ ID: 1, ElectionTick: 10, @@ -593,7 +593,7 @@ func TestNodeRestart(t *testing.T) { MustSync: true, } - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.SetHardState(st) storage.Append(entries) c := &Config{ @@ -638,7 +638,7 @@ func TestNodeRestartFromSnapshot(t *testing.T) { MustSync: true, } - s := NewMemoryStorage() + s := newMemoryStorage() s.SetHardState(st) s.ApplySnapshot(snap) s.Append(entries) @@ -669,7 +669,7 @@ func TestNodeAdvance(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - storage := NewMemoryStorage() + storage := newMemoryStorage() c := &Config{ ID: 1, ElectionTick: 10, @@ -740,7 +740,7 @@ func TestNodeProposeAddLearnerNode(t *testing.T) { ticker := time.NewTicker(time.Millisecond * 100) defer ticker.Stop() n := newNode() - s := NewMemoryStorage() + s := newMemoryStorage() r := newTestRaft(1, []uint64{1}, 10, 1, s) go n.run(r) n.Campaign(context.TODO()) diff --git a/raft/raft_flow_control_test.go b/raft/raft_flow_control_test.go index c745050f8b44..d0d4d5dfe5d7 100644 --- a/raft/raft_flow_control_test.go +++ b/raft/raft_flow_control_test.go @@ -24,7 +24,7 @@ import ( // 1. msgApp can fill the sending window until full // 2. when the window is full, no more msgApp can be sent. func TestMsgAppFlowControlFull(t *testing.T) { - r := newTestRaft(1, []uint64{1, 2}, 5, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2}, 5, 1, newMemoryStorage()) r.becomeCandidate() r.becomeLeader() @@ -60,7 +60,7 @@ func TestMsgAppFlowControlFull(t *testing.T) { // 1. valid msgAppResp.index moves the windows to pass all smaller or equal index. // 2. out-of-dated msgAppResp has no effect on the sliding window. func TestMsgAppFlowControlMoveForward(t *testing.T) { - r := newTestRaft(1, []uint64{1, 2}, 5, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2}, 5, 1, newMemoryStorage()) r.becomeCandidate() r.becomeLeader() @@ -105,7 +105,7 @@ func TestMsgAppFlowControlMoveForward(t *testing.T) { // TestMsgAppFlowControlRecvHeartbeat ensures a heartbeat response // frees one slot if the window is full. func TestMsgAppFlowControlRecvHeartbeat(t *testing.T) { - r := newTestRaft(1, []uint64{1, 2}, 5, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2}, 5, 1, newMemoryStorage()) r.becomeCandidate() r.becomeLeader() diff --git a/raft/raft_paper_test.go b/raft/raft_paper_test.go index 71a7d14ace16..3c9269d3776d 100644 --- a/raft/raft_paper_test.go +++ b/raft/raft_paper_test.go @@ -52,7 +52,7 @@ func TestLeaderUpdateTermFromMessage(t *testing.T) { // it immediately reverts to follower state. // Reference: section 5.1 func testUpdateTermFromMessage(t *testing.T, state StateType) { - r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) switch state { case StateFollower: r.becomeFollower(1, 2) @@ -83,7 +83,7 @@ func TestRejectStaleTermMessage(t *testing.T) { called = true return nil } - r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) r.step = fakeStep r.loadState(pb.HardState{Term: 2}) @@ -97,7 +97,7 @@ func TestRejectStaleTermMessage(t *testing.T) { // TestStartAsFollower tests that when servers start up, they begin as followers. // Reference: section 5.2 func TestStartAsFollower(t *testing.T) { - r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) if r.state != StateFollower { t.Errorf("state = %s, want %s", r.state, StateFollower) } @@ -110,7 +110,7 @@ func TestStartAsFollower(t *testing.T) { func TestLeaderBcastBeat(t *testing.T) { // heartbeat interval hi := 1 - r := newTestRaft(1, []uint64{1, 2, 3}, 10, hi, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2, 3}, 10, hi, newMemoryStorage()) r.becomeCandidate() r.becomeLeader() for i := 0; i < 10; i++ { @@ -152,7 +152,7 @@ func TestCandidateStartNewElection(t *testing.T) { func testNonleaderStartElection(t *testing.T, state StateType) { // election timeout et := 10 - r := newTestRaft(1, []uint64{1, 2, 3}, et, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2, 3}, et, 1, newMemoryStorage()) switch state { case StateFollower: r.becomeFollower(1, 2) @@ -216,7 +216,7 @@ func TestLeaderElectionInOneRoundRPC(t *testing.T) { {5, map[uint64]bool{}, StateCandidate}, } for i, tt := range tests { - r := newTestRaft(1, idsBySize(tt.size), 10, 1, NewMemoryStorage()) + r := newTestRaft(1, idsBySize(tt.size), 10, 1, newMemoryStorage()) r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgHup}) for id, vote := range tt.votes { @@ -249,7 +249,7 @@ func TestFollowerVote(t *testing.T) { {2, 1, true}, } for i, tt := range tests { - r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) r.loadState(pb.HardState{Term: 1, Vote: tt.vote}) r.Step(pb.Message{From: tt.nvote, To: 1, Term: 1, Type: pb.MsgVote}) @@ -275,7 +275,7 @@ func TestCandidateFallback(t *testing.T) { {From: 2, To: 1, Term: 2, Type: pb.MsgApp}, } for i, tt := range tests { - r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgHup}) if r.state != StateCandidate { t.Fatalf("unexpected state = %s, want %s", r.state, StateCandidate) @@ -308,7 +308,7 @@ func TestCandidateElectionTimeoutRandomized(t *testing.T) { // Reference: section 5.2 func testNonleaderElectionTimeoutRandomized(t *testing.T, state StateType) { et := 10 - r := newTestRaft(1, []uint64{1, 2, 3}, et, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2, 3}, et, 1, newMemoryStorage()) timeouts := make(map[int]bool) for round := 0; round < 50*et; round++ { switch state { @@ -354,7 +354,7 @@ func testNonleadersElectionTimeoutNonconflict(t *testing.T, state StateType) { rs := make([]*raft, size) ids := idsBySize(size) for k := range rs { - rs[k] = newTestRaft(ids[k], ids, et, 1, NewMemoryStorage()) + rs[k] = newTestRaft(ids[k], ids, et, 1, newMemoryStorage()) } conflicts := 0 for round := 0; round < 1000; round++ { @@ -396,7 +396,7 @@ func testNonleadersElectionTimeoutNonconflict(t *testing.T, state StateType) { // Also, it writes the new entry into stable storage. // Reference: section 5.3 func TestLeaderStartReplication(t *testing.T) { - s := NewMemoryStorage() + s := newMemoryStorage() r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, s) r.becomeCandidate() r.becomeLeader() @@ -435,7 +435,7 @@ func TestLeaderStartReplication(t *testing.T) { // servers eventually find out. // Reference: section 5.3 func TestLeaderCommitEntry(t *testing.T) { - s := NewMemoryStorage() + s := newMemoryStorage() r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, s) r.becomeCandidate() r.becomeLeader() @@ -489,7 +489,7 @@ func TestLeaderAcknowledgeCommit(t *testing.T) { {5, map[uint64]bool{2: true, 3: true, 4: true, 5: true}, true}, } for i, tt := range tests { - s := NewMemoryStorage() + s := newMemoryStorage() r := newTestRaft(1, idsBySize(tt.size), 10, 1, s) r.becomeCandidate() r.becomeLeader() @@ -522,7 +522,7 @@ func TestLeaderCommitPrecedingEntries(t *testing.T) { {{Term: 1, Index: 1}}, } for i, tt := range tests { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.Append(tt) r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, storage) r.loadState(pb.HardState{Term: 2}) @@ -579,7 +579,7 @@ func TestFollowerCommitEntry(t *testing.T) { }, } for i, tt := range tests { - r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) r.becomeFollower(1, 2) r.Step(pb.Message{From: 2, To: 1, Type: pb.MsgApp, Term: 1, Entries: tt.ents, Commit: tt.commit}) @@ -620,7 +620,7 @@ func TestFollowerCheckMsgApp(t *testing.T) { {ents[1].Term + 1, ents[1].Index + 1, ents[1].Index + 1, true, 2}, } for i, tt := range tests { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.Append(ents) r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, storage) r.loadState(pb.HardState{Commit: 1}) @@ -676,7 +676,7 @@ func TestFollowerAppendEntries(t *testing.T) { }, } for i, tt := range tests { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.Append([]pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}}) r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, storage) r.becomeFollower(2, 2) @@ -745,11 +745,11 @@ func TestLeaderSyncFollowerLog(t *testing.T) { }, } for i, tt := range tests { - leadStorage := NewMemoryStorage() + leadStorage := newMemoryStorage() leadStorage.Append(ents) lead := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, leadStorage) lead.loadState(pb.HardState{Commit: lead.raftLog.lastIndex(), Term: term}) - followerStorage := NewMemoryStorage() + followerStorage := newMemoryStorage() followerStorage.Append(tt) follower := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, followerStorage) follower.loadState(pb.HardState{Term: term - 1}) @@ -782,7 +782,7 @@ func TestVoteRequest(t *testing.T) { {[]pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}}, 3}, } for j, tt := range tests { - r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) r.Step(pb.Message{ From: 2, To: 1, Type: pb.MsgApp, Term: tt.wterm - 1, LogTerm: 0, Index: 0, Entries: tt.ents, }) @@ -843,7 +843,7 @@ func TestVoter(t *testing.T) { {[]pb.Entry{{Term: 2, Index: 1}, {Term: 1, Index: 2}}, 1, 1, true}, } for i, tt := range tests { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.Append(tt.ents) r := newTestRaft(1, []uint64{1, 2}, 10, 1, storage) @@ -879,7 +879,7 @@ func TestLeaderOnlyCommitsLogFromCurrentTerm(t *testing.T) { {3, 3}, } for i, tt := range tests { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.Append(ents) r := newTestRaft(1, []uint64{1, 2}, 10, 1, storage) r.loadState(pb.HardState{Term: 2}) @@ -903,7 +903,7 @@ func (s messageSlice) Len() int { return len(s) } func (s messageSlice) Less(i, j int) bool { return fmt.Sprint(s[i]) < fmt.Sprint(s[j]) } func (s messageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } -func commitNoopEntry(r *raft, s *MemoryStorage) { +func commitNoopEntry(r *raft, s *memoryStorage) { if r.state != StateLeader { panic("it should only be used when it is the leader") } diff --git a/raft/raft_snap_test.go b/raft/raft_snap_test.go index 3908d581233b..dfe854976566 100644 --- a/raft/raft_snap_test.go +++ b/raft/raft_snap_test.go @@ -31,7 +31,7 @@ var ( ) func TestSendingSnapshotSetPendingSnapshot(t *testing.T) { - storage := NewMemoryStorage() + storage := newMemoryStorage() sm := newTestRaft(1, []uint64{1}, 10, 1, storage) sm.restore(testingSnap) @@ -49,7 +49,7 @@ func TestSendingSnapshotSetPendingSnapshot(t *testing.T) { } func TestPendingSnapshotPauseReplication(t *testing.T) { - storage := NewMemoryStorage() + storage := newMemoryStorage() sm := newTestRaft(1, []uint64{1, 2}, 10, 1, storage) sm.restore(testingSnap) @@ -66,7 +66,7 @@ func TestPendingSnapshotPauseReplication(t *testing.T) { } func TestSnapshotFailure(t *testing.T) { - storage := NewMemoryStorage() + storage := newMemoryStorage() sm := newTestRaft(1, []uint64{1, 2}, 10, 1, storage) sm.restore(testingSnap) @@ -89,7 +89,7 @@ func TestSnapshotFailure(t *testing.T) { } func TestSnapshotSucceed(t *testing.T) { - storage := NewMemoryStorage() + storage := newMemoryStorage() sm := newTestRaft(1, []uint64{1, 2}, 10, 1, storage) sm.restore(testingSnap) @@ -112,7 +112,7 @@ func TestSnapshotSucceed(t *testing.T) { } func TestSnapshotAbort(t *testing.T) { - storage := NewMemoryStorage() + storage := newMemoryStorage() sm := newTestRaft(1, []uint64{1, 2}, 10, 1, storage) sm.restore(testingSnap) diff --git a/raft/raft_test.go b/raft/raft_test.go index 374cfd015aac..ea6f2ae00bec 100644 --- a/raft/raft_test.go +++ b/raft/raft_test.go @@ -26,7 +26,7 @@ import ( ) // nextEnts returns the appliable entries and updates the applied index -func nextEnts(r *raft, s *MemoryStorage) (ents []pb.Entry) { +func nextEnts(r *raft, s *memoryStorage) (ents []pb.Entry) { // Transfer all unstable entries to "stable" storage. s.Append(r.raftLog.unstableEntries()) r.raftLog.stableTo(r.raftLog.lastIndex(), r.raftLog.lastTerm()) @@ -262,7 +262,7 @@ func TestProgressResume(t *testing.T) { // TestProgressResumeByHeartbeatResp ensures raft.heartbeat reset progress.paused by heartbeat response. func TestProgressResumeByHeartbeatResp(t *testing.T) { - r := newTestRaft(1, []uint64{1, 2}, 5, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2}, 5, 1, newMemoryStorage()) r.becomeCandidate() r.becomeLeader() r.prs[2].Paused = true @@ -280,7 +280,7 @@ func TestProgressResumeByHeartbeatResp(t *testing.T) { } func TestProgressPaused(t *testing.T) { - r := newTestRaft(1, []uint64{1, 2}, 5, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2}, 5, 1, newMemoryStorage()) r.becomeCandidate() r.becomeLeader() r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("somedata")}}}) @@ -351,8 +351,8 @@ func testLeaderElection(t *testing.T, preVote bool) { // TestLearnerElectionTimeout verfies that the leader should not start election even // when times out. func TestLearnerElectionTimeout(t *testing.T) { - n1 := newTestLearnerRaft(1, []uint64{1}, []uint64{2}, 10, 1, NewMemoryStorage()) - n2 := newTestLearnerRaft(2, []uint64{1}, []uint64{2}, 10, 1, NewMemoryStorage()) + n1 := newTestLearnerRaft(1, []uint64{1}, []uint64{2}, 10, 1, newMemoryStorage()) + n2 := newTestLearnerRaft(2, []uint64{1}, []uint64{2}, 10, 1, newMemoryStorage()) n1.becomeFollower(1, None) n2.becomeFollower(1, None) @@ -371,8 +371,8 @@ func TestLearnerElectionTimeout(t *testing.T) { // TestLearnerPromotion verifies that the learner should not election until // it is promoted to a normal peer. func TestLearnerPromotion(t *testing.T) { - n1 := newTestLearnerRaft(1, []uint64{1}, []uint64{2}, 10, 1, NewMemoryStorage()) - n2 := newTestLearnerRaft(2, []uint64{1}, []uint64{2}, 10, 1, NewMemoryStorage()) + n1 := newTestLearnerRaft(1, []uint64{1}, []uint64{2}, 10, 1, newMemoryStorage()) + n2 := newTestLearnerRaft(2, []uint64{1}, []uint64{2}, 10, 1, newMemoryStorage()) n1.becomeFollower(1, None) n2.becomeFollower(1, None) @@ -422,7 +422,7 @@ func TestLearnerPromotion(t *testing.T) { // TestLearnerCannotVote checks that a learner can't vote even it receives a valid Vote request. func TestLearnerCannotVote(t *testing.T) { - n2 := newTestLearnerRaft(2, []uint64{1}, []uint64{2}, 10, 1, NewMemoryStorage()) + n2 := newTestLearnerRaft(2, []uint64{1}, []uint64{2}, 10, 1, newMemoryStorage()) n2.becomeFollower(1, None) @@ -554,7 +554,7 @@ func TestPreVoteFromAnyState(t *testing.T) { func testVoteFromAnyState(t *testing.T, vt pb.MessageType) { for st := StateType(0); st < numStates; st++ { - r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) r.Term = 1 switch st { @@ -687,8 +687,8 @@ func TestLogReplication(t *testing.T) { // TestLearnerLogReplication tests that a learner can receive entries from the leader. func TestLearnerLogReplication(t *testing.T) { - n1 := newTestLearnerRaft(1, []uint64{1}, []uint64{2}, 10, 1, NewMemoryStorage()) - n2 := newTestLearnerRaft(2, []uint64{1}, []uint64{2}, 10, 1, NewMemoryStorage()) + n1 := newTestLearnerRaft(1, []uint64{1}, []uint64{2}, 10, 1, newMemoryStorage()) + n2 := newTestLearnerRaft(2, []uint64{1}, []uint64{2}, 10, 1, newMemoryStorage()) nt := newNetwork(n1, n2) @@ -816,9 +816,9 @@ func TestCommitWithoutNewTermEntry(t *testing.T) { } func TestDuelingCandidates(t *testing.T) { - a := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - b := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - c := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + a := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + b := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + c := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) nt := newNetwork(a, b, c) nt.cut(1, 3) @@ -846,7 +846,7 @@ func TestDuelingCandidates(t *testing.T) { nt.send(pb.Message{From: 3, To: 3, Type: pb.MsgHup}) wlog := &raftLog{ - storage: &MemoryStorage{ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}}}, + storage: &memoryStorage{ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}}}, committed: 1, unstable: unstable{offset: 2}, } @@ -858,7 +858,7 @@ func TestDuelingCandidates(t *testing.T) { }{ {a, StateFollower, 2, wlog}, {b, StateFollower, 2, wlog}, - {c, StateFollower, 2, newLog(NewMemoryStorage(), raftLogger)}, + {c, StateFollower, 2, newLog(newMemoryStorage(), raftLogger)}, } for i, tt := range tests { @@ -881,9 +881,9 @@ func TestDuelingCandidates(t *testing.T) { } func TestDuelingPreCandidates(t *testing.T) { - cfgA := newTestConfig(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - cfgB := newTestConfig(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - cfgC := newTestConfig(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + cfgA := newTestConfig(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + cfgB := newTestConfig(2, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + cfgC := newTestConfig(3, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) cfgA.PreVote = true cfgB.PreVote = true cfgC.PreVote = true @@ -916,7 +916,7 @@ func TestDuelingPreCandidates(t *testing.T) { nt.send(pb.Message{From: 3, To: 3, Type: pb.MsgHup}) wlog := &raftLog{ - storage: &MemoryStorage{ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}}}, + storage: &memoryStorage{ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}}}, committed: 1, unstable: unstable{offset: 2}, } @@ -928,7 +928,7 @@ func TestDuelingPreCandidates(t *testing.T) { }{ {a, StateLeader, 1, wlog}, {b, StateFollower, 1, wlog}, - {c, StateFollower, 1, newLog(NewMemoryStorage(), raftLogger)}, + {c, StateFollower, 1, newLog(newMemoryStorage(), raftLogger)}, } for i, tt := range tests { @@ -976,7 +976,7 @@ func TestCandidateConcede(t *testing.T) { t.Errorf("term = %d, want %d", g, 1) } wantLog := ltoa(&raftLog{ - storage: &MemoryStorage{ + storage: &memoryStorage{ ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Index: 2, Data: data}}, }, unstable: unstable{offset: 3}, @@ -1026,7 +1026,7 @@ func TestOldMessages(t *testing.T) { tt.send(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("somedata")}}}) ilog := &raftLog{ - storage: &MemoryStorage{ + storage: &memoryStorage{ ents: []pb.Entry{ {}, {Data: nil, Term: 1, Index: 1}, {Data: nil, Term: 2, Index: 2}, {Data: nil, Term: 3, Index: 3}, @@ -1084,10 +1084,10 @@ func TestProposal(t *testing.T) { send(pb.Message{From: 1, To: 1, Type: pb.MsgHup}) send(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: data}}}) - wantLog := newLog(NewMemoryStorage(), raftLogger) + wantLog := newLog(newMemoryStorage(), raftLogger) if tt.success { wantLog = &raftLog{ - storage: &MemoryStorage{ + storage: &memoryStorage{ ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Index: 2, Data: data}}, }, unstable: unstable{offset: 3}, @@ -1126,7 +1126,7 @@ func TestProposalByProxy(t *testing.T) { tt.send(pb.Message{From: 2, To: 2, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("somedata")}}}) wantLog := &raftLog{ - storage: &MemoryStorage{ + storage: &memoryStorage{ ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Data: data, Index: 2}}, }, unstable: unstable{offset: 3}, @@ -1178,7 +1178,7 @@ func TestCommit(t *testing.T) { } for i, tt := range tests { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.Append(tt.logs) storage.hardState = pb.HardState{Term: tt.smTerm} @@ -1208,7 +1208,7 @@ func TestPastElectionTimeout(t *testing.T) { } for i, tt := range tests { - sm := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage()) + sm := newTestRaft(1, []uint64{1}, 10, 1, newMemoryStorage()) sm.electionElapsed = tt.elapse c := 0 for j := 0; j < 10000; j++ { @@ -1235,7 +1235,7 @@ func TestStepIgnoreOldTermMsg(t *testing.T) { called = true return nil } - sm := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage()) + sm := newTestRaft(1, []uint64{1}, 10, 1, newMemoryStorage()) sm.step = fakeStep sm.Term = 2 sm.Step(pb.Message{Type: pb.MsgApp, Term: sm.Term - 1}) @@ -1275,7 +1275,7 @@ func TestHandleMsgApp(t *testing.T) { } for i, tt := range tests { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.Append([]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}}) sm := newTestRaft(1, []uint64{1}, 10, 1, storage) sm.becomeFollower(2, None) @@ -1309,7 +1309,7 @@ func TestHandleHeartbeat(t *testing.T) { } for i, tt := range tests { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.Append([]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}}) sm := newTestRaft(1, []uint64{1, 2}, 5, 1, storage) sm.becomeFollower(2, 2) @@ -1330,7 +1330,7 @@ func TestHandleHeartbeat(t *testing.T) { // TestHandleHeartbeatResp ensures that we re-send log entries when we get a heartbeat response. func TestHandleHeartbeatResp(t *testing.T) { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.Append([]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}}) sm := newTestRaft(1, []uint64{1, 2}, 5, 1, storage) sm.becomeCandidate() @@ -1377,7 +1377,7 @@ func TestHandleHeartbeatResp(t *testing.T) { // readOnly readIndexQueue and pendingReadIndex map. // related issue: https://github.com/coreos/etcd/issues/7571 func TestRaftFreesReadOnlyMem(t *testing.T) { - sm := newTestRaft(1, []uint64{1, 2}, 5, 1, NewMemoryStorage()) + sm := newTestRaft(1, []uint64{1, 2}, 5, 1, newMemoryStorage()) sm.becomeCandidate() sm.becomeLeader() sm.raftLog.commitTo(sm.raftLog.lastIndex()) @@ -1425,7 +1425,7 @@ func TestRaftFreesReadOnlyMem(t *testing.T) { // TestMsgAppRespWaitReset verifies the resume behavior of a leader // MsgAppResp. func TestMsgAppRespWaitReset(t *testing.T) { - sm := newTestRaft(1, []uint64{1, 2, 3}, 5, 1, NewMemoryStorage()) + sm := newTestRaft(1, []uint64{1, 2, 3}, 5, 1, newMemoryStorage()) sm.becomeCandidate() sm.becomeLeader() @@ -1535,7 +1535,7 @@ func testRecvMsgVote(t *testing.T, msgType pb.MessageType) { } for i, tt := range tests { - sm := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage()) + sm := newTestRaft(1, []uint64{1}, 10, 1, newMemoryStorage()) sm.state = tt.state switch tt.state { case StateFollower: @@ -1547,7 +1547,7 @@ func testRecvMsgVote(t *testing.T, msgType pb.MessageType) { } sm.Vote = tt.voteFor sm.raftLog = &raftLog{ - storage: &MemoryStorage{ents: []pb.Entry{{}, {Index: 1, Term: 2}, {Index: 2, Term: 2}}}, + storage: &memoryStorage{ents: []pb.Entry{{}, {Index: 1, Term: 2}, {Index: 2, Term: 2}}}, unstable: unstable{offset: 3}, } @@ -1616,7 +1616,7 @@ func TestStateTransition(t *testing.T) { } }() - sm := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage()) + sm := newTestRaft(1, []uint64{1}, 10, 1, newMemoryStorage()) sm.state = tt.from switch tt.to { @@ -1658,7 +1658,7 @@ func TestAllServerStepdown(t *testing.T) { tterm := uint64(3) for i, tt := range tests { - sm := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + sm := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) switch tt.state { case StateFollower: sm.becomeFollower(1, None) @@ -1698,7 +1698,7 @@ func TestAllServerStepdown(t *testing.T) { } func TestLeaderStepdownWhenQuorumActive(t *testing.T) { - sm := newTestRaft(1, []uint64{1, 2, 3}, 5, 1, NewMemoryStorage()) + sm := newTestRaft(1, []uint64{1, 2, 3}, 5, 1, newMemoryStorage()) sm.checkQuorum = true @@ -1716,7 +1716,7 @@ func TestLeaderStepdownWhenQuorumActive(t *testing.T) { } func TestLeaderStepdownWhenQuorumLost(t *testing.T) { - sm := newTestRaft(1, []uint64{1, 2, 3}, 5, 1, NewMemoryStorage()) + sm := newTestRaft(1, []uint64{1, 2, 3}, 5, 1, newMemoryStorage()) sm.checkQuorum = true @@ -1733,9 +1733,9 @@ func TestLeaderStepdownWhenQuorumLost(t *testing.T) { } func TestLeaderSupersedingWithCheckQuorum(t *testing.T) { - a := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - b := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - c := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + a := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + b := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + c := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) a.checkQuorum = true b.checkQuorum = true @@ -1776,9 +1776,9 @@ func TestLeaderSupersedingWithCheckQuorum(t *testing.T) { } func TestLeaderElectionWithCheckQuorum(t *testing.T) { - a := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - b := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - c := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + a := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + b := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + c := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) a.checkQuorum = true b.checkQuorum = true @@ -1825,9 +1825,9 @@ func TestLeaderElectionWithCheckQuorum(t *testing.T) { // can disrupt the leader even if the leader still "officially" holds the lease, The // leader is expected to step down and adopt the candidate's term func TestFreeStuckCandidateWithCheckQuorum(t *testing.T) { - a := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - b := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - c := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + a := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + b := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + c := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) a.checkQuorum = true b.checkQuorum = true @@ -1892,8 +1892,8 @@ func TestFreeStuckCandidateWithCheckQuorum(t *testing.T) { } func TestNonPromotableVoterWithCheckQuorum(t *testing.T) { - a := newTestRaft(1, []uint64{1, 2}, 10, 1, NewMemoryStorage()) - b := newTestRaft(2, []uint64{1}, 10, 1, NewMemoryStorage()) + a := newTestRaft(1, []uint64{1, 2}, 10, 1, newMemoryStorage()) + b := newTestRaft(2, []uint64{1}, 10, 1, newMemoryStorage()) a.checkQuorum = true b.checkQuorum = true @@ -1926,9 +1926,9 @@ func TestNonPromotableVoterWithCheckQuorum(t *testing.T) { } func TestReadOnlyOptionSafe(t *testing.T) { - a := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - b := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - c := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + a := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + b := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + c := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) nt := newNetwork(a, b, c) setRandomizedElectionTimeout(b, b.electionTimeout+1) @@ -1980,9 +1980,9 @@ func TestReadOnlyOptionSafe(t *testing.T) { } func TestReadOnlyOptionLease(t *testing.T) { - a := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - b := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - c := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + a := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + b := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + c := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) a.readOnly.option = ReadOnlyLeaseBased b.readOnly.option = ReadOnlyLeaseBased c.readOnly.option = ReadOnlyLeaseBased @@ -2051,7 +2051,7 @@ func TestReadOnlyForNewLeader(t *testing.T) { } peers := make([]stateMachine, 0) for _, c := range nodeConfigs { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.Append([]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 1}}) storage.SetHardState(pb.HardState{Term: 1, Commit: c.committed}) if c.compact_index != 0 { @@ -2133,9 +2133,9 @@ func TestLeaderAppResp(t *testing.T) { for i, tt := range tests { // sm term is 1 after it becomes the leader. // thus the last log term must be 1 to be committed. - sm := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + sm := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) sm.raftLog = &raftLog{ - storage: &MemoryStorage{ents: []pb.Entry{{}, {Index: 1, Term: 0}, {Index: 2, Term: 1}}}, + storage: &memoryStorage{ents: []pb.Entry{{}, {Index: 1, Term: 0}, {Index: 2, Term: 1}}}, unstable: unstable{offset: 3}, } sm.becomeCandidate() @@ -2179,7 +2179,7 @@ func TestBcastBeat(t *testing.T) { ConfState: pb.ConfState{Nodes: []uint64{1, 2, 3}}, }, } - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.ApplySnapshot(s) sm := newTestRaft(1, nil, 10, 1, storage) sm.Term = 1 @@ -2240,8 +2240,8 @@ func TestRecvMsgBeat(t *testing.T) { } for i, tt := range tests { - sm := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - sm.raftLog = &raftLog{storage: &MemoryStorage{ents: []pb.Entry{{}, {Index: 1, Term: 0}, {Index: 2, Term: 1}}}} + sm := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + sm.raftLog = &raftLog{storage: &memoryStorage{ents: []pb.Entry{{}, {Index: 1, Term: 0}, {Index: 2, Term: 1}}}} sm.Term = 1 sm.state = tt.state switch tt.state { @@ -2283,7 +2283,7 @@ func TestLeaderIncreaseNext(t *testing.T) { } for i, tt := range tests { - sm := newTestRaft(1, []uint64{1, 2}, 10, 1, NewMemoryStorage()) + sm := newTestRaft(1, []uint64{1, 2}, 10, 1, newMemoryStorage()) sm.raftLog.append(previousEnts...) sm.becomeCandidate() sm.becomeLeader() @@ -2299,7 +2299,7 @@ func TestLeaderIncreaseNext(t *testing.T) { } func TestSendAppendForProgressProbe(t *testing.T) { - r := newTestRaft(1, []uint64{1, 2}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2}, 10, 1, newMemoryStorage()) r.becomeCandidate() r.becomeLeader() r.readMessages() @@ -2366,7 +2366,7 @@ func TestSendAppendForProgressProbe(t *testing.T) { } func TestSendAppendForProgressReplicate(t *testing.T) { - r := newTestRaft(1, []uint64{1, 2}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2}, 10, 1, newMemoryStorage()) r.becomeCandidate() r.becomeLeader() r.readMessages() @@ -2383,7 +2383,7 @@ func TestSendAppendForProgressReplicate(t *testing.T) { } func TestSendAppendForProgressSnapshot(t *testing.T) { - r := newTestRaft(1, []uint64{1, 2}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2}, 10, 1, newMemoryStorage()) r.becomeCandidate() r.becomeLeader() r.readMessages() @@ -2401,7 +2401,7 @@ func TestSendAppendForProgressSnapshot(t *testing.T) { func TestRecvMsgUnreachable(t *testing.T) { previousEnts := []pb.Entry{{Term: 1, Index: 1}, {Term: 1, Index: 2}, {Term: 1, Index: 3}} - s := NewMemoryStorage() + s := newMemoryStorage() s.Append(previousEnts) r := newTestRaft(1, []uint64{1, 2}, 10, 1, s) r.becomeCandidate() @@ -2431,7 +2431,7 @@ func TestRestore(t *testing.T) { }, } - storage := NewMemoryStorage() + storage := newMemoryStorage() sm := newTestRaft(1, []uint64{1, 2}, 10, 1, storage) if ok := sm.restore(s); !ok { t.Fatal("restore fail, want succeed") @@ -2463,7 +2463,7 @@ func TestRestoreWithLearner(t *testing.T) { }, } - storage := NewMemoryStorage() + storage := newMemoryStorage() sm := newTestLearnerRaft(3, []uint64{1, 2}, []uint64{3}, 10, 1, storage) if ok := sm.restore(s); !ok { t.Error("restore fail, want succeed") @@ -2510,7 +2510,7 @@ func TestRestoreInvalidLearner(t *testing.T) { }, } - storage := NewMemoryStorage() + storage := newMemoryStorage() sm := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, storage) if sm.isLearner { @@ -2532,7 +2532,7 @@ func TestRestoreLearnerPromotion(t *testing.T) { }, } - storage := NewMemoryStorage() + storage := newMemoryStorage() sm := newTestLearnerRaft(3, []uint64{1, 2}, []uint64{3}, 10, 1, storage) if !sm.isLearner { @@ -2559,8 +2559,8 @@ func TestLearnerReceiveSnapshot(t *testing.T) { }, } - n1 := newTestLearnerRaft(1, []uint64{1}, []uint64{2}, 10, 1, NewMemoryStorage()) - n2 := newTestLearnerRaft(2, []uint64{1}, []uint64{2}, 10, 1, NewMemoryStorage()) + n1 := newTestLearnerRaft(1, []uint64{1}, []uint64{2}, 10, 1, newMemoryStorage()) + n2 := newTestLearnerRaft(2, []uint64{1}, []uint64{2}, 10, 1, newMemoryStorage()) n1.restore(s) @@ -2584,7 +2584,7 @@ func TestLearnerReceiveSnapshot(t *testing.T) { func TestRestoreIgnoreSnapshot(t *testing.T) { previousEnts := []pb.Entry{{Term: 1, Index: 1}, {Term: 1, Index: 2}, {Term: 1, Index: 3}} commit := uint64(1) - storage := NewMemoryStorage() + storage := newMemoryStorage() sm := newTestRaft(1, []uint64{1, 2}, 10, 1, storage) sm.raftLog.append(previousEnts...) sm.raftLog.commitTo(commit) @@ -2624,7 +2624,7 @@ func TestProvideSnap(t *testing.T) { ConfState: pb.ConfState{Nodes: []uint64{1, 2}}, }, } - storage := NewMemoryStorage() + storage := newMemoryStorage() sm := newTestRaft(1, []uint64{1}, 10, 1, storage) sm.restore(s) @@ -2654,7 +2654,7 @@ func TestIgnoreProvidingSnap(t *testing.T) { ConfState: pb.ConfState{Nodes: []uint64{1, 2}}, }, } - storage := NewMemoryStorage() + storage := newMemoryStorage() sm := newTestRaft(1, []uint64{1}, 10, 1, storage) sm.restore(s) @@ -2684,7 +2684,7 @@ func TestRestoreFromSnapMsg(t *testing.T) { } m := pb.Message{Type: pb.MsgSnap, From: 1, Term: 2, Snapshot: s} - sm := newTestRaft(2, []uint64{1, 2}, 10, 1, NewMemoryStorage()) + sm := newTestRaft(2, []uint64{1, 2}, 10, 1, newMemoryStorage()) sm.Step(m) if sm.lead != uint64(1) { @@ -2733,7 +2733,7 @@ func TestSlowNodeRestore(t *testing.T) { // it appends the entry to log and sets pendingConf to be true. func TestStepConfig(t *testing.T) { // a raft that cannot make progress - r := newTestRaft(1, []uint64{1, 2}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2}, 10, 1, newMemoryStorage()) r.becomeCandidate() r.becomeLeader() index := r.raftLog.lastIndex() @@ -2751,7 +2751,7 @@ func TestStepConfig(t *testing.T) { // the proposal to noop and keep its original state. func TestStepIgnoreConfig(t *testing.T) { // a raft that cannot make progress - r := newTestRaft(1, []uint64{1, 2}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2}, 10, 1, newMemoryStorage()) r.becomeCandidate() r.becomeLeader() r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Type: pb.EntryConfChange}}}) @@ -2782,10 +2782,11 @@ func TestNewLeaderPendingConfig(t *testing.T) { {true, 1}, } for i, tt := range tests { - r := newTestRaft(1, []uint64{1, 2}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2}, 10, 1, newMemoryStorage()) if tt.addEntry { r.appendEntry(pb.Entry{Type: pb.EntryNormal}) } + r.becomeCandidate() r.becomeLeader() if r.pendingConfIndex != tt.wpendingIndex { @@ -2797,7 +2798,7 @@ func TestNewLeaderPendingConfig(t *testing.T) { // TestAddNode tests that addNode could update nodes correctly. func TestAddNode(t *testing.T) { - r := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1}, 10, 1, newMemoryStorage()) r.addNode(2) nodes := r.nodes() wnodes := []uint64{1, 2} @@ -2808,7 +2809,7 @@ func TestAddNode(t *testing.T) { // TestAddLearner tests that addLearner could update nodes correctly. func TestAddLearner(t *testing.T) { - r := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1}, 10, 1, newMemoryStorage()) r.addLearner(2) nodes := r.learnerNodes() wnodes := []uint64{2} @@ -2823,7 +2824,7 @@ func TestAddLearner(t *testing.T) { // TestAddNodeCheckQuorum tests that addNode does not trigger a leader election // immediately when checkQuorum is set. func TestAddNodeCheckQuorum(t *testing.T) { - r := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1}, 10, 1, newMemoryStorage()) r.checkQuorum = true r.becomeCandidate() @@ -2857,7 +2858,7 @@ func TestAddNodeCheckQuorum(t *testing.T) { // TestRemoveNode tests that removeNode could update nodes and // and removed list correctly. func TestRemoveNode(t *testing.T) { - r := newTestRaft(1, []uint64{1, 2}, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{1, 2}, 10, 1, newMemoryStorage()) r.removeNode(2) w := []uint64{1} if g := r.nodes(); !reflect.DeepEqual(g, w) { @@ -2875,7 +2876,7 @@ func TestRemoveNode(t *testing.T) { // TestRemoveLearner tests that removeNode could update nodes and // and removed list correctly. func TestRemoveLearner(t *testing.T) { - r := newTestLearnerRaft(1, []uint64{1}, []uint64{2}, 10, 1, NewMemoryStorage()) + r := newTestLearnerRaft(1, []uint64{1}, []uint64{2}, 10, 1, newMemoryStorage()) r.removeNode(2) w := []uint64{1} if g := r.nodes(); !reflect.DeepEqual(g, w) { @@ -2905,7 +2906,7 @@ func TestPromotable(t *testing.T) { {[]uint64{2, 3}, false}, } for i, tt := range tests { - r := newTestRaft(id, tt.peers, 5, 1, NewMemoryStorage()) + r := newTestRaft(id, tt.peers, 5, 1, newMemoryStorage()) if g := r.promotable(); g != tt.wp { t.Errorf("#%d: promotable = %v, want %v", i, g, tt.wp) } @@ -2927,7 +2928,7 @@ func TestRaftNodes(t *testing.T) { }, } for i, tt := range tests { - r := newTestRaft(1, tt.ids, 10, 1, NewMemoryStorage()) + r := newTestRaft(1, tt.ids, 10, 1, newMemoryStorage()) if !reflect.DeepEqual(r.nodes(), tt.wids) { t.Errorf("#%d: nodes = %+v, want %+v", i, r.nodes(), tt.wids) } @@ -2943,7 +2944,7 @@ func TestPreCampaignWhileLeader(t *testing.T) { } func testCampaignWhileLeader(t *testing.T, preVote bool) { - cfg := newTestConfig(1, []uint64{1}, 5, 1, NewMemoryStorage()) + cfg := newTestConfig(1, []uint64{1}, 5, 1, newMemoryStorage()) cfg.PreVote = preVote r := newRaft(cfg) if r.state != StateFollower { @@ -2969,7 +2970,7 @@ func testCampaignWhileLeader(t *testing.T, preVote bool) { // committed when a config change reduces the quorum requirements. func TestCommitAfterRemoveNode(t *testing.T) { // Create a cluster with two nodes. - s := NewMemoryStorage() + s := newMemoryStorage() r := newTestRaft(1, []uint64{1, 2}, 5, 1, s) r.becomeCandidate() r.becomeLeader() @@ -3359,7 +3360,7 @@ func checkLeaderTransferState(t *testing.T, r *raft, state StateType, lead uint6 // (previously, if the node also got votes, it would panic as it // transitioned to StateLeader) func TestTransferNonMember(t *testing.T) { - r := newTestRaft(1, []uint64{2, 3, 4}, 5, 1, NewMemoryStorage()) + r := newTestRaft(1, []uint64{2, 3, 4}, 5, 1, newMemoryStorage()) r.Step(pb.Message{From: 2, To: 1, Type: pb.MsgTimeoutNow}) r.Step(pb.Message{From: 2, To: 1, Type: pb.MsgVoteResp}) @@ -3375,9 +3376,9 @@ func TestTransferNonMember(t *testing.T) { // Previously the cluster would come to a standstill when run with PreVote // enabled. func TestNodeWithSmallerTermCanCompleteElection(t *testing.T) { - n1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - n2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - n3 := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + n1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + n2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + n3 := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) n1.becomeFollower(1, None) n2.becomeFollower(1, None) @@ -3470,9 +3471,9 @@ func TestNodeWithSmallerTermCanCompleteElection(t *testing.T) { // TestPreVoteWithSplitVote verifies that after split vote, cluster can complete // election in next round. func TestPreVoteWithSplitVote(t *testing.T) { - n1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - n2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - n3 := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + n1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + n2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + n3 := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) n1.becomeFollower(1, None) n2.becomeFollower(1, None) @@ -3549,9 +3550,9 @@ func TestPreVoteWithSplitVote(t *testing.T) { // n2 is follower with term 2 // n3 is partitioned, with term 4 and less log, state is candidate func newPreVoteMigrationCluster(t *testing.T) *network { - n1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - n2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - n3 := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) + n1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + n2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) + n3 := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, newMemoryStorage()) n1.becomeFollower(1, None) n2.becomeFollower(1, None) @@ -3689,7 +3690,7 @@ func TestPreVoteMigrationWithFreeStuckPreCandidate(t *testing.T) { } func entsWithConfig(configFunc func(*Config), terms ...uint64) *raft { - storage := NewMemoryStorage() + storage := newMemoryStorage() for i, term := range terms { storage.Append([]pb.Entry{{Index: uint64(i + 1), Term: term}}) } @@ -3706,7 +3707,7 @@ func entsWithConfig(configFunc func(*Config), terms ...uint64) *raft { // to the given value but no log entries (indicating that it voted in // the given term but has not received any logs). func votedWithConfig(configFunc func(*Config), vote, term uint64) *raft { - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.SetHardState(pb.HardState{Vote: vote, Term: term}) cfg := newTestConfig(1, []uint64{}, 5, 1, storage) if configFunc != nil { @@ -3719,7 +3720,7 @@ func votedWithConfig(configFunc func(*Config), vote, term uint64) *raft { type network struct { peers map[uint64]stateMachine - storage map[uint64]*MemoryStorage + storage map[uint64]*memoryStorage dropm map[connem]float64 ignorem map[pb.MessageType]bool } @@ -3739,13 +3740,13 @@ func newNetworkWithConfig(configFunc func(*Config), peers ...stateMachine) *netw peerAddrs := idsBySize(size) npeers := make(map[uint64]stateMachine, size) - nstorage := make(map[uint64]*MemoryStorage, size) + nstorage := make(map[uint64]*memoryStorage, size) for j, p := range peers { id := peerAddrs[j] switch v := p.(type) { case nil: - nstorage[id] = NewMemoryStorage() + nstorage[id] = newMemoryStorage() cfg := newTestConfig(id, peerAddrs, 10, 1, nstorage[id]) if configFunc != nil { configFunc(cfg) diff --git a/raft/rafttest/node.go b/raft/rafttest/node.go index 0c47bb2f9be1..11b2ac076190 100644 --- a/raft/rafttest/node.go +++ b/raft/rafttest/node.go @@ -32,14 +32,14 @@ type node struct { pausec chan bool // stable - storage *raft.MemoryStorage + storage raft.Storage mu sync.Mutex // guards state state raftpb.HardState } func startNode(id uint64, peers []raft.Peer, iface iface) *node { - st := raft.NewMemoryStorage() + st := raft.NewStorage() c := &raft.Config{ ID: id, ElectionTick: 10, diff --git a/raft/rawnode_test.go b/raft/rawnode_test.go index f034521be575..822de2ce3c50 100644 --- a/raft/rawnode_test.go +++ b/raft/rawnode_test.go @@ -25,7 +25,7 @@ import ( // TestRawNodeStep ensures that RawNode.Step ignore local message. func TestRawNodeStep(t *testing.T) { for i, msgn := range raftpb.MessageType_name { - s := NewMemoryStorage() + s := newMemoryStorage() rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), []Peer{{ID: 1}}) if err != nil { t.Fatal(err) @@ -47,7 +47,7 @@ func TestRawNodeStep(t *testing.T) { // TestRawNodeProposeAndConfChange ensures that RawNode.Propose and RawNode.ProposeConfChange // send the given proposal and ConfChange to the underlying raft. func TestRawNodeProposeAndConfChange(t *testing.T) { - s := NewMemoryStorage() + s := newMemoryStorage() var err error rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), []Peer{{ID: 1}}) if err != nil { @@ -113,7 +113,7 @@ func TestRawNodeProposeAndConfChange(t *testing.T) { // TestRawNodeProposeAddDuplicateNode ensures that two proposes to add the same node should // not affect the later propose to add new node. func TestRawNodeProposeAddDuplicateNode(t *testing.T) { - s := NewMemoryStorage() + s := newMemoryStorage() rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), []Peer{{ID: 1}}) if err != nil { t.Fatal(err) @@ -196,7 +196,7 @@ func TestRawNodeReadIndex(t *testing.T) { } wrs := []ReadState{{Index: uint64(1), RequestCtx: []byte("somedata")}} - s := NewMemoryStorage() + s := newMemoryStorage() c := newTestConfig(1, nil, 10, 1, s) rawNode, err := NewRawNode(c, []Peer{{ID: 1}}) if err != nil { @@ -284,7 +284,7 @@ func TestRawNodeStart(t *testing.T) { }, } - storage := NewMemoryStorage() + storage := newMemoryStorage() rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), []Peer{{ID: 1}}) if err != nil { t.Fatal(err) @@ -332,7 +332,7 @@ func TestRawNodeRestart(t *testing.T) { MustSync: true, } - storage := NewMemoryStorage() + storage := newMemoryStorage() storage.SetHardState(st) storage.Append(entries) rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), nil) @@ -369,7 +369,7 @@ func TestRawNodeRestartFromSnapshot(t *testing.T) { MustSync: true, } - s := NewMemoryStorage() + s := newMemoryStorage() s.SetHardState(st) s.ApplySnapshot(snap) s.Append(entries) @@ -391,7 +391,7 @@ func TestRawNodeRestartFromSnapshot(t *testing.T) { // no dependency check between Ready() and Advance() func TestRawNodeStatus(t *testing.T) { - storage := NewMemoryStorage() + storage := newMemoryStorage() rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), []Peer{{ID: 1}}) if err != nil { t.Fatal(err) diff --git a/raft/storage.go b/raft/storage.go index 69c3a7d90338..1bdb05af1818 100644 --- a/raft/storage.go +++ b/raft/storage.go @@ -67,11 +67,23 @@ type Storage interface { // so raft state machine could know that Storage needs some time to prepare // snapshot and call Snapshot later. Snapshot() (pb.Snapshot, error) + // SetHardState saves the current HardState. + SetHardState(st pb.HardState) error + // ApplySnapshot overwrites the contents of this Storage object with + // those of the given snapshot. + ApplySnapshot(snap pb.Snapshot) error + // CreateSnapshot makes a snapshot which can be retrieved with Snapshot() and + // can be used to reconstruct the state at that point. + CreateSnapshot(i uint64, cs *pb.ConfState, data []byte) (pb.Snapshot, error) + // Compact discards all log entries prior to compactIndex. + Compact(compactIndex uint64) error + // Append appends the new entries to storage. + Append(entries []pb.Entry) error } -// MemoryStorage implements the Storage interface backed by an +// memoryStorage implements the Storage interface backed by an // in-memory array. -type MemoryStorage struct { +type memoryStorage struct { // Protects access to all fields. Most methods of MemoryStorage are // run on the raft goroutine, but Append() is run on an application // goroutine. @@ -83,21 +95,26 @@ type MemoryStorage struct { ents []pb.Entry } -// NewMemoryStorage creates an empty MemoryStorage. -func NewMemoryStorage() *MemoryStorage { - return &MemoryStorage{ +// newMemoryStorage creates an empty memoryStorage. +func newMemoryStorage() *memoryStorage { + return &memoryStorage{ // When starting from scratch populate the list with a dummy entry at term zero. ents: make([]pb.Entry, 1), } } +// NewStorage creates an object which is an empty memoryStorage and implements Storage interface. +func NewStorage() Storage { + return newMemoryStorage() +} + // InitialState implements the Storage interface. -func (ms *MemoryStorage) InitialState() (pb.HardState, pb.ConfState, error) { +func (ms *memoryStorage) InitialState() (pb.HardState, pb.ConfState, error) { return ms.hardState, ms.snapshot.Metadata.ConfState, nil } // SetHardState saves the current HardState. -func (ms *MemoryStorage) SetHardState(st pb.HardState) error { +func (ms *memoryStorage) SetHardState(st pb.HardState) error { ms.Lock() defer ms.Unlock() ms.hardState = st @@ -105,7 +122,7 @@ func (ms *MemoryStorage) SetHardState(st pb.HardState) error { } // Entries implements the Storage interface. -func (ms *MemoryStorage) Entries(lo, hi, maxSize uint64) ([]pb.Entry, error) { +func (ms *memoryStorage) Entries(lo, hi, maxSize uint64) ([]pb.Entry, error) { ms.Lock() defer ms.Unlock() offset := ms.ents[0].Index @@ -125,7 +142,7 @@ func (ms *MemoryStorage) Entries(lo, hi, maxSize uint64) ([]pb.Entry, error) { } // Term implements the Storage interface. -func (ms *MemoryStorage) Term(i uint64) (uint64, error) { +func (ms *memoryStorage) Term(i uint64) (uint64, error) { ms.Lock() defer ms.Unlock() offset := ms.ents[0].Index @@ -139,29 +156,29 @@ func (ms *MemoryStorage) Term(i uint64) (uint64, error) { } // LastIndex implements the Storage interface. -func (ms *MemoryStorage) LastIndex() (uint64, error) { +func (ms *memoryStorage) LastIndex() (uint64, error) { ms.Lock() defer ms.Unlock() return ms.lastIndex(), nil } -func (ms *MemoryStorage) lastIndex() uint64 { +func (ms *memoryStorage) lastIndex() uint64 { return ms.ents[0].Index + uint64(len(ms.ents)) - 1 } // FirstIndex implements the Storage interface. -func (ms *MemoryStorage) FirstIndex() (uint64, error) { +func (ms *memoryStorage) FirstIndex() (uint64, error) { ms.Lock() defer ms.Unlock() return ms.firstIndex(), nil } -func (ms *MemoryStorage) firstIndex() uint64 { +func (ms *memoryStorage) firstIndex() uint64 { return ms.ents[0].Index + 1 } // Snapshot implements the Storage interface. -func (ms *MemoryStorage) Snapshot() (pb.Snapshot, error) { +func (ms *memoryStorage) Snapshot() (pb.Snapshot, error) { ms.Lock() defer ms.Unlock() return ms.snapshot, nil @@ -169,7 +186,7 @@ func (ms *MemoryStorage) Snapshot() (pb.Snapshot, error) { // ApplySnapshot overwrites the contents of this Storage object with // those of the given snapshot. -func (ms *MemoryStorage) ApplySnapshot(snap pb.Snapshot) error { +func (ms *memoryStorage) ApplySnapshot(snap pb.Snapshot) error { ms.Lock() defer ms.Unlock() @@ -189,7 +206,7 @@ func (ms *MemoryStorage) ApplySnapshot(snap pb.Snapshot) error { // can be used to reconstruct the state at that point. // If any configuration changes have been made since the last compaction, // the result of the last ApplyConfChange must be passed in. -func (ms *MemoryStorage) CreateSnapshot(i uint64, cs *pb.ConfState, data []byte) (pb.Snapshot, error) { +func (ms *memoryStorage) CreateSnapshot(i uint64, cs *pb.ConfState, data []byte) (pb.Snapshot, error) { ms.Lock() defer ms.Unlock() if i <= ms.snapshot.Metadata.Index { @@ -213,7 +230,7 @@ func (ms *MemoryStorage) CreateSnapshot(i uint64, cs *pb.ConfState, data []byte) // Compact discards all log entries prior to compactIndex. // It is the application's responsibility to not attempt to compact an index // greater than raftLog.applied. -func (ms *MemoryStorage) Compact(compactIndex uint64) error { +func (ms *memoryStorage) Compact(compactIndex uint64) error { ms.Lock() defer ms.Unlock() offset := ms.ents[0].Index @@ -236,7 +253,7 @@ func (ms *MemoryStorage) Compact(compactIndex uint64) error { // Append the new entries to storage. // TODO (xiangli): ensure the entries are continuous and // entries[0].Index > ms.entries[0].Index -func (ms *MemoryStorage) Append(entries []pb.Entry) error { +func (ms *memoryStorage) Append(entries []pb.Entry) error { if len(entries) == 0 { return nil } diff --git a/raft/storage_test.go b/raft/storage_test.go index 71d50b4c946c..ea5a148a4776 100644 --- a/raft/storage_test.go +++ b/raft/storage_test.go @@ -39,7 +39,7 @@ func TestStorageTerm(t *testing.T) { } for i, tt := range tests { - s := &MemoryStorage{ents: ents} + s := &memoryStorage{ents: ents} func() { defer func() { @@ -86,7 +86,7 @@ func TestStorageEntries(t *testing.T) { } for i, tt := range tests { - s := &MemoryStorage{ents: ents} + s := &memoryStorage{ents: ents} entries, err := s.Entries(tt.lo, tt.hi, tt.maxsize) if err != tt.werr { t.Errorf("#%d: err = %v, want %v", i, err, tt.werr) @@ -99,7 +99,7 @@ func TestStorageEntries(t *testing.T) { func TestStorageLastIndex(t *testing.T) { ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}} - s := &MemoryStorage{ents: ents} + s := &memoryStorage{ents: ents} last, err := s.LastIndex() if err != nil { @@ -121,7 +121,7 @@ func TestStorageLastIndex(t *testing.T) { func TestStorageFirstIndex(t *testing.T) { ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}} - s := &MemoryStorage{ents: ents} + s := &memoryStorage{ents: ents} first, err := s.FirstIndex() if err != nil { @@ -158,7 +158,7 @@ func TestStorageCompact(t *testing.T) { } for i, tt := range tests { - s := &MemoryStorage{ents: ents} + s := &memoryStorage{ents: ents} err := s.Compact(tt.i) if err != tt.werr { t.Errorf("#%d: err = %v, want %v", i, err, tt.werr) @@ -191,7 +191,7 @@ func TestStorageCreateSnapshot(t *testing.T) { } for i, tt := range tests { - s := &MemoryStorage{ents: ents} + s := &memoryStorage{ents: ents} snap, err := s.CreateSnapshot(tt.i, cs, data) if err != tt.werr { t.Errorf("#%d: err = %v, want %v", i, err, tt.werr) @@ -246,7 +246,7 @@ func TestStorageAppend(t *testing.T) { } for i, tt := range tests { - s := &MemoryStorage{ents: ents} + s := &memoryStorage{ents: ents} err := s.Append(tt.entries) if err != tt.werr { t.Errorf("#%d: err = %v, want %v", i, err, tt.werr) @@ -265,7 +265,7 @@ func TestStorageApplySnapshot(t *testing.T) { {Data: data, Metadata: pb.SnapshotMetadata{Index: 3, Term: 3, ConfState: *cs}}, } - s := NewMemoryStorage() + s := newMemoryStorage() //Apply Snapshot successful i := 0