mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
s3api: keep the in-flight upload counters 64-bit aligned on 32-bit (#13321)
The recursiveListDeclined sync.Map placed ahead of inFlightDataSize and inFlightUploads moved them off 8-byte alignment on 386, so the first throttled upload panicked with "unaligned 64-bit atomic operation". atomic.Int64 is aligned wherever it sits in the struct. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
006885232e
commit
2f300c2969
3 files changed
+18
-18
No files matched your search
@@ -117,8 +117,8 @@ func (cb *CircuitBreaker) Limit(f func(w http.ResponseWriter, r *http.Request),
|
||||
cb.s3a.inFlightDataLimitCond.L.Lock()
|
||||
defer cb.s3a.inFlightDataLimitCond.L.Unlock()
|
||||
// Decrement counters
|
||||
newUploads := atomic.AddInt64(&cb.s3a.inFlightUploads, -1)
|
||||
newSize := atomic.AddInt64(&cb.s3a.inFlightDataSize, -charged)
|
||||
newUploads := cb.s3a.inFlightUploads.Add(-1)
|
||||
newSize := cb.s3a.inFlightDataSize.Add(-charged)
|
||||
// Update metrics
|
||||
stats.S3InFlightUploadCountGauge.Set(float64(newUploads))
|
||||
stats.S3InFlightUploadBytesGauge.Set(float64(newSize))
|
||||
@@ -195,12 +195,12 @@ func (cb *CircuitBreaker) waitForUploadSlot(w http.ResponseWriter, r *http.Reque
|
||||
s3a := cb.s3a
|
||||
s3a.inFlightDataLimitCond.L.Lock()
|
||||
err := util.WaitUntil(r.Context(), s3a.inFlightDataLimitCond, s3a.option.InflightUploadDataTimeout, func() bool {
|
||||
inFlightDataSize := atomic.LoadInt64(&s3a.inFlightDataSize)
|
||||
inFlightDataSize := s3a.inFlightDataSize.Load()
|
||||
if !util.UploadAdmits(inFlightDataSize, charged, s3a.option.ConcurrentUploadLimit) {
|
||||
glog.V(4).Infof("wait because inflight data %d + %d > %d", inFlightDataSize, charged, s3a.option.ConcurrentUploadLimit)
|
||||
return false
|
||||
}
|
||||
inFlightUploads := atomic.LoadInt64(&s3a.inFlightUploads)
|
||||
inFlightUploads := s3a.inFlightUploads.Load()
|
||||
if s3a.option.ConcurrentFileUploadLimit != 0 && inFlightUploads >= s3a.option.ConcurrentFileUploadLimit {
|
||||
glog.V(4).Infof("wait because inflight uploads %d >= %d", inFlightUploads, s3a.option.ConcurrentFileUploadLimit)
|
||||
return false
|
||||
@@ -209,8 +209,8 @@ func (cb *CircuitBreaker) waitForUploadSlot(w http.ResponseWriter, r *http.Reque
|
||||
})
|
||||
if err == nil {
|
||||
// Increment counters
|
||||
newUploads := atomic.AddInt64(&s3a.inFlightUploads, 1)
|
||||
newSize := atomic.AddInt64(&s3a.inFlightDataSize, charged)
|
||||
newUploads := s3a.inFlightUploads.Add(1)
|
||||
newSize := s3a.inFlightDataSize.Add(charged)
|
||||
// Update metrics
|
||||
stats.S3InFlightUploadCountGauge.Set(float64(newUploads))
|
||||
stats.S3InFlightUploadBytesGauge.Set(float64(newSize))
|
||||
|
||||
@@ -284,7 +284,7 @@ func TestLimitUploadSlotWait(t *testing.T) {
|
||||
if elapsed := time.Since(start); elapsed < 100*time.Millisecond {
|
||||
t.Fatalf("rejected after %v, before the wait timeout", elapsed)
|
||||
}
|
||||
if n := atomic.LoadInt64(&cb.s3a.inFlightUploads); n != 1 {
|
||||
if n := cb.s3a.inFlightUploads.Load(); n != 1 {
|
||||
t.Fatalf("in-flight uploads %d, want 1", n)
|
||||
}
|
||||
})
|
||||
@@ -320,7 +320,7 @@ func TestLimitUploadSlotWait(t *testing.T) {
|
||||
release := holdUploadSlot(t, cb, 1000)
|
||||
releaseOther := holdUploadSlot(t, cb, 1000)
|
||||
defer releaseOther()
|
||||
if got := atomic.LoadInt64(&cb.s3a.inFlightDataSize); got != 100 {
|
||||
if got := cb.s3a.inFlightDataSize.Load(); got != 100 {
|
||||
t.Fatalf("in-flight %d, want the budget full at 100", got)
|
||||
}
|
||||
|
||||
@@ -398,7 +398,7 @@ func TestLimitUploadSlotRejectsStalledBody(t *testing.T) {
|
||||
func putThroughLimit(cb *CircuitBreaker, req *http.Request) (code int, body string, inFlight int64) {
|
||||
inFlight = -1
|
||||
h, _ := cb.Limit(func(w http.ResponseWriter, r *http.Request) {
|
||||
inFlight = atomic.LoadInt64(&cb.s3a.inFlightDataSize)
|
||||
inFlight = cb.s3a.inFlightDataSize.Load()
|
||||
}, s3_constants.ACTION_WRITE)
|
||||
rec := httptest.NewRecorder()
|
||||
h(rec, req)
|
||||
@@ -407,12 +407,12 @@ func putThroughLimit(cb *CircuitBreaker, req *http.Request) (code int, body stri
|
||||
|
||||
func TestLimitUploadSlotNoOvershoot(t *testing.T) {
|
||||
cb := newUploadLimitedCircuitBreaker(&S3ApiServerOption{ConcurrentUploadLimit: 10, InflightUploadDataTimeout: 50 * time.Millisecond})
|
||||
cb.s3a.inFlightDataSize = 8
|
||||
cb.s3a.inFlightDataSize.Store(8)
|
||||
code, body, _ := putThroughLimit(cb, putRequest(context.Background(), 4))
|
||||
if code != http.StatusServiceUnavailable || !strings.Contains(body, "<Code>SlowDown</Code>") {
|
||||
t.Fatalf("got %d %q, want 503 SlowDown", code, body)
|
||||
}
|
||||
if n := atomic.LoadInt64(&cb.s3a.inFlightDataSize); n != 8 {
|
||||
if n := cb.s3a.inFlightDataSize.Load(); n != 8 {
|
||||
t.Fatalf("in-flight bytes %d, want 8", n)
|
||||
}
|
||||
}
|
||||
@@ -421,7 +421,7 @@ func TestLimitUploadSlotChargesUnknownLength(t *testing.T) {
|
||||
const limit = 1 << 20
|
||||
newCB := func(inFlight int64) *CircuitBreaker {
|
||||
cb := newUploadLimitedCircuitBreaker(&S3ApiServerOption{ConcurrentUploadLimit: limit, MaxMB: 4, InflightUploadDataTimeout: 50 * time.Millisecond})
|
||||
cb.s3a.inFlightDataSize = inFlight
|
||||
cb.s3a.inFlightDataSize.Store(inFlight)
|
||||
return cb
|
||||
}
|
||||
unknown := func() *http.Request {
|
||||
@@ -440,7 +440,7 @@ func TestLimitUploadSlotChargesUnknownLength(t *testing.T) {
|
||||
if code != http.StatusOK || inFlight != limit/2 {
|
||||
t.Fatalf("idle: got %d in-flight %d, want 200 and %d", code, inFlight, limit/2)
|
||||
}
|
||||
if n := atomic.LoadInt64(&cb.s3a.inFlightDataSize); n != 0 {
|
||||
if n := cb.s3a.inFlightDataSize.Load(); n != 0 {
|
||||
t.Fatalf("in-flight bytes %d after release, want 0", n)
|
||||
}
|
||||
}
|
||||
@@ -474,7 +474,7 @@ func TestLimitUploadSlotUsesDecodedLengthOnlyWhenUnknown(t *testing.T) {
|
||||
|
||||
func TestLimitUploadSlotAdmitsHugeObjectBesideSmallOnes(t *testing.T) {
|
||||
cb := newUploadLimitedCircuitBreaker(&S3ApiServerOption{ConcurrentUploadLimit: 64 << 20, MaxMB: 4, InflightUploadDataTimeout: 50 * time.Millisecond})
|
||||
cb.s3a.inFlightDataSize = 32 << 20
|
||||
cb.s3a.inFlightDataSize.Store(32 << 20)
|
||||
req := putRequest(context.Background(), 1)
|
||||
req.ContentLength = 5 << 30
|
||||
code, _, inFlight := putThroughLimit(cb, req)
|
||||
@@ -506,7 +506,7 @@ func TestLimitBucketBytesCounterSymmetricForUnknownLength(t *testing.T) {
|
||||
var during, budget int64
|
||||
h, _ := cb.Limit(func(w http.ResponseWriter, r *http.Request) {
|
||||
during = atomic.LoadInt64(cb.counters[key])
|
||||
budget = atomic.LoadInt64(&cb.s3a.inFlightDataSize)
|
||||
budget = cb.s3a.inFlightDataSize.Load()
|
||||
}, s3_constants.ACTION_WRITE)
|
||||
h(httptest.NewRecorder(), req)
|
||||
if during != tc.want {
|
||||
@@ -545,7 +545,7 @@ func TestLimitBucketBytesRejectsDeclaredOversizedPut(t *testing.T) {
|
||||
if n := atomic.LoadInt64(cb.counters[key]); n != 0 {
|
||||
t.Fatalf("bucket counter after rejection %d, want 0", n)
|
||||
}
|
||||
if n := atomic.LoadInt64(&cb.s3a.inFlightDataSize); n != 0 {
|
||||
if n := cb.s3a.inFlightDataSize.Load(); n != 0 {
|
||||
t.Fatalf("upload budget after rejection %d, want 0", n)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,8 +134,8 @@ type S3ApiServer struct {
|
||||
credentialManager *credential.CredentialManager
|
||||
bucketConfigCache *BucketConfigCache
|
||||
policyEngine *BucketPolicyEngine // Engine for evaluating bucket policies
|
||||
inFlightDataSize int64
|
||||
inFlightUploads int64
|
||||
inFlightDataSize atomic.Int64
|
||||
inFlightUploads atomic.Int64
|
||||
inFlightDataLimitCond *sync.Cond
|
||||
draining atomic.Bool
|
||||
embeddedIam *EmbeddedIamApi // Embedded IAM API server (when enabled)
|
||||
|
||||
Reference in new issue
Block a user