mq broker: bound older files' messages by their write time when seeding stamps

A loaded partition seeds its buffer's last timestamp from the newest ts_max its
files record, but files flushed before ts_max was recorded contributed nothing,
so a slow-clock leader could still stamp new messages below what such a file
holds. A data file recording no ts_max now bounds its messages by the end of the
second its mtime names, since it was written after they were stamped. A message
whose time a client set later than the file's write is still not bounded; the
doc comment says so.

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:51:07 -08:00
1 parent 96fbacae68
commit f88f59527f
3 files changed
+42 -29

No files matched your search

+33 -26
View File
@@ -43,35 +43,42 @@ 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.
// a timestamp resume from the last stored message still reads what it publishes. An
// older file recording no message times is bounded by when it was written.
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()
for _, recordsTimes := range []bool{true, false} {
f, b := newFencingFiler(t)
// the flush on shutdown notifies the partition through the topic manager
b.localTopicManager = topic.NewLocalTopicManager()
stored := time.Now().Add(time.Hour)
name := f.putLogFile(stored.Truncate(time.Second), 0, 9, stored)
if recordsTimes {
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)
}
lp, err := b.newLeaderPartition(fenceTestTopic, fenceTestPartition, &mq_pb.BrokerPartitionAssignment{Partition: fenceTestPartition.ToPbPartition(), Epoch: 1})
if err != nil {
t.Fatal(err)
}
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)
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)
}
lp.Shutdown()
if len(got) != 1 || got[0] != "new" {
t.Errorf("file records message times %v: resumed after the stored messages, read %q, want the new message", recordsTimes, got)
}
}
}
@@ -235,8 +235,9 @@ 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, 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.
// the newest stored one (see OffsetRange.NewestTsNs), 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) {
+6 -1
View File
@@ -52,6 +52,9 @@ func (r *OffsetRange) Add(entry *filer_pb.Entry) {
}
if b := extended[ExtendedAttrTimestampMax]; len(b) == 8 {
r.newestTsNs = max(r.newestTsNs, int64(binary.BigEndian.Uint64(b)))
} else if mtime := entry.GetAttributes().GetMtime(); notDataErr == nil && mtime > 0 {
// an older file was written after its messages were stamped, within its mtime's second
r.newestTsNs = max(r.newestTsNs, time.Unix(mtime+1, 0).UnixNano())
}
// 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) {
@@ -81,7 +84,9 @@ 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.
// NewestTsNs bounds the stored messages' times: the latest a log or parquet file
// records, or for a file recording none, the end of the second it was written in.
// A message whose time a client set later than its file was written is not bounded.
func (r *OffsetRange) NewestTsNs() int64 {
return r.newestTsNs
}