apply suggestions

Signed-off-by: Clement <gh.2lgqz@aleeas.com>
This commit is contained in:
Clement 2024-09-25 16:44:04 +08:00
parent 06a2cb7369
commit 2028c24677
2 changed files with 76 additions and 3 deletions

View File

@ -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)
}

View File

@ -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))
}
@ -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() {
@ -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.
@ -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),