diff --git a/server/etcdserver/memory_storage_test.go b/server/etcdserver/memory_storage_test.go index bbbbc9787..19e61efc6 100644 --- a/server/etcdserver/memory_storage_test.go +++ b/server/etcdserver/memory_storage_test.go @@ -23,10 +23,9 @@ import ( "go.etcd.io/raft/v3/raftpb" ) -// TestMemoryStorageCompaction tests that after calling raftStorage.Compact(compacti) -// without errors, the dummy entry becomes {Index: compacti} and -// raftStorage.FirstIndex() returns (compacti+1, nil). -func TestMemoryStorageCompaction(t *testing.T) { +// TestMemoryStorageCompactInclusive tests that compacting is inclusive, +// meaning the first index after compaction is larger by one than compacted index. +func TestMemoryStorageCompactInclusive(t *testing.T) { // entries: [ {Index: 0} ] raftStorage := raft.NewMemoryStorage() @@ -57,11 +56,12 @@ func TestMemoryStorageCompaction(t *testing.T) { // after compacting, entries should be: // [ {Index: 3}, {Index: 4}, {Index: 5} ] - err = raftStorage.Compact(3) + compacti := uint64(3) + err = raftStorage.Compact(compacti) assert.NoError(t, err) firstIndex, err = raftStorage.FirstIndex() assert.NoError(t, err) - assert.Equal(t, uint64(3+1), firstIndex) + assert.Equal(t, compacti+1, firstIndex) } diff --git a/server/etcdserver/server.go b/server/etcdserver/server.go index 0463a175a..165a2a4c0 100644 --- a/server/etcdserver/server.go +++ b/server/etcdserver/server.go @@ -818,12 +818,9 @@ func (s *EtcdServer) run() { snapi: sn.Metadata.Index, appliedt: sn.Metadata.Term, appliedi: sn.Metadata.Index, - // compacti is the index from the last time raftStorage.Compact was called - // without errors. - // - // After calling raftStorage.Compact(compacti) without errors, the dummy entry of - // raftStorage becomes {Index: compacti}, and raftStorage.FirstIndex() returns - // (compacti+1, nil). This is validated by TestMemoryStorageCompaction. + // Compaction is inclusive, meaning compact index should be lower by one + // than the first index after compaction. + // This is validated by TestMemoryStorageCompaction. compacti: firstRaftIndex - 1, }