Skip to content
This repository was archived by the owner on Aug 13, 2019. It is now read-only.

Commit 4462229

Browse files
author
Krasi Georgiev
committed
delete only valid ULID dirs, some better logging.
Signed-off-by: Krasi Georgiev <[email protected]>
1 parent f1cfeae commit 4462229

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

cmd/tsdb/main.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io"
2121
"io/ioutil"
2222
"os"
23+
"path"
2324
"path/filepath"
2425
"runtime"
2526
"runtime/pprof"
@@ -31,6 +32,7 @@ import (
3132
"time"
3233

3334
"github.com/go-kit/kit/log"
35+
"github.com/oklog/ulid"
3436
"github.com/pkg/errors"
3537
"github.com/prometheus/tsdb"
3638
"github.com/prometheus/tsdb/labels"
@@ -92,7 +94,10 @@ are safe to delete as long as you are sure that no other application is currentl
9294
exitWithError(err)
9395
}
9496

95-
for err, unreadable := range scanner.Scan() {
97+
var unreadable []*tsdb.Block
98+
for err, unreadable = range scanner.Scan() {
99+
fmt.Printf("\nCorrupted blocks : %v \nDeleting these will remove all data in the listed time range.\n\n", err)
100+
96101
switch err.(type) {
97102
case tsdb.ErrOverlap:
98103
var biggestIndex int
@@ -106,15 +111,16 @@ are safe to delete as long as you are sure that no other application is currentl
106111
// Don't delete the bigest block in the overlaps.
107112
unreadable = append(unreadable[:biggestIndex], unreadable[biggestIndex+1:]...)
108113

109-
fmt.Printf("\n%v \nDeleting these will loose all data in the listed time ranges.\n\n", err)
110-
fmt.Printf("\nBlock %v contains most samples is ommited from the list and it won't be deleted! \n\n", biggest)
111-
112-
default:
113-
fmt.Printf("\nUnreadable blocks : %v \nDeleting these will loose all data in the listed time ranges.\n\n", err)
114-
114+
fmt.Printf("\nBlock %v contains most samples and is ommited from the deletion listgit ! \n\n", biggest)
115115
}
116116
promptDelete(unreadable, scanCmdHumanReadable)
117117
}
118+
119+
fmt.Println("Scan complete!")
120+
if len(unreadable) == 0 {
121+
fmt.Println("Hooray! The db is clean(or the scan tool is broken).\U0001f638")
122+
return
123+
}
118124
}
119125
flag.CommandLine.Set("log.level", "debug")
120126
}
@@ -144,9 +150,14 @@ func promptDelete(i interface{}, humanReadable *bool) {
144150
s = strings.ToLower(s)
145151

146152
if s == "y" || s == "yes" {
147-
for _, path := range paths {
148-
if err := os.Remove(path); err != nil {
149-
fmt.Printf("error deleting: %v, %v", path, err)
153+
for _, p := range paths {
154+
_, folder := path.Split(p)
155+
if _, err := ulid.Parse(folder); err != nil {
156+
exitWithError(fmt.Errorf("dir doesn't contain a valid ULID:%v", err))
157+
}
158+
159+
if err := os.RemoveAll(p); err != nil {
160+
fmt.Printf("error deleting: %v, %v", p, err)
150161
}
151162
}
152163
return

0 commit comments

Comments
 (0)