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/12/07 01:45:05 UTC
[GitHub] [servicecomb-service-center] xzccfzy opened a new pull request #776: [SCB-2094] add dep UT, implement dep query interface
xzccfzy opened a new pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776
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).
- [ ] Never comment source code, delete it.
- [ ] UT should has "context, subject, expected result" result as test case name, when you call t.Run().
---
----------------------------------------------------------------
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 #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
xzccfzy commented on a change in pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776#discussion_r540623102
##########
File path: datasource/mongo/dependency_query.go
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/apache/servicecomb-service-center/datasource/mongo/client"
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ pb "github.com/go-chassis/cari/discovery"
+ "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/mongo/options"
+)
+
+type DependencyRelation struct {
+ ctx context.Context
+ domainProject string
+ consumer *pb.MicroService
+ provider *pb.MicroService
+}
+
+type DependencyRelationFilterOpt struct {
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
[GitHub] [servicecomb-service-center] tianxiaoliang merged pull request #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
tianxiaoliang merged pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776
----------------------------------------------------------------
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] robotLJW commented on a change in pull request #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
robotLJW commented on a change in pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776#discussion_r537202925
##########
File path: datasource/mongo/ms.go
##########
@@ -1717,7 +1717,7 @@ func (ds *DataSource) UpdateInstanceStatus(ctx context.Context, request *pb.Upda
return resp, nil
}
- log.Infof("update instance[%s] status successfully", updateStatusFlag)
+ log.Info(fmt.Sprintf("update instance[%s] status successfully", updateStatusFlag))
Review comment:
这两个日志打印结果应该是一样的吧,话说为什么要还下面这种格式?能解释下吗,谢谢
##########
File path: datasource/mongo/dep.go
##########
@@ -20,11 +20,12 @@ package mongo
import (
"context"
+ "go.mongodb.org/mongo-driver/mongo/options"
+
Review comment:
包导入顺序,建议顺序:标准库、系统库、第三方库、本项目的库,不同分组之间使用空行分割开
##########
File path: datasource/mongo/dependency_query.go
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/apache/servicecomb-service-center/datasource/mongo/client"
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ pb "github.com/go-chassis/cari/discovery"
+ "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/mongo/options"
+)
+
+type DependencyRelation struct {
+ ctx context.Context
+ domainProject string
+ consumer *pb.MicroService
+ provider *pb.MicroService
+}
+
+type DependencyRelationFilterOpt struct {
Review comment:
个人建议啊,感觉这个能不能和上面的struct合并,初始化也是使用Functional Option
##########
File path: datasource/mongo/dependency_query.go
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/apache/servicecomb-service-center/datasource/mongo/client"
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ pb "github.com/go-chassis/cari/discovery"
+ "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/mongo/options"
+)
+
+type DependencyRelation struct {
+ ctx context.Context
+ domainProject string
+ consumer *pb.MicroService
+ provider *pb.MicroService
+}
+
+type DependencyRelationFilterOpt struct {
+ SameDomainProject bool
+ NonSelf bool
+}
+
+type DependencyRelationFilterOption func(opt DependencyRelationFilterOpt) DependencyRelationFilterOpt
+
+func NewConsumerDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, consumer, nil)
+}
+
Review comment:
个人愚见 func NewDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService,provider *pb.MicroService) *DependencyRelation 这个会不会更好
##########
File path: datasource/mongo/dependency_query.go
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/apache/servicecomb-service-center/datasource/mongo/client"
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ pb "github.com/go-chassis/cari/discovery"
+ "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/mongo/options"
+)
+
+type DependencyRelation struct {
+ ctx context.Context
+ domainProject string
+ consumer *pb.MicroService
+ provider *pb.MicroService
+}
+
+type DependencyRelationFilterOpt struct {
+ SameDomainProject bool
+ NonSelf bool
+}
+
+type DependencyRelationFilterOption func(opt DependencyRelationFilterOpt) DependencyRelationFilterOpt
+
+func NewConsumerDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, consumer, nil)
+}
+
+func NewProviderDependencyRelation(ctx context.Context, domainProject string, provider *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, nil, provider)
+}
+
+func NewDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService, provider *pb.MicroService) *DependencyRelation {
+ return &DependencyRelation{
Review comment:
如果把上面两个struct合并的话,NewDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService, provider *pb.MicroService,opts ...func(opt *DependencyRelationFilterOpt)) *DependencyRelation ,具体可以参考https://github.com/go-chassis/go-chassis/pull/925/files
----------------------------------------------------------------
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] robotLJW commented on a change in pull request #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
robotLJW commented on a change in pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776#discussion_r540777127
##########
File path: datasource/mongo/dependency_query.go
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/apache/servicecomb-service-center/datasource/mongo/client"
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ pb "github.com/go-chassis/cari/discovery"
+ "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/mongo/options"
+)
+
+type DependencyRelation struct {
+ ctx context.Context
+ domainProject string
+ consumer *pb.MicroService
+ provider *pb.MicroService
+}
+
+type DependencyRelationFilterOpt struct {
+ SameDomainProject bool
+ NonSelf bool
+}
+
+type DependencyRelationFilterOption func(opt DependencyRelationFilterOpt) DependencyRelationFilterOpt
+
+func NewConsumerDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, consumer, nil)
+}
+
+func NewProviderDependencyRelation(ctx context.Context, domainProject string, provider *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, nil, provider)
+}
+
+func NewDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService, provider *pb.MicroService) *DependencyRelation {
+ return &DependencyRelation{
+ ctx: ctx,
+ domainProject: domainProject,
+ consumer: consumer,
+ provider: provider,
+ }
+}
+
+func (dr *DependencyRelation) GetDependencyProviders(opts ...DependencyRelationFilterOption) ([]*pb.MicroService, error) {
+ keys, err := dr.getProviderKeys()
+ if err != nil {
+ return nil, err
+ }
+ services := make([]*pb.MicroService, 0, len(keys))
+ op := ToDependencyRelationFilterOpt(opts...)
+
+ for _, key := range keys {
+ if op.SameDomainProject && key.Tenant != dr.domainProject {
+ continue
+ }
+ providerIDs, err := dr.parseDependencyRule(key)
+
+ if err != nil {
+ return nil, err
+ }
+
+ if key.ServiceName == "*" {
+ services = services[:0]
+ }
+
+ for _, providerID := range providerIDs {
+ filter := GeneratorServiceFilter(dr.ctx, providerID)
+ provider, err := GetService(dr.ctx, filter)
+ if err != nil {
+ log.Warn(fmt.Sprintf("get provider[%s/%s/%s/%s] failed",
+ key.Environment, key.AppId, key.ServiceName, key.Version))
+ continue
+ }
+ if provider == nil {
+ log.Warn(fmt.Sprintf("provider[%s/%s/%s/%s] does not exist",
+ key.Environment, key.AppId, key.ServiceName, key.Version))
+ continue
+ }
+ if op.NonSelf && providerID == dr.consumer.ServiceId {
+ continue
+ }
+ services = append(services, provider.ServiceInfo)
+ }
+ if key.ServiceName == "*" {
+ break
+ }
+ }
+ return services, nil
+}
+
+func (dr *DependencyRelation) GetDependencyConsumers(opts ...DependencyRelationFilterOption) ([]*pb.MicroService, error) {
+ consumerDependAllList, err := dr.GetDependencyConsumersOfProvider()
+ if err != nil {
+ log.Error(fmt.Sprintf("get service[%s]'s consumers failed", dr.provider.ServiceId), err)
+ return nil, err
+ }
+ consumers := make([]*pb.MicroService, 0)
+ op := ToDependencyRelationFilterOpt(opts...)
+ for _, consumer := range consumerDependAllList {
+ if op.SameDomainProject && consumer.Tenant != dr.domainProject {
+ continue
+ }
+ service, err := dr.GetServiceByMicroServiceKey(consumer)
+ if err != nil {
+ return nil, err
+ }
+ if service == nil {
+ log.Warn(fmt.Sprintf("consumer[%s/%s/%s/%s] does not exist",
+ consumer.Environment, consumer.AppId, consumer.ServiceName, consumer.Version))
+ continue
+ }
+ if op.NonSelf && service.ServiceId == dr.provider.ServiceId {
+ continue
+ }
+ consumers = append(consumers, service)
+ }
+ return consumers, nil
+}
+
+func (dr *DependencyRelation) GetDependencyConsumersOfProvider() ([]*pb.MicroServiceKey, error) {
+ if dr.provider == nil {
+ return nil, ErrInvalidConsumer
+ }
+ consumerDependAllList, err := dr.getConsumerOfDependAllServices()
+ if err != nil {
+ log.Error(fmt.Sprintf("get consumers that depend on all services failed, %s", dr.provider.ServiceId), err)
+ return nil, err
+ }
+ return consumerDependAllList, nil
+}
+
+func (dr *DependencyRelation) GetServiceByMicroServiceKey(service *pb.MicroServiceKey) (*pb.MicroService, error) {
+ filter := MicroServiceKeyFilter(service)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionService, filter)
+ if err != nil {
+ return nil, err
+ }
+ for findRes.Next(dr.ctx) {
+ var service Service
+ err = findRes.Decode(&service)
+ if err != nil {
+ return nil, err
+ }
+ if service.ServiceInfo != nil {
+ return service.ServiceInfo, nil
+ }
+ }
+ return nil, nil
+}
+
+func (dr *DependencyRelation) getConsumerOfDependAllServices() ([]*pb.MicroServiceKey, error) {
+ providerService := pb.MicroServiceToKey(dr.domainProject, dr.provider)
+ providerService.ServiceName = "*"
+ filter := GenerateProviderDependencyRuleKey(dr.domainProject, dr.provider.ServiceId, providerService)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionDep, filter)
+ if err != nil {
+ return nil, err
+ }
+ var msKeys []*pb.MicroServiceKey
+ for findRes.Next(dr.ctx) {
+ var dep *Dependency
+ err = findRes.Decode(&dep)
+ if err != nil {
+ return nil, err
+ }
+ msKeys = append(msKeys, dep.DependencyInfo.Consumer)
+ }
+ return msKeys, nil
+}
+
+func (dr *DependencyRelation) getProviderKeys() ([]*pb.MicroServiceKey, error) {
+ if dr.consumer == nil {
+ return nil, ErrInvalidConsumer
+ }
+ consumerMicroServiceKey := pb.MicroServiceToKey(dr.domainProject, dr.consumer)
+ filter := GenerateConsumerDependencyRuleKey(dr.domainProject, dr.consumer.ServiceId, consumerMicroServiceKey)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionDep, filter)
+ if err != nil {
+ return nil, err
+ }
+ var dep *Dependency
+ for findRes.Next(dr.ctx) {
+ err = findRes.Decode(&dep)
+ if err != nil {
+ return nil, err
+ }
+ if dep != nil {
+ break
+ }
+ }
+ if dep != nil {
+ return dep.DependencyInfo.Providers, nil
+ }
+ return nil, nil
+}
+
+func (dr *DependencyRelation) parseDependencyRule(dependencyRule *pb.MicroServiceKey) (serviceIDs []string, err error) {
+ switch {
+ case dependencyRule.ServiceName == "*":
+ log.Info(fmt.Sprintf("service[%s/%s/%s/%s] rely all service",
+ dr.consumer.Environment, dr.consumer.AppId, dr.consumer.ServiceName, dr.consumer.Version))
+ filter := MicroServiceKeyFilter(dependencyRule)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionService, filter)
+ if err != nil {
+ return nil, err
+ }
+ for findRes.Next(dr.ctx) {
+ var service Service
+ err = findRes.Decode(&service)
+ if err != nil {
+ return nil, err
+ }
+ serviceIDs = append(serviceIDs, service.ServiceInfo.ServiceId)
+ }
+ default:
+ serviceIDs, _, err = FindServiceIds(dr.ctx, dependencyRule.Version, dependencyRule)
+ }
+ return
+}
+
+func (dr *DependencyRelation) GetDependencyConsumerIds() ([]string, error) {
+ consumerDependAllList, err := dr.GetDependencyConsumersOfProvider()
+ if err != nil {
+ return nil, err
+ }
+ consumerIDs := make([]string, 0, len(consumerDependAllList))
+ for _, consumer := range consumerDependAllList {
+ consumerID, err := GetServiceID(dr.ctx, consumer)
+ if err != nil {
+ log.Error(fmt.Sprintf("get consumer[%s/%s/%s/%s] failed",
+ consumer.Environment, consumer.AppId, consumer.ServiceName, consumer.Version), err)
+ return nil, err
+ }
+ if len(consumerID) == 0 {
+ log.Warn(fmt.Sprintf("get consumer[%s/%s/%s/%s] not exist",
+ consumer.Environment, consumer.AppId, consumer.ServiceName, consumer.Version))
+ continue
+ }
+ consumerIDs = append(consumerIDs, consumerID)
+ }
+ return consumerIDs, nil
+}
+
+func MicroServiceKeyFilter(key *pb.MicroServiceKey) bson.M {
+ tenant := strings.Split(key.Tenant, "/")
+ return bson.M{
+ ColumnDomain: tenant[0],
+ ColumnProject: tenant[1],
+ StringBuilder([]string{ColumnServiceInfo, ColumnEnv}): key.Environment,
+ StringBuilder([]string{ColumnServiceInfo, ColumnAppID}): key.AppId,
+ StringBuilder([]string{ColumnServiceInfo, ColumnAlias}): key.Alias,
+ StringBuilder([]string{ColumnServiceInfo, ColumnVersion}): key.Version}
+}
+
+func FindServiceIds(ctx context.Context, versionRule string, key *pb.MicroServiceKey) (serviceIDs []string, b bool, err error) {
+ match := ParseVersionRule(ctx, versionRule, key)
+ if match == nil {
+ tenant := strings.Split(key.Tenant, "/")
+ filter := bson.M{
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] robotLJW commented on a change in pull request #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
robotLJW commented on a change in pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776#discussion_r540776762
##########
File path: datasource/mongo/dependency_query.go
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/apache/servicecomb-service-center/datasource/mongo/client"
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ pb "github.com/go-chassis/cari/discovery"
+ "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/mongo/options"
+)
+
+type DependencyRelation struct {
+ ctx context.Context
+ domainProject string
+ consumer *pb.MicroService
+ provider *pb.MicroService
+}
+
+type DependencyRelationFilterOpt struct {
+ SameDomainProject bool
+ NonSelf bool
+}
+
+type DependencyRelationFilterOption func(opt DependencyRelationFilterOpt) DependencyRelationFilterOpt
+
+func NewConsumerDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, consumer, nil)
+}
+
+func NewProviderDependencyRelation(ctx context.Context, domainProject string, provider *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, nil, provider)
+}
+
+func NewDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService, provider *pb.MicroService) *DependencyRelation {
+ return &DependencyRelation{
+ ctx: ctx,
+ domainProject: domainProject,
+ consumer: consumer,
+ provider: provider,
+ }
+}
+
+func (dr *DependencyRelation) GetDependencyProviders(opts ...DependencyRelationFilterOption) ([]*pb.MicroService, error) {
+ keys, err := dr.getProviderKeys()
+ if err != nil {
+ return nil, err
+ }
+ services := make([]*pb.MicroService, 0, len(keys))
+ op := ToDependencyRelationFilterOpt(opts...)
+
+ for _, key := range keys {
+ if op.SameDomainProject && key.Tenant != dr.domainProject {
+ continue
+ }
+ providerIDs, err := dr.parseDependencyRule(key)
+
+ if err != nil {
+ return nil, err
+ }
+
+ if key.ServiceName == "*" {
+ services = services[:0]
+ }
+
+ for _, providerID := range providerIDs {
+ filter := GeneratorServiceFilter(dr.ctx, providerID)
+ provider, err := GetService(dr.ctx, filter)
+ if err != nil {
+ log.Warn(fmt.Sprintf("get provider[%s/%s/%s/%s] failed",
+ key.Environment, key.AppId, key.ServiceName, key.Version))
+ continue
+ }
+ if provider == nil {
+ log.Warn(fmt.Sprintf("provider[%s/%s/%s/%s] does not exist",
+ key.Environment, key.AppId, key.ServiceName, key.Version))
+ continue
+ }
+ if op.NonSelf && providerID == dr.consumer.ServiceId {
+ continue
+ }
+ services = append(services, provider.ServiceInfo)
+ }
+ if key.ServiceName == "*" {
+ break
+ }
+ }
+ return services, nil
+}
+
+func (dr *DependencyRelation) GetDependencyConsumers(opts ...DependencyRelationFilterOption) ([]*pb.MicroService, error) {
+ consumerDependAllList, err := dr.GetDependencyConsumersOfProvider()
+ if err != nil {
+ log.Error(fmt.Sprintf("get service[%s]'s consumers failed", dr.provider.ServiceId), err)
+ return nil, err
+ }
+ consumers := make([]*pb.MicroService, 0)
+ op := ToDependencyRelationFilterOpt(opts...)
+ for _, consumer := range consumerDependAllList {
+ if op.SameDomainProject && consumer.Tenant != dr.domainProject {
+ continue
+ }
+ service, err := dr.GetServiceByMicroServiceKey(consumer)
+ if err != nil {
+ return nil, err
+ }
+ if service == nil {
+ log.Warn(fmt.Sprintf("consumer[%s/%s/%s/%s] does not exist",
+ consumer.Environment, consumer.AppId, consumer.ServiceName, consumer.Version))
+ continue
+ }
+ if op.NonSelf && service.ServiceId == dr.provider.ServiceId {
+ continue
+ }
+ consumers = append(consumers, service)
+ }
+ return consumers, nil
+}
+
+func (dr *DependencyRelation) GetDependencyConsumersOfProvider() ([]*pb.MicroServiceKey, error) {
+ if dr.provider == nil {
+ return nil, ErrInvalidConsumer
+ }
+ consumerDependAllList, err := dr.getConsumerOfDependAllServices()
+ if err != nil {
+ log.Error(fmt.Sprintf("get consumers that depend on all services failed, %s", dr.provider.ServiceId), err)
+ return nil, err
+ }
+ return consumerDependAllList, nil
+}
+
+func (dr *DependencyRelation) GetServiceByMicroServiceKey(service *pb.MicroServiceKey) (*pb.MicroService, error) {
+ filter := MicroServiceKeyFilter(service)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionService, filter)
+ if err != nil {
+ return nil, err
+ }
+ for findRes.Next(dr.ctx) {
+ var service Service
+ err = findRes.Decode(&service)
+ if err != nil {
+ return nil, err
+ }
+ if service.ServiceInfo != nil {
+ return service.ServiceInfo, nil
+ }
+ }
+ return nil, nil
+}
+
+func (dr *DependencyRelation) getConsumerOfDependAllServices() ([]*pb.MicroServiceKey, error) {
+ providerService := pb.MicroServiceToKey(dr.domainProject, dr.provider)
+ providerService.ServiceName = "*"
+ filter := GenerateProviderDependencyRuleKey(dr.domainProject, dr.provider.ServiceId, providerService)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionDep, filter)
+ if err != nil {
+ return nil, err
+ }
+ var msKeys []*pb.MicroServiceKey
+ for findRes.Next(dr.ctx) {
+ var dep *Dependency
+ err = findRes.Decode(&dep)
+ if err != nil {
+ return nil, err
+ }
+ msKeys = append(msKeys, dep.DependencyInfo.Consumer)
+ }
+ return msKeys, nil
+}
+
+func (dr *DependencyRelation) getProviderKeys() ([]*pb.MicroServiceKey, error) {
+ if dr.consumer == nil {
+ return nil, ErrInvalidConsumer
+ }
+ consumerMicroServiceKey := pb.MicroServiceToKey(dr.domainProject, dr.consumer)
+ filter := GenerateConsumerDependencyRuleKey(dr.domainProject, dr.consumer.ServiceId, consumerMicroServiceKey)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionDep, filter)
+ if err != nil {
+ return nil, err
+ }
+ var dep *Dependency
+ for findRes.Next(dr.ctx) {
+ err = findRes.Decode(&dep)
+ if err != nil {
+ return nil, err
+ }
+ if dep != nil {
+ break
+ }
+ }
+ if dep != nil {
+ return dep.DependencyInfo.Providers, nil
+ }
+ return nil, nil
+}
+
+func (dr *DependencyRelation) parseDependencyRule(dependencyRule *pb.MicroServiceKey) (serviceIDs []string, err error) {
+ switch {
+ case dependencyRule.ServiceName == "*":
+ log.Info(fmt.Sprintf("service[%s/%s/%s/%s] rely all service",
+ dr.consumer.Environment, dr.consumer.AppId, dr.consumer.ServiceName, dr.consumer.Version))
+ filter := MicroServiceKeyFilter(dependencyRule)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionService, filter)
+ if err != nil {
+ return nil, err
+ }
+ for findRes.Next(dr.ctx) {
+ var service Service
+ err = findRes.Decode(&service)
+ if err != nil {
+ return nil, err
+ }
+ serviceIDs = append(serviceIDs, service.ServiceInfo.ServiceId)
+ }
+ default:
+ serviceIDs, _, err = FindServiceIds(dr.ctx, dependencyRule.Version, dependencyRule)
+ }
+ return
+}
+
+func (dr *DependencyRelation) GetDependencyConsumerIds() ([]string, error) {
+ consumerDependAllList, err := dr.GetDependencyConsumersOfProvider()
+ if err != nil {
+ return nil, err
+ }
+ consumerIDs := make([]string, 0, len(consumerDependAllList))
+ for _, consumer := range consumerDependAllList {
+ consumerID, err := GetServiceID(dr.ctx, consumer)
+ if err != nil {
+ log.Error(fmt.Sprintf("get consumer[%s/%s/%s/%s] failed",
+ consumer.Environment, consumer.AppId, consumer.ServiceName, consumer.Version), err)
+ return nil, err
+ }
+ if len(consumerID) == 0 {
+ log.Warn(fmt.Sprintf("get consumer[%s/%s/%s/%s] not exist",
+ consumer.Environment, consumer.AppId, consumer.ServiceName, consumer.Version))
+ continue
+ }
+ consumerIDs = append(consumerIDs, consumerID)
+ }
+ return consumerIDs, nil
+}
+
+func MicroServiceKeyFilter(key *pb.MicroServiceKey) bson.M {
+ tenant := strings.Split(key.Tenant, "/")
+ return bson.M{
Review comment:
tenant切分后判断越界问题,可以直接判断长度是不是为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] robotLJW commented on a change in pull request #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
robotLJW commented on a change in pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776#discussion_r540777340
##########
File path: datasource/mongo/dependency_query.go
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/apache/servicecomb-service-center/datasource/mongo/client"
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ pb "github.com/go-chassis/cari/discovery"
+ "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/mongo/options"
+)
+
+type DependencyRelation struct {
+ ctx context.Context
+ domainProject string
+ consumer *pb.MicroService
+ provider *pb.MicroService
+}
+
+type DependencyRelationFilterOpt struct {
+ SameDomainProject bool
+ NonSelf bool
+}
+
+type DependencyRelationFilterOption func(opt DependencyRelationFilterOpt) DependencyRelationFilterOpt
+
+func NewConsumerDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, consumer, nil)
+}
+
+func NewProviderDependencyRelation(ctx context.Context, domainProject string, provider *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, nil, provider)
+}
+
+func NewDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService, provider *pb.MicroService) *DependencyRelation {
+ return &DependencyRelation{
+ ctx: ctx,
+ domainProject: domainProject,
+ consumer: consumer,
+ provider: provider,
+ }
+}
+
+func (dr *DependencyRelation) GetDependencyProviders(opts ...DependencyRelationFilterOption) ([]*pb.MicroService, error) {
+ keys, err := dr.getProviderKeys()
+ if err != nil {
+ return nil, err
+ }
+ services := make([]*pb.MicroService, 0, len(keys))
+ op := ToDependencyRelationFilterOpt(opts...)
+
+ for _, key := range keys {
+ if op.SameDomainProject && key.Tenant != dr.domainProject {
+ continue
+ }
+ providerIDs, err := dr.parseDependencyRule(key)
+
+ if err != nil {
+ return nil, err
+ }
+
+ if key.ServiceName == "*" {
+ services = services[:0]
+ }
+
+ for _, providerID := range providerIDs {
+ filter := GeneratorServiceFilter(dr.ctx, providerID)
+ provider, err := GetService(dr.ctx, filter)
+ if err != nil {
+ log.Warn(fmt.Sprintf("get provider[%s/%s/%s/%s] failed",
+ key.Environment, key.AppId, key.ServiceName, key.Version))
+ continue
+ }
+ if provider == nil {
+ log.Warn(fmt.Sprintf("provider[%s/%s/%s/%s] does not exist",
+ key.Environment, key.AppId, key.ServiceName, key.Version))
+ continue
+ }
+ if op.NonSelf && providerID == dr.consumer.ServiceId {
+ continue
+ }
+ services = append(services, provider.ServiceInfo)
+ }
+ if key.ServiceName == "*" {
+ break
+ }
+ }
+ return services, nil
+}
+
+func (dr *DependencyRelation) GetDependencyConsumers(opts ...DependencyRelationFilterOption) ([]*pb.MicroService, error) {
+ consumerDependAllList, err := dr.GetDependencyConsumersOfProvider()
+ if err != nil {
+ log.Error(fmt.Sprintf("get service[%s]'s consumers failed", dr.provider.ServiceId), err)
+ return nil, err
+ }
+ consumers := make([]*pb.MicroService, 0)
+ op := ToDependencyRelationFilterOpt(opts...)
+ for _, consumer := range consumerDependAllList {
+ if op.SameDomainProject && consumer.Tenant != dr.domainProject {
+ continue
+ }
+ service, err := dr.GetServiceByMicroServiceKey(consumer)
+ if err != nil {
+ return nil, err
+ }
+ if service == nil {
+ log.Warn(fmt.Sprintf("consumer[%s/%s/%s/%s] does not exist",
+ consumer.Environment, consumer.AppId, consumer.ServiceName, consumer.Version))
+ continue
+ }
+ if op.NonSelf && service.ServiceId == dr.provider.ServiceId {
+ continue
+ }
+ consumers = append(consumers, service)
+ }
+ return consumers, nil
+}
+
+func (dr *DependencyRelation) GetDependencyConsumersOfProvider() ([]*pb.MicroServiceKey, error) {
+ if dr.provider == nil {
+ return nil, ErrInvalidConsumer
+ }
+ consumerDependAllList, err := dr.getConsumerOfDependAllServices()
+ if err != nil {
+ log.Error(fmt.Sprintf("get consumers that depend on all services failed, %s", dr.provider.ServiceId), err)
+ return nil, err
+ }
+ return consumerDependAllList, nil
+}
+
+func (dr *DependencyRelation) GetServiceByMicroServiceKey(service *pb.MicroServiceKey) (*pb.MicroService, error) {
+ filter := MicroServiceKeyFilter(service)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionService, filter)
+ if err != nil {
+ return nil, err
+ }
+ for findRes.Next(dr.ctx) {
+ var service Service
+ err = findRes.Decode(&service)
+ if err != nil {
+ return nil, err
+ }
+ if service.ServiceInfo != nil {
+ return service.ServiceInfo, nil
+ }
+ }
+ return nil, nil
+}
+
+func (dr *DependencyRelation) getConsumerOfDependAllServices() ([]*pb.MicroServiceKey, error) {
+ providerService := pb.MicroServiceToKey(dr.domainProject, dr.provider)
+ providerService.ServiceName = "*"
+ filter := GenerateProviderDependencyRuleKey(dr.domainProject, dr.provider.ServiceId, providerService)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionDep, filter)
+ if err != nil {
+ return nil, err
+ }
+ var msKeys []*pb.MicroServiceKey
+ for findRes.Next(dr.ctx) {
+ var dep *Dependency
+ err = findRes.Decode(&dep)
+ if err != nil {
+ return nil, err
+ }
+ msKeys = append(msKeys, dep.DependencyInfo.Consumer)
+ }
+ return msKeys, nil
+}
+
+func (dr *DependencyRelation) getProviderKeys() ([]*pb.MicroServiceKey, error) {
+ if dr.consumer == nil {
+ return nil, ErrInvalidConsumer
+ }
+ consumerMicroServiceKey := pb.MicroServiceToKey(dr.domainProject, dr.consumer)
+ filter := GenerateConsumerDependencyRuleKey(dr.domainProject, dr.consumer.ServiceId, consumerMicroServiceKey)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionDep, filter)
+ if err != nil {
+ return nil, err
+ }
+ var dep *Dependency
+ for findRes.Next(dr.ctx) {
+ err = findRes.Decode(&dep)
+ if err != nil {
+ return nil, err
+ }
+ if dep != nil {
+ break
+ }
+ }
+ if dep != nil {
+ return dep.DependencyInfo.Providers, nil
+ }
+ return nil, nil
+}
+
+func (dr *DependencyRelation) parseDependencyRule(dependencyRule *pb.MicroServiceKey) (serviceIDs []string, err error) {
+ switch {
+ case dependencyRule.ServiceName == "*":
+ log.Info(fmt.Sprintf("service[%s/%s/%s/%s] rely all service",
+ dr.consumer.Environment, dr.consumer.AppId, dr.consumer.ServiceName, dr.consumer.Version))
+ filter := MicroServiceKeyFilter(dependencyRule)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionService, filter)
+ if err != nil {
+ return nil, err
+ }
+ for findRes.Next(dr.ctx) {
+ var service Service
+ err = findRes.Decode(&service)
+ if err != nil {
+ return nil, err
+ }
+ serviceIDs = append(serviceIDs, service.ServiceInfo.ServiceId)
+ }
+ default:
+ serviceIDs, _, err = FindServiceIds(dr.ctx, dependencyRule.Version, dependencyRule)
+ }
+ return
+}
+
+func (dr *DependencyRelation) GetDependencyConsumerIds() ([]string, error) {
+ consumerDependAllList, err := dr.GetDependencyConsumersOfProvider()
+ if err != nil {
+ return nil, err
+ }
+ consumerIDs := make([]string, 0, len(consumerDependAllList))
+ for _, consumer := range consumerDependAllList {
+ consumerID, err := GetServiceID(dr.ctx, consumer)
+ if err != nil {
+ log.Error(fmt.Sprintf("get consumer[%s/%s/%s/%s] failed",
+ consumer.Environment, consumer.AppId, consumer.ServiceName, consumer.Version), err)
+ return nil, err
+ }
+ if len(consumerID) == 0 {
+ log.Warn(fmt.Sprintf("get consumer[%s/%s/%s/%s] not exist",
+ consumer.Environment, consumer.AppId, consumer.ServiceName, consumer.Version))
+ continue
+ }
+ consumerIDs = append(consumerIDs, consumerID)
+ }
+ return consumerIDs, nil
+}
+
+func MicroServiceKeyFilter(key *pb.MicroServiceKey) bson.M {
+ tenant := strings.Split(key.Tenant, "/")
+ return bson.M{
+ ColumnDomain: tenant[0],
+ ColumnProject: tenant[1],
+ StringBuilder([]string{ColumnServiceInfo, ColumnEnv}): key.Environment,
+ StringBuilder([]string{ColumnServiceInfo, ColumnAppID}): key.AppId,
+ StringBuilder([]string{ColumnServiceInfo, ColumnAlias}): key.Alias,
+ StringBuilder([]string{ColumnServiceInfo, ColumnVersion}): key.Version}
+}
+
+func FindServiceIds(ctx context.Context, versionRule string, key *pb.MicroServiceKey) (serviceIDs []string, b bool, err error) {
+ match := ParseVersionRule(ctx, versionRule, key)
+ if match == nil {
+ tenant := strings.Split(key.Tenant, "/")
+ filter := bson.M{
+ ColumnDomain: tenant[0],
+ ColumnProject: tenant[1],
+ StringBuilder([]string{ColumnServiceInfo, ColumnEnv}): key.Environment,
+ StringBuilder([]string{ColumnServiceInfo, ColumnVersion}): versionRule,
+ StringBuilder([]string{ColumnServiceInfo, ColumnAppID}): key.AppId,
+ StringBuilder([]string{ColumnServiceInfo, ColumnServiceName}): key.ServiceName,
+ StringBuilder([]string{ColumnServiceInfo, ColumnAlias}): key.Alias}
+ findRes, err := client.GetMongoClient().Find(ctx, CollectionService, filter)
+ if err != nil {
+ return nil, false, nil
+ }
+ for findRes.Next(ctx) {
+ var service Service
+ err = findRes.Decode(&service)
+ if err != nil {
+ return nil, false, err
+ }
+ serviceIDs = append(serviceIDs, service.ServiceInfo.ServiceId)
+ }
+ return serviceIDs, false, nil
+ }
+ return match, false, nil
+}
+
+func ParseVersionRule(ctx context.Context, versionRule string, key *pb.MicroServiceKey) []string {
+ tenant := strings.Split(key.Tenant, "/")
+ if len(versionRule) == 0 {
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 #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
little-cui commented on a change in pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776#discussion_r540213977
##########
File path: datasource/mongo/ms.go
##########
@@ -1543,9 +1543,11 @@ func (ds *DataSource) GetInstances(ctx context.Context, request *pb.GetInstances
}, 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)
+ findFlag := func() string {
+ return 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)
+ }
Review comment:
改成func的原因是成功的时候不需要打印吗?性能优化?
----------------------------------------------------------------
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] robotLJW commented on a change in pull request #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
robotLJW commented on a change in pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776#discussion_r540776152
##########
File path: datasource/mongo/dependency_query.go
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/apache/servicecomb-service-center/datasource/mongo/client"
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ pb "github.com/go-chassis/cari/discovery"
+ "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/mongo/options"
+)
+
+type DependencyRelation struct {
+ ctx context.Context
+ domainProject string
+ consumer *pb.MicroService
+ provider *pb.MicroService
+}
+
+type DependencyRelationFilterOpt struct {
+ SameDomainProject bool
+ NonSelf bool
+}
+
+type DependencyRelationFilterOption func(opt DependencyRelationFilterOpt) DependencyRelationFilterOpt
+
+func NewConsumerDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, consumer, nil)
+}
+
+func NewProviderDependencyRelation(ctx context.Context, domainProject string, provider *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, nil, provider)
+}
+
+func NewDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService, provider *pb.MicroService) *DependencyRelation {
+ return &DependencyRelation{
+ ctx: ctx,
+ domainProject: domainProject,
+ consumer: consumer,
+ provider: provider,
+ }
+}
+
+func (dr *DependencyRelation) GetDependencyProviders(opts ...DependencyRelationFilterOption) ([]*pb.MicroService, error) {
+ keys, err := dr.getProviderKeys()
+ if err != nil {
+ return nil, err
+ }
+ services := make([]*pb.MicroService, 0, len(keys))
+ op := ToDependencyRelationFilterOpt(opts...)
+
+ for _, key := range keys {
+ if op.SameDomainProject && key.Tenant != dr.domainProject {
+ continue
+ }
+ providerIDs, err := dr.parseDependencyRule(key)
+
+ if err != nil {
+ return nil, err
+ }
+
+ if key.ServiceName == "*" {
+ services = services[:0]
+ }
+
+ for _, providerID := range providerIDs {
+ filter := GeneratorServiceFilter(dr.ctx, providerID)
+ provider, err := GetService(dr.ctx, filter)
+ if err != nil {
+ log.Warn(fmt.Sprintf("get provider[%s/%s/%s/%s] failed",
+ key.Environment, key.AppId, key.ServiceName, key.Version))
+ continue
+ }
+ if provider == nil {
+ log.Warn(fmt.Sprintf("provider[%s/%s/%s/%s] does not exist",
+ key.Environment, key.AppId, key.ServiceName, key.Version))
+ continue
+ }
+ if op.NonSelf && providerID == dr.consumer.ServiceId {
+ continue
+ }
+ services = append(services, provider.ServiceInfo)
+ }
+ if key.ServiceName == "*" {
+ break
+ }
+ }
+ return services, nil
+}
+
+func (dr *DependencyRelation) GetDependencyConsumers(opts ...DependencyRelationFilterOption) ([]*pb.MicroService, error) {
+ consumerDependAllList, err := dr.GetDependencyConsumersOfProvider()
+ if err != nil {
+ log.Error(fmt.Sprintf("get service[%s]'s consumers failed", dr.provider.ServiceId), err)
+ return nil, err
+ }
+ consumers := make([]*pb.MicroService, 0)
+ op := ToDependencyRelationFilterOpt(opts...)
+ for _, consumer := range consumerDependAllList {
+ if op.SameDomainProject && consumer.Tenant != dr.domainProject {
+ continue
+ }
+ service, err := dr.GetServiceByMicroServiceKey(consumer)
+ if err != nil {
+ return nil, err
+ }
+ if service == nil {
+ log.Warn(fmt.Sprintf("consumer[%s/%s/%s/%s] does not exist",
+ consumer.Environment, consumer.AppId, consumer.ServiceName, consumer.Version))
+ continue
+ }
+ if op.NonSelf && service.ServiceId == dr.provider.ServiceId {
+ continue
+ }
+ consumers = append(consumers, service)
+ }
+ return consumers, nil
+}
+
+func (dr *DependencyRelation) GetDependencyConsumersOfProvider() ([]*pb.MicroServiceKey, error) {
+ if dr.provider == nil {
+ return nil, ErrInvalidConsumer
+ }
+ consumerDependAllList, err := dr.getConsumerOfDependAllServices()
+ if err != nil {
+ log.Error(fmt.Sprintf("get consumers that depend on all services failed, %s", dr.provider.ServiceId), err)
+ return nil, err
+ }
+ return consumerDependAllList, nil
+}
+
+func (dr *DependencyRelation) GetServiceByMicroServiceKey(service *pb.MicroServiceKey) (*pb.MicroService, error) {
+ filter := MicroServiceKeyFilter(service)
+ findRes, err := client.GetMongoClient().Find(dr.ctx, CollectionService, filter)
+ if err != nil {
+ return nil, err
+ }
+ for findRes.Next(dr.ctx) {
Review comment:
findRes需不需要判断nil
----------------------------------------------------------------
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 #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
xzccfzy commented on a change in pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776#discussion_r540624855
##########
File path: datasource/mongo/ms.go
##########
@@ -1543,9 +1543,11 @@ func (ds *DataSource) GetInstances(ctx context.Context, request *pb.GetInstances
}, 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)
+ findFlag := func() string {
+ return 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)
+ }
Review comment:
改成func是因为在使用curl调测时会因为请求参数不全导致panic,看了etcd那边是采用的func形式。
----------------------------------------------------------------
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 #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
xzccfzy commented on a change in pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776#discussion_r540624321
##########
File path: datasource/mongo/dependency_query.go
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/apache/servicecomb-service-center/datasource/mongo/client"
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ pb "github.com/go-chassis/cari/discovery"
+ "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/mongo/options"
+)
+
+type DependencyRelation struct {
+ ctx context.Context
+ domainProject string
+ consumer *pb.MicroService
+ provider *pb.MicroService
+}
+
+type DependencyRelationFilterOpt struct {
+ SameDomainProject bool
+ NonSelf bool
+}
+
+type DependencyRelationFilterOption func(opt DependencyRelationFilterOpt) DependencyRelationFilterOpt
+
+func NewConsumerDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, consumer, nil)
+}
+
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
[GitHub] [servicecomb-service-center] tianxiaoliang commented on pull request #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776#issuecomment-743058396
冲突
----------------------------------------------------------------
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] robotLJW commented on a change in pull request #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
robotLJW commented on a change in pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776#discussion_r537202925
##########
File path: datasource/mongo/ms.go
##########
@@ -1717,7 +1717,7 @@ func (ds *DataSource) UpdateInstanceStatus(ctx context.Context, request *pb.Upda
return resp, nil
}
- log.Infof("update instance[%s] status successfully", updateStatusFlag)
+ log.Info(fmt.Sprintf("update instance[%s] status successfully", updateStatusFlag))
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 #776: [SCB-2094] add dep UT, implement dep query interface
Posted by GitBox <gi...@apache.org>.
xzccfzy commented on a change in pull request #776:
URL: https://github.com/apache/servicecomb-service-center/pull/776#discussion_r540623464
##########
File path: datasource/mongo/dependency_query.go
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/apache/servicecomb-service-center/datasource/mongo/client"
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ pb "github.com/go-chassis/cari/discovery"
+ "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/mongo/options"
+)
+
+type DependencyRelation struct {
+ ctx context.Context
+ domainProject string
+ consumer *pb.MicroService
+ provider *pb.MicroService
+}
+
+type DependencyRelationFilterOpt struct {
+ SameDomainProject bool
+ NonSelf bool
+}
+
+type DependencyRelationFilterOption func(opt DependencyRelationFilterOpt) DependencyRelationFilterOpt
+
+func NewConsumerDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, consumer, nil)
+}
+
+func NewProviderDependencyRelation(ctx context.Context, domainProject string, provider *pb.MicroService) *DependencyRelation {
+ return NewDependencyRelation(ctx, domainProject, nil, provider)
+}
+
+func NewDependencyRelation(ctx context.Context, domainProject string, consumer *pb.MicroService, provider *pb.MicroService) *DependencyRelation {
+ return &DependencyRelation{
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