diff --git a/client/option.go b/client/option.go index 2b8b66e1f5..0c04e3a7a4 100644 --- a/client/option.go +++ b/client/option.go @@ -325,7 +325,7 @@ func WithFailureRetry(p *retry.FailurePolicy) Option { if p == nil { return } - di.Push(fmt.Sprintf("WithFailureRetry(%+v)", *p)) + di.Push(fmt.Sprintf("WithFailureRetry(%+v)", p)) if o.RetryMethodPolicies == nil { o.RetryMethodPolicies = make(map[string]retry.Policy) } @@ -343,7 +343,7 @@ func WithBackupRequest(p *retry.BackupPolicy) Option { if p == nil { return } - di.Push(fmt.Sprintf("WithBackupRequest(%+v)", *p)) + di.Push(fmt.Sprintf("WithBackupRequest(%+v)", p)) if o.RetryMethodPolicies == nil { o.RetryMethodPolicies = make(map[string]retry.Policy) } diff --git a/client/option_test.go b/client/option_test.go index 883f67481c..33aa6969b9 100644 --- a/client/option_test.go +++ b/client/option_test.go @@ -19,6 +19,7 @@ package client import ( "context" "crypto/tls" + "errors" "fmt" "reflect" "testing" @@ -52,47 +53,71 @@ import ( "github.com/cloudwego/kitex/transport" ) +func checkOneOptionDebugInfo(t *testing.T, opt Option, expectStr string) error { + o := &Options{} + o.Apply([]Option{opt}) + if len(o.DebugInfo) != 1 { + return errors.New("length of DebugInfo is unexpected") + } + if o.DebugInfo[0] != expectStr { + return fmt.Errorf("DebugInfo not match with expect str:\n debugInfo=%s", o.DebugInfo[0]) + } + return nil +} func TestRetryOptionDebugInfo(t *testing.T) { - fp := retry.NewFailurePolicy() - fp.WithDDLStop() - expectPolicyStr := "WithFailureRetry({StopPolicy:{MaxRetryTimes:2 MaxDurationMS:0 DisableChainStop:false DDLStop:true " + - "CBPolicy:{ErrorRate:0.1}} BackOffPolicy:&{BackOffType:none CfgItems:map[]} RetrySameNode:false ShouldResultRetry:{ErrorRetry:false, RespRetry:false}})" - policyStr := fmt.Sprintf("WithFailureRetry(%+v)", fp) - test.Assert(t, policyStr == expectPolicyStr, policyStr) - - fp.WithFixedBackOff(10) - expectPolicyStr = "WithFailureRetry({StopPolicy:{MaxRetryTimes:2 MaxDurationMS:0 DisableChainStop:false DDLStop:true " + - "CBPolicy:{ErrorRate:0.1}} BackOffPolicy:&{BackOffType:fixed CfgItems:map[fix_ms:10]} RetrySameNode:false ShouldResultRetry:{ErrorRetry:false, RespRetry:false}})" - policyStr = fmt.Sprintf("WithFailureRetry(%+v)", fp) - test.Assert(t, policyStr == expectPolicyStr, policyStr) - - fp.WithRandomBackOff(10, 20) - fp.DisableChainRetryStop() - expectPolicyStr = "WithFailureRetry({StopPolicy:{MaxRetryTimes:2 MaxDurationMS:0 DisableChainStop:true DDLStop:true " + - "CBPolicy:{ErrorRate:0.1}} BackOffPolicy:&{BackOffType:random CfgItems:map[max_ms:20 min_ms:10]} RetrySameNode:false ShouldResultRetry:{ErrorRetry:false, RespRetry:false}})" - policyStr = fmt.Sprintf("WithFailureRetry(%+v)", fp) - test.Assert(t, policyStr == expectPolicyStr, policyStr) - - fp.WithRetrySameNode() - expectPolicyStr = "WithFailureRetry({StopPolicy:{MaxRetryTimes:2 MaxDurationMS:0 DisableChainStop:true DDLStop:true " + - "CBPolicy:{ErrorRate:0.1}} BackOffPolicy:&{BackOffType:random CfgItems:map[max_ms:20 min_ms:10]} RetrySameNode:true ShouldResultRetry:{ErrorRetry:false, RespRetry:false}})" - policyStr = fmt.Sprintf("WithFailureRetry(%+v)", fp) - test.Assert(t, policyStr == expectPolicyStr, policyStr) - - fp.WithSpecifiedResultRetry(&retry.ShouldResultRetry{ErrorRetry: func(err error, ri rpcinfo.RPCInfo) bool { - return false - }}) - expectPolicyStr = "WithFailureRetry({StopPolicy:{MaxRetryTimes:2 MaxDurationMS:0 DisableChainStop:true DDLStop:true " + - "CBPolicy:{ErrorRate:0.1}} BackOffPolicy:&{BackOffType:random CfgItems:map[max_ms:20 min_ms:10]} RetrySameNode:true ShouldResultRetry:{ErrorRetry:true, RespRetry:false}})" - policyStr = fmt.Sprintf("WithFailureRetry(%+v)", fp) - test.Assert(t, policyStr == expectPolicyStr, policyStr) - - bp := retry.NewBackupPolicy(20) - expectPolicyStr = "WithBackupRequest({RetryDelayMS:20 StopPolicy:{MaxRetryTimes:1 MaxDurationMS:0 DisableChainStop:false " + - "DDLStop:false CBPolicy:{ErrorRate:0.1}} RetrySameNode:false})" - policyStr = fmt.Sprintf("WithBackupRequest(%+v)", bp) - test.Assert(t, policyStr == expectPolicyStr, policyStr) - WithBackupRequest(bp) + t.Run("FailurePolicy", func(t *testing.T) { + fp := retry.NewFailurePolicy() + fp.WithDDLStop() + expectPolicyStr := "WithFailureRetry({StopPolicy:{MaxRetryTimes:2 MaxDurationMS:0 DisableChainStop:false DDLStop:true " + + "CBPolicy:{ErrorRate:0.1}} BackOffPolicy:&{BackOffType:none CfgItems:map[]} RetrySameNode:false ShouldResultRetry:{ErrorRetry:false, RespRetry:false}})" + opt := WithFailureRetry(fp) + policyStr := fmt.Sprintf("WithFailureRetry(%+v)", fp) + test.Assert(t, policyStr == expectPolicyStr, policyStr) + + err := checkOneOptionDebugInfo(t, opt, expectPolicyStr) + test.Assert(t, err == nil, err) + + fp.WithFixedBackOff(10) + expectPolicyStr = "WithFailureRetry({StopPolicy:{MaxRetryTimes:2 MaxDurationMS:0 DisableChainStop:false DDLStop:true " + + "CBPolicy:{ErrorRate:0.1}} BackOffPolicy:&{BackOffType:fixed CfgItems:map[fix_ms:10]} RetrySameNode:false ShouldResultRetry:{ErrorRetry:false, RespRetry:false}})" + //policyStr = fmt.Sprintf("WithFailureRetry(%+v)", fp) + //test.Assert(t, policyStr == expectPolicyStr, policyStr) + opt = WithFailureRetry(fp) + err = checkOneOptionDebugInfo(t, opt, expectPolicyStr) + test.Assert(t, err == nil, err) + + fp.WithRandomBackOff(10, 20) + fp.DisableChainRetryStop() + expectPolicyStr = "WithFailureRetry({StopPolicy:{MaxRetryTimes:2 MaxDurationMS:0 DisableChainStop:true DDLStop:true " + + "CBPolicy:{ErrorRate:0.1}} BackOffPolicy:&{BackOffType:random CfgItems:map[max_ms:20 min_ms:10]} RetrySameNode:false ShouldResultRetry:{ErrorRetry:false, RespRetry:false}})" + opt = WithFailureRetry(fp) + err = checkOneOptionDebugInfo(t, opt, expectPolicyStr) + test.Assert(t, err == nil, err) + + fp.WithRetrySameNode() + expectPolicyStr = "WithFailureRetry({StopPolicy:{MaxRetryTimes:2 MaxDurationMS:0 DisableChainStop:true DDLStop:true " + + "CBPolicy:{ErrorRate:0.1}} BackOffPolicy:&{BackOffType:random CfgItems:map[max_ms:20 min_ms:10]} RetrySameNode:true ShouldResultRetry:{ErrorRetry:false, RespRetry:false}})" + opt = WithFailureRetry(fp) + err = checkOneOptionDebugInfo(t, opt, expectPolicyStr) + test.Assert(t, err == nil, err) + + fp.WithSpecifiedResultRetry(&retry.ShouldResultRetry{ErrorRetryWithCtx: func(ctx context.Context, err error, ri rpcinfo.RPCInfo) bool { + return false + }}) + expectPolicyStr = "WithFailureRetry({StopPolicy:{MaxRetryTimes:2 MaxDurationMS:0 DisableChainStop:true DDLStop:true " + + "CBPolicy:{ErrorRate:0.1}} BackOffPolicy:&{BackOffType:random CfgItems:map[max_ms:20 min_ms:10]} RetrySameNode:true ShouldResultRetry:{ErrorRetry:true, RespRetry:false}})" + opt = WithFailureRetry(fp) + err = checkOneOptionDebugInfo(t, opt, expectPolicyStr) + test.Assert(t, err == nil, err) + }) + t.Run("FailurePolicy", func(t *testing.T) { + bp := retry.NewBackupPolicy(20) + expectPolicyStr := "WithBackupRequest({RetryDelayMS:20 StopPolicy:{MaxRetryTimes:1 MaxDurationMS:0 DisableChainStop:false " + + "DDLStop:false CBPolicy:{ErrorRate:0.1}} RetrySameNode:false})" + opt := WithBackupRequest(bp) + err := checkOneOptionDebugInfo(t, opt, expectPolicyStr) + test.Assert(t, err == nil, err) + }) } func TestRetryOption(t *testing.T) {