diff --git a/weed/mq/broker/broker_log_buffer_offset_test.go b/weed/mq/broker/broker_log_buffer_offset_test.go index 9ee8a4943a..3578130bfd 100644 --- a/weed/mq/broker/broker_log_buffer_offset_test.go +++ b/weed/mq/broker/broker_log_buffer_offset_test.go @@ -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() diff --git a/weed/mq/broker/broker_topic_conf_read_write.go b/weed/mq/broker/broker_topic_conf_read_write.go index 919e02a3ea..577e9a22b7 100644 --- a/weed/mq/broker/broker_topic_conf_read_write.go +++ b/weed/mq/broker/broker_topic_conf_read_write.go @@ -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 diff --git a/weed/mq/offset_range.go b/weed/mq/offset_range.go index d941dc8da2..5202c477ed 100644 --- a/weed/mq/offset_range.go +++ b/weed/mq/offset_range.go @@ -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