fix(sw-block): volume stop keeps its checks for a transitional unit; a cut answer needs its fields

Review follow-up. activating, deactivating and reloading may have a live
holder (reloading is an active unit): the guest read and the cut stay,
refusing when the holder is down, and --force --no-cut stops the unit. A
cut's success answer must carry cutLSN and headGen (LSN 0 is an empty
volume's); without them the outcome is unknown. The lost-answer fake now
records its publication, and the refusal reports that confirmed cut.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-28 19:25:42 -08:00
1 parent 3aca5e3212
commit a01f09e6fa
3 files changed
+58 -20

No files matched your search

+12 -9
View File
@@ -409,11 +409,11 @@ func volumeStop(ctx context.Context, env verbEnv, o stopOpts, stdout, stderr io.
fmt.Fprintf(stdout, "sw-block volume stop: %s is not running (%s): nothing to cut or stop\n", unit, st)
return 0
}
switch st {
case "active":
default: // activating, deactivating, reloading: not serving now, but systemd may start it again
fmt.Fprintf(stdout, "sw-block volume stop: %s is %s, not serving: no guest read and no cut\n", unit, st)
return stopUnit(ctx, env, unit, o.Volume, "no cut taken ("+unit+" was "+st+"): "+lastConfirmedCut(ctx, env, status, o.Volume)+"; writes after it are on this host only", stdout, stderr)
if st != "active" {
// activating, deactivating, reloading: a holder may still be live,
// so the guest read and the cut stay; if the holder is down they
// refuse, and --force --no-cut stops the unit anyway.
fmt.Fprintf(stdout, " %s is %s: a holder may still run; if it does not, the steps below refuse and --force --no-cut stops the unit\n", unit, st)
}
if err := env.step(stdout, "guests", "read the guests connected to "+o.Volume); err != nil {
return refuse("guests", err.Error())
@@ -458,7 +458,7 @@ func volumeStop(ctx context.Context, env verbEnv, o stopOpts, stdout, stderr io.
"A cut that timed out keeps what it uploaded: running the same command again continues from it. "+
"Or pass --no-cut to stop without a cut.", err, lastConfirmedCut(ctx, env, status, o.Volume)))
}
fmt.Fprintf(stdout, " cut: LSN %d, head generation %d (the time below is the request's start; the cut covers every write before it)\n", res.CutLSN, res.HeadGen)
fmt.Fprintf(stdout, " cut: LSN %d, head generation %d (the time below is the request's start; the cut covers every write before it)\n", *res.CutLSN, *res.HeadGen)
final = fmt.Sprintf(stopRecoveryPoint, cutAt.UTC().Format(time.RFC3339))
}
return stopUnit(ctx, env, unit, o.Volume, final, stdout, stderr)
@@ -481,9 +481,9 @@ func stopUnit(ctx context.Context, env verbEnv, unit, volume, final string, stdo
// cutAnswer is the part of POST /chunk/cut's success body volume stop checks.
type cutAnswer struct {
VolumeID string `json:"volumeID"`
CutLSN uint64 `json:"cutLSN"`
HeadGen uint64 `json:"headGen"`
VolumeID string `json:"volumeID"`
CutLSN *uint64 `json:"cutLSN"` // required (0 is a legitimate empty volume's)
HeadGen *uint64 `json:"headGen"` // required
}
// volumeCut is POST /chunk/cut, one request bounded by timeout. Only a
@@ -527,6 +527,9 @@ func volumeCut(ctx context.Context, env verbEnv, status, volume string, timeout
if a.VolumeID != volume {
return unknown(fmt.Sprintf("answer names volume %q", a.VolumeID))
}
if a.CutLSN == nil || a.HeadGen == nil {
return unknown("answer lacks cutLSN or headGen")
}
return a, nil
}
+45 -10
View File
@@ -147,6 +147,7 @@ type fakeHost struct {
noGuests atomic.Bool // /status/guests answers 404
cutMode atomic.Value // "ok", "fail", "hang"
cuts atomic.Int64
published atomic.Int64 // cuts the fake published although their answer was lost
lastCutNs atomic.Int64
srv *httptest.Server
}
@@ -180,7 +181,9 @@ func newFakeHost(t *testing.T) *fakeHost {
case "fail":
w.WriteHeader(http.StatusServiceUnavailable)
_ = json.NewEncoder(w).Encode(map[string]any{"code": "not_primary", "message": "not the primary"})
case "drop": // published, then the answer is lost
case "drop": // published (the holder confirms it), then the answer is lost
h.lastCutNs.Store(time.Date(2026, 11, 28, 22, 59, 0, 0, time.UTC).UnixNano())
h.published.Add(1)
if hj, ok := w.(http.Hijacker); ok {
c, _, _ := hj.Hijack()
_ = c.Close()
@@ -197,8 +200,12 @@ func newFakeHost(t *testing.T) *fakeHost {
case <-r.Context().Done():
case <-time.After(2 * time.Second):
}
case "missing-fields":
_ = json.NewEncoder(w).Encode(map[string]any{"volumeID": "vol-a"})
case "zero":
_ = json.NewEncoder(w).Encode(map[string]any{"volumeID": "vol-a", "cutLSN": 0, "headGen": 1})
default:
_ = json.NewEncoder(w).Encode(map[string]any{"volumeID": "vol-a", "cutLSN": 9})
_ = json.NewEncoder(w).Encode(map[string]any{"volumeID": "vol-a", "cutLSN": 9, "headGen": 4})
}
})
h.srv = httptest.NewServer(mux)
@@ -600,16 +607,31 @@ func TestVolumePageSaysTheStopSentence(t *testing.T) {
}
}
// A unit systemd is restarting (activating) is not "nothing to stop": stop
// runs systemctl stop without a guest read or a cut, and says no cut was
// taken. Only inactive and failed are nothing to stop.
func TestVolumeStopStopsAUnitThatIsActivating(t *testing.T) {
// A transitional unit (activating, deactivating, reloading) may have a live
// holder: stop keeps the guest read and the cut. Live: it refuses for a
// guest, otherwise cuts and stops. Down (status unreadable): it refuses,
// and --force --no-cut stops it. Only inactive and failed are nothing to
// stop.
func TestVolumeStopKeepsItsChecksForATransitionalUnit(t *testing.T) {
for _, st := range []string{"activating", "deactivating", "reloading"} {
h, env := startedHost(t, true)
_, _ = h.systemctl(context.Background(), "set-state", st)
code, out, errs := runStop(env, stopOpts{Volume: "vol-a", Timeout: time.Second})
if code != 0 || h.called("stop sw-blockvolume@vol-a.service") != 1 || h.cuts.Load() != 0 || !strings.Contains(out, "no cut taken") {
t.Fatalf("%s: %d %s %s (cuts %d, calls %v)", st, code, out, errs, h.cuts.Load(), h.calls)
h.guests.Store(1)
if code, _, errs := runStop(env, stopOpts{Volume: "vol-a", Timeout: time.Second}); code != 2 || h.called("stop ") != 0 || !strings.Contains(errs, "guest connection") {
t.Fatalf("%s with a guest: %d %s; want the guest refusal", st, code, errs)
}
h.guests.Store(0)
if code, out, errs := runStop(env, stopOpts{Volume: "vol-a", Timeout: time.Second}); code != 0 || h.cuts.Load() != 1 || h.called("stop ") != 1 || !strings.Contains(out, "recovery point in the object store") {
t.Fatalf("%s, live holder: %d %s %s (cuts %d)", st, code, out, errs, h.cuts.Load())
}
d, denv := startedHost(t, true)
_, _ = d.systemctl(context.Background(), "set-state", st)
d.srv.Close() // the holder is down: status unreadable
if code, _, errs := runStop(denv, stopOpts{Volume: "vol-a", Timeout: time.Second}); code != 2 || d.called("stop ") != 0 {
t.Fatalf("%s, holder down: %d %s; want a refusal", st, code, errs)
}
if code, out, errs := runStop(denv, stopOpts{Volume: "vol-a", Force: true, NoCut: true, Timeout: time.Second}); code != 0 || d.called("stop ") != 1 || !strings.Contains(out, "no cut taken") {
t.Fatalf("%s, holder down, --force --no-cut: %d %s %s", st, code, out, errs)
}
}
for _, st := range []string{"inactive", "failed"} {
@@ -621,6 +643,16 @@ func TestVolumeStopStopsAUnitThatIsActivating(t *testing.T) {
}
}
// A zero cut LSN (an empty volume) with a head generation is a complete
// answer; a success answer without the fields is not (above).
func TestVolumeStopAcceptsAZeroLSNCut(t *testing.T) {
h, env := startedHost(t, true)
h.cutMode.Store("zero")
if code, out, errs := runStop(env, stopOpts{Volume: "vol-a", Timeout: time.Second}); code != 0 || !strings.Contains(out, "cut: LSN 0, head generation 1") {
t.Fatalf("zero LSN: %d %s %s", code, out, errs)
}
}
// The cut time printed is the one taken before the cut's request, never a
// later reading: the barrier is no earlier than it.
func TestVolumeStopPrintsTheTimeTakenBeforeTheCut(t *testing.T) {
@@ -707,7 +739,7 @@ func TestVolumeUnitStateErrorsAndTransitionsAreNotStopped(t *testing.T) {
// outcome_unknown are each an unknown outcome: stop refuses, says the cut
// may have published, stops nothing; the same command then completes.
func TestVolumeStopUnknownCutAnswersRefuse(t *testing.T) {
for _, mode := range []string{"drop", "malformed", "foreign", "unknown-code"} {
for _, mode := range []string{"drop", "malformed", "foreign", "unknown-code", "missing-fields"} {
t.Run(mode, func(t *testing.T) {
h, env := startedHost(t, true)
h.cutMode.Store(mode)
@@ -715,6 +747,9 @@ func TestVolumeStopUnknownCutAnswersRefuse(t *testing.T) {
if code != 2 || !strings.Contains(errs, "may have published") || h.called("stop ") != 0 {
t.Fatalf("%s: %d %s (calls %v)", mode, code, errs, h.calls)
}
if mode == "drop" && (h.published.Load() != 1 || !strings.Contains(errs, "the last confirmed cut was at 2026-11-28T22:59:00Z")) {
t.Fatalf("drop: published %d; the refusal must report the holder's confirmed cut: %s", h.published.Load(), errs)
}
if strings.Contains(errs, "did not move") {
t.Fatalf("%s: the refusal claims the recovery point did not move: %s", mode, errs)
}
+1 -1
View File
@@ -500,7 +500,7 @@ mini uses the commands of the sections below; the steps there remain the way to
**`sudo sw-block volume stop <volume> [--force] [--no-cut] [--timeout 2m]`**
1. **Guests.** It refuses while a guest is connected (`/status/guests`: NVMe/TCP hosts and iSCSI sessions), unless `--force`. This protects a connected guest from I/O errors; it is not a completeness guarantee. A host that vanished (a killed VM, a partitioned host) still counts until its TCP connection closes: when you know nothing uses the volume, `--force` stops it. ublk and NVMe/RDMA guests are not counted.
2. **Cut.** It takes a cut (walstore-chunk). When the cut fails or its outcome is unknown, it refuses and stops nothing: the local data is safe on this host, and the message gives the age of the object store's recovery point (from the holder's last cut; "age unknown (since restart)" after a restart). A cut that timed out keeps what it uploaded, so running the same command again continues from it. `--no-cut` stops without a cut. The time in the closing line is taken before the cut's request, so the cut covers every write before it.
3. **Unit.** It runs `systemctl stop`. Only an `inactive` or `failed` unit is "nothing to stop"; a unit systemd is restarting (`activating`) is stopped without a guest read or a cut, and the output says no cut was taken. The unit stays enabled, so it starts again at boot; `volume start` with the same flags starts it now.
3. **Unit.** It runs `systemctl stop`. Only an `inactive` or `failed` unit is "nothing to stop". A unit in a transitional state (`activating`, `deactivating`, `reloading`) may still have a live holder, so the guest read and the cut stay; if the holder is down they refuse, and `--force --no-cut` stops the unit. The unit stays enabled, so it starts again at boot; `volume start` with the same flags starts it now.
Its output ends with what it promises, and only that: