mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
mq logstore: read every offset candidate, not a binary-searched suffix
An offset read binary-searched its candidate files by offset range and dropped those before the hit, assuming name order matched offset order. A directory written under clock skew holds a file that sorts before files with lower offsets, and the search skipped it even on a full listing. The listing already drops files whose range ends before the offset, so in an ordered directory the search removed nothing but offset-less legacy files, which are filtered per entry anyway. The search is gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e2c3ea385b
commit
6ec18be4d0
2 files changed
+17
-34
No files matched your search
@@ -256,8 +256,7 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top
|
||||
t.Name, p, startOffset)
|
||||
}
|
||||
|
||||
// OPTIMIZATION: For offset-based reads, collect all files with their offset ranges first
|
||||
// Then use binary search to find the right file, and skip files that don't contain the offset
|
||||
// For offset-based reads, skip files whose offset range ends before the offset
|
||||
var candidateFiles []*filer_pb.Entry
|
||||
var foundStartFile bool
|
||||
var listed []*filer_pb.Entry
|
||||
@@ -332,38 +331,6 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top
|
||||
return startPosition, isDone, nil
|
||||
}
|
||||
|
||||
// OPTIMIZATION: For offset-based reads with many files, use binary search to find start file
|
||||
if isOffsetBased && len(candidateFiles) > 10 {
|
||||
// Binary search to find the first file that might contain our offset
|
||||
left, right := 0, len(candidateFiles)-1
|
||||
startIdx := 0
|
||||
|
||||
for left <= right {
|
||||
mid := (left + right) / 2
|
||||
entry := candidateFiles[mid]
|
||||
|
||||
fileMinOffset, fileMaxOffset, ok := logFileOffsets(entry)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if startOffset < fileMinOffset {
|
||||
// Our offset is before this file, search left
|
||||
right = mid - 1
|
||||
} else if startOffset > fileMaxOffset {
|
||||
// Our offset is after this file, search right
|
||||
left = mid + 1
|
||||
startIdx = left
|
||||
} else {
|
||||
// Found the file containing our offset
|
||||
startIdx = mid
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Process files starting from the found index
|
||||
candidateFiles = candidateFiles[startIdx:]
|
||||
}
|
||||
|
||||
// Second pass: process the filtered files
|
||||
// CRITICAL: For offset-based reads, process ALL candidate files in one call
|
||||
// This prevents multiple ReadFromDiskFn calls with 1.127s overhead each
|
||||
|
||||
@@ -326,3 +326,19 @@ func TestReadFromDiskByOffsetReadsLegacyFiles(t *testing.T) {
|
||||
t.Errorf("read %v from the last offset", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A directory written under clock skew holds a file sorting before files with
|
||||
// lower offsets; reading by offset still finds that file.
|
||||
func TestReadFromDiskByOffsetFindsAFileSortingBeforeLowerOffsets(t *testing.T) {
|
||||
const files = 2 * logFileIndexStride
|
||||
volume := serveChunks(t, map[string][]byte{})
|
||||
skewed := offsetLogFiles(t, volume, files, 1, false)[0]
|
||||
skewed.Name = topic.LogFileName(time.Date(2026, 11, 30, 9, 0, 0, 0, time.UTC), 2*files)
|
||||
_, client := serveFiler(t, volume, append(offsetLogFiles(t, volume, 0, files, false), skewed)...)
|
||||
read := testPartitionReader(client)
|
||||
|
||||
// read in name order, so the skewed file comes first
|
||||
if got := readOffsets(t, read, 0); !slices.Equal(slices.Sorted(slices.Values(got)), offsetsFrom(0, 2*files+2)) {
|
||||
t.Errorf("read %d offsets from 0, want %d", len(got), 2*files+2)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user