You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ti...@apache.org on 2020/01/13 08:48:14 UTC

[servicecomb-kie] branch master updated: SCB-1718 use global revision, distinguish create and update revision (#66)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 4ae2770  SCB-1718 use global revision, distinguish create and update revision (#66)
4ae2770 is described below

commit 4ae277002b265b56bafa4512dd932dd3cf0855bf
Author: Shawn <xi...@gmail.com>
AuthorDate: Mon Jan 13 16:48:05 2020 +0800

    SCB-1718 use global revision, distinguish create and update revision (#66)
---
 build/docker/server/Dockerfile                     |  1 -
 client/client_test.go                              |  2 +-
 deployments/docker/db.js                           | 24 ++++++-
 deployments/docker/docker-compose.yaml             |  2 +-
 examples/dev/clean.sh                              |  2 +
 examples/dev/conf/chassis.yaml                     |  2 -
 {deployments/docker => examples/dev}/db.js         | 23 ++++++-
 examples/dev/docker-compose.yaml                   |  4 +-
 go.mod                                             |  3 +-
 go.sum                                             |  7 ++
 pkg/common/common.go                               |  4 +-
 pkg/model/{mongodb_doc.go => db_schema.go}         | 22 ++++---
 server/id/id.go                                    | 50 --------------
 server/resource/v1/common.go                       | 12 ++--
 server/resource/v1/doc_struct.go                   |  6 ++
 server/resource/v1/kv_resource.go                  |  3 +-
 server/service/mongo/counter/revision.go           | 77 ++++++++++++++++++++++
 .../mongo/counter/revision_test.go}                | 32 ++++-----
 .../service/mongo/counter/struct.go                | 19 ++----
 server/service/mongo/init.go                       |  2 +
 server/service/mongo/kv/kv_dao.go                  | 30 +++++++--
 server/service/mongo/kv/kv_service.go              | 24 ++++---
 server/service/mongo/session/session.go            |  5 +-
 server/service/service.go                          | 10 ++-
 24 files changed, 239 insertions(+), 127 deletions(-)

diff --git a/build/docker/server/Dockerfile b/build/docker/server/Dockerfile
index 6f1bc7b..03e1d98 100644
--- a/build/docker/server/Dockerfile
+++ b/build/docker/server/Dockerfile
@@ -18,7 +18,6 @@ FROM ubuntu:19.04
 RUN apt-get update && apt-get install -y net-tools
 RUN mkdir -p /etc/servicecomb-kie/
 RUN mkdir -p /opt/servicecomb-kie/conf
-RUN export GO_CHASSIS=/opt/servicecomb-kie
 
 ADD ./kie /opt/servicecomb-kie/
 ADD ./start.sh /opt/servicecomb-kie/
diff --git a/client/client_test.go b/client/client_test.go
index bdf5e22..e5ba13e 100644
--- a/client/client_test.go
+++ b/client/client_test.go
@@ -110,7 +110,7 @@ var _ = Describe("Client", func() {
 			kvBody := model.KVRequest{}
 			kvBody.Key = "time"
 			kvBody.Value = "100s"
-			kvBody.ValueType = "string"
+			kvBody.ValueType = "text"
 			kvBody.Labels = make(map[string]string)
 			kvBody.Labels["env"] = "test"
 			kv, err := client2.Put(context.TODO(), kvBody, WithProject("test"))
diff --git a/deployments/docker/db.js b/deployments/docker/db.js
index 2f1ff7a..b051bfa 100644
--- a/deployments/docker/db.js
+++ b/deployments/docker/db.js
@@ -26,4 +26,26 @@ db.createUser(
             }
         ]
     }
-);
\ No newline at end of file
+);
+db.createCollection("counter");
+db.counter.insertOne( { name:"revision_counter",count: 1 } );
+db.createCollection( "kv", {
+    validator: { $jsonSchema: {
+            bsonType: "object",
+            required: [ "key","domain","project","id","value","create_revision","update_revision","value_type","label_id" ],
+            properties: {
+                key: {
+                    bsonType: "string",
+                    description: "must be a string and is required"
+                },
+                value_type: {
+                    enum: [ "text", "yaml", "json", "properties", "ini" ]
+                },
+                labels: {
+                    bsonType: "object"
+                }
+            }
+        } }
+} );
+db.kv.createIndex({"id": 1}, { unique: true } );
+db.kv.createIndex({key: 1, label_id: 1},{ unique: true });
\ No newline at end of file
diff --git a/deployments/docker/docker-compose.yaml b/deployments/docker/docker-compose.yaml
index 4bcbca0..2832c28 100644
--- a/deployments/docker/docker-compose.yaml
+++ b/deployments/docker/docker-compose.yaml
@@ -17,7 +17,7 @@
 version: '3.1'
 services:
   mongo:
-    image: mongo:3.4
+    image: mongo:4.0
     restart: always
     ports:
       - 27017:27017
diff --git a/examples/dev/clean.sh b/examples/dev/clean.sh
new file mode 100755
index 0000000..74891c5
--- /dev/null
+++ b/examples/dev/clean.sh
@@ -0,0 +1,2 @@
+sudo docker rm $(sudo docker ps -a -q)
+sudo docker volume prune
\ No newline at end of file
diff --git a/examples/dev/conf/chassis.yaml b/examples/dev/conf/chassis.yaml
index c98a93b..2b22ce9 100755
--- a/examples/dev/conf/chassis.yaml
+++ b/examples/dev/conf/chassis.yaml
@@ -7,8 +7,6 @@ cse:
   protocols:
     rest:
       listenAddress: 127.0.0.1:30110
-    rest-consul: #consul compatible API
-      listenAddress: 127.0.0.1:8500
   handler:
     chain:
       Provider:
diff --git a/deployments/docker/db.js b/examples/dev/db.js
similarity index 56%
copy from deployments/docker/db.js
copy to examples/dev/db.js
index 2f1ff7a..afd91c3 100644
--- a/deployments/docker/db.js
+++ b/examples/dev/db.js
@@ -26,4 +26,25 @@ db.createUser(
             }
         ]
     }
-);
\ No newline at end of file
+);
+db.createCollection("counter");
+db.counter.insertOne( { name:"revision_counter",count: 1 } );
+db.createCollection( "kv", {
+    validator: { $jsonSchema: {
+            bsonType: "object",
+            required: [ "key","domain","project","id","value","create_revision","update_revision","value_type","label_id" ],
+            properties: {
+                key: {
+                    bsonType: "string",
+                },
+                value_type: {
+                    enum: [ "text","string","yaml", "json", "properties", "ini" ]
+                },
+                labels: {
+                    bsonType: "object"
+                }
+            }
+        } }
+} );
+db.kv.createIndex({"id": 1}, { unique: true } );
+db.kv.createIndex({key: 1, label_id: 1},{ unique: true });
\ No newline at end of file
diff --git a/examples/dev/docker-compose.yaml b/examples/dev/docker-compose.yaml
index 700fa28..9c5a1e4 100644
--- a/examples/dev/docker-compose.yaml
+++ b/examples/dev/docker-compose.yaml
@@ -24,7 +24,9 @@ services:
     environment:
       MONGO_INITDB_ROOT_USERNAME: kie
       MONGO_INITDB_ROOT_PASSWORD: 123
-
+      MONGO_INITDB_DATABASE: kie
+    volumes:
+      - ./db.js:/docker-entrypoint-initdb.d/db.js:ro
   mongo-express:
     image: mongo-express
     restart: always
diff --git a/go.mod b/go.mod
index b9fe495..f65f24f 100644
--- a/go.mod
+++ b/go.mod
@@ -1,6 +1,7 @@
 module github.com/apache/servicecomb-kie
 
 require (
+	github.com/DataDog/zstd v1.4.4 // indirect
 	github.com/emicklei/go-restful v2.11.1+incompatible
 	github.com/go-chassis/foundation v0.1.1-0.20191113114104-2b05871e9ec4
 	github.com/go-chassis/go-archaius v1.0.0
@@ -17,7 +18,7 @@ require (
 	github.com/urfave/cli v1.20.0
 	github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
 	github.com/xdg/stringprep v1.0.0 // indirect
-	go.mongodb.org/mongo-driver v1.0.3
+	go.mongodb.org/mongo-driver v1.2.1
 	gopkg.in/yaml.v2 v2.2.4
 )
 
diff --git a/go.sum b/go.sum
index bdf601c..a13f0fb 100644
--- a/go.sum
+++ b/go.sum
@@ -9,6 +9,8 @@ github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxB
 github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc=
 github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
+github.com/DataDog/zstd v1.4.4 h1:+IawcoXhCBylN7ccwdwf8LOH2jKq7NavGpEPanrlTzE=
+github.com/DataDog/zstd v1.4.4/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
 github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
 github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
 github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
@@ -125,6 +127,8 @@ github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI
 github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
 github.com/hashicorp/mdns v1.0.0 h1:WhIgCr5a7AaVH6jPUwjtRuuE7/RDufnUvzIr48smyxs=
 github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
+github.com/hashicorp/mdns v1.0.1 h1:XFSOubp8KWB+Jd2PDyaX5xUd5bhSP/+pTDZVDMzZJM8=
+github.com/hashicorp/mdns v1.0.1/go.mod h1:4gW7WsVCke5TE7EPeYliwHlRUyBtfCwuFwuMg2DmyNY=
 github.com/hashicorp/memberlist v0.1.3 h1:EmmoJme1matNzb+hMpDuR/0sbJSUisxyqBGG676r31M=
 github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
 github.com/hashicorp/serf v0.8.5 h1:ZynDUIQiA8usmRgPdGPHFdPnb1wgGI9tK3mO9hcAJjc=
@@ -182,6 +186,7 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI
 github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
 github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
 github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
+github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
 github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -227,6 +232,8 @@ github.com/xdg/stringprep v1.0.0 h1:d9X0esnoa3dFsV0FG35rAT0RIhYFlPq7MiP+DW89La0=
 github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
 go.mongodb.org/mongo-driver v1.0.3 h1:GKoji1ld3tw2aC+GX1wbr/J2fX13yNacEYoJ8Nhr0yU=
 go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
+go.mongodb.org/mongo-driver v1.2.1 h1:ANAlYXXM5XmOdW/Nc38jOr+wS5nlk7YihT24U1imiWM=
+go.mongodb.org/mongo-driver v1.2.1/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
 go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
 golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
 golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
diff --git a/pkg/common/common.go b/pkg/common/common.go
index ba59d91..45739ad 100644
--- a/pkg/common/common.go
+++ b/pkg/common/common.go
@@ -22,15 +22,15 @@ const (
 	QueryParamQ      = "q"
 	QueryByLabelsCon = "&"
 	QueryParamWait   = "wait"
+	QueryParamRev    = "revision"
 	QueryParamMatch  = "match"
 	QueryParamKeyID  = "kv_id"
 )
 
 //http headers
 const (
-	HeaderMatch       = "X-Match"
 	HeaderDepth       = "X-Depth"
-	HeaderTenant      = "X-Domain"
+	HeaderRevision    = "X-Kie-Revision"
 	HeaderContentType = "Content-Type"
 	HeaderAccept      = "Accept"
 )
diff --git a/pkg/model/mongodb_doc.go b/pkg/model/db_schema.go
similarity index 54%
rename from pkg/model/mongodb_doc.go
rename to pkg/model/db_schema.go
index 5d6d9b6..dae9461 100644
--- a/pkg/model/mongodb_doc.go
+++ b/pkg/model/db_schema.go
@@ -27,15 +27,17 @@ type LabelDoc struct {
 
 //KVDoc is database struct to store kv
 type KVDoc struct {
-	ID        string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" swag:"string"`
-	LabelID   string `json:"label_id,omitempty" bson:"label_id,omitempty" yaml:"label_id,omitempty"`
-	Key       string `json:"key" yaml:"key"`
-	Value     string `json:"value,omitempty" yaml:"value,omitempty"`
-	ValueType string `json:"value_type,omitempty" bson:"value_type,omitempty" yaml:"value_type,omitempty"` //ini,json,text,yaml,properties
-	Checker   string `json:"check,omitempty" yaml:"check,omitempty"`                                       //python script
+	ID             string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" swag:"string"`
+	LabelID        string `json:"label_id,omitempty" bson:"label_id,omitempty" yaml:"label_id,omitempty"`
+	Key            string `json:"key" yaml:"key"`
+	Value          string `json:"value,omitempty" yaml:"value,omitempty"`
+	ValueType      string `json:"value_type,omitempty" bson:"value_type,omitempty" yaml:"value_type,omitempty"` //ini,json,text,yaml,properties
+	Checker        string `json:"check,omitempty" yaml:"check,omitempty"`                                       //python script
+	CreateRevision int64  `json:"create_revision,omitempty" bson:"create_revision," yaml:"create_revision,omitempty"`
+	UpdateRevision int64  `json:"update_revision,omitempty" bson:"update_revision," yaml:"update_revision,omitempty"`
+	Project        string `json:"project,omitempty" yaml:"project,omitempty"`
+
+	Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` //redundant
+	Domain string            `json:"domain,omitempty" yaml:"domain,omitempty"` //redundant
 
-	Labels   map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` //redundant
-	Domain   string            `json:"domain,omitempty" yaml:"domain,omitempty"` //redundant
-	Revision int               `json:"revision,omitempty" bson:"revision," yaml:"revision,omitempty"`
-	Project  string            `json:"project,omitempty" yaml:"project,omitempty"`
 }
diff --git a/server/id/id.go b/server/id/id.go
deleted file mode 100644
index b48556b..0000000
--- a/server/id/id.go
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * 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 id
-
-import (
-	"fmt"
-	"go.mongodb.org/mongo-driver/bson/bsontype"
-	"go.mongodb.org/mongo-driver/bson/primitive"
-	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
-)
-
-//ID decouple mongodb
-type ID string
-
-//UnmarshalBSONValue is implement
-func (id *ID) UnmarshalBSONValue(t bsontype.Type, raw []byte) error {
-	if t == bsontype.ObjectID && len(raw) == 12 {
-		var objID primitive.ObjectID
-		copy(objID[:], raw)
-		*id = ID(objID.Hex())
-		return nil
-	} else if t == bsontype.String {
-		if str, _, ok := bsoncore.ReadString(raw); ok {
-			*id = ID(str)
-			return nil
-		}
-	}
-
-	return fmt.Errorf("unable to unmarshal bson id &mdash; type: %v, length: %v", len(raw), t)
-}
-
-//String return string
-func (id ID) String() string {
-	return string(id)
-}
diff --git a/server/resource/v1/common.go b/server/resource/v1/common.go
index 538a2bd..d529e79 100644
--- a/server/resource/v1/common.go
+++ b/server/resource/v1/common.go
@@ -103,11 +103,6 @@ func WriteErrResponse(context *restful.Context, status int, msg, contentType str
 
 }
 
-//ErrLog record error
-func ErrLog(action string, kv *model.KVDoc, err error) {
-	openlogging.Error(fmt.Sprintf("[%s] [%v] err:%s", action, kv, err.Error()))
-}
-
 //InfoLog record info
 func InfoLog(action string, kv *model.KVDoc) {
 	openlogging.Info(
@@ -157,6 +152,13 @@ func getWaitDuration(rctx *restful.Context) string {
 	}
 	return wait
 }
+func getRevision(rctx *restful.Context) string {
+	rev := rctx.ReadQueryParameter(common.QueryParamRev)
+	if rev == "" {
+		rev = "0"
+	}
+	return rev
+}
 func getMatchPattern(rctx *restful.Context) string {
 	m := rctx.ReadQueryParameter(common.QueryParamMatch)
 
diff --git a/server/resource/v1/doc_struct.go b/server/resource/v1/doc_struct.go
index 6af548e..5ad7a4e 100644
--- a/server/resource/v1/doc_struct.go
+++ b/server/resource/v1/doc_struct.go
@@ -32,6 +32,12 @@ var (
 		ParamType: goRestful.HeaderParameterKind,
 		Desc:      "integer, default is 1, if you set match policy, you can set,depth to decide label number",
 	}
+	DocHeaderRevision = &restful.Parameters{
+		DataType:  "string",
+		Name:      common.HeaderRevision,
+		ParamType: goRestful.HeaderParameterKind,
+		Desc:      "integer, current revision of kie",
+	}
 )
 
 //swagger doc query params
diff --git a/server/resource/v1/kv_resource.go b/server/resource/v1/kv_resource.go
index 785d10b..f5335b1 100644
--- a/server/resource/v1/kv_resource.go
+++ b/server/resource/v1/kv_resource.go
@@ -19,6 +19,7 @@
 package v1
 
 import (
+	"fmt"
 	"github.com/apache/servicecomb-kie/server/pubsub"
 	"net/http"
 	"time"
@@ -54,7 +55,7 @@ func (r *KVResource) Put(context *restful.Context) {
 	kv.Project = project
 	kv, err = service.KVService.CreateOrUpdate(context.Ctx, kv)
 	if err != nil {
-		ErrLog("put", kv, err)
+		openlogging.Error(fmt.Sprintf("put [%v] err:%s", kv, err.Error()))
 		WriteErrResponse(context, http.StatusInternalServerError, err.Error(), common.ContentTypeText)
 		return
 	}
diff --git a/server/service/mongo/counter/revision.go b/server/service/mongo/counter/revision.go
new file mode 100644
index 0000000..cb4a6a1
--- /dev/null
+++ b/server/service/mongo/counter/revision.go
@@ -0,0 +1,77 @@
+/*
+ * 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 counter
+
+import (
+	"context"
+	"errors"
+	"github.com/apache/servicecomb-kie/server/service/mongo/session"
+	"github.com/go-mesh/openlogging"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/mongo/options"
+)
+
+const revision = "revision_counter"
+
+//Service is the implementation
+type Service struct {
+}
+
+//GetRevision return current revision number
+func (s *Service) GetRevision(ctx context.Context) (int64, error) {
+	collection := session.GetDB().Collection(session.CollectionCounter)
+	filter := bson.M{"name": revision}
+	cur, err := collection.Find(ctx, filter)
+	if err != nil {
+		if err.Error() == context.DeadlineExceeded.Error() {
+			openlogging.Error("operation timeout")
+			return 0, errors.New("operation timeout")
+		}
+		return 0, err
+	}
+	defer cur.Close(ctx)
+	c := &Counter{}
+	for cur.Next(ctx) {
+		if err := cur.Decode(c); err != nil {
+			openlogging.Error("decode error: " + err.Error())
+			return 0, err
+		}
+	}
+	return c.Count, nil
+}
+
+//ApplyRevision increase revision number and return modified value
+func ApplyRevision(ctx context.Context) (int64, error) {
+	collection := session.GetDB().Collection(session.CollectionCounter)
+	filter := bson.M{"name": revision}
+	sr := collection.FindOneAndUpdate(ctx, filter,
+		bson.D{
+			{"$inc", bson.D{
+				{"count", 1},
+			}}}, options.FindOneAndUpdate().SetReturnDocument(options.After))
+	if sr.Err() != nil {
+		return 0, sr.Err()
+	}
+	c := &Counter{}
+	err := sr.Decode(c)
+	if err != nil {
+		openlogging.Error("decode error: " + err.Error())
+		return 0, err
+	}
+	return c.Count, nil
+}
diff --git a/server/id/id_test.go b/server/service/mongo/counter/revision_test.go
similarity index 61%
rename from server/id/id_test.go
rename to server/service/mongo/counter/revision_test.go
index aa517f8..744fd9f 100644
--- a/server/id/id_test.go
+++ b/server/service/mongo/counter/revision_test.go
@@ -15,30 +15,26 @@
  * limitations under the License.
  */
 
-package id_test
+package counter_test
 
 import (
-	"github.com/apache/servicecomb-kie/server/id"
+	"context"
+	"github.com/apache/servicecomb-kie/server/config"
+	"github.com/apache/servicecomb-kie/server/service/mongo/counter"
+	"github.com/apache/servicecomb-kie/server/service/mongo/session"
 	"github.com/stretchr/testify/assert"
-	"go.mongodb.org/mongo-driver/bson"
-	"go.mongodb.org/mongo-driver/bson/primitive"
 	"testing"
 )
 
-func TestID_MarshalBSONValue(t *testing.T) {
-	type Obj struct {
-		ID id.ID `bson:"label_id,omitempty"`
-	}
-
-	o := new(Obj)
-	o.ID = id.ID(primitive.NewObjectID().Hex())
-
-	b, err := bson.Marshal(o)
+func TestIncreaseAndGetRevision(t *testing.T) {
+	var err error
+	config.Configurations = &config.Config{DB: config.DB{URI: "mongodb://kie:123@127.0.0.1:27017"}}
+	err = session.Init()
 	assert.NoError(t, err)
-	t.Log(b)
+	s := &counter.Service{}
+	n, _ := s.GetRevision(context.TODO())
+	t.Log(n)
 
-	o2 := new(Obj)
-	err = bson.Unmarshal(b, o2)
-	assert.NoError(t, err)
-	t.Log(o2)
+	next, _ := counter.ApplyRevision(context.TODO())
+	assert.Equal(t, n+1, next)
 }
diff --git a/deployments/docker/db.js b/server/service/mongo/counter/struct.go
similarity index 81%
copy from deployments/docker/db.js
copy to server/service/mongo/counter/struct.go
index 2f1ff7a..07ed5bf 100644
--- a/deployments/docker/db.js
+++ b/server/service/mongo/counter/struct.go
@@ -15,15 +15,10 @@
  * limitations under the License.
  */
 
-db.createUser(
-    {
-        user: "kie",
-        pwd: "123",
-        roles:[
-            {
-                role: "readWrite",
-                db:   "kie"
-            }
-        ]
-    }
-);
\ No newline at end of file
+package counter
+
+//Counter is db schema
+type Counter struct {
+	Name  string `bson:"name,omitempty"`
+	Count int64  `bson:"count,omitempty"`
+}
diff --git a/server/service/mongo/init.go b/server/service/mongo/init.go
index f5461dd..0b81c09 100644
--- a/server/service/mongo/init.go
+++ b/server/service/mongo/init.go
@@ -19,6 +19,7 @@ package mongo
 
 import (
 	"github.com/apache/servicecomb-kie/server/service"
+	"github.com/apache/servicecomb-kie/server/service/mongo/counter"
 	"github.com/apache/servicecomb-kie/server/service/mongo/history"
 	"github.com/apache/servicecomb-kie/server/service/mongo/kv"
 	"github.com/apache/servicecomb-kie/server/service/mongo/session"
@@ -30,4 +31,5 @@ func init() {
 	service.DBInit = session.Init
 	service.KVService = &kv.Service{}
 	service.HistoryService = &history.Service{}
+	service.RevisionService = &counter.Service{}
 }
diff --git a/server/service/mongo/kv/kv_dao.go b/server/service/mongo/kv/kv_dao.go
index f8eadd1..74ed8d1 100644
--- a/server/service/mongo/kv/kv_dao.go
+++ b/server/service/mongo/kv/kv_dao.go
@@ -22,6 +22,7 @@ import (
 	"fmt"
 	"github.com/apache/servicecomb-kie/pkg/model"
 	"github.com/apache/servicecomb-kie/server/service"
+	"github.com/apache/servicecomb-kie/server/service/mongo/counter"
 	"github.com/apache/servicecomb-kie/server/service/mongo/history"
 	"github.com/apache/servicecomb-kie/server/service/mongo/session"
 	"github.com/go-mesh/openlogging"
@@ -35,10 +36,21 @@ import (
 //and insert key
 func createKey(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, error) {
 	collection := session.GetDB().Collection(session.CollectionKV)
+	var err error
 	kv.ID = uuid.NewV4().String()
-	kv.Revision = 1
-	_, err := collection.InsertOne(ctx, kv)
+	revision, err := counter.ApplyRevision(ctx)
 	if err != nil {
+		openlogging.Error(err.Error())
+		return nil, err
+	}
+	kv.UpdateRevision = revision
+	kv.CreateRevision = revision
+	_, err = collection.InsertOne(ctx, kv)
+	if err != nil {
+		openlogging.Error("create error", openlogging.WithTags(openlogging.Tags{
+			"err": err.Error(),
+			"kv":  kv,
+		}))
 		return nil, err
 	}
 	err = history.AddHistory(ctx, kv)
@@ -55,12 +67,17 @@ func createKey(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, error) {
 
 //updateKeyValue update key value and add new revision
 func updateKeyValue(ctx context.Context, kv *model.KVDoc) error {
+	var err error
+	kv.UpdateRevision, err = counter.ApplyRevision(ctx)
+	if err != nil {
+		return err
+	}
 	collection := session.GetDB().Collection(session.CollectionKV)
 	ur, err := collection.UpdateOne(ctx, bson.M{"key": kv.Key, "label_id": kv.LabelID}, bson.D{
 		{"$set", bson.D{
 			{"value", kv.Value},
 			{"checker", kv.Checker},
-			{"revision", kv.Revision},
+			{"update_revision", kv.UpdateRevision},
 		}},
 	})
 	if err != nil {
@@ -110,14 +127,14 @@ func findOneKey(ctx context.Context, filter bson.M) ([]*model.KVDoc, error) {
 	collection := session.GetDB().Collection(session.CollectionKV)
 	sr := collection.FindOne(ctx, filter)
 	if sr.Err() != nil {
+		if sr.Err() == mongo.ErrNoDocuments {
+			return nil, service.ErrKeyNotExists
+		}
 		return nil, sr.Err()
 	}
 	curKV := &model.KVDoc{}
 	err := sr.Decode(curKV)
 	if err != nil {
-		if err == mongo.ErrNoDocuments {
-			return nil, service.ErrKeyNotExists
-		}
 		openlogging.Error("decode error: " + err.Error())
 		return nil, err
 	}
@@ -182,6 +199,7 @@ func findKVByLabelID(ctx context.Context, domain, labelID, key string, project s
 	filter := bson.M{"label_id": labelID, "domain": domain, "project": project}
 	if key != "" {
 		filter["key"] = key
+		openlogging.Debug("find one key")
 		return findOneKey(ctx, filter)
 	}
 	return findKeys(ctx, filter, true)
diff --git a/server/service/mongo/kv/kv_service.go b/server/service/mongo/kv/kv_service.go
index 76d1f3a..96d35de 100644
--- a/server/service/mongo/kv/kv_service.go
+++ b/server/service/mongo/kv/kv_service.go
@@ -70,19 +70,21 @@ func (s *Service) CreateOrUpdate(ctx context.Context, kv *model.KVDoc) (*model.K
 	}
 	oldKV, err := s.Exist(ctx, kv.Domain, kv.Key, kv.Project, service.WithLabelID(kv.LabelID))
 	if err != nil {
-		if err == service.ErrKeyNotExists {
-			kv, err := createKey(ctx, kv)
-			if err != nil {
-				return nil, err
-			}
-			kv.Domain = ""
-			kv.Project = ""
-			return kv, nil
+		if err != service.ErrKeyNotExists {
+			openlogging.Error(err.Error())
+			return nil, err
 		}
-		return nil, err
+		kv, err := createKey(ctx, kv)
+		if err != nil {
+			openlogging.Error(err.Error())
+			return nil, err
+		}
+		kv.Domain = ""
+		kv.Project = ""
+		return kv, nil
 	}
 	kv.ID = oldKV.ID
-	kv.Revision = oldKV.Revision + 1
+	kv.CreateRevision = oldKV.CreateRevision
 	err = updateKeyValue(ctx, kv)
 	if err != nil {
 		return nil, err
@@ -103,6 +105,7 @@ func (s *Service) Exist(ctx context.Context, domain, key string, project string,
 	if opts.LabelID != "" {
 		kvs, err := findKVByLabelID(ctx, domain, opts.LabelID, key, project)
 		if err != nil {
+			openlogging.Error(err.Error())
 			return nil, err
 		}
 		return kvs[0], nil
@@ -110,6 +113,7 @@ func (s *Service) Exist(ctx context.Context, domain, key string, project string,
 	kvs, err := s.FindKV(ctx, domain, project,
 		service.WithExactLabels(), service.WithLabels(opts.Labels), service.WithKey(key))
 	if err != nil {
+		openlogging.Error(err.Error())
 		return nil, err
 	}
 	if len(kvs) != 1 {
diff --git a/server/service/mongo/session/session.go b/server/service/mongo/session/session.go
index 2e4720e..58f9289 100644
--- a/server/service/mongo/session/session.go
+++ b/server/service/mongo/session/session.go
@@ -40,10 +40,12 @@ import (
 
 //const for db name and collection name
 const (
-	DBName               = "kie"
+	DBName = "kie"
+
 	CollectionLabel      = "label"
 	CollectionKV         = "kv"
 	CollectionKVRevision = "kv_revision"
+	CollectionCounter    = "counter"
 
 	DefaultTimeout   = 5 * time.Second
 	DefaultValueType = "text"
@@ -121,6 +123,7 @@ func Init() error {
 		db = client.Database(DBName, &options.DatabaseOptions{
 			Registry: reg,
 		})
+
 	})
 	return nil
 }
diff --git a/server/service/service.go b/server/service/service.go
index 1585a8d..e6e5310 100644
--- a/server/service/service.go
+++ b/server/service/service.go
@@ -25,9 +25,10 @@ import (
 
 //services
 var (
-	KVService      KV
-	HistoryService History
-	DBInit         Init
+	KVService       KV
+	HistoryService  History
+	RevisionService Revision
+	DBInit          Init
 )
 
 //db errors
@@ -50,6 +51,9 @@ type KV interface {
 type History interface {
 	GetHistory(ctx context.Context, keyID string, options ...FindOption) ([]*model.KVDoc, error)
 }
+type Revision interface {
+	GetRevision(ctx context.Context) (int64, error)
+}
 
 //Init init db session
 type Init func() error