Files
seaweedfs/seaweed-block/cmd/blockvolume/runtime_fence_exit_test.go
T
pingqiuandClaude Opus 5.5 83f8507d3b durable, blockvolume: review fold for the host lock and the rolled-back root
- The lock directory is created (0700) when missing; only a directory or
  lock file that cannot be created or opened refuses, with the cure
  (install the tmpfiles.d entry, or chown the directory and its lock files
  to the unit's user).
- The lock file is opened read-only (an existing file first, created 0644
  only when missing), so a lock file another user created stays lockable;
  it is never unlinked, and the next owner locks the same file.
- A stale-root startup end carries its cure into the final exit line:
  exiting status=78 reason=superseded cure="...".
- Tests: a cut's remote_lsn is locally durable at its HEAD CAS, and the
  root's crash image at the CAS reattaches and serves; the rolled-back copy
  makes no claim or PUT, admits no I/O, keeps its data and durable LSN; an
  exact copy at HEAD attaches; a read-only lock file is taken.
- Invariants: INV-WS-ONE-PROCESS-PER-HOST-001 and its M4 edge; the
  rollback row names its test file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 10:40:12 -08:00

87 lines
2.7 KiB
Go

package main
import (
"context"
"sync/atomic"
"testing"
"time"
)
type runtimeFenceStub struct {
fenced atomic.Bool
updates chan struct{}
}
func (s *runtimeFenceStub) ChunkRuntimeFenced(string) bool { return s.fenced.Load() }
func (s *runtimeFenceStub) ChunkRuntimeFenceUpdates() <-chan struct{} { return s.updates }
// runtimeFenceWait bounds every receive and join in this test.
const runtimeFenceWait = 5 * time.Second
// startRuntimeFenceWatch runs watchChunkRuntimeFence with a context it owns.
// Its cleanup, registered before the worker starts, cancels that context and
// joins the worker within runtimeFenceWait, so an earlier failed check never
// strands it. The returned channel closes when the worker returned.
func startRuntimeFenceWatch(t *testing.T, e *processExit, f flags, p chunkRuntimeFenceProvider) <-chan struct{} {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
t.Cleanup(func() {
cancel()
select {
case <-done:
case <-time.After(runtimeFenceWait):
t.Errorf("the runtime-fence watcher did not return within %s of its cancel", runtimeFenceWait)
}
})
go func() {
defer close(done)
watchChunkRuntimeFence(ctx, e, f, p)
}()
return done
}
// INV-FE-FENCE-EXIT-001: a runtime HEAD fence of a workspace chunk primary
// chooses the no-restart exit 78 head_fenced through the process owner's one
// chooser, and the watcher returns; nothing is chosen while not fenced, and a
// non-workspace volume never watches.
func TestRuntimeHeadFenceChoosesHeadFencedExit(t *testing.T) {
f := flags{workspaceLease: true, durableImpl: "walstore-chunk", volumeID: "v1"}
e := newProcessExit(context.Background())
stub := &runtimeFenceStub{updates: make(chan struct{}, 1)}
done := startRuntimeFenceWatch(t, e, f, stub)
select {
case out := <-e.decision:
t.Fatalf("an exit was chosen while not fenced: %+v", out)
case <-time.After(100 * time.Millisecond):
}
stub.fenced.Store(true)
stub.updates <- struct{}{}
select {
case out := <-e.decision:
if out != (workspaceExit{code: exitWorkspaceSuperseded, reason: "head_fenced"}) {
t.Fatalf("exit %+v, want 78 head_fenced", out)
}
case <-time.After(runtimeFenceWait):
t.Fatal("no exit chosen after the runtime fence")
}
select {
case <-done:
case <-time.After(runtimeFenceWait):
t.Fatal("the watcher chose the exit but did not return")
}
plain := newProcessExit(context.Background())
plainDone := startRuntimeFenceWatch(t, plain, flags{volumeID: "v1"}, stub)
select {
case <-plainDone: // not a workspace chunk primary: returns at once
case <-time.After(runtimeFenceWait):
t.Fatal("a non-workspace volume kept watching")
}
select {
case out := <-plain.decision:
t.Fatalf("a non-workspace volume chose %+v", out)
default:
}
}