2022-06-17 17:11:18 +08:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/s3_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
2022-06-17 17:11:18 +08:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
2022-06-20 12:35:29 +08:00
|
|
|
"sync/atomic"
|
2022-06-17 17:11:18 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestLimitCase struct {
|
2022-06-20 19:16:53 +08:00
|
|
|
actionName string
|
|
|
|
|
2022-06-17 17:11:18 +08:00
|
|
|
limitType string
|
|
|
|
bucketLimitValue int64
|
|
|
|
globalLimitValue int64
|
|
|
|
|
|
|
|
routineCount int
|
|
|
|
successCount int64
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
bucket = "/test"
|
2022-06-20 19:16:53 +08:00
|
|
|
action = s3_constants.ACTION_WRITE
|
|
|
|
fileSize int64 = 200
|
|
|
|
|
2022-06-17 17:11:18 +08:00
|
|
|
TestLimitCases = []*TestLimitCase{
|
2022-06-20 19:16:53 +08:00
|
|
|
|
|
|
|
//bucket-LimitTypeCount
|
|
|
|
{action, s3_constants.LimitTypeCount, 5, 6, 60, 5},
|
|
|
|
{action, s3_constants.LimitTypeCount, 0, 6, 6, 0},
|
|
|
|
|
|
|
|
//global-LimitTypeCount
|
|
|
|
{action, s3_constants.LimitTypeCount, 6, 5, 6, 5},
|
|
|
|
{action, s3_constants.LimitTypeCount, 6, 0, 6, 0},
|
|
|
|
|
|
|
|
//bucket-LimitTypeBytes
|
|
|
|
{action, s3_constants.LimitTypeBytes, 1000, 1020, 6, 5},
|
|
|
|
{action, s3_constants.LimitTypeBytes, 0, 1020, 6, 0},
|
|
|
|
|
|
|
|
//global-LimitTypeBytes
|
|
|
|
{action, s3_constants.LimitTypeBytes, 1020, 1000, 6, 5},
|
|
|
|
{action, s3_constants.LimitTypeBytes, 1020, 0, 6, 0},
|
2022-06-17 17:11:18 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLimit(t *testing.T) {
|
|
|
|
for _, tc := range TestLimitCases {
|
|
|
|
circuitBreakerConfig := &s3_pb.S3CircuitBreakerConfig{
|
2022-06-17 19:07:39 +08:00
|
|
|
Global: &s3_pb.S3CircuitBreakerOptions{
|
2022-06-17 17:11:18 +08:00
|
|
|
Enabled: true,
|
|
|
|
Actions: map[string]int64{
|
2022-06-17 19:07:39 +08:00
|
|
|
s3_constants.Concat(tc.actionName, tc.limitType): tc.globalLimitValue,
|
|
|
|
s3_constants.Concat(tc.actionName, tc.limitType): tc.globalLimitValue,
|
2022-06-17 17:11:18 +08:00
|
|
|
},
|
|
|
|
},
|
2022-06-17 19:07:39 +08:00
|
|
|
Buckets: map[string]*s3_pb.S3CircuitBreakerOptions{
|
2022-06-17 17:11:18 +08:00
|
|
|
bucket: {
|
|
|
|
Enabled: true,
|
|
|
|
Actions: map[string]int64{
|
2022-06-17 19:07:39 +08:00
|
|
|
s3_constants.Concat(tc.actionName, tc.limitType): tc.bucketLimitValue,
|
2022-06-17 17:11:18 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
circuitBreaker := &CircuitBreaker{
|
2022-06-20 12:35:29 +08:00
|
|
|
counters: make(map[string]*int64),
|
2022-06-17 17:11:18 +08:00
|
|
|
limitations: make(map[string]int64),
|
|
|
|
}
|
|
|
|
err := circuitBreaker.loadCircuitBreakerConfig(circuitBreakerConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-06-20 19:16:53 +08:00
|
|
|
successCount := doLimit(circuitBreaker, tc.routineCount, &http.Request{ContentLength: fileSize}, tc.actionName)
|
2022-06-17 17:11:18 +08:00
|
|
|
if successCount != tc.successCount {
|
2022-06-20 19:16:53 +08:00
|
|
|
t.Errorf("successCount not equal, expect=%d, actual=%d, case: %v", tc.successCount, successCount, tc)
|
2022-06-17 17:11:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 19:16:53 +08:00
|
|
|
func doLimit(circuitBreaker *CircuitBreaker, routineCount int, r *http.Request, action string) int64 {
|
2022-06-20 12:35:29 +08:00
|
|
|
var successCounter int64
|
2022-06-17 17:11:18 +08:00
|
|
|
resultCh := make(chan []func(), routineCount)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < routineCount; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
rollbackFn, errCode := circuitBreaker.limit(r, bucket, action)
|
|
|
|
if errCode == s3err.ErrNone {
|
2022-06-20 12:35:29 +08:00
|
|
|
atomic.AddInt64(&successCounter, 1)
|
2022-06-17 17:11:18 +08:00
|
|
|
}
|
|
|
|
resultCh <- rollbackFn
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
close(resultCh)
|
|
|
|
for fns := range resultCh {
|
|
|
|
for _, fn := range fns {
|
|
|
|
fn()
|
|
|
|
}
|
|
|
|
}
|
2022-06-20 12:35:29 +08:00
|
|
|
return successCounter
|
2022-06-17 17:11:18 +08:00
|
|
|
}
|