forked from weibocom/motan-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request weibocom#101 from Zha-Zha/bugfix/circuitBreaker
Refactor CircuitBreaker filter
- Loading branch information
Showing
16 changed files
with
397 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package filter | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
|
||
"github.com/afex/hystrix-go/hystrix" | ||
motan "github.com/weibocom/motan-go/core" | ||
"github.com/weibocom/motan-go/log" | ||
) | ||
|
||
const ( | ||
RequestVolumeThresholdField = "circuitBreaker.requestThreshold" | ||
SleepWindowField = "circuitBreaker.sleepWindow" //ms | ||
ErrorPercentThreshold = "circuitBreaker.errorPercent" //% | ||
IncludeBizException = "circuitBreaker.bizException" | ||
) | ||
|
||
type CircuitBreakerFilter struct { | ||
url *motan.URL | ||
next motan.EndPointFilter | ||
circuitBreaker *hystrix.CircuitBreaker | ||
includeBizException bool | ||
} | ||
|
||
func (c *CircuitBreakerFilter) GetIndex() int { | ||
return 20 | ||
} | ||
|
||
func (c *CircuitBreakerFilter) GetName() string { | ||
return CircuitBreaker | ||
} | ||
|
||
func (c *CircuitBreakerFilter) NewFilter(url *motan.URL) motan.Filter { | ||
bizException := newCircuitBreaker(c.GetName(), url) | ||
return &CircuitBreakerFilter{url: url, includeBizException: bizException} | ||
} | ||
|
||
func (c *CircuitBreakerFilter) Filter(caller motan.Caller, request motan.Request) motan.Response { | ||
var response motan.Response | ||
err := hystrix.Do(c.url.GetIdentity(), func() error { | ||
response = c.GetNext().Filter(caller, request) | ||
return checkException(response, c.includeBizException) | ||
}, nil) | ||
if err != nil { | ||
return defaultErrMotanResponse(request, err.Error()) | ||
} | ||
return response | ||
} | ||
|
||
func (c *CircuitBreakerFilter) HasNext() bool { | ||
return c.next != nil | ||
} | ||
|
||
func (c *CircuitBreakerFilter) SetNext(nextFilter motan.EndPointFilter) { | ||
c.next = nextFilter | ||
} | ||
|
||
func (c *CircuitBreakerFilter) GetNext() motan.EndPointFilter { | ||
return c.next | ||
} | ||
|
||
func (c *CircuitBreakerFilter) GetType() int32 { | ||
return motan.EndPointFilterType | ||
} | ||
|
||
func newCircuitBreaker(filterName string, url *motan.URL) bool { | ||
bizExceptionStr := url.GetParam(IncludeBizException, "true") | ||
bizException, err := strconv.ParseBool(bizExceptionStr) | ||
if err != nil { | ||
bizException = true | ||
vlog.Warningf("[%s] parse config %s error, use default", filterName, IncludeBizException) | ||
} | ||
commandConfig := buildCommandConfig(filterName, url) | ||
hystrix.ConfigureCommand(url.GetIdentity(), *commandConfig) | ||
if _, _, err = hystrix.GetCircuit(url.GetIdentity()); err != nil { | ||
vlog.Errorf("[%s] new circuit fail. err:%s, url:%v, config{%s}\n", err.Error(), filterName, url.GetIdentity(), getConfigStr(commandConfig)+"bizException:"+bizExceptionStr) | ||
} else { | ||
vlog.Infof("[%s] new circuit success. url:%v, config{%s}\n", filterName, url.GetIdentity(), getConfigStr(commandConfig)+"bizException:"+bizExceptionStr) | ||
} | ||
return bizException | ||
} | ||
|
||
func buildCommandConfig(filterName string, url *motan.URL) *hystrix.CommandConfig { | ||
hystrix.DefaultMaxConcurrent = 1000 | ||
hystrix.DefaultTimeout = int(url.GetPositiveIntValue(motan.TimeOutKey, int64(hystrix.DefaultTimeout))) * 2 | ||
commandConfig := &hystrix.CommandConfig{} | ||
if v, ok := url.Parameters[RequestVolumeThresholdField]; ok { | ||
if temp, _ := strconv.Atoi(v); temp > 0 { | ||
commandConfig.RequestVolumeThreshold = temp | ||
} else { | ||
vlog.Warningf("[%s] parse config %s error, use default", filterName, RequestVolumeThresholdField) | ||
} | ||
} | ||
if v, ok := url.Parameters[SleepWindowField]; ok { | ||
if temp, _ := strconv.Atoi(v); temp > 0 { | ||
commandConfig.SleepWindow = temp | ||
} else { | ||
vlog.Warningf("[%s] parse config %s error, use default", filterName, SleepWindowField) | ||
} | ||
} | ||
if v, ok := url.Parameters[ErrorPercentThreshold]; ok { | ||
if temp, _ := strconv.Atoi(v); temp > 0 && temp <= 100 { | ||
commandConfig.ErrorPercentThreshold = temp | ||
} else { | ||
vlog.Warningf("[%s] parse config %s error, use default", filterName, ErrorPercentThreshold) | ||
} | ||
} | ||
return commandConfig | ||
} | ||
|
||
func defaultErrMotanResponse(request motan.Request, errMsg string) motan.Response { | ||
response := &motan.MotanResponse{ | ||
RequestID: request.GetRequestID(), | ||
Attachment: motan.NewStringMap(motan.DefaultAttachmentSize), | ||
ProcessTime: 0, | ||
Exception: &motan.Exception{ | ||
ErrCode: 400, | ||
ErrMsg: errMsg, | ||
ErrType: motan.ServiceException}, | ||
} | ||
return response | ||
} | ||
|
||
func getConfigStr(config *hystrix.CommandConfig) string { | ||
var ret string | ||
if config.RequestVolumeThreshold != 0 { | ||
ret += "requestThreshold:" + strconv.Itoa(config.RequestVolumeThreshold) + " " | ||
} else { | ||
ret += "requestThreshold:" + strconv.Itoa(hystrix.DefaultVolumeThreshold) + " " | ||
} | ||
if config.SleepWindow != 0 { | ||
ret += "sleepWindow:" + strconv.Itoa(config.SleepWindow) + " " | ||
} else { | ||
ret += "sleepWindow:" + strconv.Itoa(hystrix.DefaultSleepWindow) + " " | ||
} | ||
if config.ErrorPercentThreshold != 0 { | ||
ret += "errorPercent:" + strconv.Itoa(config.ErrorPercentThreshold) + " " | ||
} else { | ||
ret += "errorPercent:" + strconv.Itoa(hystrix.DefaultErrorPercentThreshold) + " " | ||
} | ||
return ret | ||
} | ||
|
||
func checkException(response motan.Response, includeBizException bool) error { | ||
if ex := response.GetException(); ex != nil && (includeBizException || ex.ErrType != motan.BizException) { | ||
return errors.New(ex.ErrMsg) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package filter | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/weibocom/motan-go/core" | ||
"github.com/weibocom/motan-go/endpoint" | ||
"github.com/weibocom/motan-go/log" | ||
) | ||
|
||
var ( | ||
count = int64(0) | ||
countLock sync.RWMutex | ||
filterSleepTime = 7 * time.Millisecond | ||
filterSleepTimeLock sync.RWMutex | ||
) | ||
|
||
func TestCircuitBreakerFilter(t *testing.T) { | ||
//Init | ||
defaultExtFactory := &core.DefaultExtensionFactory{} | ||
defaultExtFactory.Initialize() | ||
RegistDefaultFilters(defaultExtFactory) | ||
endpoint.RegistDefaultEndpoint(defaultExtFactory) | ||
url := &core.URL{Host: "127.0.0.1", Port: 7888, Protocol: "mockEndpoint"} | ||
caller := defaultExtFactory.GetEndPoint(url) | ||
request := &core.MotanRequest{Method: "testMethod"} | ||
|
||
//Test NewFilter | ||
param := map[string]string{core.TimeOutKey: "2", SleepWindowField: "300"} | ||
filterURL := &core.URL{Host: "127.0.0.1", Port: 7888, Protocol: "mockEndpoint", Parameters: param} | ||
f := defaultExtFactory.GetFilter(CircuitBreaker) | ||
if f == nil { | ||
t.Error("Can not find circuitBreaker filter!") | ||
} | ||
f = f.NewFilter(filterURL) | ||
ef := f.(core.EndPointFilter) | ||
ef.SetNext(new(mockEndPointFilter)) | ||
|
||
//Test circuitBreakerTimeout & requestVolumeThreshold | ||
for i := 0; i < 30; i++ { | ||
ef.Filter(caller, request) | ||
} | ||
time.Sleep(10 * time.Millisecond) //wait until async call complete | ||
countLock.RLock() | ||
if count != 20 && count != 21 { | ||
t.Error("Test circuitBreakerTimeout failed! count:", count) | ||
} | ||
countLock.RUnlock() | ||
|
||
//Test sleepWindow | ||
time.Sleep(350 * time.Millisecond) //wait until SleepWindowField | ||
for i := 0; i < 5; i++ { | ||
ef.Filter(caller, request) | ||
} | ||
time.Sleep(10 * time.Millisecond) //wait until async call complete | ||
countLock.RLock() | ||
if count != 21 && count != 22 { | ||
t.Error("Test sleepWindow failed! count:", count) | ||
} | ||
countLock.RUnlock() | ||
|
||
//Test errorPercentThreshold | ||
time.Sleep(350 * time.Millisecond) //wait until SleepWindowField | ||
filterSleepTimeLock.Lock() | ||
filterSleepTime = 0 * time.Millisecond | ||
filterSleepTimeLock.Unlock() | ||
for i := 0; i < 100; i++ { | ||
ef.Filter(caller, request) | ||
} | ||
time.Sleep(10 * time.Millisecond) //wait until async call complete | ||
filterSleepTimeLock.Lock() | ||
filterSleepTime = 7 * time.Millisecond | ||
filterSleepTimeLock.Unlock() | ||
for i := 0; i < 50; i++ { | ||
ef.Filter(caller, request) | ||
} | ||
time.Sleep(10 * time.Millisecond) //wait until async call complete | ||
countLock.RLock() | ||
if count != 171 && count != 172 { | ||
t.Error("Test sleepWindow failed! count:", count) | ||
} | ||
countLock.RUnlock() | ||
} | ||
|
||
type mockEndPointFilter struct{} | ||
|
||
func (m *mockEndPointFilter) GetName() string { | ||
return "mockEndPointFilter" | ||
} | ||
|
||
func (m *mockEndPointFilter) NewFilter(url *core.URL) core.Filter { | ||
return core.GetLastEndPointFilter() | ||
} | ||
|
||
func (m *mockEndPointFilter) Filter(caller core.Caller, request core.Request) core.Response { | ||
countLock.Lock() | ||
atomic.AddInt64(&count, 1) | ||
countLock.Unlock() | ||
filterSleepTimeLock.RLock() | ||
time.Sleep(filterSleepTime) | ||
filterSleepTimeLock.RUnlock() | ||
return caller.Call(request) | ||
} | ||
|
||
func (m *mockEndPointFilter) HasNext() bool { | ||
return false | ||
} | ||
|
||
func (m *mockEndPointFilter) SetNext(nextFilter core.EndPointFilter) { | ||
vlog.Errorf("should not set next in mockEndPointFilter! filer:%s\n", nextFilter.GetName()) | ||
} | ||
func (m *mockEndPointFilter) GetNext() core.EndPointFilter { | ||
return nil | ||
} | ||
func (m *mockEndPointFilter) GetIndex() int { | ||
return 100 | ||
} | ||
func (m *mockEndPointFilter) GetType() int32 { | ||
return core.EndPointFilterType | ||
} |
Oops, something went wrong.