fix(s3): preserve logical part layouts on multipart completion (#13184)

* fix(s3): preserve logical part layouts on multipart completion

* s3api: accept a multipart part that is entirely a hole

UploadPartCopy of a range that overlaps no source chunk stores a part
with no chunks but a non-zero size. Completion rejected any chunkless
non-last part as empty, so the upload failed with InvalidPart even
though the part copy succeeded. Reject only parts whose logical size is
zero; the all-hole part now completes as zeros at its position.

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

* s3api: refuse a multipart completion made only of holes

When every completed part is a hole, the object gets a positive size
and no chunks, and GET answers it with InternalError as a data
integrity failure. Reject that completion with InvalidPart instead of
committing an object that cannot be read. Uploads that mix hole-only
parts with data parts still complete.

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>
This commit is contained in:
authored and GitHub committed 2026-11-30 15:14:20 -08:00
1 parent 480a1baa20
commit d807dbfcbc
2 files changed
+121 -4

No files matched your search

+20 -4
View File
@@ -473,8 +473,8 @@ func (s3a *S3ApiServer) prepareMultipartCompletionState(r *http.Request, input *
stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedEtagMismatch).Inc()
continue
}
if len(entry.Chunks) == 0 && partNumber != maxPartNo {
glog.Warningf("completeMultipartUpload %s empty chunks", entry.Name)
if filer.FileSize(entry) == 0 && partNumber != maxPartNo {
glog.Warningf("completeMultipartUpload %s empty part", entry.Name)
stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedPartEmpty).Inc()
continue
}
@@ -545,15 +545,24 @@ func (s3a *S3ApiServer) prepareMultipartCompletionState(r *http.Request, input *
partStartOffset := offset
partETag := getEtagFromEntry(entry)
partSize := filer.FileSize(entry)
if partSize > uint64(math.MaxInt64-offset) {
return nil, nil, s3err.ErrEntityTooLarge
}
for _, chunk := range entry.GetChunks() {
finalChunk, chunkErr := completedMultipartChunk(chunk, offset, sses3Info)
if chunk.Offset < 0 || uint64(chunk.Offset) > partSize || chunk.Size > partSize-uint64(chunk.Offset) {
return nil, nil, s3err.ErrInternalError
}
// Keep part-local offsets for encryption metadata derivation;
// only the destination chunk's position is translated.
finalChunk, chunkErr := completedMultipartChunk(chunk, partStartOffset+chunk.Offset, sses3Info)
if chunkErr != nil {
glog.Errorf("completeMultipartUpload %s %s SSE-S3 chunk metadata error: %v", *input.Bucket, *input.UploadId, chunkErr)
return nil, nil, s3err.ErrInternalError
}
finalParts = append(finalParts, finalChunk)
offset += int64(chunk.Size)
}
offset = partStartOffset + int64(partSize)
partEndChunk := len(finalParts)
partBoundaries = append(partBoundaries, multipartPartBoundary{
@@ -568,6 +577,13 @@ func (s3a *S3ApiServer) prepareMultipartCompletionState(r *http.Request, input *
found = true
}
}
// GET refuses a non-empty object with no chunks, so a layout of holes only
// must not commit.
if len(finalParts) == 0 && offset > 0 {
glog.Warningf("completeMultipartUpload %s %s parts hold %d bytes of holes and no data", *input.Bucket, *input.UploadId, offset)
stats.S3HandlerCounter.WithLabelValues(stats.ErrorCompletedPartEmpty).Inc()
return nil, nil, s3err.ErrInvalidPart
}
// Compute the object checksum from per-part checksums if the upload was
// initiated with a checksum algorithm (stored in the upload dir entry).
@@ -0,0 +1,101 @@
package s3api
import (
"context"
"fmt"
"net/http/httptest"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
)
func TestCompleteMultipartUploadPreservesPartLogicalLayout(t *testing.T) {
const etag = "5eb63bbbe01eeed093cb22bb8f5acdc3"
for _, tc := range []struct {
name string
chunks []*filer_pb.FileChunk
}{
{"out_of_order", []*filer_pb.FileChunk{{FileId: "3,01637037d6", Offset: 4, Size: 2}, {FileId: "3,02637037d6", Size: 4}}},
{"overlap", []*filer_pb.FileChunk{{FileId: "3,01637037d6", Offset: 2, Size: 4, ModifiedTsNs: 1}, {FileId: "3,02637037d6", Size: 4, ModifiedTsNs: 2}}},
{"holes", []*filer_pb.FileChunk{{FileId: "3,01637037d6", Offset: 2, Size: 1}}},
{"all_hole", nil},
} {
t.Run(tc.name, func(t *testing.T) {
f := &memCompleteFiler{entries: map[string]map[string]*filer_pb.Entry{}}
s3a := newCompleteTestServer(t, f)
uploadDir := s3a.genUploadsFolder("b") + "/u1"
f.entries[uploadDir] = map[string]*filer_pb.Entry{}
f.put(uploadDir, &filer_pb.Entry{Name: "0001.part", Attributes: &filer_pb.FuseAttributes{FileSize: multiPartMinSize}, Chunks: tc.chunks, Extended: map[string][]byte{s3_constants.ExtETagKey: []byte(etag)}})
f.put(uploadDir, &filer_pb.Entry{Name: "0002.part", Attributes: &filer_pb.FuseAttributes{FileSize: 4}, Chunks: []*filer_pb.FileChunk{{FileId: "3,03637037d6", Offset: 1, Size: 2}}, Extended: map[string][]byte{s3_constants.ExtETagKey: []byte(etag)}})
input := &s3.CompleteMultipartUploadInput{Bucket: aws.String("b"), Key: aws.String("a.bin"), UploadId: aws.String("u1")}
parts := &CompleteMultipartUpload{Parts: []CompletedPart{{PartNumber: 1, ETag: etag}, {PartNumber: 2, ETag: etag}}}
_, code := s3a.completeMultipartUpload(httptest.NewRequest("POST", "/b/a.bin?uploadId=u1", nil), input, parts)
if code != s3err.ErrNone {
t.Fatalf("complete: %v", code)
}
got := f.entries[s3a.bucketDir("b")]["a.bin"]
if got == nil || got.GetAttributes().GetFileSize() != multiPartMinSize+4 {
t.Fatalf("wrong logical size: %v", got)
}
if len(got.Chunks) != len(tc.chunks)+1 {
t.Fatalf("wrong chunk count: %d", len(got.Chunks))
}
for i, source := range tc.chunks {
dest := got.Chunks[i]
if dest.Offset != source.Offset || dest.Size != source.Size || dest.ModifiedTsNs != source.ModifiedTsNs || dest.GetFileIdString() != source.GetFileIdString() {
t.Errorf("part 1 chunk %d changed layout: %v", i, dest)
}
}
if last := got.Chunks[len(got.Chunks)-1]; last.Offset != multiPartMinSize+1 || last.Size != 2 {
t.Errorf("part 2 rebased incorrectly: %v", last)
}
// The real read planner must select the same bytes after completion.
before, err := filer.ViewFromChunks(context.Background(), nil, tc.chunks, 0, 6)
if err != nil {
t.Fatal(err)
}
after, err := filer.ViewFromChunks(context.Background(), nil, got.Chunks, 0, 6)
if err != nil {
t.Fatal(err)
}
b, a := before.Front(), after.Front()
for b != nil && a != nil {
if b.StartOffset != a.StartOffset || b.StopOffset != a.StopOffset || b.Value.FileId != a.Value.FileId || b.Value.OffsetInChunk != a.Value.OffsetInChunk {
t.Fatalf("read view changed: before=%+v after=%+v", b, a)
}
b, a = b.Next, a.Next
}
if b != nil || a != nil {
t.Fatal("read view count changed")
}
})
}
}
func TestCompleteMultipartUploadRejectsHoleOnlyObject(t *testing.T) {
const etag = "5eb63bbbe01eeed093cb22bb8f5acdc3"
for _, sizes := range [][]uint64{{4}, {multiPartMinSize, 4}} {
f := &memCompleteFiler{entries: map[string]map[string]*filer_pb.Entry{}}
s3a := newCompleteTestServer(t, f)
uploadDir := s3a.genUploadsFolder("b") + "/u1"
f.entries[uploadDir] = map[string]*filer_pb.Entry{}
parts := &CompleteMultipartUpload{}
for i, size := range sizes {
f.put(uploadDir, &filer_pb.Entry{Name: fmt.Sprintf("%04d.part", i+1), Attributes: &filer_pb.FuseAttributes{FileSize: size}, Extended: map[string][]byte{s3_constants.ExtETagKey: []byte(etag)}})
parts.Parts = append(parts.Parts, CompletedPart{PartNumber: i + 1, ETag: etag})
}
input := &s3.CompleteMultipartUploadInput{Bucket: aws.String("b"), Key: aws.String("a.bin"), UploadId: aws.String("u1")}
_, code := s3a.completeMultipartUpload(httptest.NewRequest("POST", "/b/a.bin?uploadId=u1", nil), input, parts)
if code != s3err.ErrInvalidPart {
t.Errorf("%d hole-only parts: got %v, want InvalidPart", len(sizes), code)
}
if got := f.entries[s3a.bucketDir("b")]["a.bin"]; got != nil {
t.Errorf("%d hole-only parts committed an object GET cannot serve: %v", len(sizes), got)
}
}
}