Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

serve s3: apply the multipart upload limits to each user separately

Nick Craig-Woodb1657092d36a
diff --git a/cmd/serve/s3/backend.go b/cmd/serve/s3/backend.go
index 14090ed44..d2476cbe2 100644
--- a/cmd/serve/s3/backend.go
+++ b/cmd/serve/s3/backend.go
@@ -48,9 +48,6 @@ type s3Backend struct {
tenantsMu sync.Mutex
tenants map[string]*tenant // per-user state by tenant ID
- // uploads counts the in-flight uploads of all tenants.
- uploads atomic.Int64
-
// warnMaxUploadsOnce logs a single NOTICE the first time a multipart
// upload is refused by --multipart-max-uploads.
warnMaxUploadsOnce sync.Once
@@ -59,9 +56,6 @@ type s3Backend struct {
// upload falls back to being buffered in memory.
warnInMemoryOnce sync.Once
- // budget limits the memory buffered by all multipart uploads.
- budget *bufferBudget
-
reaperQuit chan struct{} // closed to stop the abandoned upload reaper
reaperStop sync.Once
}
@@ -78,6 +72,14 @@ type tenant struct {
// uploads tracks in-flight streaming multipart uploads,
// keyed by gofakes3.UploadID.
uploads sync.Map
+
+ // nUploads counts the in-flight uploads, limited by
+ // --multipart-max-uploads.
+ nUploads atomic.Int64
+
+ // budget limits the memory buffered by all the multipart uploads,
+ // set by --multipart-streaming-buffer-total.
+ budget *bufferBudget
}
// metaKey is the key of an object's metadata in s3Backend.meta.
@@ -114,7 +116,6 @@ func newBackend(s *Server) *s3Backend {
s: s,
meta: newMetadataStore(s.opt.MetadataMaxObjects),
tenants: map[string]*tenant{},
- budget: newBufferBudget(int64(s.opt.MultipartStreamingBufferTotal)),
reaperQuit: make(chan struct{}),
}
}
@@ -126,7 +127,11 @@ func (b *s3Backend) tenant(ctx context.Context) *tenant {
defer b.tenantsMu.Unlock()
t, ok := b.tenants[id]
if !ok {
- t = &tenant{b: b, id: id}
+ t = &tenant{
+ b: b,
+ id: id,
+ budget: newBufferBudget(int64(b.s.opt.MultipartStreamingBufferTotal)),
+ }
b.tenants[id] = t
}
return t
diff --git a/cmd/serve/s3/multipart.go b/cmd/serve/s3/multipart.go
index f6c4cc278..35e94433c 100644
--- a/cmd/serve/s3/multipart.go
+++ b/cmd/serve/s3/multipart.go
@@ -81,11 +81,11 @@ type multipartUpload struct {
partialMD5 []byte // MD5 state after those partialSize bytes (encoding.BinaryMarshaler)
buffered int64 // bytes of parts admitted but not yet streamed or released
bufferLimit int64 // max buffered before parts ahead of nextPart must wait (<= 0 for no limit)
- budget *bufferBudget // limits buffered across all uploads (nil for no limit)
+ budget *bufferBudget // limits buffered across all the user's uploads (nil for no limit)
}
// bufferBudget limits the memory used by the reorder buffers of all the
-// multipart uploads of a server.
+// multipart uploads of a user.
type bufferBudget struct {
mu sync.Mutex
limit int64 // <= 0 for no limit
@@ -155,7 +155,7 @@ func (up *multipartUpload) broadcast() {
}
// unbuffer returns charge bytes of buffer reserved by waitForTurn to the
-// upload's and the server's budgets. Call with up.mu held.
+// upload's and the user's budgets. Call with up.mu held.
func (up *multipartUpload) unbuffer(charge int64) {
up.buffered -= charge
up.budget.release(charge)
@@ -194,25 +194,25 @@ func (b *s3Backend) loadUpload(ctx context.Context, uploadID gofakes3.UploadID)
return v.(*multipartUpload), nil
}
-// reserveUpload reserves a place for a new upload, failing with
-// gofakes3.ErrSlowDown if --multipart-max-uploads are already in progress.
-// The place is given back by releaseUpload or deleteUpload.
-func (b *s3Backend) reserveUpload() error {
- n := b.uploads.Add(1)
+// reserveUpload reserves a place for a new upload of t, failing with
+// gofakes3.ErrSlowDown if t already has --multipart-max-uploads in
+// progress. The place is given back by releaseUpload or deleteUpload.
+func (b *s3Backend) reserveUpload(t *tenant) error {
+ n := t.nUploads.Add(1)
if maxUploads := int64(b.s.opt.MultipartMaxUploads); maxUploads > 0 && n > maxUploads {
- b.uploads.Add(-1)
+ t.nUploads.Add(-1)
b.warnMaxUploadsOnce.Do(func() {
- fs.Logf(nil, "serve s3: telling clients to slow down as --multipart-max-uploads %d multipart uploads are in progress", maxUploads)
+ fs.Logf(nil, "serve s3: telling a client to slow down as it has --multipart-max-uploads %d multipart uploads in progress", maxUploads)
})
return gofakes3.ErrSlowDown
}
return nil
}
-// releaseUpload gives back a place reserved for an upload which was never
-// recorded with addUpload.
-func (b *s3Backend) releaseUpload() {
- b.uploads.Add(-1)
+// releaseUpload gives back a place reserved for an upload of t which was
+// never recorded with addUpload.
+func (b *s3Backend) releaseUpload(t *tenant) {
+ t.nUploads.Add(-1)
}
// addUpload records a new in-flight upload of t, which must hold a place
@@ -234,7 +234,7 @@ func (b *s3Backend) addUpload(t *tenant, uploadID gofakes3.UploadID, up *multipa
func (b *s3Backend) deleteUpload(uploadID gofakes3.UploadID, up *multipartUpload) {
if _, ok := up.tenant.uploads.LoadAndDelete(uploadID); ok {
up.vfs.Shutdown()
- b.uploads.Add(-1)
+ up.tenant.nUploads.Add(-1)
}
}
@@ -262,7 +262,7 @@ func (b *s3Backend) deleteUpload(uploadID gofakes3.UploadID, up *multipartUpload
// The object the parts are written to is only created when the first part
// arrives, so an upload with no parts holds no file handle or backend
// upload, only a reference to its VFS.
-// No more than --multipart-max-uploads can be in progress: after that
+// No user can have more than --multipart-max-uploads in progress: after that
// gofakes3.ErrSlowDown is returned before anything is created on the
// remote.
func (b *s3Backend) CreateMultipartUpload(ctx context.Context, bucketName, objectName string, meta map[string]string) (gofakes3.UploadID, error) {
@@ -287,14 +287,15 @@ func (b *s3Backend) CreateMultipartUpload(ctx context.Context, bucketName, objec
}
// Reserve a place before doing anything with the remote, so a refused
// upload leaves nothing behind.
- if err := b.reserveUpload(); err != nil {
+ t := b.tenant(ctx)
+ if err := b.reserveUpload(t); err != nil {
return "", err
}
objectDir := path.Dir(fp)
if objectDir != "." {
if err := mkdirRecursive(objectDir, _vfs); err != nil {
- b.releaseUpload()
+ b.releaseUpload(t)
return "", err
}
}
@@ -312,11 +313,11 @@ func (b *s3Backend) CreateMultipartUpload(ctx context.Context, bucketName, objec
}
up := newMultipartUpload(bucketName, objectName, fp, streamFp, meta, int64(b.s.opt.MultipartStreamingBufferLimit))
- up.budget = b.budget
+ up.budget = t.budget
up.vfs = _vfs
- if err := b.addUpload(b.tenant(ctx), uploadID, up); err != nil {
- b.releaseUpload()
+ if err := b.addUpload(t, uploadID, up); err != nil {
+ b.releaseUpload(t)
return "", err
}
return uploadID, nil
@@ -500,7 +501,7 @@ const (
// The next part the stream needs is streamed straight into the sink, once
// no other request is writing to it, so it needs no buffer. A part ahead of
// it must wait until its bufferCharge fits within both the upload's reorder
-// buffer limit and the server's budget for all uploads, which it then
+// buffer limit and the user's budget for all their uploads, which it then
// reserves, bounding the memory uploads can consume when clients send parts
// faster than the backend drains them. A part too big for the limits waits
// until it is the next part. Reserved bytes are returned
diff --git a/cmd/serve/s3/multipart_test.go b/cmd/serve/s3/multipart_test.go
index 8173b2393..e671bc953 100644
--- a/cmd/serve/s3/multipart_test.go
+++ b/cmd/serve/s3/multipart_test.go
@@ -475,8 +475,9 @@ func TestMultipartBufferLimit(t *testing.T) {
// storeTestUpload records up as an in-flight upload of b, as
// CreateMultipartUpload does.
func storeTestUpload(t *testing.T, b *s3Backend, uploadID gofakes3.UploadID, up *multipartUpload) {
- require.NoError(t, b.reserveUpload())
- require.NoError(t, b.addUpload(b.tenant(context.Background()), uploadID, up))
+ tenant := b.tenant(context.Background())
+ require.NoError(t, b.reserveUpload(tenant))
+ require.NoError(t, b.addUpload(tenant, uploadID, up))
}
// stubSink is a multipartUpload sink which records how it was closed.
@@ -1696,17 +1697,21 @@ func TestLimitedBody(t *testing.T) {
}
// TestMultipartBufferTotal checks that --multipart-streaming-buffer-total
-// limits the memory buffered across all uploads, and that an aborted
-// upload's buffered parts are returned to it.
+// limits the memory buffered across all the uploads of a user but not
+// those of other users, and that an aborted upload's buffered parts are
+// returned to it.
func TestMultipartBufferTotal(t *testing.T) {
b, _, bucket := newPutTestBackend(t, "", nil)
- b.budget = newBufferBudget(2 * pool.BufferSize)
+ b.s.opt.MultipartStreamingBufferTotal = 2 * pool.BufferSize
ctx := context.Background()
- upload := func(object string, uploadID gofakes3.UploadID, partNumber int) (string, error) {
+ uploadAs := func(ctx context.Context, object string, uploadID gofakes3.UploadID, partNumber int) (string, error) {
body := []byte{byte('0' + partNumber)}
return b.UploadPart(ctx, bucket, object, uploadID, partNumber, 1, bytes.NewReader(body))
}
+ upload := func(object string, uploadID gofakes3.UploadID, partNumber int) (string, error) {
+ return uploadAs(ctx, object, uploadID, partNumber)
+ }
// Upload A buffers two out-of-order parts, using up the total.
idA, err := b.CreateMultipartUpload(ctx, bucket, "a", nil)
@@ -1716,6 +1721,14 @@ func TestMultipartBufferTotal(t *testing.T) {
require.NoError(t, err)
}
+ // Another user's out-of-order part doesn't wait.
+ ctxOther := tenantCtx("other")
+ idOther, err := b.CreateMultipartUpload(ctxOther, bucket, "other", nil)
+ require.NoError(t, err)
+ _, err = uploadAs(ctxOther, "other", idOther, 2)
+ require.NoError(t, err)
+ require.NoError(t, b.AbortMultipartUpload(ctxOther, bucket, "other", idOther))
+
// Upload B's out-of-order part must wait although its own buffer is
// empty.
idB, err := b.CreateMultipartUpload(ctx, bucket, "b", nil)
@@ -1741,9 +1754,10 @@ func TestMultipartBufferTotal(t *testing.T) {
}
require.NoError(t, b.AbortMultipartUpload(ctx, bucket, "b", idB))
- b.budget.mu.Lock()
- assert.Equal(t, int64(0), b.budget.used, "buffer total not all returned")
- b.budget.mu.Unlock()
+ budget := b.tenant(ctx).budget
+ budget.mu.Lock()
+ assert.Equal(t, int64(0), budget.used, "buffer total not all returned")
+ budget.mu.Unlock()
}
// TestMultipartSinkOpenedOnFirstPart checks that starting a multipart upload
@@ -1797,9 +1811,10 @@ func TestMultipartSinkOpenedOnFirstPart(t *testing.T) {
requireOnly(t, f, bucket, "parts.bin", "empty.bin")
}
-// TestMultipartMaxUploads checks that no more than --multipart-max-uploads
-// multipart uploads can be in progress at once, and that finished uploads
-// make room for more.
+// TestMultipartMaxUploads checks that a user can have no more than
+// --multipart-max-uploads multipart uploads in progress at once, that
+// other users can still start theirs, and that finished uploads make room
+// for more.
func TestMultipartMaxUploads(t *testing.T) {
b, _, bucket := newPutTestBackend(t, "", nil)
b.s.opt.MultipartMaxUploads = 2
@@ -1812,6 +1827,12 @@ func TestMultipartMaxUploads(t *testing.T) {
_, err = b.CreateMultipartUpload(ctx, bucket, "3", nil)
assert.True(t, gofakes3.HasErrorCode(err, gofakes3.ErrSlowDown), "want SlowDown, got %v", err)
+ // Another user isn't limited by them.
+ ctxOther := tenantCtx("other")
+ idOther, err := b.CreateMultipartUpload(ctxOther, bucket, "other", nil)
+ require.NoError(t, err)
+ require.NoError(t, b.AbortMultipartUpload(ctxOther, bucket, "other", idOther))
+
// Aborting an upload makes room.
require.NoError(t, b.AbortMultipartUpload(ctx, bucket, "1", id1))
id3, err := b.CreateMultipartUpload(ctx, bucket, "3", nil)
diff --git a/cmd/serve/s3/s3.go b/cmd/serve/s3/s3.go
index 02dd77dfd..23edf1612 100644
--- a/cmd/serve/s3/s3.go
+++ b/cmd/serve/s3/s3.go
@@ -48,11 +48,11 @@ var OptionsInfo = fs.Options{{
}, {
Name: "multipart_streaming_buffer_total",
Default: fs.SizeSuffix(1024 * 1024 * 1024),
- Help: "Maximum memory buffered by all streamed multipart uploads for parts arriving out of order, 0 for unlimited",
+ Help: "Maximum memory buffered by all the streamed multipart uploads of a user for parts arriving out of order, 0 for unlimited",
}, {
Name: "multipart_max_uploads",
Default: 1000,
- Help: "Maximum number of streamed multipart uploads in progress at once, 0 for unlimited",
+ Help: "Maximum number of streamed multipart uploads a user can have in progress at once, 0 for unlimited",
}, {
Name: "metadata_max_objects",
Default: 100000,
diff --git a/cmd/serve/s3/serve_s3.md b/cmd/serve/s3/serve_s3.md
index c659cfeb7..27ca1d43f 100644
--- a/cmd/serve/s3/serve_s3.md
+++ b/cmd/serve/s3/serve_s3.md
@@ -203,8 +203,9 @@ remote as if it had completed.
uploads concurrently or out of order are buffered until their turn.
The memory used for this buffering is capped, per upload, by
`--multipart-streaming-buffer-limit` (default `256M`, `0` for no
- limit), and across all uploads by `--multipart-streaming-buffer-total`
- (default `1G`, `0` for no limit), counting each buffered part in the
+ limit), and across all the uploads of each user (each access key) by
+ `--multipart-streaming-buffer-total` (default `1G`, `0` for no
+ limit), counting each buffered part in the
whole 1 MiB memory pages it occupies: a part that would take the
buffer over either limit is stalled until the stream drains (or, if it
is bigger than the limits, until it is the next part), so a client
@@ -335,14 +336,15 @@ do on real S3 when a lifecycle rule has aborted the upload. Set
`--multipart-expiry 0` to keep incomplete uploads forever - but note
that abandoned uploads are then never cleaned up and go on counting
towards `--multipart-max-uploads` below, so enough of them will stop
-any new multipart upload being started until the server is restarted.
+the user starting any new multipart upload until the server is
+restarted.
Each multipart upload in progress holds server resources - for example
the buffers the remote's own upload uses once the first part has
-arrived - so no more than `--multipart-max-uploads` (default `1000`,
-`0` for no limit) can be in progress at once. Starting another fails
-with a `SlowDown` error, which S3 clients retry, and a one-off `NOTICE`
-is logged.
+arrived - so each user (each access key) can have no more than
+`--multipart-max-uploads` (default `1000`, `0` for no limit) in
+progress at once. Starting another fails with a `SlowDown` error,
+which S3 clients retry, and a one-off `NOTICE` is logged.
#### Disabling streaming
@@ -374,9 +376,10 @@ make it use unbounded memory.
- **Object data.** `PutObject` bodies and multipart parts arriving in
order are streamed straight through to the remote. Parts arriving out
of order are buffered, up to `--multipart-streaming-buffer-limit`
- per upload and `--multipart-streaming-buffer-total` in all.
-- **Uploads in progress.** At most `--multipart-max-uploads` multipart
- uploads can be in progress. An upload holds little memory until its
+ per upload and `--multipart-streaming-buffer-total` per user.
+- **Uploads in progress.** Each user can have at most
+ `--multipart-max-uploads` multipart uploads in progress. An upload
+ holds little memory until its
first part arrives. After that, with `--vfs-cache-mode off`, it holds
a `--streaming-upload-cutoff` sized buffer plus whatever the remote's
own upload buffers, which for many remotes is up to their chunk size
@@ -396,14 +399,18 @@ make it use unbounded memory.
cache](#vfs-directory-cache).
The largest of these is usually the remote's upload buffers, which can
-be up to `--multipart-max-uploads` times the buffers of one upload if a
-client starts many uploads and sends part of each. To limit it, lower
+be up to `--multipart-max-uploads` times the buffers of one upload for
+each user if clients start many uploads and send part of each. The
+limits apply to each user separately, so that one user can't stop
+others uploading, which means the memory use grows with the number of
+users. To limit it, lower
`--multipart-max-uploads`, the remote's chunk size or upload
concurrency, or set `--max-buffer-memory` to limit the memory used by
the buffers of all uploads to remotes which use rclone's buffer pool
(including `s3`, `azureblob` and `b2`). Buffered multipart parts come
from the same pool, so if you set `--max-buffer-memory` make it
-comfortably bigger than `--multipart-streaming-buffer-total`, otherwise
+comfortably bigger than `--multipart-streaming-buffer-total` times the
+number of users uploading at once, otherwise
buffered parts can use up all the memory the remote needs to upload the
parts which would free them.