You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ha...@apache.org on 2026/04/30 05:11:19 UTC
(skywalking-banyandb) 15/24: Fix FileSystemError not matching io/fs.ErrNotExist sentinel (#1088)
This is an automated email from the ASF dual-hosted git repository.
hanahmily pushed a commit to branch v0.10.x
in repository https://gitbox.apache.org/repos/asf/skywalking-banyandb.git
commit e11effa4d66139464ab28cc8f8408da75b92478e
Author: Gao Hongtao <ha...@gmail.com>
AuthorDate: Thu Apr 23 09:09:44 2026 +0800
Fix FileSystemError not matching io/fs.ErrNotExist sentinel (#1088)
---
banyand/internal/storage/tsdb_test.go | 59 ++++++++++++++++++++++++++++++++
pkg/fs/error.go | 22 +++++++++++-
pkg/fs/error_test.go | 63 +++++++++++++++++++++++++++++++++++
3 files changed, 143 insertions(+), 1 deletion(-)
diff --git a/banyand/internal/storage/tsdb_test.go b/banyand/internal/storage/tsdb_test.go
index cf87cb3f5..f07263741 100644
--- a/banyand/internal/storage/tsdb_test.go
+++ b/banyand/internal/storage/tsdb_test.go
@@ -399,6 +399,65 @@ func TestEpochSegmentCleanupOnOpen(t *testing.T) {
require.NoDirExists(t, epochSegDir, "epoch segment should be removed on open")
}
+// TestHalfBornSegmentCleanupOnOpen reproduces the production failure where a
+// segment directory exists on disk without a metadata file (e.g. crash between
+// MkdirPanicIfExist and metadata write, or partial sync receive). Prior to the
+// errors.Is fix in pkg/fs, localFileSystem.Read returned a *FileSystemError
+// that did not satisfy errors.Is(err, io/fs.ErrNotExist), so the segment
+// controller's cleanup branch was never taken and OpenTSDB failed, leaving the
+// group with db=nil forever.
+func TestHalfBornSegmentCleanupOnOpen(t *testing.T) {
+ logger.Init(logger.Logging{
+ Env: "dev",
+ Level: flags.LogLevel,
+ })
+ dir, defFn := test.Space(require.New(t))
+ defer defFn()
+
+ opts := TSDBOpts[*MockTSTable, any]{
+ Location: dir,
+ SegmentInterval: IntervalRule{Unit: DAY, Num: 1},
+ TTL: IntervalRule{Unit: DAY, Num: 7},
+ ShardNum: 1,
+ TSTableCreator: MockTSTableCreator,
+ }
+
+ ctx := context.Background()
+ mc := timestamp.NewMockClock()
+ ts, err := time.ParseInLocation("2006-01-02 15:04:05", "2024-05-01 00:00:00", time.Local)
+ require.NoError(t, err)
+ mc.Set(ts)
+ ctx = timestamp.SetClock(ctx, mc)
+
+ sc := NewServiceCache()
+ tsdb1, err := OpenTSDB(ctx, opts, sc, group)
+ require.NoError(t, err)
+ seg, segErr := tsdb1.CreateSegmentIfNotExist(ts)
+ require.NoError(t, segErr)
+ seg.DecRef()
+ require.NoError(t, tsdb1.Close())
+
+ halfBornDir := filepath.Join(dir, "seg-20240502")
+ lfs := fs.NewLocalFileSystem()
+ lfs.MkdirIfNotExist(halfBornDir, DirPerm)
+ require.NoFileExists(t, filepath.Join(halfBornDir, metadataFilename),
+ "half-born segment must have no metadata file to reproduce the bug")
+
+ tsdb2, err := OpenTSDB(ctx, opts, sc, group)
+ require.NoError(t, err, "OpenTSDB must not fail on a half-born segment")
+ require.NotNil(t, tsdb2)
+ defer tsdb2.Close()
+
+ require.NoDirExists(t, halfBornDir, "half-born segment should be removed on open")
+
+ db := tsdb2.(*database[*MockTSTable, any])
+ segs, _ := db.segmentController.segments(false)
+ require.Equal(t, 1, len(segs), "only the valid segment should remain after cleanup")
+ for i := range segs {
+ segs[i].DecRef()
+ }
+}
+
func TestTSDBCollect(t *testing.T) {
logger.Init(logger.Logging{
Env: "dev",
diff --git a/pkg/fs/error.go b/pkg/fs/error.go
index 8247d9e36..55ae695e9 100644
--- a/pkg/fs/error.go
+++ b/pkg/fs/error.go
@@ -18,7 +18,10 @@
// Package fs (file system) is an independent component to operate file and directory.
package fs
-import "fmt"
+import (
+ "fmt"
+ iofs "io/fs"
+)
// FileSystemError code.
const (
@@ -44,3 +47,20 @@ type FileSystemError struct {
func (err *FileSystemError) Error() string {
return fmt.Sprintf("File system return error: %s, error code: %d", err.Message, err.Code)
}
+
+// Is reports whether the error matches one of the standard io/fs sentinel
+// errors, so callers can use errors.Is with iofs.ErrNotExist, iofs.ErrExist
+// and iofs.ErrPermission regardless of the underlying OS error wrapping.
+// The target is compared directly (no recursive errors.Is) to mirror the
+// convention used by syscall.Errno.Is in the standard library.
+func (err *FileSystemError) Is(target error) bool {
+ switch target {
+ case iofs.ErrNotExist:
+ return err.Code == IsNotExistError
+ case iofs.ErrExist:
+ return err.Code == isExistError
+ case iofs.ErrPermission:
+ return err.Code == permissionError
+ }
+ return false
+}
diff --git a/pkg/fs/error_test.go b/pkg/fs/error_test.go
new file mode 100644
index 000000000..797c84077
--- /dev/null
+++ b/pkg/fs/error_test.go
@@ -0,0 +1,63 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you under
+// the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package fs
+
+import (
+ "errors"
+ "fmt"
+ iofs "io/fs"
+ "path/filepath"
+ "testing"
+)
+
+func TestFileSystemErrorIsNotExist(t *testing.T) {
+ lfs := NewLocalFileSystem()
+ missing := filepath.Join(t.TempDir(), "does-not-exist")
+ _, readErr := lfs.Read(missing)
+ if readErr == nil {
+ t.Fatalf("expected error reading missing file, got nil")
+ }
+ if !errors.Is(readErr, iofs.ErrNotExist) {
+ t.Fatalf("expected errors.Is(err, iofs.ErrNotExist) to be true for %T: %v", readErr, readErr)
+ }
+}
+
+func TestFileSystemErrorIsMatrix(t *testing.T) {
+ tests := []struct {
+ target error
+ name string
+ code int
+ want bool
+ }{
+ {name: "not-exist-matches", code: IsNotExistError, target: iofs.ErrNotExist, want: true},
+ {name: "exist-matches", code: isExistError, target: iofs.ErrExist, want: true},
+ {name: "permission-matches", code: permissionError, target: iofs.ErrPermission, want: true},
+ {name: "not-exist-vs-exist", code: IsNotExistError, target: iofs.ErrExist, want: false},
+ {name: "read-vs-not-exist", code: readError, target: iofs.ErrNotExist, want: false},
+ {name: "other-vs-permission", code: otherError, target: iofs.ErrPermission, want: false},
+ {name: "wrapped-not-exist-does-not-match", code: IsNotExistError, target: fmt.Errorf("wrapped: %w", iofs.ErrNotExist), want: false},
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ err := &FileSystemError{Code: tc.code, Message: "test"}
+ if got := errors.Is(err, tc.target); got != tc.want {
+ t.Fatalf("errors.Is(code=%d, %v) = %v, want %v", tc.code, tc.target, got, tc.want)
+ }
+ })
+ }
+}