Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

etcdserver: separate maybeCompactRaftLog function to compact raft log independently #18635

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the test, it's really great to protect our assumption via test.

clement2026 marked this conversation as resolved.
Show resolved Hide resolved
// 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)
clement2026 marked this conversation as resolved.
Show resolved Hide resolved
}
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.
clement2026 marked this conversation as resolved.
Show resolved Hide resolved
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)
serathius marked this conversation as resolved.
Show resolved Hide resolved
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
Loading