From f35caf8be3aa0a65dc09115bb41d3d32d7e5cb09 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 30 Nov 2026 13:45:28 -0800 Subject: [PATCH] mq: read a recorded offset range that ends at offset 0 The broker and the Kafka gateway each scanned a partition's files for the high water mark and counted a maximum only when it was above 0, so a partition holding one message still read as empty. Both now share mq.OffsetRange, which tracks whether a range was found apart from its value. --- .../mq/broker/broker_topic_conf_read_write.go | 39 ++----------------- weed/mq/kafka/integration/broker_client.go | 32 ++------------- weed/mq/offset_range.go | 34 ++++++++++++++++ weed/mq/offset_range_test.go | 36 +++++++++++++++++ 4 files changed, 76 insertions(+), 65 deletions(-) create mode 100644 weed/mq/offset_range.go create mode 100644 weed/mq/offset_range_test.go diff --git a/weed/mq/broker/broker_topic_conf_read_write.go b/weed/mq/broker/broker_topic_conf_read_write.go index 9e908e485d..278bda94a4 100644 --- a/weed/mq/broker/broker_topic_conf_read_write.go +++ b/weed/mq/broker/broker_topic_conf_read_write.go @@ -2,7 +2,6 @@ package broker import ( "context" - "encoding/binary" "errors" "fmt" "io" @@ -315,8 +314,7 @@ func (b *MessageQueueBroker) getOffsetRangeFromChunkMetadata(t topic.Topic, part // Scan all message files to find the highest offset_max and lowest offset_min partitionPath := fmt.Sprintf("%s/%s", versionPath, partitionDir) - highWaterMark = 0 - earliestOffset = -1 // -1 indicates no data found yet + var offsetRange mq.OffsetRange err = b.fca.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { stream, err := client.ListEntries(context.Background(), &filer_pb.ListEntriesRequest{ @@ -336,30 +334,7 @@ func (b *MessageQueueBroker) getOffsetRangeFromChunkMetadata(t topic.Topic, part } if resp.Entry != nil && !resp.Entry.IsDirectory && resp.Entry.Name != "checkpoint.offset" { // Check for offset ranges in Extended attributes (both log files and parquet files) - if resp.Entry.Extended != nil { - fileType := "log" - if strings.HasSuffix(resp.Entry.Name, ".parquet") { - fileType = "parquet" - } - - // Track maximum offset for high water mark - if maxOffsetBytes, exists := resp.Entry.Extended[mq.ExtendedAttrOffsetMax]; exists && len(maxOffsetBytes) == 8 { - maxOffset := int64(binary.BigEndian.Uint64(maxOffsetBytes)) - if maxOffset > highWaterMark { - highWaterMark = maxOffset - } - glog.V(2).Infof("%s file %s has offset_max=%d", fileType, resp.Entry.Name, maxOffset) - } - - // Track minimum offset for earliest offset - if minOffsetBytes, exists := resp.Entry.Extended[mq.ExtendedAttrOffsetMin]; exists && len(minOffsetBytes) == 8 { - minOffset := int64(binary.BigEndian.Uint64(minOffsetBytes)) - if earliestOffset == -1 || minOffset < earliestOffset { - earliestOffset = minOffset - } - glog.V(2).Infof("%s file %s has offset_min=%d", fileType, resp.Entry.Name, minOffset) - } - } + offsetRange.Add(resp.Entry.Extended) } } return nil @@ -368,15 +343,7 @@ func (b *MessageQueueBroker) getOffsetRangeFromChunkMetadata(t topic.Topic, part return 0, 0, fmt.Errorf("failed to scan message files: %v", err) } - // High water mark is the next offset after the highest written offset - if highWaterMark > 0 { - highWaterMark++ - } - - // If no data found, set earliest offset to 0 - if earliestOffset == -1 { - earliestOffset = 0 - } + earliestOffset, highWaterMark = offsetRange.Bounds() glog.V(0).Infof("Offset range for topic %s partition %s: earliest=%d, highWaterMark=%d", t, partition, earliestOffset, highWaterMark) return earliestOffset, highWaterMark, nil diff --git a/weed/mq/kafka/integration/broker_client.go b/weed/mq/kafka/integration/broker_client.go index 428cfdffef..3a09d6f2da 100644 --- a/weed/mq/kafka/integration/broker_client.go +++ b/weed/mq/kafka/integration/broker_client.go @@ -2,7 +2,6 @@ package integration import ( "context" - "encoding/binary" "fmt" "io" "strings" @@ -312,8 +311,7 @@ func (bc *BrokerClient) getOffsetRangeFromChunkMetadata(topic string, partition // Scan all message files to find the highest offset_max and lowest offset_min partitionPath := fmt.Sprintf("%s/%s", versionPath, partitionDir) - highWaterMark = 0 - earliestOffset = -1 // -1 indicates no data found yet + var offsetRange mq.OffsetRange err = bc.filerClientAccessor.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { stream, err := client.ListEntries(context.Background(), &filer_pb.ListEntriesRequest{ @@ -333,23 +331,7 @@ func (bc *BrokerClient) getOffsetRangeFromChunkMetadata(topic string, partition } if resp.Entry != nil && !resp.Entry.IsDirectory && resp.Entry.Name != "checkpoint.offset" { // Check for offset ranges in Extended attributes (both log files and parquet files) - if resp.Entry.Extended != nil { - // Track maximum offset for high water mark - if maxOffsetBytes, exists := resp.Entry.Extended[mq.ExtendedAttrOffsetMax]; exists && len(maxOffsetBytes) == 8 { - maxOffset := int64(binary.BigEndian.Uint64(maxOffsetBytes)) - if maxOffset > highWaterMark { - highWaterMark = maxOffset - } - } - - // Track minimum offset for earliest offset - if minOffsetBytes, exists := resp.Entry.Extended[mq.ExtendedAttrOffsetMin]; exists && len(minOffsetBytes) == 8 { - minOffset := int64(binary.BigEndian.Uint64(minOffsetBytes)) - if earliestOffset == -1 || minOffset < earliestOffset { - earliestOffset = minOffset - } - } - } + offsetRange.Add(resp.Entry.Extended) } } return nil @@ -358,15 +340,7 @@ func (bc *BrokerClient) getOffsetRangeFromChunkMetadata(topic string, partition return 0, 0, fmt.Errorf("failed to scan message files: %v", err) } - // High water mark is the next offset after the highest written offset - if highWaterMark > 0 { - highWaterMark++ - } - - // If no data found, set earliest offset to 0 - if earliestOffset == -1 { - earliestOffset = 0 - } + earliestOffset, highWaterMark = offsetRange.Bounds() return earliestOffset, highWaterMark, nil } diff --git a/weed/mq/offset_range.go b/weed/mq/offset_range.go new file mode 100644 index 0000000000..5e2dbe9c03 --- /dev/null +++ b/weed/mq/offset_range.go @@ -0,0 +1,34 @@ +package mq + +import "encoding/binary" + +// OffsetRange accumulates the offset ranges recorded on a partition's files. +type OffsetRange struct { + earliest, latest int64 + hasEarliest, hasLatest bool +} + +func (r *OffsetRange) Add(extended map[string][]byte) { + if b, ok := extended[ExtendedAttrOffsetMin]; ok && len(b) == 8 { + if v := int64(binary.BigEndian.Uint64(b)); !r.hasEarliest || v < r.earliest { + r.earliest, r.hasEarliest = v, true + } + } + if b, ok := extended[ExtendedAttrOffsetMax]; ok && len(b) == 8 { + if v := int64(binary.BigEndian.Uint64(b)); !r.hasLatest || v > r.latest { + r.latest, r.hasLatest = v, true + } + } +} + +// Bounds is the earliest recorded offset and the offset after the latest, +// both 0 when no file records a range. +func (r *OffsetRange) Bounds() (earliestOffset, highWaterMark int64) { + if r.hasEarliest { + earliestOffset = r.earliest + } + if r.hasLatest { + highWaterMark = r.latest + 1 + } + return +} diff --git a/weed/mq/offset_range_test.go b/weed/mq/offset_range_test.go new file mode 100644 index 0000000000..7970d03f5e --- /dev/null +++ b/weed/mq/offset_range_test.go @@ -0,0 +1,36 @@ +package mq + +import ( + "encoding/binary" + "testing" +) + +func rangeAttrs(min, max int64) map[string][]byte { + attrs := map[string][]byte{ExtendedAttrOffsetMin: make([]byte, 8), ExtendedAttrOffsetMax: make([]byte, 8)} + binary.BigEndian.PutUint64(attrs[ExtendedAttrOffsetMin], uint64(min)) + binary.BigEndian.PutUint64(attrs[ExtendedAttrOffsetMax], uint64(max)) + return attrs +} + +func TestOffsetRangeBounds(t *testing.T) { + for _, tc := range []struct { + name string + files []map[string][]byte + earliest, hwm int64 + }{ + {"no files", nil, 0, 0}, + {"no ranges", []map[string][]byte{{}}, 0, 0}, + {"one message", []map[string][]byte{rangeAttrs(0, 0)}, 0, 1}, + {"first window", []map[string][]byte{rangeAttrs(0, 199)}, 0, 200}, + {"two windows", []map[string][]byte{rangeAttrs(200, 399), rangeAttrs(0, 199)}, 0, 400}, + {"trimmed head", []map[string][]byte{rangeAttrs(500, 599)}, 500, 600}, + } { + var r OffsetRange + for _, f := range tc.files { + r.Add(f) + } + if earliest, hwm := r.Bounds(); earliest != tc.earliest || hwm != tc.hwm { + t.Errorf("%s: Bounds() = %d, %d; want %d, %d", tc.name, earliest, hwm, tc.earliest, tc.hwm) + } + } +}