Skip to content

Commit b531071

Browse files
authored
Merge pull request #19082 from ahrtr/etcd-logs_20241218
[tools/etcd-dump-logs] Fix the usage of --start-index in etcd-dump-logs
2 parents 0966b4d + 76f436c commit b531071

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

tools/etcd-dump-logs/main.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,15 @@ and output a hex encoded line of binary for each input line`)
7474
log.Fatal("start-snap and start-index flags cannot be used together.")
7575
}
7676

77+
startFromIndex := false
78+
flag.Visit(func(f *flag.Flag) {
79+
if f.Name == "start-index" {
80+
startFromIndex = true
81+
}
82+
})
83+
7784
if !*raw {
78-
ents := readUsingReadAll(lg, index, snapfile, dataDir, waldir)
85+
ents := readUsingReadAll(lg, startFromIndex, index, snapfile, dataDir, waldir)
7986

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

107-
func readUsingReadAll(lg *zap.Logger, index *uint64, snapfile *string, dataDir string, waldir *string) []raftpb.Entry {
114+
func readUsingReadAll(lg *zap.Logger, startFromIndex bool, index *uint64, snapfile *string, dataDir string, waldir *string) []raftpb.Entry {
108115
var (
109116
walsnap walpb.Snapshot
110117
snapshot *raftpb.Snapshot
111118
err error
112119
)
113120

114-
isIndex := *index != 0
115-
116-
if isIndex {
121+
if startFromIndex {
117122
fmt.Printf("Start dumping log entries from index %d.\n", *index)
118123
walsnap.Index = *index
119124
} else {
@@ -154,7 +159,7 @@ func readUsingReadAll(lg *zap.Logger, index *uint64, snapfile *string, dataDir s
154159
}
155160
wmetadata, state, ents, err := w.ReadAll()
156161
w.Close()
157-
if err != nil && (!isIndex || !errors.Is(err, wal.ErrSnapshotNotFound)) {
162+
if err != nil && (!startFromIndex || !errors.Is(err, wal.ErrSnapshotNotFound)) {
158163
log.Fatalf("Failed reading WAL: %v", err)
159164
}
160165
id, cid := parseWALMetadata(wmetadata)

0 commit comments

Comments
 (0)