test: witness master loss before forced move and true revoke drain

This commit is contained in:
pingqiu committed 2026-11-22 18:20:42 -08:00
1 parent e8e5985cf5
commit d10d2fd104
3 files changed
+170 -3

No files matched your search

@@ -0,0 +1,166 @@
//go:build subprocess && chunkstore_integration
package main_test
import (
"bytes"
"fmt"
"io"
"net"
"sync"
"testing"
"time"
)
// Coverage cells A2.24, A2.27 and A2.35: real master dial failure while
// the holder is alive. Administrative calls still reach the real master.
func TestProcedureMasterUnreachableFencesBeforeForcedMove(t *testing.T) {
env := startHandoffEnv(t, 2)
proxy, disconnect := holderMasterProxy(t, env.h.masterAddr)
args := env.volArgs("s1")
for i := range args {
if args[i] == "--master" {
args[i+1] = proxy
}
}
old := env.h.start(t, "blockvolume-s1-partition", env.h.volume, args...)
e := env.adopt(t, old, "s1")
block := pattern("master-loss-kept!")
env.writeBlock(t, "s1", 0, block)
// Install a known remote point before losing the master, so the target's
// read proves data preservation without promising unpublished WAL data.
env.h.command(t, "ops", "chunk", "cut", "--volume", "v1", "--status-addr", env.hosts["s1"].status, "--timeout", "120s")
env.requireHeadHolder(t, e, "s1")
disconnect()
b := env.hosts["s2"]
move := []string{"ops", "workspace", "force-move", "--master", env.h.masterAddr, "--volume", "v1",
"--target-host", b.id, "--target-data", b.data, "--target-ctrl", b.ctrl, "--request-id", "master-loss-move",
"--confirm-forced", fmt.Sprintf("v1@%d", e)}
refusedWhileAlive := false
poll(t, 90*time.Second, "forced move never admitted after holder lost master", func() bool {
exitedBeforePreview := false
select {
case <-old.done:
exitedBeforePreview = true
default:
}
out, _ := env.h.command(t, append(append([]string{}, move...), "--dry-run")...)
if previewAdmitted(out) {
if !exitedBeforePreview {
t.Fatal("forced move admitted before the live holder's exit was observed")
}
return true
}
if !exitedBeforePreview {
refusedWhileAlive = true
}
return false
})
if !refusedWhileAlive {
t.Fatal("no forced-move refusal observed while the disconnected holder was alive")
}
if err := old.wait(time.Second); exitCode(err) != 75 {
t.Fatalf("master-lost holder exited %v; want75", err)
}
if !old.has("reason=lease_expired") {
t.Fatal("holder exit lacks affirmative lease_expired reason")
}
if listening(env.hosts["s1"].nvme) {
t.Fatal("exited holder still has a frontend listener")
}
out, _ := env.h.command(t, move...)
_, mustU := fatalOn(t)
e2 := mustU(forcedEpoch(out, e, "s1", "s2"))
nw := env.start(t, "blockvolume-s2-after-partition", "s2")
if got := env.adopt(t, nw, "s2"); got != e2 {
t.Fatalf("target epoch%d; want%d", got, e2)
}
if !bytes.Equal(env.readBlock(t, "s2", 0), block) {
t.Fatal("target lost the confirmed-cut block")
}
}
// holderMasterProxy forwards real TCP bytes until disconnect closes every
// accepted connection and the listener. Closing a port is the injected
// failure, never the evidence used to pass the lease/exit assertion.
func holderMasterProxy(t *testing.T, target string) (string, func()) {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
var mu sync.Mutex
var wg sync.WaitGroup
var once sync.Once
closed := false
conns := map[net.Conn]bool{}
acceptDone := make(chan struct{})
stop := func() {
once.Do(func() {
mu.Lock()
closed = true
_ = ln.Close()
for c := range conns {
_ = c.Close()
}
mu.Unlock()
})
}
t.Cleanup(func() {
stop()
select {
case <-acceptDone:
case <-time.After(5 * time.Second):
t.Error("proxy accept did not end")
}
joined := make(chan struct{})
go func() { wg.Wait(); close(joined) }()
select {
case <-joined:
case <-time.After(5 * time.Second):
t.Error("proxy connections did not end")
}
})
go func() {
defer close(acceptDone)
for {
client, err := ln.Accept()
if err != nil {
return
}
mu.Lock()
if closed {
mu.Unlock()
_ = client.Close()
continue
}
conns[client] = true
wg.Add(1)
mu.Unlock()
go func() {
defer wg.Done()
defer func() { mu.Lock(); delete(conns, client); mu.Unlock(); _ = client.Close() }()
server, err := net.DialTimeout("tcp", target, 2*time.Second)
if err != nil {
return
}
mu.Lock()
if closed {
mu.Unlock()
_ = server.Close()
return
}
conns[server] = true
mu.Unlock()
defer func() { mu.Lock(); delete(conns, server); mu.Unlock(); _ = server.Close() }()
copied := make(chan struct{})
go func() { _, _ = io.Copy(server, client); _ = server.Close(); close(copied) }()
_, _ = io.Copy(client, server)
_ = client.Close()
_ = server.Close()
<-copied
}()
}
}()
return ln.Addr().String(), stop
}
@@ -87,12 +87,13 @@ func TestWorkspaceDelayedRenewalCannotUndoExpiry(t *testing.T) {
}
func TestWorkspaceRevocationAckOnlyAfterFrontendAndCutsStop(t *testing.T) {
now := time.Unix(1000, 0)
// Keep the injected clock in the live context deadline domain.
now := time.Now()
revoking := false
attempts, acks := 0, 0
var g *WorkspaceLeaseGuard
client := workspaceTestClient{renew: func() (*control.WorkspaceLeaseFact, error) {
return &control.WorkspaceLeaseFact{VolumeId: "v", ReplicaId: "r", Epoch: 1, ValidForNs: int64(time.Second), Revoking: revoking}, nil
return &control.WorkspaceLeaseFact{VolumeId: "v", ReplicaId: "r", Epoch: 1, ValidForNs: int64(30 * time.Second), Revoking: revoking}, nil
},
ack: func() error {
if attempts != 2 || g.Projection().Healthy {
+1 -1
View File
@@ -727,7 +727,7 @@ Notes:
| `INV-WS-CLEAN-MOVE-001` | A clean move is granted only with a detached HEAD from the expected holder and a matching revocation acknowledgement at the current epoch. | M2 lease: Acked → Moved | `core/authority/workspace.go` `prepareWorkspaceMoveLocked`, `AckWorkspaceRevocation` | `TestWorkspaceCleanMoveNeedsDetachedHEADAndRevocationAck` (`core/authority/workspace_test.go`) | `WorkspaceTakeover` `CleanNoLoss` | ACTIVE |
| `INV-WS-FORCED-MOVE-001` | A forced move is admitted only after the persisted lease horizon and the granted or restart deadline, each plus the clock-skew bound, have passed for the lease the last move issue started (never a predecessor's): see M2 Expired → Moved (forced). | M2 lease: Expired → Moved (forced) | `core/authority/workspace.go` `prepareWorkspaceMoveLocked`, `WorkspaceForcedMoveAdmitted`, `forcedMoveAdmissionLocked`, `forcedMoveBarrierLocked`, `workspaceHeadAdmits`; `core/host/master/workspace_service.go` `MoveWorkspace`, `workspaceMoveCheckLocked` | `TestWorkspaceSkewSeparatesOldServingFromForcedGrant` (`core/host/master/workspace_skew_test.go`); `TestWorkspaceClockSkewPersistsAndProtectsRestart` (`core/authority/workspace_skew_test.go`); `TestWorkspaceForcedMoveRequiresExplicitForceAndLeaseExpiry` (`core/authority/workspace_test.go`); `TestWorkspaceRestartForcedBarrierHonoursHorizon`, `TestWorkspaceAmbiguousExtensionLatchesUncertainty` (`core/authority/workspace_lease_horizon_test.go`); `TestWorkspaceRenewalRPCDuringHeldSameVolumeReservation` (`core/host/master/workspace_lease_horizon_test.go`); `TestWorkspaceUnclaimedGrantForcedReassignAfterExpiry`, `TestWorkspaceUnclaimedGrantHEADRule`, `TestWorkspaceLateClaimThenUnclaimedGrantForcedReassign` (`core/authority/workspace_unclaimed_grant_test.go`); `TestWorkspaceForcedMoveAdmittedMatchesMoveAdmission` (`core/authority/workspace_forced_admission_test.go`); `TestWorkspaceUnclaimedGrantForcedReassignOverRPC` (`core/host/master/workspace_unclaimed_grant_test.go`); `TestWorkspacePausedGranteeRefusedAtAttach` (`core/host/master/workspace_paused_grantee_test.go`) | — | ACTIVE |
| `INV-WS-FORCED-MOVE-REPLY-001` | The lease expiry the master reports to a host is the granted expiry, never the persisted horizon. | M2 Granted → Granted; Granted → Revoking | `core/authority/workspace_lease_view.go` `RenewWorkspaceLease`, `WorkspaceLeaseReply`, `replyExpiryNS`; `core/host/master/workspace_service.go` `RevokeWorkspace` | `TestWorkspaceLeaseClampsToHorizonUnderStalledStore`, `TestWorkspaceSameVolumeRenewalDuringHeldWrite` (`core/authority/workspace_lease_horizon_test.go`); `TestWorkspaceUnclaimedGrantForcedReassignAfterExpiry` (`core/authority/workspace_unclaimed_grant_test.go`) | — | ACTIVE |
| `INV-WS-FORCED-MOVE-HALF-SKEW-001` | The validity the master gives a host with a lease ends half the clock-skew bound before the reported lease expiry. | M2 Granted → Granted (reply); M4 NoLease, Valid → Valid | `core/authority/workspace_lease_view.go` `WorkspaceLeaseValidity`, `workspaceLeaseValidityAt`; `core/host/master/workspace_service.go` `leaseFact`; `core/authority/workspace.go` `AckWorkspaceRevocation`; `core/host/volume/workspace_lease.go` `Refresh` | `TestWorkspaceLeaseFactUsesPublisherClockAndSkew` (`core/host/master/workspace_skew_test.go`); `TestWorkspaceAckUsesGrantedHalfSkewBoundary` (`core/authority/workspace_validity_test.go`); `TestWorkspaceSkewSeparatesOldServingFromForcedGrant` (`core/host/master/workspace_skew_test.go`) | — | ACTIVE |
| `INV-WS-FORCED-MOVE-HALF-SKEW-001` | The validity the master gives a host with a lease ends half the clock-skew bound before the reported lease expiry. | M2 Granted → Granted (reply); M4 NoLease, Valid → Valid | `core/authority/workspace_lease_view.go` `WorkspaceLeaseValidity`, `workspaceLeaseValidityAt`; `core/host/master/workspace_service.go` `leaseFact`; `core/authority/workspace.go` `AckWorkspaceRevocation`; `core/host/volume/workspace_lease.go` `Refresh` | `TestWorkspaceLeaseFactUsesPublisherClockAndSkew` (`core/host/master/workspace_skew_test.go`); `TestWorkspaceAckUsesGrantedHalfSkewBoundary` (`core/authority/workspace_validity_test.go`); `TestWorkspaceSkewSeparatesOldServingFromForcedGrant` (`core/host/master/workspace_skew_test.go`); `TestProcedureMasterUnreachableFencesBeforeForcedMove` (`cmd/blockvolume/master_loss_integration_test.go`, tags `subprocess chunkstore_integration`) | — | ACTIVE |
| `INV-WS-LOST-RANGE-READOUT-001` | Every issued forced grant the master returns, also on a replay and after a restart, carries the lost range stored in the authority store before the issue, never a recomputed one: see M2 Moved → epoch issued. | M2 lease: Expired → Moved (forced); Moved → epoch issued | `core/authority/workspace_lost_range.go` `recordLostRangeLocked`, `WorkspaceLostRangeReadouts`, `encodeLostRangeEnvelope`; `core/authority/workspace.go` `prepareWorkspaceMoveLocked`; `core/host/master/workspace_head.go` `headFilerMtimeNS`; `core/host/master/workspace_service.go` `grantWithLostRange`, `issuedRecord`; `core/rpc/wsclient/wsclient.go` `Readout` | `TestWorkspaceForcedMoveRecordsTheLostRange`, `TestWorkspaceForcedMoveLostRangeOrdering`, `TestWorkspaceForcedReservationResumeNeedsItsRange` (`core/authority/workspace_lost_range_test.go`); `TestWorkspaceForcedMoveStoresHeadFilerTime` (`core/authority/workspace_lost_range_readout_test.go`); `TestWorkspaceForcedMoveLostRangeOverRPC`, `TestWorkspaceHEADReadsFilerMtime` (`core/host/master/workspace_lost_range_readout_test.go`); `TestReadoutWording` (`core/rpc/wsclient/wsclient_test.go`); `TestWorkspaceForceMove_Row3and4_SentAndGranted` (`cmd/sw-block/workspace_test.go`); `TestWorkspaceForceMoveAgainstRealMaster` (`cmd/sw-block/workspace_master_test.go`) | — | ACTIVE |
| `INV-WS-LOST-RANGE-QUERY-001` | `QueryWorkspaceLostRange` returns the stored ranges unchanged: every issued forced move's range in issue order, or the single range of a given request id. | M2 lease: Moved → epoch issued (readout) | `core/authority/workspace_lost_range.go` `WorkspaceLostRangeReadouts`; `core/host/master/workspace_service.go` `QueryWorkspaceLostRange`; `core/rpc/wsclient/wsclient.go` `LostRanges`; `cmd/sw-block/workspace.go` `runWorkspaceLostRange` | `TestWorkspaceLostRangeAccumulatesInIssueOrder` (`core/authority/workspace_lost_range_readout_test.go`); `TestLostRangesBindsTheAnswer` (`core/rpc/wsclient/wsclient_test.go`); `TestWorkspaceLostRangeListsAllInOrder` (`cmd/sw-block/workspace_test.go`) | — | ACTIVE |
| `INV-WS-LOST-RANGE-FAILCLOSED-001` | If an issued forced move's range record is missing or fails its range or HEAD-time checksum, the readout fails: the grant carries no range but the reason, the query returns `DataLoss`, the CLI exits 4; none reports the move as no loss. | M2 lease: Moved → epoch issued | `core/authority/workspace_lost_range.go` `LoadWorkspaceLostRanges`, `decodeLostRangeHeadTimes`, `WorkspaceLostRangeReadouts`; `core/host/master/workspace_service.go` `grantWithLostRange`, `QueryWorkspaceLostRange`; `core/rpc/wsclient/wsclient.go` `classify`, `checkRange`; `cmd/sw-block/workspace.go` `runWorkspaceForceMove`, `runWorkspaceLostRange` | `TestWorkspaceLostRangeReadoutFailsClosed`, `TestWorkspaceLostRangeHeadTimeTamperFailsClosed`, `TestWorkspaceLostRangeSkippedWriteIsDetected` (`core/authority/workspace_lost_range_readout_test.go`); `TestForceMoveGrantWithoutRangeIsUnproven` (`core/rpc/wsclient/wsclient_test.go`); `TestWorkspaceForceMove_Row7_ReadoutUnproven` (`cmd/sw-block/workspace_test.go`) | — | ACTIVE |