From ef584a9df6b4d5b360aa99ddee605b9b92f5ad45 Mon Sep 17 00:00:00 2001 From: ide-rea <30512600+ide-rea@users.noreply.github.com> Date: Tue, 25 May 2021 18:08:35 +0800 Subject: [PATCH] Improve wal.go segments sequential validation (#8859) Signed-off-by: XiaoYu Zhang --- tsdb/wal/watcher.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tsdb/wal/watcher.go b/tsdb/wal/watcher.go index 5deb608c3..f470641ae 100644 --- a/tsdb/wal/watcher.go +++ b/tsdb/wal/watcher.go @@ -304,20 +304,19 @@ func (w *Watcher) segments(dir string) ([]int, error) { } var refs []int - var last int for _, f := range files { k, err := strconv.Atoi(f.Name()) if err != nil { continue } - if len(refs) > 0 && k > last+1 { - return nil, errors.New("segments are not sequential") - } refs = append(refs, k) - last = k } sort.Ints(refs) - + for i := 0; i < len(refs)-1; i++ { + if refs[i]+1 != refs[i+1] { + return nil, errors.New("segments are not sequential") + } + } return refs, nil }