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]) + } + } + } +}