You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2020/11/25 01:48:03 UTC

[GitHub] [servicecomb-service-center] xzccfzy opened a new pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

xzccfzy opened a new pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759


   Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/SCB) filed for the change (usually before you start working on it).  Trivial changes like typos do not require a JIRA issue.  Your pull request should address just this issue, without pulling in other changes.
    - [ ] Each commit in the pull request should have a meaningful subject line and body.
    - [ ] Format the pull request title like `[SCB-XXX] Fixes bug in ApproximateQuantiles`, where you replace `SCB-XXX` with the appropriate JIRA issue.
    - [ ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
    - [ ] Run `go build` `go test` `go fmt` `go vet` to make sure basic checks pass. A more thorough check will be performed on your pull request automatically.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   ---
   


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r531824689



##########
File path: datasource/mongo/ms_test.go
##########
@@ -650,3 +652,503 @@ func TestRuleUpdate(t *testing.T) {
 //		assert.Equal(t, 1, len(resSchemas.Schemas))

Review comment:
       不要注释代码

##########
File path: datasource/mongo/ms.go
##########
@@ -1345,56 +1382,896 @@ func SchemaExist(ctx context.Context, serviceID, schemaID string) (bool, error)
 
 // Instance management
 func (ds *DataSource) RegisterInstance(ctx context.Context, request *pb.RegisterInstanceRequest) (*pb.RegisterInstanceResponse, error) {
-	return &pb.RegisterInstanceResponse{}, nil
+	remoteIP := util.GetIPFromContext(ctx)
+	instance := request.Instance
+
+	// 允许自定义 id
+	if len(instance.InstanceId) > 0 {
+		resp, err := ds.Heartbeat(ctx, &pb.HeartbeatRequest{
+			InstanceId: instance.InstanceId,
+			ServiceId:  instance.ServiceId,
+		})
+		if resp == nil {
+			log.Errorf(err, "register service[%s]'s instance failed, endpoints %v, host '%s', operator %s",
+				instance.ServiceId, instance.Endpoints, instance.HostName, remoteIP)
+			return &pb.RegisterInstanceResponse{
+				Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+			}, nil
+		}
+		switch resp.Response.GetCode() {
+		case pb.ResponseSuccess:
+			log.Infof("register instance successful, reuse instance[%s/%s], operator %s",
+				instance.ServiceId, instance.InstanceId, remoteIP)
+			return &pb.RegisterInstanceResponse{
+				Response:   resp.Response,
+				InstanceId: instance.InstanceId,
+			}, nil
+		case pb.ErrInstanceNotExists:
+			// register a new one
+			return registryInstance(ctx, request)
+		default:
+			log.Errorf(err, "register instance failed, reuse instance[%s/%s], operator %s",
+				instance.ServiceId, instance.InstanceId, remoteIP)
+			return &pb.RegisterInstanceResponse{
+				Response: resp.Response,
+			}, err
+		}
+	}
+
+	if err := preProcessRegisterInstance(ctx, instance); err != nil {
+		log.Errorf(err, "register service[%s]'s instance failed, endpoints %v, host '%s', operator %s",
+			instance.ServiceId, instance.Endpoints, instance.HostName, remoteIP)
+		return &pb.RegisterInstanceResponse{
+			Response: pb.CreateResponseWithSCErr(err),
+		}, nil
+	}
+	return registryInstance(ctx, request)
 }
 
 // GetInstances returns instances under the current domain
 func (ds *DataSource) GetInstance(ctx context.Context, request *pb.GetOneInstanceRequest) (*pb.GetOneInstanceResponse, error) {
-	return &pb.GetOneInstanceResponse{}, nil
+	service := &Service{}
+	var err error
+	if len(request.ConsumerServiceId) > 0 {
+		filter := GeneratorServiceFilter(ctx, request.ConsumerServiceId)
+		service, err = GetService(ctx, filter)
+		if err != nil {
+			log.Errorf(err, "get consumer failed, consumer[%s] find provider instance[%s/%s]",
+				request.ConsumerServiceId, request.ProviderServiceId, request.ProviderInstanceId)
+			return &pb.GetOneInstanceResponse{
+				Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+			}, err
+		}
+		if service == nil {
+			log.Errorf(nil, "consumer does not exist, consumer[%s] find provider instance[%s/%s]",
+				request.ConsumerServiceId, request.ProviderServiceId, request.ProviderInstanceId)
+			return &pb.GetOneInstanceResponse{
+				Response: pb.CreateResponse(pb.ErrServiceNotExists,
+					fmt.Sprintf("Consumer[%s] does not exist.", request.ConsumerServiceId)),
+			}, nil
+		}
+	}
+
+	filter := GeneratorServiceFilter(ctx, request.ProviderServiceId)
+	provider, err := GetService(ctx, filter)
+	if err != nil {
+		log.Errorf(err, "get provider failed, consumer[%s] find provider instance[%s/%s]",
+			request.ConsumerServiceId, request.ProviderServiceId, request.ProviderInstanceId)
+		return &pb.GetOneInstanceResponse{
+			Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+		}, err
+	}
+	if provider == nil {
+		log.Errorf(nil, "provider does not exist, consumer[%s] find provider instance[%s/%s]",
+			request.ConsumerServiceId, request.ProviderServiceId, request.ProviderInstanceId)
+		return &pb.GetOneInstanceResponse{
+			Response: pb.CreateResponse(pb.ErrServiceNotExists,
+				fmt.Sprintf("Provider[%s] does not exist.", request.ProviderServiceId)),
+		}, nil
+	}
+
+	findFlag := func() string {
+		return fmt.Sprintf("Consumer[%s][%s/%s/%s/%s] find provider[%s][%s/%s/%s/%s] instance[%s]",
+			request.ConsumerServiceId, service.ServiceInfo.Environment, service.ServiceInfo.AppId, service.ServiceInfo.ServiceName, service.ServiceInfo.Version,
+			provider.ServiceInfo.ServiceId, provider.ServiceInfo.Environment, provider.ServiceInfo.AppId, provider.ServiceInfo.ServiceName, provider.ServiceInfo.Version,
+			request.ProviderInstanceId)
+	}
+
+	domain := util.ParseDomain(ctx)
+	project := util.ParseProject(ctx)
+	filter = bson.M{
+		Domain:  domain,
+		Project: project,
+		strings.Join([]string{InstanceInfo, ServiceID}, "."): request.ProviderServiceId}
+	findOneRes, err := client.GetMongoClient().FindOne(ctx, CollectionInstance, filter)
+	if err != nil {
+		mes := fmt.Errorf("%s failed, provider instance does not exist", findFlag())
+		log.Errorf(mes, "FindInstances.GetWithProviderID failed")
+		return &pb.GetOneInstanceResponse{
+			Response: pb.CreateResponse(pb.ErrInstanceNotExists, mes.Error()),
+		}, nil
+	}
+	var instance Instance
+	err = findOneRes.Decode(&instance)
+	if err != nil {
+		log.Errorf(err, "FindInstances.GetWithProviderID failed, %s failed", findFlag())
+		return &pb.GetOneInstanceResponse{
+			Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+		}, err
+	}
+
+	return &pb.GetOneInstanceResponse{
+		Response: pb.CreateResponse(pb.ResponseSuccess, "Get instance successfully."),
+		Instance: instance.InstanceInfo,
+	}, nil
 }
 
 func (ds *DataSource) GetInstances(ctx context.Context, request *pb.GetInstancesRequest) (*pb.GetInstancesResponse, error) {
-	return &pb.GetInstancesResponse{}, nil
+	service := &Service{}
+	var err error
+
+	if len(request.ConsumerServiceId) > 0 {
+		filter := GeneratorServiceFilter(ctx, request.ConsumerServiceId)
+		service, err = GetService(ctx, filter)
+		if err != nil {
+			log.Errorf(err, "get consumer failed, consumer[%s] find provider instances",
+				request.ConsumerServiceId, request.ProviderServiceId)
+			return &pb.GetInstancesResponse{
+				Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+			}, err
+		}
+		if service == nil {
+			log.Errorf(nil, "consumer does not exist, consumer[%s] find provider instances",
+				request.ConsumerServiceId, request.ProviderServiceId)
+			return &pb.GetInstancesResponse{
+				Response: pb.CreateResponse(pb.ErrServiceNotExists,
+					fmt.Sprintf("Consumer[%s] does not exist.", request.ConsumerServiceId)),
+			}, nil
+		}
+	}
+
+	filter := GeneratorServiceFilter(ctx, request.ProviderServiceId)
+	provider, err := GetService(ctx, filter)
+	if err != nil {
+		log.Errorf(err, "get provider failed, consumer[%s] find provider instances",
+			request.ConsumerServiceId, request.ProviderServiceId)
+		return &pb.GetInstancesResponse{
+			Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+		}, err
+	}
+	if provider == nil {
+		log.Errorf(nil, "provider does not exist, consumer[%s] find provider instances",
+			request.ConsumerServiceId, request.ProviderServiceId)
+		return &pb.GetInstancesResponse{
+			Response: pb.CreateResponse(pb.ErrServiceNotExists,
+				fmt.Sprintf("Provider[%s] does not exist.", request.ProviderServiceId)),
+		}, nil
+	}
+
+	findFlag := fmt.Sprintf("Consumer[%s][%s/%s/%s/%s] find provider[%s][%s/%s/%s/%s] instances",
+		request.ConsumerServiceId, service.ServiceInfo.Environment, service.ServiceInfo.AppId, service.ServiceInfo.ServiceName, service.ServiceInfo.Version,
+		provider.ServiceInfo.ServiceId, provider.ServiceInfo.Environment, provider.ServiceInfo.AppId, provider.ServiceInfo.ServiceName, provider.ServiceInfo.Version)
+
+	domain := util.ParseDomain(ctx)
+	project := util.ParseProject(ctx)
+	filter = bson.M{
+		Domain:  domain,
+		Project: project,
+		strings.Join([]string{InstanceInfo, ServiceID}, "."): request.ProviderServiceId}
+	resp, err := client.GetMongoClient().Find(ctx, CollectionInstance, filter)
+	if err != nil {
+		log.Errorf(err, "FindInstancesCache.Get failed, %s failed", findFlag)
+		return &pb.GetInstancesResponse{
+			Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+		}, err
+	}
+	if resp == nil {
+		mes := fmt.Errorf("%s failed, provider does not exist", findFlag)
+		log.Errorf(mes, "FindInstancesCache.Get failed")
+		return &pb.GetInstancesResponse{
+			Response: pb.CreateResponse(pb.ErrServiceNotExists, mes.Error()),
+		}, nil
+	}
+
+	var instances []*pb.MicroServiceInstance
+	for resp.Next(ctx) {
+		var instance Instance
+		err := resp.Decode(&instance)
+		if err != nil {
+			log.Errorf(err, "FindInstances.GetWithProviderID failed, %s failed", findFlag)
+			return &pb.GetInstancesResponse{
+				Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+			}, err
+		}
+		instances = append(instances, instance.InstanceInfo)
+	}
+
+	return &pb.GetInstancesResponse{
+		Response:  pb.CreateResponse(pb.ResponseSuccess, "Query service instances successfully."),
+		Instances: instances,
+	}, nil
 }
 
 // GetProviderInstances returns instances under the specified domain
 func (ds *DataSource) GetProviderInstances(ctx context.Context, request *pb.GetProviderInstancesRequest) (instances []*pb.MicroServiceInstance, rev string, err error) {
-	return nil, "", nil
+	domain := util.ParseDomain(ctx)
+	project := util.ParseProject(ctx)
+	filter := bson.M{
+		Domain:  domain,
+		Project: project,
+		strings.Join([]string{InstanceInfo, ServiceID}, "."): request.ProviderServiceId}
+
+	findRes, err := client.GetMongoClient().Find(ctx, CollectionInstance, filter)
+	if err != nil {
+		return
+	}
+
+	for findRes.Next(ctx) {
+		var mongoInstance Instance
+		err := findRes.Decode(&mongoInstance)
+		if err == nil {
+			instances = append(instances, mongoInstance.InstanceInfo)
+		}
+	}
+
+	return instances, "", nil
 }
 
 func (ds *DataSource) GetAllInstances(ctx context.Context, request *pb.GetAllInstancesRequest) (*pb.GetAllInstancesResponse, error) {
-	return &pb.GetAllInstancesResponse{}, nil
+
+	domain := util.ParseDomain(ctx)
+	project := util.ParseProject(ctx)
+
+	filter := bson.M{Domain: domain, Project: project}
+
+	findRes, err := client.GetMongoClient().Find(ctx, CollectionInstance, filter)
+	if err != nil {
+		return nil, err
+	}
+	resp := &pb.GetAllInstancesResponse{
+		Response: pb.CreateResponse(pb.ResponseSuccess, "Get all instances successfully"),
+	}
+
+	for findRes.Next(ctx) {
+		var instance Instance
+		err := findRes.Decode(&instance)
+		if err != nil {
+			return &pb.GetAllInstancesResponse{
+				Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+			}, err
+		}
+		resp.Instances = append(resp.Instances, instance.InstanceInfo)
+	}
+
+	return resp, nil
 }
 
 func (ds *DataSource) BatchGetProviderInstances(ctx context.Context, request *pb.BatchGetInstancesRequest) (instances []*pb.MicroServiceInstance, rev string, err error) {
-	return nil, "", nil
+	if request == nil || len(request.ServiceIds) == 0 {
+		return nil, "", fmt.Errorf("invalid param BatchGetInstancesRequest")
+	}
+
+	domain := util.ParseDomain(ctx)
+	project := util.ParseProject(ctx)
+
+	for _, providerServiceID := range request.ServiceIds {
+		// todo  finish find instances
+
+		filter := bson.M{
+			Domain:  domain,
+			Project: project,
+			strings.Join([]string{InstanceInfo, ServiceID}, "."): providerServiceID}
+		findRes, err := client.GetMongoClient().Find(ctx, CollectionInstance, filter)
+		if err != nil {
+			return instances, "", nil
+		}
+
+		for findRes.Next(ctx) {
+			var mongoInstance Instance
+			err := findRes.Decode(&mongoInstance)
+			if err == nil {
+				instances = append(instances, mongoInstance.InstanceInfo)
+			}
+		}
+	}
+
+	return instances, "", nil
 }
 
 // FindInstances returns instances under the specified domain
 func (ds *DataSource) FindInstances(ctx context.Context, request *pb.FindInstancesRequest) (*pb.FindInstancesResponse, error) {
-	return &pb.FindInstancesResponse{}, nil
+	provider := &pb.MicroServiceKey{
+		Tenant:      util.ParseTargetDomainProject(ctx),
+		Environment: request.Environment,
+		AppId:       request.AppId,
+		ServiceName: request.ServiceName,
+		Alias:       request.ServiceName,
+		Version:     request.VersionRule,
+	}
+
+	return ds.findInstance(ctx, request, provider)
 }
 
 func (ds *DataSource) UpdateInstanceStatus(ctx context.Context, request *pb.UpdateInstanceStatusRequest) (*pb.UpdateInstanceStatusResponse, error) {
-	return &pb.UpdateInstanceStatusResponse{}, nil
+	updateStatusFlag := util.StringJoin([]string{request.ServiceId, request.InstanceId, request.Status}, "/")
+
+	// todo finish get instance
+	instance, err := GetInstance(ctx, request.ServiceId, request.InstanceId)
+	if err != nil {
+		log.Errorf(err, "update instance[%s] status failed", updateStatusFlag)
+		return &pb.UpdateInstanceStatusResponse{
+			Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+		}, err
+	}
+	if instance == nil {
+		log.Errorf(nil, "update instance[%s] status failed, instance does not exist", updateStatusFlag)
+		return &pb.UpdateInstanceStatusResponse{
+			Response: pb.CreateResponse(pb.ErrInstanceNotExists, "Service instance does not exist."),
+		}, nil
+	}
+
+	copyInstanceRef := *instance
+	copyInstanceRef.InstanceInfo.Status = request.Status
+
+	if err := UpdateInstanceS(ctx, copyInstanceRef.InstanceInfo); err != nil {
+		log.Errorf(err, "update instance[%s] status failed", updateStatusFlag)
+		resp := &pb.UpdateInstanceStatusResponse{
+			Response: pb.CreateResponseWithSCErr(err),
+		}
+		if err.InternalError() {
+			return resp, err
+		}
+		return resp, nil
+	}
+
+	log.Infof("update instance[%s] status successfully", updateStatusFlag)
+	return &pb.UpdateInstanceStatusResponse{
+		Response: pb.CreateResponse(pb.ResponseSuccess, "Update service instance status successfully."),
+	}, nil
 }
 
 func (ds *DataSource) UpdateInstanceProperties(ctx context.Context, request *pb.UpdateInstancePropsRequest) (*pb.UpdateInstancePropsResponse, error) {
-	return &pb.UpdateInstancePropsResponse{}, nil
+	instanceFlag := util.StringJoin([]string{request.ServiceId, request.InstanceId}, "/")
+
+	instance, err := GetInstance(ctx, request.ServiceId, request.InstanceId)
+	if err != nil {
+		log.Errorf(err, "update instance[%s] properties failed", instanceFlag)
+		return &pb.UpdateInstancePropsResponse{
+			Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+		}, err
+	}
+	if instance == nil {
+		log.Errorf(nil, "update instance[%s] properties failed, instance does not exist", instanceFlag)
+		return &pb.UpdateInstancePropsResponse{
+			Response: pb.CreateResponse(pb.ErrInstanceNotExists, "Service instance does not exist."),
+		}, nil
+	}
+
+	copyInstanceRef := *instance
+	copyInstanceRef.InstanceInfo.Properties = request.Properties
+
+	// todo finish update instance
+	if err := UpdateInstanceP(ctx, copyInstanceRef.InstanceInfo); err != nil {
+		log.Errorf(err, "update instance[%s] properties failed", instanceFlag)
+		resp := &pb.UpdateInstancePropsResponse{
+			Response: pb.CreateResponseWithSCErr(err),
+		}
+		if err.InternalError() {
+			return resp, err
+		}
+		return resp, nil
+	}
+
+	log.Infof("update instance[%s] properties successfully", instanceFlag)
+	return &pb.UpdateInstancePropsResponse{
+		Response: pb.CreateResponse(pb.ResponseSuccess, "Update service instance properties successfully."),
+	}, nil
 }
 
 func (ds *DataSource) UnregisterInstance(ctx context.Context, request *pb.UnregisterInstanceRequest) (*pb.UnregisterInstanceResponse, error) {
-	return &pb.UnregisterInstanceResponse{}, nil
+	remoteIP := util.GetIPFromContext(ctx)
+	serviceID := request.ServiceId
+	instanceID := request.InstanceId
+
+	instanceFlag := util.StringJoin([]string{serviceID, instanceID}, "/")
+
+	// todo finish revoke instance
+	//err := revokeInstance(ctx, "", serviceID, instanceID)
+	//if err != nil {
+	//	log.Errorf(err, "unregister instance failed, instance[%s], operator %s: revoke instance failed",
+	//		instanceFlag, remoteIP)
+	//	resp := &pb.UnregisterInstanceResponse{

Review comment:
       1




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#issuecomment-733636238


   conflicts


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r530926496



##########
File path: datasource/mongo/heartbeat/heartbeatchecker/heartbeat_test.go
##########
@@ -57,8 +57,8 @@ func TestUpdateInstanceRefreshTime(t *testing.T) {
 		err = updateInstanceRefreshTime(context.Background(), instance1.InstanceInfo.ServiceId, instance1.InstanceInfo.InstanceId)
 		assert.Equal(t, nil, err)
 		filter := bson.M{
-			mongo.InstanceID: instance1.InstanceInfo.InstanceId,
-			mongo.ServiceID:  instance1.InstanceInfo.ServiceId,
+			mongo.InstanceInstanceID: instance1.InstanceInfo.InstanceId,
+			mongo.InstanceServiceID:  instance1.InstanceInfo.ServiceId,

Review comment:
       这名字重复




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r533337431



##########
File path: datasource/mongo/heartbeat/common.go
##########
@@ -0,0 +1,25 @@
+/*
+ * 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 request 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 request 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 heartbeat
+
+import "errors"
+
+var (
+	ErrPluginNameNil    = errors.New("plugin implement name is nil")

Review comment:
       复用




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r530927638



##########
File path: datasource/mongo/database.go
##########
@@ -65,13 +67,17 @@ const (
 	ServiceAlias       = "serviceinfo.alias"
 	ServiceVersion     = "serviceinfo.version"
 	ServiceSchemas     = "serviceinfo.schemas"
+	ServiceEnvironment = "serviceinfo.environment"
 	RuleAttribute      = "ruleinfo.attribute"
 	RulePattern        = "ruleinfo.pattern"
 	RuleModTime        = "ruleinfo.modtimestamp"
 	RuleDescription    = "ruleinfo.description"
 	RuleRuletype       = "ruleinfo.ruletype"
 	SchemaInfo         = "schemainfo"
 	SchemaSummary      = "schemasummary"
+	DepConEnv          = "consumerdependency.consumer.environment"
+	DepConAppID        = "consumerdependency.consumer.appid"

Review comment:
       这consume在2处有,什么意思,不是冗余信息?

##########
File path: datasource/mongo/database.go
##########
@@ -31,8 +31,8 @@ const (
 	AccountTokenExpirationTime = "tokenexpirationtime"
 	AccountCurrentPassword     = "currentpassword"
 	AccountStatus              = "status"
-	InstanceID                 = "instanceinfo.instanceid"
-	ServiceID                  = "instanceinfo.serviceid"
+	InstanceInstanceID         = "instanceinfo.instanceid"

Review comment:
       here




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r530937313



##########
File path: datasource/mongo/database.go
##########
@@ -65,13 +67,17 @@ const (
 	ServiceAlias       = "serviceinfo.alias"
 	ServiceVersion     = "serviceinfo.version"
 	ServiceSchemas     = "serviceinfo.schemas"
+	ServiceEnvironment = "serviceinfo.environment"
 	RuleAttribute      = "ruleinfo.attribute"
 	RulePattern        = "ruleinfo.pattern"
 	RuleModTime        = "ruleinfo.modtimestamp"
 	RuleDescription    = "ruleinfo.description"
 	RuleRuletype       = "ruleinfo.ruletype"
 	SchemaInfo         = "schemainfo"
 	SchemaSummary      = "schemasummary"
+	DepConEnv          = "consumerdependency.consumer.environment"

Review comment:
       here

##########
File path: datasource/mongo/database.go
##########
@@ -31,8 +31,8 @@ const (
 	AccountTokenExpirationTime = "tokenexpirationtime"
 	AccountCurrentPassword     = "currentpassword"
 	AccountStatus              = "status"
-	InstanceID                 = "instanceinfo.instanceid"
-	ServiceID                  = "instanceinfo.serviceid"
+	InstanceInstanceID         = "instanceinfo.instanceid"

Review comment:
       here




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r533099892



##########
File path: datasource/mongo/dep_test.go
##########
@@ -0,0 +1,350 @@
+/*
+* 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 mongo_test
+
+import (
+	"github.com/apache/servicecomb-service-center/datasource"
+	pb "github.com/go-chassis/cari/discovery"
+	"github.com/stretchr/testify/assert"
+	"testing"
+)
+
+func TestDep_Creat(t *testing.T) {
+
+	var (
+		consumerId1 string
+		consumerId3 string
+	)
+
+	t.Run("creat service", func(t *testing.T) {
+		respCreateService, err := datasource.Instance().RegisterService(getContext(), &pb.CreateServiceRequest{
+			Service: &pb.MicroService{
+				ServiceId:   "dep1",
+				AppId:       "create_dep_group",
+				ServiceName: "create_dep_consumer",
+				Version:     "1.0.0",
+				Level:       "FRONT",
+				Status:      pb.MS_UP,
+			},
+		})
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateService.Response.GetCode())
+		consumerId1 = respCreateService.ServiceId
+
+		respCreateService, err = datasource.Instance().RegisterService(getContext(), &pb.CreateServiceRequest{
+			Service: &pb.MicroService{
+				ServiceId:   "dep2",
+				AppId:       "create_dep_group",
+				ServiceName: "create_dep_consumer_all",
+				Version:     "1.0.0",
+				Level:       "FRONT",
+				Status:      pb.MS_UP,
+			},
+		})
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateService.Response.GetCode())
+		consumerId3 = respCreateService.ServiceId
+
+		respCreateService, err = datasource.Instance().RegisterService(getContext(), &pb.CreateServiceRequest{
+			Service: &pb.MicroService{
+				ServiceId:   "dep3",
+				Environment: pb.ENV_PROD,
+				AppId:       "create_dep_group",
+				ServiceName: "create_dep_consumer",
+				Version:     "1.0.0",
+				Level:       "FRONT",
+				Status:      pb.MS_UP,
+			},
+		})
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateService.Response.GetCode())
+
+		respCreateService, err = datasource.Instance().RegisterService(getContext(), &pb.CreateServiceRequest{
+			Service: &pb.MicroService{
+				ServiceId:   "dep4",
+				AppId:       "create_dep_group",
+				ServiceName: "create_dep_provider",
+				Version:     "1.0.0",
+				Level:       "FRONT",
+				Status:      pb.MS_UP,
+			},
+		})
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateService.Response.GetCode())
+
+		respCreateService, err = datasource.Instance().RegisterService(getContext(), &pb.CreateServiceRequest{
+			Service: &pb.MicroService{
+				ServiceId:   "dep5",
+				AppId:       "create_dep_group",
+				ServiceName: "create_dep_provider",
+				Version:     "1.0.1",
+				Level:       "FRONT",
+				Status:      pb.MS_UP,
+			},
+		})
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateService.Response.GetCode())
+
+		respCreateService, err = datasource.Instance().RegisterService(getContext(), &pb.CreateServiceRequest{
+			Service: &pb.MicroService{
+				ServiceId:   "dep6",
+				Environment: pb.ENV_PROD,
+				AppId:       "create_dep_group",
+				ServiceName: "create_dep_provider",
+				Version:     "1.0.0",
+				Level:       "FRONT",
+				Status:      pb.MS_UP,
+			},
+		})
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateService.Response.GetCode())
+	})
+
+	t.Run("when request is valid, should be passed", func(t *testing.T) {
+
+		respCreateDependency, err := datasource.Instance().AddOrUpdateDependencies(getContext(), []*pb.ConsumerDependency{
+			{
+				Consumer: &pb.MicroServiceKey{
+					ServiceName: "create_dep_consumer",
+					AppId:       "create_dep_group",
+					Version:     "1.0.0",
+				},
+				Providers: []*pb.MicroServiceKey{
+					{
+						AppId:       "create_dep_group",
+						ServiceName: "create_dep_provider",
+						Version:     "latest",
+					},
+				},
+			},
+		}, false)
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateDependency.GetCode())
+
+		respPro, err := datasource.Instance().SearchConsumerDependency(getContext(), &pb.GetDependenciesRequest{
+			ServiceId: consumerId1,
+		})
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respPro.Response.GetCode())
+		assert.NotEqual(t, "1.0.1", respPro.Providers[0].Version)
+
+		respCreateDependency, err = datasource.Instance().AddOrUpdateDependencies(getContext(), []*pb.ConsumerDependency{
+			{
+				Consumer: &pb.MicroServiceKey{
+					ServiceName: "create_dep_consumer",
+					AppId:       "create_dep_group",
+					Version:     "1.0.0",
+				},
+				Providers: []*pb.MicroServiceKey{
+					{
+						AppId:       "create_dep_group",
+						ServiceName: "create_dep_provider",
+						Version:     "1.0.0+",
+					},
+				},
+			},
+		}, false)
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateDependency.GetCode())
+
+		respPro, err = datasource.Instance().SearchConsumerDependency(getContext(), &pb.GetDependenciesRequest{
+			ServiceId: consumerId1,
+		})
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respPro.Response.GetCode())
+		assert.NotEqual(t, "1.0.1", respPro.Providers[0].Version)
+
+		respCreateDependency, err = datasource.Instance().AddOrUpdateDependencies(getContext(), []*pb.ConsumerDependency{
+			{
+				Consumer: &pb.MicroServiceKey{
+					ServiceName: "create_dep_consumer",
+					AppId:       "create_dep_group",
+					Version:     "1.0.0",
+				},
+				Providers: []*pb.MicroServiceKey{
+					{
+						AppId:       "create_dep_group",
+						ServiceName: "create_dep_provider",
+						Version:     "1.0.0+",
+					},
+				},
+			},
+		}, false)
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateDependency.GetCode())
+
+		respPro, err = datasource.Instance().SearchConsumerDependency(getContext(), &pb.GetDependenciesRequest{
+			ServiceId: consumerId1,
+		})
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respPro.Response.GetCode())
+		assert.NotEqual(t, 2, len(respPro.Providers))
+
+		respCreateDependency, err = datasource.Instance().AddOrUpdateDependencies(getContext(), []*pb.ConsumerDependency{
+			{
+				Consumer: &pb.MicroServiceKey{
+					ServiceName: "create_dep_consumer_all",
+					AppId:       "create_dep_group",
+					Version:     "1.0.0",
+				},
+				Providers: []*pb.MicroServiceKey{
+					{
+						ServiceName: "*",
+					},
+				},
+			},
+		}, false)
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateDependency.GetCode())
+
+		respPro, err = datasource.Instance().SearchConsumerDependency(getContext(), &pb.GetDependenciesRequest{
+			ServiceId: consumerId3,
+		})
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respPro.Response.GetCode())
+		assert.NotEqual(t, 0, len(respPro.Providers))
+
+		respCreateDependency, err = datasource.Instance().AddOrUpdateDependencies(getContext(), []*pb.ConsumerDependency{
+			{
+				Consumer: &pb.MicroServiceKey{
+					ServiceName: "create_dep_consumer_all",
+					AppId:       "create_dep_group",
+					Version:     "1.0.0",
+				},
+				Providers: nil,
+			},
+		}, false)
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateDependency.GetCode())
+
+		respCreateDependency, err = datasource.Instance().AddOrUpdateDependencies(getContext(), []*pb.ConsumerDependency{
+			{
+				Consumer: &pb.MicroServiceKey{
+					ServiceName: "create_dep_consumer",
+					AppId:       "create_dep_group",
+					Version:     "1.0.0",
+				},
+				Providers: []*pb.MicroServiceKey{
+					{
+						AppId:       "create_dep_group",
+						ServiceName: "create_dep_provider",
+						Version:     "1.0.0",
+					},
+					{
+						ServiceName: "*",
+					},
+				},
+			},
+		}, false)
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateDependency.GetCode())
+
+		respCreateDependency, err = datasource.Instance().AddOrUpdateDependencies(getContext(), []*pb.ConsumerDependency{
+			{
+				Consumer: &pb.MicroServiceKey{
+					ServiceName: "create_dep_consumer",
+					AppId:       "create_dep_group",
+					Version:     "1.0.0",
+				},
+				Providers: []*pb.MicroServiceKey{
+					{
+						AppId:       "create_dep_group",
+						ServiceName: "create_dep_provider",
+						Version:     "1.0.0-1.0.1",
+					},
+				},
+			},
+		}, false)
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respCreateDependency.GetCode())
+
+		respPro, err = datasource.Instance().SearchConsumerDependency(getContext(), &pb.GetDependenciesRequest{
+			ServiceId: consumerId1,
+		})
+		assert.NoError(t, err)
+		assert.Equal(t, pb.ResponseSuccess, respPro.Response.GetCode())
+		assert.Equal(t, "1.0.0", respPro.Providers[0].Version)
+	})
+}
+
+func TestDep_Get(t *testing.T) {
+
+	var (
+		consumerId1 string
+		providerId1 string
+	)
+
+	t.Run("execute 'get' operartion", func(t *testing.T) {

Review comment:
       这个ut文件内的test case请按ut的标准格式写name




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#issuecomment-736184193


   解决下冲突


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r533107261



##########
File path: datasource/mongo/dep.go
##########
@@ -21,20 +21,354 @@ import (
 	"context"
 
 	pb "github.com/go-chassis/cari/discovery"
+	"go.mongodb.org/mongo-driver/bson"
+
+	"errors"
+	"fmt"
+	"strings"
+
+	"github.com/apache/servicecomb-service-center/datasource"
+	"github.com/apache/servicecomb-service-center/datasource/mongo/client"
+	"github.com/apache/servicecomb-service-center/pkg/log"
+	"github.com/apache/servicecomb-service-center/pkg/util"
 )
 
 func (ds *DataSource) SearchProviderDependency(ctx context.Context, request *pb.GetDependenciesRequest) (*pb.GetProDependenciesResponse, error) {
-	return &pb.GetProDependenciesResponse{}, nil
+	providerServiceID := request.ServiceId
+	filter := GeneratorServiceFilter(ctx, providerServiceID)
+	provider, err := GetService(ctx, filter)
+	if err != nil {
+		log.Error("GetProviderDependencies failed, provider is "+providerServiceID, err)
+		return nil, err
+	}
+	if provider == nil {
+		log.Error(fmt.Sprintf("GetProviderDependencies failed for provider %s", providerServiceID), err)
+		return &pb.GetProDependenciesResponse{
+			Response: pb.CreateResponse(pb.ErrServiceNotExists, "Provider does not exist"),
+		}, nil
+	}
+
+	services, err := GetDependencyProviders(ctx, provider.ServiceInfo, request)
+	if err != nil {
+		log.Error(fmt.Sprintf("GetProviderDependencies failed, provider is %s/%s/%s/%s",
+			provider.ServiceInfo.Environment, provider.ServiceInfo.AppId, provider.ServiceInfo.ServiceName, provider.ServiceInfo.Version), err)
+		return &pb.GetProDependenciesResponse{
+			Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+		}, err
+	}
+
+	return &pb.GetProDependenciesResponse{
+		Response:  pb.CreateResponse(pb.ResponseSuccess, "Get all consumers successful."),
+		Consumers: services,
+	}, nil
 }
 
 func (ds *DataSource) SearchConsumerDependency(ctx context.Context, request *pb.GetDependenciesRequest) (*pb.GetConDependenciesResponse, error) {
-	return &pb.GetConDependenciesResponse{}, nil
+	consumerID := request.ServiceId
+
+	filter := GeneratorServiceFilter(ctx, consumerID)
+	consumer, err := GetService(ctx, filter)
+	if err != nil {
+		log.Error(fmt.Sprintf("GetConsumerDependencies failed, consumer is %s", consumerID), err)
+		return nil, err
+	}
+	if consumer == nil {
+		log.Error(fmt.Sprintf("GetConsumerDependencies failed for consumer %s does not exist", consumerID), err)
+		return &pb.GetConDependenciesResponse{
+			Response: pb.CreateResponse(pb.ErrServiceNotExists, "Consumer does not exist"),
+		}, nil
+	}
+
+	services, err := GetDependencyProviders(ctx, consumer.ServiceInfo, request)
+	if err != nil {
+		log.Error(fmt.Sprintf("GetConsumerDependencies failed, consumer is %s/%s/%s/%s",
+			consumer.ServiceInfo.Environment, consumer.ServiceInfo.AppId, consumer.ServiceInfo.ServiceName, consumer.ServiceInfo.Version), err)
+		return &pb.GetConDependenciesResponse{
+			Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+		}, err
+	}
+
+	return &pb.GetConDependenciesResponse{
+		Response:  pb.CreateResponse(pb.ResponseSuccess, "Get all providers successfully."),
+		Providers: services,
+	}, nil
 }
 
 func (ds *DataSource) AddOrUpdateDependencies(ctx context.Context, dependencyInfos []*pb.ConsumerDependency, override bool) (*pb.Response, error) {
-	return pb.CreateResponse(pb.ResponseSuccess, ""), nil
+	domainProject := util.ParseDomainProject(ctx)
+	for _, dependencyInfo := range dependencyInfos {
+		consumerFlag := util.StringJoin([]string{
+			dependencyInfo.Consumer.Environment,
+			dependencyInfo.Consumer.AppId,
+			dependencyInfo.Consumer.ServiceName,
+			dependencyInfo.Consumer.Version}, "/")
+		consumerInfo := pb.DependenciesToKeys([]*pb.MicroServiceKey{dependencyInfo.Consumer}, domainProject)[0]
+		providersInfo := pb.DependenciesToKeys(dependencyInfo.Providers, domainProject)
+
+		rsp := datasource.ParamsChecker(consumerInfo, providersInfo)
+		if rsp != nil {
+			fmt.Print("put request into dependency queue failed, override: ", override, " consumer is",

Review comment:
       绝对不可以用fmt




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r531822882



##########
File path: datasource/mongo/ms_test.go
##########
@@ -650,3 +652,503 @@ func TestRuleUpdate(t *testing.T) {
 //		assert.Equal(t, 1, len(resSchemas.Schemas))
 //	})
 //}
+
+func TestInstance_Creat(t *testing.T) {
+	var serviceId string
+
+	t.Run("create service", func(t *testing.T) {
+		insertRes, err := datasource.Instance().RegisterService(getContext(), &pb.CreateServiceRequest{
+			Service: &pb.MicroService{

Review comment:
       UT要有上下文,动作,结果
   
   例子:如果创建服务,且参数正确,应该成功
   

##########
File path: go.mod
##########
@@ -53,6 +53,7 @@ require (
 	github.com/urfave/cli v1.22.4
 	github.com/widuu/gojson v0.0.0-20170212122013-7da9d2cd949b
 	github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
+	go.etcd.io/bbolt v1.3.5 // indirect

Review comment:
       为何这里简介引入了bbolt?




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#issuecomment-735029374


   log.Errorf  任何以f结尾的日志方法都不允许再调用了,改用日志方法(fmt.xxx)代替


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r533099236



##########
File path: datasource/mongo/ms.go
##########
@@ -1346,56 +1373,865 @@ func SchemaExist(ctx context.Context, serviceID, schemaID string) (bool, error)
 
 // Instance management
 func (ds *DataSource) RegisterInstance(ctx context.Context, request *pb.RegisterInstanceRequest) (*pb.RegisterInstanceResponse, error) {
-	return &pb.RegisterInstanceResponse{}, nil
+	remoteIP := util.GetIPFromContext(ctx)
+	instance := request.Instance
+
+	// 允许自定义 id
+	if len(instance.InstanceId) > 0 {
+		resp, err := ds.Heartbeat(ctx, &pb.HeartbeatRequest{
+			InstanceId: instance.InstanceId,
+			ServiceId:  instance.ServiceId,
+		})
+		if resp == nil {

Review comment:
       要先判断err




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r533341612



##########
File path: datasource/mongo/engine.go
##########
@@ -22,17 +22,114 @@ import (
 	"time"
 
 	"github.com/apache/servicecomb-service-center/pkg/cluster"
+
+	"fmt"
+	"strconv"
+	"strings"
+
+	"github.com/apache/servicecomb-service-center/datasource"
+	"github.com/apache/servicecomb-service-center/datasource/etcd/path"
+	"github.com/apache/servicecomb-service-center/datasource/mongo/client"
+	"github.com/apache/servicecomb-service-center/pkg/log"
+	"github.com/apache/servicecomb-service-center/pkg/util"
+	"github.com/apache/servicecomb-service-center/server/core"
+	"github.com/apache/servicecomb-service-center/server/metrics"
+	pb "github.com/go-chassis/cari/discovery"
+	"go.mongodb.org/mongo-driver/bson"
 )
 
 func (ds *DataSource) SelfRegister(ctx context.Context) error {
-	return nil
+	err := ds.registryService(ctx)
+	if err != nil {
+		return err
+	}
+
+	// 实例信息
+	err = ds.registryInstance(ctx)
+
+	// wait heartbeat
+	ds.autoSelfHeartBeat()
+
+	metrics.ReportScInstance()
+	return err
 }
 func (ds *DataSource) SelfUnregister(ctx context.Context) error {
+	if len(core.Instance.InstanceId) == 0 {
+		return nil
+	}
+
+	ctx = core.AddDefaultContextValue(ctx)
+	respI, err := datasource.Instance().UnregisterInstance(ctx, core.UnregisterInstanceRequest())
+	if err != nil {
+		log.Error("unregister failed", err)
+		return err
+	}
+	if respI.Response.GetCode() != pb.ResponseSuccess {
+		err = fmt.Errorf("unregister service center instance[%s/%s] failed, %s",
+			core.Instance.ServiceId, core.Instance.InstanceId, respI.Response.GetMessage())
+		log.Error(err.Error(), nil)
+		return err
+	}
+	log.Warn(fmt.Sprintf("unregister service center instance[%s/%s]",
+		core.Service.ServiceId, core.Instance.InstanceId))
 	return nil
 }
 
 // OPS
 func (ds *DataSource) ClearNoInstanceServices(ctx context.Context, ttl time.Duration) error {
+	services, err := GetAllServicesAcrossDomainProject(ctx)
+	if err != nil {
+		return err
+	}
+	if len(services) == 0 {
+		log.Info("no service found, no need to clear")

Review comment:
       应该也可以复用




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] xzccfzy commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
xzccfzy commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r530887764



##########
File path: datasource/mongo/dep.go
##########
@@ -19,21 +19,338 @@ package mongo
 
 import (
 	"context"
+	"fmt"
+	"github.com/apache/servicecomb-service-center/datasource/etcd/path"
+	serviceUtil "github.com/apache/servicecomb-service-center/datasource/etcd/util"

Review comment:
       ok




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#issuecomment-735029064


   解决了,点击resolve,不想解决,继续在comment讨论,不用点resolve


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r533344235



##########
File path: datasource/mongo/engine.go
##########
@@ -22,17 +22,114 @@ import (
 	"time"
 
 	"github.com/apache/servicecomb-service-center/pkg/cluster"
+
+	"fmt"
+	"strconv"
+	"strings"
+
+	"github.com/apache/servicecomb-service-center/datasource"
+	"github.com/apache/servicecomb-service-center/datasource/etcd/path"
+	"github.com/apache/servicecomb-service-center/datasource/mongo/client"
+	"github.com/apache/servicecomb-service-center/pkg/log"
+	"github.com/apache/servicecomb-service-center/pkg/util"
+	"github.com/apache/servicecomb-service-center/server/core"
+	"github.com/apache/servicecomb-service-center/server/metrics"
+	pb "github.com/go-chassis/cari/discovery"
+	"go.mongodb.org/mongo-driver/bson"
 )
 
 func (ds *DataSource) SelfRegister(ctx context.Context) error {
-	return nil
+	err := ds.registryService(ctx)
+	if err != nil {
+		return err
+	}
+
+	// 实例信息
+	err = ds.registryInstance(ctx)
+
+	// wait heartbeat
+	ds.autoSelfHeartBeat()
+
+	metrics.ReportScInstance()
+	return err
 }
 func (ds *DataSource) SelfUnregister(ctx context.Context) error {
+	if len(core.Instance.InstanceId) == 0 {
+		return nil
+	}
+
+	ctx = core.AddDefaultContextValue(ctx)
+	respI, err := datasource.Instance().UnregisterInstance(ctx, core.UnregisterInstanceRequest())
+	if err != nil {
+		log.Error("unregister failed", err)
+		return err
+	}
+	if respI.Response.GetCode() != pb.ResponseSuccess {
+		err = fmt.Errorf("unregister service center instance[%s/%s] failed, %s",
+			core.Instance.ServiceId, core.Instance.InstanceId, respI.Response.GetMessage())
+		log.Error(err.Error(), nil)
+		return err
+	}
+	log.Warn(fmt.Sprintf("unregister service center instance[%s/%s]",
+		core.Service.ServiceId, core.Instance.InstanceId))
 	return nil
 }
 
 // OPS
 func (ds *DataSource) ClearNoInstanceServices(ctx context.Context, ttl time.Duration) error {
+	services, err := GetAllServicesAcrossDomainProject(ctx)
+	if err != nil {
+		return err
+	}
+	if len(services) == 0 {
+		log.Info("no service found, no need to clear")
+		return nil
+	}
+
+	timeLimit := time.Now().Add(0 - ttl)
+	log.Info(fmt.Sprintf("clear no-instance services created before %s", timeLimit))
+	timeLimitStamp := strconv.FormatInt(timeLimit.Unix(), 10)
+
+	for domainProject, svcList := range services {
+		if len(svcList) == 0 {
+			continue
+		}
+		ctx, err := ctxFromDomainProject(ctx, domainProject)
+		if err != nil {
+			log.Error("get domain project context failed", err)
+			continue
+		}
+		for _, svc := range svcList {
+			if svc == nil {
+				continue
+			}
+			ok, err := shouldClear(ctx, timeLimitStamp, svc)
+			if err != nil {
+				log.Error("check service clear necessity failed", err)
+				continue
+			}
+			if !ok {
+				continue
+			}
+			svcCtxStr := "domainProject: " + domainProject + ", " +

Review comment:
       这个字符串也该复用




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r530926496



##########
File path: datasource/mongo/heartbeat/heartbeatchecker/heartbeat_test.go
##########
@@ -57,8 +57,8 @@ func TestUpdateInstanceRefreshTime(t *testing.T) {
 		err = updateInstanceRefreshTime(context.Background(), instance1.InstanceInfo.ServiceId, instance1.InstanceInfo.InstanceId)
 		assert.Equal(t, nil, err)
 		filter := bson.M{
-			mongo.InstanceID: instance1.InstanceInfo.InstanceId,
-			mongo.ServiceID:  instance1.InstanceInfo.ServiceId,
+			mongo.InstanceInstanceID: instance1.InstanceInfo.InstanceId,
+			mongo.InstanceServiceID:  instance1.InstanceInfo.ServiceId,

Review comment:
       这名字重复,很多这种问题,统一改




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang merged pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang merged pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759


   


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] xzccfzy commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
xzccfzy commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r532298078



##########
File path: datasource/mongo/account.go
##########
@@ -33,15 +34,15 @@ import (
 func (ds *DataSource) CreateAccount(ctx context.Context, a *rbacframe.Account) error {
 	exist, err := ds.AccountExist(ctx, a.Name)
 	if err != nil {
-		log.Errorf(err, "can not save account info")
+		fmt.Println("can not save account info", err)

Review comment:
       xiaoliang上面的意见说是【改用日志方法(fmt.xxx)代替】




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] little-cui commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
little-cui commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r532056244



##########
File path: datasource/mongo/heartbeat/heartbeatchecker/heartbeat.go
##########
@@ -19,24 +19,25 @@ package heartbeatchecker
 
 import (
 	"context"
+	"fmt"
 	"github.com/apache/servicecomb-service-center/datasource/mongo"
 	"github.com/apache/servicecomb-service-center/datasource/mongo/client"
-	"github.com/apache/servicecomb-service-center/pkg/log"
 	"go.mongodb.org/mongo-driver/bson"
+	"strings"
 	"time"
 )
 
 func updateInstanceRefreshTime(ctx context.Context, serviceID string, instanceID string) error {
 	filter := bson.M{
-		mongo.InstanceID: instanceID,
-		mongo.ServiceID:  serviceID,
+		strings.Join([]string{mongo.ColumnInstanceInfo, mongo.ColumnInstanceID}, "."): instanceID,

Review comment:
       如果只是3个字符相加,不需要使用strings.Join,编译器会优化

##########
File path: datasource/mongo/account.go
##########
@@ -33,15 +34,15 @@ import (
 func (ds *DataSource) CreateAccount(ctx context.Context, a *rbacframe.Account) error {
 	exist, err := ds.AccountExist(ctx, a.Name)
 	if err != nil {
-		log.Errorf(err, "can not save account info")
+		fmt.Println("can not save account info", err)

Review comment:
       不能直接打印到控制台,使用log.Error()代替




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#issuecomment-736916903


   请在下一个PR修整日志和错误,字符串拼接的复用问题


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] xzccfzy commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
xzccfzy commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r532406240



##########
File path: datasource/mongo/heartbeat/heartbeatchecker/heartbeat.go
##########
@@ -19,24 +19,25 @@ package heartbeatchecker
 
 import (
 	"context"
+	"fmt"
 	"github.com/apache/servicecomb-service-center/datasource/mongo"
 	"github.com/apache/servicecomb-service-center/datasource/mongo/client"
-	"github.com/apache/servicecomb-service-center/pkg/log"
 	"go.mongodb.org/mongo-driver/bson"
+	"strings"
 	"time"
 )
 
 func updateInstanceRefreshTime(ctx context.Context, serviceID string, instanceID string) error {
 	filter := bson.M{
-		mongo.InstanceID: instanceID,
-		mongo.ServiceID:  serviceID,
+		strings.Join([]string{mongo.ColumnInstanceInfo, mongo.ColumnInstanceID}, "."): instanceID,

Review comment:
       直接使用 + 连接么?还是采用buffer.Builder?




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r533180142



##########
File path: datasource/mongo/common.go
##########
@@ -0,0 +1,34 @@
+/*
+ * 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 request 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 request 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 mongo
+
+import "errors"
+
+var (
+	ErrInvalidConsumer                      = errors.New("Invalid consumer")
+	ErrUpdateNodata                         = errors.New("UpdateAccount: no data to update")
+	ErrServiceFileLost                      = errors.New("service center service file lost")
+	ErrInvalidDomainProject                 = errors.New("invalid domainProject")

Review comment:
       这里面的很多错误,我觉得都不该是mongodb相关的错误,而是业务错误,不该在这里定义,要跟etcd实现拉齐,找找目前的错误信息,复用,不要定义2套




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r533101155



##########
File path: datasource/mongo/heartbeat/manager.go
##########
@@ -48,11 +49,11 @@ func Init(opts Options) error {
 
 func New(opts Options) (HealthCheck, error) {
 	if opts.PluginImplName == "" {
-		return nil, fmt.Errorf("plugin implement name is nil")
+		return nil, errors.New("plugin implement name is nil")

Review comment:
       这些都抽成 var ErrXXX 请学习uber的go编程规范




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] xzccfzy closed pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
xzccfzy closed pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759


   


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r531822962



##########
File path: datasource/mongo/database.go
##########
@@ -42,36 +40,41 @@ const (
 	CollectionSchema   = "schema"
 	CollectionRule     = "rule"
 	CollectionInstance = "instance"
+	CollectionDep      = "dependency"
 )
 
 const (
+	DepsQueueUUID     = "0"
 	ErrorDuplicateKey = 11000
 )
 
 const (
-	Domain             = "domain"
-	Project            = "project"
-	ServiceTag         = "tags"
-	SchemaID           = "schemaid"
-	RuleServiceID      = "serviceid"
-	RuleRuleID         = "ruleinfo.ruleid"
-	SchemaServiceID    = "serviceid"
-	ServiceServiceID   = "serviceinfo.serviceid"
-	ServiceProperty    = "serviceinfo.properties"
-	ServiceModTime     = "serviceinfo.modtimestamp"
-	ServiceEnv         = "serviceinfo.environment"
-	ServiceAppID       = "serviceinfo.appid"
-	ServiceServiceName = "serviceinfo.servicename"
-	ServiceAlias       = "serviceinfo.alias"
-	ServiceVersion     = "serviceinfo.version"
-	ServiceSchemas     = "serviceinfo.schemas"
-	RuleAttribute      = "ruleinfo.attribute"
-	RulePattern        = "ruleinfo.pattern"
-	RuleModTime        = "ruleinfo.modtimestamp"
-	RuleDescription    = "ruleinfo.description"
-	RuleRuletype       = "ruleinfo.ruletype"
-	SchemaInfo         = "schemainfo"
-	SchemaSummary      = "schemasummary"
+	Domain         = "domain"
+	Project        = "project"
+	Tag            = "tags"
+	SchemaID       = "schemaid"
+	ServiceID      = "serviceid"
+	RuleID         = "ruleid"
+	ServiceInfo    = "serviceinfo"
+	Property       = "properties"
+	ModTime        = "modtimestamp"

Review comment:
       对于字段用Column开头,collection用Collection开头,增加易读性




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] xzccfzy commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
xzccfzy commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r532298117



##########
File path: datasource/mongo/heartbeat/heartbeatchecker/heartbeat.go
##########
@@ -19,24 +19,25 @@ package heartbeatchecker
 
 import (
 	"context"
+	"fmt"
 	"github.com/apache/servicecomb-service-center/datasource/mongo"
 	"github.com/apache/servicecomb-service-center/datasource/mongo/client"
-	"github.com/apache/servicecomb-service-center/pkg/log"
 	"go.mongodb.org/mongo-driver/bson"
+	"strings"
 	"time"
 )
 
 func updateInstanceRefreshTime(ctx context.Context, serviceID string, instanceID string) error {
 	filter := bson.M{
-		mongo.InstanceID: instanceID,
-		mongo.ServiceID:  serviceID,
+		strings.Join([]string{mongo.ColumnInstanceInfo, mongo.ColumnInstanceID}, "."): instanceID,

Review comment:
       好的




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] little-cui commented on a change in pull request #759: [SCB-2094] implement mongo instance/dependence/engine interface

Posted by GitBox <gi...@apache.org>.
little-cui commented on a change in pull request #759:
URL: https://github.com/apache/servicecomb-service-center/pull/759#discussion_r530205097



##########
File path: datasource/mongo/dep.go
##########
@@ -19,21 +19,338 @@ package mongo
 
 import (
 	"context"
+	"fmt"
+	"github.com/apache/servicecomb-service-center/datasource/etcd/path"
+	serviceUtil "github.com/apache/servicecomb-service-center/datasource/etcd/util"

Review comment:
       不能依赖etcd




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org