You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2021/04/12 15:23:45 UTC

[servicecomb-service-center] branch test updated (39422e9 -> fce839c)

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

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


 discard 39422e9  SCB-2176 Fix: failed to connect mongo
     new fce839c  SCB-2176 Fix: failed to connect mongo

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (39422e9)
            \
             N -- N -- N   refs/heads/test (fce839c)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

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


Summary of changes:
 datasource/mongo/sd/listwatch_inner.go | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

[servicecomb-service-center] 01/01: SCB-2176 Fix: failed to connect mongo

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

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

commit fce839cc44c3a6a28da921d2087fecd6f0285145
Author: little-cui <su...@qq.com>
AuthorDate: Mon Apr 12 19:27:16 2021 +0800

    SCB-2176 Fix: failed to connect mongo
---
 datasource/mongo/client/errortypes.go  |  6 ++++--
 datasource/mongo/mongo.go              | 11 +++++------
 datasource/mongo/sd/listwatch_inner.go | 27 ++++++++++++++++++++-------
 3 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/datasource/mongo/client/errortypes.go b/datasource/mongo/client/errortypes.go
index 759315f..64dd599 100644
--- a/datasource/mongo/client/errortypes.go
+++ b/datasource/mongo/client/errortypes.go
@@ -20,8 +20,10 @@ import (
 )
 
 const (
-	DuplicateKey      = 11000
-	CollectionsExists = 48
+	DuplicateKey = 11000
+
+	MsgDBExists  = "already exists"
+	MsgDuplicate = "duplicate key error collection"
 )
 
 func IsDuplicateKey(err error) bool {
diff --git a/datasource/mongo/mongo.go b/datasource/mongo/mongo.go
index 8addf8f..1c34550 100644
--- a/datasource/mongo/mongo.go
+++ b/datasource/mongo/mongo.go
@@ -21,6 +21,7 @@ import (
 	"context"
 	"fmt"
 	"github.com/apache/servicecomb-service-center/pkg/util"
+	"strings"
 
 	"github.com/go-chassis/go-chassis/v2/storage"
 	"go.mongodb.org/mongo-driver/mongo"
@@ -210,9 +211,8 @@ func EnsureDep() {
 
 func wrapCreateCollectionError(err error) {
 	if err != nil {
-		// commandError can be returned by any operation
-		cmdErr, ok := err.(mongo.CommandError)
-		if ok && cmdErr.Code == client.CollectionsExists {
+		if strings.Contains(err.Error(), client.MsgDBExists) {
+			log.Info(fmt.Sprintf("failed to create collection, %s", err))
 			return
 		}
 		log.Fatal(fmt.Sprintf("failed to create collection with validation, err type: %s", util.Reflect(err).FullName), err)
@@ -221,9 +221,8 @@ func wrapCreateCollectionError(err error) {
 
 func wrapCreateIndexesError(err error) {
 	if err != nil {
-		// commandError can be returned by any operation
-		cmdErr, ok := err.(mongo.CommandError)
-		if ok && cmdErr.Code == client.DuplicateKey {
+		if strings.Contains(err.Error(), client.MsgDuplicate) {
+			log.Info(fmt.Sprintf("failed to create indexes, %s", err))
 			return
 		}
 		log.Fatal(fmt.Sprintf("failed to create indexes, err type: %s", util.Reflect(err).FullName), err)
diff --git a/datasource/mongo/sd/listwatch_inner.go b/datasource/mongo/sd/listwatch_inner.go
index da673c4..0656680 100644
--- a/datasource/mongo/sd/listwatch_inner.go
+++ b/datasource/mongo/sd/listwatch_inner.go
@@ -53,6 +53,9 @@ func (lw *mongoListWatch) List(op sdcommon.ListWatchConfig) (*sdcommon.ListWatch
 
 	for resp.Next(context.Background()) {
 		info := lw.doParseDocumentToResource(resp.Current)
+		if len(info.Key) == 0 {
+			continue
+		}
 		lwRsp.Resources = append(lwRsp.Resources, &info)
 	}
 
@@ -95,17 +98,23 @@ func (lw *mongoListWatch) DoWatch(ctx context.Context, f func(*sdcommon.ListWatc
 		resource := lw.doParseWatchRspToResource(wRsp)
 
 		lwRsp := &sdcommon.ListWatchResp{}
-		lwRsp.Resources = append(lwRsp.Resources, &resource)
 		switch wRsp.OperationType {
 		case insertOp:
+			if len(resource.Key) == 0 {
+				continue
+			}
 			lwRsp.Action = sdcommon.ActionCreate
 		case updateOp:
+			if len(resource.Key) == 0 {
+				continue
+			}
 			lwRsp.Action = sdcommon.ActionUpdate
 		case deleteOp:
 			lwRsp.Action = sdcommon.ActionDelete
 		default:
 			log.Warn(fmt.Sprintf("unrecognized action:%s", lwRsp.Action))
 		}
+		lwRsp.Resources = append(lwRsp.Resources, &resource)
 
 		f(lwRsp)
 	}
@@ -132,9 +141,11 @@ func (lw *mongoListWatch) doParseDocumentToResource(fullDocument bson.Raw) (reso
 			log.Error("error to parse bson raw to documentInfo", err)
 			return
 		}
-		resource.Key = instance.Instance.InstanceId
-		resource.Value = instance
-		resource.Index = instance.Instance.ServiceId
+		if instance.Instance != nil {
+			resource.Key = instance.Instance.InstanceId
+			resource.Value = instance
+			resource.Index = instance.Instance.ServiceId
+		}
 	case service:
 		service := model.Service{}
 		err := bson.Unmarshal(fullDocument, &service)
@@ -142,9 +153,11 @@ func (lw *mongoListWatch) doParseDocumentToResource(fullDocument bson.Raw) (reso
 			log.Error("error to parse bson raw to documentInfo", err)
 			return
 		}
-		resource.Key = service.Service.ServiceId
-		resource.Value = service
-		resource.Index = util.StringJoin([]string{service.Domain, service.Project, service.Service.ServiceName, service.Service.Version, service.Service.AppId, service.Service.Environment}, "/")
+		if service.Service != nil {
+			resource.Key = service.Service.ServiceId
+			resource.Value = service
+			resource.Index = util.StringJoin([]string{service.Domain, service.Project, service.Service.ServiceName, service.Service.Version, service.Service.AppId, service.Service.Environment}, "/")
+		}
 	default:
 		return
 	}