mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-10-26 15:00:48 +01:00
* filer: enable per-path commit records only when a peer has its own store Since seaweedfs#11698 every metadata change writes a per-path commit record into the filer store (an MpTs key, plus an MpDl key on deletes), and the records stay after the path is deleted. They order the events a filer replays from peer filers that keep their own store, since replay only runs when peerSignature != f.Signature. Filers that all share one store (one Redis, MySQL, TiKV, ...) never replay each other's events, so the records are never read, yet each change still costs one or two extra store writes and the key count grows with every path ever changed — unbounded growth for in-memory stores. This turns the records off by default and latches them on the first time a peer reports a different store signature — no option needed. The latch is persisted in the store (MpRep) so it survives restarts and converges filers sharing a store even before they discover the peer themselves. While off, a filer keeps an MpOff marker so enabling later bumps the records' generation (MpGen, folded into each record key) past the unrecorded window: a record older than a change made while off cannot let an older replayed peer event win. Ordering for unrecorded changes falls back to the entry's mtime, the same as entries written before the records existed. Supersedes the manual option approach in seaweedfs#12060. * filer: keep delete tombstones while records off, harden enable path Review on the first commit: - A delete committed while the records were off left no entry and no usable tombstone: after enabling, an older replayed create could resurrect the path. Deletes and rename sources are now always recorded under the generation-0 MpDl key — they are never fenced by a generation bump, and they cannot go stale because every delete is recorded. Creates/updates still write nothing while off. - bumpPathCommitGen could regress MpGen under concurrent bumpers on a shared store. It now loops: each pass writes only a generation higher than any it has seen, so concurrent bumps converge upward. - doSubscribeToOneFiler proceeded into replay when enablePathCommitRecords failed, running the whole session without ordering. It now aborts the subscribe so the reconnect retries with the records on. * filer: bind pending commit-record repairs to their generation A failed MpTs write kept in metaTsPending could land after a shared-store generation bump, writing a pre-bump timestamp as a current-generation record and overriding newer unrecorded state. Each pending change record now carries the generation it failed under: repairs write it only while that generation is current, and ordering reads ignore it otherwise. Pending delete records need no binding — MpDl keys are never fenced. * filer: never move the local records' generation backward The converge loop in bumpPathCommitGen seeded from zero and both it and syncPathCommitGen stored unconditionally: a stale writer could leave the stored generation below what this filer already adopted, and the next sync or bump then walked the local value back — orphaning records it wrote under the higher generation. The bump now seeds from the local generation and every store only moves it forward. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer: name a records generation after the off marker it consumes Two filers consuming the same off marker each counted one past the stored generation, so the second could store N+2 after the first stored N+1 and write records under a key the other could not read until the next check. The bump now takes the marker's own timestamp — filers racing on one marker land on one value — a marker older than the stored generation is cleaned as a leftover, and a stored value that already passed the bump is adopted instead of counted past. A newly enabling filer whose marker was already consumed names its own off window with the current time, so its unrecorded period is still fenced. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer: tell a fresh off marker from one the generation already consumed A marker whose timestamp reads at or below the stored generation was dropped as leftover, but a new off period can also read that old — the off filer's clock lagging, or a host clock moving back — and dropping it kept records from before that period live, so stale replay could win. The stored generation now names the marker it was bumped for: a marker the store still shows is consumed unless it is that one, however old it reads, and only the consumed marker itself is cleared without a bump. The generation trails in the value, so a whole-slice read still yields it on stores written before the pair was stored. Enabling fences the filer's own off window with the current time instead of a marker a sharer may already have consumed. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer: cover racing off-marker consumers settling on one generation * filer: adopt the stored generation, not the consumed marker it names A filer syncing with no off marker present read the value's first field — the consumed marker's timestamp — so a restart after a lagging-clock off period under-counted the generation and kept records the bump dropped. * filer: re-check the off marker before consuming it A sync whose marker read ran behind a later off period's consume cycle still bumped for its stale read: the stored generation already covered a different marker, so the bump overwrote the consumed name, counted the generation past records a newer consumption meant to keep, and cleared whatever marker was live by then. Each pass now re-reads the marker and only bumps or clears while it is still the one the pass saw. * filer: keep a failed generation read from resetting the records' generation The final generation read ignored errors and then stored whatever gen held — possibly zero — so a transient store failure could hide records the current generation wrote. Unexpected read errors now return before the local generation changes. Also cover a sync whose marker read runs behind another consumer's delete: it must adopt the stored generation, not bump for the stale read. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer: fail a bump that cannot verify the stored generation A generation read that failed mid-bump was treated as absent: the write could land below a generation a racing bumper had stored, or re-store a marker that bumper had already consumed. Read failures now abort the pass like every other store error on the path. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer: cover generation reads that fail instead of returning a value Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer: carry the commit timestamp on the entry itself The per-path MpTs record only exists because a commit timestamp had nowhere else to live. Stamping commit_ts_ns on the entry before its store write - and reusing that stamp as the emitted event's TsNs - makes the entry its own commit record: replay and bootstrap traversal order on it with zero extra writes for creates and updates. Deletes still need a durable key since a removed entry cannot carry a timestamp, so MpDl tombstones stay (one write per delete, growth bounded by delete history). Entries predating the field fall back to mtime, matching what peers and old records already assume. This replaces the MpTs/MpOff/MpGen/MpRep machinery entirely: shared- store filers write nothing extra, and dedicated-store replication keeps last-writer-wins ordering plus tombstone coverage for deletes and ancestor drops. * filer: keep a directory update replay from tombstoning its subtree A same-path update wrote a delete record before the delete-and-reinsert, so a directory metadata change read as a subtree delete: a later peer event for a child older than the update was then skipped even though nobody deleted the children, leaving files missing on this filer. Only file updates take the tombstone now — the re-inserted directory entry's own commit stamp orders the path itself. * filer: order unstamped entries by their recorded commit before mtime Entries written before stamping existed have no CommitTsNs, so the ordering check fell back to the client-settable mtime and dropped the MpTs record those versions wrote: an entry committed at event time 300 with mtime 100 lost to a delayed peer event at 200 after upgrade. Read the legacy record for unstamped entries in localCommitTsNs, CommitTsNs, and the traverse stamp (which also stopped falling back to mtime, regressing entry_ts_ns to 0). * filer: release queued rename event stamps when the rename fails Each moved entry carried an in-flight stamp its queued event would adopt at emit time. When the move succeeded but a later child move or the commit failed, the handler rolled back without emitting the queued events or releasing their stamps, pinning the delivered and flushed watermarks below the aborted rename forever. Release every queued stamp on both failure exits in both rename handlers. * filer: apply a directory update replay with an in-place upsert Deleting the stored entry before inserting its replacement left a window where the directory had no entry and no ordering record — a tombstone cannot stand in for it without reading as a subtree delete that suppresses the children's own records. A stale peer event accepted in that gap could replay an older update or delete and sweep the directory's children. Upsert the replacement instead: the entry keeps its recorded commit order across the whole update, and a directory has no chunks or hard links for a delete to release. * filer: update an existing directory entry on replay instead of upserting InsertEntry is not an upsert on SQL stores configured without upsert support — PostgreSQL with enableUpsert=false issues a plain INSERT and its duplicate-key error does not match the MySQL-only fallback, so a replay updating a present directory failed on every retry. Pick UpdateEntry when the entry exists and InsertEntry when it is absent; UpdateEntry touches no row that is not there. * filer: detect a shared-store delete racing a replayed directory update UpdateEntry reports success even when the row was deleted between the find and the update — the UPDATE changes zero rows and the replayed change is dropped while the subscription offset advances. Re-read the entry after the update and retry the write, re-checking the recorded commit order each pass so a newer peer change wins instead of being resurrected over. * filer: declare commit_ts_ns in field-number order Field 6 sits between extended and hard_link_id; the declaration now reads in the same order. Wire format unchanged. Also syncs the Java client's checked-in copy of filer.proto, which the pb Makefile copies on gen. * filer: option to skip delete tombstones entirely filer.options.deleteTombstones=false stops writing MpDl records on deletes - a single filer or shared-store cluster never replays peer events, so the tombstones are dead weight. The tradeoff is explicit: if a dedicated-store peer does replicate, a stale event can resurrect a deleted path. * filer: drop delete tombstones entirely Deletes no longer write MpDl records, and the pending-repair machinery they needed is gone with them: per-mutation metadata overhead is now zero for every store topology. Ordering information lives only on the entry itself via commit_ts_ns. The accepted tradeoff: a dedicated-store peer replaying an event older than a delete can resurrect the path, and a kept directory no longer fences stale child creates. Recorded tombstones and MpTs records left by older builds are still read, so upgrades keep the protections they already have. * filer: stop reading legacy per-path records too Drops the MpTs/MpDl read path along with the writes: meta_path_ts.go keeps only the entry-field readers (CommitTsNs, EntryCommitTsNs, localCommitTsNs). Entries written before stamping existed fall back to mtime, and tombstones or commit records older builds left behind no longer fence replayed events. * filer: trim commit timestamp comments --------- Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
514 lines
16 KiB
Go
514 lines
16 KiB
Go
package filer
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
nethttp "net/http"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/notification"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
|
|
)
|
|
|
|
func (f *Filer) NotifyUpdateEvent(ctx context.Context, oldEntry, newEntry *Entry, deleteChunks, isFromOtherCluster bool, signatures []int32) {
|
|
f.notifyUpdateEvent(ctx, oldEntry, newEntry, deleteChunks, isFromOtherCluster, signatures)
|
|
}
|
|
|
|
func (f *Filer) notifyUpdateEvent(ctx context.Context, oldEntry, newEntry *Entry, deleteChunks, isFromOtherCluster bool, signatures []int32) *filer_pb.SubscribeMetadataResponse {
|
|
if metadataEventsSuppressed(ctx) {
|
|
// The entry's stamp stays in flight for the covering event the
|
|
// caller emits itself (the rename handler does).
|
|
return nil
|
|
}
|
|
|
|
var fullpath string
|
|
if oldEntry != nil {
|
|
fullpath = string(oldEntry.FullPath)
|
|
} else if newEntry != nil {
|
|
fullpath = string(newEntry.FullPath)
|
|
} else {
|
|
return nil
|
|
}
|
|
|
|
// println("fullpath:", fullpath)
|
|
|
|
if strings.HasPrefix(fullpath, SystemLogDir) {
|
|
// No event covers this write: release its stamp.
|
|
if newEntry != nil {
|
|
f.metaLogInflight.release(newEntry.CommitTsNs)
|
|
}
|
|
return nil
|
|
}
|
|
foundSelf := false
|
|
for _, sig := range signatures {
|
|
if sig == f.Signature {
|
|
foundSelf = true
|
|
}
|
|
}
|
|
if !foundSelf {
|
|
signatures = append(signatures, f.Signature)
|
|
}
|
|
|
|
event := f.newMetadataEvent(oldEntry, newEntry, deleteChunks, isFromOtherCluster, signatures, f.stampOf(newEntry))
|
|
// Clear the stamp after the buffer append below - deliberately also on
|
|
// append failure (see the metaLogInflight comment).
|
|
defer f.metaLogInflight.done(event.TsNs)
|
|
eventNotification := event.EventNotification
|
|
|
|
if notification.Queue != nil {
|
|
glog.V(3).Infof("notifying entry update %v", fullpath)
|
|
if err := notification.Queue.SendMessage(fullpath, eventNotification); err != nil {
|
|
// throw message
|
|
glog.Error(err)
|
|
}
|
|
}
|
|
|
|
f.logMetaEvent(ctx, event)
|
|
if sink := metadataEventSinkFromContext(ctx); sink != nil {
|
|
sink.Record(event)
|
|
}
|
|
|
|
f.onMetadataChangeEvent(event)
|
|
|
|
return event
|
|
}
|
|
|
|
// metaLogInflight tracks events stamped but not yet appended to the local
|
|
// log buffer - the two are separated by notification work that can block,
|
|
// and a claim ignoring that window would assert durability or delivery for
|
|
// timestamps still on their way in. Stamping shares the reader's lock, so an
|
|
// event is always visible here or (bumped monotonically) in the buffer.
|
|
//
|
|
// An event whose append fails also clears its stamp: it is dropped from the
|
|
// change stream entirely (loudly logged there), and a watermark waiting for
|
|
// it would pin this filer's claims forever.
|
|
type metaLogInflight struct {
|
|
sync.Mutex
|
|
stamped map[int64]int
|
|
lastStampNs int64
|
|
lastClaimNs int64
|
|
}
|
|
|
|
// stamp assigns the event timestamp and registers it as in flight. Stamps
|
|
// are monotonic against the registry's own history and every issued claim,
|
|
// so a wall-clock step backwards cannot slip a new stamp under a floor or
|
|
// watermark already handed out.
|
|
func (t *metaLogInflight) stamp() int64 {
|
|
t.Lock()
|
|
defer t.Unlock()
|
|
floor := t.lastStampNs
|
|
if t.lastClaimNs > floor {
|
|
floor = t.lastClaimNs
|
|
}
|
|
tsNs := time.Now().UnixNano()
|
|
if tsNs <= floor {
|
|
tsNs = floor + 1
|
|
}
|
|
t.lastStampNs = tsNs
|
|
if t.stamped == nil {
|
|
t.stamped = make(map[int64]int)
|
|
}
|
|
t.stamped[tsNs]++
|
|
return tsNs
|
|
}
|
|
|
|
// done removes a stamp once the event has been appended to the buffer.
|
|
func (t *metaLogInflight) done(tsNs int64) {
|
|
t.Lock()
|
|
defer t.Unlock()
|
|
if t.stamped[tsNs] <= 1 {
|
|
delete(t.stamped, tsNs)
|
|
} else {
|
|
t.stamped[tsNs]--
|
|
}
|
|
}
|
|
|
|
// inFlight reports whether tsNs is a pre-commit stamp still awaiting its
|
|
// buffer append — how an event adopts the stamp an entry carries.
|
|
func (t *metaLogInflight) inFlight(tsNs int64) bool {
|
|
t.Lock()
|
|
defer t.Unlock()
|
|
return t.stamped[tsNs] > 0
|
|
}
|
|
|
|
// release drops a stamp whose event will never be appended.
|
|
func (t *metaLogInflight) release(tsNs int64) {
|
|
if tsNs == 0 {
|
|
return
|
|
}
|
|
t.Lock()
|
|
defer t.Unlock()
|
|
if t.stamped[tsNs] <= 1 {
|
|
delete(t.stamped, tsNs)
|
|
} else {
|
|
t.stamped[tsNs]--
|
|
}
|
|
}
|
|
|
|
// minTsNs returns the oldest in-flight stamp, or 0 when nothing is in flight.
|
|
func (t *metaLogInflight) minTsNs() int64 {
|
|
t.Lock()
|
|
defer t.Unlock()
|
|
var min int64
|
|
for tsNs := range t.stamped {
|
|
if min == 0 || tsNs < min {
|
|
min = tsNs
|
|
}
|
|
}
|
|
return min
|
|
}
|
|
|
|
// claimThrough caps a completeness claim by the oldest in-flight stamp and
|
|
// fences it: later stamps always land above the returned claim, so a wall
|
|
// clock stepping backwards cannot slide a new event under a watermark a
|
|
// peer has already advanced to.
|
|
func (t *metaLogInflight) claimThrough(nowNs int64) int64 {
|
|
t.Lock()
|
|
defer t.Unlock()
|
|
claim := nowNs
|
|
for tsNs := range t.stamped {
|
|
if tsNs-1 < claim {
|
|
claim = tsNs - 1
|
|
}
|
|
}
|
|
if claim > t.lastClaimNs {
|
|
t.lastClaimNs = claim
|
|
}
|
|
return claim
|
|
}
|
|
|
|
// LocalFlushedThroughTsNs reports the timestamp through which the local meta
|
|
// log is durably on disk: everything at or below it is appended and flushed,
|
|
// and nothing can land at or below it later. The registry is consulted before
|
|
// the buffer: an event already appended is visible to the buffer claim, one
|
|
// still in flight caps the claim, and one stamped later is fenced above it.
|
|
func (f *Filer) LocalFlushedThroughTsNs(nowNs int64) int64 {
|
|
claim := f.metaLogInflight.claimThrough(nowNs)
|
|
if buffered := f.LocalMetaLogBuffer.FlushedThroughTsNs(nowNs); buffered < claim {
|
|
claim = buffered
|
|
}
|
|
return claim
|
|
}
|
|
|
|
// LocalDeliveredThroughTsNs caps a delivery-freshness claim (an idle
|
|
// heartbeat's timestamp) by the oldest in-flight stamp: a stamped-but-
|
|
// unappended event has not been streamed to anyone, and a peer aggregator
|
|
// turns the claim into its delivery low-watermark.
|
|
func (f *Filer) LocalDeliveredThroughTsNs(nowNs int64) int64 {
|
|
return f.metaLogInflight.claimThrough(nowNs)
|
|
}
|
|
|
|
// StampMetaLogInflightForTest and DoneMetaLogInflightForTest let tests in
|
|
// other packages exercise the claim caps. Not for production use.
|
|
func (f *Filer) StampMetaLogInflightForTest() int64 {
|
|
return f.metaLogInflight.stamp()
|
|
}
|
|
|
|
func (f *Filer) DoneMetaLogInflightForTest(tsNs int64) {
|
|
f.metaLogInflight.done(tsNs)
|
|
}
|
|
|
|
// stampEntryCommitTsNs assigns the mutation's event timestamp to the entry
|
|
// pre-write so the emitted event and stored version carry the same commit
|
|
// time. The stamp stays in flight until the event appends; a write no event
|
|
// covers must release it via releaseEntryCommitTsNs.
|
|
func (f *Filer) stampEntryCommitTsNs(entry *Entry) int64 {
|
|
tsNs := f.metaLogInflight.stamp()
|
|
entry.CommitTsNs = tsNs
|
|
return tsNs
|
|
}
|
|
|
|
// releaseEntryCommitTsNs drops a stamp no event will adopt.
|
|
func (f *Filer) releaseEntryCommitTsNs(tsNs int64) {
|
|
f.metaLogInflight.release(tsNs)
|
|
}
|
|
|
|
// StampEntryCommitTsNs/ReleaseEntryCommitTsNs expose the lifecycle to the
|
|
// filer server's mutation paths.
|
|
func (f *Filer) StampEntryCommitTsNs(entry *Entry) int64 {
|
|
return f.stampEntryCommitTsNs(entry)
|
|
}
|
|
|
|
func (f *Filer) ReleaseEntryCommitTsNs(tsNs int64) {
|
|
f.metaLogInflight.release(tsNs)
|
|
}
|
|
|
|
// stampOf returns the event's timestamp: the entry's in-flight stamp, or a
|
|
// fresh one for events with no newly stamped entry (deletes).
|
|
func (f *Filer) stampOf(newEntry *Entry) int64 {
|
|
if newEntry != nil && f.metaLogInflight.inFlight(newEntry.CommitTsNs) {
|
|
return newEntry.CommitTsNs
|
|
}
|
|
return f.metaLogInflight.stamp()
|
|
}
|
|
|
|
func (f *Filer) newMetadataEvent(oldEntry, newEntry *Entry, deleteChunks, isFromOtherCluster bool, signatures []int32, tsNs int64) *filer_pb.SubscribeMetadataResponse {
|
|
if oldEntry == nil && newEntry == nil {
|
|
return nil
|
|
}
|
|
var fullpath util.FullPath
|
|
if oldEntry != nil {
|
|
fullpath = oldEntry.FullPath
|
|
}
|
|
if fullpath == "" && newEntry != nil {
|
|
fullpath = newEntry.FullPath
|
|
}
|
|
dir, _ := fullpath.DirAndName()
|
|
newParentPath := ""
|
|
if newEntry != nil {
|
|
newParentPath, _ = newEntry.FullPath.DirAndName()
|
|
}
|
|
return &filer_pb.SubscribeMetadataResponse{
|
|
Directory: dir,
|
|
EventNotification: &filer_pb.EventNotification{
|
|
OldEntry: oldEntry.ToProtoEntry(),
|
|
NewEntry: newEntry.ToProtoEntry(),
|
|
DeleteChunks: deleteChunks,
|
|
NewParentPath: newParentPath,
|
|
IsFromOtherCluster: isFromOtherCluster,
|
|
Signatures: signatures,
|
|
},
|
|
// In flight until appended to the local log buffer (see metaLogInflight).
|
|
TsNs: tsNs,
|
|
}
|
|
}
|
|
|
|
func (f *Filer) logMetaEvent(ctx context.Context, event *filer_pb.SubscribeMetadataResponse) {
|
|
data, err := proto.Marshal(event)
|
|
if err != nil {
|
|
glog.Errorf("failed to marshal filer_pb.SubscribeMetadataResponse %+v: %v", event, err)
|
|
return
|
|
}
|
|
|
|
if err := f.LocalMetaLogBuffer.AddDataToBuffer([]byte(event.Directory), data, event.TsNs); err != nil {
|
|
glog.Errorf("failed to add data to log buffer for %s: %v", event.Directory, err)
|
|
}
|
|
|
|
}
|
|
|
|
// metadataLogUploadLimit is the piece size a metadata log flush starts with. A
|
|
// volume server refuses anything over its -fileSizeLimitMB (256 MB by default),
|
|
// and a single oversized event — a CreateEntry carrying a large inline Content,
|
|
// say — grows the log buffer well past that, leaving a blob that can never be
|
|
// written and blocks every later flush behind it. BufferSize is what an
|
|
// ordinary flush already produces, so it is a size the volume server accepts
|
|
// under any configuration that works at all; a cluster running below it says so
|
|
// in the rejection and volumeFileSizeLimit picks the real limit up from there.
|
|
const metadataLogUploadLimit = log_buffer.BufferSize
|
|
|
|
// shutdownMetadataLogFlushBudget bounds how long metadata-log flushes may keep
|
|
// retrying once the filer is shutting down; Shutdown cancels the shared flush
|
|
// context when it expires.
|
|
const shutdownMetadataLogFlushBudget = 15 * time.Second
|
|
|
|
var fileSizeLimitPattern = regexp.MustCompile(`file over the limited (\d+) bytes`)
|
|
|
|
// volumeFileSizeLimit reads the byte limit back out of a volume server's size
|
|
// rejection, and returns 0 for any other error.
|
|
func volumeFileSizeLimit(err error) int {
|
|
match := fileSizeLimitPattern.FindStringSubmatch(err.Error())
|
|
if match == nil {
|
|
return 0
|
|
}
|
|
limit, convErr := strconv.Atoi(match[1])
|
|
if convErr != nil {
|
|
return 0
|
|
}
|
|
return limit
|
|
}
|
|
|
|
func (f *Filer) logFlushFunc(logBuffer *log_buffer.LogBuffer, startTime, stopTime time.Time, buf []byte, minOffset, maxOffset int64) {
|
|
|
|
if len(buf) == 0 {
|
|
return
|
|
}
|
|
|
|
startTime, stopTime = startTime.UTC(), stopTime.UTC()
|
|
|
|
targetFile := fmt.Sprintf("%s/%04d-%02d-%02d/%02d-%02d.%08x", SystemLogDir,
|
|
startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(), f.UniqueFilerId,
|
|
// startTime.Second(), startTime.Nanosecond(),
|
|
)
|
|
|
|
// One piece at a time, each retried on its own so a partial success is not
|
|
// replayed, and the piece size follows the limit the volume servers report.
|
|
// While the filer keeps running the retry is unbounded so no metadata is
|
|
// dropped; once Shutdown arms the flush deadline the shared context cancels
|
|
// and the flush drops what is left instead of holding shutdown open.
|
|
limit := metadataLogUploadLimit
|
|
ctx := f.flushCtx
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
for len(buf) > 0 {
|
|
piece := nextLogPiece(buf, limit)
|
|
if err := f.appendToFile(ctx, targetFile, piece); err != nil {
|
|
glog.V(0).Infof("metadata log write failed %s: %v", targetFile, err)
|
|
if reported := volumeFileSizeLimit(err); reported > 0 && reported < limit {
|
|
glog.V(0).Infof("metadata log upload limit lowered to %d bytes", reported)
|
|
limit = reported
|
|
continue
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
logBuffer.NoteFlushDropped(len(buf))
|
|
glog.V(0).Infof("metadata log flush abandoned during shutdown, %d bytes left for %s: %v", len(buf), targetFile, ctx.Err())
|
|
return
|
|
case <-time.After(737 * time.Millisecond):
|
|
}
|
|
continue
|
|
}
|
|
buf = buf[len(piece):]
|
|
}
|
|
}
|
|
|
|
// nextLogPiece returns the leading piece of a flushed log buffer, at most
|
|
// maxSize bytes and ending on a record boundary where it can so the piece still
|
|
// decodes on its own. A record longer than maxSize is cut by size instead; the
|
|
// readers fall back to streaming the whole file when a chunk does not decode
|
|
// standalone, so a record may cross a chunk boundary.
|
|
func nextLogPiece(buf []byte, maxSize int) []byte {
|
|
if len(buf) <= maxSize {
|
|
return buf
|
|
}
|
|
|
|
pos := 0
|
|
for pos+4 <= len(buf) {
|
|
size := int(util.BytesToUint32(buf[pos : pos+4]))
|
|
end := pos + 4 + size
|
|
if size <= 0 || end > len(buf) || end > maxSize {
|
|
break
|
|
}
|
|
pos = end
|
|
}
|
|
if pos == 0 {
|
|
// Either the leading record alone is over the limit, or buf starts
|
|
// mid-record because the piece before it was cut by size.
|
|
return buf[:maxSize]
|
|
}
|
|
return buf[:pos]
|
|
}
|
|
|
|
var (
|
|
volumeNotFoundPattern = regexp.MustCompile(`volume \d+? not found`)
|
|
chunkNotFoundPattern = regexp.MustCompile(`(urls not found|File Not Found)`)
|
|
httpNotFoundPattern = regexp.MustCompile(`404 Not Found: not found`)
|
|
)
|
|
|
|
// isChunkNotFoundError checks if the error indicates that a volume or chunk
|
|
// has been deleted and is no longer available. These errors can be skipped
|
|
// when reading persisted log files since the data is unrecoverable.
|
|
func isChunkNotFoundError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
if errors.Is(err, util_http.ErrNotFound) || errors.Is(err, nethttp.ErrMissingFile) {
|
|
return true
|
|
}
|
|
errMsg := err.Error()
|
|
return volumeNotFoundPattern.MatchString(errMsg) ||
|
|
chunkNotFoundPattern.MatchString(errMsg) ||
|
|
httpNotFoundPattern.MatchString(errMsg)
|
|
}
|
|
|
|
// persistedLogReplayLimit caps concurrent legacy replays; decodes are shared
|
|
// through the persisted-log cache, so this only bounds the listing fan-out.
|
|
const persistedLogReplayLimit = 64
|
|
|
|
var persistedLogReplaySem = make(chan struct{}, persistedLogReplayLimit)
|
|
|
|
func (f *Filer) ReadPersistedLogBuffer(ctx context.Context, startPosition log_buffer.MessagePosition, stopTsNs int64, eachLogEntryFn log_buffer.EachLogEntryFuncType) (lastTsNs int64, isDone bool, err error) {
|
|
|
|
// Cap concurrent replays; bail if the stream is already gone so cancelled
|
|
// clients do not park on the semaphore.
|
|
if err := ctx.Err(); err != nil {
|
|
return 0, false, err
|
|
}
|
|
select {
|
|
case persistedLogReplaySem <- struct{}{}:
|
|
defer func() { <-persistedLogReplaySem }()
|
|
case <-ctx.Done():
|
|
return 0, false, ctx.Err()
|
|
}
|
|
|
|
visitor, visitErr := f.collectPersistedLogBuffer(startPosition, stopTsNs)
|
|
if visitErr != nil {
|
|
if visitErr == io.EOF {
|
|
return
|
|
}
|
|
err = fmt.Errorf("reading from persisted logs: %w", visitErr)
|
|
return
|
|
}
|
|
|
|
// Readahead: run the visitor in a background goroutine so volume server I/O
|
|
// for the next log file overlaps with event processing and gRPC delivery.
|
|
const readaheadSize = 8192
|
|
type entryOrErr struct {
|
|
entry *filer_pb.LogEntry
|
|
err error
|
|
}
|
|
ch := make(chan entryOrErr, readaheadSize)
|
|
stopReadahead := make(chan struct{})
|
|
readaheadDone := make(chan struct{})
|
|
go func() {
|
|
defer close(ch)
|
|
defer close(readaheadDone)
|
|
for {
|
|
entry, readErr := visitor.GetNext()
|
|
if readErr != nil {
|
|
if readErr != io.EOF {
|
|
select {
|
|
case ch <- entryOrErr{err: fmt.Errorf("read next from persisted logs: %w", readErr)}:
|
|
case <-stopReadahead:
|
|
}
|
|
}
|
|
return
|
|
}
|
|
select {
|
|
case ch <- entryOrErr{entry: entry}:
|
|
case <-stopReadahead:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
// Stop the readahead goroutine, wait for it to exit, then release any log
|
|
// file readers it left open (e.g. on early return or cancellation).
|
|
defer func() {
|
|
close(stopReadahead)
|
|
<-readaheadDone
|
|
visitor.Close()
|
|
}()
|
|
|
|
for item := range ch {
|
|
if item.err != nil {
|
|
err = item.err
|
|
return
|
|
}
|
|
var processErr error
|
|
isDone, processErr = eachLogEntryFn(item.entry)
|
|
if processErr != nil {
|
|
err = fmt.Errorf("process persisted log entry: %w", processErr)
|
|
return
|
|
}
|
|
lastTsNs = item.entry.TsNs
|
|
if isDone {
|
|
return
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|