From 975d79d57f7f63358e892c20a4fc3ca7b583e7df Mon Sep 17 00:00:00 2001 From: Dean Chen <51218137+locker95@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:29:25 -0500 Subject: [PATCH] filer: keep chunk coverage when timestamps tie in readResolvedChunks (#12940) * filer: keep chunk coverage when timestamps tie in readResolvedChunks When an overlapping chunk started with the same ModifiedTsNs as the currently visible one, it was never inserted into the sweep queue, and an END point removed whichever queued chunk had a matching ts rather than the chunk that actually ended. Either way part of the file was dropped from the visible intervals, and SeparateGarbageChunks could then treat the still-live chunk as garbage. Insert tied starts ahead of the existing tail so the visible chunk keeps winning, and remove the queued entry for the chunk that is ending. Fixes #12934 * filer: break readResolvedChunks timestamp ties by chunk order On a tie the sweep let the chunk that started first keep the overlap, while MergeIntoVisibles, which a section uses when a chunk is added after its first read, lets the later chunk win. A later write that got the same timestamp lost its overlap, and the two paths disagreed on which chunk's bytes a read returns and which chunk is garbage. Ties now go to the later chunk in the list, which also makes the order total. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Chris Lu Co-authored-by: Claude Opus 5.5 --- weed/filer/filechunks_read.go | 25 +++++++-- weed/filer/filechunks_read_test.go | 88 ++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 5 deletions(-) diff --git a/weed/filer/filechunks_read.go b/weed/filer/filechunks_read.go index 59a00e6ff9..5bca141cf7 100644 --- a/weed/filer/filechunks_read.go +++ b/weed/filer/filechunks_read.go @@ -10,7 +10,7 @@ import ( func readResolvedChunks(chunks []*filer_pb.FileChunk, startOffset int64, stopOffset int64) (visibles *IntervalList[*VisibleInterval]) { var points []*Point - for _, chunk := range chunks { + for i, chunk := range chunks { if chunk.IsChunkManifest { println("This should not happen! A manifest chunk found:", chunk.GetFileIdString()) } @@ -21,12 +21,14 @@ func readResolvedChunks(chunks []*filer_pb.FileChunk, startOffset int64, stopOff points = append(points, &Point{ x: chunk.Offset, ts: chunk.ModifiedTsNs, + order: i, chunk: chunk, isStart: true, }) points = append(points, &Point{ x: chunk.Offset + int64(chunk.Size), ts: chunk.ModifiedTsNs, + order: i, chunk: chunk, isStart: false, }) @@ -38,6 +40,9 @@ func readResolvedChunks(chunks []*filer_pb.FileChunk, startOffset int64, stopOff if a.ts != b.ts { return int(a.ts - b.ts) } + if a.order != b.order { + return a.order - b.order + } if a.isStart { return 1 } @@ -59,18 +64,18 @@ func readResolvedChunks(chunks []*filer_pb.FileChunk, startOffset int64, stopOff } if point.isStart { if prevPoint != nil { - if point.x != prevX && prevPoint.ts < point.ts { + if point.x != prevX && point.newerThan(prevPoint) { addToVisibles(visibles, prevX, prevPoint, point) prevX = point.x } } // insert into queue - if prevPoint == nil || prevPoint.ts < point.ts { + if prevPoint == nil || point.newerThan(prevPoint) { queue.PushBack(point) prevX = point.x } else { for e := queue.Front(); e != nil; e = e.Next() { - if e.Value.(*Point).ts > point.ts { + if e.Value.(*Point).newerThan(point) { queue.InsertBefore(point, e) break } @@ -79,7 +84,7 @@ func readResolvedChunks(chunks []*filer_pb.FileChunk, startOffset int64, stopOff } else { isLast := true for e := queue.Back(); e != nil; e = e.Prev() { - if e.Value.(*Point).ts == point.ts { + if e.Value.(*Point).order == point.order { queue.Remove(e) break } @@ -124,6 +129,16 @@ func appendVisibleInterfal(visibles *IntervalList[*VisibleInterval], visible *Vi type Point struct { x int64 ts int64 + order int chunk *filer_pb.FileChunk isStart bool } + +// newerThan breaks a ts tie by position in the chunk list, so the later chunk +// wins, as it does in MergeIntoVisibles. +func (p *Point) newerThan(o *Point) bool { + if p.ts != o.ts { + return p.ts > o.ts + } + return p.order > o.order +} diff --git a/weed/filer/filechunks_read_test.go b/weed/filer/filechunks_read_test.go index 5bfd34cfda..35492e9f33 100644 --- a/weed/filer/filechunks_read_test.go +++ b/weed/filer/filechunks_read_test.go @@ -279,3 +279,91 @@ func TestActualReadResolvedChunks2(t *testing.T) { } } + +func TestReadResolvedChunksTiedTimestamps(t *testing.T) { + a := &filer_pb.FileChunk{FileId: "1,aaa", Offset: 0, Size: 100, ModifiedTsNs: 7} + b := &filer_pb.FileChunk{FileId: "2,bbb", Offset: 50, Size: 100, ModifiedTsNs: 7} + + visibles := readResolvedChunks([]*filer_pb.FileChunk{a, b}, 0, 300) + + type span struct { + start, stop int64 + fileId string + } + var got []span + for x := visibles.Front(); x != nil; x = x.Next { + got = append(got, span{x.Value.start, x.Value.stop, x.Value.fileId}) + } + want := []span{{0, 50, "1,aaa"}, {50, 150, "2,bbb"}} + if len(got) != len(want) { + t.Fatalf("visibles = %+v, want %+v", got, want) + } + for i := range want { + if got[i] != want[i] { + t.Fatalf("visibles = %+v, want %+v", got, want) + } + } +} + +func TestRandomizedReadResolvedChunksTiedTimestamps(t *testing.T) { + const limit = 4096 + for round := 0; round < 200; round++ { + var chunks []*filer_pb.FileChunk + incremental := NewIntervalList[*VisibleInterval]() + for i := 0; i < 32; i++ { + start := rand.Int63n(limit) + size := 1 + rand.Int63n(256) + if start+size > limit { + size = limit - start + } + chunk := &filer_pb.FileChunk{ + FileId: fmt.Sprintf("%d,%d", round, i), + Offset: start, + Size: uint64(size), + ModifiedTsNs: rand.Int63n(4), // lots of ties + } + chunks = append(chunks, chunk) + MergeIntoVisibles(incremental, start, start+size, chunk) + } + var wantFileId [limit]string + for x := incremental.Front(); x != nil; x = x.Next { + for i := x.Value.start; i < x.Value.stop; i++ { + wantFileId[i] = x.Value.fileId + } + } + + // expected max ts at each position, -1 when nothing covers it + var want [limit]int64 + for i := range want { + want[i] = -1 + } + for _, c := range chunks { + for i := c.Offset; i < c.Offset+int64(c.Size); i++ { + want[i] = max(want[i], c.ModifiedTsNs) + } + } + + var got [limit]int64 + for i := range got { + got[i] = -1 + } + visibles := readResolvedChunks(chunks, 0, math.MaxInt64) + for x := visibles.Front(); x != nil; x = x.Next { + v := x.Value + for i := v.start; i < v.stop; i++ { + if got[i] != -1 { + t.Fatalf("round %d: position %d covered twice", round, i) + } + if v.fileId != wantFileId[i] { + t.Fatalf("round %d: position %d reads %s, MergeIntoVisibles has %s", round, i, v.fileId, wantFileId[i]) + } + got[i] = v.modifiedTsNs + } + } + for i := range want { + if got[i] != want[i] { + t.Fatalf("round %d: position %d expected ts %d actual ts %d", round, i, want[i], got[i]) + } + } + } +}