mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f35caf8be3 | ||
|
|
89f70bb012 |
No files matched your search
@@ -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
|
||||
|
||||
@@ -20,13 +20,18 @@ func (b *MessageQueueBroker) appendToFile(targetFile string, data []byte) error
|
||||
return b.appendToFileWithBufferIndex(targetFile, data, 0)
|
||||
}
|
||||
|
||||
func (b *MessageQueueBroker) appendToFileWithBufferIndex(targetFile string, data []byte, bufferOffset int64, offsetArgs ...int64) error {
|
||||
// Extract optional offset parameters (minOffset, maxOffset)
|
||||
var minOffset, maxOffset int64
|
||||
if len(offsetArgs) >= 2 {
|
||||
minOffset = offsetArgs[0]
|
||||
maxOffset = offsetArgs[1]
|
||||
// offsetRange reads the optional (minOffset, maxOffset) of a flushed window.
|
||||
// A partition's first window starts at offset 0.
|
||||
func offsetRange(offsetArgs []int64) (minOffset, maxOffset int64, ok bool) {
|
||||
if len(offsetArgs) < 2 {
|
||||
return 0, 0, false
|
||||
}
|
||||
minOffset, maxOffset = offsetArgs[0], offsetArgs[1]
|
||||
return minOffset, maxOffset, minOffset >= 0 && maxOffset >= minOffset
|
||||
}
|
||||
|
||||
func (b *MessageQueueBroker) appendToFileWithBufferIndex(targetFile string, data []byte, bufferOffset int64, offsetArgs ...int64) error {
|
||||
minOffset, maxOffset, hasOffsetRange := offsetRange(offsetArgs)
|
||||
|
||||
fileId, uploadResult, err2 := b.assignAndUpload(targetFile, data)
|
||||
if err2 != nil {
|
||||
@@ -60,7 +65,7 @@ func (b *MessageQueueBroker) appendToFileWithBufferIndex(targetFile string, data
|
||||
}
|
||||
|
||||
// Add offset range metadata for Kafka integration
|
||||
if minOffset > 0 && maxOffset >= minOffset {
|
||||
if hasOffsetRange {
|
||||
if entry.Extended == nil {
|
||||
entry.Extended = make(map[string][]byte)
|
||||
}
|
||||
@@ -108,7 +113,7 @@ func (b *MessageQueueBroker) appendToFileWithBufferIndex(targetFile string, data
|
||||
}
|
||||
|
||||
// Update offset range metadata for existing files
|
||||
if minOffset > 0 && maxOffset >= minOffset {
|
||||
if hasOffsetRange {
|
||||
// Update minimum offset if this chunk has a lower minimum
|
||||
if existingMinData, exists := entry.Extended[mq.ExtendedAttrOffsetMin]; exists && len(existingMinData) == 8 {
|
||||
existingMin := int64(binary.BigEndian.Uint64(existingMinData))
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package broker
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestOffsetRangeIncludesFirstWindow(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
args []int64
|
||||
min, max int64
|
||||
ok bool
|
||||
}{
|
||||
{nil, 0, 0, false},
|
||||
{[]int64{0, 199}, 0, 199, true},
|
||||
{[]int64{0, 0}, 0, 0, true},
|
||||
{[]int64{200, 399}, 200, 399, true},
|
||||
{[]int64{5, 3}, 5, 3, false},
|
||||
{[]int64{-1, 3}, -1, 3, false},
|
||||
} {
|
||||
min, max, ok := offsetRange(tc.args)
|
||||
if min != tc.min || max != tc.max || ok != tc.ok {
|
||||
t.Errorf("offsetRange(%v) = %d, %d, %v; want %d, %d, %v", tc.args, min, max, ok, tc.min, tc.max, tc.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user