mq logstore: deliver a parquet file's rows in offset order for an offset read

Compaction writes rows in the order it reads its source logs, which need not be
offset order, so a file could hold offsets 1000, 1001 and then 0 to 999. An
offset read that stopped after 1001 returned 1002 as the next offset and the
resume skipped 0 to 999. An offset read now delivers a file's eligible rows
sorted by offset, and a stopped or failed read resumes at the offset after its
last delivered row, kept at or below the first offset of every file it did not
finish.

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

No files matched your search

+32 -2
View File
@@ -169,7 +169,7 @@ func (f *compactionFiler) putLogFile(t *testing.T, start time.Time, minOffset in
f.putLogEntries(t, start, entries...)
}
// putLogEntries stores a flushed window holding entries, which are numbered in order.
// putLogEntries stores a flushed window holding entries, named by the first one's offset.
func (f *compactionFiler) putLogEntries(t *testing.T, start time.Time, entries ...*filer_pb.LogEntry) {
var buf []byte
for _, entry := range entries {
@@ -180,7 +180,10 @@ func (f *compactionFiler) putLogEntries(t *testing.T, start time.Time, entries .
buf = binary.BigEndian.AppendUint32(buf, uint32(len(data)))
buf = append(buf, data...)
}
minOffset, maxOffset := entries[0].Offset, entries[len(entries)-1].Offset
minOffset, maxOffset := entries[0].Offset, entries[0].Offset
for _, entry := range entries {
minOffset, maxOffset = min(minOffset, entry.Offset), max(maxOffset, entry.Offset)
}
fileId := newCompactionFileId()
f.mu.Lock()
defer f.mu.Unlock()
@@ -627,3 +630,30 @@ func TestMergedReadFailsWhenParquetCannotBeSetUp(t *testing.T) {
t.Fatal("a read reaching parquet with an unusable schema returned no error")
}
}
// Compaction writes rows in the order it reads its sources, which need not be offset
// order; a stopped offset read must not resume past an offset it did not deliver.
func TestParquetOffsetReadStoppedEarlySkipsNothing(t *testing.T) {
f, client := newCompactionFiler(t)
f.putTopicConf(t)
base := time.Date(2026, 11, 30, 10, 0, 0, 0, time.UTC).UnixNano()
var entries []*filer_pb.LogEntry
for i, offset := range append([]int64{1000, 1001}, offsetsFrom(0, 1000)...) {
entries = append(entries, &filer_pb.LogEntry{TsNs: base + int64(i), Key: []byte(fmt.Sprintf("k%d", offset)), Data: []byte("v"), Offset: offset})
}
f.putLogEntries(t, time.Unix(0, base), entries...)
compactSchemaless(t, client)
f.removeLogFiles()
var got []int64
position, isDone, err := GenParquetReadFunc(client, compactionTopic, compactionPartition)(log_buffer.NewMessagePositionFromOffset(0), 0, func(entry *filer_pb.LogEntry) (bool, error) {
got = append(got, entry.Offset)
return len(got) == 2, nil
})
if err != nil || !isDone {
t.Fatalf("done %v err %v", isDone, err)
}
if !slices.Equal(got, []int64{0, 1}) || position.Offset != 2 {
t.Errorf("stopped after %v, resuming at %d", got, position.Offset)
}
}
+12
View File
@@ -173,6 +173,18 @@ func stoppedReadTsNs(startTsNs, lastTsNs int64, unread []*filer_pb.Entry) int64
return max(resumeTsNs, startTsNs)
}
// stoppedReadOffset is where an offset read that stopped after delivering lastOffset
// resumes without skipping anything, given that each file delivers in offset order.
func stoppedReadOffset(startOffset, lastOffset int64, unread []*filer_pb.Entry) int64 {
next := lastOffset + 1
for _, file := range unread {
if minOffset, _, ok := logFileOffsets(file); ok {
next = min(next, minOffset)
}
}
return max(next, startOffset)
}
// logFileTsNs is the message time a log file records under key, if it records one.
func logFileTsNs(entry *filer_pb.Entry, key string) (int64, bool) {
if b := entry.Extended[key]; len(b) == 8 {
+12 -9
View File
@@ -199,9 +199,12 @@ func newParquetReader(filerClient filer_pb.FilerClient, t topic.Topic, p topic.P
}
}
// rows of windows flushed across a reload are not in time order; delivered in
// order, what a stopped read delivered is all of the file up to where it stopped
if !isOffsetBased {
// rows of windows flushed across a reload are not in time order, and compaction
// wrote sources in name order; delivered in order, what a stopped read delivered
// is all of the file up to where it stopped
if isOffsetBased {
slices.SortStableFunc(eligible, func(a, b *filer_pb.LogEntry) int { return cmp.Compare(a.Offset, b.Offset) })
} else {
slices.SortStableFunc(eligible, func(a, b *filer_pb.LogEntry) int { return cmp.Compare(a.TsNs, b.TsNs) })
}
for _, logEntry := range eligible {
@@ -264,27 +267,27 @@ func newParquetReader(filerClient filer_pb.FilerClient, t topic.Topic, p topic.P
sortByOffset(candidateFiles)
}
nextOffset := int64(-1)
lastDeliveredTsNs := startTsNs
lastDeliveredTsNs, lastDeliveredOffset := startTsNs, startOffset-1
deliver := func(logEntry *filer_pb.LogEntry) (bool, error) {
nextOffset = max(nextOffset, logEntry.Offset+1)
lastDeliveredTsNs = logEntry.TsNs
lastDeliveredTsNs, lastDeliveredOffset = logEntry.TsNs, logEntry.Offset
return eachLogEntryFn(logEntry)
}
for i, entry := range candidateFiles {
var fileTsNs int64
fileTsNs, isDone, err = eachFileFn(entry, deliver, startTsNs, stopTsNs, startOffset, endOffset, isOffsetBased)
processedTsNs = max(processedTsNs, fileTsNs)
if !isOffsetBased && (err != nil || isDone) {
if err != nil || isDone {
// a failed file may have delivered none of its rows
unread := candidateFiles[i+1:]
if err != nil {
unread = candidateFiles[i:]
}
if isOffsetBased {
return log_buffer.NewMessagePositionFromOffset(stoppedReadOffset(startOffset, lastDeliveredOffset, unread)), isDone, err
}
return log_buffer.NewMessagePosition(stoppedReadTsNs(startTsNs, lastDeliveredTsNs, unread), -2), isDone, err
}
if err != nil || isDone {
break
}
}
if isOffsetBased {
lastReadPosition = startPosition