diff --git a/seaweed-block/cmd/blockvolume/chunk_remote.go b/seaweed-block/cmd/blockvolume/chunk_remote.go index 825ef0c517..57f77a5b99 100644 --- a/seaweed-block/cmd/blockvolume/chunk_remote.go +++ b/seaweed-block/cmd/blockvolume/chunk_remote.go @@ -6,6 +6,7 @@ package main // given by --chunk-filer; chunk bytes are needles on its volume servers. import ( + "context" "errors" "fmt" "path" @@ -13,6 +14,7 @@ import ( "time" "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore" + "github.com/seaweedfs/seaweed-block/core/frontend/durable" "github.com/seaweedfs/seaweed-block/core/storage" ) @@ -49,9 +51,17 @@ func validateChunkFlags(f flags) error { 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) { +// chunkWiring is what --durable-impl=walstore-chunk dials once: the +// provider's per-volume remotes, the catalog under the same base (VOLUME +// entries, snapshots) and the client's closer. +type chunkWiring struct { + remoteFor func(volumeID string) (storage.ChunkRemote, error) + catalog *chunkstore.Catalog + close func() error +} + +// newChunkWiring dials the filer (lazily). +func newChunkWiring(c chunkFlags) (chunkWiring, error) { client, err := chunkstore.DialSWFS(chunkstore.SWFSConfig{ FilerGRPC: c.filer, Collection: c.collection, @@ -59,15 +69,35 @@ func newChunkRemoteFor(c chunkFlags) (func(volumeID string) (storage.ChunkRemote AssignPath: c.path, }) if err != nil { - return nil, nil, err + return chunkWiring{}, err + } + cat, err := chunkstore.NewCatalog(client, c.path) + if err != nil { + _ = client.Close() + return chunkWiring{}, err + } + return chunkWiring{ + remoteFor: func(volumeID string) (storage.ChunkRemote, error) { + root, err := chunkVolumeRoot(c.path, volumeID) + if err != nil { + return nil, err + } + return chunkstore.NewSWFSChunkRemote(client, root) + }, + catalog: cat, + close: client.Close, + }, nil +} + +// chunkAttachCheck applies the VOLUME entry's attach rules before the chunk +// layer attaches (B9 W2 §4.2): create VOLUME from the local geometry when +// it is missing, refuse geometry_mismatch, and refuse fork_incomplete for a +// fork whose HEAD is not written yet. +func chunkAttachCheck(cat *chunkstore.Catalog) func(context.Context, string, durable.ChunkGeometry) error { + return func(ctx context.Context, volumeID string, g durable.ChunkGeometry) error { + _, err := cat.EnsureVolume(ctx, volumeID, chunkstore.Geometry{NumBlocks: g.NumBlocks, BlockSize: g.BlockSize, ChunkBlocks: g.ChunkBlocks}) + return 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 diff --git a/seaweed-block/cmd/blockvolume/chunk_remote_test.go b/seaweed-block/cmd/blockvolume/chunk_remote_test.go index 2b5c568bc0..25cd87f4a7 100644 --- a/seaweed-block/cmd/blockvolume/chunk_remote_test.go +++ b/seaweed-block/cmd/blockvolume/chunk_remote_test.go @@ -1,9 +1,13 @@ package main import ( + "context" "strings" "testing" "time" + + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore" + "github.com/seaweedfs/seaweed-block/core/frontend/durable" ) func TestWALStoreChunkFlags(t *testing.T) { @@ -47,3 +51,31 @@ func TestChunkVolumeRootStaysInsideBase(t *testing.T) { } } } + +// The attach check is the VOLUME entry's attach rules (B9 W2 §4.2): the first +// attach creates VOLUME from the local geometry, a later attach with the same +// geometry passes, and another geometry is refused. +func TestChunkAttachCheckAppliesVolumeRules(t *testing.T) { + ctx := context.Background() + cat, err := chunkstore.NewCatalog(chunkstore.NewMemFiler(), defaultChunkPath) + if err != nil { + t.Fatal(err) + } + check := chunkAttachCheck(cat) + g := durable.ChunkGeometry{NumBlocks: 64, BlockSize: 4096, ChunkBlocks: 8} + if err := check(ctx, "v1", g); err != nil { + t.Fatalf("first attach: %v", err) + } + v, ok, err := cat.ReadVolume(ctx, "v1") + if err != nil || !ok || v.Origin != chunkstore.OriginCreate || v.Geometry != (chunkstore.Geometry{NumBlocks: 64, BlockSize: 4096, ChunkBlocks: 8}) { + t.Fatalf("VOLUME after the first attach: %+v ok=%v %v", v, ok, err) + } + if err := check(ctx, "v1", g); err != nil { + t.Fatalf("attach with the same geometry: %v", err) + } + other := g + other.NumBlocks = 128 + if err := check(ctx, "v1", other); chunkstore.CodeOf(err) != chunkstore.CodeGeometryMismatch { + t.Fatalf("attach with another geometry: %v, want geometry_mismatch", err) + } +} diff --git a/seaweed-block/cmd/blockvolume/main.go b/seaweed-block/cmd/blockvolume/main.go index 573380023b..bf0f28a482 100644 --- a/seaweed-block/cmd/blockvolume/main.go +++ b/seaweed-block/cmd/blockvolume/main.go @@ -544,12 +544,14 @@ func run(f flags) int { WALRecoveryTestDisableFlusher: f.durableWALRecoveryTestDisableFlusher, } var chunkErr error + var chunk chunkWiring if cfg.Impl == durable.ImplWALStoreChunk { - var closeChunkClient func() error - cfg.ChunkRemoteFor, closeChunkClient, chunkErr = newChunkRemoteFor(f.chunk) + chunk, chunkErr = newChunkWiring(f.chunk) cfg.ChunkCaptureInterval = f.chunk.captureInterval if chunkErr == nil { - defer closeChunkClient() + cfg.ChunkRemoteFor = chunk.remoteFor + cfg.ChunkAttachCheck = chunkAttachCheck(chunk.catalog) + defer chunk.close() } } dp, err := durable.NewDurableProvider(cfg, h.ProjectionView())