Files
seaweedfs/weed/mq/offset_range.go
T
Chris LuandClaude Opus 5.5 b9cb0999e8 mq broker: never name a log file before the newest one in its partition
A log file is named by its window's start second, taken from the writing
broker's clock. A new leader whose clock is behind the previous one's wrote
windows whose names sorted before older files holding lower offsets, so names
no longer followed offsets. An offset read listing from an indexed file then
skipped the new windows, and a read starting below such a file passed over it
without notice; no reader-side check can see a file that sorts before where
it starts listing. The partition fence now carries the newest log file time:
the leader seeds it from the listing it already pages to continue the
partition's offsets, which now also reports the newest log file name, legacy
names included and compacted files, the fence and directories excluded. Each
flush names its window no earlier than that time and advances it. Files
sharing a second still sort by their min offset suffix. Timestamp reads
resuming past the old leader's names now list such windows too, though
entries timestamped before the resume point are still filtered out by
timestamp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 00:42:59 -08:00

67 lines
2.0 KiB
Go

package mq
import (
"context"
"encoding/binary"
"math"
"time"
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
)
// OffsetRange accumulates the offset ranges recorded on a partition's files,
// and the newest start second a log file is named by.
type OffsetRange struct {
earliest, latest int64
hasEarliest, hasLatest bool
newestLogFileTime time.Time
}
func (r *OffsetRange) Add(entry *filer_pb.Entry) {
if entry.IsDirectory {
return
}
if b, ok := entry.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 := entry.Extended[ExtendedAttrOffsetMax]; ok && len(b) == 8 {
if v := int64(binary.BigEndian.Uint64(b)); !r.hasLatest || v > r.latest {
r.latest, r.hasLatest = v, true
}
}
// 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
}
}
// 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
}
// NewestLogFileTime is the latest start second naming a log file, zero when there is none.
func (r *OffsetRange) NewestLogFileTime() time.Time {
return r.newestLogFileTime
}
// ReadOffsetRange scans every file of a partition directory, not just the
// filer's default listing page, for the offset ranges they record.
func ReadOffsetRange(ctx context.Context, client filer_pb.SeaweedFilerClient, partitionDir string) (r OffsetRange, err error) {
err = filer_pb.SeaweedList(ctx, client, partitionDir, "", func(entry *filer_pb.Entry, isLast bool) error {
r.Add(entry)
return nil
}, "", false, math.MaxInt32)
return r, err
}