mq broker: stamp a loaded partition's messages after the newest stored one

Within one LogBuffer timestamps only increase, but a new buffer starts LastTsNs
at 0, so a leader whose clock is behind the previous leader's stamped new
messages earlier than what was already stored, and a subscriber resuming by
timestamp from the last stored message dropped them. OffsetRange now also
records the newest ts_max of the partition's log and parquet files, and loading
a partition seeds LastTsNs with it, so the buffer bumps new messages past it.

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:17:57 -08:00
1 parent be7b803379
commit 49d6091e59
3 files changed
+48 -4

No files matched your search

@@ -42,6 +42,39 @@ func TestLoadedPartitionStartsAtTheEarliestStoredOffset(t *testing.T) {
}
}
// A leader whose clock is behind the stored messages stamps new ones after them, so
// a timestamp resume from the last stored message still reads what it publishes.
func TestLoadedPartitionStampsAfterTheNewestStoredMessage(t *testing.T) {
f, b := newFencingFiler(t)
stored := time.Now().Add(time.Hour)
name := f.putLogFile(stored.Truncate(time.Second), 0, 9, stored)
entry := f.entry(topic.PartitionDir(fenceTestTopic, fenceTestPartition) + "/" + name)
f.mu.Lock()
entry.Extended[mq.ExtendedAttrTimestampMin] = binary.BigEndian.AppendUint64(nil, uint64(stored.Truncate(time.Second).UnixNano()))
entry.Extended[mq.ExtendedAttrTimestampMax] = binary.BigEndian.AppendUint64(nil, uint64(stored.UnixNano()))
f.mu.Unlock()
lp, err := b.newLeaderPartition(fenceTestTopic, fenceTestPartition, &mq_pb.BrokerPartitionAssignment{Partition: fenceTestPartition.ToPbPartition(), Epoch: 1})
if err != nil {
t.Fatal(err)
}
defer lp.Shutdown()
if _, err := lp.PublishWithOffset(&mq_pb.DataMessage{Key: []byte("k"), Value: []byte("new"), TsNs: time.Now().UnixNano()}); err != nil {
t.Fatal(err)
}
var got []string
if _, _, err := lp.LogBuffer.LoopProcessLogData("test", log_buffer.NewMessagePosition(stored.UnixNano(), -2), 0, func() bool { return false }, func(logEntry *filer_pb.LogEntry) (bool, error) {
got = append(got, string(logEntry.Data))
return true, nil
}); err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0] != "new" {
t.Errorf("resumed after the stored messages, read %q, want the new message", got)
}
}
// A loaded partition's buffer assigns offsets, so it is the high water mark.
func TestPartitionOffsetInfoFromLoadedBuffer(t *testing.T) {
b := newUnelectedBroker()
@@ -233,14 +233,16 @@ func (b *MessageQueueBroker) ensureTopicActiveAssignments(t topic.Topic, conf *m
}
// initializePartitionOffsetFromExistingData continues the partition's offsets above
// what is already stored, starts its log at the earliest stored offset, and names
// its windows no earlier than the newest stored log file. A partition whose stored
// offsets cannot be read is not served.
// what is already stored, starts its log at the earliest stored offset, names its
// windows no earlier than the newest stored log file, and stamps new messages after
// the newest stored one, which a leader with a slower clock would otherwise stamp
// earlier. A partition whose stored offsets cannot be read is not served.
func (b *MessageQueueBroker) initializePartitionOffsetFromExistingData(localPartition *topic.LocalPartition, t topic.Topic, partition topic.Partition) error {
var earliestOffset int64
err := localPartition.LogBuffer.InitializeOffsetFromExistingData(func() (int64, error) {
r, err := b.getOffsetRangeFromChunkMetadata(t, partition)
localPartition.Fence.NoteLogFileTime(r.NewestLogFileTime())
localPartition.LogBuffer.LastTsNs.Store(r.NewestTsNs())
var highWaterMark int64
earliestOffset, highWaterMark = r.Bounds()
return highWaterMark - 1, err
+10 -1
View File
@@ -13,11 +13,12 @@ import (
)
// OffsetRange accumulates the offset ranges recorded on a partition's files,
// and the newest start second a log file is named by.
// the newest start second a log file is named by, and the newest message time.
type OffsetRange struct {
earliest, latest int64
hasEarliest, hasLatest bool
newestLogFileTime time.Time
newestTsNs int64
// data files that record no range, which may hold offsets below every recorded one
unrangedLog bool
unrangedParquet [][]string
@@ -49,6 +50,9 @@ func (r *OffsetRange) Add(entry *filer_pb.Entry) {
r.latest, r.hasLatest = v, true
}
}
if b := extended[ExtendedAttrTimestampMax]; len(b) == 8 {
r.newestTsNs = max(r.newestTsNs, int64(binary.BigEndian.Uint64(b)))
}
// compacted files, the fence and other entries do not parse as log file names
if t, err := topic.ParseLogFileTime(entry.Name); err == nil && t.After(r.newestLogFileTime) {
r.newestLogFileTime = t
@@ -77,6 +81,11 @@ func (r *OffsetRange) NewestLogFileTime() time.Time {
return r.newestLogFileTime
}
// NewestTsNs is the latest message time a log or parquet file records, zero when none does.
func (r *OffsetRange) NewestTsNs() int64 {
return r.newestTsNs
}
func (r *OffsetRange) hasUnknownOffsets() bool {
if r.unrangedLog {
return true