diff --git a/seaweed-block/cmd/blockvolume/chunk_remote.go b/seaweed-block/cmd/blockvolume/chunk_remote.go new file mode 100644 index 0000000000..825ef0c517 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/chunk_remote.go @@ -0,0 +1,80 @@ +package main + +// --durable-impl=walstore-chunk: walstore with the chunk layer publishing +// the volume to SeaweedFS (opt-in proof of concept). Each volume's HEAD and +// chunk key bindings live under <--chunk-path>/ on the filer +// given by --chunk-filer; chunk bytes are needles on its volume servers. + +import ( + "errors" + "fmt" + "path" + "strings" + "time" + + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore" + "github.com/seaweedfs/seaweed-block/core/storage" +) + +const defaultChunkPath = "/swblock/chunks" + +type chunkFlags struct { + filer string + path string + collection string + replication string + captureInterval time.Duration +} + +func validateChunkFlags(f flags) error { + c := f.chunk + if f.durableImpl != "walstore-chunk" { + if c.filer != "" || c.collection != "" || c.replication != "" || c.captureInterval != 0 || c.path != defaultChunkPath { + return errors.New("--chunk-* flags require --durable-impl=walstore-chunk") + } + return nil + } + if f.durableRoot == "" { + return errors.New("--durable-impl=walstore-chunk requires --durable-root") + } + if c.filer == "" { + return errors.New("--durable-impl=walstore-chunk requires --chunk-filer") + } + if !path.IsAbs(c.path) || path.Clean(c.path) != c.path || c.path == "/" { + return fmt.Errorf("--chunk-path=%q must be a clean absolute filer directory other than /", c.path) + } + if c.captureInterval < 0 { + return fmt.Errorf("--chunk-capture-interval=%s must not be negative", c.captureInterval) + } + return nil +} + +// newChunkRemoteFor dials the filer (lazily) and returns the provider's +// ChunkRemoteFor plus the client's closer. +func newChunkRemoteFor(c chunkFlags) (func(volumeID string) (storage.ChunkRemote, error), func() error, error) { + client, err := chunkstore.DialSWFS(chunkstore.SWFSConfig{ + FilerGRPC: c.filer, + Collection: c.collection, + Replication: c.replication, + AssignPath: c.path, + }) + if err != nil { + return nil, nil, err + } + return func(volumeID string) (storage.ChunkRemote, error) { + root, err := chunkVolumeRoot(c.path, volumeID) + if err != nil { + return nil, err + } + return chunkstore.NewSWFSChunkRemote(client, root) + }, client.Close, nil +} + +// chunkVolumeRoot is /; the volume ID must be one plain +// path element so no volume can reach another volume's directory. +func chunkVolumeRoot(base, volumeID string) (string, error) { + if volumeID == "" || volumeID == "." || volumeID == ".." || strings.ContainsAny(volumeID, "/\\") { + return "", fmt.Errorf("blockvolume: volume id %q is not a plain path element", volumeID) + } + return path.Join(base, volumeID), nil +} diff --git a/seaweed-block/cmd/blockvolume/chunk_remote_test.go b/seaweed-block/cmd/blockvolume/chunk_remote_test.go new file mode 100644 index 0000000000..2b5c568bc0 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/chunk_remote_test.go @@ -0,0 +1,49 @@ +package main + +import ( + "strings" + "testing" + "time" +) + +func TestWALStoreChunkFlags(t *testing.T) { + base := func(extra ...string) []string { + return append(phase150RequiredBlockvolumeArgs(), extra...) + } + f, err := parseFlags(base("--durable-root=/tmp/sw-block", "--durable-impl=walstore-chunk", + "--chunk-filer=127.0.0.1:18888", "--chunk-capture-interval=30s")) + if err != nil { + t.Fatalf("walstore-chunk with a filer: %v", err) + } + if f.chunk.filer != "127.0.0.1:18888" || f.chunk.path != defaultChunkPath || f.chunk.captureInterval != 30*time.Second { + t.Fatalf("parsed chunk flags = %+v", f.chunk) + } + refused := map[string][]string{ + "no filer": base("--durable-root=/tmp/sw-block", "--durable-impl=walstore-chunk"), + "no durable root": base("--durable-impl=walstore-chunk", "--chunk-filer=127.0.0.1:18888"), + "relative path": base("--durable-root=/tmp/sw-block", "--durable-impl=walstore-chunk", "--chunk-filer=x:1", "--chunk-path=swblock"), + "root path": base("--durable-root=/tmp/sw-block", "--durable-impl=walstore-chunk", "--chunk-filer=x:1", "--chunk-path=/"), + "negative interval": base("--durable-root=/tmp/sw-block", "--durable-impl=walstore-chunk", "--chunk-filer=x:1", "--chunk-capture-interval=-1s"), + "chunk flag on walstore": base("--durable-root=/tmp/sw-block", "--durable-impl=walstore", "--chunk-filer=x:1"), + "chunk path on walstore": base("--durable-root=/tmp/sw-block", "--durable-impl=walstore", "--chunk-path=/other"), + } + for name, args := range refused { + if _, err := parseFlags(args); err == nil { + t.Errorf("%s: parseFlags succeeded; want an error", name) + } + } +} + +func TestChunkVolumeRootStaysInsideBase(t *testing.T) { + root, err := chunkVolumeRoot(defaultChunkPath, "v1") + if err != nil || root != defaultChunkPath+"/v1" { + t.Fatalf("chunkVolumeRoot(v1) = %q, %v", root, err) + } + for _, id := range []string{"", ".", "..", "a/b", "../v2", `a\b`} { + if r, err := chunkVolumeRoot(defaultChunkPath, id); err == nil { + t.Errorf("volume id %q mapped to %q; want refusal", id, r) + } else if !strings.Contains(err.Error(), "plain path element") { + t.Errorf("volume id %q: unexpected error %v", id, err) + } + } +} diff --git a/seaweed-block/cmd/blockvolume/main.go b/seaweed-block/cmd/blockvolume/main.go index 8c856dbf2b..573380023b 100644 --- a/seaweed-block/cmd/blockvolume/main.go +++ b/seaweed-block/cmd/blockvolume/main.go @@ -96,8 +96,10 @@ type flags struct { // parallel-walstore candidate (default smartwal). // --durable-blocks + --durable-blocksize are used on first-time // storage create. - durableRoot string - durableImpl string // "smartwal" | "walstore" | "parallel-walstore" + durableRoot string + durableImpl string // "smartwal" | "walstore" | "parallel-walstore" | "walstore-chunk" + // --chunk-*: the SeaweedFS remote of --durable-impl=walstore-chunk. + chunk chunkFlags durableBlocks uint durableBlockSize uint durableWALMultiBlockRecords bool @@ -167,7 +169,12 @@ func parseFlags(args []string) (flags, error) { fs.UintVar(&f.nvmeMaxH2C, "nvme-max-h2c-data-length", 0, "NVMe/TCP MaxH2CDataLength in bytes. 0 preserves the target default") fs.BoolVar(&f.allowExternalNVMeBind, "allow-external-nvme-bind", false, "allow NVMe to bind a non-loopback address; unauthenticated, intended only for explicit gates") fs.StringVar(&f.durableRoot, "durable-root", "", "directory for persistent storage files; empty = memback (non-durable)") - fs.StringVar(&f.durableImpl, "durable-impl", "smartwal", "LogicalStorage impl: smartwal (default), walstore, or opt-in parallel-walstore; ignored unless --durable-root is set") + fs.StringVar(&f.durableImpl, "durable-impl", "smartwal", "LogicalStorage impl: smartwal (default), walstore, opt-in parallel-walstore, or opt-in walstore-chunk (walstore published to SeaweedFS; proof of concept); ignored unless --durable-root is set") + fs.StringVar(&f.chunk.filer, "chunk-filer", "", "walstore-chunk: SeaweedFS filer gRPC address host:port") + fs.StringVar(&f.chunk.path, "chunk-path", defaultChunkPath, "walstore-chunk: filer directory holding one directory per volume (HEAD and chunk key bindings)") + fs.StringVar(&f.chunk.collection, "chunk-collection", "", "walstore-chunk: collection for chunk needles (empty = filer default / storage rule)") + fs.StringVar(&f.chunk.replication, "chunk-replication", "", "walstore-chunk: replication for chunk needles (empty = filer default / storage rule)") + fs.DurationVar(&f.chunk.captureInterval, "chunk-capture-interval", 0, "walstore-chunk: publish a cut every interval while healthy; 0 = only on demand") fs.UintVar(&f.durableBlocks, "durable-blocks", 2048, "number of blocks per volume on first create (ignored when opening existing)") fs.UintVar(&f.durableBlockSize, "durable-blocksize", 4096, "block size in bytes on first create") fs.BoolVar(&f.durableWALMultiBlockRecords, "durable-wal-multiblock-records", false, "enable experimental walstore multi-block WAL records; default false; requires --durable-impl=walstore") @@ -217,9 +224,12 @@ func parseFlags(args []string) (flags, error) { return flags{}, err } switch f.durableImpl { - case "smartwal", "walstore", "parallel-walstore": + case "smartwal", "walstore", "parallel-walstore", "walstore-chunk": default: - return flags{}, fmt.Errorf("--durable-impl=%q invalid; want smartwal, walstore, or parallel-walstore", f.durableImpl) + return flags{}, fmt.Errorf("--durable-impl=%q invalid; want smartwal, walstore, parallel-walstore, or walstore-chunk", f.durableImpl) + } + if err := validateChunkFlags(f); err != nil { + return flags{}, err } if f.durableWALMultiBlockRecords { if f.durableRoot == "" { @@ -533,7 +543,19 @@ func run(f flags) int { WALMultiBlockRecords: f.durableWALMultiBlockRecords, WALRecoveryTestDisableFlusher: f.durableWALRecoveryTestDisableFlusher, } + var chunkErr error + if cfg.Impl == durable.ImplWALStoreChunk { + var closeChunkClient func() error + cfg.ChunkRemoteFor, closeChunkClient, chunkErr = newChunkRemoteFor(f.chunk) + cfg.ChunkCaptureInterval = f.chunk.captureInterval + if chunkErr == nil { + defer closeChunkClient() + } + } dp, err := durable.NewDurableProvider(cfg, h.ProjectionView()) + if chunkErr != nil { + err = chunkErr + } if err != nil { fmt.Fprintln(os.Stderr, "blockvolume: durable provider:", err) if status != nil {