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
Next Next commit
feat(metadata): add renew and GC policy for app-level metadata
  • Loading branch information
eye-gu committed Jun 10, 2026
commit 2a6c5c828f34041bd0b51a8c5f24dacaea24232b
5 changes: 5 additions & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ const (
MetadataServiceURLParamsPropertyName = MetadataServicePrefix + "url-params"
MetadataServiceURLsPropertyName = MetadataServicePrefix + "urls"
ServiceDiscoveryKey = "service_discovery" // indicate which service discovery instance will be used

// metadata GC configuration keys
MetadataGCEnabledKey = "metadata.gc.enabled"
MetadataGCWindowKey = "metadata.gc.window" // GC window in days, aligned with daily renew cycle
MetadataRenewOnStartupKey = "metadata.renew-on-startup" // whether to run renewAppMetadata once on startup
)

// Generic Filter
Expand Down
1 change: 1 addition & 0 deletions metadata/info/metadata_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type MetadataInfo struct {
Services map[string]*ServiceInfo `json:"services,omitempty" hessian:"services"`
exportedServiceURLs map[string][]*common.URL `hessian:"-"` // server exported service urls
subscribedServiceURLs map[string][]*common.URL `hessian:"-"` // client subscribed service urls
LastUpdatedTime int64 `json:"lastUpdatedTime,omitempty" hessian:"-"`
}

func NewAppMetadataInfo(app string) *MetadataInfo {
Expand Down
10 changes: 10 additions & 0 deletions metadata/mapping/metadata/service_name_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,13 @@ func (m *mockMetadataReport) RemoveServiceAppMappingListener(string, string) err
args := m.Called()
return args.Error(0)
}

func (m *mockMetadataReport) UnPublishAppMetadata(string, string) error {
args := m.Called()
return args.Error(0)
}

func (m *mockMetadataReport) ListAppRevisions(string) ([]report.AppRevision, error) {
args := m.Called()
return args.Get(0).([]report.AppRevision), args.Error(1)
}
58 changes: 54 additions & 4 deletions metadata/report/etcd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ import (
"dubbo.apache.org/dubbo-go/v3/metadata/report"
)

// etcdClient abstracts the etcd Client operations used by etcdMetadataReport.
type etcdClient interface {
Get(key string) (string, error)
Put(key, value string) error
Delete(key string) error
GetChildren(key string) ([]string, []string, error)
}

// etcdClientWrapper wraps *gxetcd.Client to implement etcdClient.
type etcdClientWrapper struct {
*gxetcd.Client
}

func (w etcdClientWrapper) Put(key, value string) error {
return w.Client.Put(key, value)
}

const DEFAULT_ROOT = "dubbo"

func init() {
Expand All @@ -50,13 +67,13 @@ func init() {

// etcdMetadataReport is the implementation of MetadataReport based etcd
type etcdMetadataReport struct {
client *gxetcd.Client
client etcdClient
rootDir string
}

// GetAppMetadata get metadata info from etcd
func (e *etcdMetadataReport) GetAppMetadata(application, revision string) (*info.MetadataInfo, error) {
key := e.rootDir + application + constant.PathSeparator + revision
key := e.rootDir + constant.PathSeparator + application + constant.PathSeparator + revision
data, err := e.client.Get(key)
if err != nil {
return nil, err
Expand All @@ -68,7 +85,7 @@ func (e *etcdMetadataReport) GetAppMetadata(application, revision string) (*info

// PublishAppMetadata publish metadata info to etcd
func (e *etcdMetadataReport) PublishAppMetadata(application, revision string, info *info.MetadataInfo) error {
key := e.rootDir + application + constant.PathSeparator + revision
key := e.rootDir + constant.PathSeparator + application + constant.PathSeparator + revision
value, err := json.Marshal(info)
if err == nil {
err = e.client.Put(key, string(value))
Expand Down Expand Up @@ -125,6 +142,39 @@ func (e *etcdMetadataReport) RemoveServiceAppMappingListener(key string, group s
return nil
}

// UnPublishAppMetadata removes metadata for a specific revision from etcd.
// This operation is idempotent.
func (e *etcdMetadataReport) UnPublishAppMetadata(application, revision string) error {
key := e.rootDir + constant.PathSeparator + application + constant.PathSeparator + revision
return e.client.Delete(key)
}

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

result := make([]report.AppRevision, 0, len(keys))
for _, 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)),
})
}
return result, nil
}
Comment thread
eye-gu marked this conversation as resolved.
Resolved

type etcdMetadataReportFactory struct{}

// CreateMetadataReport get the MetadataReport instance of etcd
Expand All @@ -138,5 +188,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: client, rootDir: group}
return &etcdMetadataReport{client: etcdClientWrapper{client}, rootDir: group}
}
Loading