s3api_circuit_breaker_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package s3api
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb/s3_pb"
  4. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  5. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  6. "net/http"
  7. "sync"
  8. "sync/atomic"
  9. "testing"
  10. )
  11. type TestLimitCase struct {
  12. actionName string
  13. limitType string
  14. bucketLimitValue int64
  15. globalLimitValue int64
  16. routineCount int
  17. successCount int64
  18. }
  19. var (
  20. bucket = "/test"
  21. action = s3_constants.ACTION_WRITE
  22. fileSize int64 = 200
  23. TestLimitCases = []*TestLimitCase{
  24. //bucket-LimitTypeCount
  25. {action, s3_constants.LimitTypeCount, 5, 6, 60, 5},
  26. {action, s3_constants.LimitTypeCount, 0, 6, 6, 0},
  27. //global-LimitTypeCount
  28. {action, s3_constants.LimitTypeCount, 6, 5, 6, 5},
  29. {action, s3_constants.LimitTypeCount, 6, 0, 6, 0},
  30. //bucket-LimitTypeBytes
  31. {action, s3_constants.LimitTypeBytes, 1000, 1020, 6, 5},
  32. {action, s3_constants.LimitTypeBytes, 0, 1020, 6, 0},
  33. //global-LimitTypeBytes
  34. {action, s3_constants.LimitTypeBytes, 1020, 1000, 6, 5},
  35. {action, s3_constants.LimitTypeBytes, 1020, 0, 6, 0},
  36. }
  37. )
  38. func TestLimit(t *testing.T) {
  39. for _, tc := range TestLimitCases {
  40. circuitBreakerConfig := &s3_pb.S3CircuitBreakerConfig{
  41. Global: &s3_pb.S3CircuitBreakerOptions{
  42. Enabled: true,
  43. Actions: map[string]int64{
  44. s3_constants.Concat(tc.actionName, tc.limitType): tc.globalLimitValue,
  45. s3_constants.Concat(tc.actionName, tc.limitType): tc.globalLimitValue,
  46. },
  47. },
  48. Buckets: map[string]*s3_pb.S3CircuitBreakerOptions{
  49. bucket: {
  50. Enabled: true,
  51. Actions: map[string]int64{
  52. s3_constants.Concat(tc.actionName, tc.limitType): tc.bucketLimitValue,
  53. },
  54. },
  55. },
  56. }
  57. circuitBreaker := &CircuitBreaker{
  58. counters: make(map[string]*int64),
  59. limitations: make(map[string]int64),
  60. }
  61. err := circuitBreaker.loadCircuitBreakerConfig(circuitBreakerConfig)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. successCount := doLimit(circuitBreaker, tc.routineCount, &http.Request{ContentLength: fileSize}, tc.actionName)
  66. if successCount != tc.successCount {
  67. t.Errorf("successCount not equal, expect=%d, actual=%d, case: %v", tc.successCount, successCount, tc)
  68. }
  69. }
  70. }
  71. func doLimit(circuitBreaker *CircuitBreaker, routineCount int, r *http.Request, action string) int64 {
  72. var successCounter int64
  73. resultCh := make(chan []func(), routineCount)
  74. var wg sync.WaitGroup
  75. for i := 0; i < routineCount; i++ {
  76. wg.Add(1)
  77. go func() {
  78. defer wg.Done()
  79. rollbackFn, errCode := circuitBreaker.limit(r, bucket, action)
  80. if errCode == s3err.ErrNone {
  81. atomic.AddInt64(&successCounter, 1)
  82. }
  83. resultCh <- rollbackFn
  84. }()
  85. }
  86. wg.Wait()
  87. close(resultCh)
  88. for fns := range resultCh {
  89. for _, fn := range fns {
  90. fn()
  91. }
  92. }
  93. return successCounter
  94. }