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/23 00:59:16 UTC

[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #752: SCB-2094 implement basic service/rule/schema/tag interface

tianxiaoliang commented on a change in pull request #752:
URL: https://github.com/apache/servicecomb-service-center/pull/752#discussion_r528433346



##########
File path: datasource/mongo/database.go
##########
@@ -38,6 +37,66 @@ const (
 	RefreshTime                = "refreshtime"
 )
 
+const (
+	CollectionAccount = "account"
+	CollectionService = "service"
+	CollectionSchema  = "schema"
+	CollectionRule    = "rule"
+)
+
+const (
+	ErrorDuplicateKey = 11000
+)
+
+const (
+	Domain             = "domain"
+	Project            = "project"
+	ServiceTag         = "tags"
+	SchemaID           = "schemaid"
+	RuleServiceID      = "serviceid"
+	RuleRuleID         = "rule.ruleid"
+	SchemaServiceID    = "serviceid"
+	ServiceServiceID   = "service.serviceid"
+	ServiceProperty    = "service.properties"
+	ServiceModTime     = "service.modtimestamp"
+	ServiceEnv         = "service.environment"
+	ServiceAppID       = "service.appid"
+	ServiceServiceName = "service.servicename"
+	ServiceAlias       = "service.alias"
+	ServiceVersion     = "service.version"
+	ServiceSchemas     = "service.schemas"
+	RuleAttribute      = "rule.attribute"
+	RulePattern        = "rule.pattern"
+	RuleModTime        = "rule.modtimestamp"
+	RuleDescription    = "rule.description"
+	RuleRuletype       = "rule.ruletype"
+	Schema             = "schema"
+	SchemaSummary      = "schemasummary"
+)
+
+type MgService struct {

Review comment:
       mg是什么意思

##########
File path: datasource/mongo/ms.go
##########
@@ -19,164 +19,1433 @@ package mongo
 
 import (
 	"context"
+	"errors"
+	"fmt"
+	serviceUtil "github.com/apache/servicecomb-service-center/datasource/etcd/util"
+	"github.com/apache/servicecomb-service-center/datasource/mongo/client"
 	"github.com/apache/servicecomb-service-center/datasource/mongo/heartbeat"
+	"github.com/apache/servicecomb-service-center/pkg/log"
 	pb "github.com/apache/servicecomb-service-center/pkg/registry"
+	"github.com/apache/servicecomb-service-center/pkg/util"
+	apt "github.com/apache/servicecomb-service-center/server/core"
+	"github.com/apache/servicecomb-service-center/server/plugin/uuid"
+	scerr "github.com/apache/servicecomb-service-center/server/scerror"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/mongo"
+	"go.mongodb.org/mongo-driver/mongo/options"
+	"strconv"
+	"time"
 )
 
 func (ds *DataSource) RegisterService(ctx context.Context, request *pb.CreateServiceRequest) (
 	*pb.CreateServiceResponse, error) {
-	return &pb.CreateServiceResponse{}, nil
-}
+	service := request.Service
+	serviceUtil.SetServiceDefaultValue(service)
 
-func (ds *DataSource) GetServices(ctx context.Context, request *pb.GetServicesRequest) (*pb.GetServicesResponse, error) {
-	return &pb.GetServicesResponse{}, nil
-}
+	domain := util.ParseDomain(ctx)
+	project := util.ParseProject(ctx)
+	//todo add quota check
+	requestServiceID := service.ServiceId
 
-func (ds *DataSource) GetService(ctx context.Context, request *pb.GetServiceRequest) (*pb.GetServiceResponse, error) {
-	return &pb.GetServiceResponse{}, nil
-}
+	if len(requestServiceID) == 0 {
+		ctx = util.SetContext(ctx, uuid.ContextKey, util.StringJoin([]string{domain, project, service.Environment, service.AppId, service.ServiceName, service.Alias, service.Version}, "/"))
+		service.ServiceId = uuid.Generator().GetServiceID(ctx)
+	}
+	// the service unique index in table is (serviceId,serviceEnv,serviceAppid,servicename,serviceAlias,serviceVersion)
+	existID, err := ServiceExistID(ctx, service.ServiceId)
+	if err != nil {
+		return &pb.CreateServiceResponse{
+			Response: pb.CreateResponse(scerr.ErrInternal, "Check service exist failed"),
+		}, err
+	}
+	exist, err := ServiceExist(ctx, &pb.MicroServiceKey{
+		Environment: service.Environment,
+		AppId:       service.AppId,
+		ServiceName: service.ServiceName,
+		Alias:       service.Alias,
+		Version:     service.Version,
+	})
+	if err != nil {
+		return &pb.CreateServiceResponse{
+			Response: pb.CreateResponse(scerr.ErrInternal, "Check service exist failed"),
+		}, err
+	}
+	if existID || exist {
+		return &pb.CreateServiceResponse{
+			Response: pb.CreateResponse(scerr.ErrServiceAlreadyExists, "ServiceID conflict or found the same service."),
+		}, nil
+	}
+	insertRes, err := client.GetMongoClient().Insert(ctx, CollectionService, &MgService{Domain: domain, Project: project, Service: service})
+	if err != nil {
+		switch tt := err.(type) {
+		case mongo.WriteException:

Review comment:
       嵌套太多,难以阅读,必须重构

##########
File path: datasource/mongo/mongo.go
##########
@@ -49,5 +49,24 @@ func NewDataSource(opts datasource.Options) (datasource.DataSource, error) {
 }
 
 func (ds *DataSource) initialize() error {
+	//err := ds.initClient()
+	//if err != nil {
+	//	log.Errorf(err, "initialize mongo client failed")
+	//}
+
 	return nil
 }
+
+//func (ds *DataSource) initClient() error {

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