Files
seaweedfs/weed/util/log_buffer/log_buffer.go
T
Chris LuandClaude Opus 5.5 746cae3895 mq: enforce topic retention (#13210)
* log_buffer: find a fetched offset in a disk chunk by its offset

A stateless read caches disk reads in chunks aligned to 1000 offsets and
picked the requested message by its distance from the aligned start. A
chunk read from disk begins at the first offset stored at or above that
start, so once the partition's oldest files are gone the chunk begins
later and the read returned messages from further on, under the
requested offsets. The message is now found by its offset, and the next
offset follows the last message returned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: start a loaded partition's log at its earliest stored offset

A loaded partition reported its earliest offset as 0 and its log start as
the start of its memory, so neither said where the stored data begins. The
log buffer now keeps the earliest offset still stored: a leader seeds it
from the offset ranges on its files when it loads, it only moves up, and
the partition's offset info and log start report it. A stateless read
below it fails with ErrOffsetOutOfRange instead of being answered from a
later file under the requested offsets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: purge a partition's files past its topic's retention

A topic's retention was stored but never applied, so a topic that is not
re-partitioned kept every flushed file. Every five minutes the broker now
purges each partition it leads whose topic has retention enabled. It
deletes the oldest run of files whose newest message is older than the
retention, stopping at the first file still retained so the stored
offsets stay contiguous, and always keeps the file with the highest
offset, which a later load continues offsets from. A log file is judged
by when it was flushed and a parquet file by its newest row; a parquet
file is deleted together with its source log files. The partition's log
start is raised before the deletes, and each delete is a transaction
conditioned on the partition's fence token, so a leader that lost its
fence deletes nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq/kafka: map an unknown broker error to UNKNOWN_SERVER_ERROR (-1)

The gateway's error table gave UNKNOWN_SERVER_ERROR the code 1, which
Kafka uses for OFFSET_OUT_OF_RANGE, and the broker's table carried the
same value for its unknown and publisher-not-found errors. Today the
mapped code only reaches error text, but a fetch path that answers
OFFSET_OUT_OF_RANGE by its Kafka code would make every unknown broker
failure reset a consumer. The code is now -1. The gateway only tests
these codes against 0 and against specific codes, and the cached
assignment is still dropped on the broker's own code 1, so nothing
treated 1 as success or depended on it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq: answer a fetch below retention with OFFSET_OUT_OF_RANGE

A read below the earliest retained offset failed like any other read, so
FetchMessage reported a storage error and the gateway answered
KAFKA_STORAGE_ERROR, which a consumer retries at the same offset forever
once retention has purged it. FetchMessage now answers
ErrOffsetOutOfRange with its own broker code, the earliest retained
offset as LogStartOffset, and the requested offset as NextOffset. The
gateway carries it as ErrOffsetOutOfRange and answers the partition with
OFFSET_OUT_OF_RANGE, so the consumer applies auto.offset.reset; a reset
to earliest asks ListOffsets, which reports the retained start through
the leader's range info.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* log_buffer: keep a disk chunk's entries in offset order

A disk read filled its 1000-offset chunk with the first 1000 entries
in the order the files listed, and the lookup searches the chunk by
offset. Files whose names sort out of offset order deliver entries out
of order, so the chunk held the wrong offsets and was cached that way.
It now keeps only the chunk's offsets, wherever they come, and sorts
them before caching.

* mq broker: purge a partition's files in offset order

The purge walked a partition's files in name order and stopped at the
first one still retained. Names sort by the start of a window, which can
be earlier than older files' after a reload, so a file holding higher
offsets could be purged ahead of a retained one below it, leaving a hole.
The walk now goes by each file's first offset, taking a parquet file that
records none from its first source, and by name among equals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: round a log file's flush time up to bound its messages

A log file without a recorded newest timestamp is judged by its Mtime,
which the filer keeps in whole seconds, so a file flushed later in the
cutoff's second looked older than messages it may hold. The purge now
takes the end of that second as the bound. A recorded newest timestamp
is still used as is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* log_buffer: keep one entry per offset in a disk chunk

A disk read goes through a partition's log files and then its parquet
files, and a parquet file holds the same offsets as the log files it was
compacted from, so a chunk could take an offset twice and a fetch
returned the duplicate as the next message. The chunk now keeps the
first entry for each offset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* log_buffer: reread a cached disk chunk that lacks the fetched offset

A disk chunk can be cached while its later offsets are still in memory.
Once those are flushed and memory moves on, a fetch past the cached part
hit the cache, found nothing, and returned no messages until the chunk
was evicted. A cached chunk that does not hold the fetched offset is now
dropped and read from disk again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq/logstore: record a parquet file's offset range from its sources

The compactor took a parquet file's offset range from its entries'
offsets above 0 and recorded it only when the lowest was above 0, so a
file compacted from a partition's first window recorded no range, and one
whose entries started at 0 would have claimed to start at 1. The range
now comes from the source log files' recorded ranges, including one that
starts at offset 0, and is left out unless every source records one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq: keep the earliest offset at or below files that record no range

The earliest stored offset was the lowest offset any file records, so a
file that records none, such as a parquet file compacted from a
partition's first window or a log file flushed without a range, was left
out. A reload or a purge could then start the log above offsets a read
can still return, and ListOffsets for the earliest offset left them out.
OffsetRange now takes the whole entry and keeps the earliest offset at 0
while a log or parquet file records no range, except a parquet file whose
listed sources are all present, as their ranges cover it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: keep a partition whose files cannot all be placed by offset

The purge ordered a data file that records no offsets, and whose
sources give none, as if it held the lowest, so an expired one could
be deleted while a retained file below its offsets stayed, leaving a
hole. A partition holding such a file is now left alone; only files
written before offsets were recorded lack them.

* mq broker: purge a parquet file only when each of its sources has expired

The purge deleted every source of an expired parquet file along with it,
judging the whole group by the parquet file's newest timestamp. Message
timestamps are not ordered across reloads, so a parquet file can hold an
expired source next to one still retained, and the retained one went
early. A parquet group is now purged only when the parquet file and each
source in it are past the cutoff on their own; otherwise the walk stops
there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq/logstore: record a parquet file's lowest and highest timestamps

The compactor recorded a parquet file's timestamp range from its first
and last rows. Message timestamps are not ordered across reloads, so the
last row need not be the newest, and the recorded ts_max could sit below
messages the file holds, making it look older to the retention purge and
to readers that skip files by time. It now records the lowest and
highest timestamps over all rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq/logstore: keep each compacted row's offset in the _offset column

The compaction schema had no _offset column, so a schematized row's
offset was dropped and a schemaless row never carried one, while the
parquet reader expects the column and a fetch places rows by offset. The
compactor now adds the column when the schema lacks it and fills it for
every row, schemaless ones included, when the source files record their
offsets; otherwise it is left null.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq/logstore: read a parquet file by its own columns and mark unknown offsets

The parquet reader always expected an _offset column and read rows by
column position, so a file compacted before the column existed was read
out of alignment and panicked with an index out of range. A row with no
real offset also read back as offset 0, so a disk chunk, which keeps one
entry per offset, collapsed such rows into one. The reader now uses the
_offset column only when the file has it, trusts it only when the file
records its offset range, and reports any other row with offset -1,
which keeps it out of reads by offset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: keep a purge group that reaches past the first retained offset

A parquet file is purged with its sources as one group, and the sources
need not be contiguous: an expired parquet file could list expired files
on both sides of a retained one, so purging the group left a hole above
the retained file. A group now goes only when every offset it and its
sources cover lies below the first offset left behind; the walk keeps the
first group that does not, and the ones after it. A log file whose
offsets a retained parquet file also starts at now stays until that
parquet file goes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq/logstore: give compaction test fixtures file ids no other fixture uses

The parquet reader caches chunks in a package-wide cache keyed by file
id, and each compaction fixture numbered its file ids from 1, so one
fixture could read another's cached bytes; with -count=3 a test read a
parquet file from an earlier run and failed. File ids are now numbered
across all fixtures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq/logstore: compact legacy log files apart from ones that record offsets

A compaction group could hold a legacy log file without an offset range
next to files that record one, and a parquet file keeps its rows'
offsets only when every source records them, so the ranged rows lost
their offsets and could no longer be fetched by offset. Groups now split
where a file's having an offset range differs from the group's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: treat a parquet file's end as unknown when a source is gone

A parquet file that records no offset range took its last offset from
the sources still present, so one whose later source was already gone
looked as if it ended below a retained file and was purged, though it
may hold that source's offsets above the retained one. A gone or
unranged source now leaves the end unknown, and the group stays.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 10:18:45 -08:00

1517 lines
55 KiB
Go

package log_buffer
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"sync"
"sync/atomic"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/seaweedfs/seaweedfs/weed/util/mem"
)
const BufferSize = 8 * 1024 * 1024
const PreviousBufferCount = 4
// EvictionGatedOffset is a sentinel cursor offset (-2..-6 are taken by other
// sentinels) that reads like the plain -2 sentinel except below the eviction
// watermark: there ReadFromBuffer refuses with ResumeFromDiskError instead of
// silently serving from the earliest retained window. The check runs under the
// read lock, atomically with the serve decision, which callers cannot do from
// outside - an eviction can land between any caller-side check and the read.
const EvictionGatedOffset = -7
// flushQueueDepth bounds queued flush copies (BufferSize each); a full queue
// blocks producers, so a stalled flush backpressures writers instead of
// pinning hundreds of buffer copies.
const flushQueueDepth = 16
// flushQueueBudget bounds the same queue in bytes. Counting copies only holds
// if every copy is a window's worth: an entry larger than BufferSize grows its
// window to fit, and a queue of those multiplies straight through — sixteen
// 100 MB windows is 1.6 GB of flush copies alone. The ceiling is the one the
// depth was chosen for, so ordinary windows still queue sixteen deep.
//
// This covers the flush queue only. A sealed window stays reachable through
// prevBuffers for PreviousBufferCount more seals, and readers may take a
// snapshot of it, so an oversized entry still costs several times its size
// before it falls out of the ring.
const flushQueueBudget = flushQueueDepth * BufferSize
// Errors that can be returned by log buffer operations
var (
// ErrBufferCorrupted indicates the log buffer contains corrupted data
ErrBufferCorrupted = fmt.Errorf("log buffer is corrupted")
// ErrBufferStopped indicates that shutdown has closed write admission.
ErrBufferStopped = fmt.Errorf("log buffer is stopping")
// ErrOffsetOutOfRange is a read below the earliest offset still stored.
ErrOffsetOutOfRange = fmt.Errorf("offset out of range")
)
type dataToFlush struct {
startTime time.Time
stopTime time.Time
data []byte // slab from mem.Allocate; returned via mem.Free after flush
minOffset int64
maxOffset int64
seq uint64 // seal order, so the budget admits windows in order
budget int // bytes reserved from flushBudget, released after the flush
done chan struct{} // Signal when flush completes
}
// flushBudget accounts the bytes of sealed windows waiting to be written, so a
// producer waits for the queue to drain instead of adding another copy to it.
// Windows are admitted strictly in seal order: a producer parked here can wait
// seconds, and letting a later window overtake an earlier one would hand
// loopFlush the windows out of order.
type flushBudget struct {
mu sync.Mutex
cond *sync.Cond
limit int
queued int
nextSeq uint64
closed bool
}
func newFlushBudget(limit int) *flushBudget {
b := &flushBudget{limit: limit}
b.cond = sync.NewCond(&b.mu)
return b
}
// reserve blocks until it is this window's turn and its bytes fit under the
// limit, then returns the amount to hand back to release. A window larger than
// the whole budget is admitted on its own once the queue empties, so an
// oversized entry still gets through.
func (b *flushBudget) reserve(seq uint64, n int) int {
if n > b.limit {
n = b.limit
}
b.mu.Lock()
defer b.mu.Unlock()
for !b.closed && (seq != b.nextSeq || (b.queued > 0 && b.queued+n > b.limit)) {
b.cond.Wait()
}
if seq == b.nextSeq {
b.nextSeq++
}
b.queued += n
b.cond.Broadcast() // wake whoever is next in line
return n
}
// waitForRoom parks until the queue has headroom for a window of n bytes,
// without charging anything. A window is copied into its slab while the write
// lock is held, before queueFlush gets to reserve, so a burst of concurrent
// oversized writers would each be holding a full copy in hand by the time they
// queue up -- memory the budget never sees. Large writers wait here first so
// they arrive at the seal a few at a time. This throttles the burst rather than
// bounding it: a writer that passes the check still seals unconditionally.
func (b *flushBudget) waitForRoom(n int) {
if n > b.limit {
n = b.limit
}
b.mu.Lock()
defer b.mu.Unlock()
for !b.closed && b.queued > 0 && b.queued+n > b.limit {
b.cond.Wait()
}
}
func (b *flushBudget) release(n int) {
if n == 0 {
return
}
b.mu.Lock()
b.queued -= n
b.mu.Unlock()
b.cond.Broadcast()
}
// close stops the budget from parking anyone, so shutdown is never held up by
// a producer waiting on a flush that will not run.
func (b *flushBudget) close() {
b.mu.Lock()
b.closed = true
b.mu.Unlock()
b.cond.Broadcast()
}
type EachLogEntryFuncType func(logEntry *filer_pb.LogEntry) (isDone bool, err error)
type EachLogEntryWithOffsetFuncType func(logEntry *filer_pb.LogEntry, offset int64) (isDone bool, err error)
type LogFlushFuncType func(logBuffer *LogBuffer, startTime, stopTime time.Time, buf []byte, minOffset, maxOffset int64)
type LogReadFromDiskFuncType func(startPosition MessagePosition, stopTsNs int64, eachLogEntryFn EachLogEntryFuncType) (lastReadPosition MessagePosition, isDone bool, err error)
// DiskChunkCache caches chunks of historical data read from disk
type DiskChunkCache struct {
mu sync.RWMutex
chunks map[int64]*CachedDiskChunk // Key: chunk start offset (aligned to chunkSize)
maxChunks int // Maximum number of chunks to cache
}
// CachedDiskChunk represents a cached chunk of disk data
type CachedDiskChunk struct {
startOffset int64
endOffset int64
messages []*filer_pb.LogEntry
lastAccess time.Time
}
type LogBuffer struct {
// 8-byte aligned fields
LastTsNs atomic.Int64
lastFlushTsNs atomic.Int64
lastFlushedOffset atomic.Int64 // Highest offset that has been flushed to disk (-1 = nothing flushed yet)
lastEvictedTsNs atomic.Int64 // Latest stopTime evicted from the sealed ring (0 = nothing evicted yet)
// lastEvictedTsNs in pre-bump timestamps: gap proofs compare disk cursors,
// which never see the bumped values out-of-order arrivals get.
lastEvictedOriginalTsNs atomic.Int64
curWindowMaxOriginalTsNs int64 // max pre-bump ts in the open window, under the write lock
offset int64
bufferStartOffset int64
logStartOffset int64 // earliest offset still stored, raised as old files are purged
minOffset int64
maxOffset int64
flushInterval time.Duration
startTime time.Time
stopTime time.Time
// Other fields
name string
prevBuffers *SealedBuffers
buf []byte
idx []int
pos int
sizeBuf []byte
flushFn LogFlushFuncType
ReadFromDiskFn LogReadFromDiskFuncType
notifyFn func()
// Per-subscriber notification channels for instant wake-up
subscribersMu sync.RWMutex
subscribers map[string]*subscription // subscriberID -> shared notification channel
// Notified only when a flush lands, for readers that cannot act on an append
flushSubscribers map[string]*subscription
// wakeFilters skip a subscriber's append wake-up when the entry's partition
// key cannot interest it; it still reads the entry on its next poll.
wakeFilters map[string]func(key []byte) bool
isStopping *atomic.Bool
shutdownCh chan struct{} // closed by ShutdownLogBuffer to wake blocked subscribers
// flushDroppedBytes counts bytes the flushFn gave up on during shutdown.
// loopFlush reads it after each window so an abandoned flush does not
// advance the flushed watermark or wake subscribers as if it had landed.
flushDroppedBytes atomic.Int64
loopsDone sync.WaitGroup // loopFlush and loopInterval signal exit
isAllFlushed bool
flushChan chan *dataToFlush
flushBudget *flushBudget
flushSeq uint64 // seal counter, assigned under the write lock
writeMu sync.Mutex // serializes sealing and enqueueing, including shutdown
// Offset range tracking for Kafka integration
hasOffsets bool
// Disk chunk cache for historical data reads
diskChunkCache *DiskChunkCache
// curSnap is a GC-owned copy of the current window's append-only prefix
// buf[:len(curSnap)], shared by all readers so each byte is copied once per
// window instead of once per reader. Extended lazily under curSnapMu; reset
// at seal. Existing holders keep their prefix slices, which never mutate.
curSnapMu sync.Mutex
curSnap []byte
sync.RWMutex
}
func NewLogBuffer(name string, flushInterval time.Duration, flushFn LogFlushFuncType,
readFromDiskFn LogReadFromDiskFuncType, notifyFn func()) *LogBuffer {
lb := &LogBuffer{
name: name,
prevBuffers: newSealedBuffers(PreviousBufferCount),
buf: make([]byte, BufferSize),
sizeBuf: make([]byte, 4),
flushInterval: flushInterval,
flushFn: flushFn,
ReadFromDiskFn: readFromDiskFn,
notifyFn: notifyFn,
subscribers: make(map[string]*subscription),
flushSubscribers: make(map[string]*subscription),
wakeFilters: make(map[string]func(key []byte) bool),
flushChan: make(chan *dataToFlush, flushQueueDepth),
isStopping: new(atomic.Bool),
shutdownCh: make(chan struct{}),
offset: 0, // Will be initialized from existing data if available
flushBudget: newFlushBudget(flushQueueBudget),
diskChunkCache: &DiskChunkCache{
chunks: make(map[int64]*CachedDiskChunk),
maxChunks: 16, // Cache up to 16 chunks (configurable)
},
}
lb.lastFlushedOffset.Store(-1) // Nothing flushed to disk yet
lb.loopsDone.Add(2)
go lb.loopFlush()
go lb.loopInterval()
return lb
}
// subscription is one notification channel and the number of readers holding
// it. Registrations that share a subscriberID share the channel - two streams
// of the same client, or an old one that has not noticed its replacement yet -
// so it may only be closed once the last of them unregisters. Closing it under
// a reader parked in awaitNotificationOrTimeoutFor makes every receive there
// succeed instantly, spinning that reader on a full core for the rest of its
// life.
type subscription struct {
notifyChan chan struct{}
refCount int
wants func(key []byte) bool
}
func registerSubscription(subscriptions map[string]*subscription, subscriberID string) chan struct{} {
if existing, exists := subscriptions[subscriberID]; exists {
existing.refCount++
return existing.notifyChan
}
// Create buffered channel (size 1) so notifications never block
sub := &subscription{notifyChan: make(chan struct{}, 1), refCount: 1}
subscriptions[subscriberID] = sub
return sub.notifyChan
}
func unregisterSubscription(subscriptions map[string]*subscription, subscriberID string) {
sub, exists := subscriptions[subscriberID]
if !exists {
return
}
sub.refCount--
if sub.refCount > 0 {
return
}
close(sub.notifyChan)
delete(subscriptions, subscriberID)
}
// RegisterSubscriber registers a subscriber for instant notifications when data is written
// Returns a channel that will receive notifications (<1ms latency)
func (logBuffer *LogBuffer) RegisterSubscriber(subscriberID string) chan struct{} {
logBuffer.subscribersMu.Lock()
defer logBuffer.subscribersMu.Unlock()
notifyChan := registerSubscription(logBuffer.subscribers, subscriberID)
logBuffer.subscribers[subscriberID].wants = logBuffer.wakeFilters[subscriberID]
return notifyChan
}
// UnregisterSubscriber removes a subscriber and closes its notification channel
// once no other registration of the same subscriberID is left holding it.
func (logBuffer *LogBuffer) UnregisterSubscriber(subscriberID string) {
logBuffer.subscribersMu.Lock()
defer logBuffer.subscribersMu.Unlock()
unregisterSubscription(logBuffer.subscribers, subscriberID)
}
// RegisterFlushSubscriber registers a subscriber woken only when a flush lands.
// A reader waiting for data it can only get from disk has nothing to do with an
// append, and taking those wake-ups off the shared channel would cost it one
// scheduling round-trip per write - and keep that channel drained, so every
// writer's non-blocking send succeeds instead of falling through.
func (logBuffer *LogBuffer) RegisterFlushSubscriber(subscriberID string) chan struct{} {
logBuffer.subscribersMu.Lock()
defer logBuffer.subscribersMu.Unlock()
return registerSubscription(logBuffer.flushSubscribers, subscriberID)
}
// UnregisterFlushSubscriber removes a flush subscriber and closes its channel
// once no other registration of the same subscriberID is left holding it.
func (logBuffer *LogBuffer) UnregisterFlushSubscriber(subscriberID string) {
logBuffer.subscribersMu.Lock()
defer logBuffer.subscribersMu.Unlock()
unregisterSubscription(logBuffer.flushSubscribers, subscriberID)
}
// IsOffsetInMemory checks if the given offset is available in the in-memory buffer
// Returns true if:
// 1. Offset is newer than what's been flushed to disk (must be in memory)
// 2. Offset is in current buffer or previous buffers (may be flushed but still in memory)
// Returns false if offset is older than memory buffers (only on disk)
func (logBuffer *LogBuffer) IsOffsetInMemory(offset int64) bool {
logBuffer.RLock()
defer logBuffer.RUnlock()
// Check if we're tracking offsets at all
if !logBuffer.hasOffsets {
return false // No offsets tracked yet
}
// OPTIMIZATION: If offset is newer than what's been flushed to disk,
// it MUST be in memory (not written to disk yet)
lastFlushed := logBuffer.lastFlushedOffset.Load()
if lastFlushed >= 0 && offset > lastFlushed {
return true
}
// Check if offset is in current buffer range AND buffer has data
// (data can be both on disk AND in memory during flush window)
if offset >= logBuffer.bufferStartOffset && offset <= logBuffer.offset {
// CRITICAL: Check if buffer actually has data (pos > 0)
// After flush, pos=0 but range is still valid - data is on disk, not in memory
if logBuffer.pos > 0 {
return true
}
// Buffer is empty (just flushed) - data is on disk
return false
}
// Check if offset is in previous buffers AND they have data
for _, buf := range logBuffer.prevBuffers.buffers {
if offset >= buf.startOffset && offset <= buf.offset {
// Check if prevBuffer actually has data
if buf.size > 0 {
return true
}
// Buffer is empty (flushed) - data is on disk
return false
}
}
// Offset is older than memory buffers - only available on disk
return false
}
// SetSubscriberWakeFilter limits append wake-ups of the subscriber registered
// as subscriberID to entries whose partition key satisfies wants; a nil wants
// removes the filter. Flushes and keyless appends still wake every subscriber.
func (logBuffer *LogBuffer) SetSubscriberWakeFilter(subscriberID string, wants func(key []byte) bool) {
logBuffer.subscribersMu.Lock()
defer logBuffer.subscribersMu.Unlock()
if wants == nil {
delete(logBuffer.wakeFilters, subscriberID)
} else {
logBuffer.wakeFilters[subscriberID] = wants
}
if sub, ok := logBuffer.subscribers[subscriberID]; ok {
sub.wants = wants
}
}
// NotifySubscribers wakes the subscribers interested in key, for entries that
// also concern a key other than the partition key they were appended under.
func (logBuffer *LogBuffer) NotifySubscribers(key []byte) {
logBuffer.notifySubscribers(key)
}
// notifySubscribers wakes the subscribers interested in key, or all of them
// for an empty key. Non-blocking: a full channel already holds a wake-up.
func (logBuffer *LogBuffer) notifySubscribers(key []byte) {
logBuffer.subscribersMu.RLock()
defer logBuffer.subscribersMu.RUnlock()
for _, sub := range logBuffer.subscribers {
if len(key) > 0 && sub.wants != nil && !sub.wants(key) {
continue
}
select {
case sub.notifyChan <- struct{}{}:
default:
}
}
}
// notifyFlushSubscribers wakes the readers that only care about a flush landing
func (logBuffer *LogBuffer) notifyFlushSubscribers() {
logBuffer.subscribersMu.RLock()
defer logBuffer.subscribersMu.RUnlock()
for _, sub := range logBuffer.flushSubscribers {
select {
case sub.notifyChan <- struct{}{}:
default:
}
}
}
// InitializeOffsetFromExistingData initializes the offset counter from existing data on disk
// This should be called after LogBuffer creation to ensure offset continuity on restart
func (logBuffer *LogBuffer) InitializeOffsetFromExistingData(getHighestOffsetFn func() (int64, error)) error {
if getHighestOffsetFn == nil {
return nil // No initialization function provided
}
highestOffset, err := getHighestOffsetFn()
if err != nil {
return fmt.Errorf("read the highest stored offset: %w", err)
}
if highestOffset >= 0 {
// Set the next offset to be one after the highest existing offset
nextOffset := highestOffset + 1
logBuffer.offset = nextOffset
// bufferStartOffset should match offset after initialization
// This ensures that reads for old offsets (0...highestOffset) will trigger disk reads
// New data written after this will start at nextOffset
logBuffer.bufferStartOffset = nextOffset
// CRITICAL: Track that data [0...highestOffset] is on disk
logBuffer.lastFlushedOffset.Store(highestOffset)
// Set lastFlushTsNs to current time (we know data up to highestOffset is on disk)
logBuffer.lastFlushTsNs.Store(time.Now().UnixNano())
} else {
logBuffer.bufferStartOffset = 0 // Start from offset 0
// No data on disk yet
}
return nil
}
func (logBuffer *LogBuffer) AddToBuffer(message *mq_pb.DataMessage) error {
return logBuffer.AddDataToBuffer(message.Key, message.Value, message.TsNs)
}
// AddLogEntryToBuffer appends an entry. An entry with a negative Offset is given the
// next offset; an assigned one is kept and moves the next offset past it.
func (logBuffer *LogBuffer) AddLogEntryToBuffer(logEntry *filer_pb.LogEntry) error {
if len(logEntry.Data) > BufferSize {
logBuffer.flushBudget.waitForRoom(len(logEntry.Data))
}
if !logBuffer.beginWrite() {
return ErrBufferStopped
}
var toFlush *dataToFlush
var marshalErr error
logBuffer.Lock()
defer func() {
logBuffer.Unlock()
if toFlush != nil {
logBuffer.queueFlush(toFlush)
}
logBuffer.writeMu.Unlock()
// Only notify if there was no error
if marshalErr == nil {
if logBuffer.notifyFn != nil {
logBuffer.notifyFn()
}
// Notify all registered subscribers instantly (<1ms latency)
logBuffer.notifySubscribers(logEntry.Key)
}
}()
processingTsNs := logEntry.TsNs
ts := time.Unix(0, processingTsNs)
originalTsNs := processingTsNs
// Handle timestamp collision inside lock (rare case)
if logBuffer.LastTsNs.Load() >= processingTsNs {
processingTsNs = logBuffer.LastTsNs.Add(1)
ts = time.Unix(0, processingTsNs)
// Re-marshal with corrected timestamp
logEntry.TsNs = processingTsNs
} else {
logBuffer.LastTsNs.Store(processingTsNs)
}
// A negative offset asks the buffer to assign the next one, in append order.
if logEntry.Offset < 0 {
logEntry.Offset = logBuffer.offset
}
// Size is computed without allocating; the entry is marshaled straight into
// the buffer below via MarshalToSizedBufferVT.
size := logEntry.SizeVT()
if logBuffer.pos == 0 {
logBuffer.startTime = ts
// Reset offset tracking for new buffer
logBuffer.hasOffsets = false
}
if logBuffer.startTime.Add(logBuffer.flushInterval).Before(ts) || len(logBuffer.buf)-logBuffer.pos < size+4 {
toFlush = logBuffer.copyToFlush()
logBuffer.startTime = ts
if len(logBuffer.buf) < size+4 {
// Validate size to prevent integer overflow in computation BEFORE allocation
const maxBufferSize = 1 << 30 // 1 GiB practical limit
// The window is sized size+4, so that is what has to stay in bounds
if size < 0 || size > math.MaxInt-4 || size > maxBufferSize-4 {
marshalErr = fmt.Errorf("message size %d exceeds maximum allowed size", size)
glog.Errorf("%v", marshalErr)
return marshalErr
}
// Fit the entry exactly. Doubling left room for a second oversized
// record in the same window, which only doubles the flush copy and
// the snapshot taken of it.
logBuffer.buf = make([]byte, size+4)
}
}
logBuffer.stopTime = ts
// Marshal directly into the buffer, avoiding an intermediate slice and copy.
// On the (practically impossible) error the entry is dropped before idx/pos
// are advanced, leaving the buffer consistent.
if _, err := logEntry.MarshalToSizedBufferVT(logBuffer.buf[logBuffer.pos+4 : logBuffer.pos+4+size]); err != nil {
marshalErr = fmt.Errorf("failed to marshal LogEntry: %w", err)
glog.Errorf("%v", marshalErr)
return marshalErr
}
logBuffer.idx = append(logBuffer.idx, logBuffer.pos)
util.Uint32toBytes(logBuffer.sizeBuf, uint32(size))
copy(logBuffer.buf[logBuffer.pos:logBuffer.pos+4], logBuffer.sizeBuf)
logBuffer.pos += size + 4
// Only now is the entry's window known: a rollover above seals the previous
// window first, and crediting this timestamp before that hands it to the
// sealed window and loses it from the new one - corrupting the received-ts
// eviction watermark in both directions.
if originalTsNs > logBuffer.curWindowMaxOriginalTsNs {
logBuffer.curWindowMaxOriginalTsNs = originalTsNs
}
logBuffer.trackWindowOffset(logEntry.Offset)
if logEntry.Offset >= logBuffer.offset {
logBuffer.offset = logEntry.Offset + 1
}
return nil
}
// trackWindowOffset widens the current window's offset range. It runs after a
// rollover has sealed the previous window, so each window claims only its own entries.
func (logBuffer *LogBuffer) trackWindowOffset(offset int64) {
if offset < 0 {
return
}
if !logBuffer.hasOffsets {
logBuffer.minOffset, logBuffer.maxOffset, logBuffer.hasOffsets = offset, offset, true
return
}
logBuffer.minOffset = min(logBuffer.minOffset, offset)
logBuffer.maxOffset = max(logBuffer.maxOffset, offset)
}
func (logBuffer *LogBuffer) AddDataToBuffer(partitionKey, data []byte, processingTsNs int64) error {
_, err := logBuffer.AddDataToBufferAndGetTs(partitionKey, data, processingTsNs)
return err
}
// AddDataToBufferAndGetTs appends like AddDataToBuffer and returns the
// position the entry was assigned, bumped past processingTsNs when the
// buffer must stay monotone.
func (logBuffer *LogBuffer) AddDataToBufferAndGetTs(partitionKey, data []byte, processingTsNs int64) (int64, error) {
// An entry this large gets a window to itself, so it will seal and copy one;
// wait for the queue to have room before joining the queue for the lock.
if len(data) > BufferSize {
logBuffer.flushBudget.waitForRoom(len(data))
}
// PERFORMANCE OPTIMIZATION: Pre-process expensive operations OUTSIDE the lock
var ts time.Time
if processingTsNs == 0 {
ts = time.Now()
processingTsNs = ts.UnixNano()
} else {
ts = time.Unix(0, processingTsNs)
}
logEntry := &filer_pb.LogEntry{
TsNs: processingTsNs, // Will be updated if needed
PartitionKeyHash: util.HashToInt32(partitionKey),
Data: data,
Key: partitionKey,
}
if !logBuffer.beginWrite() {
return 0, ErrBufferStopped
}
var toFlush *dataToFlush
var marshalErr error
logBuffer.Lock()
defer func() {
logBuffer.Unlock()
if toFlush != nil {
logBuffer.queueFlush(toFlush)
}
logBuffer.writeMu.Unlock()
// Only notify if there was no error
if marshalErr == nil {
if logBuffer.notifyFn != nil {
logBuffer.notifyFn()
}
// Notify all registered subscribers instantly (<1ms latency)
logBuffer.notifySubscribers(partitionKey)
}
}()
originalTsNs := processingTsNs
// Handle timestamp collision inside lock (rare case)
if logBuffer.LastTsNs.Load() >= processingTsNs {
processingTsNs = logBuffer.LastTsNs.Add(1)
ts = time.Unix(0, processingTsNs)
logEntry.TsNs = processingTsNs
} else {
logBuffer.LastTsNs.Store(processingTsNs)
}
// Set the offset in the LogEntry before marshaling
// This ensures the flushed data contains the correct offset information
// Note: This also enables AddToBuffer to work correctly with Kafka-style offset-based reads
logEntry.Offset = logBuffer.offset
// Size is computed without allocating; the entry is marshaled straight into
// the buffer below via MarshalToSizedBufferVT.
size := logEntry.SizeVT()
if logBuffer.pos == 0 {
logBuffer.startTime = ts
// Reset offset tracking for new buffer
logBuffer.hasOffsets = false
}
if logBuffer.startTime.Add(logBuffer.flushInterval).Before(ts) || len(logBuffer.buf)-logBuffer.pos < size+4 {
toFlush = logBuffer.copyToFlush()
logBuffer.startTime = ts
if len(logBuffer.buf) < size+4 {
// Validate size to prevent integer overflow in computation BEFORE allocation
const maxBufferSize = 1 << 30 // 1 GiB practical limit
// The window is sized size+4, so that is what has to stay in bounds
if size < 0 || size > math.MaxInt-4 || size > maxBufferSize-4 {
marshalErr = fmt.Errorf("message size %d exceeds maximum allowed size", size)
glog.Errorf("%v", marshalErr)
return 0, marshalErr
}
// Fit the entry exactly. Doubling left room for a second oversized
// record in the same window, which only doubles the flush copy and
// the snapshot taken of it.
logBuffer.buf = make([]byte, size+4)
}
}
logBuffer.stopTime = ts
// Marshal directly into the buffer, avoiding an intermediate slice and copy.
// On the (practically impossible) error the entry is dropped before idx/pos
// are advanced, leaving the buffer consistent.
if _, err := logEntry.MarshalToSizedBufferVT(logBuffer.buf[logBuffer.pos+4 : logBuffer.pos+4+size]); err != nil {
marshalErr = fmt.Errorf("failed to marshal LogEntry: %w", err)
glog.Errorf("%v", marshalErr)
return 0, marshalErr
}
logBuffer.idx = append(logBuffer.idx, logBuffer.pos)
util.Uint32toBytes(logBuffer.sizeBuf, uint32(size))
copy(logBuffer.buf[logBuffer.pos:logBuffer.pos+4], logBuffer.sizeBuf)
logBuffer.pos += size + 4
// Only now is the entry's window known: a rollover above seals the previous
// window first, and crediting this timestamp before that hands it to the
// sealed window and loses it from the new one - corrupting the received-ts
// eviction watermark in both directions.
if originalTsNs > logBuffer.curWindowMaxOriginalTsNs {
logBuffer.curWindowMaxOriginalTsNs = originalTsNs
}
logBuffer.trackWindowOffset(logEntry.Offset)
logBuffer.offset++
return logEntry.TsNs, nil
}
func (logBuffer *LogBuffer) IsStopping() bool {
return logBuffer.isStopping.Load()
}
// ForceFlush queues the current buffer content, then waits up to 5 seconds for completion
// This is useful for critical topics that need immediate persistence
// Queueing itself has no timeout.
func (logBuffer *LogBuffer) ForceFlush() {
if !logBuffer.beginWrite() {
return
}
logBuffer.Lock()
toFlush := logBuffer.copyToFlushWithCallback()
logBuffer.Unlock()
if toFlush != nil {
logBuffer.queueFlush(toFlush)
}
logBuffer.writeMu.Unlock()
if toFlush != nil {
select {
case <-toFlush.done:
// Flush completed
case <-time.After(5 * time.Second):
// Queued but not yet flushed; loopFlush will still persist it
}
}
}
// beginWrite serializes admission and batch handoff with shutdown.
// Callers hold writeMu until any sealed batch has been queued.
func (logBuffer *LogBuffer) beginWrite() bool {
logBuffer.writeMu.Lock()
if logBuffer.isStopping.Load() {
logBuffer.writeMu.Unlock()
return false
}
return true
}
// ShutdownLogBuffer flushes the buffer and stops the log buffer
func (logBuffer *LogBuffer) ShutdownLogBuffer() {
isAlreadyStopped := logBuffer.isStopping.Swap(true)
if isAlreadyStopped {
return
}
// Wake any subscribers blocked in awaitNotificationOrTimeout so they can
// notice IsStopping() and exit promptly, even on an idle buffer where no
// flush notification would otherwise fire.
close(logBuffer.shutdownCh)
// Wake oversized writers waiting for room; they will observe shutdown
// before appending. Already admitted writes must finish their handoff.
logBuffer.flushBudget.close()
logBuffer.writeMu.Lock()
defer logBuffer.writeMu.Unlock()
logBuffer.Lock()
toFlush := logBuffer.copyToFlush()
logBuffer.Unlock()
if toFlush != nil {
logBuffer.queueFlush(toFlush)
}
// Every accepted batch is now queued, and no producer can append after
// this sentinel. loopFlush drains the queue before exiting.
logBuffer.flushChan <- nil
}
// WaitForShutdown waits for pending flushes and background loops to finish.
// Call ShutdownLogBuffer first, after stopping producers.
func (logBuffer *LogBuffer) WaitForShutdown() {
logBuffer.loopsDone.Wait()
}
// NoteFlushDropped records that the flush function abandoned n bytes instead
// of writing them (a shutdown-bounded retry giving up). loopFlush then skips
// the watermark advance and subscriber notifications for that window, so no
// reader is told the bytes were flushed when they were not.
func (logBuffer *LogBuffer) NoteFlushDropped(n int) {
logBuffer.flushDroppedBytes.Add(int64(n))
}
// GetLastFlushedOffset returns the highest offset a landed flush stored, or -1.
func (logBuffer *LogBuffer) GetLastFlushedOffset() int64 {
return logBuffer.lastFlushedOffset.Load()
}
// IsAllFlushed returns true if all data in the buffer has been flushed, after calling ShutdownLogBuffer().
func (logBuffer *LogBuffer) IsAllFlushed() bool {
return logBuffer.isAllFlushed
}
// queueFlush hands a sealed window to loopFlush. The caller holds writeMu
// from before sealing until this handoff completes, so shutdown cannot pass it.
func (logBuffer *LogBuffer) queueFlush(d *dataToFlush) {
// Charge the pooled slab, whose capacity may exceed the window length.
d.budget = logBuffer.flushBudget.reserve(d.seq, cap(d.data))
logBuffer.flushChan <- d
}
func (logBuffer *LogBuffer) loopFlush() {
defer logBuffer.loopsDone.Done()
for d := range logBuffer.flushChan {
if d == nil {
break // shutdown sentinel
}
logBuffer.flushFn(logBuffer, d.startTime, d.stopTime, d.data, d.minOffset, d.maxOffset)
dropped := logBuffer.flushDroppedBytes.Swap(0)
d.releaseMemory()
logBuffer.flushBudget.release(d.budget)
if dropped > 0 {
glog.Warningf("log buffer %s: shutdown flush dropped %d bytes", logBuffer.name, dropped)
if d.done != nil {
close(d.done)
}
continue
}
// local logbuffer is different from aggregate logbuffer here
if d.maxOffset >= 0 {
logBuffer.lastFlushedOffset.Store(d.maxOffset)
}
if !d.stopTime.IsZero() {
logBuffer.lastFlushTsNs.Store(d.stopTime.UnixNano())
}
// Wake readers that may be waiting to retry disk reads after the flush lands.
// LOAD-BEARING ORDER: the watermark store above must precede these
// notifications. A parked filer subscriber re-checks GetLastFlushTsNs on
// wake-up and goes back to sleep if it has not moved; notifying first
// opens a window where the wake-up looks spurious and the flush that
// caused it is only picked up by the retry timer. Not testable from
// outside (the window is nanoseconds on this goroutine) - keep the order.
if logBuffer.notifyFn != nil {
logBuffer.notifyFn()
}
logBuffer.notifySubscribers(nil)
logBuffer.notifyFlushSubscribers()
// Signal completion if there's a callback channel
if d.done != nil {
close(d.done)
}
}
logBuffer.isAllFlushed = true
}
func (logBuffer *LogBuffer) loopInterval() {
defer logBuffer.loopsDone.Done()
// Wake on shutdown instead of sleeping through the interval: a goroutine
// parked in time.Sleep keeps the buffer and its ~40MB of slabs reachable
// for up to flushInterval after ShutdownLogBuffer.
ticker := time.NewTicker(logBuffer.flushInterval)
defer ticker.Stop()
for {
select {
case <-logBuffer.shutdownCh:
return
case <-ticker.C:
}
if !logBuffer.beginWrite() {
return
}
logBuffer.Lock()
toFlush := logBuffer.copyToFlush()
logBuffer.Unlock()
if toFlush != nil {
logBuffer.queueFlush(toFlush)
}
logBuffer.writeMu.Unlock()
}
}
func (logBuffer *LogBuffer) copyToFlush() *dataToFlush {
return logBuffer.copyToFlushInternal(false)
}
func (logBuffer *LogBuffer) copyToFlushWithCallback() *dataToFlush {
return logBuffer.copyToFlushInternal(true)
}
func (logBuffer *LogBuffer) copyToFlushInternal(withCallback bool) *dataToFlush {
if logBuffer.pos > 0 {
var d *dataToFlush
if logBuffer.flushFn != nil {
d = &dataToFlush{
startTime: logBuffer.startTime,
stopTime: logBuffer.stopTime,
data: copiedBytes(logBuffer.buf[:logBuffer.pos]),
minOffset: logBuffer.minOffset,
maxOffset: logBuffer.maxOffset,
}
// Add callback channel for synchronous ForceFlush
if withCallback {
d.done = make(chan struct{})
}
// Stamped under the lock so the budget can admit windows in the
// order they were sealed. Every stamped window must reach reserve
// exactly once or the queue stalls behind the missing turn.
d.seq = logBuffer.flushSeq
logBuffer.flushSeq++
}
// CRITICAL: logBuffer.offset is the "next offset to assign", so last offset in buffer is offset-1
lastOffsetInBuffer := logBuffer.offset - 1
// Slot 0 falls out of the ring in SealBuffer below, so record how far
// eviction has reached before it goes - in both timestamp spaces.
if evicted := logBuffer.prevBuffers.buffers[0]; evicted.size > 0 && !evicted.stopTime.IsZero() {
if ts := evicted.stopTime.UnixNano(); ts > logBuffer.lastEvictedTsNs.Load() {
logBuffer.lastEvictedTsNs.Store(ts)
}
if ts := evicted.maxOriginalTsNs; ts > logBuffer.lastEvictedOriginalTsNs.Load() {
logBuffer.lastEvictedOriginalTsNs.Store(ts)
}
}
logBuffer.buf = logBuffer.prevBuffers.SealBuffer(logBuffer.startTime, logBuffer.stopTime, logBuffer.buf, logBuffer.pos, logBuffer.bufferStartOffset, lastOffsetInBuffer)
logBuffer.prevBuffers.buffers[len(logBuffer.prevBuffers.buffers)-1].maxOriginalTsNs = logBuffer.curWindowMaxOriginalTsNs
logBuffer.curWindowMaxOriginalTsNs = 0
// SealBuffer hands back the oldest window array to reuse. An entry larger
// than BufferSize grew one of these arrays to fit it, and buffers cycle
// forever, so without this a single oversized entry leaves every later
// window carrying — and snapshotting — its size. Growth is on demand, so
// the next oversized entry just reallocates.
if len(logBuffer.buf) > BufferSize {
logBuffer.buf = make([]byte, BufferSize)
}
// Hand a fully extended prefix snapshot to the sealed slot so sealed
// readers reuse it instead of re-copying the window; reset for the next
// window either way (holders keep their immutable prefix slices).
if len(logBuffer.curSnap) == logBuffer.pos {
logBuffer.prevBuffers.buffers[len(logBuffer.prevBuffers.buffers)-1].snapshot = logBuffer.curSnap[:logBuffer.pos:logBuffer.pos]
}
logBuffer.curSnap = nil
// Use zero time (time.Time{}) not epoch time (time.Unix(0,0))
// Epoch time (1970) breaks time-based reads after flush
logBuffer.startTime = time.Time{}
logBuffer.stopTime = time.Time{}
logBuffer.pos = 0
logBuffer.idx = logBuffer.idx[:0]
// DON'T increment offset - it's already pointing to the next offset!
// logBuffer.offset++ // REMOVED - this was causing offset gaps!
logBuffer.bufferStartOffset = logBuffer.offset // Next buffer starts at current offset (which is already the next one)
// Reset offset tracking
logBuffer.hasOffsets = false
logBuffer.minOffset = 0
logBuffer.maxOffset = 0
// Invalidate disk cache chunks after flush
// The cache may contain stale data from before this flush
// Invalidating ensures consumers will re-read fresh data from disk after flush
logBuffer.invalidateAllDiskCacheChunks()
return d
}
return nil
}
// invalidateAllDiskCacheChunks clears all cached disk chunks
// This should be called after a buffer flush to ensure consumers read fresh data from disk
func (logBuffer *LogBuffer) invalidateAllDiskCacheChunks() {
logBuffer.diskChunkCache.mu.Lock()
defer logBuffer.diskChunkCache.mu.Unlock()
if len(logBuffer.diskChunkCache.chunks) > 0 {
logBuffer.diskChunkCache.chunks = make(map[int64]*CachedDiskChunk)
}
}
// GetEarliestTime returns the oldest timestamp still resident in the buffer.
// It must consider the sealed prev buffers in addition to the active buffer,
// because ReadFromBuffer's tsMemory (and therefore ResumeFromDiskError) is
// computed from the min across both. Returning only the active startTime
// would cause gap-detection callers to skip past data still living in prev
// buffers, and can also silently equal the consumer's lastReadTime.
func (logBuffer *LogBuffer) GetEarliestTime() time.Time {
logBuffer.RLock()
defer logBuffer.RUnlock()
earliest := logBuffer.startTime
for _, prevBuf := range logBuffer.prevBuffers.buffers {
if prevBuf.startTime.IsZero() {
continue
}
if earliest.IsZero() || prevBuf.startTime.Before(earliest) {
earliest = prevBuf.startTime
}
}
return earliest
}
func (logBuffer *LogBuffer) HasData() bool {
logBuffer.RLock()
defer logBuffer.RUnlock()
if logBuffer.pos > 0 {
return true
}
for _, buf := range logBuffer.prevBuffers.buffers {
if buf.size > 0 {
return true
}
}
return false
}
func (logBuffer *LogBuffer) GetEarliestPosition() MessagePosition {
return MessagePosition{
Time: logBuffer.startTime,
Offset: logBuffer.offset,
}
}
// FlushedThroughTsNs reports the timestamp through which this buffer's data
// is durably on disk: "now" when nothing is pending a flush (a later append
// is bumped past the head), otherwise the last flushed window's stop time.
// Covers only entries that reached the buffer - callers stamping timestamps
// before the append must also bound by their in-flight floor (see
// Filer.LocalFlushedThroughTsNs).
func (logBuffer *LogBuffer) FlushedThroughTsNs(nowNs int64) int64 {
flushed := logBuffer.lastFlushTsNs.Load()
if logBuffer.LastTsNs.Load() <= flushed {
return nowNs
}
return flushed
}
// GetLastFlushTsNs returns the latest flushed timestamp in Unix nanoseconds.
// Returns 0 if nothing has been flushed yet.
func (logBuffer *LogBuffer) GetLastFlushTsNs() int64 {
return logBuffer.lastFlushTsNs.Load()
}
// GetLastEvictedOriginalTsNs is GetLastEvictedTsNs in pre-bump timestamps -
// the space disk cursors live in.
func (logBuffer *LogBuffer) GetLastEvictedOriginalTsNs() int64 {
return logBuffer.lastEvictedOriginalTsNs.Load()
}
// GetLastEvictedTsNs returns the stopTime of the newest window dropped from the
// sealed ring, or 0 if nothing has been evicted. A reader positioned past it
// knows the retained buffers still hold every entry after its position, which is
// the only emptiness proof available to a buffer that never flushes.
func (logBuffer *LogBuffer) GetLastEvictedTsNs() int64 {
return logBuffer.lastEvictedTsNs.Load()
}
// MarkEvictedThrough treats entries at or below tsNs as evicted even though
// nothing was rotated out yet: a merge-fed buffer is born empty while its
// sources hold history it must not skip. Call before the first append.
func (logBuffer *LogBuffer) MarkEvictedThrough(tsNs int64) {
if tsNs > logBuffer.lastEvictedTsNs.Load() {
logBuffer.lastEvictedTsNs.Store(tsNs)
}
if tsNs > logBuffer.lastEvictedOriginalTsNs.Load() {
logBuffer.lastEvictedOriginalTsNs.Store(tsNs)
}
}
func (logBuffer *LogBuffer) SetLastFlushTsNs(ts int64) {
logBuffer.lastFlushTsNs.Store(ts)
}
func (d *dataToFlush) releaseMemory() {
// Clearing data makes a double release harmless.
if d.data != nil {
mem.Free(d.data)
d.data = nil
}
}
// ReadFromBuffer returns the in-memory log data at lastReadPosition. isPooled
// reports whether the returned buffer is a private pooled copy the caller must
// return via ReleaseMemory; when false the buffer wraps a snapshot shared with
// other readers and must not be released (or written to).
func (logBuffer *LogBuffer) ReadFromBuffer(lastReadPosition MessagePosition) (bufferCopy *bytes.Buffer, batchIndex int64, isPooled bool, err error) {
logBuffer.RLock()
defer logBuffer.RUnlock()
isOffsetBased := lastReadPosition.IsOffsetBased
// For offset-based subscriptions, use offset comparisons, not time comparisons!
if isOffsetBased {
requestedOffset := lastReadPosition.Offset
// Check if the requested offset is in the current buffer range
if requestedOffset >= logBuffer.bufferStartOffset && requestedOffset <= logBuffer.offset {
// If current buffer is empty (pos=0), check if data is on disk or not yet written
if logBuffer.pos == 0 {
// If buffer is empty but offset range covers the request,
// it means data was in memory and has been flushed/moved out.
// The bufferStartOffset advancing to cover this offset proves data existed.
//
// Three cases:
// 1. requestedOffset < logBuffer.offset: Data was here, now flushed
// 2. requestedOffset == logBuffer.offset && bufferStartOffset > 0: Buffer advanced, data flushed
// 3. requestedOffset == logBuffer.offset && bufferStartOffset == 0: Initial state - try disk first!
//
// Cases 1 & 2: try disk read
// Case 3: try disk read (historical data might exist)
if requestedOffset < logBuffer.offset {
// Data was in the buffer range but buffer is now empty = flushed to disk
return nil, -2, false, ResumeFromDiskError
}
// requestedOffset == logBuffer.offset: Current position
// CRITICAL: For subscribers starting from offset 0, try disk read first
// (historical data might exist from previous runs)
if requestedOffset == 0 && logBuffer.bufferStartOffset == 0 && logBuffer.offset == 0 {
// Initial state: try disk read before waiting for new data
return nil, -2, false, ResumeFromDiskError
}
// Otherwise, wait for new data to arrive
return nil, logBuffer.offset, false, nil
}
return logBuffer.currentSnapshotView(0, logBuffer.pos), logBuffer.offset, false, nil
}
// Check previous buffers for the requested offset
for _, buf := range logBuffer.prevBuffers.buffers {
if requestedOffset >= buf.startOffset && requestedOffset <= buf.offset {
// If prevBuffer is empty, it means the data was flushed to disk
// (prevBuffers are created when buffer is flushed)
if buf.size == 0 {
// Empty prevBuffer covering this offset means data was flushed
return nil, -2, false, ResumeFromDiskError
}
return sharedBufferView(buf, 0), buf.offset, false, nil
}
}
// Offset not found in any buffer
if requestedOffset < logBuffer.bufferStartOffset {
// Data not in current buffers - must be on disk (flushed or never existed)
// Return ResumeFromDiskError to trigger disk read
return nil, -2, false, ResumeFromDiskError
}
if requestedOffset > logBuffer.offset {
// Future data, not available yet
return nil, logBuffer.offset, false, nil
}
// Offset not found - return nil
return nil, logBuffer.offset, false, nil
}
// TIMESTAMP-BASED READ (original logic)
// Read from disk and memory
// 1. read from disk, last time is = td
// 2. in memory, the earliest time = tm
// if tm <= td, case 2.1
// read from memory
// if tm is empty, case 2.2
// read from memory
// if td < tm, case 2.3
// read from disk again
var tsMemory time.Time
if !logBuffer.startTime.IsZero() {
tsMemory = logBuffer.startTime
}
for _, prevBuf := range logBuffer.prevBuffers.buffers {
if !prevBuf.startTime.IsZero() {
// If tsMemory is zero, assign directly; otherwise compare
if tsMemory.IsZero() || prevBuf.startTime.Before(tsMemory) {
tsMemory = prevBuf.startTime
}
}
}
if tsMemory.IsZero() { // case 2.2
// Buffer is empty - return ResumeFromDiskError so caller can read from disk
// This fixes issue #4977 where SubscribeMetadata stalls because
// MetaAggregator.MetaLogBuffer is empty in single-filer setups
return nil, -2, false, ResumeFromDiskError
} else if lastReadPosition.Time.Before(tsMemory) { // case 2.3
// For time-based reads, only check timestamp for disk reads
// Don't use offset comparisons as they're not meaningful for time-based subscriptions
// A gated cursor below the eviction watermark must go to disk: serving it
// from the earliest retained window would silently skip the evicted span.
if lastReadPosition.Offset == EvictionGatedOffset && lastReadPosition.Time.UnixNano() < logBuffer.lastEvictedTsNs.Load() {
return nil, -2, false, ResumeFromDiskError
}
// Special case: If requested time is zero (Unix epoch), treat as "start from beginning"
// This handles queries that want to read all data without knowing the exact start time
if lastReadPosition.Time.IsZero() || lastReadPosition.Time.Unix() == 0 {
// Start from the beginning of memory
// Fall through to case 2.1 to read from earliest buffer
} else if lastReadPosition.Offset <= 0 && lastReadPosition.Time.Before(tsMemory) {
// Treat first read with sentinel/zero offset as inclusive of earliest in-memory data
} else {
// Data not in memory buffers - read from disk
return nil, -2, false, ResumeFromDiskError
}
}
// the following is case 2.1
if lastReadPosition.Time.Equal(logBuffer.stopTime) && !logBuffer.stopTime.IsZero() {
// For first-read sentinel/zero offset, allow inclusive read at the boundary
if lastReadPosition.Offset > 0 {
return nil, logBuffer.offset, false, nil
}
}
if lastReadPosition.Time.After(logBuffer.stopTime) && !logBuffer.stopTime.IsZero() {
return nil, logBuffer.offset, false, nil
}
// Also check prevBuffers when current buffer is empty (startTime is zero)
if lastReadPosition.Time.Before(logBuffer.startTime) || logBuffer.startTime.IsZero() {
for _, buf := range logBuffer.prevBuffers.buffers {
if buf.startTime.After(lastReadPosition.Time) {
return sharedBufferView(buf, 0), buf.offset, false, nil
}
if !buf.startTime.After(lastReadPosition.Time) && buf.stopTime.After(lastReadPosition.Time) {
searchTime := lastReadPosition.Time
if lastReadPosition.Offset <= 0 {
searchTime = searchTime.Add(-time.Nanosecond)
}
pos, err := buf.locateByTs(searchTime)
if err != nil {
// Buffer corruption detected - return error wrapped with ErrBufferCorrupted
glog.Errorf("ReadFromBuffer: buffer corruption in prevBuffer: %v", err)
return nil, -1, false, fmt.Errorf("%w: %v", ErrBufferCorrupted, err)
}
if pos < buf.size {
return sharedBufferView(buf, pos), buf.offset, false, nil
}
}
}
// If current buffer is not empty, return it
if logBuffer.pos > 0 {
return logBuffer.currentSnapshotView(0, logBuffer.pos), logBuffer.offset, false, nil
}
// Buffer is empty and no data in prevBuffers - wait for new data
return nil, logBuffer.offset, false, nil
}
lastTs := lastReadPosition.Time.UnixNano()
// Inclusive boundary for first-read sentinel/zero offset
searchTs := lastTs
if lastReadPosition.Offset <= 0 {
if searchTs > math.MinInt64+1 { // prevent underflow
searchTs = searchTs - 1
}
}
l, h := 0, len(logBuffer.idx)-1
/*
for i, pos := range m.idx {
logEntry, ts := readTs(m.buf, pos)
event := &filer_pb.SubscribeMetadataResponse{}
proto.Unmarshal(logEntry.Data, event)
entry := event.EventNotification.OldEntry
if entry == nil {
entry = event.EventNotification.NewEntry
}
}
*/
for l <= h {
mid := (l + h) / 2
pos := logBuffer.idx[mid]
_, t, err := readTs(logBuffer.buf, pos)
if err != nil {
// Buffer corruption detected in binary search
glog.Errorf("ReadFromBuffer: buffer corruption at idx[%d] pos %d: %v", mid, pos, err)
return nil, -1, false, fmt.Errorf("%w: %v", ErrBufferCorrupted, err)
}
if t <= searchTs {
l = mid + 1
} else if searchTs < t {
var prevT int64
if mid > 0 {
_, prevT, err = readTs(logBuffer.buf, logBuffer.idx[mid-1])
if err != nil {
// Buffer corruption detected in binary search (previous entry)
glog.Errorf("ReadFromBuffer: buffer corruption at idx[%d] pos %d: %v", mid-1, logBuffer.idx[mid-1], err)
return nil, -1, false, fmt.Errorf("%w: %v", ErrBufferCorrupted, err)
}
}
if prevT <= searchTs {
return logBuffer.currentSnapshotView(pos, logBuffer.pos), logBuffer.offset, false, nil
}
h = mid
}
}
// Binary search didn't find the timestamp - data may have been flushed to disk already
// Returning -2 signals to caller that data is not available in memory
return nil, -2, false, nil
}
// ReleaseMemory returns a pooled buffer for reuse. Only call it for buffers
// ReadFromBuffer reported as pooled: recycling a shared sealed-window view
// would let the next copiedBytes overwrite bytes other readers still hold.
func (logBuffer *LogBuffer) ReleaseMemory(b *bytes.Buffer) {
bufferPool.Put(b)
}
// GetName returns the log buffer name for metadata tracking
func (logBuffer *LogBuffer) GetName() string {
logBuffer.RLock()
defer logBuffer.RUnlock()
return logBuffer.name
}
// GetOffset returns the current offset for metadata tracking
func (logBuffer *LogBuffer) GetOffset() int64 {
logBuffer.RLock()
defer logBuffer.RUnlock()
return logBuffer.offset
}
var bufferPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
// copiedBytes returns a private copy of buf backed by the shared, size-classed
// slab pool. The caller owns it until mem.Free (see dataToFlush.releaseMemory),
// so the live buffer never outlives the flush. Routing through mem reuses slabs
// across the process instead of reallocating a window-sized array per flush.
func copiedBytes(buf []byte) []byte {
copied := mem.Allocate(len(buf))
copy(copied, buf)
return copied
}
// sharedBufferView wraps the sealed window's shared snapshot from pos onward.
// The returned buffer aliases memory shared with other readers: it must be
// treated as read-only and never passed to ReleaseMemory (the full-slice cap
// makes an accidental append reallocate instead of scribbling on the snapshot).
func sharedBufferView(mb *MemBuffer, pos int) *bytes.Buffer {
snap := mb.sharedSnapshot()
return bytes.NewBuffer(snap[pos:len(snap):len(snap)])
}
// currentSnapshotView returns buf[from:to] of the current window as a view of
// the shared prefix snapshot, extending the snapshot to cover [0:to) first.
// buf[:pos] is append-only until the window seals, so extension only ever
// appends stable bytes; earlier holders' slices are unaffected. Callers must
// hold the read lock (keeps buf and pos stable during extension).
func (logBuffer *LogBuffer) currentSnapshotView(from, to int) *bytes.Buffer {
logBuffer.curSnapMu.Lock()
if cap(logBuffer.curSnap) < to {
// First use in this window (or the rare window whose array outgrew the
// previous one): size to the window array so extensions never reallocate.
grown := make([]byte, len(logBuffer.curSnap), len(logBuffer.buf))
copy(grown, logBuffer.curSnap)
logBuffer.curSnap = grown
}
if len(logBuffer.curSnap) < to {
logBuffer.curSnap = append(logBuffer.curSnap, logBuffer.buf[len(logBuffer.curSnap):to]...)
}
snap := logBuffer.curSnap[:to]
logBuffer.curSnapMu.Unlock()
return bytes.NewBuffer(snap[from:to:to])
}
func readTs(buf []byte, pos int) (size int, ts int64, err error) {
// Bounds check for size field (overflow-safe)
if pos < 0 || pos > len(buf)-4 {
return 0, 0, fmt.Errorf("corrupted log buffer: cannot read size at pos %d, buffer length %d", pos, len(buf))
}
size = int(util.BytesToUint32(buf[pos : pos+4]))
// Bounds check for entry data (overflow-safe, protects against negative size)
if size < 0 || size > len(buf)-pos-4 {
return 0, 0, fmt.Errorf("corrupted log buffer: entry size %d at pos %d exceeds buffer length %d", size, pos, len(buf))
}
entryData := buf[pos+4 : pos+4+size]
// Read only LogEntry.ts_ns rather than unmarshaling the whole entry. This
// runs on every binary-search probe in ReadFromBuffer; a full proto.Unmarshal
// there allocates fresh slices for the data/key byte fields on each call, which
// dominated allocation churn under metadata-subscription fan-out.
ts, err = readTsNs(entryData)
if err != nil {
return 0, 0, fmt.Errorf("corrupted log buffer at pos %d, size %d: %w", pos, size, err)
}
return size, ts, nil
}
// readTsNs scans a marshaled LogEntry and returns its ts_ns (field 1, varint)
// without decoding the data/key byte fields. Fields serialize in number order,
// so ts_ns is normally the first tag and this returns after one varint; the full
// scan keeps it correct for any field order. A missing field 1 means the proto3
// default, ts_ns == 0.
func readTsNs(entryData []byte) (tsNs int64, err error) {
for i := 0; i < len(entryData); {
tag, n := binary.Uvarint(entryData[i:])
if n <= 0 {
return 0, fmt.Errorf("bad field tag at %d", i)
}
i += n
fieldNum := tag >> 3
switch tag & 0x7 { // wire type
case 0: // varint
v, m := binary.Uvarint(entryData[i:])
if m <= 0 {
return 0, fmt.Errorf("bad varint for field %d", fieldNum)
}
i += m
if fieldNum == 1 { // ts_ns
return int64(v), nil
}
case 1: // 64-bit
i += 8
case 2: // length-delimited: read length, skip payload without copying
l, m := binary.Uvarint(entryData[i:])
if m <= 0 {
return 0, fmt.Errorf("bad length for field %d", fieldNum)
}
i += m
if l > uint64(len(entryData)-i) {
return 0, fmt.Errorf("field %d length %d overruns buffer", fieldNum, l)
}
i += int(l)
case 5: // 32-bit
i += 4
default:
return 0, fmt.Errorf("unknown wire type for field %d", fieldNum)
}
if i > len(entryData) {
return 0, fmt.Errorf("field %d overruns buffer", fieldNum)
}
}
return 0, nil
}
// unmarshalLogEntryAliased decodes a marshaled LogEntry into out, pointing the
// data and key fields at sub-slices of entryData instead of copying them. A full
// proto.Unmarshal allocates fresh slices for those byte fields on every entry
// (protobuf consumeBytesNoZero), which dominated allocation churn when many
// metadata subscribers each re-read the in-memory log window.
//
// The aliased data/key are only valid while entryData is, i.e. for the duration
// of the eachLogEntryFn callback. Callers must copy anything they retain past the
// callback; all current subscribers do (they re-decode data into their own event
// or hand it to a synchronous grpc Send).
func unmarshalLogEntryAliased(entryData []byte, out *filer_pb.LogEntry) error {
out.TsNs = 0
out.PartitionKeyHash = 0
out.Data = nil
out.Key = nil
out.Offset = 0
for i := 0; i < len(entryData); {
tag, n := binary.Uvarint(entryData[i:])
if n <= 0 {
return fmt.Errorf("bad field tag at %d", i)
}
i += n
fieldNum := tag >> 3
switch tag & 0x7 { // wire type
case 0: // varint: ts_ns / partition_key_hash / offset
v, m := binary.Uvarint(entryData[i:])
if m <= 0 {
return fmt.Errorf("bad varint for field %d", fieldNum)
}
i += m
switch fieldNum {
case 1:
out.TsNs = int64(v)
case 2:
out.PartitionKeyHash = int32(v)
case 5:
out.Offset = int64(v)
}
case 2: // length-delimited: data / key, aliased not copied
l, m := binary.Uvarint(entryData[i:])
if m <= 0 {
return fmt.Errorf("bad length for field %d", fieldNum)
}
i += m
if l > uint64(len(entryData)-i) {
return fmt.Errorf("field %d length %d overruns buffer", fieldNum, l)
}
switch fieldNum {
case 3:
out.Data = entryData[i : i+int(l)]
case 4:
out.Key = entryData[i : i+int(l)]
}
i += int(l)
case 1: // 64-bit
i += 8
case 5: // 32-bit
i += 4
default:
return fmt.Errorf("unknown wire type for field %d", fieldNum)
}
if i > len(entryData) {
return fmt.Errorf("field %d overruns buffer", fieldNum)
}
}
return nil
}