Skip to content

Commit

Permalink
Fix the usage of --start-index in etcd-dump-logs
Browse files Browse the repository at this point in the history
Currently if users explicitly set --start-index=0, it will
be ignored. It isn't expected, we should respect it as long
as users explicitly set it.

Signed-off-by: Benjamin Wang <[email protected]>
  • Loading branch information
ahrtr committed Dec 18, 2024
1 parent 0966b4d commit 76f436c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions tools/etcd-dump-logs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,15 @@ and output a hex encoded line of binary for each input line`)
log.Fatal("start-snap and start-index flags cannot be used together.")
}

startFromIndex := false
flag.Visit(func(f *flag.Flag) {
if f.Name == "start-index" {
startFromIndex = true
}
})

if !*raw {
ents := readUsingReadAll(lg, index, snapfile, dataDir, waldir)
ents := readUsingReadAll(lg, startFromIndex, index, snapfile, dataDir, waldir)

fmt.Printf("WAL entries: %d\n", len(ents))
if len(ents) > 0 {
Expand Down Expand Up @@ -104,16 +111,14 @@ and output a hex encoded line of binary for each input line`)
}
}

func readUsingReadAll(lg *zap.Logger, index *uint64, snapfile *string, dataDir string, waldir *string) []raftpb.Entry {
func readUsingReadAll(lg *zap.Logger, startFromIndex bool, index *uint64, snapfile *string, dataDir string, waldir *string) []raftpb.Entry {
var (
walsnap walpb.Snapshot
snapshot *raftpb.Snapshot
err error
)

isIndex := *index != 0

if isIndex {
if startFromIndex {
fmt.Printf("Start dumping log entries from index %d.\n", *index)
walsnap.Index = *index
} else {
Expand Down Expand Up @@ -154,7 +159,7 @@ func readUsingReadAll(lg *zap.Logger, index *uint64, snapfile *string, dataDir s
}
wmetadata, state, ents, err := w.ReadAll()
w.Close()
if err != nil && (!isIndex || !errors.Is(err, wal.ErrSnapshotNotFound)) {
if err != nil && (!startFromIndex || !errors.Is(err, wal.ErrSnapshotNotFound)) {
log.Fatalf("Failed reading WAL: %v", err)
}
id, cid := parseWALMetadata(wmetadata)
Expand Down

0 comments on commit 76f436c

Please sign in to comment.