Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(metadata): address review feedback on mapping consistency (#3354)
- nacos: stop swallowing the getConfig read error. On a failed read the old
  value was treated as empty, so registration would publish only the current
  app and overwrite an existing set (e.g. appA,appB -> appC). Return the error
  instead so an existing mapping is never clobbered. A genuinely absent config
  still returns ("", nil) and takes the first-write path.
- zookeeper: CacheListener.DataChange now builds the set via
  report.DecodeServiceAppNames, so mapping change events no longer surface
  empty app names from legacy/malformed comma-separated values (",app",
  "app,,other"). Added a listener test covering this.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
  • Loading branch information
NeverENG and claude committed Jun 7, 2026
commit 03fc739a3c8d2529f0e2d5d7ba51a155c2c6017d
7 changes: 6 additions & 1 deletion metadata/report/nacos/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,15 @@ func (n *nacosMetadataReport) removeServiceMappingListener(key string, group str

// RegisterServiceAppMapping map the specified Dubbo service interface to current Dubbo app name
func (n *nacosMetadataReport) RegisterServiceAppMapping(key string, group string, value string) error {
oldVal, _ := n.getConfig(vo.ConfigParam{
oldVal, err := n.getConfig(vo.ConfigParam{
DataId: key,
Group: group,
})
if err != nil {
// Do not treat a read failure as an empty value: that would publish only our app and
// overwrite an existing set. Fail instead so the mapping is never clobbered.
return err
}
merged, changed := report.MergeServiceAppMapping(oldVal, value)
if !changed {
return nil
Expand Down
8 changes: 2 additions & 6 deletions metadata/report/zookeeper/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (
)

import (
gxset "github.com/dubbogo/gost/container/set"
"github.com/dubbogo/gost/log/logger"
)

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/metadata/mapping"
"dubbo.apache.org/dubbo-go/v3/metadata/report"
"dubbo.apache.org/dubbo-go/v3/registry"
"dubbo.apache.org/dubbo-go/v3/remoting"
"dubbo.apache.org/dubbo-go/v3/remoting/zookeeper"
Expand Down Expand Up @@ -128,11 +128,7 @@ func (l *CacheListener) RemoveKeyListeners(key string) {
// DataChange changes all listeners' event
func (l *CacheListener) DataChange(event remoting.Event) bool {
if listeners, ok := l.keyListeners.Load(event.Path); ok {
appNames := strings.Split(event.Content, constant.CommaSeparator)
set := gxset.NewSet()
for _, e := range appNames {
set.Add(e)
}
set := report.DecodeServiceAppNames(event.Content)
err := listeners.(*ListenerSet).ForEach(func(listener mapping.MappingListener) error {
return listener.OnEvent(registry.NewServiceMappingChangedEvent(l.pathToKey(event.Path), set))
})
Expand Down
26 changes: 26 additions & 0 deletions metadata/report/zookeeper/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

import (
gxset "github.com/dubbogo/gost/container/set"
"github.com/dubbogo/gost/gof/observer"

"github.com/stretchr/testify/assert"
Expand All @@ -36,13 +37,15 @@ import (

import (
"dubbo.apache.org/dubbo-go/v3/metadata/mapping"
"dubbo.apache.org/dubbo-go/v3/registry"
"dubbo.apache.org/dubbo-go/v3/remoting"
)

type mockMappingListener struct {
eventCount atomic.Int32
mu sync.Mutex
onEventErr error
lastNames *gxset.HashSet
}

func newMockMappingListener() *mockMappingListener { return &mockMappingListener{} }
Expand All @@ -51,6 +54,9 @@ func (m *mockMappingListener) OnEvent(e observer.Event) error {
m.mu.Lock()
defer m.mu.Unlock()
m.eventCount.Add(1)
if sm, ok := e.(*registry.ServiceMappingChangeEvent); ok {
m.lastNames = sm.GetServiceNames()
}
return m.onEventErr
}

Expand Down Expand Up @@ -164,3 +170,23 @@ func TestCacheListenerPathToKey(t *testing.T) {
assert.Equal(t, "com.example.Service", cl.pathToKey("/dubbo/mapping/com.example.Service"))
assert.Empty(t, cl.pathToKey(""))
}

// TestCacheListenerDataChangeFiltersEmptyAppNames ensures a change event built from a
// malformed/legacy comma-separated value (stray separators) does not surface empty app names
// to the listener.
func TestCacheListenerDataChangeFiltersEmptyAppNames(t *testing.T) {
cl := NewCacheListener("/dubbo", nil)
key := "/dubbo/mapping/com.example.Service"

ml := newMockMappingListener()
listenerSet := NewListenerSet()
listenerSet.Add(ml)
cl.keyListeners.Store(key, listenerSet)

assert.True(t, cl.DataChange(remoting.Event{Path: key, Action: remoting.EventTypeUpdate, Content: ",appA,,appB,"}))
require.NotNil(t, ml.lastNames)
assert.Equal(t, 2, ml.lastNames.Size())
assert.True(t, ml.lastNames.Contains("appA"))
assert.True(t, ml.lastNames.Contains("appB"))
assert.False(t, ml.lastNames.Contains(""))
}
Loading