mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* s3api: keep a long CopyObject/UploadPartCopy alive with whitespace, as AWS does CopyObject and UploadPartCopy copy every chunk and only then write the response. On a slow volume disk (~10 MB/s), or when the source is a remote-only file of a remote-mounted bucket that is fetched first, a copy of tens of MB sends no byte for 20-30 s. Clients with a low-speed check give up while the copy finishes: the AWS SDK for C++ (pyarrow, among others) sets libcurl's CURLOPT_LOW_SPEED_LIMIT to 1 byte/s over a few seconds, and the abandoned copy leaves an orphan. AWS S3's documented behaviour for long copies: answer 200 at once, keep the connection alive with whitespace, then send <CopyObjectResult> (or an <Error> document) in the body. This does the same: - Once every check that refuses with its own status has run (auth, missing source, preconditions, invalid arguments, ACL and lock headers, the remote-cache 503), a copy still running after 1 s commits to 200, application/xml and the XML declaration, then sends one space a second, flushed, until <CopyObjectResult>/<CopyPartResult> (checksum in the body) or an <Error> with the code the handler would have answered. A copy that ends sooner answers byte for byte as before. - Armed before a remote-only source is fetched into the cluster, which is the slow part on a remote-mounted bucket. - Never kept alive: the metadata-only self-copy; a destination with versioning Enabled (x-amz-version-id is known only after the copy); a part copy whose upload or request carries SSE (its response headers are known only after the copy). - One goroutine owns the ResponseWriter from arming until halt() returns; a client that leaves ends it without touching the copy. - s3err gains StartStreamingResponse, ErrorResponseFor and EncodeXMLBody, shared with WriteErrorResponse. Accepted: a slow failure is 200 + <Error>, as on AWS, and the request is counted as 200. Tests: s3api_object_handlers_copy_keepalive_test.go (slow success and failure, fast unchanged, a refusal keeps its status, part copy, versioned destination, SSE-C part, checksum in the body, remote fill covered, client gone under -race, slow lookup then bad tag = 400, self-copy 412 under the lock, part range on a remote-only source). With the keep-alive forced off, the slow-copy tests fail. * s3api: send a block of spaces per copy keep-alive tick libcurl rounds its measured rate down, so one byte a second sits right at the AWS SDK for C++'s 1 byte/s low-speed limit and jitter can trip it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: audit a kept-alive copy error at the 200 it was sent with Once the keep-alive has sent 200, the <Error> rides in that response, so the access log records 200 and the error code, as AWS server access logs do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: fill a conditional remote-only copy source before the keep-alive A source condition read the filled entry only after the keep-alive had committed 200, so a slow fill turned its 412 (or the 400 of a malformed date) into an <Error> inside a 200. With any x-amz-copy-source-if-* header the fill runs first, as it did before the keep-alive. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: fill a remote-only self-copy that moves data under the keep-alive Only a self-copy that can stay metadata-only needs its source filled before the keep-alive; one that re-encrypts takes the data path, so its fill is the slow part the keep-alive is for. A fill changes no encryption state, so the unfilled entry decides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: test that the copy keep-alive reaches a client mid-copy A ResponseRecorder keeps every write, flushed or not; over a real connection the declaration and the spaces must arrive while the copy is still held. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: bound the copy keep-alive streaming test with a request deadline 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>
193 lines
6.6 KiB
Go
193 lines
6.6 KiB
Go
package s3api
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
|
"github.com/seaweedfs/seaweedfs/weed/util/request_id"
|
|
)
|
|
|
|
// A server-side copy moves every byte through a volume before it can answer,
|
|
// which on a slow disk takes longer than a client's low-speed timeout allows
|
|
// for a response that sends nothing. Like AWS S3, a copy still running after
|
|
// copyKeepAliveDelay commits to 200, sends the XML declaration, and then a
|
|
// space every copyKeepAliveInterval until it ends; the outcome follows as the
|
|
// result document or as an <Error> document in that same 200 response, which
|
|
// the AWS SDKs read as the error it names.
|
|
//
|
|
// The numbers answer the strictest client we serve: the AWS SDK for C++ (under
|
|
// pyarrow) sets libcurl's CURLOPT_LOW_SPEED_LIMIT to 1 byte/s over
|
|
// CURLOPT_LOW_SPEED_TIME = its request timeout, ~3 s by default. So the
|
|
// declaration (39 bytes) goes out after 1 s, well inside the first window, and
|
|
// then copyKeepAliveSpaces a second keep every later window well above 1
|
|
// byte/s, which libcurl measures rounded down. A copy that ends within 1 s
|
|
// answers exactly as it always did. Variables so tests can shorten them.
|
|
var (
|
|
copyKeepAliveDelay = 1 * time.Second
|
|
copyKeepAliveInterval = 1 * time.Second
|
|
)
|
|
|
|
var copyKeepAliveSpaces = []byte(" ")
|
|
|
|
// copyKeepAlive owns the ResponseWriter from start until halt returns: the
|
|
// handler must not touch w (headers included) in between, and afterwards
|
|
// answers only through writeSuccess or writeError.
|
|
type copyKeepAlive struct {
|
|
w http.ResponseWriter
|
|
r *http.Request
|
|
headers func(h http.Header) // headers known before the copy, sent on the slow path too
|
|
started bool // the 200 and the declaration went out; set by run, read after done
|
|
stop chan struct{}
|
|
done chan struct{}
|
|
once sync.Once
|
|
}
|
|
|
|
// startCopyKeepAlive arms the keep-alive once a copy is authorized and
|
|
// validated. Disabled, it never writes and the answer is exactly today's.
|
|
func startCopyKeepAlive(w http.ResponseWriter, r *http.Request, enabled bool, headers func(h http.Header)) *copyKeepAlive {
|
|
// One request id for the header and a late <Error>; a private copy of the
|
|
// request, because the copy paths may still set headers on theirs.
|
|
r, _ = request_id.Ensure(r)
|
|
r = r.Clone(r.Context())
|
|
k := ©KeepAlive{w: w, r: r, headers: headers, stop: make(chan struct{}), done: make(chan struct{})}
|
|
if !enabled {
|
|
close(k.done)
|
|
return k
|
|
}
|
|
go k.run()
|
|
return k
|
|
}
|
|
|
|
func (k *copyKeepAlive) run() {
|
|
defer close(k.done)
|
|
timer := time.NewTimer(copyKeepAliveDelay)
|
|
defer timer.Stop()
|
|
select {
|
|
case <-k.stop:
|
|
return
|
|
case <-k.r.Context().Done():
|
|
return
|
|
case <-timer.C:
|
|
}
|
|
if k.headers != nil {
|
|
k.headers(k.w.Header())
|
|
}
|
|
s3err.StartStreamingResponse(k.w, k.r, http.StatusOK, s3err.MimeXML)
|
|
k.started = true
|
|
if !k.write([]byte(xml.Header)) {
|
|
return
|
|
}
|
|
ticker := time.NewTicker(copyKeepAliveInterval)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-k.stop:
|
|
return
|
|
case <-k.r.Context().Done():
|
|
return
|
|
case <-ticker.C:
|
|
if !k.write(copyKeepAliveSpaces) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// copySourceVersionHeader sets x-amz-copy-source-version-id, which a copy
|
|
// knows before it moves any data.
|
|
func copySourceVersionHeader(versionId string) func(h http.Header) {
|
|
return func(h http.Header) {
|
|
if versionId != "" {
|
|
h.Set("x-amz-copy-source-version-id", versionId)
|
|
}
|
|
}
|
|
}
|
|
|
|
// partCopyAnswersSSEHeaders reports whether an UploadPartCopy answers SSE
|
|
// headers it can know only once the part is written: any SSE recorded on the
|
|
// upload (SSE-KMS, SSE-S3, or SSE-C's algorithm and key MD5), or SSE headers
|
|
// on the request, its copy-source key included.
|
|
func partCopyAnswersSSEHeaders(r *http.Request, uploadEntry *filer_pb.Entry) bool {
|
|
if uploadEntryHasSSE(uploadEntry) || IsSSECEncrypted(uploadEntry.GetExtended()) {
|
|
return true
|
|
}
|
|
return r.Header.Get(s3_constants.AmzServerSideEncryption) != "" ||
|
|
r.Header.Get(s3_constants.AmzServerSideEncryptionCustomerAlgorithm) != "" ||
|
|
r.Header.Get(s3_constants.AmzCopySourceServerSideEncryptionCustomerAlgorithm) != ""
|
|
}
|
|
|
|
// Test seams around the data-moving steps of each copy (a slow volume or
|
|
// remote fill, a failing one); production never reassigns them.
|
|
var (
|
|
copyObjectData = (*S3ApiServer).executeUnifiedCopyStrategy
|
|
copyPartData = (*S3ApiServer).copyChunksForRange
|
|
copyPartReencrypt = (*S3ApiServer).copyObjectPartViaReencryption
|
|
copyRemoteFill = (*S3ApiServer).cacheRemoteObjectForCopy
|
|
)
|
|
|
|
// write sends p and flushes it; false once the client is gone.
|
|
func (k *copyKeepAlive) write(p []byte) bool {
|
|
if _, err := k.w.Write(p); err != nil {
|
|
glog.V(1).Infof("copy keep-alive %s: %v", k.r.URL.Path, err)
|
|
return false
|
|
}
|
|
if err := http.NewResponseController(k.w).Flush(); err != nil {
|
|
glog.V(1).Infof("copy keep-alive flush %s: %v", k.r.URL.Path, err)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// halt stops the keep-alive and waits for it, so the caller owns the writer
|
|
// again; it reports whether the response has started. Safe to call twice.
|
|
func (k *copyKeepAlive) halt() bool {
|
|
k.once.Do(func() { close(k.stop) })
|
|
<-k.done
|
|
return k.started
|
|
}
|
|
|
|
// writeSuccess answers response. setHeaders runs only while the status line
|
|
// is still unsent: headers known only after the copy cannot reach a response
|
|
// already started (its body carries the ETag).
|
|
func (k *copyKeepAlive) writeSuccess(setHeaders func(), response interface{}) {
|
|
if !k.halt() {
|
|
if k.headers != nil {
|
|
k.headers(k.w.Header())
|
|
}
|
|
if setHeaders != nil {
|
|
setHeaders()
|
|
}
|
|
writeSuccessResponseXML(k.w, k.r, response)
|
|
return
|
|
}
|
|
k.write(s3err.EncodeXMLBody(response))
|
|
s3err.PostLog(k.r, http.StatusOK, s3err.ErrNone)
|
|
}
|
|
|
|
// writeError answers errorCode: with its own status while nothing is sent,
|
|
// else as the <Error> document closing the 200 already sent, which the audit
|
|
// log records as the 200 the client got, with the error code.
|
|
func (k *copyKeepAlive) writeError(errorCode s3err.ErrorCode) {
|
|
k.writeErrorHeaders(errorCode, nil)
|
|
}
|
|
|
|
// writeErrorHeaders is writeError with headers (e.g. Retry-After) that only
|
|
// an unstarted response can still carry.
|
|
func (k *copyKeepAlive) writeErrorHeaders(errorCode s3err.ErrorCode, setHeaders func(h http.Header)) {
|
|
if !k.halt() {
|
|
if setHeaders != nil {
|
|
setHeaders(k.w.Header())
|
|
}
|
|
s3err.WriteErrorResponse(k.w, k.r, errorCode)
|
|
return
|
|
}
|
|
k.write(s3err.EncodeXMLBody(s3err.ErrorResponseFor(k.r, errorCode)))
|
|
s3err.PostLog(k.r, http.StatusOK, errorCode)
|
|
}
|