test: expose stale snapshot publication after owner readmission

This commit is contained in:
pingqiu committed 2026-12-01 18:48:40 -08:00
1 parent 6add466034
commit c60d9421eb
4 files changed
+140 -16

No files matched your search

+1 -1
View File
@@ -248,7 +248,7 @@ func chunkSnapshot(args []string, stdout, stderr io.Writer) int {
fs.BoolVar(&pin, "pin", false, "shorthand for --class pinned")
fs.BoolVar(&noWait, "no-wait", false, "return the durable snapshot operation handle without observing completion")
fs.Var(labels, "label", "key=value label (repeatable)")
fs.DurationVar(&timeout, "timeout", 60*time.Second, chunkTimeoutHelp)
fs.DurationVar(&timeout, "timeout", 60*time.Second, "admission and individual observation request bound; asynchronous observation has no overall deadline; interrupt or --no-wait preserves the operation ID for resume")
if err := fs.Parse(args); err != nil {
return chunkFail(chunkName(fs), stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false)
}
@@ -81,6 +81,64 @@ func TestSnapshotOperationStallReadmissionFencesOldOwner(t *testing.T) {
}
}
// This deterministic seam witness pauses publication after its receipt guard.
// Advancing the receipt's owner must invalidate that already-checked HEAD CAS.
func TestSnapshotOperationReadmissionInvalidatesCheckedPublication(t *testing.T) {
for _, readmit := range []bool{false, true} {
t.Run(map[bool]string{false: "unchanged-owner-control", true: "readmitted-owner"}[readmit], func(t *testing.T) {
ctx := context.Background()
_, _, st, _ := cleanFixture(t)
op, _, err := st.CreateSnapshotOperation(ctx, "publication-race", "create", "s", strings.Repeat("a", 64))
if err != nil {
t.Fatal(err)
}
op, err = st.AdvanceSnapshotOperation(ctx, op, "running", nil, "")
if err != nil {
t.Fatal(err)
}
h, found, err := st.ReadHead(ctx)
if err != nil || !found {
t.Fatal(err)
}
raw, err := h.Encode()
if err != nil {
t.Fatal(err)
}
next, err := layout.DecodeHead(raw, h.Gen, h.VolumeID)
if err != nil {
t.Fatal(err)
}
next.Version = 1
sn := next.Snapshots["s"]
sn.Meta.Operation = &layout.SnapshotOperationBinding{ID: op.ID, Instance: op.Instance, Owner: op.Owner, Generation: h.Gen + 1, Epoch: h.Epoch}
next.Snapshots["s"] = sn
if err := st.GuardSnapshotOperations(ctx, h, next); err != nil {
t.Fatal(err)
}
if readmit {
stalled, err := st.AdvanceSnapshotOperation(ctx, op, "stalled", nil, "")
if err != nil {
t.Fatal(err)
}
fresh, err := st.AdvanceSnapshotOperation(ctx, stalled, "running", nil, "")
if err != nil {
t.Fatal(err)
}
if fresh.Owner == op.Owner {
t.Fatal("fixture failed to change owner")
}
}
err = st.UpdateHead(ctx, h.Gen, next, layout.CauseCommit)
if !readmit && err != nil {
t.Fatalf("unchanged owner failed publication: %v", err)
}
if readmit && err == nil {
t.Fatal("revoked owner published after readmission acknowledged a new owner")
}
})
}
}
func TestSnapshotOperationBindingCanonicalRoundTrip(t *testing.T) {
ctx := context.Background()
_, _, s, _ := cleanFixture(t)
@@ -11,11 +11,43 @@ import (
"github.com/seaweedfs/seaweed-block/core/storage"
"net/http"
"net/http/httptest"
"reflect"
"sync/atomic"
"testing"
"time"
)
func TestSnapshotOperationChangedIntentHTTPConflictBeforeWrite(t *testing.T) {
ctx := context.Background()
dp, f, _, head := limitVolume(t)
ctl := NewChunkControl(dp, f.Catalog, f.Ref)
if _, err := ctl.Snapshot(ctx, "v1", ChunkSnapshotRequest{Name: "daily", RequestID: "http-conflict"}); err != nil {
t.Fatal(err)
}
server := chunkServer(t, ctl, true)
t.Cleanup(func() { _ = server.Close(context.Background()) })
before := head()
r := httptest.NewRequest(http.MethodPost, "/chunk/snapshot", bytes.NewBufferString(`{"volumeID":"v1","name":"other","requestID":"http-conflict"}`))
r.RemoteAddr = "127.0.0.1:4444"
r.Header.Set("Prefer", "respond-async")
w := httptest.NewRecorder()
server.chunkMux().ServeHTTP(w, r)
var answer struct {
Code string `json:"code"`
Retryable bool `json:"retryable"`
}
if err := json.Unmarshal(w.Body.Bytes(), &answer); err != nil {
t.Fatal(err)
}
if w.Code != http.StatusConflict || answer.Code != "operation_conflict" || answer.Retryable {
t.Fatalf("intent refusal: %d %s", w.Code, w.Body.String())
}
after := head()
if !reflect.DeepEqual(before, after) {
t.Fatal("changed intent wrote HEAD")
}
}
func TestSnapshotOperationAsyncAdmissionOwnsWorkerAndPollIsReadOnly(t *testing.T) {
ctx := context.Background()
dp, f, s, head := limitVolume(t)
@@ -275,19 +307,49 @@ func TestSnapshotOperationControlReplacementJoinsAndCannotReopenStoppedOwner(t *
}
}
func TestSnapshotOperationCloseAlsoJoinsLegacySynchronousRequest(t *testing.T){
ctx:=context.Background();dp,f,s,_:=limitVolume(t)
if _,err:=s.Write(0,bytes.Repeat([]byte{'A'},4096));err!=nil{t.Fatal(err)}
entered:=make(chan struct{},1)
f.Blobs.PutHook=func(ctx context.Context)error{entered<-struct{}{};<-ctx.Done();return ctx.Err()}
ctl:=NewChunkControl(dp,f.Catalog,f.Ref).(*chunkControl)
caller,cancel:=context.WithCancel(ctx)
done:=make(chan error,1)
go func(){_,err:=ctl.Snapshot(caller,"v1",ChunkSnapshotRequest{Name:"daily",RequestID:"legacy-borrow"});done<-err}()
joined:=false
defer func(){cancel();if !joined{select{case <-done:case <-time.After(3*time.Second):t.Error("legacy caller did not join")}}}()
select{case <-entered:case <-time.After(time.Second):t.Fatal("legacy request never borrowed storage")}
closing,stop:=context.WithTimeout(ctx,3*time.Second);defer stop()
if err:=ctl.closeSnapshotOperations(closing);err!=nil{t.Fatal(err)}
select{case err:=<-done:joined=true;if err==nil{t.Fatal("canceled legacy attempt answered success")};case <-time.After(time.Second):t.Fatal("close returned while a synchronous request still borrowed storage")}
func TestSnapshotOperationCloseAlsoJoinsLegacySynchronousRequest(t *testing.T) {
ctx := context.Background()
dp, f, s, _ := limitVolume(t)
if _, err := s.Write(0, bytes.Repeat([]byte{'A'}, 4096)); err != nil {
t.Fatal(err)
}
entered := make(chan struct{}, 1)
f.Blobs.PutHook = func(ctx context.Context) error { entered <- struct{}{}; <-ctx.Done(); return ctx.Err() }
ctl := NewChunkControl(dp, f.Catalog, f.Ref).(*chunkControl)
caller, cancel := context.WithCancel(ctx)
done := make(chan error, 1)
go func() {
_, err := ctl.Snapshot(caller, "v1", ChunkSnapshotRequest{Name: "daily", RequestID: "legacy-borrow"})
done <- err
}()
joined := false
defer func() {
cancel()
if !joined {
select {
case <-done:
case <-time.After(3 * time.Second):
t.Error("legacy caller did not join")
}
}
}()
select {
case <-entered:
case <-time.After(time.Second):
t.Fatal("legacy request never borrowed storage")
}
closing, stop := context.WithTimeout(ctx, 3*time.Second)
defer stop()
if err := ctl.closeSnapshotOperations(closing); err != nil {
t.Fatal(err)
}
select {
case err := <-done:
joined = true
if err == nil {
t.Fatal("canceled legacy attempt answered success")
}
case <-time.After(time.Second):
t.Fatal("close returned while a synchronous request still borrowed storage")
}
}
+4
View File
@@ -419,6 +419,10 @@ A snapshot delete can answer `head_conflict` (HTTP 409, retryable) too: another
holder's cleaning beginning, landed between the delete's read of HEAD and its write. Nothing of the delete applied;
retry it (`TestSnapshotDeleteRacedByACleanBeginIsAHeadConflict`).
On development builds with snapshot operations, the CLI prints an operation ID before admission. Keep that ID and pass `--request-id` on retries, including after interruption; `--no-wait` returns the typed handle without polling. After asynchronous admission, `--timeout` bounds individual calls, not total observation time. Interrupting observation does not cancel server-owned work. A retained operation answers its original outcome even after the snapshot name is removed or reused. The bounded-retirement contract and asynchronous delete remain under development; this is not a release-branch guarantee.
The following request-timeout behavior describes released synchronous servers.
A snapshot's time grows with the data changed since the volume's last cut. `sw-block ops chunk snapshot` waits 60 s by default, so pass `--timeout` above the expected time; the
server bounds one request at 5 minutes whatever `--timeout` says. A request that runs
out of time answers a retryable code: `remote_unavailable` or `deadline_exceeded` when it ran out before its commit,