fix(storage): confirm publication from validated root without segment scans

This commit is contained in:
pingqiu committed 2026-11-20 01:22:57 -08:00
1 parent c39240587f
commit 816eb03286
4 files changed
+32 -2

No files matched your search

@@ -378,6 +378,11 @@ func (r *ChunkRemote) GetHead(ctx context.Context) (storage.ChunkHead, error) {
return r.resolveManifest(ctx, h)
}
// GetHeadRoot validates publication identity without reading segment bodies.
func (r *ChunkRemote) GetHeadRoot(ctx context.Context) (storage.ChunkHead, error) {
return r.getHeadRoot(ctx)
}
// getHeadRoot validates the root and its CAS attributes without reading any
// immutable segment. CASHead only needs authority and lineage from this root.
func (r *ChunkRemote) getHeadRoot(ctx context.Context) (storage.ChunkHead, error) {
@@ -48,6 +48,10 @@ func TestManifest_CASReadsOnlyRoot(t *testing.T) {
if gen != h.Gen+1 || tracked.roots != 1 || tracked.children != 0 {
t.Fatalf("CAS gen=%d root reads=%d child reads=%d", gen, tracked.roots, tracked.children)
}
root, err := r.GetHeadRoot(context.Background())
if err != nil || root.Gen != gen || root.Epoch != h.Epoch || root.Holder != h.Holder || tracked.children != 0 {
t.Fatalf("confirmation resolved segments: root=%+v children=%d err=%v", root, tracked.children, err)
}
if _, err := r.GetHead(context.Background()); err == nil {
t.Fatal("GetHead skipped segment resolution")
}
@@ -514,6 +514,15 @@ func (r *closeBoundRemote) GetHead(ctx context.Context) (storage.ChunkHead, erro
return r.ChunkRemote.GetHead(c)
}
func (r *closeBoundRemote) GetHeadRoot(ctx context.Context) (storage.ChunkHead, error) {
c, end := r.bind(ctx)
defer end()
if reader, ok := r.ChunkRemote.(storage.ChunkHeadRootReader); ok {
return reader.GetHeadRoot(c)
}
return r.ChunkRemote.GetHead(c)
}
func (r *closeBoundRemote) CASHead(ctx context.Context, expected storage.ChunkHeadVersion, next storage.ChunkHead) (uint64, error) {
c, end := r.bind(ctx)
defer end()
+14 -2
View File
@@ -11,12 +11,24 @@ import (
// detection belt, not a substitute for a single HEAD serialization point.
const chunkHeadCoolingWindow = 5 * time.Second
// ChunkHeadRootReader confirms publication identity without resolving every
// immutable manifest. The returned Chunks need not be populated.
type ChunkHeadRootReader interface {
GetHeadRoot(context.Context) (ChunkHead, error)
}
func confirmChunkHead(ctx context.Context, remote chunkRemote, want chunkHead) error {
got, err := remote.GetHead(ctx)
var got chunkHead
var err error
if reader, ok := remote.(ChunkHeadRootReader); ok {
got, err = reader.GetHeadRoot(ctx)
} else {
got, err = remote.GetHead(ctx)
}
if err != nil {
return fmt.Errorf("storage: HEAD publication confirmation: %w: %w", errChunkCASOutcomeUnknown, err)
}
if got.Gen != want.Gen || !sameProposal(got, want) {
if got.Gen != want.Gen || got.Epoch != want.Epoch || got.Holder != want.Holder {
log.Printf("ALARM: HEAD publication displaced: wanted gen=%d epoch=%d holder=%q, read gen=%d epoch=%d holder=%q", want.Gen, want.Epoch, want.Holder, got.Gen, got.Epoch, got.Holder)
return fmt.Errorf("%w: HEAD publication displaced", errChunkLayerFenced)
}