You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by as...@apache.org on 2019/06/27 11:53:39 UTC

[servicecomb-kie] 28/29: fix bug: if find by labels, will lose key value record

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

asifdxtreme pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-kie.git

commit bf3c4d6121be294afd97b706b356481e6ea5c1a8
Author: tian <xi...@gmail.com>
AuthorDate: Thu Jun 27 15:54:06 2019 +0800

    fix bug: if find by labels, will lose key value record
---
 go.mod                              |  2 +-
 server/config/config.go             |  4 ++--
 server/dao/kie_api.go               | 19 +++++++++++++++++--
 server/resource/v1/common.go        |  3 +++
 server/resource/v1/common_test.go   | 16 ++++++++++++++++
 server/resource/v1/kv_resource.go   | 14 +++++++-------
 server/resource/v1/v1_suite_test.go | 17 +++++++++++++++++
 7 files changed, 63 insertions(+), 12 deletions(-)

diff --git a/go.mod b/go.mod
index 6646204..2540c72 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module github.com/apache/servicecomb-kie
 require (
 	github.com/emicklei/go-restful v2.8.0+incompatible
 	github.com/go-chassis/foundation v0.0.0-20190516083152-b8b2476b6db7
-	github.com/go-chassis/go-archaius v0.16.0
+	github.com/go-chassis/go-archaius v0.18.0
 	github.com/go-chassis/go-chassis v1.4.3
 	github.com/go-chassis/paas-lager v1.0.2-0.20190328010332-cf506050ddb2
 	github.com/go-mesh/openlogging v1.0.1-0.20181205082104-3d418c478b2d
diff --git a/server/config/config.go b/server/config/config.go
index 76571de..36b66c2 100644
--- a/server/config/config.go
+++ b/server/config/config.go
@@ -19,7 +19,7 @@ package config
 
 import (
 	"github.com/go-chassis/go-archaius"
-	"github.com/go-chassis/go-archaius/sources/file-source"
+	"github.com/go-chassis/go-archaius/sources/utils"
 	"gopkg.in/yaml.v2"
 	"path/filepath"
 )
@@ -28,7 +28,7 @@ var configurations *Config
 
 //Init initiate config files
 func Init(file string) error {
-	if err := archaius.AddFile(file, archaius.WithFileHandler(filesource.UseFileNameAsKeyContentAsValue)); err != nil {
+	if err := archaius.AddFile(file, archaius.WithFileHandler(utils.UseFileNameAsKeyContentAsValue)); err != nil {
 		return err
 	}
 	_, filename := filepath.Split(file)
diff --git a/server/dao/kie_api.go b/server/dao/kie_api.go
index 0e0020d..5e641aa 100644
--- a/server/dao/kie_api.go
+++ b/server/dao/kie_api.go
@@ -58,6 +58,11 @@ func (s *MongodbService) CreateOrUpdate(ctx context.Context, domain string, kv *
 	if domain == "" {
 		return nil, ErrMissingDomain
 	}
+	if len(kv.Labels) == 0 {
+		kv.Labels = map[string]string{
+			"default": "default",
+		}
+	}
 	ctx, _ = context.WithTimeout(ctx, DefaultTimeout)
 	//check labels exits or not
 	labelID, err := s.LabelsExist(ctx, domain, kv.Labels)
@@ -66,6 +71,10 @@ func (s *MongodbService) CreateOrUpdate(ctx context.Context, domain string, kv *
 		if err == ErrLabelNotExists {
 			l, err = s.createLabel(ctx, domain, kv.Labels)
 			if err != nil {
+				openlogging.Error("create label failed", openlogging.WithTags(openlogging.Tags{
+					"k":      kv.Key,
+					"domain": kv.Domain,
+				}))
 				return nil, err
 			}
 			labelID = l.ID
@@ -96,6 +105,7 @@ func (s *MongodbService) CreateOrUpdate(ctx context.Context, domain string, kv *
 		return nil, err
 	}
 	kv.Revision = revision
+	kv.Domain = ""
 	return kv, nil
 
 }
@@ -208,7 +218,7 @@ func (s *MongodbService) FindKV(ctx context.Context, domain string, options ...F
 	defer cur.Close(ctx)
 
 	kvResp := make([]*model.KVResponse, 0)
-	if opts.Depth == 0 {
+	if opts.Depth == 0 && opts.Key != "" {
 		openlogging.Debug("find one key", openlogging.WithTags(
 			map[string]interface{}{
 				"key":    opts.Key,
@@ -218,6 +228,11 @@ func (s *MongodbService) FindKV(ctx context.Context, domain string, options ...F
 		))
 		return cursorToOneKV(ctx, cur, opts.Labels)
 	}
+	openlogging.Debug("find more", openlogging.WithTags(openlogging.Tags{
+		"depth":  opts.Depth,
+		"k":      opts.Key,
+		"labels": opts.Labels,
+	}))
 	for cur.Next(ctx) {
 		curKV := &model.KVDoc{}
 
@@ -231,7 +246,7 @@ func (s *MongodbService) FindKV(ctx context.Context, domain string, options ...F
 			openlogging.Debug("so deep, skip this key")
 			continue
 		}
-		openlogging.Info(fmt.Sprintf("%v", curKV))
+		openlogging.Debug(fmt.Sprintf("%v", curKV))
 		var groupExist bool
 		var labelGroup *model.KVResponse
 		for _, labelGroup = range kvResp {
diff --git a/server/resource/v1/common.go b/server/resource/v1/common.go
index dcf9901..9b01260 100644
--- a/server/resource/v1/common.go
+++ b/server/resource/v1/common.go
@@ -77,6 +77,9 @@ func ReadLabelCombinations(req *goRestful.Request) ([]map[string]string, error)
 		}
 		labelCombinations = append(labelCombinations, labels)
 	}
+	if len(labelCombinations) == 0 {
+		return []map[string]string{{"default": "default"}}, nil
+	}
 	return labelCombinations, nil
 }
 
diff --git a/server/resource/v1/common_test.go b/server/resource/v1/common_test.go
index d662799..792383c 100644
--- a/server/resource/v1/common_test.go
+++ b/server/resource/v1/common_test.go
@@ -44,5 +44,21 @@ var _ = Describe("Common", func() {
 			})
 
 		})
+		Context("find default", func() {
+			r, err := http.NewRequest("GET",
+				"/kv",
+				nil)
+			It("should not return err ", func() {
+				Expect(err).Should(BeNil())
+			})
+			c, err := ReadLabelCombinations(restful.NewRequest(r))
+			It("should not return err ", func() {
+				Expect(err).Should(BeNil())
+			})
+			It("should has 1 combinations", func() {
+				Expect(len(c)).Should(Equal(1))
+			})
+
+		})
 	})
 })
diff --git a/server/resource/v1/kv_resource.go b/server/resource/v1/kv_resource.go
index c072dc6..832dae7 100644
--- a/server/resource/v1/kv_resource.go
+++ b/server/resource/v1/kv_resource.go
@@ -99,11 +99,11 @@ func (r *KVResource) GetByKey(context *restful.Context) {
 		return
 	}
 	kvs, err := s.FindKV(context.Ctx, domain.(string), dao.WithKey(key), dao.WithLabels(labels), dao.WithDepth(d))
-	if err == dao.ErrKeyNotExists {
-		WriteErrResponse(context, http.StatusNotFound, err.Error())
-		return
-	}
 	if err != nil {
+		if err == dao.ErrKeyNotExists {
+			WriteErrResponse(context, http.StatusNotFound, err.Error())
+			return
+		}
 		WriteErrResponse(context, http.StatusInternalServerError, err.Error())
 		return
 	}
@@ -146,7 +146,7 @@ func (r *KVResource) SearchByLabels(context *restful.Context) {
 
 	}
 	if len(kvs) == 0 {
-		WriteErrResponse(context, http.StatusNotFound, err.Error())
+		WriteErrResponse(context, http.StatusNotFound, "no kv found")
 		return
 	}
 
@@ -214,7 +214,7 @@ func (r *KVResource) URLPatterns() []restful.Route {
 			ResourceFuncName: "GetByKey",
 			FuncDesc:         "get key values by key and labels",
 			Parameters: []*restful.Parameters{
-				DocPathKey, DocHeaderMath, DocHeaderDepth,
+				DocPathKey, DocHeaderDepth,
 			},
 			Returns: []*restful.Returns{
 				{
@@ -232,7 +232,7 @@ func (r *KVResource) URLPatterns() []restful.Route {
 			ResourceFuncName: "SearchByLabels",
 			FuncDesc:         "search key values by labels combination",
 			Parameters: []*restful.Parameters{
-				DocHeaderMath, DocQueryCombination,
+				DocQueryCombination,
 			},
 			Returns: []*restful.Returns{
 				{
diff --git a/server/resource/v1/v1_suite_test.go b/server/resource/v1/v1_suite_test.go
index 23b7482..99a2884 100644
--- a/server/resource/v1/v1_suite_test.go
+++ b/server/resource/v1/v1_suite_test.go
@@ -1,3 +1,20 @@
+/*
+ * 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 v1_test
 
 import (