Skip to content

Commit

Permalink
sstable: return error instead of panic on SSTable ID extract
Browse files Browse the repository at this point in the history
  • Loading branch information
karol-kokoszka committed Jul 12, 2024
1 parent e5c123f commit e0ace3a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
14 changes: 12 additions & 2 deletions pkg/service/backup/worker_deduplicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ func (w *worker) deduplicateHost(ctx context.Context, h hostInfo) error {
Recurse: true,
}
if err := w.Client.RcloneListDirIter(ctx, h.IP, dataDst, listOpts, func(f *scyllaclient.RcloneListDirItem) {
id := sstable.ExtractID(f.Name)
id, err := sstable.ExtractID(f.Name)
if err != nil {
// just log and continue (should never happen)
w.Logger.Error(ctx, "Extracting SSTable generation ID of remote SSTable", "error", err)
return
}
remoteFilesSSTableIDSet[id] = struct{}{}
}); err != nil {
return errors.Wrapf(err, "host %s: listing all files from %s", h.IP, dataDst)
Expand All @@ -61,7 +66,12 @@ func (w *worker) deduplicateHost(ctx context.Context, h hostInfo) error {
// Iterate over all SSTable IDs and group files per ID.
ssTablesGroupByID := make(map[string][]string)
for _, file := range d.Progress.files {
id := sstable.ExtractID(file.Name)
id, err := sstable.ExtractID(file.Name)
if err != nil {
// just log and continue
w.Logger.Error(ctx, "Extracting SSTable generation ID", "error", err)
continue
}
ssTablesGroupByID[id] = append(ssTablesGroupByID[id], file.Name)
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/service/restore/tablesdir_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,10 @@ type bundle []string
func newBundles(fm FilesMeta) map[string]bundle {
bundles := make(map[string]bundle)
for _, f := range fm.Files {
id := sstable.ExtractID(f)
id, err := sstable.ExtractID(f)
if err != nil {
panic(err)
}
bundles[id] = append(bundles[id], f)
}
return bundles
Expand Down
13 changes: 8 additions & 5 deletions pkg/sstable/sstable_naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"
// Supported SSTable format versions are: "mc", "md", "me", "la", "ka".
// Scylla code validating SSTable format can be found here:
// https://github.com/scylladb/scylladb/blob/decbc841b749d8dd3e2ddd4be4817c57d905eff2/sstables/sstables.cc#L2115-L2117
func ExtractID(sstable string) string {
func ExtractID(sstable string) (string, error) {
parts := strings.Split(sstable, "-")

if regexLaMx.MatchString(sstable) || regexNewLaMx.MatchString(sstable) {
return parts[1]
return parts[1], nil
}
if regexKa.MatchString(sstable) {
return parts[3]
return parts[3], nil
}

panic(unknownSSTableError(sstable))
return "", unknownSSTableError(sstable)
}

// Crc32FileNameFromGivenSSTableFile returns name of the .crc32 file generated for the SSTable which contains
Expand Down Expand Up @@ -101,7 +101,10 @@ func RenameSStables(sstables []string, nameGen func(id string) string) map[strin
out := make(map[string]string)

for _, sst := range sstables {
id := ExtractID(sst)
id, err := ExtractID(sst)
if err != nil {
panic(err)
}
newID, ok := idMapping[id]
if !ok {
newID = nameGen(id)
Expand Down
5 changes: 4 additions & 1 deletion pkg/sstable/sstable_naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ func TestSExtractID(t *testing.T) {
tcase := testCases[id]
t.Run(tcase.in, func(t *testing.T) {
t.Parallel()
val := ExtractID(tcase.in)
val, err := ExtractID(tcase.in)
if err != nil {
t.Fatalf("invalid sstable, error = {%v}", err)
}
if tcase.expected != val {
t.Fatalf("expected %s, received %s", tcase.expected, val)
}
Expand Down

0 comments on commit e0ace3a

Please sign in to comment.