diff --git a/weed/s3api/filer_multipart.go b/weed/s3api/filer_multipart.go index e25b6a3cd6..f3138d5eef 100644 --- a/weed/s3api/filer_multipart.go +++ b/weed/s3api/filer_multipart.go @@ -976,6 +976,9 @@ func (s3a *S3ApiServer) completeMultipartUpload(r *http.Request, input *s3.Compl if errors.Is(err, errObjectPrecondition) { return s3err.ErrPreconditionFailed } + if errors.Is(err, filer_pb.ErrEntryNameTooLong) { + return s3err.ErrKeyTooLongError + } // The transaction may have committed the object before failing on // the upload removal; a surviving entry references the manifests. if exists, err := s3a.exists(dirName, entryName, false); err != nil || exists { @@ -1074,6 +1077,9 @@ func (s3a *S3ApiServer) completeMultipartUpload(r *http.Request, input *s3.Compl if errors.Is(err, errObjectPrecondition) { return s3err.ErrPreconditionFailed } + if errors.Is(err, filer_pb.ErrEntryNameTooLong) { + return s3err.ErrKeyTooLongError + } // The transaction may have committed the object before failing on // the upload removal; a surviving entry references the manifests. if exists, err := s3a.exists(dirName, entryName, false); err != nil || exists { diff --git a/weed/s3api/s3api_object_handlers_put.go b/weed/s3api/s3api_object_handlers_put.go index 86a6578dac..ce15744bf8 100644 --- a/weed/s3api/s3api_object_handlers_put.go +++ b/weed/s3api/s3api_object_handlers_put.go @@ -1268,6 +1268,9 @@ func (s3a *S3ApiServer) putToFiler(r *http.Request, filePath string, dataReader glog.Warningf("putToFiler: routed PUT to %s failed for %s, falling back to lock: %v", owner, filePath, err) case resp.ErrorCode == filer_pb.FilerError_PRECONDITION_FAILED: createCode, routed = s3err.ErrPreconditionFailed, true + case resp.ErrorCode == filer_pb.FilerError_ENTRY_NAME_TOO_LONG && resp.FailedMutation != nil && *resp.FailedMutation == 0: + // The filer refuses the name before the first mutation stores anything. + createCode, routed = s3err.ErrKeyTooLongError, true case resp.Error != "": // A mutation can store the entry before a later step of the // same mutation fails, so even an error from the first mutation diff --git a/weed/s3api/s3api_object_handlers_put_ambiguous_test.go b/weed/s3api/s3api_object_handlers_put_ambiguous_test.go index 240d42a4d0..a972405878 100644 --- a/weed/s3api/s3api_object_handlers_put_ambiguous_test.go +++ b/weed/s3api/s3api_object_handlers_put_ambiguous_test.go @@ -222,6 +222,8 @@ type ambiguousPutFiler struct { supersede *filer_pb.Entry txnErr error txnNoApply bool + // txnResp, when set, is the transaction's answer and nothing is applied. + txnResp *filer_pb.ObjectTransactionResponse // beforeCreate runs inside CreateEntry's lock, before the condition is // evaluated, letting a test land a racing write at the worst moment. // beforeLookup runs inside LookupDirectoryEntry, letting a test delete a @@ -339,6 +341,9 @@ func (f *ambiguousPutFiler) ObjectTransaction(_ context.Context, req *filer_pb.O if f.txnCheck != nil { f.txnCheck(req) } + if f.txnResp != nil { + return f.txnResp, nil + } key := req.LockKey if req.ConditionKey != "" { key = req.ConditionKey @@ -1122,6 +1127,42 @@ func TestPutToFilerRoutedAmbiguousEmptyPutForeignWriteId(t *testing.T) { } } +// A routed PUT whose name the filer refuses stored nothing, so it answers +// KeyTooLongError whatever the body's size, and the uploaded chunks are freed. +func TestPutToFilerRoutedNameTooLong(t *testing.T) { + for _, body := range []string{"", "hello world"} { + t.Run(fmt.Sprintf("%d-byte body", len(body)), func(t *testing.T) { + volume := startFakeVolumeServer(t) + firstMutation := int32(0) + filerImpl := &ambiguousPutFiler{ + volume: volume, + entries: map[string]*filer_pb.Entry{}, + txnResp: &filer_pb.ObjectTransactionResponse{ + Error: "mutation 0: entry name too long", + ErrorCode: filer_pb.FilerError_ENTRY_NAME_TOO_LONG, + FailedMutation: &firstMutation, + }, + } + filerAddr := startFakeFiler(t, filerImpl) + s3a := newPutTestServer(t, filerAddr) + s3a.objectWriteLockClient = cluster.NewLockClient(s3a.option.GrpcDialOption, filerAddr) + s3a.objectWriteLockClient.SetRing([]pb.ServerAddress{filerAddr}, 1) + + r := httptest.NewRequest(http.MethodPut, "/b/o", nil) + _, code, _ := s3a.putToFiler(r, "/buckets/b/o", strings.NewReader(body), "b", "o", 1, 0, nil, false, "") + if code != s3err.ErrKeyTooLongError { + t.Fatalf("putToFiler returned %v, want KeyTooLongError", code) + } + if filerImpl.txnCount != 1 || filerImpl.stores != 0 { + t.Fatalf("refused PUT was retried: %d transactions, %d creates", filerImpl.txnCount, filerImpl.stores) + } + if body != "" && len(volume.deleted()) == 0 { + t.Fatal("the refused PUT's chunks were not deleted") + } + }) + } +} + // A volume-encrypted chunk must be decrypted before SSE decryption sees it: // fetchChunkData feeds ciphered chunks through the cipher-aware read path and // slices plaintext space, while plain chunks keep streaming range reads. diff --git a/weed/s3api/s3api_object_routed_write.go b/weed/s3api/s3api_object_routed_write.go index 7bbd7f4c23..1d99d52821 100644 --- a/weed/s3api/s3api_object_routed_write.go +++ b/weed/s3api/s3api_object_routed_write.go @@ -282,6 +282,9 @@ func (s3a *S3ApiServer) routedMkFile(owner pb.ServerAddress, routeKey, parentDir return 0, errUploadRemoved } if resp.Error != "" { + if sentinel := filer_pb.FilerErrorToSentinel(resp.ErrorCode); sentinel != nil { + return 0, fmt.Errorf("routed mkfile %s/%s: %w", parentDir, name, sentinel) + } return 0, fmt.Errorf("routed mkfile %s/%s: %s", parentDir, name, resp.Error) } return resp.LogTsNs, nil diff --git a/weed/s3api/s3api_object_routed_write_test.go b/weed/s3api/s3api_object_routed_write_test.go index 1fe23e5953..e32a30c0ff 100644 --- a/weed/s3api/s3api_object_routed_write_test.go +++ b/weed/s3api/s3api_object_routed_write_test.go @@ -2,6 +2,7 @@ package s3api import ( "context" + "errors" "fmt" "net" "net/http" @@ -657,6 +658,32 @@ func TestRoutedMultipartFinalize(t *testing.T) { } } +// A routed mkfile the filer refuses keeps the refusal's sentinel, so callers +// map it as they map the same refusal from CreateEntry. +func TestRoutedMkFileKeepsFilerErrorCode(t *testing.T) { + firstMutation := int32(0) + fake := &fakeTxnFiler{resp: &filer_pb.ObjectTransactionResponse{ + Error: "mutation 0: entry name too long", + ErrorCode: filer_pb.FilerError_ENTRY_NAME_TOO_LONG, + FailedMutation: &firstMutation, + }} + owner := startFakeFiler(t, fake) + s3a := &S3ApiServer{ + option: &S3ApiServerOption{ + BucketsPath: "/buckets", + GrpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()), + }, + } + + _, err := s3a.writeMultipartObject(owner, "s3.object.write:/buckets/b/o", "/buckets/b", "o", nil, nil, nil, nil, "") + if !errors.Is(err, filer_pb.ErrEntryNameTooLong) { + t.Fatalf("writeMultipartObject returned %v, want ErrEntryNameTooLong", err) + } + if code := filerErrorToS3Error(err); code != s3err.ErrKeyTooLongError { + t.Fatalf("filerErrorToS3Error(%v) = %v, want KeyTooLongError", err, code) + } +} + // A non-versioned multipart completion removes .uploads/ metadata-only in // the same transaction as the object's PUT. func TestWriteMultipartObjectRemovesUploadDir(t *testing.T) {