mq logstore: stop a disk read when the callback is done

GenLogOnDiskReadFunc ignored the isDone the per-entry callback returned, so it
kept fetching and delivering every later entry, chunk and file, and the merged
read then went on into the parquet files. The chunked offset read and the query
scanner's close path both return true and expect the read to stop. The reader
now stops at that entry, skips the remaining chunks and files, and returns done
with a position just past the entry: the next offset for an offset read, its
time for a timestamp read. The merged read returns there instead of reading
parquet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-01 11:12:29 -08:00
1 parent d6dee41f00
commit c9e84e4463
3 files changed
+84 -12

No files matched your search

+1 -6
View File
@@ -22,12 +22,7 @@ func mergeReadFuncs(readLogDirectFn, fromParquetFn log_buffer.LogReadFromDiskFun
return func(startPosition log_buffer.MessagePosition, stopTsNs int64, eachLogEntryFn log_buffer.EachLogEntryFuncType) (lastReadPosition log_buffer.MessagePosition, isDone bool, err error) {
// Always try reading from live logs first (recent data)
lastReadPosition, isDone, err = readLogDirectFn(startPosition, stopTsNs, eachLogEntryFn)
if isDone {
// For very early timestamps (like timestamp=1 for RESET_TO_EARLIEST),
// we want to continue to read from in-memory data
isDone = false
}
if err != nil {
if err != nil || isDone {
return
}
+26 -6
View File
@@ -176,7 +176,7 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top
lookupFileIdFn := filer.LookupFn(filerClient)
eachChunkFn := func(buf []byte, eachLogEntryFn log_buffer.EachLogEntryFuncType, starTsNs, stopTsNs int64, startOffset int64, isOffsetBased bool) (processedTsNs int64, err error) {
eachChunkFn := func(buf []byte, eachLogEntryFn log_buffer.EachLogEntryFuncType, starTsNs, stopTsNs int64, startOffset int64, isOffsetBased bool) (processedTsNs int64, isDone bool, err error) {
entriesSkipped := 0
entriesProcessed := 0
for pos := 0; pos+4 < len(buf); {
@@ -215,13 +215,16 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top
}
// fmt.Printf(" read logEntry: %v, ts %v\n", string(logEntry.Key), time.Unix(0, logEntry.TsNs).UTC())
if _, err = eachLogEntryFn(logEntry); err != nil {
if isDone, err = eachLogEntryFn(logEntry); err != nil {
err = fmt.Errorf("process log entry %v: %w", logEntry, err)
return
}
processedTsNs = logEntry.TsNs
entriesProcessed++
if isDone {
return
}
pos += 4 + int(size)
@@ -230,7 +233,7 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top
return
}
eachFileFn := func(entry *filer_pb.Entry, eachLogEntryFn log_buffer.EachLogEntryFuncType, starTsNs, stopTsNs int64, startOffset int64, isOffsetBased bool) (processedTsNs int64, err error) {
eachFileFn := func(entry *filer_pb.Entry, eachLogEntryFn log_buffer.EachLogEntryFuncType, starTsNs, stopTsNs int64, startOffset int64, isOffsetBased bool) (processedTsNs int64, isDone bool, err error) {
if len(entry.Content) > 0 {
// skip .offset files
return
@@ -266,12 +269,15 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top
}
// Positive cache hit - data exists
var chunkTsNs int64
chunkTsNs, err = eachChunkFn(cachedData, eachLogEntryFn, starTsNs, stopTsNs, startOffset, isOffsetBased)
chunkTsNs, isDone, err = eachChunkFn(cachedData, eachLogEntryFn, starTsNs, stopTsNs, startOffset, isOffsetBased)
processedTsNs = max(processedTsNs, chunkTsNs)
if err != nil {
glog.V(1).Infof("eachChunkFn failed on cached data: %v", err)
return
}
if isDone {
return
}
continue
}
@@ -289,12 +295,15 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top
fileCache.Put(cacheKey, data, startOffset)
var chunkTsNs int64
chunkTsNs, err = eachChunkFn(data, eachLogEntryFn, starTsNs, stopTsNs, startOffset, isOffsetBased)
chunkTsNs, isDone, err = eachChunkFn(data, eachLogEntryFn, starTsNs, stopTsNs, startOffset, isOffsetBased)
processedTsNs = max(processedTsNs, chunkTsNs)
if err != nil {
glog.V(1).Infof("eachChunkFn failed: %v", err)
return
}
if isDone {
return
}
break
} else {
glog.V(2).Infof("failed to fetch from %s: %v", urlString, err)
@@ -420,9 +429,14 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top
// This prevents multiple ReadFromDiskFn calls with 1.127s overhead each
var filesProcessed int
var lastProcessedOffset int64
var lastDeliveredOffset int64
deliver := func(logEntry *filer_pb.LogEntry) (bool, error) {
lastDeliveredOffset = logEntry.Offset
return eachLogEntryFn(logEntry)
}
for _, entry := range candidateFiles {
var fileTsNs int64
if fileTsNs, err = eachFileFn(entry, eachLogEntryFn, startTsNs, stopTsNs, startOffset, isOffsetBased); err != nil {
if fileTsNs, isDone, err = eachFileFn(entry, deliver, startTsNs, stopTsNs, startOffset, isOffsetBased); err != nil {
return lastReadPosition, isDone, err
}
if fileTsNs > 0 {
@@ -430,6 +444,12 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top
processedTsNs = max(processedTsNs, fileTsNs)
filesProcessed++
}
if isDone {
if isOffsetBased {
return log_buffer.NewMessagePositionFromOffset(lastDeliveredOffset + 1), true, nil
}
return log_buffer.NewMessagePosition(processedTsNs, -2), true, nil
}
// For offset-based reads, track the last processed offset
// We need to continue reading ALL files to avoid multiple disk read calls
@@ -519,3 +519,60 @@ func TestFetchByOffsetOverFilesSortingOutOfOffsetOrder(t *testing.T) {
}
}
}
// A callback that says it is done stops the read inside a chunk, before later chunks
// and files are fetched, and the returned position follows the last entry delivered.
func TestReadFromDiskStopsWhenTheCallbackIsDone(t *testing.T) {
volume := serveChunks(t, map[string][]byte{})
files := offsetLogFiles(t, volume, 0, 3, false)
// the first file holds offsets 0..3 in two chunks; the next file holds 4 and 5
volume.Lock()
volume.chunks["/3,00ff"] = volume.chunks["/3,0001"]
volume.Unlock()
files[0].Chunks = append(files[0].Chunks, &filer_pb.FileChunk{FileId: "3,00ff", Size: files[1].Chunks[0].Size})
files[0].Extended[mq.ExtendedAttrOffsetMax] = binary.BigEndian.AppendUint64(nil, 3)
files = []*filer_pb.Entry{files[0], files[2]}
_, client := serveFiler(t, volume, files...)
for _, stopAt := range []int64{0, 2, 3} {
read := mergeReadFuncs(testPartitionReader(client), func(log_buffer.MessagePosition, int64, log_buffer.EachLogEntryFuncType) (log_buffer.MessagePosition, bool, error) {
t.Errorf("stopped at %d, then read parquet files", stopAt)
return log_buffer.MessagePosition{}, false, nil
})
volume.Lock()
clear(volume.fetched)
volume.Unlock()
var got []int64
position, isDone, err := read(log_buffer.NewMessagePositionFromOffset(0), 0, func(logEntry *filer_pb.LogEntry) (bool, error) {
got = append(got, logEntry.Offset)
return logEntry.Offset == stopAt, nil
})
if err != nil {
t.Fatal(err)
}
if want := offsetsFrom(0, stopAt+1); !slices.Equal(got, want) || !isDone {
t.Errorf("stopping at %d read %v done %v, want %v done", stopAt, got, isDone, want)
}
if position.Offset != stopAt+1 {
t.Errorf("stopping at %d returned offset %d", stopAt, position.Offset)
}
if stopAt < 2 && volume.fetchCount("3,00ff") != 0 {
t.Errorf("stopping at %d fetched the next chunk", stopAt)
}
if volume.fetchCount("3,0002") != 0 {
t.Errorf("stopping at %d fetched the next file", stopAt)
}
}
var got []int64
position, isDone, err := testPartitionReader(client)(log_buffer.NewMessagePosition(1, -2), 0, func(logEntry *filer_pb.LogEntry) (bool, error) {
got = append(got, logEntry.TsNs)
return len(got) == 3, nil
})
if err != nil {
t.Fatal(err)
}
if len(got) != 3 || !isDone || position.Time.UnixNano() != got[2] {
t.Errorf("by time read %v done %v, position %d", got, isDone, position.Time.UnixNano())
}
}