Skip to content

Commit b4d9662

Browse files
committed
fix(zookeeper): align listener paths and empty config events
1 parent 9a59b60 commit b4d9662

4 files changed

Lines changed: 39 additions & 13 deletions

File tree

config_center/zookeeper/impl.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,8 @@ func newZookeeperDynamicConfiguration(url *common.URL) (*zookeeperDynamicConfigu
110110
}
111111

112112
// AddListener add listener for key
113-
// TODO this method should has a parameter 'group', and it does not now, so we should concat group and key with '/' manually
114113
func (c *zookeeperDynamicConfiguration) AddListener(key string, listener config_center.ConfigurationListener, options ...config_center.Option) {
115-
key = strings.Join([]string{c.GetURL().GetParam(constant.ConfigNamespaceKey, config_center.DefaultGroup), key}, "/")
116-
qualifiedKey := buildPath(c.rootPath, key)
114+
qualifiedKey := c.getPropertiesPath(key, options...)
117115
c.cacheListener.AddListener(qualifiedKey, listener)
118116
}
119117

@@ -128,8 +126,7 @@ func buildPath(rootPath, subPath string) string {
128126
}
129127

130128
func (c *zookeeperDynamicConfiguration) RemoveListener(key string, listener config_center.ConfigurationListener, options ...config_center.Option) {
131-
key = strings.Join([]string{c.GetURL().GetParam(constant.ConfigNamespaceKey, config_center.DefaultGroup), key}, "/")
132-
qualifiedKey := buildPath(c.rootPath, key)
129+
qualifiedKey := c.getPropertiesPath(key, options...)
133130
c.cacheListener.RemoveListener(qualifiedKey, listener)
134131
}
135132

config_center/zookeeper/impl_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,37 @@ func TestLoadPropertiesRegistersWatchOnlyWhenInactive(t *testing.T) {
183183
require.True(t, waitForEvent(inactivePath, time.Second))
184184
}
185185

186+
func TestListenerUsesGroupOption(t *testing.T) {
187+
cluster, client, _, err := gxzookeeper.NewMockZookeeperClient("listener-group", 5*time.Second)
188+
if err != nil {
189+
t.Skipf("skip mock zk setup: %v", err)
190+
}
191+
defer cluster.Stop()
192+
193+
zkListener := remotingzookeeper.NewZkEventListener(client)
194+
defer zkListener.Close()
195+
cfg := &zookeeperDynamicConfiguration{
196+
rootPath: "/dubbo/config",
197+
client: client,
198+
url: mustURL(t, "registry://127.0.0.1:2181"),
199+
cache: newConfigCache(time.Minute),
200+
listener: zkListener,
201+
}
202+
cfg.cacheListener = newCacheListener(cfg.rootPath, zkListener, &cfg.cache)
203+
key := "app.properties"
204+
group := "custom"
205+
path := cfg.getPropertiesPath(key, config_center.WithGroup(group))
206+
rec := &recListener{}
207+
208+
cfg.AddListener(key, rec, config_center.WithGroup(group))
209+
_, ok := cfg.cacheListener.keyListeners.Load(path)
210+
require.True(t, ok)
211+
212+
cfg.RemoveListener(key, rec, config_center.WithGroup(group))
213+
_, ok = cfg.cacheListener.keyListeners.Load(path)
214+
require.False(t, ok)
215+
}
216+
186217
func TestGetPropertiesFallsBackToTTLAtAutoWatchLimit(t *testing.T) {
187218
cluster, client, events, err := gxzookeeper.NewMockZookeeperClient("watch-limit", 5*time.Second)
188219
if err != nil {

config_center/zookeeper/listener.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,14 @@ func (l *CacheListener) DataChange(event remoting.Event) bool {
203203
}
204204
}
205205

206-
changeType := event.Action
207-
if event.Content == "" {
208-
changeType = remoting.EventTypeDel
209-
}
210-
211206
key, group := l.pathToKeyGroup(event.Path)
212-
defer metrics.Publish(metricsConfigCenter.NewIncMetricEvent(key, group, changeType, metricsConfigCenter.Zookeeper))
207+
defer metrics.Publish(metricsConfigCenter.NewIncMetricEvent(key, group, event.Action, metricsConfigCenter.Zookeeper))
213208
if listeners, ok := l.keyListeners.Load(event.Path); ok {
214209
for listener := range listeners.(map[config_center.ConfigurationListener]struct{}) {
215210
listener.Process(&config_center.ConfigChangeEvent{
216211
Key: key,
217212
Value: event.Content,
218-
ConfigType: changeType,
213+
ConfigType: event.Action,
219214
})
220215
}
221216
return true

config_center/zookeeper/listener_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestCacheListenerDataChangeEmptyContent(t *testing.T) {
6969
if !ok {
7070
t.Fatalf("expected listeners to be notified")
7171
}
72-
if len(rec.events) != 1 || rec.events[0].ConfigType != remoting.EventTypeDel {
72+
if len(rec.events) != 1 || rec.events[0].ConfigType != remoting.EventTypeAdd {
7373
t.Fatalf("unexpected events %+v", rec.events)
7474
}
7575
entry, ok := cache.getFresh(path)
@@ -78,6 +78,9 @@ func TestCacheListenerDataChangeEmptyContent(t *testing.T) {
7878
}
7979

8080
l.DataChange(remoting.Event{Path: path, Action: remoting.EventTypeDel})
81+
if len(rec.events) != 2 || rec.events[1].ConfigType != remoting.EventTypeDel {
82+
t.Fatalf("unexpected events %+v", rec.events)
83+
}
8184
entry, ok = cache.getFresh(path)
8285
if !ok || entry.exists {
8386
t.Fatalf("deleted configuration should be cached as missing: %+v", entry)

0 commit comments

Comments
 (0)