Skip to content

Commit

Permalink
Fix ZK BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Jul 5, 2020
1 parent e90fac6 commit d3aab02
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
36 changes: 18 additions & 18 deletions config/base_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ func TestRefresh(t *testing.T) {
config.GetEnvInstance().UpdateExternalConfigMap(mockMap)

father := &ConsumerConfig{
Check: &[]bool{true}[0],
Check: &[]bool{true}[0],
BaseConfig: BaseConfig{
ApplicationConfig:baseAppConfig,
ApplicationConfig: baseAppConfig,
},
Registries: baseRegistries,
References: baseMockRef,
Registries: baseRegistries,
References: baseMockRef,
ShutdownConfig: &ShutdownConfig{
Timeout: "12s",
StepTimeout: "2s",
Expand Down Expand Up @@ -150,12 +150,12 @@ func TestAppExternalRefresh(t *testing.T) {
mockMap["dubbo.consumer.check"] = "true"
config.GetEnvInstance().UpdateExternalConfigMap(mockMap)
father := &ConsumerConfig{
Check: &[]bool{true}[0],
Check: &[]bool{true}[0],
BaseConfig: BaseConfig{
ApplicationConfig:baseAppConfig,
ApplicationConfig: baseAppConfig,
},
Registries: baseRegistries,
References: baseMockRef,
Registries: baseRegistries,
References: baseMockRef,
}

c.SetFatherConfig(father)
Expand All @@ -178,12 +178,12 @@ func TestAppExternalWithoutIDRefresh(t *testing.T) {
mockMap["dubbo.consumer.check"] = "true"
config.GetEnvInstance().UpdateExternalConfigMap(mockMap)
father := &ConsumerConfig{
Check: &[]bool{true}[0],
Check: &[]bool{true}[0],
BaseConfig: BaseConfig{
ApplicationConfig:baseAppConfig,
ApplicationConfig: baseAppConfig,
},
Registries: baseRegistries,
References: baseMockRef,
Registries: baseRegistries,
References: baseMockRef,
}

c.SetFatherConfig(father)
Expand All @@ -208,13 +208,13 @@ func TestRefreshSingleRegistry(t *testing.T) {
config.GetEnvInstance().UpdateExternalConfigMap(mockMap)

father := &ConsumerConfig{
Check: &[]bool{true}[0],
Check: &[]bool{true}[0],
BaseConfig: BaseConfig{
ApplicationConfig: baseAppConfig,
ApplicationConfig: baseAppConfig,
},
Registries: map[string]*RegistryConfig{},
Registry: &RegistryConfig{},
References: baseMockRef,
Registries: map[string]*RegistryConfig{},
Registry: &RegistryConfig{},
References: baseMockRef,
}

c.SetFatherConfig(father)
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestRefreshProvider(t *testing.T) {
BaseConfig: BaseConfig{
ApplicationConfig: baseAppConfig,
},
Registries: baseRegistries,
Registries: baseRegistries,
Services: map[string]*ServiceConfig{
"MockService": {
InterfaceName: "com.MockService",
Expand Down
4 changes: 4 additions & 0 deletions remoting/zookeeper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ func (z *ZookeeperClient) CreateWithValue(basePath string, value []byte) error {

// CreateTempWithValue will create the node recursively, which means that if the parent node is absent,
// it will create parent node first,and set value in last child path
// If the path exist, it will update data
func (z *ZookeeperClient) CreateTempWithValue(basePath string, value []byte) error {
var (
err error
Expand All @@ -453,6 +454,9 @@ func (z *ZookeeperClient) CreateTempWithValue(basePath string, value []byte) err
// last child need be ephemeral
if i == length-1 {
_, err = conn.Create(tmpPath, value, zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
if err == zk.ErrNodeExists {
return err
}
} else {
_, err = conn.Create(tmpPath, []byte{}, 0, zk.WorldACL(zk.PermAll))
}
Expand Down
12 changes: 12 additions & 0 deletions remoting/zookeeper/curator_discovery/service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"path"
"strings"
"sync"

"github.com/dubbogo/go-zookeeper/zk"
)

import (
Expand Down Expand Up @@ -71,6 +73,16 @@ func (sd *ServiceDiscovery) registerService(instance *ServiceInstance) error {
return err
}
err = sd.client.CreateTempWithValue(path, data)
if err == zk.ErrNodeExists {
_, state, _ := sd.client.GetContent(path)
if state != nil {
_, err = sd.client.SetContent(path, data, state.Version+1)
if err != nil {
logger.Debugf("Try to update the node data failed. In most cases, it's not a problem. ")
}
}
return nil
}
if err != nil {
return err
}
Expand Down

0 comments on commit d3aab02

Please sign in to comment.