mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
storage: validate dark map publication against merged coverage and volatile proof
This commit is contained in:
1 parent
f0fd2916cc
commit
cb8a0fb819
3 files changed
+409
-3
No files matched your search
@@ -0,0 +1,131 @@
|
||||
package storage
|
||||
|
||||
// Dark row-7 map publication/proof controls. These are NOT flusher/provider
|
||||
// integration: the row-5b real WALStore must call the lifecycle invalidations
|
||||
// and prepare from its permitted quiesced mutation snapshot before activation.
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var ErrMapCoverageStale = errors.New("storage: v3 chunk map coverage or proof stale")
|
||||
|
||||
// Each open has a distinct domain pointer. Epochs never wrap. Recover must
|
||||
// invalidate at START and at END (also on failure); reset and every install
|
||||
// transition invalidate too. No store/map lock is taken by an invalidation.
|
||||
// The owner must serialize these lifecycle transitions against descriptor
|
||||
// publication using its existing lifecycle admission; this type adds no lock.
|
||||
type mapProofEpochV3 struct{ epoch, mutation atomic.Uint64 }
|
||||
|
||||
func newMapProofEpochV3() *mapProofEpochV3 { d := &mapProofEpochV3{}; d.epoch.Store(1); return d }
|
||||
func advanceMapProofCounterV3(v *atomic.Uint64) error {
|
||||
for {
|
||||
n := v.Load()
|
||||
if n >= counterMaxV3 {
|
||||
return ErrCounterExhausted
|
||||
}
|
||||
if v.CompareAndSwap(n, n+1) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
func (d *mapProofEpochV3) invalidateV3() error { return advanceMapProofCounterV3(&d.epoch) }
|
||||
func (d *mapProofEpochV3) mutatedV3() error { return advanceMapProofCounterV3(&d.mutation) }
|
||||
|
||||
type mapPublicationProofV3 struct {
|
||||
domain *mapProofEpochV3
|
||||
epoch, mutation uint64
|
||||
}
|
||||
|
||||
func (d *mapProofEpochV3) snapshotV3() mapPublicationProofV3 {
|
||||
return mapPublicationProofV3{domain: d, epoch: d.epoch.Load(), mutation: d.mutation.Load()}
|
||||
}
|
||||
func (p mapPublicationProofV3) validV3(d *mapProofEpochV3) bool {
|
||||
return d != nil && p.domain == d && p.epoch < counterMaxV3 && p.mutation < counterMaxV3 && p.epoch == d.epoch.Load() && p.mutation == d.mutation.Load()
|
||||
}
|
||||
|
||||
type preparedMapPublicationV3 struct {
|
||||
candidate *chunkMapCandidateV3
|
||||
proof mapPublicationProofV3
|
||||
}
|
||||
|
||||
// The caller supplies entries from its permitted mutation/version boundary.
|
||||
// Preparation takes the sidecar mutex and I/O only, before store/desc locks.
|
||||
func prepareMapPublicationV3(m *chunkLSNMapV3, d *mapProofEpochV3, g, C uint64, entries []chunkLSNEntryV3, prior *chunkMapCandidateV3) (*preparedMapPublicationV3, error) {
|
||||
if m == nil || d == nil {
|
||||
return nil, ErrMapCoverageStale
|
||||
}
|
||||
proof := d.snapshotV3()
|
||||
c, e := m.Prepare(g, C, entries, prior)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
if !proof.validV3(d) {
|
||||
return nil, ErrMapCoverageStale
|
||||
}
|
||||
return &preparedMapPublicationV3{candidate: c, proof: proof}, nil
|
||||
}
|
||||
func sameMapTupleV3(d descriptorV3, c *chunkMapCandidateV3) bool {
|
||||
if c == nil || c.owner == nil {
|
||||
return false
|
||||
}
|
||||
id := c.owner.identity
|
||||
return d.UUID == id.UUID && sameGeometryV3(d, id) && d.MapLineage == id.MapLineage && d.MapFormatID == mapFormatIDV1 && d.MapGeneration == c.generation && d.MapCoveredCheckpoint == c.covered && d.MapRootLocator == c.rootOffset && d.MapRootHash == c.hash
|
||||
}
|
||||
|
||||
// bindMapV3 is used only while opening/quiesced. Dependencies are read before
|
||||
// descMu. The final descriptor comparison under descMu prevents a stale bind.
|
||||
func (p *descriptorPublisherV3) bindMapV3(c *chunkMapCandidateV3, d *mapProofEpochV3) error {
|
||||
if c == nil || c.owner == nil || d == nil {
|
||||
return ErrMapCoverageStale
|
||||
}
|
||||
tuple := c.descriptor(c.owner.identity)
|
||||
if _, _, e := readChunkLSNMapV3(c.owner.fd, tuple, c.owner.chunkBlocks); e != nil {
|
||||
return e
|
||||
}
|
||||
p.descMu.Lock()
|
||||
defer p.descMu.Unlock()
|
||||
if !sameMapTupleV3(p.cur, c) {
|
||||
return ErrMapCoverageStale
|
||||
}
|
||||
p.mapCurrent = c
|
||||
p.mapDomain = d
|
||||
return nil
|
||||
}
|
||||
func (p *descriptorPublisherV3) validateMapPublicationV3(next descriptorV3, prepared *preparedMapPublicationV3) error {
|
||||
if p.mapCurrent == nil {
|
||||
if prepared != nil {
|
||||
return ErrMapCoverageStale
|
||||
}
|
||||
return nil
|
||||
} // dark core not bound yet
|
||||
c := p.mapCurrent
|
||||
if prepared != nil {
|
||||
c = prepared.candidate
|
||||
if c == nil || c.owner != p.mapCurrent.owner || !prepared.proof.validV3(p.mapDomain) {
|
||||
return ErrMapCoverageStale
|
||||
}
|
||||
if c.generation <= p.mapCurrent.generation || c.covered < p.mapCurrent.covered {
|
||||
return ErrMapCoverageStale
|
||||
}
|
||||
}
|
||||
if !sameMapTupleV3(next, c) || c.covered < next.CheckpointLSN || c.covered > next.W {
|
||||
return ErrMapCoverageStale
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (p *descriptorPublisherV3) publishMapV3(storeMu sync.Locker, snap func(), prepared *preparedMapPublicationV3, mutate func(*descriptorV3) error) (descriptorV3, error) {
|
||||
if prepared == nil || prepared.candidate == nil {
|
||||
return descriptorV3{}, ErrMapCoverageStale
|
||||
}
|
||||
return p.publishWithMapV3(storeMu, snap, func(d *descriptorV3) error {
|
||||
if mutate != nil {
|
||||
if e := mutate(d); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
*d = prepared.candidate.descriptor(*d)
|
||||
return nil
|
||||
}, prepared)
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type mapPublicationHarnessV3 struct {
|
||||
h *descHarnessV3
|
||||
m *chunkLSNMapV3
|
||||
domain *mapProofEpochV3
|
||||
initial *chunkMapCandidateV3
|
||||
}
|
||||
|
||||
func newMapPublicationHarnessV3(t *testing.T, gen uint64) *mapPublicationHarnessV3 {
|
||||
t.Helper()
|
||||
h := newDescHarnessV3(t)
|
||||
d := h.pub.current()
|
||||
d.W = 200
|
||||
d.CheckpointLSN = 100
|
||||
d.MapCoveredCheckpoint = 100
|
||||
cb := d.VolumeSize / uint64(d.BlockSize)
|
||||
m, e := createChunkLSNMapV3(filepath.Join(t.TempDir(), "map"), d, cb)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
t.Cleanup(func() { m.Close() })
|
||||
c, e := m.Prepare(gen, 100, []chunkLSNEntryV3{{LSN: 100}}, nil)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
d = c.descriptor(d)
|
||||
h.pub.cur = d
|
||||
if _, e = h.f.WriteAt(d.encode(), 0); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
domain := newMapProofEpochV3()
|
||||
if e = h.pub.bindMapV3(c, domain); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
return &mapPublicationHarnessV3{h, m, domain, c}
|
||||
}
|
||||
func (x *mapPublicationHarnessV3) prepare(t *testing.T, g, C uint64) *preparedMapPublicationV3 {
|
||||
t.Helper()
|
||||
c, e := prepareMapPublicationV3(x.m, x.domain, g, C, []chunkLSNEntryV3{{LSN: C}}, x.initial)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
return c
|
||||
}
|
||||
func mapSlotsV3(t *testing.T, h *descHarnessV3) []byte {
|
||||
t.Helper()
|
||||
b := make([]byte, 8192)
|
||||
if _, e := h.f.ReadAt(b, 0); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
return b
|
||||
}
|
||||
func awaitMapPublishV3(t *testing.T, ch <-chan error) error {
|
||||
t.Helper()
|
||||
select {
|
||||
case e := <-ch:
|
||||
return e
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("map publication did not settle")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
func TestMapPublicationV3_HeldPublicationsBothOrders(t *testing.T) {
|
||||
for _, reverse := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "A_then_B", true: "B_then_A"}[reverse], func(t *testing.T) {
|
||||
x := newMapPublicationHarnessV3(t, 11)
|
||||
a := x.prepare(t, 12, 120)
|
||||
b := x.prepare(t, 13, 110)
|
||||
releaseA := make(chan struct{})
|
||||
releaseB := make(chan struct{})
|
||||
aDone := make(chan error, 1)
|
||||
bDone := make(chan error, 1)
|
||||
joined := make(chan struct{}, 2)
|
||||
t.Cleanup(func() {
|
||||
select {
|
||||
case <-releaseA:
|
||||
default:
|
||||
close(releaseA)
|
||||
}
|
||||
select {
|
||||
case <-releaseB:
|
||||
default:
|
||||
close(releaseB)
|
||||
}
|
||||
timer := time.NewTimer(5 * time.Second)
|
||||
defer timer.Stop()
|
||||
for i := 0; i < 2; i++ {
|
||||
select {
|
||||
case <-joined:
|
||||
case <-timer.C:
|
||||
t.Error("publication workers did not join")
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
go func() {
|
||||
defer func() { joined <- struct{}{} }()
|
||||
<-releaseA
|
||||
_, e := x.h.pub.publishMapV3(&x.h.storeMu, nil, a, func(d *descriptorV3) error { d.CheckpointLSN = 120; return nil })
|
||||
aDone <- e
|
||||
}()
|
||||
go func() {
|
||||
defer func() { joined <- struct{}{} }()
|
||||
<-releaseB
|
||||
_, e := x.h.pub.publishMapV3(&x.h.storeMu, nil, b, func(d *descriptorV3) error { d.CheckpointLSN = 110; return nil })
|
||||
bDone <- e
|
||||
}()
|
||||
if reverse {
|
||||
close(releaseB)
|
||||
if e := awaitMapPublishV3(t, bDone); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
before := mapSlotsV3(t, x.h)
|
||||
close(releaseA)
|
||||
if e := awaitMapPublishV3(t, aDone); !errors.Is(e, ErrMapCoverageStale) {
|
||||
t.Fatalf("lower-generation A=%v", e)
|
||||
}
|
||||
if !bytes.Equal(before, mapSlotsV3(t, x.h)) {
|
||||
t.Fatal("refused A wrote slots")
|
||||
}
|
||||
} else {
|
||||
close(releaseA)
|
||||
if e := awaitMapPublishV3(t, aDone); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
before := mapSlotsV3(t, x.h)
|
||||
close(releaseB)
|
||||
if e := awaitMapPublishV3(t, bDone); !errors.Is(e, ErrMapCoverageStale) {
|
||||
t.Fatalf("stale-coverage B=%v", e)
|
||||
}
|
||||
if !bytes.Equal(before, mapSlotsV3(t, x.h)) {
|
||||
t.Fatal("refused B wrote slots")
|
||||
}
|
||||
}
|
||||
fresh := x.prepare(t, 14, 130)
|
||||
if _, e := x.h.pub.publishMapV3(&x.h.storeMu, nil, fresh, func(d *descriptorV3) error { d.CheckpointLSN = 125; return nil }); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
d := x.h.pub.current()
|
||||
if d.MapGeneration != 14 || d.MapCoveredCheckpoint != 130 || d.CheckpointLSN != 125 {
|
||||
t.Fatalf("fresh tuple=%+v", d)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestMapPublicationV3_IsolatedMergedCoverageAndWPositive(t *testing.T) {
|
||||
x := newMapPublicationHarnessV3(t, 12)
|
||||
c := x.prepare(t, 13, 110)
|
||||
before := mapSlotsV3(t, x.h)
|
||||
d0 := x.h.pub.current()
|
||||
floor, ok := x.h.pub.reuseFloor()
|
||||
if _, e := x.h.pub.publishMapV3(&x.h.storeMu, nil, c, func(d *descriptorV3) error { d.CheckpointLSN = 120; return nil }); !errors.Is(e, ErrMapCoverageStale) {
|
||||
t.Fatalf("isolated merged coverage=%v", e)
|
||||
}
|
||||
f2, o2 := x.h.pub.reuseFloor()
|
||||
if d0 != x.h.pub.current() || floor != f2 || ok != o2 || !bytes.Equal(before, mapSlotsV3(t, x.h)) {
|
||||
t.Fatal("refused candidate changed descriptor/tail/floor/slots")
|
||||
}
|
||||
if _, e := x.h.pub.publish(&x.h.storeMu, nil, func(d *descriptorV3) error { d.W = 210; return nil }); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if x.h.pub.current().MapRootHash != d0.MapRootHash {
|
||||
t.Fatal("W-only changed root")
|
||||
}
|
||||
}
|
||||
func TestMapPublicationV3_ProofEpochAndMutationRefusal(t *testing.T) {
|
||||
for _, event := range []string{"recover_start", "recover_end_success", "recover_end_failure", "reset", "install_begin", "install_complete", "install_abort", "mutation", "new_open"} {
|
||||
t.Run(event, func(t *testing.T) {
|
||||
x := newMapPublicationHarnessV3(t, 12)
|
||||
if event == "recover_end_success" || event == "recover_end_failure" {
|
||||
if e := x.domain.invalidateV3(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
c := x.prepare(t, 13, 110)
|
||||
if event == "mutation" {
|
||||
if e := x.domain.mutatedV3(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
} else if event == "new_open" {
|
||||
if e := x.m.Close(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
m, loaded, e := openChunkLSNMapV3(x.m.fd.Name(), x.h.pub.current(), x.m.chunkBlocks)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
x.m = m
|
||||
t.Cleanup(func() { m.Close() })
|
||||
x.initial = loaded
|
||||
x.domain = newMapProofEpochV3()
|
||||
if e = x.h.pub.bindMapV3(loaded, x.domain); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
} else {
|
||||
if e := x.domain.invalidateV3(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
before := mapSlotsV3(t, x.h)
|
||||
if _, e := x.h.pub.publishMapV3(&x.h.storeMu, nil, c, nil); !errors.Is(e, ErrMapCoverageStale) {
|
||||
t.Fatalf("%s stale proof=%v", event, e)
|
||||
}
|
||||
if !bytes.Equal(before, mapSlotsV3(t, x.h)) {
|
||||
t.Fatal("stale proof wrote slots")
|
||||
}
|
||||
// Reprepare with current epoch/open owner and a strictly higher generation.
|
||||
fresh := x.prepare(t, 14, 110)
|
||||
if _, e := x.h.pub.publishMapV3(&x.h.storeMu, nil, fresh, nil); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestMapPublicationV3_UncertainFinalResponsePinsCandidate(t *testing.T) {
|
||||
x := newMapPublicationHarnessV3(t, 12)
|
||||
c := x.prepare(t, 13, 110)
|
||||
boom := errors.New("response lost after descriptor write")
|
||||
x.h.writeHook = func(off int64, b []byte) error {
|
||||
if _, e := x.h.f.WriteAt(b, off); e != nil {
|
||||
return e
|
||||
}
|
||||
return boom
|
||||
}
|
||||
if _, e := x.h.pub.publishMapV3(&x.h.storeMu, nil, c, nil); !errors.Is(e, ErrDescriptorPublishUncertain) {
|
||||
t.Fatalf("unknown final response=%v", e)
|
||||
}
|
||||
pending, ok := x.h.pub.uncertainCandidate()
|
||||
if !ok || pending.MapRootHash != c.candidate.hash {
|
||||
t.Fatal("uncertain map dependency unpinned")
|
||||
}
|
||||
if _, _, e := readChunkLSNMapV3(x.m.fd, pending, x.m.chunkBlocks); e != nil {
|
||||
t.Fatalf("candidate pages lost: %v", e)
|
||||
}
|
||||
if _, ok := x.h.pub.reuseFloor(); ok {
|
||||
t.Fatal("unknown publication allowed retirement")
|
||||
}
|
||||
}
|
||||
func TestMapPublicationV3_CoverageWAndNoUnpreparedRoot(t *testing.T) {
|
||||
x := newMapPublicationHarnessV3(t, 12)
|
||||
c := x.prepare(t, 13, 201)
|
||||
if _, e := x.h.pub.publishMapV3(&x.h.storeMu, nil, c, nil); !errors.Is(e, ErrMapCoverageStale) {
|
||||
t.Fatalf("C>W=%v", e)
|
||||
}
|
||||
if _, e := x.h.pub.publish(&x.h.storeMu, nil, func(d *descriptorV3) error { *d = c.candidate.descriptor(*d); return nil }); !errors.Is(e, ErrMapCoverageStale) {
|
||||
t.Fatalf("unprepared root bypass=%v", e)
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,10 @@ type descriptorPublisherV3 struct {
|
||||
failed error
|
||||
uncertain *descriptorV3
|
||||
|
||||
// Immutable map dependencies and volatile proof domain, bound before serving.
|
||||
mapCurrent *chunkMapCandidateV3
|
||||
mapDomain *mapProofEpochV3
|
||||
|
||||
// trace, when set by a test, records lock acquisitions and releases
|
||||
// in order (the section 11 item 9 lock recorder).
|
||||
trace func(event string)
|
||||
@@ -131,6 +135,10 @@ func (p *descriptorPublisherV3) reuseFloorLocked() (uint64, bool) {
|
||||
// and the transition rules, validates the whole result and writes it into
|
||||
// the fallback slot with slotGen+1.
|
||||
func (p *descriptorPublisherV3) publish(storeMu sync.Locker, snap func(), mutate func(next *descriptorV3) error) (descriptorV3, error) {
|
||||
return p.publishWithMapV3(storeMu, snap, mutate, nil)
|
||||
}
|
||||
|
||||
func (p *descriptorPublisherV3) publishWithMapV3(storeMu sync.Locker, snap func(), mutate func(next *descriptorV3) error, prepared *preparedMapPublicationV3) (descriptorV3, error) {
|
||||
if storeMu != nil {
|
||||
storeMu.Lock()
|
||||
p.note("lock WALStore.mu")
|
||||
@@ -149,7 +157,7 @@ func (p *descriptorPublisherV3) publish(storeMu sync.Locker, snap func(), mutate
|
||||
p.note("unlock descMu")
|
||||
p.descMu.Unlock()
|
||||
}()
|
||||
return p.publishLocked(mutate)
|
||||
return p.publishLockedWithMapV3(mutate, prepared)
|
||||
}
|
||||
|
||||
// uncertainCandidate returns the candidate whose publication failed, if
|
||||
@@ -165,6 +173,10 @@ func (p *descriptorPublisherV3) uncertainCandidate() (descriptorV3, bool) {
|
||||
}
|
||||
|
||||
func (p *descriptorPublisherV3) publishLocked(mutate func(next *descriptorV3) error) (descriptorV3, error) {
|
||||
return p.publishLockedWithMapV3(mutate, nil)
|
||||
}
|
||||
|
||||
func (p *descriptorPublisherV3) publishLockedWithMapV3(mutate func(next *descriptorV3) error, prepared *preparedMapPublicationV3) (descriptorV3, error) {
|
||||
if p.failed != nil {
|
||||
return descriptorV3{}, p.failed
|
||||
}
|
||||
@@ -180,6 +192,9 @@ func (p *descriptorPublisherV3) publishLocked(mutate func(next *descriptorV3) er
|
||||
if err := mergeDescriptorV3(&p.cur, &next); err != nil {
|
||||
return descriptorV3{}, err
|
||||
}
|
||||
if err := p.validateMapPublicationV3(next, prepared); err != nil {
|
||||
return descriptorV3{}, err
|
||||
}
|
||||
if err := checkCountersV3(&next); err != nil {
|
||||
return descriptorV3{}, err
|
||||
}
|
||||
@@ -235,6 +250,9 @@ func (p *descriptorPublisherV3) publishLocked(mutate func(next *descriptorV3) er
|
||||
}
|
||||
p.other, p.otherValid = p.cur, true
|
||||
p.cur, p.curSlot = next, target
|
||||
if prepared != nil {
|
||||
p.mapCurrent = prepared.candidate
|
||||
}
|
||||
return next, nil
|
||||
}
|
||||
|
||||
@@ -282,8 +300,8 @@ func mergeDescriptorV3(cur, next *descriptorV3) error {
|
||||
next.MapRootHash != cur.MapRootHash || next.MapCoveredCheckpoint != cur.MapCoveredCheckpoint ||
|
||||
next.MapLineage != cur.MapLineage || next.MapFormatID != cur.MapFormatID {
|
||||
if next.MapGeneration <= cur.MapGeneration {
|
||||
return fmt.Errorf("%w: map root change without a higher generation (%d -> %d)",
|
||||
errDescriptorTransition, cur.MapGeneration, next.MapGeneration)
|
||||
return fmt.Errorf("%w: %w: map root change without a higher generation (%d -> %d)",
|
||||
ErrMapCoverageStale, errDescriptorTransition, cur.MapGeneration, next.MapGeneration)
|
||||
}
|
||||
if next.MapLineage != cur.MapLineage {
|
||||
return fmt.Errorf("%w: map lineage change outside an install or reset", errDescriptorTransition)
|
||||
|
||||
Reference in new issue
Block a user