diff --git a/weed/mq/logstore/read_log_from_disk.go b/weed/mq/logstore/read_log_from_disk.go index d8151982dd..c0cbd94a7d 100644 --- a/weed/mq/logstore/read_log_from_disk.go +++ b/weed/mq/logstore/read_log_from_disk.go @@ -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 diff --git a/weed/mq/logstore/read_log_from_disk_test.go b/weed/mq/logstore/read_log_from_disk_test.go index a357067684..a8a441e276 100644 --- a/weed/mq/logstore/read_log_from_disk_test.go +++ b/weed/mq/logstore/read_log_from_disk_test.go @@ -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) + } +}