From 1a2be918bfe2737bfc1876d77ae0aaa53b27c3a5 Mon Sep 17 00:00:00 2001 From: Tiago Posse Date: Mon, 30 Nov 2026 22:14:34 +0100 Subject: [PATCH] s3api: reset the circuit breaker when its config is deleted (#13148) * s3api: reset the circuit breaker when its config is deleted The delete handler parsed an empty file, which always fails, so the old limits stayed in place. It now loads an empty config. Co-Authored-By: Claude Opus 5.5 * s3api: read and swap the circuit breaker limits under its lock A config change or delete replaced Enabled and the limitations map while requests read them without a lock. The swap now holds the breaker's write lock, and requests read both under the read lock that loadCounterAndCompare already took for the counters. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Chris Lu --- weed/s3api/auth_credentials_subscribe.go | 7 +--- weed/s3api/s3api_circuit_breaker.go | 19 ++++++---- weed/s3api/s3api_circuit_breaker_test.go | 48 ++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/weed/s3api/auth_credentials_subscribe.go b/weed/s3api/auth_credentials_subscribe.go index 9bc332ae3e..ac68b24866 100644 --- a/weed/s3api/auth_credentials_subscribe.go +++ b/weed/s3api/auth_credentials_subscribe.go @@ -8,6 +8,7 @@ import ( "github.com/seaweedfs/seaweedfs/weed/glog" "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/pb/s3_pb" "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" "github.com/seaweedfs/seaweedfs/weed/util" ) @@ -185,11 +186,7 @@ func (s3a *S3ApiServer) onCircuitBreakerConfigChange(dir string, oldEntry *filer // Handle deletion: reset to empty config if newEntry == nil && oldEntry != nil && oldEntry.Name == s3_constants.CircuitBreakerConfigFile { glog.V(1).Infof("Circuit breaker config file deleted, resetting to defaults") - if err := s3a.cb.LoadS3ApiConfigurationFromBytes([]byte{}); err != nil { - glog.Warningf("failed to reset circuit breaker config on deletion: %v", err) - return err - } - return nil + return s3a.cb.loadCircuitBreakerConfig(&s3_pb.S3CircuitBreakerConfig{}) } // Handle create/update diff --git a/weed/s3api/s3api_circuit_breaker.go b/weed/s3api/s3api_circuit_breaker.go index 633e269acc..6a64614e44 100644 --- a/weed/s3api/s3api_circuit_breaker.go +++ b/weed/s3api/s3api_circuit_breaker.go @@ -86,7 +86,6 @@ func (cb *CircuitBreaker) loadCircuitBreakerConfig(cfg *s3_pb.S3CircuitBreakerCo limitations[action] = limit } } - cb.Enabled = globalEnabled //buckets for bucket, cbOptions := range cfg.Buckets { @@ -97,7 +96,10 @@ func (cb *CircuitBreaker) loadCircuitBreakerConfig(cfg *s3_pb.S3CircuitBreakerCo } } + cb.Lock() + cb.Enabled = globalEnabled cb.limitations = limitations + cb.Unlock() return nil } @@ -125,7 +127,10 @@ func (cb *CircuitBreaker) Limit(f func(w http.ResponseWriter, r *http.Request), } // Apply circuit breaker logic - if !cb.Enabled { + cb.RLock() + enabled := cb.Enabled + cb.RUnlock() + if !enabled { f(w, r) return } @@ -284,11 +289,11 @@ func (cb *CircuitBreaker) limit(r *http.Request, bucket string, action string) ( func (cb *CircuitBreaker) loadCounterAndCompare(key string, inc int64, errCode s3err.ErrorCode) (f func(), e s3err.ErrorCode) { e = s3err.ErrNone - if max, ok := cb.limitations[key]; ok { - cb.RLock() - counter, exists := cb.counters[key] - cb.RUnlock() - + cb.RLock() + max, ok := cb.limitations[key] + counter, exists := cb.counters[key] + cb.RUnlock() + if ok { if !exists { cb.Lock() counter, exists = cb.counters[key] diff --git a/weed/s3api/s3api_circuit_breaker_test.go b/weed/s3api/s3api_circuit_breaker_test.go index 5814c542ae..a528b4fbfd 100644 --- a/weed/s3api/s3api_circuit_breaker_test.go +++ b/weed/s3api/s3api_circuit_breaker_test.go @@ -14,6 +14,7 @@ import ( "time" "github.com/gorilla/mux" + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/pb/s3_pb" "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" "github.com/seaweedfs/seaweedfs/weed/s3api/s3err" @@ -548,3 +549,50 @@ func TestLimitBucketBytesRejectsDeclaredOversizedPut(t *testing.T) { t.Fatalf("upload budget after rejection %d, want 0", n) } } + +func TestDeletingTheCircuitBreakerConfigResetsIt(t *testing.T) { + s3a := &S3ApiServer{cb: &CircuitBreaker{counters: make(map[string]*int64), limitations: make(map[string]int64)}} + entry := &filer_pb.Entry{ + Name: s3_constants.CircuitBreakerConfigFile, + Content: []byte(`{"global": {"enabled": true, "actions": {"Read:Count": 1}}, "buckets": {"b": {"enabled": true, "actions": {"Write:Count": 2}}}}`), + } + if err := s3a.onCircuitBreakerConfigChange(s3_constants.CircuitBreakerConfigDir, nil, entry); err != nil { + t.Fatal(err) + } + if !s3a.cb.Enabled || len(s3a.cb.limitations) != 2 { + t.Fatalf("config not loaded: enabled=%v limitations=%v", s3a.cb.Enabled, s3a.cb.limitations) + } + + if err := s3a.onCircuitBreakerConfigChange(s3_constants.CircuitBreakerConfigDir, entry, nil); err != nil { + t.Fatal(err) + } + if s3a.cb.Enabled || len(s3a.cb.limitations) != 0 { + t.Errorf("config not reset: enabled=%v limitations=%v", s3a.cb.Enabled, s3a.cb.limitations) + } +} + +// Config changes and deletes swap the limits while requests read them. +func TestCircuitBreakerConfigChangesDuringRequests(t *testing.T) { + s3a := &S3ApiServer{cb: &CircuitBreaker{counters: make(map[string]*int64), limitations: make(map[string]int64)}} + entry := &filer_pb.Entry{ + Name: s3_constants.CircuitBreakerConfigFile, + Content: []byte(`{"global": {"enabled": true, "actions": {"Read:Count": 1000}}}`), + } + h, _ := s3a.cb.Limit(func(w http.ResponseWriter, r *http.Request) {}, s3_constants.ACTION_READ) + done := make(chan struct{}) + go func() { + defer close(done) + for i := 0; i < 200; i++ { + _ = s3a.onCircuitBreakerConfigChange(s3_constants.CircuitBreakerConfigDir, nil, entry) + _ = s3a.onCircuitBreakerConfigChange(s3_constants.CircuitBreakerConfigDir, entry, nil) + } + }() + for { + select { + case <-done: + return + default: + h(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/b/k", nil)) + } + } +}