2022-06-15 13:07:55 +00:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/s3_pb"
|
2022-06-17 11:07:39 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
|
2022-06-15 13:07:55 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/s3api/s3err"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"net/http"
|
2022-06-17 09:11:18 +00:00
|
|
|
"sync"
|
2022-06-20 04:35:29 +00:00
|
|
|
"sync/atomic"
|
2022-06-15 13:07:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CircuitBreaker struct {
|
2022-06-20 11:16:53 +00:00
|
|
|
sync.RWMutex
|
2022-06-15 13:07:55 +00:00
|
|
|
Enabled bool
|
2022-06-20 04:35:29 +00:00
|
|
|
counters map[string]*int64
|
2022-06-15 13:07:55 +00:00
|
|
|
limitations map[string]int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCircuitBreaker(option *S3ApiServerOption) *CircuitBreaker {
|
|
|
|
cb := &CircuitBreaker{
|
2022-06-20 04:35:29 +00:00
|
|
|
counters: make(map[string]*int64),
|
2022-06-15 13:07:55 +00:00
|
|
|
limitations: make(map[string]int64),
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:11:18 +00:00
|
|
|
err := pb.WithFilerClient(false, option.Filer, option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2022-06-17 11:07:39 +00:00
|
|
|
content, err := filer.ReadInsideFiler(client, s3_constants.CircuitBreakerConfigDir, s3_constants.CircuitBreakerConfigFile)
|
2022-06-15 13:07:55 +00:00
|
|
|
if err != nil {
|
2022-06-17 09:11:18 +00:00
|
|
|
return fmt.Errorf("read S3 circuit breaker config: %v", err)
|
2022-06-15 13:07:55 +00:00
|
|
|
}
|
2022-06-17 09:11:18 +00:00
|
|
|
return cb.LoadS3ApiConfigurationFromBytes(content)
|
2022-06-15 13:07:55 +00:00
|
|
|
})
|
2022-06-17 09:11:18 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Warningf("fail to load config: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-06-15 13:07:55 +00:00
|
|
|
return cb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cb *CircuitBreaker) LoadS3ApiConfigurationFromBytes(content []byte) error {
|
|
|
|
cbCfg := &s3_pb.S3CircuitBreakerConfig{}
|
|
|
|
if err := filer.ParseS3ConfigurationFromBytes(content, cbCfg); err != nil {
|
|
|
|
glog.Warningf("unmarshal error: %v", err)
|
|
|
|
return fmt.Errorf("unmarshal error: %v", err)
|
|
|
|
}
|
2022-06-17 09:11:18 +00:00
|
|
|
if err := cb.loadCircuitBreakerConfig(cbCfg); err != nil {
|
2022-06-15 13:07:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:11:18 +00:00
|
|
|
func (cb *CircuitBreaker) loadCircuitBreakerConfig(cfg *s3_pb.S3CircuitBreakerConfig) error {
|
2022-06-15 13:07:55 +00:00
|
|
|
|
|
|
|
//global
|
|
|
|
globalEnabled := false
|
|
|
|
globalOptions := cfg.Global
|
|
|
|
limitations := make(map[string]int64)
|
|
|
|
if globalOptions != nil && globalOptions.Enabled && len(globalOptions.Actions) > 0 {
|
|
|
|
globalEnabled = globalOptions.Enabled
|
|
|
|
for action, limit := range globalOptions.Actions {
|
|
|
|
limitations[action] = limit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cb.Enabled = globalEnabled
|
|
|
|
|
|
|
|
//buckets
|
|
|
|
for bucket, cbOptions := range cfg.Buckets {
|
|
|
|
if cbOptions.Enabled {
|
|
|
|
for action, limit := range cbOptions.Actions {
|
2022-06-17 11:07:39 +00:00
|
|
|
limitations[s3_constants.Concat(bucket, action)] = limit
|
2022-06-15 13:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cb.limitations = limitations
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:11:18 +00:00
|
|
|
func (cb *CircuitBreaker) Limit(f func(w http.ResponseWriter, r *http.Request), action string) (http.HandlerFunc, Action) {
|
2022-06-15 13:07:55 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !cb.Enabled {
|
|
|
|
f(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
2022-06-17 09:11:18 +00:00
|
|
|
rollback, errCode := cb.limit(r, bucket, action)
|
2022-06-15 13:07:55 +00:00
|
|
|
defer func() {
|
|
|
|
for _, rf := range rollback {
|
|
|
|
rf()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if errCode == s3err.ErrNone {
|
|
|
|
f(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s3err.WriteErrorResponse(w, r, errCode)
|
|
|
|
}, Action(action)
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:11:18 +00:00
|
|
|
func (cb *CircuitBreaker) limit(r *http.Request, bucket string, action string) (rollback []func(), errCode s3err.ErrorCode) {
|
2022-06-15 13:07:55 +00:00
|
|
|
|
|
|
|
//bucket simultaneous request count
|
2022-06-20 11:16:53 +00:00
|
|
|
bucketCountRollBack, errCode := cb.loadCounterAndCompare(s3_constants.Concat(bucket, action, s3_constants.LimitTypeCount), 1, s3err.ErrTooManyRequest)
|
2022-06-15 13:07:55 +00:00
|
|
|
if bucketCountRollBack != nil {
|
|
|
|
rollback = append(rollback, bucketCountRollBack)
|
|
|
|
}
|
|
|
|
if errCode != s3err.ErrNone {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//bucket simultaneous request content bytes
|
2022-06-20 11:16:53 +00:00
|
|
|
bucketContentLengthRollBack, errCode := cb.loadCounterAndCompare(s3_constants.Concat(bucket, action, s3_constants.LimitTypeBytes), r.ContentLength, s3err.ErrRequestBytesExceed)
|
2022-06-15 13:07:55 +00:00
|
|
|
if bucketContentLengthRollBack != nil {
|
|
|
|
rollback = append(rollback, bucketContentLengthRollBack)
|
|
|
|
}
|
|
|
|
if errCode != s3err.ErrNone {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//global simultaneous request count
|
2022-06-20 11:16:53 +00:00
|
|
|
globalCountRollBack, errCode := cb.loadCounterAndCompare(s3_constants.Concat(action, s3_constants.LimitTypeCount), 1, s3err.ErrTooManyRequest)
|
2022-06-15 13:07:55 +00:00
|
|
|
if globalCountRollBack != nil {
|
|
|
|
rollback = append(rollback, globalCountRollBack)
|
|
|
|
}
|
|
|
|
if errCode != s3err.ErrNone {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//global simultaneous request content bytes
|
2022-06-20 11:16:53 +00:00
|
|
|
globalContentLengthRollBack, errCode := cb.loadCounterAndCompare(s3_constants.Concat(action, s3_constants.LimitTypeBytes), r.ContentLength, s3err.ErrRequestBytesExceed)
|
2022-06-15 13:07:55 +00:00
|
|
|
if globalContentLengthRollBack != nil {
|
|
|
|
rollback = append(rollback, globalContentLengthRollBack)
|
|
|
|
}
|
|
|
|
if errCode != s3err.ErrNone {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-20 11:16:53 +00:00
|
|
|
func (cb *CircuitBreaker) loadCounterAndCompare(key string, inc int64, errCode s3err.ErrorCode) (f func(), e s3err.ErrorCode) {
|
2022-06-15 13:07:55 +00:00
|
|
|
e = s3err.ErrNone
|
|
|
|
if max, ok := cb.limitations[key]; ok {
|
2022-06-20 11:16:53 +00:00
|
|
|
cb.RLock()
|
2022-06-15 13:07:55 +00:00
|
|
|
counter, exists := cb.counters[key]
|
2022-06-20 11:16:53 +00:00
|
|
|
cb.RUnlock()
|
|
|
|
|
2022-06-15 13:07:55 +00:00
|
|
|
if !exists {
|
2022-06-17 09:11:18 +00:00
|
|
|
cb.Lock()
|
|
|
|
counter, exists = cb.counters[key]
|
|
|
|
if !exists {
|
2022-06-20 04:35:29 +00:00
|
|
|
var newCounter int64
|
|
|
|
counter = &newCounter
|
2022-06-17 09:11:18 +00:00
|
|
|
cb.counters[key] = counter
|
|
|
|
}
|
|
|
|
cb.Unlock()
|
2022-06-15 13:07:55 +00:00
|
|
|
}
|
2022-06-20 04:35:29 +00:00
|
|
|
current := atomic.LoadInt64(counter)
|
2022-06-15 13:07:55 +00:00
|
|
|
if current+inc > max {
|
|
|
|
e = errCode
|
|
|
|
return
|
|
|
|
} else {
|
2022-06-20 04:35:29 +00:00
|
|
|
current := atomic.AddInt64(counter, inc)
|
2022-06-15 13:07:55 +00:00
|
|
|
f = func() {
|
2022-06-20 04:35:29 +00:00
|
|
|
atomic.AddInt64(counter, -inc)
|
2022-06-15 13:07:55 +00:00
|
|
|
}
|
2022-06-17 09:11:18 +00:00
|
|
|
if current > max {
|
2022-06-15 13:07:55 +00:00
|
|
|
e = errCode
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|