From 74a0810bb02e8e08744bef5d0af6c32e86ec8a33 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 1 Dec 2026 01:11:57 -0800 Subject: [PATCH] mq logstore: return the latest time a timestamp read delivered A timestamp read returned the last processed file's last message time as its position, and each chunk's result replaced the file's. Files are read in name order, and a slow leader's window can sort after a file holding later messages, so the position went back and the next read delivered those later messages again; a trailing chunk that delivered nothing also reset a file's time to zero. The position is now the highest time delivered across chunks and files. Co-Authored-By: Claude Opus 5.5 --- weed/mq/logstore/read_log_from_disk.go | 13 ++++-- weed/mq/logstore/read_log_from_disk_test.go | 47 +++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/weed/mq/logstore/read_log_from_disk.go b/weed/mq/logstore/read_log_from_disk.go index 7c61fa47bc..436fad9219 100644 --- a/weed/mq/logstore/read_log_from_disk.go +++ b/weed/mq/logstore/read_log_from_disk.go @@ -218,7 +218,10 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top continue } // Positive cache hit - data exists - if processedTsNs, err = eachChunkFn(cachedData, eachLogEntryFn, starTsNs, stopTsNs, startOffset, isOffsetBased); err != nil { + var chunkTsNs int64 + chunkTsNs, 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 } @@ -238,7 +241,10 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top // Store in cache for future reads fileCache.Put(cacheKey, data, startOffset) - if processedTsNs, err = eachChunkFn(data, eachLogEntryFn, starTsNs, stopTsNs, startOffset, isOffsetBased); err != nil { + var chunkTsNs int64 + chunkTsNs, err = eachChunkFn(data, eachLogEntryFn, starTsNs, stopTsNs, startOffset, isOffsetBased) + processedTsNs = max(processedTsNs, chunkTsNs) + if err != nil { glog.V(1).Infof("eachChunkFn failed: %v", err) return } @@ -370,7 +376,8 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top return lastReadPosition, isDone, err } if fileTsNs > 0 { - processedTsNs = fileTsNs + // files sort by name, which a slow leader's window can hold out of time order + processedTsNs = max(processedTsNs, fileTsNs) filesProcessed++ } diff --git a/weed/mq/logstore/read_log_from_disk_test.go b/weed/mq/logstore/read_log_from_disk_test.go index 4f7df25b6f..0a27587225 100644 --- a/weed/mq/logstore/read_log_from_disk_test.go +++ b/weed/mq/logstore/read_log_from_disk_test.go @@ -225,6 +225,53 @@ func TestReadFromDiskResumesPastAClampedName(t *testing.T) { } } +// When a slow leader's window sorts after a file holding later messages, the read +// returns the latest time it delivered, so the next read does not deliver it again. +func TestReadFromDiskReturnsTheLatestDeliveredTime(t *testing.T) { + second := time.Date(2026, 11, 30, 10, 0, 0, 0, time.UTC) + at := func(ms int) time.Time { return second.Add(time.Duration(ms) * time.Millisecond) } + timeAttrs := func(minTs, maxTs time.Time) map[string][]byte { + return map[string][]byte{ + mq.ExtendedAttrTimestampMin: binary.BigEndian.AppendUint64(nil, uint64(minTs.UnixNano())), + mq.ExtendedAttrTimestampMax: binary.BigEndian.AppendUint64(nil, uint64(maxTs.UnixNano())), + } + } + volume := serveChunks(t, map[string][]byte{ + "/3,01": logChunk(t, at(0).UnixNano(), at(4000).UnixNano()), + "/3,02": logChunk(t, at(-60000).UnixNano(), at(3000).UnixNano()), + }) + a := volume.file(topic.LogFileName(second, 0), "3,01") + a.Extended = timeAttrs(at(0), at(4000)) + b := volume.file(topic.LogFileName(second, 2), "3,02") + b.Extended = timeAttrs(at(-60000), at(3000)) + _, client := serveFiler(t, volume, a, b) + read := testPartitionReader(client) + + var got []int64 + collect := func(logEntry *filer_pb.LogEntry) (bool, error) { + got = append(got, logEntry.TsNs) + return false, nil + } + position, _, err := read(log_buffer.NewMessagePosition(at(2000).UnixNano(), -2), 0, collect) + if err != nil { + t.Fatal(err) + } + if want := []int64{at(4000).UnixNano(), at(3000).UnixNano()}; !slices.Equal(got, want) { + t.Fatalf("resumed at 10:00:02, read %v, want %v", got, want) + } + if position.Time.Before(at(4000)) { + t.Errorf("returned position %v, before the delivered %v", position.Time, at(4000)) + } + + got = nil + if _, _, err := read(position, 0, collect); err != nil { + t.Fatal(err) + } + if len(got) != 0 { + t.Errorf("read from the returned position delivered %v again", got) + } +} + // Resuming inside a window must read the rest of the file that started before the // resume second, and nothing from files that ended before it. func TestReadFromDiskResumesInsideAnEarlierFile(t *testing.T) {