mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
test: delete a signal-stopped sealed volume after its lease horizon
This commit is contained in:
1 parent
c73bedfd37
commit
606da5a1a7
1 file changed
+142
-23
@@ -3,7 +3,9 @@
|
||||
package main_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -22,6 +24,113 @@ func (env *handoffEnv) volumeDelete(t *testing.T, request string) (string, strin
|
||||
"--master", env.h.masterAddr, "--request-id", request)
|
||||
}
|
||||
|
||||
// deletePastPrintedHorizon follows volume-lifecycle steps 2-4. The first
|
||||
// refusal proves the lease is still live; the timestamp only schedules
|
||||
// retries. The CLI verdict decides success. All retries keep the same id and
|
||||
// the first T+5s deadline, even if another lease appears (which fails the test).
|
||||
func (env *handoffEnv) deletePastPrintedHorizon(t *testing.T, volume, request string, requireLive bool) (string, string, int) {
|
||||
t.Helper()
|
||||
call := func(ctx context.Context) (string, string, int) {
|
||||
out, errOut, code, err := runCLI(ctx, env.h, "ops", "chunk", "volume-delete", "--volume", volume,
|
||||
"--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath,
|
||||
"--master", env.h.masterAddr, "--request-id", request)
|
||||
if err != nil {
|
||||
t.Fatalf("volume-delete %s: %v", volume, err)
|
||||
}
|
||||
return out, errOut, code
|
||||
}
|
||||
first, stop := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer stop()
|
||||
out, errOut, code := call(first)
|
||||
until, err := deleteLeaseHorizon(out, code)
|
||||
if err != nil {
|
||||
if requireLive {
|
||||
t.Fatalf("first delete must refuse the live lease: %v; %s %s", err, out, errOut)
|
||||
}
|
||||
return out, errOut, code
|
||||
}
|
||||
if wait := time.Until(until); wait < 0 || wait > 90*time.Second {
|
||||
t.Fatalf("first refusal horizon must be upcoming and within 90s, got %s: %s", until, out)
|
||||
}
|
||||
ctx, cancel := context.WithDeadline(context.Background(), until.Add(5*time.Second))
|
||||
defer cancel()
|
||||
timer := time.NewTimer(time.Until(until))
|
||||
defer timer.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
t.Fatalf("delete did not resolve by original horizon + 5s: %s %s", out, errOut)
|
||||
case <-timer.C:
|
||||
}
|
||||
out, errOut, code = call(ctx)
|
||||
next, err := deleteLeaseHorizon(out, code)
|
||||
if err != nil {
|
||||
return out, errOut, code
|
||||
}
|
||||
if !next.Equal(until) {
|
||||
t.Fatalf("lease horizon changed from %s to %s; refusing to widen deadline", until, next)
|
||||
}
|
||||
timer.Reset(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// deleteLeaseHorizon accepts only the documented typed CLI refusal, never
|
||||
// a matching stderr fragment or a successful exit with an error-shaped body.
|
||||
func deleteLeaseHorizon(out string, code int) (time.Time, error) {
|
||||
var body struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
if code != 1 {
|
||||
return time.Time{}, fmt.Errorf("exit %d is not refusal", code)
|
||||
}
|
||||
if err := json.Unmarshal([]byte(out), &body); err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
if body.Code != "not_admitted" {
|
||||
return time.Time{}, fmt.Errorf("code %q is not not_admitted", body.Code)
|
||||
}
|
||||
_, rest, ok := strings.Cut(body.Message, "live until ")
|
||||
if !ok {
|
||||
return time.Time{}, fmt.Errorf("missing lease horizon")
|
||||
}
|
||||
fields := strings.Fields(rest)
|
||||
if len(fields) == 0 {
|
||||
return time.Time{}, fmt.Errorf("empty lease horizon")
|
||||
}
|
||||
return time.Parse(time.RFC3339Nano, strings.TrimSuffix(fields[0], ":"))
|
||||
}
|
||||
|
||||
// The CLI error body, rather than a substring in combined output, is the
|
||||
// authority for retry timing. Every refusal class stays distinguishable.
|
||||
func TestDeleteLeaseHorizonRequiresTypedRefusal(t *testing.T) {
|
||||
const valid = `{"code":"not_admitted","message":"workspace delete: live until 2026-11-26T13:00:00.125Z: lease expiry plus clock-skew margin has not elapsed"}`
|
||||
for _, tc := range []struct {
|
||||
name, out string
|
||||
code int
|
||||
ok bool
|
||||
}{
|
||||
{"typed horizon", valid, 1, true},
|
||||
{"successful exit cannot refuse", valid, 0, false},
|
||||
{"usage failure", valid, 2, false},
|
||||
{"plain stderr is not evidence", "not_admitted: live until 2026-11-26T13:00:00.125Z", 1, false},
|
||||
{"dependent refusal", `{"code":"snapshot_has_dependents","message":"snapshot s1"}`, 1, false},
|
||||
{"uncertain lease", `{"code":"not_admitted","message":"restart required"}`, 1, false},
|
||||
{"missing time", `{"code":"not_admitted","message":"live until "}`, 1, false},
|
||||
{"invalid time", `{"code":"not_admitted","message":"live until tomorrow: refused"}`, 1, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
until, err := deleteLeaseHorizon(tc.out, tc.code)
|
||||
if (err == nil) != tc.ok {
|
||||
t.Fatalf("horizon %s error %v, want valid=%v", until, err, tc.ok)
|
||||
}
|
||||
if tc.ok && until.Format(time.RFC3339Nano) != "2026-11-26T13:00:00.125Z" {
|
||||
t.Fatalf("wrong horizon %s", until)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// requireDeleteCompleted checks step 4's documented state:"deleted" result
|
||||
// (volume-lifecycle.md, "Delete a volume", step 4). Retries use the same
|
||||
// request id and must complete again; the CLI reports no replayed flag.
|
||||
@@ -96,40 +205,50 @@ func TestProcedureDeleteAVolume(t *testing.T) {
|
||||
env.requireDeletedForGood(t, "del-v1")
|
||||
}
|
||||
|
||||
// TestProcedureDeleteAfterASignalStop: the page's recovery. The volume is
|
||||
// sealed and blockvolume is then stopped with SIGTERM, so the ACK is
|
||||
// skipped: volume-delete is refused (live authority requires a clean
|
||||
// revocation ACK) and nothing is deleted. blockvolume is started again with
|
||||
// the same flags and local root; it is revoked and sends the ACK (exit 0 by
|
||||
// itself); volume-delete then completes, HEAD is deleted with no chunks, and
|
||||
// a rerun with the same request id completes again.
|
||||
// TestProcedureDeleteAfterASignalStop: a sealed holder stopped by SIGTERM
|
||||
// stays down. The first delete refuses the live lease and changes nothing;
|
||||
// the same request completes after its printed horizon without a restart or
|
||||
// ACK. A retry completes again, and a late start cannot claim or serve.
|
||||
func TestProcedureDeleteAfterASignalStop(t *testing.T) {
|
||||
env := startHandoffEnv(t, 1)
|
||||
holder := env.start(t, "blockvolume-s1", "s1")
|
||||
e := env.adopt(t, holder, "s1")
|
||||
env.writeBlock(t, "s1", 0, pattern("delete-after-sig"))
|
||||
|
||||
g := env.seal(t, "s1", e)
|
||||
env.seal(t, "s1", e)
|
||||
holder.term(t, 30*time.Second)
|
||||
stopped := func() {
|
||||
t.Helper()
|
||||
select {
|
||||
case <-holder.done:
|
||||
default:
|
||||
t.Fatal("signal-stopped holder is still running")
|
||||
}
|
||||
if listening(env.hosts["s1"].status) {
|
||||
t.Fatal("signal-stopped holder listened again before deletion")
|
||||
}
|
||||
}
|
||||
stopped()
|
||||
state0, n0 := env.headShown(t)
|
||||
out, errOut, code := env.volumeDelete(t, "del-v1")
|
||||
if code == 0 || !strings.Contains(out+errOut, "live authority requires clean revocation ACK") {
|
||||
t.Fatalf("volume-delete after a signal stop: exit %d %s %s; want refused for the missing ACK", code, out, errOut)
|
||||
if _, err := deleteLeaseHorizon(out, code); err != nil {
|
||||
t.Fatalf("volume-delete after signal stop: exit %d %s %s; want typed live-until refusal: %v", code, out, errOut, err)
|
||||
}
|
||||
if state, n := env.headShown(t); state != "detached" || state != state0 || n != n0 || n == 0 {
|
||||
t.Fatalf("after the refused delete HEAD is %q with %d chunks; want it as before (%q, %d chunks): nothing deleted", state, n, state0, n0)
|
||||
t.Fatalf("refused delete changed HEAD/pool: %q %d; before %q %d", state, n, state0, n0)
|
||||
}
|
||||
|
||||
again := env.start(t, "blockvolume-s1-again", "s1")
|
||||
poll(t, 60*time.Second, "the restarted holder never listened on its status address", func() bool {
|
||||
return listening(env.hosts["s1"].status)
|
||||
})
|
||||
env.revoke(t, e, g)
|
||||
if err := again.wait(60 * time.Second); err != nil {
|
||||
t.Fatalf("the restarted s1 after the revocation: %v; want a clean exit by itself", err)
|
||||
}
|
||||
env.waitAck(t, e, g)
|
||||
out, errOut, code = env.volumeDelete(t, "del-v1")
|
||||
stopped()
|
||||
out, errOut, code = env.deletePastPrintedHorizon(t, "v1", "del-v1", true)
|
||||
requireDeleteCompleted(t, out, errOut, code)
|
||||
stopped()
|
||||
env.requireDeletedForGood(t, "del-v1")
|
||||
late := env.start(t, "blockvolume-s1-late", "s1")
|
||||
err := late.wait(60 * time.Second)
|
||||
served := late.has("blockvolume: backend admits I/O: starting the frontends") || volumeFrontendReady(env.hosts["s1"].status, "v1")
|
||||
keepFullLog(t, late)
|
||||
if err == nil || strings.Contains(fmt.Sprint(err), "still running") || served {
|
||||
t.Fatalf("late start after deletion must exit without serving: exit=%v served=%v", err, served)
|
||||
}
|
||||
if state, n := env.headShown(t); state != "deleted" || n != 0 {
|
||||
t.Fatalf("late start changed deleted HEAD/pool: %q %d", state, n)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user