You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2023/07/01 01:13:13 UTC

[dubbo-go] branch main updated: fix: solve config bool field zero value bug by using pointer (#2344)

This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/main by this push:
     new ff1e96417 fix: solve config bool field zero value bug by using pointer (#2344)
ff1e96417 is described below

commit ff1e96417e3e573df847054880635bdad82758d5
Author: Wang Guan <wg...@gmail.com>
AuthorDate: Sat Jul 1 09:13:08 2023 +0800

    fix: solve config bool field zero value bug by using pointer (#2344)
    
    fixes #2336
---
 cluster/router/condition/dynamic_router.go |  4 +-
 cluster/router/tag/match.go                |  2 +-
 cluster/router/tag/router.go               |  7 +--
 cluster/router/tag/router_test.go          | 87 ++++++++++++++++--------------
 config/provider_config.go                  |  4 +-
 config/router_config.go                    | 16 +++---
 config/tracing_config.go                   |  2 +-
 config/tracing_config_test.go              |  2 +-
 protocol/dubbo3/dubbo3_invoker.go          |  2 +-
 protocol/dubbo3/dubbo3_protocol.go         |  2 +-
 10 files changed, 67 insertions(+), 61 deletions(-)

diff --git a/cluster/router/condition/dynamic_router.go b/cluster/router/condition/dynamic_router.go
index 9d613dfe4..6d89771b3 100644
--- a/cluster/router/condition/dynamic_router.go
+++ b/cluster/router/condition/dynamic_router.go
@@ -88,8 +88,8 @@ func generateConditions(routerConfig *config.RouterConfig) ([]*StateRouter, erro
 			return nil, err
 		}
 		url.AddParam(constant.RuleKey, conditionRule)
-		url.AddParam(constant.ForceKey, strconv.FormatBool(routerConfig.Force))
-		url.AddParam(constant.EnabledKey, strconv.FormatBool(routerConfig.Enabled))
+		url.AddParam(constant.ForceKey, strconv.FormatBool(*routerConfig.Force))
+		url.AddParam(constant.EnabledKey, strconv.FormatBool(*routerConfig.Enabled))
 		conditionRoute, err := NewConditionStateRouter(url)
 		if err != nil {
 			return nil, err
diff --git a/cluster/router/tag/match.go b/cluster/router/tag/match.go
index f9f900e03..14633606a 100644
--- a/cluster/router/tag/match.go
+++ b/cluster/router/tag/match.go
@@ -117,7 +117,7 @@ func requestTag(invokers []protocol.Invoker, url *common.URL, invocation protoco
 		logger.Debugf("[tag router] filter dynamic tag address, invokers=%+v", result)
 	}
 	// returns the result directly
-	if cfg.Force || requestIsForce(url, invocation) {
+	if *cfg.Force || requestIsForce(url, invocation) {
 		return result
 	}
 	if len(result) != 0 {
diff --git a/cluster/router/tag/router.go b/cluster/router/tag/router.go
index 7561cf1e0..3475d7f18 100644
--- a/cluster/router/tag/router.go
+++ b/cluster/router/tag/router.go
@@ -59,7 +59,7 @@ func (p *PriorityRouter) Route(invokers []protocol.Invoker, url *common.URL, inv
 		return staticTag(invokers, url, invocation)
 	}
 	routerCfg := value.(config.RouterConfig)
-	if !routerCfg.Enabled || !routerCfg.Valid {
+	if !*routerCfg.Enabled || !*routerCfg.Valid {
 		return staticTag(invokers, url, invocation)
 	}
 	return dynamicTag(invokers, url, invocation, routerCfg)
@@ -119,9 +119,10 @@ func parseRoute(routeContent string) (*config.RouterConfig, error) {
 	if err != nil {
 		return nil, err
 	}
-	routerConfig.Valid = true
+	routerConfig.Valid = new(bool)
+	*routerConfig.Valid = true
 	if len(routerConfig.Tags) == 0 {
-		routerConfig.Valid = false
+		*routerConfig.Valid = false
 	}
 	return routerConfig, nil
 }
diff --git a/cluster/router/tag/router_test.go b/cluster/router/tag/router_test.go
index 8a29503ac..cb270a1bb 100644
--- a/cluster/router/tag/router_test.go
+++ b/cluster/router/tag/router_test.go
@@ -18,6 +18,7 @@
 package tag
 
 import (
+	"strings"
 	"testing"
 )
 
@@ -53,6 +54,10 @@ func initUrl() {
 
 func TestRouter(t *testing.T) {
 	initUrl()
+	trueValue := true
+	truePointer := &trueValue
+	falseValue := false
+	falsePointer := &falseValue
 	t.Run("staticEmptyTag", func(t *testing.T) {
 		p, err := NewTagPriorityRouter()
 		assert.Nil(t, err)
@@ -129,11 +134,11 @@ func TestRouter(t *testing.T) {
 	t.Run("dynamicEmptyTag_requestEmptyTag", func(t *testing.T) {
 		p, err := NewTagPriorityRouter()
 		assert.Nil(t, err)
-		p.routerConfigs.Store(consumerUrl.GetParam(constant.ApplicationKey, "")+constant.TagRouterRuleSuffix, config.RouterConfig{
-			Key:     consumerUrl.GetParam(constant.ApplicationKey, "") + constant.TagRouterRuleSuffix,
-			Force:   false,
-			Enabled: true,
-			Valid:   true,
+		p.routerConfigs.Store(consumerUrl.Service()+constant.TagRouterRuleSuffix, config.RouterConfig{
+			Key:     consumerUrl.Service() + constant.TagRouterRuleSuffix,
+			Force:   falsePointer,
+			Enabled: truePointer,
+			Valid:   truePointer,
 		})
 		ivk := protocol.NewBaseInvoker(url1)
 		ivk1 := protocol.NewBaseInvoker(url2)
@@ -149,11 +154,11 @@ func TestRouter(t *testing.T) {
 	t.Run("dynamicEmptyTag_requestHasTag", func(t *testing.T) {
 		p, err := NewTagPriorityRouter()
 		assert.Nil(t, err)
-		p.routerConfigs.Store(consumerUrl.GetParam(constant.ApplicationKey, "")+constant.TagRouterRuleSuffix, config.RouterConfig{
-			Key:     consumerUrl.GetParam(constant.ApplicationKey, "") + constant.TagRouterRuleSuffix,
-			Force:   false,
-			Enabled: true,
-			Valid:   true,
+		p.routerConfigs.Store(consumerUrl.Service()+constant.TagRouterRuleSuffix, config.RouterConfig{
+			Key:     consumerUrl.Service() + constant.TagRouterRuleSuffix,
+			Force:   falsePointer,
+			Enabled: truePointer,
+			Valid:   truePointer,
 		})
 		ivk := protocol.NewBaseInvoker(url1)
 		ivk1 := protocol.NewBaseInvoker(url2)
@@ -170,11 +175,11 @@ func TestRouter(t *testing.T) {
 	t.Run("dynamicTag_requestEmptyTag", func(t *testing.T) {
 		p, err := NewTagPriorityRouter()
 		assert.Nil(t, err)
-		p.routerConfigs.Store(consumerUrl.GetParam(constant.ApplicationKey, "")+constant.TagRouterRuleSuffix, config.RouterConfig{
-			Key:     consumerUrl.GetParam(constant.ApplicationKey, "") + constant.TagRouterRuleSuffix,
-			Force:   false,
-			Enabled: true,
-			Valid:   true,
+		p.routerConfigs.Store(strings.Join([]string{consumerUrl.GetParam(constant.ApplicationKey, ""), constant.TagRouterRuleSuffix}, ""), config.RouterConfig{
+			Key:     consumerUrl.Service() + constant.TagRouterRuleSuffix,
+			Force:   falsePointer,
+			Enabled: truePointer,
+			Valid:   truePointer,
 			Tags: []config.Tag{{
 				Name:      "tag",
 				Addresses: []string{"192.168.0.3:20000"},
@@ -194,11 +199,11 @@ func TestRouter(t *testing.T) {
 	t.Run("dynamicTag_emptyAddress_requestHasTag", func(t *testing.T) {
 		p, err := NewTagPriorityRouter()
 		assert.Nil(t, err)
-		p.routerConfigs.Store(consumerUrl.GetParam(constant.ApplicationKey, "")+constant.TagRouterRuleSuffix, config.RouterConfig{
-			Key:     consumerUrl.GetParam(constant.ApplicationKey, "") + constant.TagRouterRuleSuffix,
-			Force:   false,
-			Enabled: true,
-			Valid:   true,
+		p.routerConfigs.Store(consumerUrl.Service()+constant.TagRouterRuleSuffix, config.RouterConfig{
+			Key:     consumerUrl.Service() + constant.TagRouterRuleSuffix,
+			Force:   falsePointer,
+			Enabled: truePointer,
+			Valid:   truePointer,
 			Tags: []config.Tag{{
 				Name: "tag",
 			}},
@@ -218,11 +223,11 @@ func TestRouter(t *testing.T) {
 	t.Run("dynamicTag_address_requestHasTag", func(t *testing.T) {
 		p, err := NewTagPriorityRouter()
 		assert.Nil(t, err)
-		p.routerConfigs.Store(consumerUrl.GetParam(constant.ApplicationKey, "")+constant.TagRouterRuleSuffix, config.RouterConfig{
-			Key:     consumerUrl.GetParam(constant.ApplicationKey, "") + constant.TagRouterRuleSuffix,
-			Force:   false,
-			Enabled: true,
-			Valid:   true,
+		p.routerConfigs.Store(strings.Join([]string{consumerUrl.GetParam(constant.ApplicationKey, ""), constant.TagRouterRuleSuffix}, ""), config.RouterConfig{
+			Key:     consumerUrl.Service() + constant.TagRouterRuleSuffix,
+			Force:   falsePointer,
+			Enabled: truePointer,
+			Valid:   truePointer,
 			Tags: []config.Tag{{
 				Name:      "tag",
 				Addresses: []string{"192.168.0.3:20000"},
@@ -243,11 +248,11 @@ func TestRouter(t *testing.T) {
 	t.Run("dynamicTag_twoAddress_requestHasTag", func(t *testing.T) {
 		p, err := NewTagPriorityRouter()
 		assert.Nil(t, err)
-		p.routerConfigs.Store(consumerUrl.GetParam(constant.ApplicationKey, "")+constant.TagRouterRuleSuffix, config.RouterConfig{
-			Key:     consumerUrl.GetParam(constant.ApplicationKey, "") + constant.TagRouterRuleSuffix,
-			Force:   false,
-			Enabled: true,
-			Valid:   true,
+		p.routerConfigs.Store(strings.Join([]string{consumerUrl.GetParam(constant.ApplicationKey, ""), constant.TagRouterRuleSuffix}, ""), config.RouterConfig{
+			Key:     consumerUrl.Service() + constant.TagRouterRuleSuffix,
+			Force:   falsePointer,
+			Enabled: truePointer,
+			Valid:   truePointer,
 			Tags: []config.Tag{{
 				Name:      "tag",
 				Addresses: []string{"192.168.0.1:20000", "192.168.0.3:20000"},
@@ -268,11 +273,11 @@ func TestRouter(t *testing.T) {
 	t.Run("dynamicTag_addressNotMatch_requestHasTag", func(t *testing.T) {
 		p, err := NewTagPriorityRouter()
 		assert.Nil(t, err)
-		p.routerConfigs.Store(consumerUrl.GetParam(constant.ApplicationKey, "")+constant.TagRouterRuleSuffix, config.RouterConfig{
-			Key:     consumerUrl.GetParam(constant.ApplicationKey, "") + constant.TagRouterRuleSuffix,
-			Force:   false,
-			Enabled: true,
-			Valid:   true,
+		p.routerConfigs.Store(consumerUrl.Service()+constant.TagRouterRuleSuffix, config.RouterConfig{
+			Key:     consumerUrl.Service() + constant.TagRouterRuleSuffix,
+			Force:   falsePointer,
+			Enabled: truePointer,
+			Valid:   truePointer,
 			Tags: []config.Tag{{
 				Name:      "tag",
 				Addresses: []string{"192.168.0.4:20000"},
@@ -293,11 +298,11 @@ func TestRouter(t *testing.T) {
 	t.Run("dynamicTag_notValid", func(t *testing.T) {
 		p, err := NewTagPriorityRouter()
 		assert.Nil(t, err)
-		p.routerConfigs.Store(consumerUrl.GetParam(constant.ApplicationKey, "")+constant.TagRouterRuleSuffix, config.RouterConfig{
-			Key:     consumerUrl.GetParam(constant.ApplicationKey, "") + constant.TagRouterRuleSuffix,
-			Force:   false,
-			Enabled: true,
-			Valid:   false,
+		p.routerConfigs.Store(consumerUrl.Service()+constant.TagRouterRuleSuffix, config.RouterConfig{
+			Key:     consumerUrl.Service() + constant.TagRouterRuleSuffix,
+			Force:   falsePointer,
+			Enabled: truePointer,
+			Valid:   falsePointer,
 			Tags: []config.Tag{{
 				Name:      "tag",
 				Addresses: []string{"192.168.0.1:20000", "192.168.0.3:20000"},
@@ -365,7 +370,7 @@ tags:
 		routerCfg := value.(config.RouterConfig)
 		assert.True(t, routerCfg.Key == "org.apache.dubbo.UserProvider.Test")
 		assert.True(t, len(routerCfg.Tags) == 2)
-		assert.True(t, routerCfg.Enabled)
+		assert.True(t, *routerCfg.Enabled)
 	})
 
 	t.Run("configNotValid", func(t *testing.T) {
diff --git a/config/provider_config.go b/config/provider_config.go
index 364319192..5ca41f33b 100644
--- a/config/provider_config.go
+++ b/config/provider_config.go
@@ -55,8 +55,8 @@ type ProviderConfig struct {
 	FilterConf   interface{}               `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf"`
 	ConfigType   map[string]string         `yaml:"config_type" json:"config_type,omitempty" property:"config_type"`
 	// adaptive service
-	AdaptiveService        bool `default:"false" yaml:"adaptive-service" json:"adaptive-service" property:"adaptive-service"`
-	AdaptiveServiceVerbose bool `default:"false" yaml:"adaptive-service-verbose" json:"adaptive-service-verbose" property:"adaptive-service-verbose"`
+	AdaptiveService        bool `yaml:"adaptive-service" json:"adaptive-service" property:"adaptive-service"`
+	AdaptiveServiceVerbose bool `yaml:"adaptive-service-verbose" json:"adaptive-service-verbose" property:"adaptive-service-verbose"`
 
 	rootConfig *RootConfig
 }
diff --git a/config/router_config.go b/config/router_config.go
index ed9babf64..616e994ea 100644
--- a/config/router_config.go
+++ b/config/router_config.go
@@ -31,10 +31,10 @@ import (
 type RouterConfig struct {
 	Scope      string   `validate:"required" yaml:"scope" json:"scope,omitempty" property:"scope"` // must be chosen from `service` and `application`.
 	Key        string   `validate:"required" yaml:"key" json:"key,omitempty" property:"key"`       // specifies which service or application the rule body acts on.
-	Force      bool     `default:"false" yaml:"force" json:"force,omitempty" property:"force"`
-	Runtime    bool     `default:"false" yaml:"runtime" json:"runtime,omitempty" property:"runtime"`
-	Enabled    bool     `default:"true" yaml:"enabled" json:"enabled,omitempty" property:"enabled"`
-	Valid      bool     `default:"true" yaml:"valid" json:"valid,omitempty" property:"valid"`
+	Force      *bool    `default:"false" yaml:"force" json:"force,omitempty" property:"force"`
+	Runtime    *bool    `default:"false" yaml:"runtime" json:"runtime,omitempty" property:"runtime"`
+	Enabled    *bool    `default:"true" yaml:"enabled" json:"enabled,omitempty" property:"enabled"`
+	Valid      *bool    `default:"true" yaml:"valid" json:"valid,omitempty" property:"valid"`
 	Priority   int      `default:"0" yaml:"priority" json:"priority,omitempty" property:"priority"`
 	Conditions []string `yaml:"conditions" json:"conditions,omitempty" property:"conditions"`
 	Tags       []Tag    `yaml:"tags" json:"tags,omitempty" property:"tags"`
@@ -91,22 +91,22 @@ func (rcb *RouterConfigBuilder) SetKey(key string) *RouterConfigBuilder {
 }
 
 func (rcb *RouterConfigBuilder) SetForce(force bool) *RouterConfigBuilder {
-	rcb.routerConfig.Force = force
+	rcb.routerConfig.Force = &force
 	return rcb
 }
 
 func (rcb *RouterConfigBuilder) SetRuntime(runtime bool) *RouterConfigBuilder {
-	rcb.routerConfig.Runtime = runtime
+	rcb.routerConfig.Runtime = &runtime
 	return rcb
 }
 
 func (rcb *RouterConfigBuilder) SetEnabled(enabled bool) *RouterConfigBuilder {
-	rcb.routerConfig.Enabled = enabled
+	rcb.routerConfig.Enabled = &enabled
 	return rcb
 }
 
 func (rcb *RouterConfigBuilder) SetValid(valid bool) *RouterConfigBuilder {
-	rcb.routerConfig.Valid = valid
+	rcb.routerConfig.Valid = &valid
 	return rcb
 }
 
diff --git a/config/tracing_config.go b/config/tracing_config.go
index 9807aff31..b79aef6cc 100644
--- a/config/tracing_config.go
+++ b/config/tracing_config.go
@@ -30,7 +30,7 @@ type TracingConfig struct {
 	Name        string `default:"jaeger" yaml:"name" json:"name,omitempty" property:"name"` // jaeger or zipkin(todo)
 	ServiceName string `yaml:"serviceName" json:"serviceName,omitempty" property:"serviceName"`
 	Address     string `yaml:"address" json:"address,omitempty" property:"address"`
-	UseAgent    bool   `default:"false" yaml:"use-agent" json:"use-agent,omitempty" property:"use-agent"`
+	UseAgent    *bool  `default:"false" yaml:"use-agent" json:"use-agent,omitempty" property:"use-agent"`
 }
 
 // Prefix dubbo.router
diff --git a/config/tracing_config_test.go b/config/tracing_config_test.go
index d21f3d1dd..0a04ea315 100644
--- a/config/tracing_config_test.go
+++ b/config/tracing_config_test.go
@@ -36,5 +36,5 @@ func TestTracingConfig(t *testing.T) {
 	assert.Nil(t, err)
 	assert.Equal(t, tracing.Prefix(), constant.TracingConfigPrefix)
 	assert.Equal(t, tracing.Name, "jaeger")
-	assert.Equal(t, tracing.UseAgent, false)
+	assert.Equal(t, *tracing.UseAgent, false)
 }
diff --git a/protocol/dubbo3/dubbo3_invoker.go b/protocol/dubbo3/dubbo3_invoker.go
index 327b58d3b..af6d9fa21 100644
--- a/protocol/dubbo3/dubbo3_invoker.go
+++ b/protocol/dubbo3/dubbo3_invoker.go
@@ -108,7 +108,7 @@ func NewDubboInvoker(url *common.URL) (*DubboInvoker, error) {
 				opts = append(opts, triConfig.WithJaegerConfig(
 					tracingConfig.Address,
 					tracingConfig.ServiceName,
-					tracingConfig.UseAgent,
+					*tracingConfig.UseAgent,
 				))
 			} else {
 				logger.Warnf("unsupported tracing name %s, now triple only support jaeger", tracingConfig.Name)
diff --git a/protocol/dubbo3/dubbo3_protocol.go b/protocol/dubbo3/dubbo3_protocol.go
index 66caf2836..8d824b95d 100644
--- a/protocol/dubbo3/dubbo3_protocol.go
+++ b/protocol/dubbo3/dubbo3_protocol.go
@@ -235,7 +235,7 @@ func (dp *DubboProtocol) openServer(url *common.URL, tripleCodecType tripleConst
 				opts = append(opts, triConfig.WithJaegerConfig(
 					tracingConfig.Address,
 					tracingConfig.ServiceName,
-					tracingConfig.UseAgent,
+					*tracingConfig.UseAgent,
 				))
 			default:
 				logger.Warnf("unsupported tracing name %s, now triple only support jaeger", tracingConfig.Name)