From 0025e7177fa139ded947dcf7eb5b77255fa84657 Mon Sep 17 00:00:00 2001 From: Song Bian Date: Mon, 30 Nov 2026 13:24:21 -0800 Subject: [PATCH] fix(filer): preserve chunk ordering on 32-bit systems (#13175) --- weed/filer/filechunks_read.go | 5 +- weed/filer/filechunks_read_order_test.go | 88 ++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 weed/filer/filechunks_read_order_test.go diff --git a/weed/filer/filechunks_read.go b/weed/filer/filechunks_read.go index a9913f7edd..bc1e4523f1 100644 --- a/weed/filer/filechunks_read.go +++ b/weed/filer/filechunks_read.go @@ -1,6 +1,7 @@ package filer import ( + "cmp" "container/list" "slices" @@ -37,10 +38,10 @@ func readResolvedChunks(chunks []*filer_pb.FileChunk, startOffset int64, stopOff } slices.SortFunc(points, func(a, b *Point) int { if a.x != b.x { - return int(a.x - b.x) + return cmp.Compare(a.x, b.x) } if a.ts != b.ts { - return int(a.ts - b.ts) + return cmp.Compare(a.ts, b.ts) } if a.order != b.order { return a.order - b.order diff --git a/weed/filer/filechunks_read_order_test.go b/weed/filer/filechunks_read_order_test.go new file mode 100644 index 0000000000..7060aed577 --- /dev/null +++ b/weed/filer/filechunks_read_order_test.go @@ -0,0 +1,88 @@ +package filer_test + +import ( + "context" + "math" + "testing" + + "github.com/seaweedfs/seaweedfs/weed/filer" + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" +) + +func TestSparseChunkViewsPreserveOffsetOrder(t *testing.T) { + for _, offset := range []int64{1 << 30, (1 << 31) - 1, 1 << 31, (1 << 31) + 1, 3 << 30, 1 << 32, math.MaxInt64 - 1} { + chunks := []*filer_pb.FileChunk{ + {FileId: "1,0000000100000001", Offset: 0, Size: 1, ModifiedTsNs: 1}, + {FileId: "1,0000000200000002", Offset: offset, Size: 1, ModifiedTsNs: 2}, + } + views, err := filer.ViewFromChunks(context.Background(), nil, chunks, 0, math.MaxInt64) + if err != nil { + t.Fatal(err) + } + var starts []int64 + for x := views.Front(); x != nil; x = x.Next { + starts = append(starts, x.StartOffset) + } + if len(starts) != 2 || starts[0] != 0 || starts[1] != offset { + t.Errorf("offset=%d: views at %v, want [0 %d]", offset, starts, offset) + } + } +} + +type chunkOrderByteCache struct{} + +func (*chunkOrderByteCache) ReadChunkAt(data []byte, fileID string, _ uint64) (int, error) { + value := byte(0xCD) + if fileID == "1,0000000100000001" { + value = 0xAB + } + for i := range data { + data[i] = value + } + return len(data), nil +} +func (*chunkOrderByteCache) SetChunk(string, []byte) {} +func (*chunkOrderByteCache) IsInCache(string, bool) bool { return true } +func (*chunkOrderByteCache) GetMaxFilePartSizeInCache() uint64 { return 1 << 20 } + +func TestSparseChunkReadPreservesFirstByte(t *testing.T) { + const farOffset = int64(3 << 30) + chunks := []*filer_pb.FileChunk{ + {FileId: "1,0000000100000001", Offset: 0, Size: 1, ModifiedTsNs: 1}, + {FileId: "1,0000000200000002", Offset: farOffset, Size: 1, ModifiedTsNs: 2}, + } + views, err := filer.ViewFromChunks(context.Background(), nil, chunks, 0, math.MaxInt64) + if err != nil { + t.Fatal(err) + } + cache := filer.NewReaderCache(1, &chunkOrderByteCache{}, nil, nil) + reader := filer.NewChunkReaderAtFromClient(context.Background(), cache, views, farOffset+1, 0) + defer reader.Close() + buf := []byte{0xFF} + n, err := reader.ReadAt(buf, 0) + if err != nil || n != 1 || buf[0] != 0xAB { + t.Fatalf("first stored byte must be 0xAB; got n=%d err=%v data=%x", n, err, buf) + } +} + +func TestChunkViewsPreserveTimestampPriority(t *testing.T) { + for _, timestamps := range [][2]int64{{1, 1 + (3 << 30)}, {math.MinInt64, math.MaxInt64}} { + chunks := []*filer_pb.FileChunk{ + {FileId: "1,0000000100000001", Offset: 0, Size: 2, ModifiedTsNs: timestamps[0]}, + {FileId: "1,0000000200000002", Offset: 0, Size: 1, ModifiedTsNs: timestamps[1]}, + } + views, err := filer.ViewFromChunks(context.Background(), nil, chunks, 0, 2) + if err != nil { + t.Fatal(err) + } + var ids []string + var offsets []int64 + for view := views.Front(); view != nil; view = view.Next { + ids = append(ids, view.Value.FileId) + offsets = append(offsets, view.StartOffset) + } + if len(ids) != 2 || ids[0] != chunks[1].FileId || ids[1] != chunks[0].FileId || offsets[0] != 0 || offsets[1] != 1 { + t.Fatalf("timestamps=%v: got file IDs %v at offsets %v; newer chunk must cover [0,1), older chunk [1,2)", timestamps, ids, offsets) + } + } +}