From d05821466040764a9cabb8bf48bd2919b2eadd91 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 1 Dec 2026 11:40:02 -0800 Subject: [PATCH] mq logstore: start an offset read of parquet files at the offset An offset read of the parquet files delivered every row from the first one on, and the stateless fetch fills a disk chunk with a callback that stops after a chunk's worth of entries outside it. Now that the reader honors that stop, a fetch at 1000 from a partition stored only in parquet stopped after rows 0-999 and returned nothing. An offset read now skips files whose recorded range ends below the offset or that record none, reads the rest in offset order, and skips rows below the offset before they reach the callback. It returns the offset after the last row delivered, not a timestamp position. Co-Authored-By: Claude Opus 5.5 --- weed/mq/logstore/compaction_test.go | 44 +++++++++++++++++++++++++ weed/mq/logstore/read_parquet_to_log.go | 37 +++++++++++++++++---- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/weed/mq/logstore/compaction_test.go b/weed/mq/logstore/compaction_test.go index 3b45ddec5b..1dcee5bb47 100644 --- a/weed/mq/logstore/compaction_test.go +++ b/weed/mq/logstore/compaction_test.go @@ -454,3 +454,47 @@ func TestParquetReadIncludesARowAtTheStopTime(t *testing.T) { t.Errorf("stopping at the file's first row read %v, want %v", got, want) } } + +// putWindows stores count offsets from first in windows of size, one second apart. +func (f *compactionFiler) putWindows(t *testing.T, first, count, size int) { + base := time.Date(2026, 11, 30, 10, 0, 0, 0, time.UTC) + for start := first; start < first+count; start += size { + var tsNs []int64 + for o := start; o < start+size; o++ { + tsNs = append(tsNs, base.UnixNano()+int64(o)) + } + f.putLogFile(t, base.Add(time.Duration(start/size)*time.Second), int64(start), tsNs...) + } +} + +// fetchOffsets fetches through the broker's merged reader as a Kafka fetch does. +func fetchOffsets(t *testing.T, client listingFilerClient, end, start int64) (got []int64) { + t.Helper() + lb := log_buffer.NewLogBuffer("test", time.Hour, nil, GenMergedReadFunc(client, compactionTopic, compactionPartition), func() {}) + t.Cleanup(lb.ShutdownLogBuffer) + if err := lb.InitializeOffsetFromExistingData(func() (int64, error) { return end - 1, nil }); err != nil { + t.Fatal(err) + } + messages, _, _, _, err := lb.ReadMessagesAtOffset(start, 3, 1<<20) + if err != nil { + t.Fatal(err) + } + for _, m := range messages { + got = append(got, m.Offset) + } + return got +} + +// A fetch fills a whole disk chunk around its offset and stops after as many entries +// outside it; rows of compacted files below the offset must not count toward that. +func TestFetchFromCompactedFilesPastTheirFirstChunk(t *testing.T) { + f, client := newCompactionFiler(t) + f.putTopicConf(t) + f.putWindows(t, 0, 2000, 500) + compactSchemaless(t, client) + f.removeLogFiles() + + if got := fetchOffsets(t, client, 2000, 1000); !slices.Equal(got, []int64{1000, 1001, 1002}) { + t.Errorf("fetch at 1000 returned %v", got) + } +} diff --git a/weed/mq/logstore/read_parquet_to_log.go b/weed/mq/logstore/read_parquet_to_log.go index 8dac6e014b..f4ffb0bcba 100644 --- a/weed/mq/logstore/read_parquet_to_log.go +++ b/weed/mq/logstore/read_parquet_to_log.go @@ -101,7 +101,7 @@ func GenParquetReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p topic } // eachFileFn reads a parquet file and calls eachLogEntryFn for each log entry - eachFileFn := func(entry *filer_pb.Entry, eachLogEntryFn log_buffer.EachLogEntryFuncType, starTsNs, stopTsNs int64) (processedTsNs int64, isDone bool, 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) { // create readerAt for the parquet file fileSize := filer.FileSize(entry) visibleIntervals, err := filer.NonOverlappingVisibleIntervals(context.Background(), lookupFileIdFn, entry.Chunks, 0, int64(fileSize)) @@ -139,15 +139,19 @@ func GenParquetReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p topic continue } - data, marshalErr := proto.Marshal(recordValue) - if marshalErr != nil { - return processedTsNs, false, fmt.Errorf("marshal record value: %w", marshalErr) - } - offset := int64(-1) if offsetValue := recordValue.Fields[SW_COLUMN_NAME_OFFSET]; hasOffsets && offsetValue != nil { offset = offsetValue.GetInt64Value() } + // rows below the offset must not reach a callback that stops after so many it cannot use + if isOffsetBased && offset < startOffset { + continue + } + + data, marshalErr := proto.Marshal(recordValue) + if marshalErr != nil { + return processedTsNs, false, fmt.Errorf("marshal record value: %w", marshalErr) + } logEntry := &filer_pb.LogEntry{ Key: recordValue.Fields[SW_COLUMN_NAME_KEY].GetBytesValue(), @@ -188,6 +192,7 @@ func GenParquetReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p topic index := &logFileIndex{} return func(startPosition log_buffer.MessagePosition, stopTsNs int64, eachLogEntryFn log_buffer.EachLogEntryFuncType) (lastReadPosition log_buffer.MessagePosition, isDone bool, err error) { startTsNs := startPosition.Time.UnixNano() + isOffsetBased, startOffset := startPosition.IsOffsetBased, startPosition.Offset var processedTsNs int64 var candidateFiles, listed []*filer_pb.Entry @@ -215,6 +220,9 @@ func GenParquetReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p topic if maxTsNs < startTsNs { return nil } + if _, maxOffset, ok := logFileOffsets(entry); isOffsetBased && (!ok || maxOffset < startOffset) { + return nil + } candidateFiles = append(candidateFiles, entry) return nil @@ -225,14 +233,29 @@ func GenParquetReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p topic return startPosition, false, err } index.add(listed) + if isOffsetBased { + sortByOffset(candidateFiles) + } + nextOffset := int64(-1) + deliver := func(logEntry *filer_pb.LogEntry) (bool, error) { + nextOffset = max(nextOffset, logEntry.Offset+1) + return eachLogEntryFn(logEntry) + } for _, entry := range candidateFiles { var fileTsNs int64 - fileTsNs, isDone, err = eachFileFn(entry, eachLogEntryFn, startTsNs, stopTsNs) + fileTsNs, isDone, err = eachFileFn(entry, deliver, startTsNs, stopTsNs, startOffset, isOffsetBased) processedTsNs = max(processedTsNs, fileTsNs) if err != nil || isDone { break } } + if isOffsetBased { + lastReadPosition = startPosition + if nextOffset > startOffset { + lastReadPosition = log_buffer.NewMessagePositionFromOffset(nextOffset) + } + return + } lastReadPosition = log_buffer.NewMessagePosition(processedTsNs, -2) return }