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 review
  • Loading branch information
eye-gu committed Jun 10, 2026
commit 48e2c1d2b8fcbb37e885afdfef87666a32ecb31d
16 changes: 16 additions & 0 deletions metadata/info/metadata_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,22 @@ func (info *MetadataInfo) ReplaceExportedServices(urls []*common.URL) {
}
}

// Snapshot creates a deep copy of the MetadataInfo for safe concurrent access.
// The caller can modify the snapshot without affecting the original.
func (info *MetadataInfo) Snapshot() MetadataInfo {
services := make(map[string]*ServiceInfo, len(info.Services))
for k, v := range info.Services {
si := *v
services[k] = &si
}
return MetadataInfo{
App: info.App,
Revision: info.Revision,
Tag: info.Tag,
Services: services,
}
}

func (info *MetadataInfo) findExportedServiceURL(matchKey string) *common.URL {
for _, urls := range info.exportedServiceURLs {
for _, serviceURL := range urls {
Expand Down
5 changes: 5 additions & 0 deletions metadata/mapping/metadata/service_name_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,8 @@ func (m *mockMetadataReport) ListAppRevisions(string) ([]report.AppRevision, err
args := m.Called()
return args.Get(0).([]report.AppRevision), args.Error(1)
}

func (m *mockMetadataReport) URL() *common.URL {
u, _ := common.NewURL("mock://127.0.0.1:8848")
return u
}
29 changes: 14 additions & 15 deletions metadata/report/etcd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,22 @@ package etcd

import (
"encoding/json"
"errors"
"fmt"
"strings"
)

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

gxetcd "github.com/dubbogo/gost/database/kv/etcd/v3"
"github.com/dubbogo/gost/log/logger"

perrors "github.com/pkg/errors"
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/metadata/info"
"dubbo.apache.org/dubbo-go/v3/metadata/mapping"
"dubbo.apache.org/dubbo-go/v3/metadata/report"
perrors "github.com/pkg/errors"
)

// etcdClient abstracts the etcd Client operations used by etcdMetadataReport.
Expand Down Expand Up @@ -69,6 +66,12 @@ func init() {
type etcdMetadataReport struct {
client etcdClient
rootDir string
url *common.URL
}

// URL returns the URL used to create this metadata report.
func (e *etcdMetadataReport) URL() *common.URL {
return e.url
}

// GetAppMetadata get metadata info from etcd
Expand Down Expand Up @@ -151,25 +154,21 @@ func (e *etcdMetadataReport) UnPublishAppMetadata(application, revision string)

func (e *etcdMetadataReport) ListAppRevisions(application string) ([]report.AppRevision, error) {
prefix := e.rootDir + constant.PathSeparator + application + constant.PathSeparator
keys, _, err := e.client.GetChildren(prefix)
keys, values, err := e.client.GetChildren(prefix)
if err != nil {
if perrors.Cause(err) == gxetcd.ErrKVPairNotFound {
if errors.Is(perrors.Cause(err), gxetcd.ErrKVPairNotFound) {
return nil, nil
}
return nil, err
}

result := make([]report.AppRevision, 0, len(keys))
for _, key := range keys {
for i, key := range keys {
// Extract revision from key suffix (key is full path, revision is last segment)
revision := key[strings.LastIndex(key, constant.PathSeparator)+1:]
val, err := e.client.Get(key)
if err != nil {
continue // skip if key disappeared between listing and reading
}
result = append(result, report.AppRevision{
Revision: revision,
ModifyTime: report.ParseMetadataLastUpdatedTime([]byte(val)),
ModifyTime: report.ParseMetadataLastUpdatedTime([]byte(values[i])),
})
}
return result, nil
Expand All @@ -188,5 +187,5 @@ func (e *etcdMetadataReportFactory) CreateMetadataReport(url *common.URL) report
}
group := url.GetParam(constant.MetadataReportGroupKey, DEFAULT_ROOT)
group = constant.PathSeparator + strings.TrimPrefix(group, constant.PathSeparator)
return &etcdMetadataReport{client: etcdClientWrapper{client}, rootDir: group}
return &etcdMetadataReport{client: etcdClientWrapper{client}, rootDir: group, url: url}
}
74 changes: 40 additions & 34 deletions metadata/report/nacos/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func init() {
type nacosMetadataReport struct {
client *nacosClient.NacosConfigClient
group string
url *common.URL
}

// URL returns the URL used to create this metadata report.
func (n *nacosMetadataReport) URL() *common.URL {
return n.url
}

// GetAppMetadata get metadata info from nacos
Expand Down Expand Up @@ -262,41 +268,41 @@ func (n *nacosMetadataReport) UnPublishAppMetadata(application, revision string)
// ListAppRevisions lists all stored revisions for an application from nacos.
func (n *nacosMetadataReport) ListAppRevisions(application string) ([]report.AppRevision, error) {
pageNo, pageSize := 1, 500
configs, err := n.client.Client().SearchConfig(vo.SearchConfigParam{
Search: "accurate",
DataId: application,
Group: "",
PageNo: pageNo,
PageSize: pageSize,
})
if err != nil {
return nil, perrors.WithMessage(err, "Could not search configs for ListAppRevisions")
}
if configs == nil || len(configs.PageItems) == 0 {
return nil, nil
}
if int(configs.TotalCount) > len(configs.PageItems) {
logger.Warnf("ListAppRevisions for app=%s: total configs (%d) exceeds page size (%d), "+
"some revisions may be silently dropped. Consider implementing pagination.",
application, configs.TotalCount, len(configs.PageItems))
}

result := make([]report.AppRevision, 0, len(configs.PageItems))
for _, item := range configs.PageItems {
if item.Group == "" || item.Group == n.group {
// Skip legacy format entries — they use group=n.group and dataId=app:revision
// We only want the Java-compatible format where group=revision and dataId=application
continue
var result []report.AppRevision
for {
configs, err := n.client.Client().SearchConfig(vo.SearchConfigParam{
Search: "accurate",
DataId: application,
Group: "",
PageNo: pageNo,
PageSize: pageSize,
})
if err != nil {
return nil, perrors.WithMessage(err, "Could not search configs for ListAppRevisions")
}
rev := item.Group
var modifyTime int64
if item.Content != "" {
modifyTime = report.ParseMetadataLastUpdatedTime([]byte(item.Content))
if configs == nil || len(configs.PageItems) == 0 {
break
}
result = append(result, report.AppRevision{
Revision: rev,
ModifyTime: modifyTime,
})
for _, item := range configs.PageItems {
if item.Group == "" || item.Group == n.group {
// Skip legacy format entries — they use group=n.group and dataId=app:revision
// We only want the Java-compatible format where group=revision and dataId=application
continue
}
rev := item.Group
var modifyTime int64
if item.Content != "" {
modifyTime = report.ParseMetadataLastUpdatedTime([]byte(item.Content))
}
result = append(result, report.AppRevision{
Revision: rev,
ModifyTime: modifyTime,
})
}
if pageNo*pageSize >= int(configs.TotalCount) {
break
}
pageNo++
}
return result, nil
}
Expand All @@ -316,5 +322,5 @@ func (n *nacosMetadataReportFactory) CreateMetadataReport(url *common.URL) repor
logger.Errorf("[Metadata][Nacos] could not create nacos metadata report, url=%s", url.String())
return nil
}
return &nacosMetadataReport{client: client, group: group}
return &nacosMetadataReport{client: client, group: group, url: url}
}
4 changes: 4 additions & 0 deletions metadata/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/metadata/info"
"dubbo.apache.org/dubbo-go/v3/metadata/mapping"
)
Expand Down Expand Up @@ -69,4 +70,7 @@ type MetadataReport interface {
// ListAppRevisions lists all stored revisions for an application.
// Each AppRevision contains the revision string and the last modification time (unix ms).
ListAppRevisions(application string) ([]AppRevision, error)

// URL returns the URL used to create this metadata report instance.
URL() *common.URL
}
21 changes: 13 additions & 8 deletions metadata/report/zookeeper/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ import (
"encoding/json"
"fmt"
"strings"
)

import (
"github.com/dubbogo/go-zookeeper/zk"

gxset "github.com/dubbogo/gost/container/set"

gxzookeeper "github.com/dubbogo/gost/database/kv/zk"
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
Expand All @@ -39,6 +36,7 @@ import (
"dubbo.apache.org/dubbo-go/v3/metadata/mapping/metadata"
"dubbo.apache.org/dubbo-go/v3/metadata/report"
"dubbo.apache.org/dubbo-go/v3/remoting/zookeeper"
perrors "github.com/pkg/errors"
)

// zkClient abstracts the ZookeeperClient operations used by zookeeperMetadataReport.
Expand Down Expand Up @@ -84,6 +82,12 @@ type zookeeperMetadataReport struct {
rootDir string
listener *zookeeper.ZkEventListener
cacheListener *CacheListener
url *common.URL
}

// URL returns the URL used to create this metadata report.
func (m *zookeeperMetadataReport) URL() *common.URL {
return m.url
}

// GetAppMetadata get metadata info from zookeeper
Expand All @@ -109,7 +113,7 @@ func (m *zookeeperMetadataReport) PublishAppMetadata(application, revision strin
return err
}
err = m.client.CreateWithValue(k, data)
if err == zk.ErrNodeExists {
if perrors.Is(err, zk.ErrNodeExists) {
_, err = m.client.SetContent(k, data, -1)
}
Comment thread
eye-gu marked this conversation as resolved.
Resolved
return err
Expand All @@ -120,7 +124,7 @@ func (m *zookeeperMetadataReport) PublishAppMetadata(application, revision strin
func (m *zookeeperMetadataReport) UnPublishAppMetadata(application, revision string) error {
k := m.rootDir + application + constant.PathSeparator + revision
err := m.client.Delete(k)
if err == zk.ErrNoNode {
if perrors.Is(err, zk.ErrNoNode) {
return nil
}
return err
Expand All @@ -131,7 +135,7 @@ func (m *zookeeperMetadataReport) ListAppRevisions(application string) ([]report
parent := m.rootDir + application
children, _, err := m.client.Children(parent)
if err != nil {
if err == zk.ErrNoNode {
if perrors.Is(err, zk.ErrNoNode) {
return nil, nil
}
return nil, err
Expand All @@ -155,7 +159,7 @@ func (m *zookeeperMetadataReport) ListAppRevisions(application string) ([]report
func (m *zookeeperMetadataReport) RegisterServiceAppMapping(key string, group string, value string) error {
path := m.rootDir + group + constant.PathSeparator + key
v, state, err := m.client.GetContent(path)
if err == zk.ErrNoNode {
if perrors.Is(err, zk.ErrNoNode) {
if cErr := m.client.CreateWithValue(path, []byte(value)); cErr != nil {
if perrors.Is(cErr, zk.ErrNodeExists) {
return fmt.Errorf("create mapping %s: %w", path, report.ErrMappingCASConflict)
Expand Down Expand Up @@ -227,6 +231,7 @@ func (mf *zookeeperMetadataReportFactory) CreateMetadataReport(url *common.URL)
client: zkClientWrapper{client},
rootDir: rootDir,
listener: zookeeper.NewZkEventListener(client),
url: url,
}

reporter.cacheListener = NewCacheListener(rootDir, reporter.listener)
Expand Down
3 changes: 3 additions & 0 deletions metadata/report/zookeeper/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func (m *mockZkClient) CreateWithValue(path string, data []byte) error {
if err, ok := m.errors["CreateWithValue:"+path]; ok {
return err
}
if _, exists := m.data[path]; exists {
return zk.ErrNodeExists
}
m.data[path] = data
stat := &zk.Stat{Version: 0, Mtime: 0}
m.stats[path] = stat
Expand Down
5 changes: 5 additions & 0 deletions metadata/report_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ type DelegateMetadataReport struct {
instance report.MetadataReport
}

// URL returns the URL of the underlying metadata report instance.
func (d *DelegateMetadataReport) URL() *common.URL {
return d.instance.URL()
}

// PublishAppMetadata delegate publish metadata info
func (d *DelegateMetadataReport) PublishAppMetadata(application, revision string, meta *info.MetadataInfo) error {
event := metadataMetrics.NewMetadataMetricTimeEvent(metadataMetrics.MetadataPush)
Expand Down
5 changes: 5 additions & 0 deletions metadata/report_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ func (m *mockMetadataReport) ListAppRevisions(string) ([]report.AppRevision, err
return args.Get(0).([]report.AppRevision), args.Error(1)
}

func (m *mockMetadataReport) URL() *common.URL {
u, _ := common.NewURL("mock://127.0.0.1:8848")
return u
}

type listener struct {
}

Expand Down
Loading