sw-block: ops chunk restore creates the restore volume (--source --snapshot --target)

sw-block ops chunk restore --source --snapshot --target creates the target
from the snapshot with Catalog.Restore (idempotent by --request-id) and, with
--status-addr naming the blockvolume that serves the target, starts the
restore driver there in the same command and prints both results. Without
--status-addr it creates the target and prints the command that starts the
driver once the target is attached. --volume keeps driving an existing
restore. README lists both forms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-21 08:43:47 -08:00
1 parent ee92613b18
commit 720fe00f23
3 files changed
+176 -10

No files matched your search

+8
View File
@@ -128,6 +128,8 @@ sw-block ops chunk cut --volume v1 --status-addr <status> # publish chang
sw-block ops chunk snapshot --volume v1 --status-addr <status> --name s1 # name a snapshot
sw-block ops chunk snapshots --volume v1 --chunk-filer <filer> # list snapshots
sw-block ops chunk fork --chunk-filer <filer> --source v1 --snapshot s1 --target v2 # root copy only
sw-block ops chunk restore --chunk-filer <filer> --source v1 --snapshot s1 --target v3 --status-addr <v3's status> # independent copy
sw-block ops chunk restore --volume v3 --status-addr <status> --status # restore progress
sw-block ops chunk detach --volume v1 --status-addr <status> # final cut and release; resume elsewhere
sw-block ops chunk volume-delete --volume v2 --chunk-filer <filer> --master <blockmaster> --request-id del-v2
sw-block ops chunk gc --chunk-filer <filer> --master <blockmaster> # preview; --apply to reclaim
@@ -135,6 +137,12 @@ sw-block ops chunk verify --volume v1 --chunk-filer <filer> # read-only che
```
`volume-delete` is refused while the volume is attached or has snapshots.
`restore` creates the target from the snapshot and, with `--status-addr` naming
the blockvolume that serves the target, starts the background restore there:
the target serves at once, copies every chunk under its own keys and becomes
independent of the source (snapshots of it are refused until then). Without
`--status-addr` it only creates the target and prints the command that starts
the restore once the target is attached.
The chunk path (`walstore-chunk`) is a proof of concept. It is not yet a
default, a release claim or a production path.
+66 -10
View File
@@ -1,30 +1,86 @@
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"time"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
"github.com/seaweedfs/seaweed-block/core/ops"
)
// chunkRestore starts (or joins) the independent-restore driver of a volume
// restored from a snapshot and prints its status; with --status it only
// prints the status. The driver runs in blockvolume and outlives this call.
// chunkRestore restores a snapshot into a new independent volume, or drives
// an existing restore.
//
// With --source, --snapshot and --target it creates the target volume from
// the snapshot (Catalog.Restore, idempotent by --request-id) and, when
// --status-addr names the blockvolume serving the target, starts the restore
// driver there and prints both results. With --volume and --status-addr it
// starts (or joins) the driver of an existing restore and prints its status;
// --status only prints the status. The driver runs in blockvolume and
// outlives this call.
func chunkRestore(args []string, stdout, stderr io.Writer) int {
fs := chunkFlagSet("restore", stderr)
var (
volumeID, statusAddr string
statusOnly bool
volumeID, statusAddr string
filer, base, source, snapshot, target, requestID string
statusOnly bool
timeout time.Duration
)
fs.StringVar(&volumeID, "volume", "", "restored volume id")
fs.StringVar(&statusAddr, "status-addr", "", "blockvolume status address or URL (with --chunk-control-endpoint)")
fs.StringVar(&volumeID, "volume", "", "restored volume id (drive an existing restore)")
fs.StringVar(&statusAddr, "status-addr", "", "blockvolume status address or URL serving the restored volume (with --chunk-control-endpoint)")
fs.BoolVar(&statusOnly, "status", false, "print the restore status without starting a run")
fs.StringVar(&source, "source", "", "source volume id (create a restore)")
fs.StringVar(&snapshot, "snapshot", "", "source snapshot name (create a restore)")
fs.StringVar(&target, "target", "", "new volume id (create a restore)")
fs.StringVar(&requestID, "request-id", "", "idempotency id for the create; generated and printed when empty (reuse it to retry)")
fs.StringVar(&filer, "chunk-filer", "", "SeaweedFS filer gRPC address host:port (create a restore)")
fs.StringVar(&base, "chunk-path", defaultChunkPathCLI, "filer directory holding one directory per volume")
fs.DurationVar(&timeout, "timeout", 6*time.Minute, "filer timeout for the create")
if err := fs.Parse(args); err != nil {
return chunkFail(chunkName(fs), stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false)
}
if volumeID == "" || statusAddr == "" {
return chunkFail("restore", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume and --status-addr are required", false)
create := source != "" || snapshot != "" || target != ""
switch {
case create && volumeID != "":
return chunkFail("restore", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "use --source/--snapshot/--target to create a restore, or --volume to drive one, not both", false)
case create && (source == "" || snapshot == "" || target == ""):
return chunkFail("restore", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--source, --snapshot and --target are required to create a restore", false)
case create && statusOnly:
return chunkFail("restore", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--status reads an existing restore: use it with --volume", false)
case !create && (volumeID == "" || statusAddr == ""):
return chunkFail("restore", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume and --status-addr are required (or --source, --snapshot and --target to create a restore)", false)
}
body := map[string]any{"volumeID": volumeID, "statusOnly": statusOnly}
if !create {
body := map[string]any{"volumeID": volumeID, "statusOnly": statusOnly}
return chunkPost("restore", statusAddr, "/chunk/restore", 30*time.Second, body, stdout, stderr)
}
if requestID == "" {
requestID = newRequestID()
fmt.Fprintf(stderr, "sw-block ops chunk restore: request id %s (reuse it to retry)\n", requestID)
}
cat, closeFn, exit := chunkCatalog("restore", filer, base, true, timeout, stdout, stderr)
if exit >= 0 {
return exit
}
defer closeFn()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
res, err := cat.Restore(ctx, chunkstore.ForkRequest{Source: source, Snapshot: snapshot, Target: target, RequestID: requestID})
if err != nil || statusAddr == "" {
if err == nil {
fmt.Fprintf(stderr, "sw-block ops chunk restore: %s created; attach it, then run: sw-block ops chunk restore --volume %s --status-addr <its blockvolume>\n", target, target)
}
return chunkResult("restore", res, err, stdout, stderr)
}
created, err := json.Marshal(res)
if err != nil {
return chunkFail("restore", stdout, stderr, chunkExitFailed, codeInternalCLI, err.Error(), false)
}
fmt.Fprintf(stdout, "%s\n", created)
body := map[string]any{"volumeID": target}
return chunkPost("restore", statusAddr, "/chunk/restore", 30*time.Second, body, stdout, stderr)
}
@@ -2,11 +2,16 @@ package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
"github.com/seaweedfs/seaweed-block/core/storage"
)
func TestOpsChunk_RestoreStartsAndReportsStatus(t *testing.T) {
@@ -37,3 +42,100 @@ func TestOpsChunk_RestoreStartsAndReportsStatus(t *testing.T) {
t.Fatal("restore without --volume must be refused")
}
}
// restoreSource publishes volume src with one chunk and snapshot s1 in a
// fresh store, and routes the CLI's filer dial to it.
func restoreSource(t *testing.T, base string) (*gcCLIStore, *chunkstore.Catalog) {
t.Helper()
ctx := context.Background()
store := &gcCLIStore{MemFiler: chunkstore.NewMemFiler()}
old := dialChunkFiler
t.Cleanup(func() { dialChunkFiler = old })
dialChunkFiler = func(string) (chunkstore.Filer, func() error, error) { return store, func() error { return nil }, nil }
cat, err := chunkstore.NewCatalog(store, base)
if err != nil {
t.Fatal(err)
}
geo := chunkstore.Geometry{NumBlocks: 64, BlockSize: 4096, ChunkBlocks: 8}
if _, err := cat.EnsureVolume(ctx, "src", geo); err != nil {
t.Fatal(err)
}
r, err := chunkstore.NewChunkRemote(store, store, base+"/src")
if err != nil {
t.Fatal(err)
}
cur, err := r.GetHead(ctx)
if err != nil {
t.Fatal(err)
}
data := make([]byte, 8*4096)
data[0] = 0x7
if err := r.Put(ctx, "chunks/e1/c0/k", data); err != nil {
t.Fatal(err)
}
h := storage.ChunkHead{Epoch: 1, Holder: "h1", RemoteLSN: 5, Chunks: map[uint64]storage.ChunkRef{0: {Key: "chunks/e1/c0/k", SHA256: sha256.Sum256(data)}}}
h, ids, err := r.PrepareManifest(ctx, cur, h, 8*4096)
if err != nil {
t.Fatal(err)
}
if h.Gen, err = r.CASHead(ctx, storage.ChunkHeadVersion{Gen: cur.Gen, Epoch: cur.Epoch}, h); err != nil {
t.Fatal(err)
}
for _, id := range ids {
_ = r.DeleteIntent(ctx, id)
}
if _, err := cat.CreateSnapshot(ctx, "src", chunkstore.SnapshotRequest{Name: "s1", RequestID: "r"}, h); err != nil {
t.Fatal(err)
}
return store, cat
}
// --source/--snapshot/--target creates the restore volume (idempotent by
// request id); without --status-addr it says how to start the driver.
func TestOpsChunk_RestoreCreatesTheTarget(t *testing.T) {
_, cat := restoreSource(t, "/rs")
args := []string{"restore", "--source", "src", "--snapshot", "s1", "--target", "dst", "--request-id", "rq", "--chunk-filer", "x", "--chunk-path", "/rs"}
code, out, errs := runChunk(args...)
if code != 0 || !strings.Contains(out, `"dst"`) || !strings.Contains(errs, "--volume dst") {
t.Fatalf("create: exit %d out %s err %s", code, out, errs)
}
v, ok, err := cat.ReadVolume(context.Background(), "dst")
if err != nil || !ok || v.Origin != chunkstore.OriginRestore {
t.Fatalf("target: %+v ok=%v %v, want a restore volume", v, ok, err)
}
if code, out, errs := runChunk(args...); code != 0 {
t.Fatalf("retry with the same request id: exit %d out %s err %s", code, out, errs)
}
for _, bad := range [][]string{
{"restore", "--source", "src", "--target", "dst", "--chunk-filer", "x"},
{"restore", "--source", "src", "--snapshot", "s1", "--target", "dst", "--volume", "dst", "--chunk-filer", "x"},
{"restore", "--source", "src", "--snapshot", "s1", "--target", "dst", "--status", "--chunk-filer", "x"},
} {
if code, _, _ := runChunk(bad...); code == 0 {
t.Fatalf("%v accepted", bad)
}
}
}
// With --status-addr the create then starts the driver for the target on
// that blockvolume, in one command.
func TestOpsChunk_RestoreCreatesAndStartsTheDriver(t *testing.T) {
_, cat := restoreSource(t, "/rs2")
var got map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = json.NewDecoder(r.Body).Decode(&got)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"volumeID":"dst","phase":"hydrating","running":true}`))
}))
defer srv.Close()
code, out, errs := runChunk("restore", "--source", "src", "--snapshot", "s1", "--target", "dst", "--request-id", "rq", "--chunk-filer", "x", "--chunk-path", "/rs2", "--status-addr", srv.URL)
if code != 0 || !strings.Contains(out, `"phase":"hydrating"`) {
t.Fatalf("create and start: exit %d out %s err %s", code, out, errs)
}
if got["volumeID"] != "dst" || got["statusOnly"] != nil {
t.Fatalf("driver request %v, want a start for dst", got)
}
if _, ok, _ := cat.ReadVolume(context.Background(), "dst"); !ok {
t.Fatal("the target was not created before the driver start")
}
}