feat(cli): expose plain detach and print the sealed generation

This commit is contained in:
pingqiu committed 2026-11-21 19:46:24 -08:00
1 parent 0dde418dfd
commit dacd9be6ff
3 files changed
+52 -5

No files matched your search

+14 -4
View File
@@ -259,14 +259,15 @@ func chunkSnapshot(args []string, stdout, stderr io.Writer) int {
func chunkDetach(args []string, stdout, stderr io.Writer) int {
fs := chunkFlagSet("detach", stderr)
var (
volumeID, statusAddr string
timeout time.Duration
frozen, allowOverBudget bool
freezeStartedAtNs int64
volumeID, statusAddr string
timeout time.Duration
plain, frozen, allowOverBudget bool
freezeStartedAtNs int64
)
fs.StringVar(&volumeID, "volume", "", "volume id")
fs.StringVar(&statusAddr, "status-addr", "", "blockvolume status address or URL (with --chunk-control-endpoint)")
fs.DurationVar(&timeout, "timeout", 120*time.Second, "operation bound (capped at the server's OpDeadline)")
fs.BoolVar(&plain, "plain", false, "seal an idle volume without pre-copy; prints sealed HEAD generation on stderr")
fs.BoolVar(&frozen, "frozen", false, "not supported in this release: the server refuses a frozen detach (frozen_detach_unsupported)")
fs.Int64Var(&freezeStartedAtNs, "freeze-started-at-ns", 0, "not supported in this release; only valid with --frozen")
fs.BoolVar(&allowOverBudget, "allow-over-budget", false, "not supported in this release; only valid with --frozen")
@@ -276,10 +277,19 @@ func chunkDetach(args []string, stdout, stderr io.Writer) int {
if volumeID == "" || statusAddr == "" {
return chunkFail("detach", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume and --status-addr are required", false)
}
// M5 PlainStop -> Quiesce, INV-CUT-PLAIN-DETACH-001: an idle seal has
// its own finite caller bound and cannot encode frozen policy fields.
if plain && (timeout <= 0 || frozen || freezeStartedAtNs != 0 || allowOverBudget || fs.NArg() != 0) {
return chunkFail("detach", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--plain requires positive --timeout, no extra arguments and no frozen options", false)
}
if (frozen && freezeStartedAtNs <= 0) || (!frozen && (freezeStartedAtNs != 0 || allowOverBudget)) {
return chunkFail("detach", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--frozen requires --freeze-started-at-ns; --allow-over-budget requires --frozen", false)
}
body := map[string]any{"volumeID": volumeID}
if plain {
body["plain"] = true
return chunkPlainDetachPost(statusAddr, volumeID, timeout, body, stdout, stderr)
}
if frozen {
body["frozen"] = true
body["freezeStartedAtNs"] = freezeStartedAtNs
+37
View File
@@ -0,0 +1,37 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"time"
"github.com/seaweedfs/seaweed-block/core/ops"
)
// M5 PlainStop -> Quiesce, INV-CUT-PLAIN-DETACH-001. Keep stdout JSON
// unchanged; the separate human readout identifies the seal to the operator.
func chunkPlainDetachPost(addr, volume string, timeout time.Duration, body map[string]any, stdout, stderr io.Writer) int {
var out bytes.Buffer
code := chunkPost("detach", addr, "/chunk/detach", timeout, body, &out, stderr)
if code != ops.VolumeStatusExitOK {
if err := writeAll(stdout, out.Bytes()); err != nil {
fmt.Fprintf(stderr, "sw-block ops chunk detach: write result: %v\n", err)
}
return code
}
var seal struct {
VolumeID string `json:"volumeID"`
HeadGen uint64 `json:"headGen"`
}
if err := json.Unmarshal(out.Bytes(), &seal); err != nil || seal.VolumeID != volume || seal.HeadGen == 0 {
return chunkFail("detach", stdout, stderr, chunkExitFailed, codeInternalCLI, "plain detach outcome unknown: reply has no matching volume and sealed HEAD generation", false)
}
if err := writeAll(stdout, out.Bytes()); err != nil {
fmt.Fprintf(stderr, "sw-block ops chunk detach: write result: %v\n", err)
return chunkExitFailed
}
fmt.Fprintf(stderr, "sealed HEAD generation: %d\n", seal.HeadGen)
return ops.VolumeStatusExitOK
}
@@ -9,7 +9,7 @@ import (
// M5 PlainStop -> Quiesce, INV-CUT-PLAIN-DETACH-001: encode the existing
// idle seal request; preserve JSON stdout for existing operator tooling.
func TestOpsChunkPlainDetachSendsTheExistingRequest(t *testing.T) {
const reply = `{"volumeID":"vol","headGen":11,"headState":"detached"}`
const reply = `{"volumeID":"vol","headGen":11}`
srv, got := chunkServer(t, http.StatusOK, reply)
code, out, errOut := runChunk("detach", "--plain", "--volume", "vol", "--status-addr", srv.URL, "--timeout", "2s")
if code != 0 || out != reply || !strings.Contains(errOut, "sealed HEAD generation: 11\n") {