mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
fix(filer): preserve chunk ordering on 32-bit systems (#13175)
This commit is contained in:
1 parent
1a2be918bf
commit
0025e7177f
2 files changed
+91
-2
No files matched your search
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user