Files
seaweedfs/weed/s3api/s3api_object_routed_upload.go
T
Chris LuandClaude Opus 5.5 df30c883fe s3api: keep a multipart upload's state on the object's owner filer (#12668)
* s3api: create a multipart upload's directory on the object's owner filer

CreateMultipartUpload wrote .uploads/<id> to the gateway's own filer while
the upload's completion commits on the object's write owner. With
independent filer stores, a part or completion reaching another gateway
before peer metadata replication caught up could not see the upload.

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

* s3api: look up a multipart upload on the object's owner filer

UploadPart, UploadPartCopy, ListParts, CompleteMultipartUpload,
AbortMultipartUpload and the bucket policy SSE lookup read .uploads/<id>
from the gateway's own filer, so a request on another gateway shortly
after CreateMultipartUpload answered NoSuchUpload until replication
caught up. Read it preferring the object's write owner, like object reads.

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

* s3api: write multipart parts to the upload's owner filer

UploadPart and UploadPartCopy stored part entries on the gateway's own
filer, so ListParts and CompleteMultipartUpload on another gateway could
miss a part that replication had not delivered yet. Store them next to the
upload directory on the object's write owner.

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

* s3api: list and clean up a multipart upload on its owner filer

ListParts, CompleteMultipartUpload and AbortMultipartUpload listed and
removed part entries on the gateway's own filer. Do it where the upload
and its parts now live, so completion sees every part and cleanup does
not miss entries replication has not delivered yet.

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

* s3api: test a multipart upload against a lagging gateway filer

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

* s3api: check the upload's owner before freeing a failed part's chunks

A part write that the owner stored but answered with an error went to
recovery with no object, so the owner was never among the filers checked.
When the owner is outside the gateway's filer list, the others' not-found
answers freed chunks the stored part still references.

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

* s3api: list an upload's parts on the prior owner after a ring change

The upload record lookup already falls back to the key's prior owner while
a ring change is in its cooling-off window, but the part listing read only
the new owner. ListParts then reported no parts for an open upload. Fall
back the same way when the new owner has nothing yet.

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

* s3api: find the upload record on the prior owner past a bare directory

A part routed to a new owner that has not received the upload yet makes
that filer create .uploads/<id> without the upload's key. The post-write
check then read it as an aborted upload and removed the part. While the
ring change is in its cooling-off window, a record on the prior owner
now wins over such a directory.

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

* s3api: merge the prior owner's parts into an upload listing

During a ring change's cooling-off window the new owner may have received
only some of an upload's parts. Listing returned the new owner's parts
alone once it had any, so ListParts dropped the rest and a completion
naming them failed with InvalidPart. Both owners' pages are now merged,
the owner's entry winning a shared name, and cut to the page limit.

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

* s3api: fail an upload listing the prior owner could not answer

When the new owner has none of an upload's parts yet and the prior
owner's listing fails, the parts may all be on the prior owner. That
listing read as empty, so CompleteMultipartUpload answered NoSuchUpload
for an upload that still exists; it now returns the error.

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

* s3api: fail an upload lookup the prior owner could not answer

During a ring change's cooling-off window an upload the new owner has
not received yet, or holds only as a bare directory, read as NoSuchUpload
when the prior owner's lookup failed. That failure is now returned, so
the client sees a retryable error instead of an upload that is gone.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 19:03:41 -08:00

142 lines
5.7 KiB
Go

package s3api
import (
"context"
"errors"
"sort"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
)
// uploadFilerClient reads and writes one multipart upload's .uploads/<id>
// directory on the destination object's write owner, the filer the upload's
// completion commits on. Gateways on independent filer stores then see an
// upload, and its parts, as soon as the owner stored them.
type uploadFilerClient struct {
s3a *S3ApiServer
bucket string
object string
}
func (s3a *S3ApiServer) uploadFiler(bucket, object string) uploadFilerClient {
return uploadFilerClient{s3a: s3a, bucket: bucket, object: object}
}
func (u uploadFilerClient) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
owner := u.s3a.routableWriteOwner(u.bucket, u.object)
if owner == "" || u.s3a.filerClient == nil {
return u.s3a.WithFilerClient(streamingMode, fn)
}
if u.s3a.ownerRecentlyUnreachable(owner) {
owner = ""
}
return u.s3a.withFilerClientFailover(context.Background(), owner, streamingMode, true, fn)
}
func (u uploadFilerClient) AdjustedUrl(location *filer_pb.Location) string {
return u.s3a.AdjustedUrl(location)
}
func (u uploadFilerClient) GetDataCenter() string {
return u.s3a.GetDataCenter()
}
// getEntry reads the upload record. Right after a ring change a part write can
// leave the new owner a bare directory before the record replicated there, so
// the prior owner's record wins over one without the upload's key, and a prior
// owner that cannot answer fails the lookup rather than reading as no upload.
func (u uploadFilerClient) getEntry(parentDirectoryPath, entryName string) (*filer_pb.Entry, error) {
entry, err := u.s3a.getObjectScopedEntryRoutedByKey(u.bucket, u.object, parentDirectoryPath, entryName)
if (err != nil && !errors.Is(err, filer_pb.ErrNotFound)) || isMultipartUploadEntry(entry) {
return entry, err
}
prior := u.s3a.priorWriteOwner(u.bucket, u.object)
if prior == "" {
return entry, err
}
priorEntry, priorErr := u.s3a.lookupEntryOnFiler(context.Background(), prior, parentDirectoryPath, entryName)
if priorErr != nil && !isFilerNotFound(priorErr) {
return nil, priorErr
}
if priorErr == nil && isMultipartUploadEntry(priorEntry) {
return priorEntry, nil
}
return entry, err
}
// list reads the owner's listing. While a ring change is in its cooling-off
// window the prior owner may still hold parts the new owner has not received,
// so its listing is merged in, the owner's entry winning a name both hold.
func (u uploadFilerClient) list(parentDirectoryPath, prefix, startFrom string, inclusive bool, limit uint32) ([]*filer_pb.Entry, bool, error) {
entries, isLast, err := u.s3a.listObjectScopedEntriesRoutedByKey(u.bucket, u.object, parentDirectoryPath, prefix, startFrom, inclusive, limit)
if err != nil && !errors.Is(err, filer_pb.ErrNotFound) {
return entries, isLast, err
}
prior := u.s3a.priorWriteOwner(u.bucket, u.object)
if prior == "" {
return entries, isLast, err
}
var priorEntries []*filer_pb.Entry
priorLast := false
priorErr := pb.WithFilerClient(true, 0, prior, u.s3a.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
return filer_pb.SeaweedList(context.Background(), client, parentDirectoryPath, prefix, func(entry *filer_pb.Entry, isLastEntry bool) error {
priorEntries = append(priorEntries, entry)
priorLast = priorLast || isLastEntry
return nil
}, startFrom, inclusive, limit)
})
if priorErr != nil && !isFilerNotFound(priorErr) && len(entries) == 0 {
// with nothing on the owner yet, every part may be on the prior owner
return nil, false, priorErr
}
if priorErr != nil || len(priorEntries) == 0 {
return entries, isLast, err
}
merged, mergedLast := mergeEntryListings(entries, isLast, priorEntries, priorLast, limit)
return merged, mergedLast, nil
}
// mergeEntryListings merges two pages of one directory listed from the same
// cursor, taking primary's entry for a name both hold. Each page holds the
// first limit names past the cursor, so the merged first limit are exact.
func mergeEntryListings(primary []*filer_pb.Entry, primaryLast bool, secondary []*filer_pb.Entry, secondaryLast bool, limit uint32) ([]*filer_pb.Entry, bool) {
byName := make(map[string]*filer_pb.Entry, len(primary)+len(secondary))
for _, entry := range secondary {
byName[entry.Name] = entry
}
for _, entry := range primary {
byName[entry.Name] = entry
}
merged := make([]*filer_pb.Entry, 0, len(byName))
for _, entry := range byName {
merged = append(merged, entry)
}
sort.Slice(merged, func(i, j int) bool { return merged[i].Name < merged[j].Name })
isLast := primaryLast && secondaryLast
if limit > 0 && uint32(len(merged)) > limit {
merged, isLast = merged[:limit], false
}
return merged, isLast
}
func (u uploadFilerClient) exists(parentDirectoryPath, entryName string, isDirectory bool) (bool, error) {
return filer_pb.Exists(context.Background(), u, parentDirectoryPath, entryName, isDirectory)
}
func (u uploadFilerClient) mkdir(parentDirectoryPath, dirName string, fn func(entry *filer_pb.Entry)) error {
return filer_pb.Mkdir(context.Background(), u, parentDirectoryPath, dirName, fn)
}
func (u uploadFilerClient) mkFile(parentDirectoryPath, fileName string, chunks []*filer_pb.FileChunk, fn func(entry *filer_pb.Entry)) error {
return filer_pb.MkFile(context.Background(), u, parentDirectoryPath, fileName, chunks, fn)
}
func (u uploadFilerClient) rm(ctx context.Context, parentDirectoryPath, entryName string, isDeleteData, isRecursive bool) error {
return retryFilerOp(ctx, "rm "+parentDirectoryPath+"/"+entryName, func() error {
return u.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
return doDeleteEntry(ctx, client, parentDirectoryPath, entryName, isDeleteData, isRecursive)
})
})
}