Files
seaweedfs/weed/s3api/s3api_object_handlers_copy_cleanup.go
T
9f9c28471f s3api: free the parts a multipart completion does not name (#13248)
* s3api: free the parts a multipart completion does not name

prepareMultipartCompletionState skipped every entry of the upload
directory whose part number the request does not name: it was neither a
part of the object nor one of the unused entries freed with their data.
The completion then removes the upload directory metadata-only, since
the object's chunks are its parts' chunks, so the skipped parts' chunks
stayed on the volume servers with nothing referencing them until a
volume.fsck purge. Once the upload is completed they cannot be listed,
completed or aborted any more.

The completion now keeps those entries aside and, once its object is
committed and the upload directory is gone, deletes their chunks by
file id, resolving manifest chunks first. They are not freed before the
commit, as the unused attempts are on the routed paths: until a
completion succeeds the upload stays open with all its parts, and the
commit transaction can still refuse the write. When another completion
of the upload owns the commit (metadataOnlyCleanup), its object can hold
them, and they are kept.

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

* s3api: give the unrequested-part cleanup one deadline and check the committed object first

The client waits for the completion it already committed, so the cleanup
of the parts the request did not name gets one deadline,
unrequestedPartsCleanupTimeout, instead of a lookup timeout for each part
with manifests: the chunks of the parts without manifests are deleted
first, then manifests are resolved part by part while time is left.
deleteOrphanedChunksWithContext passes the deadline to the volume lookups
and deletes; deleteOrphanedChunks keeps its behaviour.

A completion without a ring owner commits under the object lock without
the upload-directory precondition, so a routed completion of the same
upload can commit after it with a list that names those parts, and
remove the directory. The cleanup now frees them only when the key's
current object is this completion's commit (this upload, the same
chunks), checked once the directory is gone, when no routed completion
can commit any more.

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

* s3api: deleteOrphanedChunks takes the context its lookups and deletes use

Fold deleteOrphanedChunksWithContext into deleteOrphanedChunks instead of
keeping a second name; the existing callers pass context.Background().

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

* s3api: check the entry the completion wrote, under the cleanup deadline

The check before freeing the unrequested parts resolved the object by
its key, outside the cleanup deadline. A key ending in "/" is written
inside the directory it names ("dir/" at dir/dir), so the check read
another entry and kept the parts, and a filer that stopped answering
after the commit could hold the completion without a limit. The
completion now records where it writes its object (the version file on a
versioned bucket), and the cleanup reads that entry back, from the write
owner when there is one, under the same deadline as the deletes.

Each part whose manifests resolve is deleted before the next manifest is
read, so a read that runs out the deadline no longer strands the parts
resolved before it.

The cleanup's filer reads, that check and deleteOrphanedChunks' volume
lookup, go through withFilerClient with the deadline, so the deadline
running out is not booked as a failure of the filer and does not open
its circuit for other requests.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-12-02 03:18:04 -08:00

156 lines
4.6 KiB
Go

package s3api
import (
"context"
"fmt"
"sync/atomic"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// chunkCopyConcurrency bounds the chunk copies one request runs at once.
const chunkCopyConcurrency = 4
// copyChunksConcurrently runs copyOne for chunk indexes 0..n-1, at most
// chunkCopyConcurrency at a time, and returns the copied chunks in index order.
//
// Every copy that started is waited for before it returns: a copy left running
// would write a needle after the request is over, with nothing to ever reference
// it. When a copy fails, no further copy is started, and the chunks the others
// wrote are deleted, since the failed request leaves no entry that holds them.
func (s3a *S3ApiServer) copyChunksConcurrently(n int, copyOne func(i int) (*filer_pb.FileChunk, error)) ([]*filer_pb.FileChunk, error) {
dstChunks := make([]*filer_pb.FileChunk, n)
executor := util.NewLimitedConcurrentExecutor(chunkCopyConcurrency)
errChan := make(chan error, n)
var failed atomic.Bool
started := 0
for i := 0; i < n && !failed.Load(); i++ {
chunkIndex := i
started++
executor.Execute(func() {
if failed.Load() {
errChan <- nil
return
}
dstChunk, err := copyOne(chunkIndex)
if err != nil {
failed.Store(true)
errChan <- fmt.Errorf("chunk %d: %w", chunkIndex, err)
return
}
dstChunks[chunkIndex] = dstChunk
errChan <- nil
})
}
// Collect every started copy, not just up to the first error; the first
// error reported is the one returned.
var firstErr error
for i := 0; i < started; i++ {
if err := <-errChan; err != nil && firstErr == nil {
firstErr = err
}
}
if firstErr != nil {
s3a.deleteCopiedChunks(dstChunks)
return nil, firstErr
}
return dstChunks, nil
}
// deleteCopiedChunks removes chunks a copy wrote for an entry that was never
// written. It is best-effort: a failed delete is logged and leaves the needles
// for volume.fsck, and never changes the error the client is answered with.
func (s3a *S3ApiServer) deleteCopiedChunks(chunks []*filer_pb.FileChunk) {
written := make([]*filer_pb.FileChunk, 0, len(chunks))
for _, chunk := range chunks {
if chunk != nil {
written = append(written, chunk)
}
}
if len(written) == 0 {
return
}
glog.V(1).Infof("copy failed, deleting %d chunks it wrote", len(written))
s3a.deleteOrphanedChunks(context.Background(), written)
}
// chunkCopyRollback collects the chunks a sequential, multi-step copy writes
// and deletes them if the copy ends without commit.
type chunkCopyRollback struct {
s3a *S3ApiServer
chunks []*filer_pb.FileChunk
committed bool
}
func (s3a *S3ApiServer) newChunkCopyRollback() *chunkCopyRollback {
return &chunkCopyRollback{s3a: s3a}
}
func (c *chunkCopyRollback) add(chunk *filer_pb.FileChunk) {
c.chunks = append(c.chunks, chunk)
}
// commit keeps the chunks: the copy succeeded and the caller owns them now.
func (c *chunkCopyRollback) commit() { c.committed = true }
// release is deferred by the copy; it deletes the chunks unless commit ran.
func (c *chunkCopyRollback) release() {
if !c.committed {
c.s3a.deleteCopiedChunks(c.chunks)
}
}
// chunkFidKey identifies a chunk's needle independent of how its file id is
// spelled.
func chunkFidKey(c *filer_pb.FileChunk) string {
fid := c.GetFid()
if fid == nil {
fid, _ = filer_pb.ToFileIdObject(c.GetFileIdString())
}
if fid == nil {
return c.GetFileIdString()
}
return fmt.Sprintf("%d,%x,%x", fid.VolumeId, fid.FileKey, fid.Cookie)
}
// copyOwnedChunks returns the chunks a failed copy may delete: the chunks it
// wrote (dstChunks) and the manifest chunks folded over them (entryChunks),
// minus any needle the source entry also holds. A copy that reuses the source's
// chunks (SSE-KMS key rotation onto the same key) writes none of its own for
// them, so those must stay.
func copyOwnedChunks(srcChunks, dstChunks, entryChunks []*filer_pb.FileChunk) []*filer_pb.FileChunk {
shared := make(map[string]struct{}, len(srcChunks))
for _, chunk := range srcChunks {
shared[chunkFidKey(chunk)] = struct{}{}
}
var owned []*filer_pb.FileChunk
seen := make(map[string]struct{}, len(dstChunks))
take := func(chunk *filer_pb.FileChunk) {
if chunk == nil {
return
}
key := chunkFidKey(chunk)
if _, isShared := shared[key]; isShared {
return
}
if _, dup := seen[key]; dup {
return
}
seen[key] = struct{}{}
owned = append(owned, chunk)
}
for _, chunk := range dstChunks {
take(chunk)
}
for _, chunk := range entryChunks {
if chunk.GetIsChunkManifest() {
take(chunk)
}
}
return owned
}