Skip to content

Commit

Permalink
apply suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: Clement <[email protected]>
  • Loading branch information
clement2026 committed Sep 25, 2024
1 parent 06a2cb7 commit 2028c24
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
67 changes: 67 additions & 0 deletions server/etcdserver/memory_storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package etcdserver

import (
"testing"

"github.com/stretchr/testify/assert"

"go.etcd.io/raft/v3"
"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) {
// entries: [ {Index: 0} ]
raftStorage := raft.NewMemoryStorage()

firstIndex, err := raftStorage.FirstIndex()
assert.NoError(t, err)
assert.Equal(t, uint64(1), firstIndex)

// after appending, entries should be:
// [ {Index: 0}, {Index: 1}, {Index: 2}, {Index: 3}, {Index: 4}, {Index: 5} ]
appliedIndex := uint64(1)
for ; appliedIndex <= 5; appliedIndex++ {
e := raftpb.Entry{
Type: raftpb.EntryNormal,
Term: 1,
Index: appliedIndex,
}
err := raftStorage.Append([]raftpb.Entry{e})
assert.NoError(t, err)
}

firstIndex, err = raftStorage.FirstIndex()
assert.NoError(t, err)
assert.Equal(t, uint64(1), firstIndex)

lastIndex, err := raftStorage.LastIndex()
assert.NoError(t, err)
assert.Equal(t, uint64(5), lastIndex)

// after compacting, entries should be:
// [ {Index: 3}, {Index: 4}, {Index: 5} ]
err = raftStorage.Compact(3)
assert.NoError(t, err)


firstIndex, err = raftStorage.FirstIndex()
assert.NoError(t, err)
assert.Equal(t, uint64(3+1), firstIndex)
}
12 changes: 9 additions & 3 deletions server/etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ func (s *EtcdServer) run() {
if err != nil {
lg.Panic("failed to get snapshot from Raft storage", zap.Error(err))
}
fi, err := s.r.raftStorage.FirstIndex()
firstIndex, err := s.r.raftStorage.FirstIndex()
if err != nil {
lg.Panic("failed to get first index from Raft storage", zap.Error(err))
}
Expand Down Expand Up @@ -818,7 +818,13 @@ func (s *EtcdServer) run() {
snapi: sn.Metadata.Index,
appliedt: sn.Metadata.Term,
appliedi: sn.Metadata.Index,
compacti: fi - 1,
// 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.
compacti: firstIndex - 1,
}

defer func() {
Expand Down Expand Up @@ -2205,7 +2211,6 @@ func (s *EtcdServer) maybeCompactRaftLog(ep *etcdProgress) {
}

err := s.r.raftStorage.Compact(compacti)
ep.compacti = compacti
if err != nil {
// the compaction was done asynchronously with the progress of raft.
// raft log might already been compact.
Expand All @@ -2214,6 +2219,7 @@ func (s *EtcdServer) maybeCompactRaftLog(ep *etcdProgress) {
}
lg.Panic("failed to compact", zap.Error(err))
}
ep.compacti = compacti
lg.Info(
"compacted Raft logs",
zap.Uint64("compact-index", compacti),
Expand Down

0 comments on commit 2028c24

Please sign in to comment.