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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-30 13:14:34 -08:00
1 parent c637d60dfc
commit 1a2be918bf
3 files changed
+62 -12

No files matched your search

+2 -5
View File
@@ -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
+12 -7
View File
@@ -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]
+48
View File
@@ -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))
}
}
}