mq: bound a log file by its first message time, not its name

Now that a window can be named after a later second than its messages to
keep names in offset order, the disk reader's stop filter, which dropped
every file whose name second was after the stop time, missed windows whose
messages were inside a time-bounded read. A flush now records the window's
first message timestamp on the log file as ts_min, the attribute parquet
files already carry, and the stop filter compares that when present. Files
without it were written before names could be moved later, so their name
still bounds them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-01 00:51:25 -08:00
1 parent b9cb0999e8
commit e2c3ea385b
5 files changed
+34 -12

No files matched your search

+1 -1
View File
@@ -119,7 +119,7 @@ func (b *MessageQueueBroker) PublishFollowMe(stream mq_pb.SeaweedMessaging_Publi
targetFile := fmt.Sprintf("%s/%s", partitionDir, topic.LogFileName(startTime, 0))
// A follower holds no fence, so the fenced writer refuses its copy.
if err := b.writeLogFile(context.Background(), nil, targetFile, mem.buf); err != nil {
if err := b.writeLogFile(context.Background(), nil, targetFile, startTime, mem.buf); err != nil {
glog.Warningf("dropping remaining data at %v %v size %d: %v", t, p, len(mem.buf), err)
continue
}
@@ -38,7 +38,7 @@ func (b *MessageQueueBroker) genLogFlushFunc(t topic.Topic, p topic.Partition, f
if ctx == nil {
ctx = context.Background()
}
if err := b.writeLogFile(ctx, fence, targetFile, buf, minOffset, maxOffset); err != nil {
if err := b.writeLogFile(ctx, fence, targetFile, startTime, buf, minOffset, maxOffset); err != nil {
// Once cancelled or fenced, every later window drops too, so neither the
// watermark nor the follower's flush notice passes a dropped window.
logBuffer.NoteFlushDropped(len(buf))
+8 -7
View File
@@ -29,9 +29,10 @@ func offsetRange(offsetArgs []int64) (minOffset, maxOffset int64, ok bool) {
return minOffset, maxOffset, minOffset >= 0 && maxOffset >= minOffset
}
// writeLogFile stores one window as a new file while fence is still the partition's fence.
// writeLogFile stores one window, whose first message is at startTime, as a new file
// while fence is still the partition's fence.
// It uploads once and retries only the transaction, under one request id.
func (b *MessageQueueBroker) writeLogFile(ctx context.Context, fence *topic.PartitionFence, targetFile string, data []byte, offsetArgs ...int64) error {
func (b *MessageQueueBroker) writeLogFile(ctx context.Context, fence *topic.PartitionFence, targetFile string, startTime time.Time, data []byte, offsetArgs ...int64) error {
if fence == nil {
return fmt.Errorf("%s: this broker holds no fence: %w", targetFile, errFenceLost)
}
@@ -66,12 +67,12 @@ func (b *MessageQueueBroker) writeLogFile(ctx context.Context, fence *topic.Part
Gid: uint32(os.Getgid()),
},
}
// the name may be later than the window's first message, so readers bound by time use this
entry.Extended = map[string][]byte{mq.ExtendedAttrTimestampMin: binary.BigEndian.AppendUint64(nil, uint64(startTime.UnixNano()))}
if minOffset, maxOffset, ok := offsetRange(offsetArgs); ok {
entry.Extended = map[string][]byte{
mq.ExtendedAttrOffsetMin: binary.BigEndian.AppendUint64(nil, uint64(minOffset)),
mq.ExtendedAttrOffsetMax: binary.BigEndian.AppendUint64(nil, uint64(maxOffset)),
mq.ExtendedAttrBufferStart: binary.BigEndian.AppendUint64(nil, uint64(minOffset)),
}
entry.Extended[mq.ExtendedAttrOffsetMin] = binary.BigEndian.AppendUint64(nil, uint64(minOffset))
entry.Extended[mq.ExtendedAttrOffsetMax] = binary.BigEndian.AppendUint64(nil, uint64(maxOffset))
entry.Extended[mq.ExtendedAttrBufferStart] = binary.BigEndian.AppendUint64(nil, uint64(minOffset))
}
condition := fenceTokenCondition(fence.Token)
+14 -2
View File
@@ -151,7 +151,7 @@ func TestFlushWritesOneFencedFilePerWindow(t *testing.T) {
if len(written.Chunks) != 1 || written.Attributes.FileSize != written.Chunks[0].Size || written.Attributes.FileMode != 0644 {
t.Errorf("written entry %v", written)
}
for key, want := range map[string]uint64{mq.ExtendedAttrOffsetMin: 200, mq.ExtendedAttrOffsetMax: 399, mq.ExtendedAttrBufferStart: 200} {
for key, want := range map[string]uint64{mq.ExtendedAttrOffsetMin: 200, mq.ExtendedAttrOffsetMax: 399, mq.ExtendedAttrBufferStart: 200, mq.ExtendedAttrTimestampMin: uint64(start.UnixNano())} {
if got := written.Extended[key]; len(got) != 8 || binary.BigEndian.Uint64(got) != want {
t.Errorf("%s = %v, want %d", key, got, want)
}
@@ -233,7 +233,7 @@ func TestFlushStopsWhenTheFenceIsTaken(t *testing.T) {
func TestFollowerCannotWriteWithoutAFence(t *testing.T) {
f, b := newFencingFiler(t)
err := b.writeLogFile(context.Background(), nil, topic.PartitionDir(fenceTestTopic, fenceTestPartition)+"/x", []byte("data"))
err := b.writeLogFile(context.Background(), nil, topic.PartitionDir(fenceTestTopic, fenceTestPartition)+"/x", time.Now(), []byte("data"))
if !errors.Is(err, errFenceLost) {
t.Fatalf("wrote without a fence: %v", err)
}
@@ -311,4 +311,16 @@ func TestFlushNamesSortAfterTheNewestStoredLogFile(t *testing.T) {
if got := readOffsets(2); !slices.Equal(got, []int64{2, 3}) {
t.Errorf("read %v from the new leader's window", got)
}
// the window's name is later than its messages, which a time-bounded read still finds
var got []int64
if _, _, err := read(log_buffer.NewMessagePosition(now.Add(-time.Minute).UnixNano(), -2), now.Add(time.Millisecond).UnixNano(), func(logEntry *filer_pb.LogEntry) (bool, error) {
got = append(got, logEntry.Offset)
return false, nil
}); err != nil {
t.Fatal(err)
}
if !slices.Equal(got, []int64{2, 3}) {
t.Errorf("time-bounded read %v, want the new leader's window", got)
}
}
+10 -1
View File
@@ -90,6 +90,15 @@ func logFileOffsets(entry *filer_pb.Entry) (minOffset, maxOffset int64, ok bool)
return int64(binary.BigEndian.Uint64(minBytes)), int64(binary.BigEndian.Uint64(maxBytes)), true
}
// logFileStartsAfter reports whether a log file's first message is after stopTime. A name
// can be later than that message; files recording no first message time were named by it.
func logFileStartsAfter(entry *filer_pb.Entry, stopTime time.Time) bool {
if b := entry.Extended[mq.ExtendedAttrTimestampMin]; len(b) == 8 {
return int64(binary.BigEndian.Uint64(b)) > stopTime.UnixNano()
}
return topic.LogFileTimePrefix(entry.Name) > stopTime.UTC().Format(topic.TIME_FORMAT)
}
func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p topic.Partition) log_buffer.LogReadFromDiskFuncType {
partitionDir := topic.PartitionDir(t, p)
index := &logFileIndex{}
@@ -269,7 +278,7 @@ func GenLogOnDiskReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p top
return nil
}
listed = append(listed, entry)
if stopTsNs != 0 && topic.LogFileTimePrefix(entry.Name) > stopTime.UTC().Format(topic.TIME_FORMAT) {
if stopTsNs != 0 && logFileStartsAfter(entry, stopTime) {
return nil
}