DMwangnima commented on code in PR #2534:
URL: https://github.com/apache/dubbo-go/pull/2534#discussion_r1451638508


##########
metadata/mapping/metadata/service_name_mapping.go:
##########
@@ -32,90 +32,78 @@ 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/config"
-       "dubbo.apache.org/dubbo-go/v3/config/instance"
        "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/metadata/report/instance"
 )
 
 const (
        DefaultGroup = "mapping"
-       slash        = "/"
+       retryTimes   = 10
 )
 
 func init() {
        extension.SetGlobalServiceNameMapping(GetNameMappingInstance)
 }
 
-// MetadataServiceNameMapping is the implementation based on metadata report
+var (
+       serviceNameMappingInstance *ServiceNameMapping
+       serviceNameMappingOnce     sync.Once
+)
+
+// GetNameMappingInstance return an instance, if not found, it creates one
+func GetNameMappingInstance() mapping.ServiceNameMapping {
+       serviceNameMappingOnce.Do(func() {
+               serviceNameMappingInstance = &ServiceNameMapping{}
+       })
+       return serviceNameMappingInstance
+}
+
+// ServiceNameMapping is the implementation based on metadata report
 // it's a singleton
-type MetadataServiceNameMapping struct {
+type ServiceNameMapping struct {
 }
 
 // Map will map the service to this application-level service
-func (d *MetadataServiceNameMapping) Map(url *common.URL) error {
+func (d *ServiceNameMapping) Map(url *common.URL) error {
        serviceInterface := url.GetParam(constant.InterfaceKey, "")
-       // metadata service is admin service, should not be mapped
-       if constant.MetadataServiceName == serviceInterface {
-               logger.Debug("try to map the metadata service, will be ignored")
-               return nil
-       }
-
-       appName := config.GetApplicationConfig().Name
-
-       metadataReport := getMetaDataReport(url.GetParam(constant.RegistryKey, 
""))
-       if metadataReport == nil {
-               logger.Info("get metadata report instance is nil, metadata 
service will be enabled!")
+       appName := url.GetParam(constant.ApplicationKey, "")
+       // url is the service url,not the registry url,this url has no registry 
id info,can not got where to write mapping,so write all
+       // if the mapping can hold a report instance, it can write once
+       metadataReports := instance.GetMetadataReports()
+       if len(metadataReports) == 0 {
+               return perrors.New("can not registering mapping to remote cause 
no metadata report instance found")
        } else {

Review Comment:
   We call remove this else so that it would be like:
   ```
   if len(metadataReports) == 0 {
       return ...
   }
   // logics in else
   ...
   ```



##########
metadata/report/instance/metadata_report.go:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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 instance
+
+import (
+       "sync"
+       "time"
+)
+
+import (
+       "github.com/dubbogo/gost/container/set"
+       "github.com/dubbogo/gost/log/logger"
+)
+
+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"
+       "dubbo.apache.org/dubbo-go/v3/metrics"
+       metadataMetrics "dubbo.apache.org/dubbo-go/v3/metrics/metadata"
+)
+
+var (
+       instances    = make(map[string]report.MetadataReport)
+       once         sync.Once
+       metadataType = constant.DefaultMetadataStorageType
+)
+
+func Init(urls []*common.URL, metaType string) {
+       once.Do(func() {
+               metadataType = metaType
+               if len(urls) != 0 {
+                       for _, url := range urls {
+                               fac := 
extension.GetMetadataReportFactory(url.Protocol)
+                               if fac != nil {

Review Comment:
   This style is better.
   ```
   if fac == nil {
       logger.Warnf(...)
       return
   }
   // deal with (fac != nil) logic
   ...
   ```



##########
metadata/report/instance/metadata_report.go:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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 instance
+
+import (
+       "sync"
+       "time"
+)
+
+import (
+       "github.com/dubbogo/gost/container/set"
+       "github.com/dubbogo/gost/log/logger"
+)
+
+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"
+       "dubbo.apache.org/dubbo-go/v3/metrics"
+       metadataMetrics "dubbo.apache.org/dubbo-go/v3/metrics/metadata"
+)
+
+var (
+       instances    = make(map[string]report.MetadataReport)
+       once         sync.Once
+       metadataType = constant.DefaultMetadataStorageType
+)
+
+func Init(urls []*common.URL, metaType string) {
+       once.Do(func() {
+               metadataType = metaType
+               if len(urls) != 0 {
+                       for _, url := range urls {
+                               fac := 
extension.GetMetadataReportFactory(url.Protocol)
+                               if fac != nil {
+                                       key := 
url.GetParam(constant.RegistryKey, constant.DefaultKey)
+                                       instances[key] = 
&DelegateMetadataReport{instance: fac.CreateMetadataReport(url)}
+                               } else {
+                                       logger.Warnf("metadata report factory 
is not found, url: %s", url.String())
+                               }
+                       }
+               }
+       })
+}
+
+func GetMetadataReport() report.MetadataReport {
+       for _, v := range instances {
+               return v
+       }
+       return nil
+}
+
+func GetMetadataReportByRegistry(registry string) report.MetadataReport {
+       if len(registry) == 0 {
+               registry = constant.DefaultKey
+       }
+       if r, ok := instances[registry]; ok {
+               return r
+       }
+       return GetMetadataReport()
+}
+
+func GetMetadataReports() []report.MetadataReport {
+       reports := make([]report.MetadataReport, 0)
+       for _, r := range instances {
+               reports = append(reports, r)
+       }
+       return reports
+}
+
+func GetMetadataType() string {
+       return metadataType
+}
+
+//func GetMetadataReports() []report.MetadataReport {

Review Comment:
   This commented function could be deleted?



##########
metadata/report/instance/metadata_report.go:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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 instance
+
+import (
+       "sync"
+       "time"
+)
+
+import (
+       "github.com/dubbogo/gost/container/set"
+       "github.com/dubbogo/gost/log/logger"
+)
+
+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"
+       "dubbo.apache.org/dubbo-go/v3/metrics"
+       metadataMetrics "dubbo.apache.org/dubbo-go/v3/metrics/metadata"
+)
+
+var (
+       instances    = make(map[string]report.MetadataReport)
+       once         sync.Once
+       metadataType = constant.DefaultMetadataStorageType
+)
+
+func Init(urls []*common.URL, metaType string) {
+       once.Do(func() {
+               metadataType = metaType
+               if len(urls) != 0 {
+                       for _, url := range urls {
+                               fac := 
extension.GetMetadataReportFactory(url.Protocol)
+                               if fac != nil {
+                                       key := 
url.GetParam(constant.RegistryKey, constant.DefaultKey)
+                                       instances[key] = 
&DelegateMetadataReport{instance: fac.CreateMetadataReport(url)}
+                               } else {
+                                       logger.Warnf("metadata report factory 
is not found, url: %s", url.String())
+                               }
+                       }
+               }
+       })
+}
+
+func GetMetadataReport() report.MetadataReport {
+       for _, v := range instances {
+               return v
+       }
+       return nil
+}
+
+func GetMetadataReportByRegistry(registry string) report.MetadataReport {
+       if len(registry) == 0 {
+               registry = constant.DefaultKey
+       }
+       if r, ok := instances[registry]; ok {
+               return r
+       }
+       return GetMetadataReport()
+}
+
+func GetMetadataReports() []report.MetadataReport {
+       reports := make([]report.MetadataReport, 0)

Review Comment:
   Since we have known the length of instances, allocate reports slice with 
fixed length is better.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to