diff --git a/weed/filer/filer_implicit_dir_test.go b/weed/filer/filer_implicit_dir_test.go index c6824b86f6..61c53545d6 100644 --- a/weed/filer/filer_implicit_dir_test.go +++ b/weed/filer/filer_implicit_dir_test.go @@ -4,8 +4,11 @@ import ( "context" "errors" "os" + "runtime" + "strings" "sync/atomic" "testing" + "time" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" @@ -14,23 +17,41 @@ import ( "github.com/stretchr/testify/require" ) -// missHookStore runs onMiss once, outside the store lock, right after a lookup -// of path finds nothing. +// missHookStore runs onMiss, outside the store lock, right after the miss-th +// lookup of path that finds nothing. type missHookStore struct { *stubFilerStore path util.FullPath - fired atomic.Bool + miss int32 + misses atomic.Int32 onMiss func() } func (s *missHookStore) FindEntry(ctx context.Context, p util.FullPath) (*Entry, error) { e, err := s.stubFilerStore.FindEntry(ctx, p) - if p == s.path && errors.Is(err, filer_pb.ErrNotFound) && s.fired.CompareAndSwap(false, true) { + if p == s.path && errors.Is(err, filer_pb.ErrNotFound) && s.misses.Add(1) == s.miss { s.onMiss() } return e, err } +func directoryMarker(p util.FullPath) *Entry { + return &Entry{ + FullPath: p, + Attr: Attr{Mode: os.ModeDir | 0o755, Mime: s3_constants.FolderMimeType}, + Extended: map[string][]byte{s3_constants.ExtETagKey: []byte("d41d8cd98f00b204e9800998ecf8427e")}, + } +} + +func assertDirectoryMarker(t *testing.T, store *stubFilerStore, p util.FullPath) { + t.Helper() + dir, err := store.FindEntry(context.Background(), p) + require.NoError(t, err) + assert.True(t, dir.IsDirectory()) + assert.Equal(t, s3_constants.FolderMimeType, dir.Attr.Mime, "the implicit parent replaced the directory created meanwhile") + assert.Contains(t, dir.Extended, s3_constants.ExtETagKey) +} + // A directory created while a nested create is filling in that directory as // a missing parent keeps what it was created with: the parent row is only // written where none exists. @@ -39,23 +60,60 @@ func TestImplicitParentKeepsConcurrentDirectory(t *testing.T) { ctx := context.Background() dirPath := util.FullPath("/buckets/bkt/markers/dir") - store := &missHookStore{stubFilerStore: stub, path: dirPath} + store := &missHookStore{stubFilerStore: stub, path: dirPath, miss: 1} f.Store = NewFilerStoreWrapper(store) store.onMiss = func() { - marker := &Entry{ - FullPath: dirPath, - Attr: Attr{Mode: os.ModeDir | 0o755, Mime: s3_constants.FolderMimeType}, - Extended: map[string][]byte{s3_constants.ExtETagKey: []byte("d41d8cd98f00b204e9800998ecf8427e")}, - } - require.NoError(t, f.CreateEntry(ctx, marker, nil, false, false, nil, false, f.MaxFilenameLength)) + require.NoError(t, f.CreateEntry(ctx, directoryMarker(dirPath), nil, false, false, nil, false, f.MaxFilenameLength)) } child := &Entry{FullPath: dirPath.Child("child"), Attr: Attr{Mode: 0o644}} require.NoError(t, f.CreateEntry(ctx, child, nil, false, false, nil, false, f.MaxFilenameLength)) - dir, err := stub.FindEntry(ctx, dirPath) - require.NoError(t, err) - assert.True(t, dir.IsDirectory()) - assert.Equal(t, s3_constants.FolderMimeType, dir.Attr.Mime, "the implicit parent replaced the directory created meanwhile") - assert.Contains(t, dir.Extended, s3_constants.ExtETagKey) + assertDirectoryMarker(t, stub, dirPath) +} + +// A directory create arriving after the parent's re-check, before its insert, +// waits for that insert and then updates the row rather than being replaced. +func TestDirectoryCreateWaitsForImplicitParentInsert(t *testing.T) { + f, stub := newTestFilerWithStubStore(t) + ctx := context.Background() + dirPath := util.FullPath("/buckets/bkt/markers/dir") + + markerDone := make(chan error, 1) + store := &missHookStore{stubFilerStore: stub, path: dirPath, miss: 2} + f.Store = NewFilerStoreWrapper(store) + store.onMiss = func() { + go func() { + markerDone <- f.CreateEntry(ctx, directoryMarker(dirPath), nil, false, false, nil, false, f.MaxFilenameLength) + }() + waitForDirectoryCreateWaiter(t, markerDone) + } + + child := &Entry{FullPath: dirPath.Child("child"), Attr: Attr{Mode: 0o644}} + require.NoError(t, f.CreateEntry(ctx, child, nil, false, false, nil, false, f.MaxFilenameLength)) + require.GreaterOrEqual(t, store.misses.Load(), int32(2), "the parent was inserted without a re-check") + require.NoError(t, <-markerDone) + + assertDirectoryMarker(t, stub, dirPath) +} + +// waitForDirectoryCreateWaiter returns once a goroutine waits in +// lockDirectoryCreate, or once the create has finished without waiting. +func waitForDirectoryCreateWaiter(t *testing.T, done chan error) { + t.Helper() + buf := make([]byte, 1<<20) + for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(time.Millisecond) { + select { + case err := <-done: + done <- err + return + default: + } + for _, g := range strings.Split(string(buf[:runtime.Stack(buf, true)]), "\n\n") { + if strings.Contains(g, "sync.(*Cond).Wait") && strings.Contains(g, "lockDirectoryCreate") { + return + } + } + } + t.Fatal("the directory create neither waited nor finished") }