mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
Whole-job commands over existing calls, with no state of their own: - volume backup: the holder's snapshot, request id derived from the volume and the name; the same command resumes the same request. - volume restore: the master's restore create (request id derived from target, source and snapshot), then binding and placement once the target is store-ready, then volume start's steps. - volume restore --abort: stop and disable the target's unit, remove its settings, then delete it (volume-delete with HEAD, release which tombstones without HEAD); the dependent is released only after the target can no longer serve. Every failure names the exact command to rerun or to abort. bindAndPlace moves out of ops volume create so both use the same two calls. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
5.8 KiB
Go
116 lines
5.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"regexp"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
|
|
"github.com/seaweedfs/seaweed-block/core/lifecycle"
|
|
"github.com/seaweedfs/seaweed-block/core/rpc/control"
|
|
"github.com/seaweedfs/seaweed-block/core/rpc/link"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// S1 composes existing receiver admissions. It does not issue a lease or
|
|
// decide whether the target is serving, and never rolls back a partial result.
|
|
func runOpsVolumeCreate(args []string, stdout, stderr io.Writer) int {
|
|
const usage = "usage: sw-block ops volume create --master ADDR --size-bytes N --server-id HOST --replica-id REPLICA --data-addr HOST:PORT --ctrl-addr HOST:PORT [--protocol iscsi|nvme] [--transport tcp|rdma] [--timeout 10s] NAME"
|
|
fs := flag.NewFlagSet("sw-block ops volume create", flag.ContinueOnError)
|
|
fs.SetOutput(stderr)
|
|
fs.Usage = func() { fmt.Fprintln(stderr, usage) }
|
|
master := fs.String("master", "", "master gRPC address")
|
|
size := fs.Uint64("size-bytes", 0, "volume size in bytes (multiple of 4096)")
|
|
protocol := fs.String("protocol", "nvme", "nvme (default) or iscsi")
|
|
transport := fs.String("transport", "", "NVMe transport: tcp (default) or rdma")
|
|
server := fs.String("server-id", "", "already registered host")
|
|
replica := fs.String("replica-id", "", "replica identity")
|
|
data := fs.String("data-addr", "", "per-volume data endpoint")
|
|
ctrl := fs.String("ctrl-addr", "", "per-volume control endpoint")
|
|
timeout := fs.Duration("timeout", 10*time.Second, "deadline for the entire operation")
|
|
if err := fs.Parse(args); err != nil {
|
|
return 2
|
|
}
|
|
if *protocol == "nvme" && *transport == "" {
|
|
*transport = "tcp"
|
|
}
|
|
validID := regexp.MustCompile(`^[A-Za-z0-9._-]+$`)
|
|
binding := &control.WorkspaceBinding{VolumeId: fs.Arg(0), ServerId: *server, DataAddr: *data, CtrlAddr: *ctrl}
|
|
geometry := layout.Geometry{SizeBytes: *size, BlockSize: 4096, ChunkBytes: 4 << 20}
|
|
if fs.NArg() != 1 || *master == "" || *timeout <= 0 || !validID.MatchString(fs.Arg(0)) || !validID.MatchString(*server) || !validID.MatchString(*replica) || geometry.Check() != nil ||
|
|
!(*protocol == "iscsi" && *transport == "" || *protocol == "nvme" && (*transport == "tcp" || *transport == "rdma")) {
|
|
fs.Usage()
|
|
return 2
|
|
}
|
|
if err := lifecycle.ValidateWorkspaceEndpoints(lifecycle.WorkspaceBinding{VolumeID: binding.VolumeId, ServerID: *server, DataAddr: *data, CtrlAddr: *ctrl}); err != nil {
|
|
fmt.Fprintf(stderr, "sw-block ops volume create: %v\n", err)
|
|
return 2
|
|
}
|
|
conn, err := link.DialMasterFromEnv(*master)
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "sw-block ops volume create: connection configuration: %v\n", err)
|
|
return 2
|
|
}
|
|
defer conn.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
|
|
defer cancel()
|
|
fail := func(step string, err error) int {
|
|
code, outcome := 78, "refused"
|
|
switch status.Code(err) {
|
|
case codes.Unavailable, codes.DeadlineExceeded, codes.Canceled, codes.Unknown, codes.Internal, codes.DataLoss:
|
|
code, outcome = 75, "unknown"
|
|
}
|
|
fmt.Fprintf(stderr, "sw-block ops volume create: %s %s: %v; earlier steps may remain recorded; retry the identical name, geometry and placement\n", step, outcome, err)
|
|
return code
|
|
}
|
|
q := &control.CreateVolumeRequest{VolumeId: binding.VolumeId, SizeBytes: *size, ReplicationFactor: 1, Protocol: *protocol, FrontendTransport: *transport, BlockSize: geometry.BlockSize, ChunkBytes: geometry.ChunkBytes}
|
|
v, err := control.NewLifecycleServiceClient(conn).CreateVolume(ctx, q)
|
|
if err != nil {
|
|
return fail("create", err)
|
|
}
|
|
if _, err := layout.ParseVolumeID(v.GetLayoutId()); err != nil || v.GetVolumeId() != q.VolumeId || v.GetSizeBytes() != q.SizeBytes || v.GetReplicationFactor() != 1 || v.GetProtocol() != q.Protocol || v.GetFrontendTransport() != q.FrontendTransport || v.GetBlockSize() != q.BlockSize || v.GetChunkBytes() != q.ChunkBytes {
|
|
return fail("create reply", status.Error(codes.FailedPrecondition, "identity or geometry does not match; no binding or placement attempted"))
|
|
}
|
|
if !v.GetStoreReady() {
|
|
fmt.Fprintln(stderr, "sw-block ops volume create: created, store not ready; no binding or placement attempted; retry the identical name, geometry and placement")
|
|
return 75
|
|
}
|
|
if step, err := bindAndPlace(ctx, control.NewWorkspaceServiceClient(conn), binding, *replica); err != nil {
|
|
return fail(step, err)
|
|
}
|
|
if err := json.NewEncoder(stdout).Encode(struct {
|
|
Status string `json:"status"`
|
|
Volume string `json:"volume"`
|
|
LayoutID string `json:"layout_id"`
|
|
ServerID string `json:"server_id"`
|
|
ReplicaID string `json:"replica_id"`
|
|
}{"placement_recorded", q.VolumeId, v.LayoutId, *server, *replica}); err != nil {
|
|
fmt.Fprintf(stderr, "sw-block ops volume create: placement recorded, output failed: %v\n", err)
|
|
return 75
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// bindAndPlace records a created volume's workspace binding and its
|
|
// placement on the binding's server, the two workspace calls ops volume
|
|
// create and volume restore both run. A binding answer that differs from
|
|
// the request stops before the placement. step names the failed call.
|
|
func bindAndPlace(ctx context.Context, ws control.WorkspaceServiceClient, binding *control.WorkspaceBinding, replica string) (step string, err error) {
|
|
b, err := ws.RegisterWorkspaceBinding(ctx, binding)
|
|
if err != nil {
|
|
return "binding", err
|
|
}
|
|
if b.GetVolumeId() != binding.VolumeId || b.GetServerId() != binding.ServerId || b.GetDataAddr() != binding.DataAddr || b.GetCtrlAddr() != binding.CtrlAddr || b.GetRetired() {
|
|
return "binding reply", status.Error(codes.FailedPrecondition, "binding does not match; no placement attempted")
|
|
}
|
|
if _, err := ws.PlaceWorkspace(ctx, &control.WorkspacePlacement{VolumeId: binding.VolumeId, ServerId: binding.ServerId, ReplicaId: replica}); err != nil {
|
|
return "placement", err
|
|
}
|
|
return "", nil
|
|
}
|