You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2022/01/17 12:59:13 UTC

[servicecomb-service-center] branch grpc created (now 2de96b0)

This is an automated email from the ASF dual-hosted git repository.

littlecui pushed a change to branch grpc
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git.


      at 2de96b0  Bug: return internal properties when list all microservices

This branch includes the following new commits:

     new 2de96b0  Bug: return internal properties when list all microservices

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[servicecomb-service-center] 01/01: Bug: return internal properties when list all microservices

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

littlecui pushed a commit to branch grpc
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git

commit 2de96b03a37450cde71a2fa2cab291f4f4235915
Author: little-cui <su...@qq.com>
AuthorDate: Mon Jan 17 20:58:51 2022 +0800

    Bug: return internal properties when list all microservices
---
 datasource/etcd/etcd.go   |  6 +++++-
 datasource/etcd/ms.go     | 49 +++++++++++++++++++++++++++++++----------------
 datasource/mongo/mongo.go |  6 +++++-
 datasource/mongo/ms.go    |  3 ++-
 datasource/options.go     |  3 ++-
 server/server.go          |  7 ++++---
 6 files changed, 50 insertions(+), 24 deletions(-)

diff --git a/datasource/etcd/etcd.go b/datasource/etcd/etcd.go
index 75ebf72..19ab461 100644
--- a/datasource/etcd/etcd.go
+++ b/datasource/etcd/etcd.go
@@ -94,7 +94,11 @@ func NewDataSource(opts datasource.Options) (datasource.DataSource, error) {
 	if err := inst.initialize(); err != nil {
 		return nil, err
 	}
-	inst.metadataManager = newMetadataManager(opts.SchemaNotEditable, opts.InstanceTTL)
+	inst.metadataManager = &MetadataManager{
+		SchemaNotEditable:  opts.SchemaNotEditable,
+		InstanceTTL:        opts.InstanceTTL,
+		InstanceProperties: opts.InstanceProperties,
+	}
 	inst.sysManager = newSysManager()
 	inst.depManager = &DepManager{}
 	inst.scManager = &SCManager{}
diff --git a/datasource/etcd/ms.go b/datasource/etcd/ms.go
index 355c08d..c20a370 100644
--- a/datasource/etcd/ms.go
+++ b/datasource/etcd/ms.go
@@ -57,14 +57,8 @@ type MetadataManager struct {
 	// SchemaNotEditable determines whether schema modification is not allowed
 	SchemaNotEditable bool
 	// InstanceTTL options
-	InstanceTTL int64
-}
-
-func newMetadataManager(schemaNotEditable bool, instanceTTL int64) datasource.MetadataManager {
-	return &MetadataManager{
-		SchemaNotEditable: schemaNotEditable,
-		InstanceTTL:       instanceTTL,
-	}
+	InstanceTTL        int64
+	InstanceProperties map[string]string
 }
 
 // RegisterService implement:
@@ -299,16 +293,9 @@ func (ds *MetadataManager) ListServiceDetail(ctx context.Context, request *pb.Ge
 			return nil, pb.NewError(pb.ErrInternal, err.Error())
 		}
 		serviceDetail.MicroService = service
-		tmpServiceDetail := &pb.ServiceDetail{}
-		err = copier.CopyWithOption(tmpServiceDetail, serviceDetail, copier.Option{DeepCopy: true})
+		tmpServiceDetail, err := ds.copyOne(serviceDetail)
 		if err != nil {
-			return nil, pb.NewError(pb.ErrInternal, err.Error())
-		}
-		tmpServiceDetail.MicroService.Properties = nil
-		tmpServiceDetail.MicroService.Schemas = nil
-		instances := tmpServiceDetail.Instances
-		for _, instance := range instances {
-			instance.Properties = nil
+			return nil, err
 		}
 		allServiceDetails = append(allServiceDetails, tmpServiceDetail)
 	}
@@ -319,6 +306,34 @@ func (ds *MetadataManager) ListServiceDetail(ctx context.Context, request *pb.Ge
 	}, nil
 }
 
+func (ds *MetadataManager) copyOne(serviceDetail *pb.ServiceDetail) (*pb.ServiceDetail, error) {
+	tmpServiceDetail := &pb.ServiceDetail{}
+	err := copier.CopyWithOption(tmpServiceDetail, serviceDetail, copier.Option{DeepCopy: true})
+	if err != nil {
+		return nil, pb.NewError(pb.ErrInternal, err.Error())
+	}
+	tmpServiceDetail.MicroService.Properties = nil
+	tmpServiceDetail.MicroService.Schemas = nil
+	instances := tmpServiceDetail.Instances
+	for _, instance := range instances {
+		instance.Properties = ds.removeCustomProperties(instance.Properties)
+	}
+	return tmpServiceDetail, nil
+}
+
+func (ds *MetadataManager) removeCustomProperties(properties map[string]string) map[string]string {
+	if len(ds.InstanceProperties) == 0 {
+		return nil
+	}
+	props := make(map[string]string, 0)
+	for k, v := range properties {
+		if _, ok := ds.InstanceProperties[k]; ok {
+			props[k] = v
+		}
+	}
+	return props
+}
+
 func (ds *MetadataManager) filterServices(domainProject string, request *pb.GetServicesInfoRequest, service *pb.MicroService) bool {
 	if !request.WithShared && datasource.IsGlobal(pb.MicroServiceToKey(domainProject, service)) {
 		return false
diff --git a/datasource/mongo/mongo.go b/datasource/mongo/mongo.go
index 8bac3c4..68e6b32 100644
--- a/datasource/mongo/mongo.go
+++ b/datasource/mongo/mongo.go
@@ -74,7 +74,11 @@ func NewDataSource(opts datasource.Options) (datasource.DataSource, error) {
 	inst.scManager = &SCManager{}
 	inst.depManager = &DepManager{}
 	inst.sysManager = &SysManager{}
-	inst.metadataManager = &MetadataManager{SchemaNotEditable: opts.SchemaNotEditable, InstanceTTL: opts.InstanceTTL}
+	inst.metadataManager = &MetadataManager{
+		SchemaNotEditable:  opts.SchemaNotEditable,
+		InstanceTTL:        opts.InstanceTTL,
+		InstanceProperties: opts.InstanceProperties,
+	}
 	inst.metricsManager = &MetricsManager{}
 	return inst, nil
 }
diff --git a/datasource/mongo/ms.go b/datasource/mongo/ms.go
index 45fb2a7..37fe414 100644
--- a/datasource/mongo/ms.go
+++ b/datasource/mongo/ms.go
@@ -61,7 +61,8 @@ type MetadataManager struct {
 	// SchemaNotEditable determines whether schema modification is not allowed
 	SchemaNotEditable bool
 	// InstanceTTL options
-	InstanceTTL int64
+	InstanceTTL        int64
+	InstanceProperties map[string]string
 }
 
 func (ds *MetadataManager) RegisterService(ctx context.Context, request *discovery.CreateServiceRequest) (*discovery.CreateServiceResponse, error) {
diff --git a/datasource/options.go b/datasource/options.go
index 7a136db..e9ce0d7 100644
--- a/datasource/options.go
+++ b/datasource/options.go
@@ -28,5 +28,6 @@ type Options struct {
 	EnableCache       bool
 	SchemaNotEditable bool
 	// InstanceTTL: the default ttl of instance lease
-	InstanceTTL int64
+	InstanceTTL        int64
+	InstanceProperties map[string]string
 }
diff --git a/server/server.go b/server/server.go
index 76a2aea..a26703f 100644
--- a/server/server.go
+++ b/server/server.go
@@ -124,9 +124,10 @@ func (s *ServiceCenterServer) initDatasource() {
 				}
 			},
 		},
-		EnableCache:       config.GetRegistry().EnableCache,
-		InstanceTTL:       config.GetRegistry().InstanceTTL,
-		SchemaNotEditable: config.GetRegistry().SchemaNotEditable,
+		EnableCache:        config.GetRegistry().EnableCache,
+		SchemaNotEditable:  config.GetRegistry().SchemaNotEditable,
+		InstanceTTL:        config.GetRegistry().InstanceTTL,
+		InstanceProperties: config.GetStringMap("registry.instance.properties"),
 	}); err != nil {
 		log.Fatal("init datasource failed", err)
 	}