Skip to content

Commit

Permalink
âfeat: support v3 remote zk metadata report
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenceLiZhixin committed Apr 16, 2021
1 parent abbd985 commit 3d172bf
Show file tree
Hide file tree
Showing 61 changed files with 1,925 additions and 1,768 deletions.
10 changes: 5 additions & 5 deletions .asf.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
notifications:
commits: commits@dubbo.apache.org
issues: notifications@dubbo.apache.org
pullrequests: notifications@dubbo.apache.org
jira_options: link label link label
notifications:
commits: commits@dubbo.apache.org
issues: notifications@dubbo.apache.org
pullrequests: notifications@dubbo.apache.org
jira_options: link label link label
38 changes: 19 additions & 19 deletions .github/ISSUE_TEMPLATE/bug-report.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
---
name: Bug Report
about: Report a bug
labels: kind/bug

---

<!-- Please use this template while reporting a bug and provide as much info as possible. Not doing so may result in your bug not being addressed in a timely manner. Thanks!
-->


**What happened**:

**What you expected to happen**:

**How to reproduce it (as minimally and precisely as possible)**:

**Anything else we need to know?**:
---
name: Bug Report
about: Report a bug
labels: kind/bug

---

<!-- Please use this template while reporting a bug and provide as much info as possible. Not doing so may result in your bug not being addressed in a timely manner. Thanks!
-->


**What happened**:

**What you expected to happen**:

**How to reproduce it (as minimally and precisely as possible)**:

**Anything else we need to know?**:
20 changes: 10 additions & 10 deletions .github/ISSUE_TEMPLATE/enhancement.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
name: Enhancement Request
about: Suggest an enhancement
labels: kind/feature

---
<!-- Please only use this template for submitting enhancement requests -->

**What would you like to be added**:

---
name: Enhancement Request
about: Suggest an enhancement
labels: kind/feature

---
<!-- Please only use this template for submitting enhancement requests -->

**What would you like to be added**:

**Why is this needed**:
3 changes: 2 additions & 1 deletion common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,12 @@ const (
const (
SUBSCRIBED_SERVICE_NAMES_KEY = "subscribed-services"
PROVIDER_BY = "provided-by"
EXPORTED_SERVICES_REVISION_PROPERTY_NAME = "dubbo.exported-services.revision"
EXPORTED_SERVICES_REVISION_PROPERTY_NAME = "dubbo.metadata.revision"
SUBSCRIBED_SERVICES_REVISION_PROPERTY_NAME = "dubbo.subscribed-services.revision"
SERVICE_INSTANCE_SELECTOR = "service-instance-selector"
METADATA_STORAGE_TYPE_PROPERTY_NAME = "dubbo.metadata.storage-type"
DEFAULT_METADATA_STORAGE_TYPE = "local"
REMOTE_METADATA_STORAGE_TYPE = "remote"
SERVICE_INSTANCE_ENDPOINTS = "dubbo.endpoints"
METADATA_SERVICE_PREFIX = "dubbo.metadata-service."
METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME = METADATA_SERVICE_PREFIX + "url-params"
Expand Down
25 changes: 25 additions & 0 deletions common/extension/metadata_remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package extension

import (
"fmt"
"github.com/apache/dubbo-go/metadata/remote"
perrors "github.com/pkg/errors"
)

var (
creator func() (remote.RemoteMetadataService, error)
)

// SetMetadataRemoteService will store the
func SetMetadataRemoteService(creatorFunc func() (remote.RemoteMetadataService, error)) {
creator = creatorFunc
}

// GetRemoteMetadataServiceFactory will create a MetadataService instance
func GetRemoteMetadataService() (remote.RemoteMetadataService, error) {
if creator != nil {
return creator()
}
return nil, perrors.New(fmt.Sprintf("could not find the metadata service creator for metadataType: remote, please check whether you have imported relative packages, \n" +
"remote - github.com/apache/dubbo-go/metadata/remote/impl"))
}
15 changes: 0 additions & 15 deletions common/extension/metadata_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
)

import (
"github.com/apache/dubbo-go/common/logger"
"github.com/apache/dubbo-go/metadata/service"
)

Expand All @@ -51,17 +50,3 @@ func GetMetadataService(msType string) (service.MetadataService, error) {
"local - github.com/apache/dubbo-go/metadata/service/inmemory, \n"+
"remote - github.com/apache/dubbo-go/metadata/service/remote", msType))
}

// GetRemoteMetadataService will get a RemoteMetadataService instance
func GetRemoteMetadataService() (service.MetadataService, error) {
if remoteMetadataService != nil {
return remoteMetadataService, nil
}
if creator, ok := metadataServiceInsMap["remote"]; ok {
var err error
remoteMetadataService, err = creator()
return remoteMetadataService, err
}
logger.Warn("could not find the metadata service creator for metadataType: remote")
return nil, perrors.New(fmt.Sprintf("could not find the metadata service creator for metadataType: remote"))
}
5 changes: 2 additions & 3 deletions common/extension/metadata_service_proxy_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func GetMetadataServiceProxyFactory(name string) service.MetadataServiceProxyFac
if f, ok := metadataServiceProxyFactoryMap[name]; ok {
return f()
}
panic(fmt.Sprintf("could not find the metadata service factory creator for name: %s, please check whether you have imported relative packages, \n"+
"local - github.com/apache/dubbo-go/metadata/service/inmemory, \n"+
"remote - github.com/apache/dubbo-go/metadata/service/remote", name))
panic(fmt.Sprintf("could not find the metadata service factory creator for name: %s, please check whether you have imported relative packages, \n" +
"local - github.com/apache/dubbo-go/metadata/service/inmemory, \n"))
}
213 changes: 213 additions & 0 deletions common/metadata_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package common

import (
"fmt"
"github.com/apache/dubbo-go/common/constant"
gxset "github.com/dubbogo/gost/container/set"
"go.uber.org/atomic"
"hash/crc32"
"net/url"
"sort"
"strings"
)

var IncludeKeys = gxset.NewSet(
constant.APPLICATION_KEY,
constant.GROUP_KEY, constant.TIMESTAMP_KEY, constant.SERIALIZATION_KEY, constant.CLUSTER_KEY,
constant.LOADBALANCE_KEY, constant.PATH_KEY, constant.TIMEOUT_KEY,
constant.TOKEN_KEY, constant.VERSION_KEY, constant.WARMUP_KEY,
constant.WEIGHT_KEY, constant.RELEASE_KEY)

type MetadataInfo struct {
App string `json:"app"`
Revision string `json:"revision"`
Services map[string]*ServiceInfo `json:"services"`

reported *atomic.Bool `json:"-"`
}

func NewMetadataInfWithApp(app string) *MetadataInfo {
return NewMetadataInfo(app, "", make(map[string]*ServiceInfo))
}

func NewMetadataInfo(app string, revision string, services map[string]*ServiceInfo) *MetadataInfo {
return &MetadataInfo{
App: app,
Revision: revision,
Services: services,
reported: atomic.NewBool(false),
}
}

// CalAndGetRevision is different from Dubbo because golang doesn't support overload
// so that we could use interface + method name as identifier and ignore the method params
// per my understanding, it's enough because Dubbo actually ignore the url params.
// please refer org.apache.dubbo.common.URL#toParameterString(java.lang.String...)
func (mi *MetadataInfo) CalAndGetRevision() string {
if mi.Revision != "" && mi.reported.Load() {
return mi.Revision
}
if len(mi.Services) == 0 {
return "0"
}
candidates := make([]string, 8)

for _, s := range mi.Services {
sk := s.serviceKey
ms := s.url.Methods
if len(ms) == 0 {
candidates = append(candidates, sk)
} else {
for _, m := range ms {
// methods are part of candidates
candidates = append(candidates, sk+constant.KEY_SEPARATOR+m)
}
}

// append url params if we need it
}
sort.Strings(candidates)

// it's nearly impossible to be overflow
res := uint64(0)
for _, c := range candidates {
res += uint64(crc32.ChecksumIEEE([]byte(c)))
}
mi.Revision = fmt.Sprint(res)
return mi.Revision

}

func (mi *MetadataInfo) HasReported() bool {
return mi.reported.Load()
}

func (mi *MetadataInfo) MarkReported() {
mi.reported.CAS(false, true)
}

func (mi *MetadataInfo) AddService(service *ServiceInfo) {
if service == nil {
return
}
mi.Services[service.GetMatchKey()] = service
}

func (mi *MetadataInfo) RemoveService(service *ServiceInfo) {
if service == nil {
return
}
delete(mi.Services, service.matchKey)
}

type ServiceInfo struct {
Name string `json:"name"`
Group string `json:"group"`
Version string `json:"version"`
Protocol string `json:"protocol"`
Path string `json:"path"`
Params map[string]string `json:"params"`

serviceKey string `json:"-"`
matchKey string `json:"-"`
url *URL `json:"-"`
}

func NewServiceInfoWithUrl(url *URL) *ServiceInfo {
service := NewServiceInfo(url.Service(), url.Group(), url.Version(), url.Protocol, url.Path, nil)
service.url = url
// TODO includeKeys load dynamic
p := make(map[string]string, 8)
for _, keyInter := range IncludeKeys.Values() {
key := keyInter.(string)
value := url.GetParam(key, "")
if len(value) != 0 {
p[key] = value
}
for _, method := range url.Methods {
value = url.GetMethodParam(method, key, "")
if len(value) != 0 {
p[method+"."+key] = value
}
}
}
service.Params = p
return service
}

func NewServiceInfo(name string, group string, version string, protocol string, path string, params map[string]string) *ServiceInfo {
serviceKey := ServiceKey(name, group, version)
matchKey := MatchKey(serviceKey, protocol)
return &ServiceInfo{
Name: name,
Group: group,
Version: version,
Protocol: protocol,
Path: path,
Params: params,
serviceKey: serviceKey,
matchKey: matchKey,
}
}

func (si *ServiceInfo) GetMethods() []string {
if si.Params[constant.METHODS_KEY] != "" {
s := si.Params[constant.METHODS_KEY]
return strings.Split(s, ",")
}
methods := make([]string, 8)
for k, _ := range si.Params {
ms := strings.Index(k, ".")
if ms > 0 {
methods = append(methods, k[0:ms])
}
}
return methods
}

func (si *ServiceInfo) GetParams() url.Values {
v := url.Values{}
for k, p := range si.Params {
ms := strings.Index(k, ".")
if ms > 0 {
v.Set("methods."+k, p)
} else {
v.Set(k, p)
}
}
return v
}

func (si *ServiceInfo) GetMatchKey() string {
if si.matchKey != "" {
return si.matchKey
}
serviceKey := si.GetServiceKey()
si.matchKey = MatchKey(serviceKey, si.Protocol)
return si.matchKey
}

func (si *ServiceInfo) GetServiceKey() string {
if si.serviceKey != "" {
return si.serviceKey
}
si.serviceKey = ServiceKey(si.Name, si.Group, si.Version)
return si.serviceKey
}
16 changes: 15 additions & 1 deletion common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ func NewURL(urlString string, opts ...Option) (*URL, error) {
return &s, nil
}

func MatchKey(serviceKey string, protocol string) string {
return serviceKey + ":" + protocol
}

// Group get group
func (c *URL) Group() string {
return c.GetParam(constant.GROUP_KEY, "")
}

// Version get group
func (c *URL) Version() string {
return c.GetParam(constant.VERSION_KEY, "")
}

// URLEqual judge @url and @c is equal or not.
func (c *URL) URLEqual(url *URL) bool {
tmpC := c.Clone()
Expand Down Expand Up @@ -663,7 +677,7 @@ func MergeUrl(serviceUrl *URL, referenceUrl *URL) *URL {
mergedUrl := serviceUrl.Clone()
params := mergedUrl.GetParams()
// iterator the referenceUrl if serviceUrl not have the key ,merge in
// referenceUrl usually will not changed. so change RangeParams to GetParams to avoid the string value copy.
// referenceUrl usually will not changed. so change RangeParams to GetParams to avoid the string value copy.// Group get group
for key, value := range referenceUrl.GetParams() {
if v := mergedUrl.GetParam(key, ""); len(v) == 0 {
if len(value) > 0 {
Expand Down
Loading

0 comments on commit 3d172bf

Please sign in to comment.