feat: resolve standalone volume layout identity through the master

This commit is contained in:
pingqiu committed 2026-11-25 19:09:59 -08:00
1 parent f96eb41e57
commit 3de99a839c
4 files changed
+158 -1

No files matched your search

+4 -1
View File
@@ -82,7 +82,7 @@ func run(args []string, stdout, stderr io.Writer) int {
return ops.VolumeStatusExitInvalid
}
if len(args) < 2 {
fmt.Fprintln(stderr, "sw-block: expected subcommand ops status|inventory|list|cluster|volumes|describe|timeline|explain|report|dashboard|generate-helm-values|operator-status|lifecycle-owner|authority-executor|rebuild-target-owner|failback-target-owner|failback-executor|frontend-publication-target-owner|frontend-publication-executor|chunk|volume-unheld|workspace")
fmt.Fprintln(stderr, "sw-block: expected subcommand ops status|inventory|list|cluster|volumes|describe|timeline|explain|report|dashboard|generate-helm-values|operator-status|lifecycle-owner|authority-executor|rebuild-target-owner|failback-target-owner|failback-executor|frontend-publication-target-owner|frontend-publication-executor|chunk|volume|volume-unheld|workspace")
usage(stderr)
return ops.VolumeStatusExitInvalid
}
@@ -125,6 +125,8 @@ func run(args []string, stdout, stderr io.Writer) int {
return runOpsFrontendPublicationExecutor(args[2:], stdout, stderr)
case "chunk":
return runOpsChunk(args[2:], stdout, stderr)
case "volume":
return runOpsVolume(args[2:], stdout, stderr)
case "volume-unheld":
return runOpsVolumeUnheld(args[2:], stdout, stderr)
case "workspace":
@@ -2296,6 +2298,7 @@ func usage(w io.Writer) {
fmt.Fprintln(w, " sw-block ops chunk show --volume <id> --chunk-filer <host:port> [--chunk-path /swblock/chunks]")
fmt.Fprintln(w, " sw-block ops chunk verify --volume <id> --chunk-filer <host:port> [--chunk-path /swblock/chunks] [--depth meta|bindings|data] [--orphans] [--timeout <d>]")
fmt.Fprintln(w, " (read-only; every root HEAD names is checked; exit 0 clean, 3 findings, 1 incomplete; --depth data needs --timeout)")
fmt.Fprintln(w, " sw-block ops volume resolve --master <addr> [--timeout 10s] <name> (prints the master layout id)")
fmt.Fprintln(w, " sw-block ops volume-unheld --path <volume-file> (exit 0: no process on this host holds it; local filesystems only)")
fmt.Fprintln(w, " sw-block ops workspace force-move --master <addr> --volume <id> --target-host <server-id> --target-data <addr> --target-ctrl <addr> --request-id <id> --confirm-forced <volume>@<epoch> [--dry-run]")
fmt.Fprintln(w, " sw-block ops workspace lost-range --master <addr> --volume <id> [--request-id <id>] (authority only; no bypass: see `sw-block ops workspace help`)")
@@ -0,0 +1,41 @@
package main
import (
"context"
"flag"
"fmt"
"io"
"time"
"github.com/seaweedfs/seaweed-block/core/ops"
)
// runOpsVolume exposes the same master name binding used by ops chunk.
func runOpsVolume(args []string, stdout, stderr io.Writer) int {
const usage = "usage: sw-block ops volume resolve --master <addr> [--timeout 10s] <name>"
if len(args) == 0 || args[0] != "resolve" {
fmt.Fprintln(stderr, usage)
return ops.VolumeStatusExitInvalid
}
fs := flag.NewFlagSet("sw-block ops volume resolve", flag.ContinueOnError)
fs.SetOutput(stderr)
fs.Usage = func() { fmt.Fprintln(stderr, usage) }
master := fs.String("master", "", "master gRPC address")
timeout := fs.Duration("timeout", 10*time.Second, "master query timeout")
if err := fs.Parse(args[1:]); err != nil {
return ops.VolumeStatusExitInvalid
}
if *master == "" || *timeout <= 0 || fs.NArg() != 1 || fs.Arg(0) == "" {
fmt.Fprintln(stderr, usage)
return ops.VolumeStatusExitInvalid
}
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
defer cancel()
id, err := resolveVolumeID(ctx, *master, fs.Arg(0))
if err != nil {
fmt.Fprintf(stderr, "sw-block ops volume resolve: %v\n", err)
return ops.VolumeStatusExitInvalid
}
fmt.Fprintln(stdout, id.String())
return ops.VolumeStatusExitOK
}
@@ -0,0 +1,91 @@
package main
import (
"bytes"
"context"
"net"
"sync"
"testing"
"github.com/seaweedfs/seaweed-block/core/rpc/control"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type volumeResolveServer struct {
control.UnimplementedLifecycleServiceServer
mu sync.Mutex
reply *control.CreateVolumeResponse
err error
names []string
}
func (s *volumeResolveServer) GetVolume(_ context.Context, q *control.GetLifecycleVolumeRequest) (*control.CreateVolumeResponse, error) {
s.mu.Lock()
s.names = append(s.names, q.GetVolumeId())
s.mu.Unlock()
return s.reply, s.err
}
func serveVolumeResolver(t *testing.T, s *volumeResolveServer) string {
t.Helper()
lis, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
server := grpc.NewServer()
control.RegisterLifecycleServiceServer(server, s)
go func() { _ = server.Serve(lis) }()
t.Cleanup(server.Stop)
return lis.Addr().String()
}
func TestOpsVolumeResolvePrintsOnlyMasterLayoutID(t *testing.T) {
const id = "1234567890abcdef1234567890abcdef"
s := &volumeResolveServer{reply: &control.CreateVolumeResponse{VolumeId: "v1", LayoutId: id}}
addr := serveVolumeResolver(t, s)
var out, errout bytes.Buffer
code := run([]string{"ops", "volume", "resolve", "--master", addr, "v1"}, &out, &errout)
if code != 0 || out.String() != id+"\n" || errout.Len() != 0 {
t.Fatalf("code=%d stdout=%q stderr=%q", code, &out, &errout)
}
s.mu.Lock()
defer s.mu.Unlock()
if len(s.names) != 1 || s.names[0] != "v1" {
t.Fatalf("resolved names=%v", s.names)
}
}
func TestOpsVolumeResolveRefusesMissingOrForeignIdentity(t *testing.T) {
for _, tc := range []struct {
name, volume, id string
err error
}{
{"missing", "", "", status.Error(codes.NotFound, "no such volume")},
{"unavailable", "", "", status.Error(codes.Unavailable, "master unavailable")},
{"invalid-id", "v1", "bad", nil},
{"wrong-name", "v2", "1234567890abcdef1234567890abcdef", nil},
} {
t.Run(tc.name, func(t *testing.T) {
s := &volumeResolveServer{reply: &control.CreateVolumeResponse{VolumeId: tc.volume, LayoutId: tc.id}, err: tc.err}
addr := serveVolumeResolver(t, s)
var out, errout bytes.Buffer
code := run([]string{"ops", "volume", "resolve", "--master", addr, "v1"}, &out, &errout)
if code != 2 || out.Len() != 0 || errout.Len() == 0 {
t.Fatalf("code=%d stdout=%q stderr=%q", code, &out, &errout)
}
})
}
}
func TestOpsVolumeResolveRequiresOneNameAndBoundedQuery(t *testing.T) {
for _, args := range [][]string{
{}, {"other"}, {"resolve", "v1"}, {"resolve", "--master", "unused"},
{"resolve", "--master", "unused", "v1", "v2"},
{"resolve", "--master", "unused", "--timeout", "0s", "v1"},
} {
var out, errout bytes.Buffer
code := run(append([]string{"ops", "volume"}, args...), &out, &errout)
if code != 2 || out.Len() != 0 || errout.Len() == 0 {
t.Fatalf("args=%v code=%d stdout=%q stderr=%q", args, code, &out, &errout)
}
}
}
@@ -176,6 +176,28 @@ that store. This release has no `sw-block` command that creates that record. Che
volume`. A move target must also be a registered node, or the move is refused
with `move target node "s2" is not registered`.
### Resolve the LOCAL volume's layout id
Before starting a standalone `walstore-chunk` blockvolume for an existing
volume, ask the same master for its layout id:
```bash
sw-block ops volume resolve --master 127.0.0.1:9180 v1
```
The command prints only the full 32-hex-digit id and a newline. Pass that
value as `blockvolume --layout-id <printed-id>` along with `--volume-id v1`
and the other startup flags. For the example systemd units, add
`--layout-id <printed-id>` to `SWB_VOLUME_EXTRA_ARGS` in
`/etc/sw-block/blockvolume.env` before starting the service. Resolve each
volume separately; do not mint an id or copy another volume's value.
This is a read-only query of the master's volume record, using the same
TLS and token environment settings as `ops chunk`. A missing volume, an
invalid identity or an unreachable master returns nonzero and prints no id;
fix that error before starting blockvolume. The query does not create a
volume or prove frontend readiness.
## blockmaster settings
| Variable | Flag | Default | Meaning |