From 2ac8db5cb4e8ab85299daae585a3daada1f9e437 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 19 Nov 2026 21:01:08 -0800 Subject: [PATCH] filer: lock the path for plain HTTP overwrites so replaced chunks get deleted (#12801) * filer: lock the path for plain HTTP overwrites CreateEntry looks up the entry it replaces and queues that entry's chunks for deletion. Two plain PUT/POST writes of one path could both read the same old entry, so the chunks of whichever write lost were never queued. Take the exclusive path lock around CreateEntry, as the gRPC CreateEntry already does. Co-Authored-By: Claude Opus 5.5 * filer: test concurrent HTTP overwrites queue every replaced chunk Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .../filer_server_handlers_write_autochunk.go | 5 ++ ...lers_write_autochunk_inline_append_test.go | 68 +++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/weed/server/filer_server_handlers_write_autochunk.go b/weed/server/filer_server_handlers_write_autochunk.go index f6df863eb2..f198ecd7f4 100644 --- a/weed/server/filer_server_handlers_write_autochunk.go +++ b/weed/server/filer_server_handlers_write_autochunk.go @@ -525,6 +525,11 @@ func (fs *FilerServer) saveMetaData(ctx context.Context, r *http.Request, fileNa replyerr = fmt.Errorf("entry %s changed during append; retry the request", path) return } + } else if fs.entryLockTable != nil { + // CreateEntry deletes the chunks of the entry it replaces, so concurrent + // overwrites must not both replace the same entry. + pathLock := fs.entryLockTable.AcquireLock("CreateEntry", util.FullPath(path), util.ExclusiveLock) + defer fs.entryLockTable.ReleaseLock(util.FullPath(path), pathLock) } dbErr := fs.filer.CreateEntry(context.WithoutCancel(ctx), entry, nil, false, false, nil, skipCheckParentDirEntry(r), so.MaxFileNameLength) diff --git a/weed/server/filer_server_handlers_write_autochunk_inline_append_test.go b/weed/server/filer_server_handlers_write_autochunk_inline_append_test.go index 21cb9358e6..a19280f8a1 100644 --- a/weed/server/filer_server_handlers_write_autochunk_inline_append_test.go +++ b/weed/server/filer_server_handlers_write_autochunk_inline_append_test.go @@ -976,6 +976,74 @@ func TestFilerInlineAppendConcurrentKeepsAllData(t *testing.T) { } } +func TestFilerConcurrentOverwritesQueueReplacedChunks(t *testing.T) { + ctx := context.Background() + const filePath = "/overwrite.txt" + store := newRenameTestStore() + f := newRenameTestFiler(t, store) + f.Store = filer.NewFilerStoreWrapper(&inlineAppendSlowFindStore{renameTestStore: store, delay: 2 * time.Millisecond}) + written := []string{"1,00000001"} + if err := store.InsertEntry(ctx, &filer.Entry{ + FullPath: util.FullPath(filePath), + Attr: filer.Attr{Mtime: time.Unix(1, 0), Crtime: time.Unix(1, 0), FileSize: 4}, + Chunks: []*filer_pb.FileChunk{{FileId: written[0], Size: 4}}, + }); err != nil { + t.Fatalf("insert chunk-backed entry: %v", err) + } + + fs := &FilerServer{ + filer: f, + option: &FilerOption{MaxMB: 1}, + entryLockTable: util.NewLockTable[util.FullPath](), + } + var wg sync.WaitGroup + errs := make(chan error, 8) + for i := 2; i < 10; i++ { + fileId := fmt.Sprintf("1,%08x", i) + written = append(written, fileId) + wg.Add(1) + go func() { + defer wg.Done() + r := httptest.NewRequest(http.MethodPut, filePath, strings.NewReader("data")) + _, err, _ := fs.saveMetaData(ctx, r, "overwrite.txt", "", &operation.StorageOption{}, 4, nil, []*filer_pb.FileChunk{{FileId: fileId, Size: 4}}, 4, nil) + errs <- err + }() + } + wg.Wait() + close(errs) + for err := range errs { + if err != nil { + t.Fatalf("concurrent overwrite failed: %v", err) + } + } + + final, err := f.FindEntry(ctx, util.FullPath(filePath)) + if err != nil { + t.Fatalf("find overwritten entry: %v", err) + } + if len(final.Chunks) != 1 { + t.Fatalf("final entry has %d chunks, want 1", len(final.Chunks)) + } + queued := make(map[string]bool) + for i := 0; i < 2; i++ { + f.FileIdDeletionQueue.Consume(func(fileIds []string) { + for _, fileId := range fileIds { + queued[fileId] = true + } + }) + } + live := final.Chunks[0].GetFileIdString() + for _, fileId := range written { + if fileId == live { + if queued[fileId] { + t.Errorf("live chunk %s was queued for deletion", fileId) + } + } else if !queued[fileId] { + t.Errorf("chunk %s is neither referenced by the entry nor queued for deletion", fileId) + } + } +} + type inlineAppendSlowFindStore struct { *renameTestStore delay time.Duration