mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
mq: record the offset range of a partition's first window
The flush wrote offset_min/offset_max only when minOffset > 0, using 0 as "no range given". A partition's first window starts at offset 0, so its file never got a range. A restarted broker derives the high water mark from those ranges, so a partition whose data was all in its first file read as empty.
This commit is contained in:
1 parent
0964dd7713
commit
89f70bb012
2 files changed
+36
-8
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user