mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
122 lines
5.9 KiB
Go
122 lines
5.9 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/answer"
|
|
"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 {
|
|
policy, detail := operatorAnswer(err, answer.VolumeCreate)
|
|
fmt.Fprintf(stderr, "sw-block ops volume create: connection configuration: %v\n", detail)
|
|
return policy.Exit
|
|
}
|
|
defer conn.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
|
|
defer cancel()
|
|
fail := func(step string, err error) int {
|
|
policy, check := answer.PolicyOf(answer.EnsureEvidence(answer.PossiblyAdmitted, err), answer.VolumeCreate)
|
|
if check != nil {
|
|
fmt.Fprintf(stderr, "sw-block ops volume create: %s: %v\n", step, check)
|
|
return 75
|
|
}
|
|
outcome := "refused"
|
|
if policy.Retryable {
|
|
outcome = "unknown"
|
|
}
|
|
fmt.Fprintf(stderr, "sw-block ops volume create: %s %s: %v; %s\n", step, outcome, err, policy.Cure)
|
|
return policy.Exit
|
|
}
|
|
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
|
|
}
|