Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:failoverbug and retries change to string type #195

Merged
merged 5 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
*.jar

# Test binary, build with `go test -c`
*.test
Expand All @@ -24,3 +25,8 @@ coverage.txt
logs/
.vscode/
coverage.txt

# unit test
remoting/zookeeper/zookeeper-4unittest/
config_center/zookeeper/zookeeper-4unittest/
registry/zookeeper/zookeeper-4unittest/
11 changes: 7 additions & 4 deletions cluster/cluster_impl/failback_cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package cluster_impl

import (
"strconv"
"sync"
"time"
)
Expand Down Expand Up @@ -54,15 +55,17 @@ func newFailbackClusterInvoker(directory cluster.Directory) protocol.Invoker {
invoker := &failbackClusterInvoker{
baseClusterInvoker: newBaseClusterInvoker(directory),
}
retriesConfig := invoker.GetUrl().GetParamInt(constant.RETRIES_KEY, constant.DEFAULT_FAILBACK_TIMES)
if retriesConfig <= 0 {
retriesConfig = constant.DEFAULT_FAILBACK_TIMES
retriesConfig := invoker.GetUrl().GetParam(constant.RETRIES_KEY, constant.DEFAULT_FAILBACK_TIMES)
retries, err := strconv.Atoi(retriesConfig)
if err != nil || retries < 0 {
retries = constant.DEFAULT_FAILBACK_TIMES_INT
hxmhlt marked this conversation as resolved.
Show resolved Hide resolved
}

failbackTasksConfig := invoker.GetUrl().GetParamInt(constant.FAIL_BACK_TASKS_KEY, constant.DEFAULT_FAILBACK_TASKS)
if failbackTasksConfig <= 0 {
failbackTasksConfig = constant.DEFAULT_FAILBACK_TASKS
}
invoker.maxRetries = retriesConfig
invoker.maxRetries = int64(retries)
invoker.failbackTasks = failbackTasksConfig
return invoker
}
Expand Down
13 changes: 9 additions & 4 deletions cluster/cluster_impl/failover_cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cluster_impl

import (
perrors "github.com/pkg/errors"
"strconv"
hxmhlt marked this conversation as resolved.
Show resolved Hide resolved
)

import (
Expand Down Expand Up @@ -53,16 +54,20 @@ func (invoker *failoverClusterInvoker) Invoke(invocation protocol.Invocation) pr
url := invokers[0].GetUrl()

//get reties
retries := url.GetParamInt(constant.RETRIES_KEY, constant.DEFAULT_RETRIES)
retriesConfig := url.GetParam(constant.RETRIES_KEY, constant.DEFAULT_RETRIES)

//Get the service method loadbalance config if have
if v := url.GetMethodParamInt(methodName, constant.RETRIES_KEY, 0); v != 0 {
retries = v
if v := url.GetMethodParam(methodName, constant.RETRIES_KEY, ""); len(v) != 0 {
retriesConfig = v
}
retries, err := strconv.Atoi(retriesConfig)
if err != nil || retries < 0 {
retries = constant.DEFAULT_RETRIES_INT
}
invoked := []protocol.Invoker{}
providers := []string{}
var result protocol.Result
for i := int64(0); i < retries; i++ {
for i := 0; i <= retries; i++ {
hxmhlt marked this conversation as resolved.
Show resolved Hide resolved
//Reselect before retry to avoid a change of candidate `invokers`.
//NOTE: if `invokers` changed, then `invoked` also lose accuracy.
if i > 0 {
Expand Down
8 changes: 4 additions & 4 deletions cluster/cluster_impl/failover_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,22 @@ func normalInvoke(t *testing.T, successCount int, urlParam url.Values, invocatio
}
func Test_FailoverInvokeSuccess(t *testing.T) {
urlParams := url.Values{}
result := normalInvoke(t, 2, urlParams)
result := normalInvoke(t, 3, urlParams)
assert.NoError(t, result.Error())
count = 0
}

func Test_FailoverInvokeFail(t *testing.T) {
urlParams := url.Values{}
result := normalInvoke(t, 3, urlParams)
result := normalInvoke(t, 4, urlParams)
assert.Errorf(t, result.Error(), "error")
count = 0
}

func Test_FailoverInvoke1(t *testing.T) {
urlParams := url.Values{}
urlParams.Set(constant.RETRIES_KEY, "3")
result := normalInvoke(t, 3, urlParams)
result := normalInvoke(t, 4, urlParams)
assert.NoError(t, result.Error())
count = 0
}
Expand All @@ -144,7 +144,7 @@ func Test_FailoverInvoke2(t *testing.T) {
urlParams.Set("methods.test."+constant.RETRIES_KEY, "3")

ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))
result := normalInvoke(t, 3, urlParams, ivc)
result := normalInvoke(t, 4, urlParams, ivc)
assert.NoError(t, result.Error())
count = 0
}
Expand Down
16 changes: 9 additions & 7 deletions common/constant/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ const (
)

const (
DEFAULT_LOADBALANCE = "random"
DEFAULT_RETRIES = 2
DEFAULT_PROTOCOL = "dubbo"
DEFAULT_REG_TIMEOUT = "10s"
DEFAULT_CLUSTER = "failover"
DEFAULT_FAILBACK_TIMES = 3
DEFAULT_FAILBACK_TASKS = 100
DEFAULT_LOADBALANCE = "random"
DEFAULT_RETRIES = "2"
DEFAULT_RETRIES_INT = 2
DEFAULT_PROTOCOL = "dubbo"
DEFAULT_REG_TIMEOUT = "10s"
DEFAULT_CLUSTER = "failover"
DEFAULT_FAILBACK_TIMES = "3"
DEFAULT_FAILBACK_TIMES_INT = 3
DEFAULT_FAILBACK_TASKS = 100
)

const (
Expand Down
20 changes: 10 additions & 10 deletions config/base_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,22 @@ func Test_refresh(t *testing.T) {
Protocol: "mock",
Cluster: "failover",
Loadbalance: "random",
Retries: 3,
Retries: "3",
Group: "huadong_idc",
Version: "1.0.0",
Methods: []*MethodConfig{
{
InterfaceId: "MockService",
InterfaceName: "com.MockService",
Name: "GetUser",
Retries: 2,
Retries: "2",
Loadbalance: "random",
},
{
InterfaceId: "MockService",
InterfaceName: "com.MockService",
Name: "GetUser1",
Retries: 2,
Retries: "2",
Loadbalance: "random",
},
},
Expand All @@ -118,9 +118,9 @@ func Test_refresh(t *testing.T) {
c.SetFatherConfig(father)
c.fresh()
assert.Equal(t, "mock100", father.Registries["shanghai_reg1"].Protocol)
assert.Equal(t, int64(10), father.References["MockService"].Retries)
assert.Equal(t, "10", father.References["MockService"].Retries)

assert.Equal(t, int64(10), father.References["MockService"].Methods[0].Retries)
assert.Equal(t, "10", father.References["MockService"].Methods[0].Retries)
assert.Equal(t, &[]bool{false}[0], father.Check)
assert.Equal(t, "dubbo", father.ApplicationConfig.Name)
}
Expand Down Expand Up @@ -188,21 +188,21 @@ func Test_refreshProvider(t *testing.T) {
Protocol: "mock",
Cluster: "failover",
Loadbalance: "random",
Retries: 3,
Retries: "3",
Group: "huadong_idc",
Version: "1.0.0",
Methods: []*MethodConfig{
{
InterfaceId: "MockService",
InterfaceName: "com.MockService",
Name: "GetUser",
Retries: 2,
Retries: "2",
Loadbalance: "random",
},
{InterfaceId: "MockService",
InterfaceName: "com.MockService",
Name: "GetUser1",
Retries: 2,
Retries: "2",
Loadbalance: "random",
},
},
Expand All @@ -213,9 +213,9 @@ func Test_refreshProvider(t *testing.T) {
c.SetFatherConfig(father)
c.fresh()
assert.Equal(t, "mock100", father.Registries["shanghai_reg1"].Protocol)
assert.Equal(t, int64(10), father.Services["MockService"].Retries)
assert.Equal(t, "10", father.Services["MockService"].Retries)

assert.Equal(t, int64(10), father.Services["MockService"].Methods[0].Retries)
assert.Equal(t, "10", father.Services["MockService"].Methods[0].Retries)
assert.Equal(t, "dubbo", father.ApplicationConfig.Name)
assert.Equal(t, "20001", father.Protocols["jsonrpc1"].Port)
}
Expand Down
2 changes: 1 addition & 1 deletion config/method_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type MethodConfig struct {
InterfaceId string
InterfaceName string
Name string `yaml:"name" json:"name,omitempty" property:"name"`
Retries int64 `yaml:"retries" json:"retries,omitempty" property:"retries"`
Retries string `yaml:"retries" json:"retries,omitempty" property:"retries"`
Loadbalance string `yaml:"loadbalance" json:"loadbalance,omitempty" property:"loadbalance"`
Weight int64 `yaml:"weight" json:"weight,omitempty" property:"weight"`
}
Expand Down
6 changes: 3 additions & 3 deletions config/reference_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type ReferenceConfig struct {
Registry string `yaml:"registry" json:"registry,omitempty" property:"registry"`
Cluster string `yaml:"cluster" json:"cluster,omitempty" property:"cluster"`
Loadbalance string `yaml:"loadbalance" json:"loadbalance,omitempty" property:"loadbalance"`
Retries int64 `yaml:"retries" json:"retries,omitempty" property:"retries"`
Retries string `yaml:"retries" json:"retries,omitempty" property:"retries"`
Group string `yaml:"group" json:"group,omitempty" property:"group"`
Version string `yaml:"version" json:"version,omitempty" property:"version"`
Methods []*MethodConfig `yaml:"methods" json:"methods,omitempty" property:"methods"`
Expand Down Expand Up @@ -154,7 +154,7 @@ func (refconfig *ReferenceConfig) getUrlMap() url.Values {
urlMap.Set(constant.TIMESTAMP_KEY, strconv.FormatInt(time.Now().Unix(), 10))
urlMap.Set(constant.CLUSTER_KEY, refconfig.Cluster)
urlMap.Set(constant.LOADBALANCE_KEY, refconfig.Loadbalance)
urlMap.Set(constant.RETRIES_KEY, strconv.FormatInt(refconfig.Retries, 10))
urlMap.Set(constant.RETRIES_KEY, refconfig.Retries)
urlMap.Set(constant.GROUP_KEY, refconfig.Group)
urlMap.Set(constant.VERSION_KEY, refconfig.Version)
urlMap.Set(constant.GENERIC_KEY, strconv.FormatBool(refconfig.Generic))
Expand All @@ -180,7 +180,7 @@ func (refconfig *ReferenceConfig) getUrlMap() url.Values {

for _, v := range refconfig.Methods {
urlMap.Set("methods."+v.Name+"."+constant.LOADBALANCE_KEY, v.Loadbalance)
urlMap.Set("methods."+v.Name+"."+constant.RETRIES_KEY, strconv.FormatInt(v.Retries, 10))
urlMap.Set("methods."+v.Name+"."+constant.RETRIES_KEY, v.Retries)
}

return urlMap
Expand Down
6 changes: 3 additions & 3 deletions config/reference_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ func doInit() {
Protocol: "mock",
Cluster: "failover",
Loadbalance: "random",
Retries: 3,
Retries: "3",
Group: "huadong_idc",
Version: "1.0.0",
Methods: []*MethodConfig{
{
Name: "GetUser",
Retries: 2,
Retries: "2",
Loadbalance: "random",
},
{
Name: "GetUser1",
Retries: 2,
Retries: "2",
Loadbalance: "random",
},
},
Expand Down
6 changes: 3 additions & 3 deletions config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type ServiceConfig struct {
Version string `yaml:"version" json:"version,omitempty" property:"version" `
Methods []*MethodConfig `yaml:"methods" json:"methods,omitempty" property:"methods"`
Warmup string `yaml:"warmup" json:"warmup,omitempty" property:"warmup"`
Retries int64 `yaml:"retries" json:"retries,omitempty" property:"retries"`
Retries string `yaml:"retries" json:"retries,omitempty" property:"retries"`
Params map[string]string `yaml:"params" json:"params,omitempty" property:"params"`
unexported *atomic.Bool
exported *atomic.Bool
Expand Down Expand Up @@ -160,7 +160,7 @@ func (srvconfig *ServiceConfig) getUrlMap() url.Values {
urlMap.Set(constant.CLUSTER_KEY, srvconfig.Cluster)
urlMap.Set(constant.LOADBALANCE_KEY, srvconfig.Loadbalance)
urlMap.Set(constant.WARMUP_KEY, srvconfig.Warmup)
urlMap.Set(constant.RETRIES_KEY, strconv.FormatInt(srvconfig.Retries, 10))
urlMap.Set(constant.RETRIES_KEY, srvconfig.Retries)
urlMap.Set(constant.GROUP_KEY, srvconfig.Group)
urlMap.Set(constant.VERSION_KEY, srvconfig.Version)
urlMap.Set(constant.ROLE_KEY, strconv.Itoa(common.PROVIDER))
Expand All @@ -178,7 +178,7 @@ func (srvconfig *ServiceConfig) getUrlMap() url.Values {

for _, v := range srvconfig.Methods {
urlMap.Set("methods."+v.Name+"."+constant.LOADBALANCE_KEY, v.Loadbalance)
urlMap.Set("methods."+v.Name+"."+constant.RETRIES_KEY, strconv.FormatInt(v.Retries, 10))
urlMap.Set("methods."+v.Name+"."+constant.RETRIES_KEY, v.Retries)
urlMap.Set("methods."+v.Name+"."+constant.WEIGHT_KEY, strconv.FormatInt(v.Weight, 10))
}

Expand Down
6 changes: 3 additions & 3 deletions config/service_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ func doinit() {
Registry: "shanghai_reg1,shanghai_reg2,hangzhou_reg1,hangzhou_reg2",
Cluster: "failover",
Loadbalance: "random",
Retries: 3,
Retries: "3",
Group: "huadong_idc",
Version: "1.0.0",
Methods: []*MethodConfig{
{
Name: "GetUser",
Retries: 2,
Retries: "2",
Loadbalance: "random",
Weight: 200,
},
{
Name: "GetUser1",
Retries: 2,
Retries: "2",
Loadbalance: "random",
Weight: 200,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ references:
cluster: "failover"
methods :
- name: "GetUser"
retries: 3
retries: "3"

protocol_conf:
dubbo:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ references:
cluster: "failover"
methods :
- name: "GetUser"
retries: 3
retries: "3"

protocol_conf:
dubbo:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ references:
cluster: "failover"
methods :
- name: "GetUser"
retries: 3
retries: "3"

protocol_conf:
dubbo:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
cluster: "failover"
methods:
- name: "GetUser"
retries: 1
retries: "1"
loadbalance: "random"

protocol_conf:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
cluster: "failover"
methods:
- name: "GetUser"
retries: 1
retries: "1"
loadbalance: "random"

protocol_conf:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
cluster: "failover"
methods:
- name: "GetUser"
retries: 1
retries: "1"
loadbalance: "random"

protocol_conf:
Expand Down
2 changes: 1 addition & 1 deletion examples/consul/go-client/config/client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ references:
cluster: "failover"
methods :
- name: "SayHello"
retries: 3
retries: "3"

protocol_conf:
dubbo:
Expand Down
2 changes: 1 addition & 1 deletion examples/consul/go-server/config/server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
cluster: "failover"
methods:
- name: "SayHello"
retries: 1
retries: "1"
loadbalance: "random"

protocols:
Expand Down
Loading