package mq import ( "context" "encoding/binary" "encoding/json" "math" "strings" "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, // 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 names map[string]bool } func (r *OffsetRange) Add(entry *filer_pb.Entry) { if entry.IsDirectory { return } extended := entry.Extended base, isParquet := strings.CutSuffix(entry.Name, ".parquet") _, notDataErr := topic.ParseLogFileTime(base) 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 } } else if notDataErr == nil { if isParquet { var sources []string _ = json.Unmarshal(extended[ExtendedAttrSources], &sources) r.unrangedParquet = append(r.unrangedParquet, sources) } else { r.unrangedLog = 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 } } if b := extended[ExtendedAttrTimestampMax]; len(b) == 8 { r.newestTsNs = max(r.newestTsNs, int64(binary.BigEndian.Uint64(b))) } else if t, err := topic.ParseLogFileTime(entry.Name); err == nil { // a log file recording no times is named by its first message r.newestTsNs = max(r.newestTsNs, t.UnixNano()) } // 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 } if r.names == nil { r.names = make(map[string]bool) } r.names[entry.Name] = true } // Bounds is the earliest recorded offset and the offset after the latest, // both 0 when no file records a range. The earliest is 0 while a data file // records no range, unless it is a parquet file whose sources are all present. func (r *OffsetRange) Bounds() (earliestOffset, highWaterMark int64) { if r.hasEarliest && !r.hasUnknownOffsets() { 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 } // NewestTsNs is the latest message time a log or parquet file records, or the start // second of a log file recording none, zero when there is neither. func (r *OffsetRange) NewestTsNs() int64 { return r.newestTsNs } func (r *OffsetRange) hasUnknownOffsets() bool { if r.unrangedLog { return true } for _, sources := range r.unrangedParquet { if len(sources) == 0 { return true } for _, source := range sources { if !r.names[source] { return true } } } return false } // 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 }