mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-07 04:27:49 +01:00
* volume: a delete cancels the writes of its needle still in flight A write queued behind the volume locks, for example behind another write's fsync on a busy disk, is not in the needle map yet. A delete of that needle in the meantime finds nothing, and the write lands afterwards, so the needle comes back on this copy only while the other replicas have it deleted. Each volume now tracks its writes from entry until they return. A delete cancels the ones for its needle, and a cancelled write that landed is tombstoned before it returns, unless the needle was deleted or rewritten since. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: a replicate delete that finds nothing cancels the pending write Rolling back a failed chunk upload sends DELETE ?type=replicate to every holder, including the slow one whose write timed out on the client. That holder has not indexed the write yet, so the delete answered 404 and the write landed live afterwards. Cancel the needle's pending writes, then look again in case one finished in between. A plain delete that finds nothing still answers 404 without cancelling: it is not sent on to the replicas. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * seaweed-volume: a delete cancels the writes of its needle still in flight Mirrors the Go volume. Volume::track_pending_write registers a write until its guard drops, delete_needle cancels the needle's pending writes, and settle_pending_write tombstones a cancelled write that landed unless the needle was deleted or rewritten since. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * seaweed-volume: track uploads and cancel them on a replicate delete An upload is tracked from the handler, and a durable one from submission to the write queue, where the guard travels with the request: a handler dropped while the write waits in the queue does not untrack it, and the worker settles it after the batch. A replicate delete that finds nothing cancels the needle's pending writes and looks again, like the Go handler. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: take a cancelled write back in the lock span that publishes it The write tombstoned itself only after it returned, with the volume locks released. In that gap a compaction could move the record so the offset check skipped it, a close could drop it, and an identical write sent after the delete could match the live record and report success before the tombstone removed it. The check now runs right after the publish, under the same dataFileSyncLock and dataFileAccessLock hold, in the plain, fsync and batch write paths. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: a delete cancels only the writes carrying its cookie A delete with the right needle id but the wrong cookie could cancel an in-flight write and get it tombstoned. Each pending write now records its cookie, and a cancel only marks the matching ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * seaweed-volume: take a cancelled write back in the lock span that publishes it Mirror the Go change: settle_pending_write ran after write_needles_grouped returned, so a compaction, a close or an identical write later in the same batch could slip in before the tombstone. drop_cancelled_write now runs right after the publish, under the same sync and access lock hold, in the synced run, the unsynced run and write_needle_unsynced_or_refused. write_pending_needles carries each write's PendingWrite down to that point. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * seaweed-volume: a delete cancels only the writes carrying its cookie Mirror the Go change: a delete with the right needle id and the wrong cookie no longer cancels an in-flight write. Each tracked write keeps its cookie next to its cancel flag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * seaweed-volume: refuse a queued write whose volume was remounted The write queue tracks a write on the volume found at submit, but the worker looks the volume up again by id. After a remount, a delete reaches the new volume and cannot cancel the write registered on the old one, so the worker now refuses such a write instead of landing it on the new volume. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * seaweed-volume: decode the delete's type parameter like Go's FormValue The delete handler matched the raw query text for type=replicate, so a percent-encoded name or value was not treated as a replica delete and did not cancel the write it undoes. Parse the query the way the upload handler does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: test a replica delete landing in a write's fsync window Drive DeleteHandler while a durable write is parked in its .dat fsync, appended but not indexed: the replica delete answers 404 and the write is taken back when it publishes, while a plain delete leaves it live. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: flush a take-back tombstone before the index row that points at it A durable write taken back during its fsync appends the tombstone after the .dat sync, and the .idx sync that follows made its row durable while the record itself could still be lost, which reloads the volume read only. The fsync and batch paths now sync the .dat again first when a take-back appended a tombstone; a failure stops the volume taking writes like a failed index sync. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * seaweed-volume: flush a take-back tombstone before the index row that points at it A durable write taken back in write_synced_run appends its tombstone after the .dat sync, so the .idx flush that follows could make the row durable while the tombstone record itself was still in the page cache. The run now syncs the .dat again first when a take-back appended a tombstone; a failure stops the volume taking writes like a failed index flush. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: a skip-cookie batch delete cancels the writes of the file id it names BatchDelete with SkipCookieCheck left the needle's cookie at zero, so the delete's cancel matched none of the in-flight writes and a retry queued behind it could land afterwards. The non-EC delete now carries the file id's cookie, as the Rust volume server already does; the EC path keeps cookie 0, which DeleteEcShardNeedle reads as "skip the check". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: release a held test write before the store closes A failed wait ended the test before release ran, so the writer stayed parked in its fsync and the store cleanup blocked on its lock until the package timed out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
4.5 KiB
Go
142 lines
4.5 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"runtime"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/backend"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// heldSyncBackend parks the first .dat fsync until released: the window where
|
|
// a durable write is appended but not indexed yet.
|
|
type heldSyncBackend struct {
|
|
backend.BackendStorageFile
|
|
once sync.Once
|
|
entered chan struct{}
|
|
release chan struct{}
|
|
}
|
|
|
|
func (b *heldSyncBackend) Sync() error {
|
|
b.once.Do(func() {
|
|
close(b.entered)
|
|
<-b.release
|
|
})
|
|
return b.BackendStorageFile.Sync()
|
|
}
|
|
|
|
// startHeldWrite starts a durable write of needle 1 on a fresh volume and
|
|
// returns once it is parked in its .dat fsync, appended but not indexed.
|
|
func startHeldWrite(t *testing.T) (store *storage.Store, fid string, release func(), written <-chan error) {
|
|
store = storage.NewStore(nil, "localhost", 8080, 18080, "http://localhost:8080", "store-id",
|
|
[]string{t.TempDir()}, []int32{10}, []util.MinFreeSpace{{}}, "", storage.NeedleMapInMemory,
|
|
[]types.DiskType{types.HardDriveType}, nil, 0, stats.DefaultDiskIOProbeConfig())
|
|
t.Cleanup(store.Close)
|
|
require.NoError(t, store.AddVolume(1, "", storage.NeedleMapInMemory, "000", "", 0, needle.Version3, 0, types.HardDriveType, 0))
|
|
v := store.GetVolume(1)
|
|
held := &heldSyncBackend{BackendStorageFile: v.DataBackend, entered: make(chan struct{}), release: make(chan struct{})}
|
|
v.DataBackend = held
|
|
|
|
n := &needle.Needle{Id: 1, Cookie: 0x1234, Data: []byte("late write")}
|
|
n.Checksum = needle.NewCRC(n.Data)
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
_, err := store.WriteVolumeNeedle(1, n, true, true)
|
|
done <- err
|
|
}()
|
|
<-held.entered
|
|
var releaseOnce sync.Once
|
|
release = func() { releaseOnce.Do(func() { close(held.release) }) }
|
|
t.Cleanup(release)
|
|
return store, needle.NewFileIdFromNeedle(1, n).String(), release, done
|
|
}
|
|
|
|
func requireTakenBack(t *testing.T, store *storage.Store, written <-chan error, takenBack bool) {
|
|
t.Helper()
|
|
err := <-written
|
|
_, readErr := store.ReadVolumeNeedle(1, &needle.Needle{Id: 1}, nil, nil)
|
|
if takenBack {
|
|
require.ErrorIs(t, err, storage.ErrorDeleted)
|
|
require.ErrorIs(t, readErr, storage.ErrorDeleted)
|
|
} else {
|
|
require.NoError(t, err)
|
|
require.NoError(t, readErr)
|
|
}
|
|
}
|
|
|
|
// A replica delete that arrives while the write it undoes is in its fsync
|
|
// finds nothing, yet the write must not land live afterwards. A plain delete
|
|
// that finds nothing is not sent on to the replicas, so it leaves the write.
|
|
func TestDeleteHandlerCancelsAWriteInItsFsyncWindow(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
query string
|
|
live bool
|
|
}{
|
|
{"?type=replicate", false},
|
|
{"", true},
|
|
} {
|
|
t.Run(tc.query, func(t *testing.T) {
|
|
store, fid, release, written := startHeldWrite(t)
|
|
resp := httptest.NewRecorder()
|
|
vs := &VolumeServer{store: store, guard: security.NewGuard(nil, "", 0, "", 0)}
|
|
vs.DeleteHandler(resp, httptest.NewRequest(http.MethodDelete, "/"+fid+tc.query, nil))
|
|
require.Equal(t, http.StatusNotFound, resp.Code)
|
|
release()
|
|
requireTakenBack(t, store, written, !tc.live)
|
|
})
|
|
}
|
|
}
|
|
|
|
// A batch delete that skips the cookie check still cancels the in-flight
|
|
// writes of the file id it names, and only those.
|
|
func TestBatchDeleteCancelsAWriteInItsFsyncWindow(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
cookie string
|
|
takenBack bool
|
|
}{
|
|
{"", true},
|
|
{"00004321", false},
|
|
} {
|
|
t.Run(tc.cookie, func(t *testing.T) {
|
|
store, fid, release, written := startHeldWrite(t)
|
|
if tc.cookie != "" {
|
|
fid = fid[:len(fid)-8] + tc.cookie
|
|
}
|
|
deleted := make(chan error, 1)
|
|
go func() {
|
|
_, err := (&VolumeServer{store: store}).BatchDelete(context.Background(), &volume_server_pb.BatchDeleteRequest{
|
|
FileIds: []string{fid},
|
|
SkipCookieCheck: true,
|
|
})
|
|
deleted <- err
|
|
}()
|
|
// the delete waits behind the held write's sync lock
|
|
require.Eventually(t, func() bool {
|
|
buf := make([]byte, 1<<20)
|
|
return bytes.Contains(buf[:runtime.Stack(buf, true)], []byte("(*Volume).syncDelete"))
|
|
}, 10*time.Second, time.Millisecond)
|
|
release()
|
|
require.NoError(t, <-deleted)
|
|
err := <-written
|
|
if tc.takenBack {
|
|
require.ErrorIs(t, err, storage.ErrorDeleted)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|