storage: HEAD's remote_lsn never decreases

A cut whose LSN is below the base HEAD's remote_lsn is refused before any
upload (ErrChunkCaptureRefused with ErrChunkLSNBehind; the expected gen
pins that base HEAD), unless that HEAD belongs to a newer holder, which
fences as before. MemChunkRemote refuses a lowering CAS too (B9 W2
appendix, 2026-11-15 14:30Z).

Until LSN continuity across hosts exists, a fresh, migrated or forked
volume cannot cut or detach; the carry-forward must merge before or with
this check. Tests that model a host continuing from a seeded HEAD now
advance the local log to that HEAD's remote_lsn first, as the
carry-forward will.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-15 06:11:08 -08:00
1 parent 95b6b7d62e
commit b06bf7dc45
9 files changed
+81 -3

No files matched your search

@@ -411,10 +411,12 @@ func TestFork_ThenFreshHostReadsSnapshotImage(t *testing.T) {
if _, err := layer.Cut(ctx); err != nil {
t.Fatal(err)
}
if _, err := c.Fork(ctx, ForkRequest{Source: "v1", Snapshot: "s1", Target: "v2", RequestID: "f"}); err != nil {
fork, err := c.Fork(ctx, ForkRequest{Source: "v1", Snapshot: "s1", Target: "v2", RequestID: "f"})
if err != nil {
t.Fatal(err)
}
dst, dlayer := attach("v2", true, 1)
dst.AdvanceFrontier(fork.RemoteLSN) // the fork's log continues from the copied remote_lsn (W4b's job in production)
zero := make([]byte, 4096)
for lba := uint32(0); lba < blocks; lba++ {
got, err := dst.Read(lba)
@@ -617,6 +617,13 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (chunkC
l.abortCut(all)
return chunkCutResult{}, fmt.Errorf("storage: capture copy: %w", copyErr)
}
if h < baseHead.RemoteLSN && !l.supersededBy(baseHead) {
// HEAD holds the image of a later LSN than this host's log has
// reached: publishing would move remote_lsn backwards. Refused
// before any upload. (A HEAD of a newer holder fences below.)
l.abortCut(all)
return chunkCutResult{}, fmt.Errorf("%w: %w: cut LSN %d, HEAD remote_lsn %d", errChunkCaptureRefused, errChunkLSNBehind, h, baseHead.RemoteLSN)
}
// Upload only what changed against HEAD.
next := copyHead(baseHead)
@@ -278,6 +278,7 @@ func TestChunkLayer_C3_WriteDuringCopyWaitsForTheCut(t *testing.T) {
f := newFakeChunkRemote()
f.seedHead(t, src, 10, 1)
s, _ := newChunkTestStore(t)
s.AdvanceFrontier(10) // the log continues from HEAD's remote_lsn (W4b's job in production)
cfg := ctConfig(2)
cfg.FreshFromHead = true
layer := mustAttach(t, s, f, cfg)
@@ -111,6 +111,7 @@ func TestChunkCut_NewHolderClearsDetachedState(t *testing.T) {
f.mu.Unlock()
s, _ := newChunkTestStore(t)
s.AdvanceFrontier(10) // the log continues from HEAD's remote_lsn (W4b's job in production)
cfg := ctConfig(2)
cfg.FreshFromHead = true
layer := mustAttach(t, s, f, cfg)
@@ -0,0 +1,58 @@
package storage
import (
"context"
"errors"
"testing"
)
// HEAD's remote_lsn never decreases (B9 W2 appendix, 2026-11-15 14:30Z): a
// host whose log is behind HEAD (a fresh or migrated host before W4b carries
// LSN continuity) cannot cut or detach; nothing is uploaded and no CAS is
// sent. Once its log continues from HEAD's remote_lsn, it cuts.
func TestChunkCut_RemoteLSNNeverDecreases(t *testing.T) {
src := make([]byte, ctNumBlocks*ctBlockSize)
copy(src, makeBlock(ctBlockSize, 0x44))
f := newFakeChunkRemote()
f.seedHead(t, src, 10, 1)
s, _ := newChunkTestStore(t)
cfg := ctConfig(2)
cfg.FreshFromHead = true
layer := mustAttach(t, s, f, cfg)
w := newWriteLog()
w.write(t, s, 3, 0x33) // local LSN 1, HEAD remote_lsn 10
before := f.stats()
if _, err := layer.Cut(ctTimeout(t)); !errors.Is(err, errChunkCaptureRefused) || !errors.Is(err, errChunkLSNBehind) {
t.Fatalf("cut behind HEAD's remote_lsn: %v; want refused with errChunkLSNBehind", err)
}
if _, err := layer.Detach(ctTimeout(t)); !errors.Is(err, errChunkLSNBehind) {
t.Fatalf("detach behind HEAD's remote_lsn: %v; want errChunkLSNBehind", err)
}
after := f.stats()
if after.cas != before.cas || after.puts != before.puts || after.head.Gen != before.head.Gen {
t.Fatalf("a refused cut published: %d CAS, %d puts, HEAD gen %d -> %d", after.cas-before.cas, after.puts-before.puts, before.head.Gen, after.head.Gen)
}
s.AdvanceFrontier(10) // continuity from HEAD (W4b's job in production)
w.write(t, s, 4, 0x34)
r, err := layer.Cut(ctTimeout(t))
if err != nil || r.Head.RemoteLSN < 10 || r.Head.Gen != before.head.Gen+1 {
t.Fatalf("cut once the log continues from HEAD: %+v %v", r.Head, err)
}
}
// MemChunkRemote refuses a proposal that lowers remote_lsn.
func TestMemChunkRemote_RefusesLowerRemoteLSN(t *testing.T) {
ctx := context.Background()
m := NewMemChunkRemote()
gen, err := m.CASHead(ctx, chunkHeadVersion{}, chunkHead{Epoch: 1, Holder: "a", RemoteLSN: 10, Chunks: map[uint64]chunkRef{}})
if err != nil {
t.Fatal(err)
}
if _, err := m.CASHead(ctx, chunkHeadVersion{Gen: gen, Epoch: 1}, chunkHead{Epoch: 1, Holder: "a", RemoteLSN: 9, Chunks: map[uint64]chunkRef{}}); !errors.Is(err, errChunkHeadPrecondition) {
t.Fatalf("CAS lowering remote_lsn 10 -> 9: %v; want a precondition refusal", err)
}
if _, err := m.CASHead(ctx, chunkHeadVersion{Gen: gen, Epoch: 1}, chunkHead{Epoch: 1, Holder: "a", RemoteLSN: 10, Chunks: map[uint64]chunkRef{}}); err != nil {
t.Fatalf("CAS keeping remote_lsn: %v", err)
}
}
@@ -193,6 +193,7 @@ func TestChunkLayer_StaleAttachDoesNotPublishOverNewerHolder(t *testing.T) {
f.heads[len(f.heads)-1].Holder = tc.headHolder
f.mu.Unlock()
s, _ := newChunkTestStore(t)
s.AdvanceFrontier(5) // the log continues from HEAD's remote_lsn (W4b's job in production)
if _, err := s.Write(3, makeBlock(ctBlockSize, 0x33)); err != nil { // stale local change
t.Fatal(err)
}
@@ -327,6 +328,7 @@ func TestChunkLayer_StaleFillNeverAuthorizesUnresolvedExtentRead(t *testing.T) {
f := newFakeChunkRemote()
f.seedHead(t, src, 5, 1)
s, _ := newChunkTestStore(t)
s.AdvanceFrontier(5) // the log continues from HEAD's remote_lsn (W4b's job in production)
cfg := ctConfig(2)
cfg.FreshFromHead = true
layer := mustAttach(t, s, f, cfg)
@@ -407,6 +409,7 @@ func TestChunkLayer_FreshHostHashCutFetchesNoUntouchedChunk(t *testing.T) {
f := newFakeChunkRemote()
f.seedHead(t, src, 5, 1)
s, _ := newChunkTestStore(t)
s.AdvanceFrontier(5) // the log continues from HEAD's remote_lsn (W4b's job in production)
cfg := ctConfig(2)
cfg.FreshFromHead = true
layer := mustAttach(t, s, f, cfg)
@@ -122,8 +122,9 @@ func (m *MemChunkRemote) CASHead(ctx context.Context, expected chunkHeadVersion,
if expected.Gen != m.head.Gen || expected.Epoch != m.head.Epoch {
return 0, errChunkHeadPrecondition
}
// Ownership only moves forward: a proposal never lowers the epoch.
if next.Epoch < m.head.Epoch {
// Ownership only moves forward: a proposal never lowers the epoch, and
// the image never moves to an earlier LSN.
if next.Epoch < m.head.Epoch || next.RemoteLSN < m.head.RemoteLSN {
return 0, errChunkHeadPrecondition
}
for idx, ref := range next.Chunks {
@@ -51,6 +51,7 @@ var (
ErrChunkLayerClosed = errChunkLayerClosed
ErrChunkBaseInstalling = errChunkBaseInstalling
ErrChunkRemoteUnavailable = errChunkRemoteUnavailable
ErrChunkLSNBehind = errChunkLSNBehind
)
// ValidChunkKey enforces the chunk key domain (non-empty valid UTF-8); a
+4
View File
@@ -390,6 +390,10 @@ var (
errChunkLayerClosed = errors.New("storage: chunk layer closed")
errChunkBaseInstalling = errors.New("storage: chunk BASE install in progress")
errChunkRemoteUnavailable = errors.New("storage: chunk remote unavailable")
// errChunkLSNBehind: this host's log has not reached the HEAD's
// remote_lsn, so a cut would move remote_lsn backwards (B9 W2 appendix,
// 2026-11-15 14:30Z; LSN continuity across hosts is W4b's).
errChunkLSNBehind = errors.New("storage: cut LSN is behind the HEAD's remote_lsn")
)
// attachChunkLayer attaches a chunk layer to an open store (chunk_layer.go).