mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* test(mount): reproduce the #5289 mount abort on a late metadata event A metadata event that arrives after metaCache.Shutdown() reaches glog.Fatalf through FatalOnError, so the mount dumps every goroutine and exits 255 instead of unmounting cleanly — the signature reported in issue #5289, still reachable on current master. The subscriber has no way to stop: its context is rooted at context.Background() and util.RetryUntil always retries, while unmount closes the cache underneath it. glog.Fatalf cannot be intercepted from another package (glog's logExitFunc is unexported), so the reproduction runs the mount in a child process and reads its exit code. * mount,pb: stop following metadata when the cache closes The mount follows metadata with FatalOnError, so an event that arrives after metaCache.Shutdown() — which unmount guarantees, since the subscriber is never stopped — was logged as a fatal and aborted the process with a goroutine dump. Report a closed cache as pb.ErrStopFollowing, a class the follow loop ends on instead of routing through the per-event error modes. It is not a failed event but the end of the subscription: no later event can land either. SubscribeMetaEvents then leaves its retry loop rather than reconnecting onto a store that no longer exists. Also export the meta_cache sentinel as ErrClosed so consumers can tell a closed cache from a storage failure. * mount: end the metadata follow before closing the meta cache The subscription goroutine had no owner: its context was rooted at context.Background() and util.RetryUntil retried forever, while unmount closed the cache underneath it. Every unmount left a window in which a filer event landed on a closed store, and the mount survived it only because the previous commit demoted that error. Give the follow a context the mount owns, and stop it — cancel, then join — before metaCache.Shutdown() in both teardown paths. MetadataFollowOption takes a ParentContext so cancelling the caller ends the stream, which is the ordering the filer already got in #11363 and #11663; the mount had no equivalent. Verified that the new assertion catches the ordering on its own: making stopMetadataFollow a no-op fails it with the subscriber still running, even with the fatal path already demoted. * mount,pb: make a cancelled follow a stop, and cover the new seams Review findings on the previous commits: Cancelling the follow context surfaced as a bare gRPC Canceled, which matched neither terminal condition, so every clean unmount logged an ERROR, ran the retry callback — which wipes every cached listing on the assumption that events were missed — and slept a second before the loop stopped itself. Test the context instead of the error text. Also assert the two new weed/pb seams where they are defined rather than only through a mount exit code: ErrStopFollowing ends the stream without reaching the per-event error modes and leaves the cursor behind the event, and a cancelled ParentContext ends the stream. Publishing the cancel handle and bumping the WaitGroup happen under one lock, so a start racing a stop cannot let the join report success too early. All four new tests were mutation-checked: dropping the early return, ignoring ParentContext, removing stopMetadataFollow from ClearCacheDir, and making stopMetadataFollow a no-op each fail their own test. --------- Co-authored-by: whitefox <whitefox@mayflower.work>
137 lines
4.8 KiB
Go
137 lines
4.8 KiB
Go
package meta_cache
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
|
)
|
|
|
|
type MetadataFollower struct {
|
|
PathPrefixToWatch string
|
|
ProcessEventFn func(resp *filer_pb.SubscribeMetadataResponse) error
|
|
}
|
|
|
|
func mergeProcessors(mainProcessor func(resp *filer_pb.SubscribeMetadataResponse) error, followers ...*MetadataFollower) func(resp *filer_pb.SubscribeMetadataResponse) error {
|
|
return func(resp *filer_pb.SubscribeMetadataResponse) error {
|
|
|
|
// build the full path
|
|
entry := resp.EventNotification.NewEntry
|
|
if entry == nil {
|
|
entry = resp.EventNotification.OldEntry
|
|
}
|
|
if entry != nil {
|
|
dir := resp.Directory
|
|
if resp.EventNotification.NewParentPath != "" {
|
|
dir = resp.EventNotification.NewParentPath
|
|
}
|
|
fp := util.NewFullPath(dir, entry.Name)
|
|
|
|
for _, follower := range followers {
|
|
if strings.HasPrefix(string(fp), follower.PathPrefixToWatch) {
|
|
if err := follower.ProcessEventFn(resp); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return mainProcessor(resp)
|
|
}
|
|
}
|
|
|
|
// SubscribeMetaEvents keeps the meta cache current by following the filer's
|
|
// metadata log until ctx is cancelled, the cache is closed, the stream ends
|
|
// cleanly, or the process exits. Cancelling ctx is how a mount quiesces the
|
|
// follow before closing the cache it writes into.
|
|
func SubscribeMetaEvents(ctx context.Context, mc *MetaCache, selfSignature int32, client filer_pb.FilerClient, lookupFn wdclient.LookupFileIdFunctionType, dir string, lastTsNs int64, skipSelfEvents bool, onRetry func(lastTsNs int64, err error), followers ...*MetadataFollower) error {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
|
|
var prefixes []string
|
|
for _, follower := range followers {
|
|
prefixes = append(prefixes, follower.PathPrefixToWatch)
|
|
}
|
|
|
|
processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error {
|
|
if skipSelfEvents && resp.EventNotification != nil {
|
|
for _, sig := range resp.EventNotification.Signatures {
|
|
if sig == selfSignature {
|
|
glog.V(4).Infof("skip self-originated event %s", resp.Directory)
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
err := mc.ApplyMetadataResponse(context.Background(), resp, SubscriberMetadataResponseApplyOptions)
|
|
if errors.Is(err, ErrClosed) {
|
|
// The cache is gone: no event can be applied to it and no retry
|
|
// helps. Report it as the end of the subscription rather than as a
|
|
// failed event, which under the mount's FatalOnError would abort a
|
|
// process that is only unmounting.
|
|
return fmt.Errorf("%w: %w", pb.ErrStopFollowing, err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
prefix := dir
|
|
if !strings.HasSuffix(prefix, "/") {
|
|
prefix = prefix + "/"
|
|
}
|
|
|
|
// Replaying the persisted log chunks here keeps the cost off the filer's heap.
|
|
// The caller's lookup says where to read them from, so a mount that cannot
|
|
// reach volume servers directly still replays through its filer.
|
|
metadataFollowOption := &pb.MetadataFollowOption{
|
|
ClientName: "mount",
|
|
ClientId: selfSignature,
|
|
ClientEpoch: 1,
|
|
SelfSignature: selfSignature,
|
|
PathPrefix: prefix,
|
|
AdditionalPathPrefixes: prefixes,
|
|
DirectoriesToWatch: nil,
|
|
StartTsNs: lastTsNs,
|
|
StopTsNs: 0,
|
|
EventErrorType: pb.FatalOnError,
|
|
ParentContext: ctx,
|
|
}
|
|
// A nil lookup asks for decoded events instead: a keyless mount cannot
|
|
// read raw metadata-log chunks with its entry-scoped volume tokens.
|
|
if lookupFn != nil {
|
|
metadataFollowOption.LogFileReaderFn = func(chunks []*filer_pb.FileChunk) (io.ReadCloser, error) {
|
|
return filer.NewChunkStreamReaderFromLookup(context.Background(), lookupFn, chunks), nil
|
|
}
|
|
}
|
|
util.RetryUntil("followMetaUpdates", func() error {
|
|
if err := ctx.Err(); err != nil {
|
|
return fmt.Errorf("%w: %w", pb.ErrStopFollowing, err)
|
|
}
|
|
metadataFollowOption.ClientEpoch++
|
|
return pb.WithFilerClientFollowMetadata(client, metadataFollowOption, mergeProcessors(processEventFn, followers...))
|
|
}, func(err error) bool {
|
|
// A cancelled context is the common case here, and it does not arrive
|
|
// wrapped: the stream's own gRPC Canceled status surfaces bare. Without
|
|
// this the stop is logged as a gap, the retry callback wipes every cached
|
|
// listing on that assumption, and the loop sleeps before its own check
|
|
// ends it — so a clean unmount would read as a metadata failure.
|
|
if ctx.Err() != nil || errors.Is(err, ErrClosed) || errors.Is(err, pb.ErrStopFollowing) {
|
|
glog.V(0).Infof("follow metadata updates stopped: %v", err)
|
|
return false
|
|
}
|
|
if onRetry != nil {
|
|
onRetry(metadataFollowOption.StartTsNs, err)
|
|
}
|
|
glog.Errorf("follow metadata updates: %v", err)
|
|
return true
|
|
})
|
|
|
|
return nil
|
|
}
|