Files
seaweedfs/weed/mount/weedfs_filer_token.go
T
394e9d247e mount: keep assignment proofs until the filer stores their chunks (#13186)
* operation: return the assignment proof of an upload

A filer serving a scoped token returns an assignment proof with
AssignVolume and stores a new chunk in an entry only when the request
presents that proof in chunk_auth. UploadWithRetry dropped it, so a
client had no way to commit the chunks it uploaded.

UploadResult.AssignAuth now carries the proof of the assignment the data
was finally stored under. A proof from an assignment the upload abandoned
after a failed attempt is never returned. The field is not serialized and
stays empty when the filer sends none, so existing callers are unchanged.

* mount: keep assignment proofs until the filer stores their chunks

A mount with a scoped filer token was refused whenever it wrote new
chunks: the filer asks for the assignment proof of every chunk the stored
entry does not hold, and the mount never kept it.

The mount now keeps the proof of every chunk it uploads and attaches the
proofs of the chunks a request names to each create, update and append
it sends, over the mutation stream and over unary calls, on every
attempt. A proof is released only once a filer acknowledged an entry that
names the chunk, or a manifest that holds it. A failed, refused or timed
out commit releases nothing, since it may have been stored: the retry
still carries the proof, and a filer ignores proofs for chunks it holds.
Proofs are stateless, so a commit retried on another filer of the cluster
is accepted there.

The store is bounded. When it is full an upload waits for commits to make
room before it is assigned a file id, and fails after a minute instead of
evicting a proof a pending commit still needs. Only proofs well past their
expiry, which a filer refuses anyway, are dropped to make room.

Without a filer token the mount keeps no store: its requests and chunk
lists are as before. A token without path grants is given no proofs, so
nothing is attached either.

* mount: keep chunk lists flat with a filer token file

A filer serving a scoped token stores no new chunk in an entry that holds
manifests, so once a mount folded a file's small chunks into a manifest,
every later write to that file was refused. A mount with a filer token now
keeps chunk lists flat; mounts without one fold as before.

* mount: test the unscoped-token path with a signed token

GenJwtForFilerScope returns an empty token when it is given no path
grants, so both cases of TestUnscopedMountRequestsCarryNoProofs ran a
tokenless mount. Sign an unscoped filer token instead and check that it
is not empty, so the test covers a mount holding a token the filer issues
no proofs for.

* mount: fold chunk lists unless the filer scopes the mount's token

A mount with -filer.tokenFile kept every chunk list flat, also when the
token is an unscoped one the filer issues no proofs for. Such a mount
stopped folding chunks into manifests, so its small-chunk runs were never
folded and, on FoundationDB, a file past about 1000 chunks no longer fit
its entry. Keep lists flat only while the current token has path grants
or a method list, which the filer scopes; fold as before otherwise.

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

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 18:18:29 -08:00

91 lines
3.3 KiB
Go

package mount
import (
"context"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/mount/meta_cache"
"github.com/seaweedfs/seaweedfs/weed/security"
)
// filerTokenPollInterval is how often an open metadata subscription checks
// its token source for a replacement. Tests shorten it.
var filerTokenPollInterval = time.Second
// subscriptionCredentials opens each metadata subscription attempt with the
// token current at that moment, and ends the attempt once the source holds a
// different one, so the follow resubscribes from its checkpoint instead of
// running on with the old token until the filer ends the stream at expiry. A
// replacement must therefore reach the file before the old token expires;
// without one, the filer's end of the stream takes the gap path as before.
//
// Cached listings survive a replacement whose grant covers the old one. When
// the new token may show less, or either cannot be decoded, they are
// distrusted as after a gap, the kernel's copies included, so no listing the
// old grant showed outlives it.
// A readdir already under way on an open handle finishes from what it read.
// Nil without a token source.
func (wfs *WFS) subscriptionCredentials() meta_cache.SubscriptionCredentials {
source := wfs.option.FilerTokenSource
if source == nil {
return nil
}
// Attempts run one at a time, on the subscription goroutine.
var previous security.EncodedJwt
return func(ctx context.Context) (security.FilerTokenSource, <-chan struct{}, error) {
token, err := source(ctx)
if err != nil {
return nil, nil, err
}
if previous != "" && !security.FilerTokenGrantCovers(previous, token) {
glog.V(0).Infof("filer token grant may have narrowed: reading cached listings again")
wfs.distrustCachedListings()
}
previous = token
refresh := make(chan struct{})
go watchFilerToken(ctx, source, token, refresh)
return func(context.Context) (security.EncodedJwt, error) { return token, nil }, refresh, nil
}
}
// watchFilerToken closes refresh once source holds a token other than token.
// A failed read leaves the open stream alone: its authority was fixed when it
// opened, and the next attempt fails closed if the file is still unreadable.
func watchFilerToken(ctx context.Context, source security.FilerTokenSource, token security.EncodedJwt, refresh chan<- struct{}) {
ticker := time.NewTicker(filerTokenPollInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if current, err := source(ctx); err == nil && current != token {
close(refresh)
return
}
}
}
}
// filerTokenExpiry is when token expires by its own claims, zero when it does
// not say or cannot be decoded.
func filerTokenExpiry(token security.EncodedJwt) time.Time {
if claims, ok := security.FilerTokenClaims(token); ok && claims.ExpiresAt != nil {
return claims.ExpiresAt.Time
}
return time.Time{}
}
// filerTokenScoped reports whether the filer scopes the mount's current
// token, as it does one with path grants or a method list. A token that
// cannot be read or decoded counts as scoped.
func (wfs *WFS) filerTokenScoped() bool {
token, err := wfs.option.FilerTokenSource(context.Background())
if err != nil {
return true
}
claims, ok := security.FilerTokenClaims(token)
return !ok || claims.IsScoped() || len(claims.AllowedMethods) > 0
}