From 400cf3edf73dd98a552f15351d2c9cc4d64c44af Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 28 Nov 2026 15:54:33 -0800 Subject: [PATCH] fix(blockvolume): the template passes --volume-id=%i; guests are an observation, not complete detection Review follow-up. The template unit takes the volume id from its instance name, so a settings file cannot name another volume (a fourth per-instance directive, kept by the drift test). /status/guests and its schema say what it is: the counted frontends (NVMe/TCP, iSCSI) only; an absent frontend is not served or not counted; a vanished host stays counted until TCP closes; 0 is not proof that nothing uses the volume. The endpoint test's callbacks use atomics. Co-Authored-By: Claude Opus 5.5 --- .../core/host/volume/status_guests_test.go | 16 ++++++++++------ .../core/host/volume/status_schema_test.go | 2 +- seaweed-block/core/host/volume/status_server.go | 16 ++++++++++------ .../deploy/systemd/sw-blockvolume@.service | 6 ++++-- seaweed-block/docs/status-schema.md | 6 +++--- .../testops/blockvolume_template_unit_test.go | 4 +++- 6 files changed, 31 insertions(+), 19 deletions(-) diff --git a/seaweed-block/core/host/volume/status_guests_test.go b/seaweed-block/core/host/volume/status_guests_test.go index d8209a4fa8..475bbe8d81 100644 --- a/seaweed-block/core/host/volume/status_guests_test.go +++ b/seaweed-block/core/host/volume/status_guests_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "net/http" + "sync/atomic" "testing" ) @@ -23,8 +24,8 @@ func getGuests(t *testing.T, addr, vol string) (int, GuestsStatus) { return resp.StatusCode, body } -// /status/guests reports the guests connected now through each frontend -// served, read at request time, and their sum; with no frontend served it +// /status/guests reports the guests connected now through each counted +// frontend, read at request time, and their sum; with none registered it // reports none; another volume is not answered for. func TestStatusServer_GuestsEndpoint(t *testing.T) { s := NewStatusServer(NewAdapterProjectionView(stubProjector{}, "v1", "r1", nil)) @@ -37,14 +38,17 @@ func TestStatusServer_GuestsEndpoint(t *testing.T) { if code, body := getGuests(t, addr, "v1"); code != http.StatusOK || body.Connected != 0 || len(body.Frontends) != 0 { t.Fatalf("no frontend served: %d %+v; want 200 and none", code, body) } - nvmeHosts, iscsiSessions := 1, 2 - s.SetGuestSource("nvme", func() int { return nvmeHosts }) - s.SetGuestSource("iscsi", func() int { return iscsiSessions }) + var nvmeHosts, iscsiSessions atomic.Int64 // read by the server's handler goroutine + nvmeHosts.Store(1) + iscsiSessions.Store(2) + s.SetGuestSource("nvme", func() int { return int(nvmeHosts.Load()) }) + s.SetGuestSource("iscsi", func() int { return int(iscsiSessions.Load()) }) code, body := getGuests(t, addr, "v1") if code != http.StatusOK || body.VolumeID != "v1" || body.Frontends["nvme"] != 1 || body.Frontends["iscsi"] != 2 || body.Connected != 3 { t.Fatalf("two frontends: %d %+v; want nvme 1, iscsi 2, connected 3", code, body) } - nvmeHosts, iscsiSessions = 0, 0 + nvmeHosts.Store(0) + iscsiSessions.Store(0) if _, body := getGuests(t, addr, "v1"); body.Connected != 0 || len(body.Frontends) != 2 { t.Fatalf("all disconnected: %+v; want both frontends at 0 (read now, not cached)", body) } diff --git a/seaweed-block/core/host/volume/status_schema_test.go b/seaweed-block/core/host/volume/status_schema_test.go index 232e65b135..257d1e5206 100644 --- a/seaweed-block/core/host/volume/status_schema_test.go +++ b/seaweed-block/core/host/volume/status_schema_test.go @@ -31,7 +31,7 @@ var statusRoutes = []struct { {"/status/recovery", "the engine's replica projection (only with recovery enabled)", reflect.TypeOf(engine.ReplicaProjection{})}, {"/status/chunk", "chunk store usage of a walstore-chunk volume (only with --chunk-control-endpoint)", reflect.TypeOf(chunkstore.Usage{})}, {"/status/nvme", "NVMe/TCP frontend counters", reflect.TypeOf(NVMeStatus{})}, - {"/status/guests", "the guests connected now through each frontend served", reflect.TypeOf(GuestsStatus{})}, + {"/status/guests", "guests connected now through the counted frontends (NVMe/TCP, iSCSI); an observation, not complete detection", reflect.TypeOf(GuestsStatus{})}, {"/status/ublk", "ublk frontend counters", reflect.TypeOf(UblkStatus{})}, {"/status/frontend-capabilities", "the frontends this host can serve", reflect.TypeOf(FrontendCapabilitiesStatus{})}, } diff --git a/seaweed-block/core/host/volume/status_server.go b/seaweed-block/core/host/volume/status_server.go index cce516071a..0cd88ba987 100644 --- a/seaweed-block/core/host/volume/status_server.go +++ b/seaweed-block/core/host/volume/status_server.go @@ -339,8 +339,8 @@ func (s *StatusServer) SetNVMeStatusSource(src nvmeStatusSource) { } // SetGuestSource registers how many guests are connected now through one -// frontend ("nvme": connected hosts, "iscsi": Normal sessions), for -// /status/guests. +// frontend whose connections are counted ("nvme": NVMe/TCP hosts, "iscsi": +// Normal sessions), for /status/guests. func (s *StatusServer) SetGuestSource(frontend string, connected func() int) { s.mu.Lock() defer s.mu.Unlock() @@ -512,12 +512,16 @@ func (s *StatusServer) handleStatusUblk(w http.ResponseWriter, r *http.Request) } // GuestsStatus is /status/guests: the guests connected now through each -// frontend this host serves the volume on, and their total. A frontend not -// served is absent. +// counted frontend (NVMe/TCP and iSCSI), and their total. It is an +// observation, not complete guest detection: a frontend absent from +// Frontends is not served or not counted (ublk and NVMe/RDMA are not), a +// vanished host stays counted until its TCP connection closes, and a guest +// may connect right after the read. Connected 0 is not proof that nothing +// uses the volume. type GuestsStatus struct { VolumeID string `json:"volumeID"` // the served volume - Frontends map[string]int `json:"frontends"` // guests connected now per frontend served ("nvme": hosts, "iscsi": Normal sessions) - Connected int `json:"connected"` // the sum over Frontends + Frontends map[string]int `json:"frontends"` // guests connected now per counted frontend ("nvme": NVMe/TCP hosts, "iscsi": Normal sessions); absent: not served or not counted + Connected int `json:"connected"` // the sum over Frontends; 0 is not proof that nothing uses the volume } func (s *StatusServer) handleStatusGuests(w http.ResponseWriter, r *http.Request) { diff --git a/seaweed-block/deploy/systemd/sw-blockvolume@.service b/seaweed-block/deploy/systemd/sw-blockvolume@.service index d59ca40fb6..12730d1ac9 100644 --- a/seaweed-block/deploy/systemd/sw-blockvolume@.service +++ b/seaweed-block/deploy/systemd/sw-blockvolume@.service @@ -3,7 +3,9 @@ # and starts it. Settings: /etc/sw-block/volumes/.env (the same keys # as deploy/config/blockvolume.env), each instance with its own state # directory (/var/lib/sw-block-volume/; SWB_DURABLE_ROOT inside it), -# ports and NQN. Apart from the lines marked "per instance" this unit is +# ports and NQN. The volume id is the instance name (--volume-id=%i): the +# settings file's SWB_VOLUME_ID is not read, so the two cannot differ. +# Apart from the lines marked "per instance" this unit is # sw-blockvolume.service; a test keeps the two the same. [Unit] StartLimitIntervalSec=600 @@ -29,7 +31,7 @@ StateDirectoryMode=0750 ExecStart=/usr/local/bin/blockvolume \ --master=${SWB_MASTER_ADDR} \ --server-id=${SWB_SERVER_ID} \ - --volume-id=${SWB_VOLUME_ID} \ + --volume-id=%i \ --replica-id=${SWB_REPLICA_ID} \ --data-addr=${SWB_DATA_ADDR} \ --ctrl-addr=${SWB_CTRL_ADDR} \ diff --git a/seaweed-block/docs/status-schema.md b/seaweed-block/docs/status-schema.md index b146e913e8..99db0922a4 100644 --- a/seaweed-block/docs/status-schema.md +++ b/seaweed-block/docs/status-schema.md @@ -369,13 +369,13 @@ NVMe/TCP frontend counters. Go type `volume.NVMeStatus`. ### `GET /status/guests` -The guests connected now through each frontend served. Go type `volume.GuestsStatus`. +Guests connected now through the counted frontends (NVMe/TCP, iSCSI); an observation, not complete detection. Go type `volume.GuestsStatus`. | Field | Type | Meaning | |---|---|---| | `volumeID` | string | the served volume | -| `frontends{}` | int | guests connected now per frontend served ("nvme": hosts, "iscsi": Normal sessions) | -| `connected` | int | the sum over Frontends | +| `frontends{}` | int | guests connected now per counted frontend ("nvme": NVMe/TCP hosts, "iscsi": Normal sessions); absent: not served or not counted | +| `connected` | int | the sum over Frontends; 0 is not proof that nothing uses the volume | ### `GET /status/ublk` diff --git a/seaweed-block/internal/testops/blockvolume_template_unit_test.go b/seaweed-block/internal/testops/blockvolume_template_unit_test.go index 6d03a4ecf1..9efc61a15a 100644 --- a/seaweed-block/internal/testops/blockvolume_template_unit_test.go +++ b/seaweed-block/internal/testops/blockvolume_template_unit_test.go @@ -22,11 +22,13 @@ func unitDirectives(body string) []string { // templatePerInstance are the only directives sw-blockvolume@.service may // set differently from sw-blockvolume.service: each instance's name, its -// settings file and its state directory. +// settings file, its state directory, and its volume id (the instance +// name, so a settings file cannot name another volume). var templatePerInstance = map[string]string{ "Description=Seaweed Block volume (one replica)": "Description=Seaweed Block volume %i (one replica)", "EnvironmentFile=/etc/sw-block/blockvolume.env": "EnvironmentFile=/etc/sw-block/volumes/%i.env", "StateDirectory=sw-block-volume": "StateDirectory=sw-block-volume/%i", + `--volume-id=${SWB_VOLUME_ID} \`: `--volume-id=%i \`, } // The per-volume template is the shipped single-volume unit, directive for