Files
seaweedfs/seaweed-block/core/frontend/memback/provider.go
T
wzcheng99 91371e5737 T1 frontend contract: core/frontend + memback + L0/L1 tests
Implements the frontend contract per T1 sketch §5-9: Identity,
Backend, Provider, ProjectionView types; ErrStalePrimary /
ErrNotReady / ErrBackendClosed sentinels; memback contract-smoke
backend that re-validates full lineage on every Read/Write.

Tests per QA test-spec:
  L0 x7 (provider open healthy + not-ready, backend read/write,
    stale read, stale write table-driven, close, boundary guard)
  L1 x1 (Publisher + VolumeBridge publication route, Epoch-bump
    move, old backend rejects, new backend serves post-move)

Two sw review notes for QA Owner:
  - Spec samples use engine.ReplicaProjection{VolumeID, ReplicaID,
    Healthy}. Actual engine type lacks those fields; per sketch §5
    the frontend defines its own Projection struct. Documented in
    file headers.
  - Spec's T1.L1.1 asserts ReplicaID change across the move.
    Cross-replica authorship requires TopologyController, not
    StaticDirective. This L1 proves Epoch-drift fence (the fence's
    actual invariant). ReplicaID-change coverage routed to L2
    over the real product route.

L2 test (T1.L2.1) not written: BLOCKED-BY-T0 per test-spec §6 —
the T0 noop executor cannot produce a frontend-serving projection
until the T1-owned readiness bridge lands in cmd/blockvolume.
2026-04-21 02:41:22 -07:00

88 lines
2.7 KiB
Go

// Package memback is the T1 contract-smoke backend
// (sketch: sw-block/design/v3-phase-15-t1-sketch.md §8).
//
// memback stores bytes in process-local memory per backend
// instance. It is NOT replicated, NOT durable, and MUST NOT be
// used to claim G8 failover data continuity. It exists to prove
// the frontend contract — healthy open, lineage-gated I/O,
// stale-primary rejection — and to be easy to remove once a
// real block backend lands.
package memback
import (
"context"
"sync"
"time"
"github.com/seaweedfs/seaweed-block/core/frontend"
)
// Provider opens memback backends against a ProjectionView.
type Provider struct {
view frontend.ProjectionView
// volumes holds one shared byte map per (volumeID, lineage).
// Keyed by the full Identity so a reopened backend on a new
// lineage starts from a clean slice — consistent with the
// explicit non-claim in sketch §8 that pre-move bytes do not
// survive on the new primary.
mu sync.Mutex
volumes map[Identity]*volumeStore
}
// Identity is the internal map key. Same layout as
// frontend.Identity; kept distinct so we never accidentally
// expose internal storage keying through the public API.
type Identity = frontend.Identity
// NewProvider constructs a Provider bound to view. view is the
// ONLY source of authority lineage the provider is allowed to
// consult — sketch §9 boundary rule.
func NewProvider(view frontend.ProjectionView) *Provider {
return &Provider{
view: view,
volumes: map[Identity]*volumeStore{},
}
}
// Open polls the projection until it reports healthy for the
// requested volumeID and snapshots the lineage into the
// backend's Identity. Returns ErrNotReady if ctx expires before
// the projection becomes healthy.
func (p *Provider) Open(ctx context.Context, volumeID string) (frontend.Backend, error) {
const poll = 10 * time.Millisecond
for {
proj := p.view.Projection()
if proj.VolumeID == volumeID && proj.Healthy {
id := frontend.Identity{
VolumeID: proj.VolumeID,
ReplicaID: proj.ReplicaID,
Epoch: proj.Epoch,
EndpointVersion: proj.EndpointVersion,
}
store := p.storeFor(id)
return newBackend(p.view, id, store), nil
}
select {
case <-ctx.Done():
return nil, frontend.ErrNotReady
case <-time.After(poll):
}
}
}
// storeFor returns the shared byte store for a given Identity,
// creating it on first use. Backends opened against the same
// lineage share a store so a round-trip write→read within a
// single (volume, replica, epoch, ev) sees its own bytes.
func (p *Provider) storeFor(id Identity) *volumeStore {
p.mu.Lock()
defer p.mu.Unlock()
s, ok := p.volumes[id]
if !ok {
s = newVolumeStore()
p.volumes[id] = s
}
return s
}