fix(sw-block): mini sets weed's admin ports and records the run for --reuse

Review follow-up. weed mini always runs its admin server (-admin.ui=false
only hides the UI): its HTTP and worker gRPC ports are now set,
preflighted and shifted, and its legacy listener pair is off
(-admin.port.legacy=0); mini owns 13 ports. A fresh start records the
run's size, port shift, mode (its weed) and NQN in <dir>/mini.json;
--reuse starts that run again, refusing a flag that differs from the
record, a recorded weed that is gone, and a walstore run while a weed is
found (never a silent change of mode).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-28 14:11:52 -08:00
1 parent d6ef70d2fe
commit f74e1246ff
3 files changed
+161 -17

No files matched your search

+93 -10
View File
@@ -2,6 +2,7 @@ package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
@@ -42,6 +43,9 @@ type miniPorts struct {
// weed mini's listeners (HTTP and gRPC of master, volume and filer),
// all set explicitly: its S3, WebDAV and admin UI are turned off.
WeedMaster, WeedMasterG, WeedVolume, WeedVolumeG, WeedFiler, WeedFG int
// weed mini always runs its admin server (-admin.ui=false only hides the
// UI): its HTTP and worker gRPC ports are set too; its legacy pair is off.
WeedAdmin, WeedAdminG int
}
// shifted moves every port by n (--port-shift), for a host where a default
@@ -49,16 +53,17 @@ type miniPorts struct {
func (p miniPorts) shifted(n int) miniPorts {
return miniPorts{Master: p.Master + n, Data: p.Data + n, Ctrl: p.Ctrl + n, Status: p.Status + n, NVMe: p.NVMe + n,
WeedMaster: p.WeedMaster + n, WeedMasterG: p.WeedMasterG + n, WeedVolume: p.WeedVolume + n, WeedVolumeG: p.WeedVolumeG + n,
WeedFiler: p.WeedFiler + n, WeedFG: p.WeedFG + n}
WeedFiler: p.WeedFiler + n, WeedFG: p.WeedFG + n, WeedAdmin: p.WeedAdmin + n, WeedAdminG: p.WeedAdminG + n}
}
// weed are weed mini's ports, as mini sets and checks them.
func (p miniPorts) weed() []int {
return []int{p.WeedMaster, p.WeedMasterG, p.WeedVolume, p.WeedVolumeG, p.WeedFiler, p.WeedFG}
return []int{p.WeedMaster, p.WeedMasterG, p.WeedVolume, p.WeedVolumeG, p.WeedFiler, p.WeedFG, p.WeedAdmin, p.WeedAdminG}
}
var defaultMiniPorts = miniPorts{Master: 9180, Data: 9201, Ctrl: 9202, Status: 9203, NVMe: 4420,
WeedMaster: 9333, WeedMasterG: 19333, WeedVolume: 9340, WeedVolumeG: 19340, WeedFiler: 8888, WeedFG: 18888}
WeedMaster: 9333, WeedMasterG: 19333, WeedVolume: 9340, WeedVolumeG: 19340, WeedFiler: 8888, WeedFG: 18888,
WeedAdmin: 12535, WeedAdminG: 22535}
// miniStartWait bounds each child's readiness, and miniResolveWait the
// layout id lookup; miniStopWait bounds a child's exit after SIGTERM.
@@ -72,7 +77,71 @@ type miniConfig struct {
Dir, Bin, Weed, NQN string
Reuse bool
SizeBytes uint64
PortShift int
Ports miniPorts
// Explicit names the flags given on the command line (fs.Visit); a
// --reuse refuses one that differs from the recorded run.
Explicit map[string]bool
}
// miniRecord is what a fresh start records in <dir>/mini.json and --reuse
// starts again with: the volume's size, the ports, the mode and its weed,
// and the NQN.
type miniRecord struct {
SizeBytes uint64 `json:"size_bytes"`
PortShift int `json:"port_shift"`
Weed string `json:"weed,omitempty"` // empty: walstore, no weed
NQN string `json:"nqn"`
}
const miniRecordFile = "mini.json"
func writeMiniRecord(dir string, r miniRecord) error {
b, err := json.MarshalIndent(r, "", " ")
if err != nil {
return err
}
return os.WriteFile(filepath.Join(dir, miniRecordFile), append(b, '\n'), 0o644)
}
// reuseMiniRecord loads the recorded run into c: a flag given now must agree
// with the record, and a recorded weed must still be there (never a silent
// fall back to walstore).
func reuseMiniRecord(c *miniConfig, weedFound string) (string, error) {
b, err := os.ReadFile(filepath.Join(c.Dir, miniRecordFile))
if err != nil {
return "", fmt.Errorf("--reuse: %s has no %s (it was not started by sw-block mini): %w", c.Dir, miniRecordFile, err)
}
var r miniRecord
if err := json.Unmarshal(b, &r); err != nil {
return "", fmt.Errorf("--reuse: %s/%s: %w", c.Dir, miniRecordFile, err)
}
differs := func(flag string, now, then any) error {
if c.Explicit[flag] && fmt.Sprint(now) != fmt.Sprint(then) {
return fmt.Errorf("--reuse: --%s %v differs from this dir's run (%v); drop it to reuse the run as recorded", flag, now, then)
}
return nil
}
for _, e := range []error{differs("size", c.SizeBytes, r.SizeBytes), differs("port-shift", c.PortShift, r.PortShift),
differs("nqn", c.NQN, r.NQN), differs("weed", c.Weed, r.Weed)} {
if e != nil {
return "", e
}
}
if r.Weed != "" {
if st, err := os.Stat(r.Weed); err != nil || st.IsDir() {
return "", fmt.Errorf("--reuse: this dir's run used weed %s, which is not there now (pass it again with --weed, or start a new dir)", r.Weed)
}
}
if c.Explicit["weed"] == false && weedFound != "" && r.Weed == "" {
return "", fmt.Errorf("--reuse: this dir's run is walstore (no weed); a weed is found now: start a new dir for walstore-chunk")
}
c.SizeBytes, c.NQN, c.Weed = r.SizeBytes, r.NQN, r.Weed
if r.PortShift != c.PortShift {
c.Ports = defaultMiniPorts.shifted(r.PortShift)
c.PortShift = r.PortShift
}
return r.Weed, nil
}
const (
@@ -94,6 +163,7 @@ func miniWeedArgs(c miniConfig) []string {
port("master.port", c.Ports.WeedMaster), port("master.port.grpc", c.Ports.WeedMasterG),
port("volume.port", c.Ports.WeedVolume), port("volume.port.grpc", c.Ports.WeedVolumeG),
port("filer.port", c.Ports.WeedFiler), port("filer.port.grpc", c.Ports.WeedFG),
port("admin.port", c.Ports.WeedAdmin), port("admin.port.grpc", c.Ports.WeedAdminG), "-admin.port.legacy=0",
"-s3=false", "-webdav=false", "-admin.ui=false"}
}
@@ -312,7 +382,9 @@ func runMini(args []string, stdout, stderr io.Writer) int {
fmt.Fprintf(stderr, "sw-block mini: --port-shift %d out of range 0..10000\n", shift)
return ops.VolumeStatusExitInvalid
}
cfg.Ports = cfg.Ports.shifted(shift)
cfg.Ports, cfg.PortShift = cfg.Ports.shifted(shift), shift
cfg.Explicit = map[string]bool{}
fs.Visit(func(f *flag.Flag) { cfg.Explicit[f.Name] = true })
var err error
if cfg.SizeBytes, err = parseSize(size); err != nil {
fmt.Fprintf(stderr, "sw-block mini: %v\n", err)
@@ -348,6 +420,16 @@ func mini(ctx context.Context, cfg miniConfig, stdout, stderr io.Writer) (int, e
} else if st, err := os.Stat(weed); err != nil || st.IsDir() {
return ops.VolumeStatusExitInvalid, fmt.Errorf("--weed %s not found", weed)
}
if cfg.Reuse {
if st, err := os.Stat(cfg.Dir); err != nil || !st.IsDir() {
return ops.VolumeStatusExitInvalid, fmt.Errorf("--reuse: %s is not an existing directory", cfg.Dir)
}
w, err := reuseMiniRecord(&cfg, weed)
if err != nil {
return ops.VolumeStatusExitInvalid, err
}
weed = w
}
chunk := weed != ""
if !chunk {
fmt.Fprint(stdout, miniBanner())
@@ -366,11 +448,7 @@ func mini(ctx context.Context, cfg miniConfig, stdout, stderr io.Writer) (int, e
// The dir is claimed before anything is written: a fresh one by one
// exclusive mkdir, a reused one must exist; then a lock held for the run
// refuses a second mini on it.
if cfg.Reuse {
if st, err := os.Stat(cfg.Dir); err != nil || !st.IsDir() {
return ops.VolumeStatusExitInvalid, fmt.Errorf("--reuse: %s is not an existing directory", cfg.Dir)
}
} else {
if !cfg.Reuse {
if err := os.MkdirAll(filepath.Dir(filepath.Clean(cfg.Dir)), 0o755); err != nil {
return 1, err
}
@@ -386,6 +464,11 @@ func mini(ctx context.Context, cfg miniConfig, stdout, stderr io.Writer) (int, e
return ops.VolumeStatusExitInvalid, err
}
defer unlock()
if !cfg.Reuse {
if err := writeMiniRecord(cfg.Dir, miniRecord{SizeBytes: cfg.SizeBytes, PortShift: cfg.PortShift, Weed: weed, NQN: cfg.NQN}); err != nil {
return 1, err
}
}
for _, d := range []string{filepath.Join(cfg.Dir, "master"), filepath.Join(cfg.Dir, "vol")} {
if err := os.MkdirAll(d, 0o755); err != nil {
return 1, err
@@ -518,5 +601,5 @@ func miniBeforeStop(w io.Writer, c miniConfig) {
// miniAfterStop says where the data is and how to resume it.
func miniAfterStop(w io.Writer, c miniConfig) {
fmt.Fprintf(w, "sw-block mini: stopped. The data stays in %s; sw-block mini --dir %s --reuse starts it again.\n", c.Dir, c.Dir)
fmt.Fprintf(w, "sw-block mini: stopped. The data stays in %s; sw-block mini --dir %s --reuse starts it again, with this run's size, ports, mode and NQN (recorded in %s).\n", c.Dir, c.Dir, miniRecordFile)
}
+67 -6
View File
@@ -206,7 +206,7 @@ func freePorts(t *testing.T) miniPorts {
t.Helper()
var ps []int
var ls []net.Listener
for i := 0; i < 11; i++ {
for i := 0; i < 13; i++ {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
@@ -218,7 +218,8 @@ func freePorts(t *testing.T) miniPorts {
l.Close()
}
return miniPorts{Master: ps[0], Data: ps[1], Ctrl: ps[2], Status: ps[3], NVMe: ps[4],
WeedMaster: ps[5], WeedMasterG: ps[6], WeedVolume: ps[7], WeedVolumeG: ps[8], WeedFiler: ps[9], WeedFG: ps[10]}
WeedMaster: ps[5], WeedMasterG: ps[6], WeedVolume: ps[7], WeedVolumeG: ps[8], WeedFiler: ps[9], WeedFG: ps[10],
WeedAdmin: ps[11], WeedAdminG: ps[12]}
}
// The refusals happen before anything starts: an existing --dir without
@@ -431,7 +432,8 @@ func wantChunkCommands(dir, id string) map[string][]string {
return map[string][]string{
"weed": {"mini", "-dir=" + filepath.Join(dir, "weed"), "-ip=127.0.0.1", "-ip.bind=127.0.0.1",
"-master.port=9333", "-master.port.grpc=19333", "-volume.port=9340", "-volume.port.grpc=19340",
"-filer.port=8888", "-filer.port.grpc=18888", "-s3=false", "-webdav=false", "-admin.ui=false"},
"-filer.port=8888", "-filer.port.grpc=18888", "-admin.port=12535", "-admin.port.grpc=22535", "-admin.port.legacy=0",
"-s3=false", "-webdav=false", "-admin.ui=false"},
"blockmaster": {"--authority-store", filepath.Join(dir, "master", "authority"), "--lifecycle-store", filepath.Join(dir, "master", "lifecycle"),
"--cluster-spec", filepath.Join(dir, "cluster-spec.yaml"), "--expected-slots-per-volume", "1", "--listen", "127.0.0.1:9180",
"--workspace-rpc", "--chunk-filer", "127.0.0.1:18888", "--chunk-path", "/swblock/chunks"},
@@ -490,8 +492,8 @@ func TestMiniPortShiftMovesEveryPort(t *testing.T) {
t.Fatalf("port %d not shifted: %d -> %d", i, pa[i], pb[i])
}
}
if len(pa) != 11 {
t.Fatalf("%d ports; mini owns 11", len(pa))
if len(pa) != 13 {
t.Fatalf("%d ports; mini owns 13", len(pa))
}
}
@@ -521,6 +523,9 @@ func TestMiniDirIsOwnedByOneMini(t *testing.T) {
if err := os.Mkdir(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := writeMiniRecord(dir, miniRecord{SizeBytes: 1 << 20, NQN: "n"}); err != nil {
t.Fatal(err)
}
unlock, err := miniLock(dir)
if err != nil {
t.Fatal(err)
@@ -530,7 +535,7 @@ func TestMiniDirIsOwnedByOneMini(t *testing.T) {
if code, err := mini(context.Background(), c, io.Discard, io.Discard); code == 0 || err == nil || !strings.Contains(err.Error(), "in use") {
t.Fatalf("--reuse of a held dir: code %d err %v; want a refusal", code, err)
}
if entries, _ := os.ReadDir(dir); len(entries) != 1 {
if entries, _ := os.ReadDir(dir); len(entries) != 2 { // the lock and the record only
t.Fatalf("the refused start wrote into the held dir: %v", entries)
}
c.Reuse = false
@@ -570,3 +575,59 @@ func TestMiniWeedDyingDuringStartupStopsTheRest(t *testing.T) {
}
}
}
// --reuse starts the recorded run again: its size, ports, mode (its weed)
// and NQN, whatever the defaults are; a flag that differs from the record
// is refused, and so are a recorded weed that is gone and a walstore run
// while a weed is found.
func TestMiniReuseRestoresTheRecordedRun(t *testing.T) {
weed := filepath.Join(t.TempDir(), "weed")
if err := os.WriteFile(weed, []byte("x"), 0o755); err != nil {
t.Fatal(err)
}
rec := miniRecord{SizeBytes: 1 << 30, PortShift: 1000, Weed: weed, NQN: "nqn.custom:v1"}
dir := t.TempDir()
if err := writeMiniRecord(dir, rec); err != nil {
t.Fatal(err)
}
defaults := func() miniConfig {
return miniConfig{Dir: dir, NQN: "nqn.2026-05.io.seaweedfs:v1", SizeBytes: 256 << 20, Ports: defaultMiniPorts, Reuse: true, Explicit: map[string]bool{}}
}
c := defaults()
got, err := reuseMiniRecord(&c, "")
if err != nil {
t.Fatal(err)
}
if got != weed || c.Weed != weed || c.SizeBytes != rec.SizeBytes || c.NQN != rec.NQN || c.PortShift != 1000 || c.Ports != defaultMiniPorts.shifted(1000) {
t.Fatalf("reuse loaded %+v (weed %q); want the recorded run %+v", c, got, rec)
}
c = defaults()
c.Explicit["size"] = true
if _, err := reuseMiniRecord(&c, ""); err == nil || !strings.Contains(err.Error(), "--size") {
t.Fatalf("a --size that differs from the record: %v; want a refusal", err)
}
c = defaults()
c.SizeBytes, c.Explicit["size"] = rec.SizeBytes, true
if _, err := reuseMiniRecord(&c, ""); err != nil {
t.Fatalf("a --size equal to the record: %v", err)
}
if err := os.Remove(weed); err != nil {
t.Fatal(err)
}
c = defaults()
if _, err := reuseMiniRecord(&c, ""); err == nil || !strings.Contains(err.Error(), "not there now") {
t.Fatalf("a recorded weed that is gone: %v; want a refusal, not a walstore fall back", err)
}
if err := writeMiniRecord(dir, miniRecord{SizeBytes: 1 << 20, NQN: "n"}); err != nil {
t.Fatal(err)
}
c = defaults()
if _, err := reuseMiniRecord(&c, "/usr/bin/weed"); err == nil || !strings.Contains(err.Error(), "walstore") {
t.Fatalf("a walstore run reused while a weed is found: %v; want a refusal", err)
}
c = defaults()
if _, err := reuseMiniRecord(&miniConfig{Dir: t.TempDir(), Explicit: map[string]bool{}}, ""); err == nil {
t.Fatal("a dir without a record was reused")
}
_ = c
}
+1 -1
View File
@@ -424,7 +424,7 @@ sw-block mini --dir ./swb-mini # blockmaster and blockvolume next to sw
- **It does not connect the volume.** It never runs `sudo` or `nvme connect`; it prints the connect command, the status URL and the next commands (snapshot, checks).
- **Stopping:** Ctrl-C stops everything it started, the last started first. So does any of its processes exiting.
- If you connected v1, unmount it and run `sudo nvme disconnect -n <nqn>` **before** stopping mini. Otherwise a mounted filesystem sits on a stopped target while the kernel retries for 3 minutes. mini prints this with the connect command and again when it stops.
- The data stays in `--dir`; `sw-block mini --dir <dir> --reuse` starts it again.
- The data stays in `--dir`; `sw-block mini --dir <dir> --reuse` starts it again, with that run's size, ports, mode and NQN (recorded in `<dir>/mini.json`; a flag that differs is refused).
- **It is a trial setup:** one host, loopback, no authentication.
- **Refusals:** it refuses a `--dir` that already exists, unless you pass `--reuse` to start again on it, and refuses a port that is already in use. `--port-shift N` moves every port by N, for a host where a default port is taken.
- **Size:** `--size` sets v1's size (default `256MiB`). Volumes above the release maximum are refused by blockmaster as usual.