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

littlecui 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 20cdeb9  decouple mongodb completely as a plugin (#190)
20cdeb9 is described below

commit 20cdeb9c93d66810d3a15b9f0ad41949e0b4071c
Author: Shawn <[email protected]>
AuthorDate: Sat Jul 31 15:31:31 2021 +0800

    decouple mongodb completely as a plugin (#190)
---
 .github/workflows/golangci-lint.yml                |   2 +-
 cmd/kieserver/main.go                              |   4 +-
 go.sum                                             |   2 +
 pkg/model/kv_test.go                               |   3 +-
 pkg/stringutil/string_util_test.go                 |   3 +-
 pkg/validator/rule_test.go                         |   3 +-
 server/config/config.go                            |   3 +-
 server/config/config_test.go                       |   7 +-
 server/config/struct.go                            |   3 +-
 server/{service/service.go => datasource/dao.go}   | 100 ++++++++++++++++-----
 .../mongo/counter/revision.go                      |   2 +-
 .../mongo/counter/revision_test.go                 |  17 ++--
 .../mongo/counter/struct.go                        |   0
 .../{service => datasource}/mongo/history/dao.go   |  11 +--
 .../mongo/history/service.go                       |   7 +-
 .../mongo/history/service_test.go                  |  15 ++--
 server/{service => datasource}/mongo/init.go       |  38 +++++---
 server/{service => datasource}/mongo/kv/kv_dao.go  |  23 +++--
 .../{service => datasource}/mongo/kv/kv_service.go |  45 ++++------
 .../mongo/kv/kv_service_test.go                    |  28 +++---
 server/{service => datasource}/mongo/kv/tool.go    |   0
 .../mongo/session/session.go                       |  69 ++++++--------
 .../mongo/session/session_test.go                  |  14 +--
 .../mongo/session/struct.go                        |   0
 .../mongo/track/polling_detail_dao.go              |   7 +-
 .../mongo/track/polling_detail_service.go          |   1 +
 .../{service => datasource}/mongo/view/view_dao.go |   3 +-
 .../mongo/view/view_service.go                     |  17 ++--
 .../mongo/view/view_service_test.go                |  23 ++---
 server/{service => datasource}/options.go          |  28 +++---
 server/handler/track_handler.go                    |  13 +--
 server/plugin/qms/quota.go                         |   5 +-
 server/pubsub/bus.go                               |   3 +-
 server/pubsub/bus_test.go                          |   3 +-
 server/pubsub/event_handler.go                     |   3 +-
 server/pubsub/struct_test.go                       |   3 +-
 server/rbac/rbac.go                                |   9 +-
 server/resource/v1/admin_resource.go               |  13 +--
 server/resource/v1/admin_resource_test.go          |   9 +-
 server/resource/v1/common.go                       |  11 +--
 server/resource/v1/common_test.go                  |   5 +-
 server/resource/v1/history_resource.go             |  17 ++--
 server/resource/v1/history_resource_test.go        |  11 +--
 server/resource/v1/kv_resource.go                  |  19 ++--
 server/resource/v1/kv_resource_test.go             |  26 +++---
 server/server.go                                   |   4 +-
 server/service/kv/kv.go                            |  27 +++---
 server/service/kv/override.go                      |   1 +
 server/service/kv/override_abort.go                |   1 +
 server/service/kv/override_force.go                |   5 +-
 server/service/kv/override_skip.go                 |   1 +
 test/init.go                                       |  11 +++
 52 files changed, 384 insertions(+), 294 deletions(-)

diff --git a/.github/workflows/golangci-lint.yml 
b/.github/workflows/golangci-lint.yml
index 508e33e..080f489 100644
--- a/.github/workflows/golangci-lint.yml
+++ b/.github/workflows/golangci-lint.yml
@@ -16,4 +16,4 @@ jobs:
         uses: golangci/golangci-lint-action@v2
         with:
           version: v1.29
-          args: --skip-dirs=examples,test --out-format=colored-line-number 
--skip-files=.*_test.go$
\ No newline at end of file
+          args: --skip-dirs=examples,test --skip-files=.*_test.go$
\ No newline at end of file
diff --git a/cmd/kieserver/main.go b/cmd/kieserver/main.go
index 978c04d..ddb4952 100644
--- a/cmd/kieserver/main.go
+++ b/cmd/kieserver/main.go
@@ -23,13 +23,15 @@ import (
        "github.com/apache/servicecomb-kie/server"
        "github.com/apache/servicecomb-kie/server/command"
        "github.com/go-chassis/openlog"
+
        //custom handlers
        _ "github.com/apache/servicecomb-kie/server/handler"
        _ "github.com/go-chassis/go-chassis/v2/middleware/jwt"
        _ "github.com/go-chassis/go-chassis/v2/middleware/monitoring"
        _ "github.com/go-chassis/go-chassis/v2/middleware/ratelimiter"
+
        //storage
-       _ "github.com/apache/servicecomb-kie/server/service/mongo"
+       _ "github.com/apache/servicecomb-kie/server/datasource/mongo"
        //quota management
        _ "github.com/apache/servicecomb-kie/server/plugin/qms"
        //noop cipher
diff --git a/go.sum b/go.sum
index 6682d6b..0650bf0 100644
--- a/go.sum
+++ b/go.sum
@@ -351,6 +351,7 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod 
h1:SYymIcj16QtmaHHD7aYtjjsJG7V
 github.com/karlseguin/ccache 
v2.0.3-0.20170217060820-3ba9789cfd2c+incompatible/go.mod 
h1:CM9tNPzT6EdRh14+jiW8mEF9mkNZuuE51qmgGYUB93w=
 github.com/karlseguin/ccache/v2 v2.0.8 
h1:lT38cE//uyf6KcFok0rlgXtGFBWxkI6h/qg4tbFyDnA=
 github.com/karlseguin/ccache/v2 v2.0.8/go.mod 
h1:2BDThcfQMf/c0jnZowt16eW405XIqZPavt+HoYEtcxQ=
+github.com/karlseguin/expect v1.0.2-0.20190806010014-778a5f0c6003 
h1:vJ0Snvo+SLMY72r5J4sEfkuE7AFbixEP2qRbEcum/wA=
 github.com/karlseguin/expect v1.0.2-0.20190806010014-778a5f0c6003/go.mod 
h1:zNBxMY8P21owkeogJELCLeHIt+voOSduHYTFUbwRAV8=
 github.com/karlseguin/expect v1.0.7/go.mod 
h1:lXdI8iGiQhmzpnnmU/EGA60vqKs8NbRNFnhhrJGoD5g=
 github.com/karrick/godirwalk v1.8.0/go.mod 
h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
@@ -545,6 +546,7 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod 
h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
 github.com/valyala/fasttemplate v1.0.1/go.mod 
h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
 github.com/valyala/fasttemplate v1.2.1/go.mod 
h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
 github.com/widuu/gojson v0.0.0-20170212122013-7da9d2cd949b/go.mod 
h1:9W1pyetRkwXqjR9tjOSrSuhGHBK0EqXoQSwWbhBHHwA=
+github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0 
h1:3UeQBvD0TFrlVjOeLOBz+CPAI8dnbqNSVwUwRrkp7vQ=
 github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0/go.mod 
h1:IXCdmsXIht47RaVFLEdVnh1t+pgYtTAhQGj73kz+2DM=
 github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c 
h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk=
 github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod 
h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
diff --git a/pkg/model/kv_test.go b/pkg/model/kv_test.go
index feb1ce3..2c9f789 100644
--- a/pkg/model/kv_test.go
+++ b/pkg/model/kv_test.go
@@ -19,9 +19,10 @@ package model_test
 
 import (
        "encoding/json"
+       "testing"
+
        "github.com/apache/servicecomb-kie/pkg/model"
        "github.com/stretchr/testify/assert"
-       "testing"
 )
 
 func TestKV_UnmarshalJSON(t *testing.T) {
diff --git a/pkg/stringutil/string_util_test.go 
b/pkg/stringutil/string_util_test.go
index 7dcac58..7963f8c 100644
--- a/pkg/stringutil/string_util_test.go
+++ b/pkg/stringutil/string_util_test.go
@@ -18,9 +18,10 @@
 package stringutil_test
 
 import (
+       "testing"
+
        "github.com/apache/servicecomb-kie/pkg/stringutil"
        "github.com/stretchr/testify/assert"
-       "testing"
 )
 
 func TestFormat(t *testing.T) {
diff --git a/pkg/validator/rule_test.go b/pkg/validator/rule_test.go
index 4302b4a..1a3e6fb 100644
--- a/pkg/validator/rule_test.go
+++ b/pkg/validator/rule_test.go
@@ -1,10 +1,11 @@
 package validator_test
 
 import (
-       "github.com/go-chassis/foundation/validator"
        "strings"
        "testing"
 
+       "github.com/go-chassis/foundation/validator"
+
        "github.com/apache/servicecomb-kie/pkg/model"
        validsvc "github.com/apache/servicecomb-kie/pkg/validator"
        "github.com/stretchr/testify/assert"
diff --git a/server/config/config.go b/server/config/config.go
index 27b6fd2..25bf5ac 100644
--- a/server/config/config.go
+++ b/server/config/config.go
@@ -18,10 +18,11 @@
 package config
 
 import (
+       "path/filepath"
+
        "github.com/go-chassis/go-archaius"
        "github.com/go-chassis/go-archaius/source/util"
        "gopkg.in/yaml.v2"
-       "path/filepath"
 )
 
 //Configurations is kie config items
diff --git a/server/config/config_test.go b/server/config/config_test.go
index 427118b..1250d2e 100644
--- a/server/config/config_test.go
+++ b/server/config/config_test.go
@@ -18,12 +18,13 @@
 package config_test
 
 import (
-       "github.com/apache/servicecomb-kie/server/config"
-       "github.com/go-chassis/go-archaius"
-       "github.com/stretchr/testify/assert"
        "io"
        "os"
        "testing"
+
+       "github.com/apache/servicecomb-kie/server/config"
+       "github.com/go-chassis/go-archaius"
+       "github.com/stretchr/testify/assert"
 )
 
 func TestInit(t *testing.T) {
diff --git a/server/config/struct.go b/server/config/struct.go
index fac1519..1a0a05d 100644
--- a/server/config/struct.go
+++ b/server/config/struct.go
@@ -29,9 +29,10 @@ type Config struct {
        AdvertiseAddr  string
 }
 
-//DB is yaml file struct to set mongodb config
+//DB is yaml file struct to set persistent config
 type DB struct {
        URI        string `yaml:"uri"`
+       Kind       string `yaml:"kind"`
        PoolSize   int    `yaml:"poolSize"`
        SSLEnabled bool   `yaml:"sslEnabled"`
        RootCA     string `yaml:"rootCAFile"`
diff --git a/server/service/service.go b/server/datasource/dao.go
similarity index 60%
rename from server/service/service.go
rename to server/datasource/dao.go
index 0648183..d4fab83 100644
--- a/server/service/service.go
+++ b/server/datasource/dao.go
@@ -15,33 +15,59 @@
  * limitations under the License.
  */
 
-package service
+//package dao supply pure persistence layer access
+package datasource
 
 import (
        "context"
        "errors"
+       "fmt"
+       "time"
+
        "github.com/apache/servicecomb-kie/pkg/model"
+       "github.com/apache/servicecomb-kie/server/config"
+       "github.com/go-chassis/openlog"
 )
 
-//services
 var (
-       KVService       KV
-       HistoryService  History
-       TrackService    Track
-       RevisionService Revision
-       DBInit          Init
+       b       Broker
+       plugins = make(map[string]New)
 )
 
-//db errors
 var (
        ErrKeyNotExists     = errors.New("can not find any key value")
        ErrRecordNotExists  = errors.New("can not find any polling data")
        ErrRevisionNotExist = errors.New("revision does not exist")
        ErrAliasNotGiven    = errors.New("label alias not given")
+       ErrKVAlreadyExists  = errors.New("kv already exists")
+       ErrTooMany          = errors.New("key with labels should be only one")
+)
+
+const (
+       DefaultValueType = "text"
 )
 
-//KV provide api of KV entity
-type KV interface {
+//New init db session
+type New func(c *Config) (Broker, error)
+
+func RegisterPlugin(name string, f New) {
+       plugins[name] = f
+}
+
+//Broker avoid directly depend on one kind of persistence solution
+type Broker interface {
+       GetRevisionDao() RevisionDao
+       GetHistoryDao() HistoryDao
+       GetTrackDao() TrackDao
+       GetKVDao() KVDao
+}
+
+func GetBroker() Broker {
+       return b
+}
+
+//KVDao provide api of KV entity
+type KVDao interface {
        //below 3 methods is usually for admin console
        Create(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, error)
        Update(ctx context.Context, kv *model.UpdateKVRequest) (*model.KVDoc, 
error)
@@ -52,28 +78,28 @@ type KV interface {
        FindManyAndDelete(ctx context.Context, kvIDs []string, domain, project 
string) ([]*model.KVDoc, error)
        //Get return kv by id
        Get(ctx context.Context, request *model.GetKVRequest) (*model.KVDoc, 
error)
-       //KV is a resource of kie, this api should return kv resource number by 
domain id
+       //KVDao is a resource of kie, this api should return kv resource number 
by domain id
        Total(ctx context.Context, domain string) (int64, error)
 }
 
-//History provide api of History entity
-type History interface {
+//HistoryDao provide api of HistoryDao entity
+type HistoryDao interface {
        GetHistory(ctx context.Context, keyID string, options ...FindOption) 
(*model.KVResponse, error)
 }
 
-//History provide api of History entity
-type Track interface {
+//TrackDao provide api of TrackDao entity
+type TrackDao interface {
        CreateOrUpdate(ctx context.Context, detail *model.PollingDetail) 
(*model.PollingDetail, error)
        GetPollingDetail(ctx context.Context, detail *model.PollingDetail) 
([]*model.PollingDetail, error)
 }
 
-//Revision is global revision number management
-type Revision interface {
+//RevisionDao is global revision number management
+type RevisionDao interface {
        GetRevision(ctx context.Context, domain string) (int64, error)
 }
 
-//View create update and get view data
-type View interface {
+//ViewDao create update and get view data
+type ViewDao interface {
        Create(ctx context.Context, viewDoc *model.ViewDoc, options 
...FindOption) error
        Update(ctx context.Context, viewDoc *model.ViewDoc) error
        //TODO
@@ -82,5 +108,37 @@ type View interface {
        GetContent(ctx context.Context, id, domain, project string, options 
...FindOption) ([]*model.KVResponse, error)
 }
 
-//Init init db session
-type Init func() error
+const DefaultTimeout = 5 * time.Second
+
+func Init(c config.DB) error {
+       var err error
+       if c.Kind == "" {
+               c.Kind = "mongo"
+       }
+       f, ok := plugins[c.Kind]
+       if !ok {
+               return fmt.Errorf("do not support %s", c.Kind)
+       }
+       var timeout time.Duration
+       if c.Timeout != "" {
+               timeout, err = time.ParseDuration(c.Timeout)
+               if err != nil {
+                       return errors.New("timeout setting invalid:" + 
c.Timeout)
+               }
+       }
+       if timeout == 0 {
+               timeout = DefaultTimeout
+       }
+       dbc := &Config{
+               URI:        c.URI,
+               PoolSize:   c.PoolSize,
+               SSLEnabled: c.SSLEnabled,
+               RootCA:     c.RootCA,
+               Timeout:    timeout,
+       }
+       if b, err = f(dbc); err != nil {
+               return err
+       }
+       openlog.Info(fmt.Sprintf("use %s as storage", c.Kind))
+       return nil
+}
diff --git a/server/service/mongo/counter/revision.go 
b/server/datasource/mongo/counter/revision.go
similarity index 97%
rename from server/service/mongo/counter/revision.go
rename to server/datasource/mongo/counter/revision.go
index 7b638cf..90c2d9c 100644
--- a/server/service/mongo/counter/revision.go
+++ b/server/datasource/mongo/counter/revision.go
@@ -21,7 +21,7 @@ import (
        "context"
        "errors"
 
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
        "github.com/go-chassis/openlog"
        "go.mongodb.org/mongo-driver/bson"
        "go.mongodb.org/mongo-driver/mongo/options"
diff --git a/server/service/mongo/counter/revision_test.go 
b/server/datasource/mongo/counter/revision_test.go
similarity index 78%
rename from server/service/mongo/counter/revision_test.go
rename to server/datasource/mongo/counter/revision_test.go
index b35c91d..7ff2c78 100644
--- a/server/service/mongo/counter/revision_test.go
+++ b/server/datasource/mongo/counter/revision_test.go
@@ -18,20 +18,25 @@
 package counter_test
 
 import (
+       "time"
+
+       "github.com/apache/servicecomb-kie/server/datasource"
        _ "github.com/apache/servicecomb-kie/test"
 
        "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"
        "testing"
+
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/counter"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
+       "github.com/stretchr/testify/assert"
 )
 
 func TestIncreaseAndGetRevision(t *testing.T) {
        var err error
-       config.Configurations = &config.Config{DB: config.DB{URI: 
"mongodb://kie:[email protected]:27017/kie"}}
-       err = session.Init()
+       err = session.Init(&datasource.Config{
+               URI:     "mongodb://kie:[email protected]:27017/kie",
+               Timeout: 10 * time.Second,
+       })
        assert.NoError(t, err)
        s := &counter.Service{}
        n, _ := s.GetRevision(context.TODO(), "default")
diff --git a/server/service/mongo/counter/struct.go 
b/server/datasource/mongo/counter/struct.go
similarity index 100%
rename from server/service/mongo/counter/struct.go
rename to server/datasource/mongo/counter/struct.go
diff --git a/server/service/mongo/history/dao.go 
b/server/datasource/mongo/history/dao.go
similarity index 93%
rename from server/service/mongo/history/dao.go
rename to server/datasource/mongo/history/dao.go
index 58e723a..3c78874 100644
--- a/server/service/mongo/history/dao.go
+++ b/server/datasource/mongo/history/dao.go
@@ -22,9 +22,10 @@ import (
        "fmt"
        "time"
 
+       "github.com/apache/servicecomb-kie/server/datasource"
+
        "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/server/service"
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
        "github.com/go-chassis/openlog"
        "go.mongodb.org/mongo-driver/bson"
        "go.mongodb.org/mongo-driver/mongo/options"
@@ -67,7 +68,7 @@ func getHistoryByKeyID(ctx context.Context, filter bson.M, 
offset, limit int64)
                kvs = append(kvs, &elem)
        }
        if !exist {
-               return nil, service.ErrRevisionNotExist
+               return nil, datasource.ErrRevisionNotExist
        }
        result := &model.KVResponse{
                Data:  kvs,
@@ -78,8 +79,6 @@ func getHistoryByKeyID(ctx context.Context, filter bson.M, 
offset, limit int64)
 
 //AddHistory add kv history
 func AddHistory(ctx context.Context, kv *model.KVDoc) error {
-       ctx, cancel := context.WithTimeout(ctx, session.Timeout)
-       defer cancel()
        collection := session.GetDB().Collection(session.CollectionKVRevision)
        _, err := collection.InsertOne(ctx, kv)
        if err != nil {
@@ -118,8 +117,6 @@ func AddDeleteTime(ctx context.Context, kvIDs []string, 
project, domain string)
 
 //historyRotate delete historical versions for a key that exceeds the limited 
number
 func historyRotate(ctx context.Context, kvID, project, domain string) error {
-       ctx, cancel := context.WithTimeout(ctx, session.Timeout)
-       defer cancel()
        filter := bson.M{"id": kvID, "domain": domain, "project": project}
        collection := session.GetDB().Collection(session.CollectionKVRevision)
        curTotal, err := collection.CountDocuments(ctx, filter)
diff --git a/server/service/mongo/history/service.go 
b/server/datasource/mongo/history/service.go
similarity index 89%
rename from server/service/mongo/history/service.go
rename to server/datasource/mongo/history/service.go
index f6e13c1..c089fdf 100644
--- a/server/service/mongo/history/service.go
+++ b/server/datasource/mongo/history/service.go
@@ -19,8 +19,9 @@ package history
 
 import (
        "context"
+
        "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/server/service"
+       "github.com/apache/servicecomb-kie/server/datasource"
        "go.mongodb.org/mongo-driver/bson"
        "go.mongodb.org/mongo-driver/bson/primitive"
 )
@@ -30,9 +31,9 @@ type Service struct {
 }
 
 //GetHistory get all history by label id
-func (s *Service) GetHistory(ctx context.Context, kvID string, options 
...service.FindOption) (*model.KVResponse, error) {
+func (s *Service) GetHistory(ctx context.Context, kvID string, options 
...datasource.FindOption) (*model.KVResponse, error) {
        var filter primitive.M
-       opts := service.FindOptions{}
+       opts := datasource.FindOptions{}
        for _, o := range options {
                o(&opts)
        }
diff --git a/server/service/mongo/history/service_test.go 
b/server/datasource/mongo/history/service_test.go
similarity index 84%
rename from server/service/mongo/history/service_test.go
rename to server/datasource/mongo/history/service_test.go
index 2d6fcc0..280ef52 100644
--- a/server/service/mongo/history/service_test.go
+++ b/server/datasource/mongo/history/service_test.go
@@ -19,18 +19,23 @@ package history_test
 
 import (
        "context"
+       "time"
 
-       "github.com/apache/servicecomb-kie/server/config"
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
+       "github.com/apache/servicecomb-kie/server/datasource"
+
+       "testing"
+
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
        _ "github.com/apache/servicecomb-kie/test"
        "github.com/stretchr/testify/assert"
        "go.mongodb.org/mongo-driver/bson"
-       "testing"
 )
 
 func init() {
-       config.Configurations = &config.Config{DB: config.DB{URI: 
"mongodb://kie:[email protected]:27017/kie"}}
-       _ = session.Init()
+       session.Init(&datasource.Config{
+               URI:     "mongodb://kie:[email protected]:27017/kie",
+               Timeout: 10 * time.Second,
+       })
 }
 
 func TestAddHistory(t *testing.T) {
diff --git a/server/service/mongo/init.go b/server/datasource/mongo/init.go
similarity index 51%
rename from server/service/mongo/init.go
rename to server/datasource/mongo/init.go
index e53d55a..b01dae4 100644
--- a/server/service/mongo/init.go
+++ b/server/datasource/mongo/init.go
@@ -18,20 +18,34 @@
 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"
-       "github.com/apache/servicecomb-kie/server/service/mongo/track"
+       "github.com/apache/servicecomb-kie/server/datasource"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/counter"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/history"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/kv"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/track"
        "github.com/go-chassis/openlog"
 )
 
-func init() {
+type Broker struct {
+}
+
+func NewFrom(c *datasource.Config) (datasource.Broker, error) {
        openlog.Info("use mongodb as storage")
-       service.DBInit = session.Init
-       service.KVService = &kv.Service{}
-       service.HistoryService = &history.Service{}
-       service.TrackService = &track.Service{}
-       service.RevisionService = &counter.Service{}
+       return &Broker{}, session.Init(c)
+}
+func (*Broker) GetRevisionDao() datasource.RevisionDao {
+       return &counter.Service{}
+}
+func (*Broker) GetKVDao() datasource.KVDao {
+       return &kv.Service{}
+}
+func (*Broker) GetHistoryDao() datasource.HistoryDao {
+       return &history.Service{}
+}
+func (*Broker) GetTrackDao() datasource.TrackDao {
+       return &track.Service{}
+}
+func init() {
+       datasource.RegisterPlugin("mongo", NewFrom)
 }
diff --git a/server/service/mongo/kv/kv_dao.go 
b/server/datasource/mongo/kv/kv_dao.go
similarity index 94%
rename from server/service/mongo/kv/kv_dao.go
rename to server/datasource/mongo/kv/kv_dao.go
index 38ab84f..e81bd54 100644
--- a/server/service/mongo/kv/kv_dao.go
+++ b/server/datasource/mongo/kv/kv_dao.go
@@ -24,11 +24,12 @@ import (
        "strings"
        "time"
 
+       "github.com/apache/servicecomb-kie/server/datasource"
+
        "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/apache/servicecomb-kie/server/datasource/mongo/counter"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/history"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
        "github.com/go-chassis/openlog"
        uuid "github.com/satori/go.uuid"
        "go.mongodb.org/mongo-driver/bson"
@@ -117,7 +118,7 @@ func getValue(str string) string {
        return res[len(res)-1]
 }
 
-func findKV(ctx context.Context, domain string, project string, opts 
service.FindOptions) (*mongo.Cursor, int, error) {
+func findKV(ctx context.Context, domain string, project string, opts 
datasource.FindOptions) (*mongo.Cursor, int, error) {
        collection := session.GetDB().Collection(session.CollectionKV)
        ctx, cancel := context.WithTimeout(ctx, opts.Timeout)
        defer cancel()
@@ -175,7 +176,7 @@ func findOneKey(ctx context.Context, filter bson.M) 
([]*model.KVDoc, error) {
        sr := collection.FindOne(ctx, filter)
        if sr.Err() != nil {
                if sr.Err() == mongo.ErrNoDocuments {
-                       return nil, service.ErrKeyNotExists
+                       return nil, datasource.ErrKeyNotExists
                }
                return nil, sr.Err()
        }
@@ -194,7 +195,7 @@ func findOneKVAndDelete(ctx context.Context, kvID, project, 
domain string) (*mod
        sr := collection.FindOneAndDelete(ctx, bson.M{"id": kvID, "project": 
project, "domain": domain})
        if sr.Err() != nil {
                if sr.Err() == mongo.ErrNoDocuments {
-                       return nil, service.ErrKeyNotExists
+                       return nil, datasource.ErrKeyNotExists
                }
                return nil, sr.Err()
        }
@@ -224,7 +225,7 @@ func findKVsAndDelete(ctx context.Context, kvIDs []string, 
project, domain strin
                {Key: "domain", Value: domain}}
        kvs, err := findKeys(ctx, filter, false)
        if err != nil {
-               if err != service.ErrKeyNotExists {
+               if err != datasource.ErrKeyNotExists {
                        openlog.Error("find Keys error: " + err.Error())
                }
                return nil, err
@@ -256,9 +257,7 @@ func findKeys(ctx context.Context, filter interface{}, 
withoutLabel bool) ([]*mo
        cur, err := collection.Find(ctx, filter)
        if err != nil {
                if err.Error() == context.DeadlineExceeded.Error() {
-                       openlog.Error("find kvs failed, dead line exceeded", 
openlog.WithTags(openlog.Tags{
-                               "timeout": session.Timeout,
-                       }))
+                       openlog.Error("find kvs failed: " + err.Error())
                        return nil, fmt.Errorf("can not find keys due to 
timout")
                }
                return nil, err
@@ -281,7 +280,7 @@ func findKeys(ctx context.Context, filter interface{}, 
withoutLabel bool) ([]*mo
 
        }
        if len(kvs) == 0 {
-               return nil, service.ErrKeyNotExists
+               return nil, datasource.ErrKeyNotExists
        }
        return kvs, nil
 }
diff --git a/server/service/mongo/kv/kv_service.go 
b/server/datasource/mongo/kv/kv_service.go
similarity index 79%
rename from server/service/mongo/kv/kv_service.go
rename to server/datasource/mongo/kv/kv_service.go
index eb085fc..092fba3 100644
--- a/server/service/mongo/kv/kv_service.go
+++ b/server/datasource/mongo/kv/kv_service.go
@@ -19,11 +19,11 @@ package kv
 
 import (
        "context"
+
        "github.com/apache/servicecomb-kie/pkg/model"
        "github.com/apache/servicecomb-kie/pkg/stringutil"
        "github.com/apache/servicecomb-kie/pkg/util"
-       "github.com/apache/servicecomb-kie/server/service"
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
+       "github.com/apache/servicecomb-kie/server/datasource"
        "github.com/go-chassis/openlog"
 )
 
@@ -41,22 +41,19 @@ type Service struct {
 
 //Create will create a key value record
 func (s *Service) Create(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, 
error) {
-       ctx, cancel := context.WithTimeout(ctx, session.Timeout)
-       defer cancel()
-
        if kv.Labels == nil {
                kv.Labels = map[string]string{}
        }
        //check whether the project has certain labels or not
        kv.LabelFormat = stringutil.FormatMap(kv.Labels)
        if kv.ValueType == "" {
-               kv.ValueType = session.DefaultValueType
+               kv.ValueType = datasource.DefaultValueType
        }
-       _, err := s.Exist(ctx, kv.Domain, kv.Key, kv.Project, 
service.WithLabelFormat(kv.LabelFormat))
+       _, err := s.Exist(ctx, kv.Domain, kv.Key, kv.Project, 
datasource.WithLabelFormat(kv.LabelFormat))
        if err == nil {
-               return nil, session.ErrKVAlreadyExists
+               return nil, datasource.ErrKVAlreadyExists
        }
-       if err != service.ErrKeyNotExists {
+       if err != datasource.ErrKeyNotExists {
                openlog.Error(err.Error())
                return nil, err
        }
@@ -71,9 +68,6 @@ func (s *Service) Create(ctx context.Context, kv 
*model.KVDoc) (*model.KVDoc, er
 
 //Update will update a key value record
 func (s *Service) Update(ctx context.Context, kv *model.UpdateKVRequest) 
(*model.KVDoc, error) {
-       ctx, cancel := context.WithTimeout(ctx, session.Timeout)
-       defer cancel()
-
        getRequest := &model.GetKVRequest{
                Domain:  kv.Domain,
                Project: kv.Project,
@@ -99,18 +93,15 @@ func (s *Service) Update(ctx context.Context, kv 
*model.UpdateKVRequest) (*model
 }
 
 //Exist supports you query a key value by label map or labels id
-func (s *Service) Exist(ctx context.Context, domain, key string, project 
string, options ...service.FindOption) (*model.KVDoc, error) {
-       ctx, cancel := context.WithTimeout(ctx, session.Timeout)
-       defer cancel()
-
-       opts := service.FindOptions{}
+func (s *Service) Exist(ctx context.Context, domain, key string, project 
string, options ...datasource.FindOption) (*model.KVDoc, error) {
+       opts := datasource.FindOptions{}
        for _, o := range options {
                o(&opts)
        }
        if opts.LabelFormat != "" {
                kvs, err := findKVByLabel(ctx, domain, opts.LabelFormat, key, 
project)
                if err != nil {
-                       if err != service.ErrKeyNotExists {
+                       if err != datasource.ErrKeyNotExists {
                                openlog.Error(err.Error())
                        }
                        return nil, err
@@ -118,15 +109,15 @@ func (s *Service) Exist(ctx context.Context, domain, key 
string, project string,
                return kvs[0], nil
        }
        kvs, err := s.List(ctx, domain, project,
-               service.WithExactLabels(),
-               service.WithLabels(opts.Labels),
-               service.WithKey(key))
+               datasource.WithExactLabels(),
+               datasource.WithLabels(opts.Labels),
+               datasource.WithKey(key))
        if err != nil {
                openlog.Error("check kv exist: " + err.Error())
                return nil, err
        }
        if len(kvs.Data) != 1 {
-               return nil, session.ErrTooMany
+               return nil, datasource.ErrTooMany
        }
 
        return kvs.Data[0], nil
@@ -136,21 +127,17 @@ func (s *Service) Exist(ctx context.Context, domain, key 
string, project string,
 //FindOneAndDelete deletes one kv by id and return the deleted kv as these 
appeared before deletion
 //domain=tenant
 func (s *Service) FindOneAndDelete(ctx context.Context, kvID string, domain 
string, project string) (*model.KVDoc, error) {
-       ctx, cancel := context.WithTimeout(ctx, session.Timeout)
-       defer cancel()
        return findOneKVAndDelete(ctx, kvID, project, domain)
 }
 
 //FindManyAndDelete deletes multiple kvs and return the deleted kv list as 
these appeared before deletion
 func (s *Service) FindManyAndDelete(ctx context.Context, kvIDs []string, 
domain string, project string) ([]*model.KVDoc, error) {
-       ctx, cancel := context.WithTimeout(ctx, session.Timeout)
-       defer cancel()
        return findKVsAndDelete(ctx, kvIDs, project, domain)
 }
 
 //List get kv list by key and criteria
-func (s *Service) List(ctx context.Context, domain, project string, options 
...service.FindOption) (*model.KVResponse, error) {
-       opts := service.NewDefaultFindOpts()
+func (s *Service) List(ctx context.Context, domain, project string, options 
...datasource.FindOption) (*model.KVResponse, error) {
+       opts := datasource.NewDefaultFindOpts()
        for _, o := range options {
                o(&opts)
        }
@@ -187,7 +174,5 @@ func (s *Service) Get(ctx context.Context, request 
*model.GetKVRequest) (*model.
 
 //Total return kv record number
 func (s *Service) Total(ctx context.Context, domain string) (int64, error) {
-       ctx, cancel := context.WithTimeout(ctx, session.Timeout)
-       defer cancel()
        return total(ctx, domain)
 }
diff --git a/server/service/mongo/kv/kv_service_test.go 
b/server/datasource/mongo/kv/kv_service_test.go
similarity index 86%
rename from server/service/mongo/kv/kv_service_test.go
rename to server/datasource/mongo/kv/kv_service_test.go
index 07e854d..7ad9c71 100644
--- a/server/service/mongo/kv/kv_service_test.go
+++ b/server/datasource/mongo/kv/kv_service_test.go
@@ -18,19 +18,21 @@
 package kv_test
 
 import (
+       "time"
+
+       "github.com/apache/servicecomb-kie/server/datasource"
        _ "github.com/apache/servicecomb-kie/test"
 
        "context"
+       "testing"
+
        common2 "github.com/apache/servicecomb-kie/pkg/common"
        "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/server/config"
-       "github.com/apache/servicecomb-kie/server/service"
-       "github.com/apache/servicecomb-kie/server/service/mongo/kv"
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/kv"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
        "github.com/go-chassis/openlog"
        log "github.com/go-chassis/seclog"
        "github.com/stretchr/testify/assert"
-       "testing"
 )
 
 var id string
@@ -47,8 +49,10 @@ func init() {
 
 func TestService_CreateOrUpdate(t *testing.T) {
        var err error
-       config.Configurations = &config.Config{DB: config.DB{URI: 
"mongodb://kie:[email protected]:27017/kie"}}
-       err = session.Init()
+       err = session.Init(&datasource.Config{
+               URI:     "mongodb://kie:[email protected]:27017/kie",
+               Timeout: 10 * time.Second,
+       })
        assert.NoError(t, err)
        kvsvc := &kv.Service{}
        t.Run("put kv timeout,with labels app and service", func(t *testing.T) {
@@ -79,7 +83,7 @@ func TestService_CreateOrUpdate(t *testing.T) {
                        Domain:  "default",
                        Project: "kv-test",
                })
-               oid, err := kvsvc.Exist(context.TODO(), "default", "timeout", 
"kv-test", service.WithLabels(map[string]string{
+               oid, err := kvsvc.Exist(context.TODO(), "default", "timeout", 
"kv-test", datasource.WithLabels(map[string]string{
                        "app":     "mall",
                        "service": "cart",
                        "version": "1.0.0",
@@ -108,7 +112,7 @@ func TestService_CreateOrUpdate(t *testing.T) {
                        Project: "kv-test",
                })
                assert.Equal(t, "3s", afterKV.Value)
-               savedKV, err := kvsvc.Exist(context.Background(), "default", 
"timeout", "kv-test", service.WithLabels(map[string]string{
+               savedKV, err := kvsvc.Exist(context.Background(), "default", 
"timeout", "kv-test", datasource.WithLabels(map[string]string{
                        "app": "mall",
                }))
                assert.NoError(t, err)
@@ -147,13 +151,13 @@ func TestService_Create(t *testing.T) {
                        Domain:  "default",
                        Project: "kv-test",
                })
-               assert.EqualError(t, err, session.ErrKVAlreadyExists.Error())
+               assert.EqualError(t, err, datasource.ErrKVAlreadyExists.Error())
        })
        t.Run("list the kv", func(t *testing.T) {
-               res, err := kvsvc.List(context.TODO(), "default", "kv-test", 
service.WithKey("wildcard(time*1)"))
+               res, err := kvsvc.List(context.TODO(), "default", "kv-test", 
datasource.WithKey("wildcard(time*1)"))
                assert.NoError(t, err)
                assert.Equal(t, 0, len(res.Data))
-               res, err = kvsvc.List(context.TODO(), "default", "kv-test", 
service.WithKey("wildcard(time*t)"))
+               res, err = kvsvc.List(context.TODO(), "default", "kv-test", 
datasource.WithKey("wildcard(time*t)"))
                assert.NoError(t, err)
                assert.NotEqual(t, 0, len(res.Data))
        })
diff --git a/server/service/mongo/kv/tool.go 
b/server/datasource/mongo/kv/tool.go
similarity index 100%
rename from server/service/mongo/kv/tool.go
rename to server/datasource/mongo/kv/tool.go
diff --git a/server/service/mongo/session/session.go 
b/server/datasource/mongo/session/session.go
similarity index 86%
rename from server/service/mongo/session/session.go
rename to server/datasource/mongo/session/session.go
index 48b6b90..b0e4bf9 100644
--- a/server/service/mongo/session/session.go
+++ b/server/datasource/mongo/session/session.go
@@ -24,21 +24,22 @@ import (
        "crypto/x509"
        "errors"
        "fmt"
-       "github.com/apache/servicecomb-kie/pkg/cipherutil"
-       "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/go-chassis/openlog"
-       "go.mongodb.org/mongo-driver/bson"
-       "go.mongodb.org/mongo-driver/bson/bsoncodec"
-       "go.mongodb.org/mongo-driver/mongo/options"
-       "gopkg.in/mgo.v2"
        "io/ioutil"
        "reflect"
        "strings"
        "sync"
        "time"
 
-       "github.com/apache/servicecomb-kie/server/config"
+       "github.com/apache/servicecomb-kie/pkg/cipherutil"
+       "github.com/apache/servicecomb-kie/pkg/model"
+       "github.com/apache/servicecomb-kie/server/datasource"
+       "github.com/go-chassis/openlog"
+
+       "go.mongodb.org/mongo-driver/bson"
+       "go.mongodb.org/mongo-driver/bson/bsoncodec"
        "go.mongodb.org/mongo-driver/mongo"
+       "go.mongodb.org/mongo-driver/mongo/options"
+       "gopkg.in/mgo.v2"
 )
 
 //const for db name and collection name
@@ -51,16 +52,14 @@ const (
        CollectionPollingDetail = "polling_detail"
        CollectionCounter       = "counter"
        CollectionView          = "view"
-       DefaultTimeout          = 5 * time.Second
-       DefaultValueType        = "text"
 )
 
 //db errors
 var (
-       ErrMissingDomain   = errors.New("domain info missing, illegal access")
-       ErrMissingProject  = errors.New("project info missing, illegal access")
-       ErrLabelNotExists  = errors.New("labels does not exits")
-       ErrTooMany         = errors.New("key with labels should be only one")
+       ErrMissingDomain  = errors.New("domain info missing, illegal access")
+       ErrMissingProject = errors.New("project info missing, illegal access")
+       ErrLabelNotExists = errors.New("labels does not exits")
+
        ErrKeyMustNotEmpty = errors.New("must supply key if you want to get 
exact one result")
 
        ErrIDIsNil         = errors.New("id is empty")
@@ -85,36 +84,24 @@ var client *mongo.Client
 var once sync.Once
 var db *mongo.Database
 
-//Timeout db operation time out
-var Timeout time.Duration
-
 //Init prepare params
-func Init() error {
+func Init(c *datasource.Config) error {
        var err error
-       if config.GetDB().Timeout != "" {
-               Timeout, err = time.ParseDuration(config.GetDB().Timeout)
-               if err != nil {
-                       return errors.New("timeout setting invalid:" + 
config.GetDB().Timeout)
-               }
-       }
-       if Timeout == 0 {
-               Timeout = DefaultTimeout
-       }
        once.Do(func() {
                sc, _ := 
bsoncodec.NewStructCodec(bsoncodec.DefaultStructTagParser)
                reg := bson.NewRegistryBuilder().
                        RegisterTypeEncoder(reflect.TypeOf(model.LabelDoc{}), 
sc).
                        RegisterTypeEncoder(reflect.TypeOf(model.KVDoc{}), sc).
                        Build()
-               uri := cipherutil.TryDecrypt(config.GetDB().URI)
+               uri := cipherutil.TryDecrypt(c.URI)
                clientOps := 
[]*options.ClientOptions{options.Client().ApplyURI(uri)}
-               if config.GetDB().SSLEnabled {
-                       if config.GetDB().RootCA == "" {
+               if c.SSLEnabled {
+                       if c.RootCA == "" {
                                openlog.Error(ErrRootCAMissing.Error())
                                return
                        }
                        pool := x509.NewCertPool()
-                       caCert, err := ioutil.ReadFile(config.GetDB().RootCA)
+                       caCert, err := ioutil.ReadFile(c.RootCA)
                        if err != nil {
                                openlog.Error(fmt.Sprintf("read ca cert file %s 
failed", caCert))
                                return
@@ -123,7 +110,7 @@ func Init() error {
                        // #nosec
                        tc := &tls.Config{
                                RootCAs:            pool,
-                               InsecureSkipVerify: !config.GetDB().VerifyPeer,
+                               InsecureSkipVerify: !c.VerifyPeer,
                        }
                        clientOps = append(clientOps, 
options.Client().SetTLSConfig(tc))
                        openlog.Info("enabled ssl communication to mongodb")
@@ -147,7 +134,7 @@ func Init() error {
                })
 
        })
-       EnsureDB()
+       EnsureDB(c)
        return nil
 }
 
@@ -202,8 +189,8 @@ func GetColInfo(ctx context.Context, name string) 
(*CollectionInfo, error) {
 }
 
 //EnsureDB build mongo db schema
-func EnsureDB() {
-       session := OpenSession()
+func EnsureDB(c *datasource.Config) {
+       session := OpenSession(c)
        defer session.Close()
        session.SetMode(mgo.Primary, true)
 
@@ -218,17 +205,11 @@ func EnsureDB() {
        ensureKVLongPolling(session)
 }
 
-func OpenSession() *mgo.Session {
-       var timeout time.Duration
+func OpenSession(c *datasource.Config) *mgo.Session {
+       timeout := c.Timeout
        var uri string
        var err error
-       if config.GetDB().Timeout != "" {
-               timeout, err = time.ParseDuration(config.GetDB().Timeout)
-               if err != nil {
-                       openlog.Fatal("invalid timeout :" + err.Error())
-               }
-       }
-       uri = cipherutil.TryDecrypt(config.GetDB().URI)
+       uri = cipherutil.TryDecrypt(c.URI)
        session, err := mgo.DialWithTimeout(uri, timeout)
        if err != nil {
                openlog.Warn("can not dial db, retry once:" + err.Error())
diff --git a/server/service/mongo/session/session_test.go 
b/server/datasource/mongo/session/session_test.go
similarity index 72%
rename from server/service/mongo/session/session_test.go
rename to server/datasource/mongo/session/session_test.go
index 4eb864c..5abdaaa 100644
--- a/server/service/mongo/session/session_test.go
+++ b/server/datasource/mongo/session/session_test.go
@@ -2,18 +2,22 @@ package session_test
 
 import (
        "context"
-       "github.com/apache/servicecomb-kie/server/config"
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
+       "testing"
+       "time"
+
+       "github.com/apache/servicecomb-kie/server/datasource"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
        _ "github.com/apache/servicecomb-kie/test"
        "github.com/stretchr/testify/assert"
        "go.mongodb.org/mongo-driver/bson"
-       "testing"
 )
 
 func TestGetColInfo(t *testing.T) {
        var err error
-       config.Configurations = &config.Config{DB: config.DB{URI: 
"mongodb://kie:[email protected]:27017/kie"}}
-       err = session.Init()
+       err = session.Init(&datasource.Config{
+               URI:     "mongodb://kie:[email protected]:27017/kie",
+               Timeout: 10 * time.Second,
+       })
        assert.NoError(t, err)
        err = session.CreateView(context.Background(), "test_view", 
session.CollectionKV, []bson.D{
                {{
diff --git a/server/service/mongo/session/struct.go 
b/server/datasource/mongo/session/struct.go
similarity index 100%
rename from server/service/mongo/session/struct.go
rename to server/datasource/mongo/session/struct.go
diff --git a/server/service/mongo/track/polling_detail_dao.go 
b/server/datasource/mongo/track/polling_detail_dao.go
similarity index 94%
rename from server/service/mongo/track/polling_detail_dao.go
rename to server/datasource/mongo/track/polling_detail_dao.go
index 42bb812..ec427ed 100644
--- a/server/service/mongo/track/polling_detail_dao.go
+++ b/server/datasource/mongo/track/polling_detail_dao.go
@@ -19,9 +19,10 @@ package track
 
 import (
        "context"
+
        "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/server/service"
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
+       "github.com/apache/servicecomb-kie/server/datasource"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
        "github.com/go-chassis/openlog"
        uuid "github.com/satori/go.uuid"
        "go.mongodb.org/mongo-driver/bson"
@@ -90,7 +91,7 @@ func Get(ctx context.Context, detail *model.PollingDetail) 
([]*model.PollingDeta
                records = append(records, curRecord)
        }
        if len(records) == 0 {
-               return nil, service.ErrRecordNotExists
+               return nil, datasource.ErrRecordNotExists
        }
        return records, nil
 }
diff --git a/server/service/mongo/track/polling_detail_service.go 
b/server/datasource/mongo/track/polling_detail_service.go
similarity index 99%
rename from server/service/mongo/track/polling_detail_service.go
rename to server/datasource/mongo/track/polling_detail_service.go
index 0386a60..dae5803 100644
--- a/server/service/mongo/track/polling_detail_service.go
+++ b/server/datasource/mongo/track/polling_detail_service.go
@@ -19,6 +19,7 @@ package track
 
 import (
        "context"
+
        "github.com/apache/servicecomb-kie/pkg/model"
 )
 
diff --git a/server/service/mongo/view/view_dao.go 
b/server/datasource/mongo/view/view_dao.go
similarity index 96%
rename from server/service/mongo/view/view_dao.go
rename to server/datasource/mongo/view/view_dao.go
index ac11ae7..14acd75 100644
--- a/server/service/mongo/view/view_dao.go
+++ b/server/datasource/mongo/view/view_dao.go
@@ -19,8 +19,9 @@ package view
 
 import (
        "context"
+
        "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
        "github.com/go-chassis/openlog"
        uuid "github.com/satori/go.uuid"
        "go.mongodb.org/mongo-driver/bson"
diff --git a/server/service/mongo/view/view_service.go 
b/server/datasource/mongo/view/view_service.go
similarity index 92%
rename from server/service/mongo/view/view_service.go
rename to server/datasource/mongo/view/view_service.go
index 049e7ff..bbfb704 100644
--- a/server/service/mongo/view/view_service.go
+++ b/server/datasource/mongo/view/view_service.go
@@ -19,9 +19,10 @@ package view
 
 import (
        "context"
+
        "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/server/service"
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
+       "github.com/apache/servicecomb-kie/server/datasource"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
        "github.com/go-chassis/openlog"
        uuid "github.com/satori/go.uuid"
        "go.mongodb.org/mongo-driver/bson"
@@ -34,7 +35,7 @@ type Service struct {
 }
 
 //Create insert a view data and create a mongo db view
-func (s *Service) Create(ctx context.Context, viewDoc *model.ViewDoc, options 
...service.FindOption) (*model.ViewDoc, error) {
+func (s *Service) Create(ctx context.Context, viewDoc *model.ViewDoc, options 
...datasource.FindOption) (*model.ViewDoc, error) {
        if viewDoc.Domain == "" {
                return nil, session.ErrMissingDomain
        }
@@ -47,7 +48,7 @@ func (s *Service) Create(ctx context.Context, viewDoc 
*model.ViewDoc, options ..
                        },
                }},
        }
-       opts := service.FindOptions{}
+       opts := datasource.FindOptions{}
        for _, o := range options {
                o(&opts)
        }
@@ -113,8 +114,8 @@ func (s *Service) Update(ctx context.Context, viewDoc 
*model.ViewDoc) error {
 }
 
 //List return all view user created
-func (s *Service) List(ctx context.Context, domain, project string, opts 
...service.FindOption) (*model.ViewResponse, error) {
-       option := service.FindOptions{}
+func (s *Service) List(ctx context.Context, domain, project string, opts 
...datasource.FindOption) (*model.ViewResponse, error) {
+       option := datasource.FindOptions{}
        for _, o := range opts {
                o(&option)
        }
@@ -143,8 +144,8 @@ func (s *Service) List(ctx context.Context, domain, project 
string, opts ...serv
 }
 
 //GetContent query view's kv data
-func (s *Service) GetContent(ctx context.Context, id, domain, project string, 
opts ...service.FindOption) (*model.KVResponse, error) {
-       option := service.FindOptions{}
+func (s *Service) GetContent(ctx context.Context, id, domain, project string, 
opts ...datasource.FindOption) (*model.KVResponse, error) {
+       option := datasource.FindOptions{}
        for _, o := range opts {
                o(&option)
        }
diff --git a/server/service/mongo/view/view_service_test.go 
b/server/datasource/mongo/view/view_service_test.go
similarity index 87%
rename from server/service/mongo/view/view_service_test.go
rename to server/datasource/mongo/view/view_service_test.go
index 81ab596..d3405e0 100644
--- a/server/service/mongo/view/view_service_test.go
+++ b/server/datasource/mongo/view/view_service_test.go
@@ -20,24 +20,27 @@ package view_test
 import (
        "context"
        "encoding/json"
+       "testing"
+       "time"
+
        common2 "github.com/apache/servicecomb-kie/pkg/common"
        "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/server/config"
-       "github.com/apache/servicecomb-kie/server/service"
-       "github.com/apache/servicecomb-kie/server/service/mongo/kv"
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
-       "github.com/apache/servicecomb-kie/server/service/mongo/view"
+       "github.com/apache/servicecomb-kie/server/datasource"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/kv"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/view"
        _ "github.com/apache/servicecomb-kie/test"
        "github.com/stretchr/testify/assert"
        "go.mongodb.org/mongo-driver/bson"
        "go.mongodb.org/mongo-driver/mongo"
-       "testing"
 )
 
 func TestGet(t *testing.T) {
        var err error
-       config.Configurations = &config.Config{DB: config.DB{URI: 
"mongodb://kie:[email protected]:27017/kie"}}
-       err = session.Init()
+       err = session.Init(&datasource.Config{
+               URI:     "mongodb://kie:[email protected]:27017/kie",
+               Timeout: 10 * time.Second,
+       })
        assert.NoError(t, err)
        kvsvc := &kv.Service{}
        t.Run("put view data", func(t *testing.T) {
@@ -89,14 +92,14 @@ func TestGet(t *testing.T) {
                        Display: "timeout_config",
                        Project: "view_test",
                        Domain:  "default",
-               }, service.WithKey("timeout"))
+               }, datasource.WithKey("timeout"))
                assert.NoError(t, err)
                assert.NotEmpty(t, view1.ID)
                view2, err := svc.Create(context.TODO(), &model.ViewDoc{
                        Display: "mall_config",
                        Project: "view_test",
                        Domain:  "default",
-               }, service.WithLabels(map[string]string{
+               }, datasource.WithLabels(map[string]string{
                        "app": "mall",
                }))
                assert.NoError(t, err)
diff --git a/server/service/options.go b/server/datasource/options.go
similarity index 85%
rename from server/service/options.go
rename to server/datasource/options.go
index 1293691..60e53fa 100644
--- a/server/service/options.go
+++ b/server/datasource/options.go
@@ -15,17 +15,25 @@
  * limitations under the License.
  */
 
-package service
+package datasource
 
 import (
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
        "time"
 )
 
+type Config struct {
+       URI        string        `yaml:"uri"`
+       PoolSize   int           `yaml:"poolSize"`
+       SSLEnabled bool          `yaml:"sslEnabled"`
+       RootCA     string        `yaml:"rootCAFile"`
+       Timeout    time.Duration `yaml:"timeout"`
+       VerifyPeer bool          `yaml:"verifyPeer"`
+}
+
 //NewDefaultFindOpts return default options
 func NewDefaultFindOpts() FindOptions {
        return FindOptions{
-               Timeout: session.DefaultTimeout,
+               Timeout: DefaultTimeout,
        }
 }
 
@@ -96,20 +104,6 @@ func WithLabelFormat(label string) FindOption {
        }
 }
 
-//WithDepth if you use greedy match this can specify the match depth
-func WithDepth(d int) FindOption {
-       return func(o *FindOptions) {
-               o.Depth = d
-       }
-}
-
-//WithOutLabelField will clear all labels attributes in kv doc
-func WithOutLabelField() FindOption {
-       return func(o *FindOptions) {
-               o.ClearLabel = true
-       }
-}
-
 //WithLimit tells service paging limit
 func WithLimit(l int64) FindOption {
        return func(o *FindOptions) {
diff --git a/server/handler/track_handler.go b/server/handler/track_handler.go
index f633478..e4fd5ae 100644
--- a/server/handler/track_handler.go
+++ b/server/handler/track_handler.go
@@ -18,18 +18,19 @@
 package handler
 
 import (
+       v1 "github.com/apache/servicecomb-kie/server/resource/v1"
+       "net/http"
+       "strings"
+       "time"
+
        "github.com/apache/servicecomb-kie/pkg/common"
        "github.com/apache/servicecomb-kie/pkg/iputil"
        "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/server/resource/v1"
-       "github.com/apache/servicecomb-kie/server/service"
+       "github.com/apache/servicecomb-kie/server/datasource"
        "github.com/emicklei/go-restful"
        "github.com/go-chassis/go-chassis/v2/core/handler"
        "github.com/go-chassis/go-chassis/v2/core/invocation"
        "github.com/go-chassis/openlog"
-       "net/http"
-       "strings"
-       "time"
 )
 
 //const of noop auth handler
@@ -86,7 +87,7 @@ func (h *TrackHandler) Handle(chain *handler.Chain, inv 
*invocation.Invocation,
                        "wait":     wait,
                        "labels":   req.QueryParameter("label"),
                }
-               _, err := service.TrackService.CreateOrUpdate(inv.Ctx, data)
+               _, err := 
datasource.GetBroker().GetTrackDao().CreateOrUpdate(inv.Ctx, data)
                if err != nil {
                        openlog.Warn("record polling detail failed:" + 
err.Error())
                        cb(ir)
diff --git a/server/plugin/qms/quota.go b/server/plugin/qms/quota.go
index 639b22a..6d80c0a 100644
--- a/server/plugin/qms/quota.go
+++ b/server/plugin/qms/quota.go
@@ -19,7 +19,8 @@ package qms
 
 import (
        "context"
-       "github.com/apache/servicecomb-kie/server/service"
+
+       "github.com/apache/servicecomb-kie/server/datasource"
        "github.com/go-chassis/go-archaius"
        "github.com/go-chassis/go-chassis/v2/pkg/backends/quota"
        "github.com/go-chassis/openlog"
@@ -39,7 +40,7 @@ type BuildInManager struct {
 //GetQuotas get usage and quota
 func (m *BuildInManager) GetQuotas(serviceName, domain string) 
([]*quota.Quota, error) {
        max := archaius.GetInt64(QuotaConfigKey, DefaultQuota)
-       total, err := service.KVService.Total(context.TODO(), domain)
+       total, err := datasource.GetBroker().GetKVDao().Total(context.TODO(), 
domain)
        if err != nil {
                openlog.Error("find quotas failed: " + err.Error())
                return nil, err
diff --git a/server/pubsub/bus.go b/server/pubsub/bus.go
index a0f95c4..ab9925e 100644
--- a/server/pubsub/bus.go
+++ b/server/pubsub/bus.go
@@ -19,12 +19,13 @@ package pubsub
 
 import (
        "encoding/json"
+       "sync"
+
        "github.com/apache/servicecomb-kie/pkg/stringutil"
        "github.com/apache/servicecomb-kie/server/config"
        "github.com/go-chassis/openlog"
        "github.com/hashicorp/serf/cmd/serf/command/agent"
        "github.com/hashicorp/serf/serf"
-       "sync"
 )
 
 var once sync.Once
diff --git a/server/pubsub/bus_test.go b/server/pubsub/bus_test.go
index 8105e2a..809fe2b 100644
--- a/server/pubsub/bus_test.go
+++ b/server/pubsub/bus_test.go
@@ -18,10 +18,11 @@
 package pubsub_test
 
 import (
+       "testing"
+
        "github.com/apache/servicecomb-kie/server/config"
        "github.com/apache/servicecomb-kie/server/pubsub"
        uuid "github.com/satori/go.uuid"
-       "testing"
 )
 
 func TestInit(t *testing.T) {
diff --git a/server/pubsub/event_handler.go b/server/pubsub/event_handler.go
index 8b4f4e5..707bcce 100644
--- a/server/pubsub/event_handler.go
+++ b/server/pubsub/event_handler.go
@@ -18,9 +18,10 @@
 package pubsub
 
 import (
+       "strings"
+
        "github.com/go-chassis/openlog"
        "github.com/hashicorp/serf/serf"
-       "strings"
 )
 
 //EventHandler handler serf custom event
diff --git a/server/pubsub/struct_test.go b/server/pubsub/struct_test.go
index 6ce46f9..f3fb349 100644
--- a/server/pubsub/struct_test.go
+++ b/server/pubsub/struct_test.go
@@ -19,8 +19,9 @@ package pubsub_test
 
 import (
        "encoding/json"
-       "github.com/apache/servicecomb-kie/server/pubsub"
        "testing"
+
+       "github.com/apache/servicecomb-kie/server/pubsub"
 )
 
 func TestTopic_String(t *testing.T) {
diff --git a/server/rbac/rbac.go b/server/rbac/rbac.go
index 5cf7b90..659cfd9 100644
--- a/server/rbac/rbac.go
+++ b/server/rbac/rbac.go
@@ -18,6 +18,11 @@
 package rbac
 
 import (
+       "io/ioutil"
+       "net/http"
+       "path/filepath"
+       "strings"
+
        "github.com/apache/servicecomb-kie/server/config"
        "github.com/go-chassis/cari/rbac"
        "github.com/go-chassis/go-archaius"
@@ -25,10 +30,6 @@ import (
        "github.com/go-chassis/go-chassis/v2/security/secret"
        "github.com/go-chassis/go-chassis/v2/security/token"
        "github.com/go-chassis/openlog"
-       "io/ioutil"
-       "net/http"
-       "path/filepath"
-       "strings"
 )
 
 const (
diff --git a/server/resource/v1/admin_resource.go 
b/server/resource/v1/admin_resource.go
index d359044..6a8a6e0 100644
--- a/server/resource/v1/admin_resource.go
+++ b/server/resource/v1/admin_resource.go
@@ -18,16 +18,17 @@
 package v1
 
 import (
+       "net/http"
+       "strconv"
+       "time"
+
        "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/server/service"
+       "github.com/apache/servicecomb-kie/server/datasource"
        goRestful "github.com/emicklei/go-restful"
        "github.com/go-chassis/cari/config"
        "github.com/go-chassis/go-chassis/v2/pkg/runtime"
        "github.com/go-chassis/go-chassis/v2/server/restful"
        "github.com/go-chassis/openlog"
-       "net/http"
-       "strconv"
-       "time"
 )
 
 type AdminResource struct {
@@ -58,7 +59,7 @@ func (r *AdminResource) URLPatterns() []restful.Route {
 func (r *AdminResource) HealthCheck(context *restful.Context) {
        domain := ReadDomain(context.Ctx)
        resp := &model.DocHealthCheck{}
-       latest, err := service.RevisionService.GetRevision(context.Ctx, domain)
+       latest, err := 
datasource.GetBroker().GetRevisionDao().GetRevision(context.Ctx, domain)
        if err != nil {
                WriteErrResponse(context, config.ErrInternal, err.Error())
                return
@@ -66,7 +67,7 @@ func (r *AdminResource) HealthCheck(context *restful.Context) 
{
        resp.Revision = strconv.FormatInt(latest, 10)
        resp.Version = runtime.Version
        resp.Timestamp = time.Now().Unix()
-       total, err := service.KVService.Total(context.Ctx, domain)
+       total, err := datasource.GetBroker().GetKVDao().Total(context.Ctx, 
domain)
        if err != nil {
                WriteErrResponse(context, config.ErrInternal, err.Error())
                return
diff --git a/server/resource/v1/admin_resource_test.go 
b/server/resource/v1/admin_resource_test.go
index b132aed..895310d 100644
--- a/server/resource/v1/admin_resource_test.go
+++ b/server/resource/v1/admin_resource_test.go
@@ -20,14 +20,15 @@ package v1_test
 import (
        "encoding/json"
        "fmt"
-       "github.com/apache/servicecomb-kie/pkg/model"
-       v1 "github.com/apache/servicecomb-kie/server/resource/v1"
-       "github.com/go-chassis/go-chassis/v2/server/restful/restfultest"
-       "github.com/stretchr/testify/assert"
        "io/ioutil"
        "net/http"
        "net/http/httptest"
        "testing"
+
+       "github.com/apache/servicecomb-kie/pkg/model"
+       v1 "github.com/apache/servicecomb-kie/server/resource/v1"
+       "github.com/go-chassis/go-chassis/v2/server/restful/restfultest"
+       "github.com/stretchr/testify/assert"
 )
 
 func Test_HeathCheck(t *testing.T) {
diff --git a/server/resource/v1/common.go b/server/resource/v1/common.go
index 5383928..82a3009 100644
--- a/server/resource/v1/common.go
+++ b/server/resource/v1/common.go
@@ -21,18 +21,19 @@ import (
        "context"
        "encoding/json"
        "errors"
-       kvsvc "github.com/apache/servicecomb-kie/server/service/kv"
-       "github.com/go-chassis/cari/rbac"
        "net/http"
        "strconv"
        "strings"
        "time"
 
+       "github.com/apache/servicecomb-kie/server/datasource"
+       kvsvc "github.com/apache/servicecomb-kie/server/service/kv"
+       "github.com/go-chassis/cari/rbac"
+
        "github.com/apache/servicecomb-kie/pkg/common"
        "github.com/apache/servicecomb-kie/pkg/model"
+       "github.com/apache/servicecomb-kie/server/datasource/mongo/session"
        "github.com/apache/servicecomb-kie/server/pubsub"
-       "github.com/apache/servicecomb-kie/server/service"
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
        goRestful "github.com/emicklei/go-restful"
        "github.com/go-chassis/cari/config"
        "github.com/go-chassis/go-chassis/v2/server/restful"
@@ -160,7 +161,7 @@ func isRevised(ctx context.Context, revStr, domain string) 
(bool, error) {
        if err != nil {
                return false, ErrInvalidRev
        }
-       latest, err := service.RevisionService.GetRevision(ctx, domain)
+       latest, err := datasource.GetBroker().GetRevisionDao().GetRevision(ctx, 
domain)
        if err != nil {
                return false, err
        }
diff --git a/server/resource/v1/common_test.go 
b/server/resource/v1/common_test.go
index e57b805..502e9e0 100644
--- a/server/resource/v1/common_test.go
+++ b/server/resource/v1/common_test.go
@@ -18,11 +18,12 @@
 package v1_test
 
 import (
+       "net/http"
+       "testing"
+
        v1 "github.com/apache/servicecomb-kie/server/resource/v1"
        "github.com/emicklei/go-restful"
        "github.com/stretchr/testify/assert"
-       "net/http"
-       "testing"
 )
 
 func TestGetLabels(t *testing.T) {
diff --git a/server/resource/v1/history_resource.go 
b/server/resource/v1/history_resource.go
index dbfbf80..89d9cea 100644
--- a/server/resource/v1/history_resource.go
+++ b/server/resource/v1/history_resource.go
@@ -18,11 +18,12 @@
 package v1
 
 import (
+       "net/http"
+
        "github.com/apache/servicecomb-kie/pkg/common"
        "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/server/service"
+       "github.com/apache/servicecomb-kie/server/datasource"
        "github.com/go-chassis/cari/config"
-       "net/http"
 
        goRestful "github.com/emicklei/go-restful"
        "github.com/go-chassis/go-chassis/v2/server/restful"
@@ -49,11 +50,11 @@ func (r *HistoryResource) GetRevisions(context 
*restful.Context) {
                WriteErrResponse(context, config.ErrRequiredRecordId, "kv_id 
must not be empty")
                return
        }
-       revisions, err := service.HistoryService.GetHistory(context.Ctx, kvID,
-               service.WithOffset(offset),
-               service.WithLimit(limit))
+       revisions, err := 
datasource.GetBroker().GetHistoryDao().GetHistory(context.Ctx, kvID,
+               datasource.WithOffset(offset),
+               datasource.WithLimit(limit))
        if err != nil {
-               if err == service.ErrRevisionNotExist {
+               if err == datasource.ErrRevisionNotExist {
                        WriteErrResponse(context, config.ErrRecordNotExists, 
err.Error())
                        return
                }
@@ -99,9 +100,9 @@ func (r *HistoryResource) GetPollingData(context 
*restful.Context) {
                return
        }
        query.Domain = domain
-       records, err := service.TrackService.GetPollingDetail(context.Ctx, 
query)
+       records, err := 
datasource.GetBroker().GetTrackDao().GetPollingDetail(context.Ctx, query)
        if err != nil {
-               if err == service.ErrRecordNotExists {
+               if err == datasource.ErrRecordNotExists {
                        WriteErrResponse(context, config.ErrRecordNotExists, 
err.Error())
                        return
                }
diff --git a/server/resource/v1/history_resource_test.go 
b/server/resource/v1/history_resource_test.go
index ce8a863..deb7412 100644
--- a/server/resource/v1/history_resource_test.go
+++ b/server/resource/v1/history_resource_test.go
@@ -20,22 +20,23 @@ import (
        "context"
        "encoding/json"
        "fmt"
-       common2 "github.com/apache/servicecomb-kie/pkg/common"
        "io/ioutil"
        "net/http"
        "net/http/httptest"
        "testing"
 
+       common2 "github.com/apache/servicecomb-kie/pkg/common"
+       "github.com/apache/servicecomb-kie/server/datasource"
+
        "github.com/apache/servicecomb-kie/pkg/model"
        handler2 "github.com/apache/servicecomb-kie/server/handler"
        v1 "github.com/apache/servicecomb-kie/server/resource/v1"
-       "github.com/apache/servicecomb-kie/server/service"
        "github.com/go-chassis/go-chassis/v2/core/common"
        "github.com/go-chassis/go-chassis/v2/core/handler"
        "github.com/go-chassis/go-chassis/v2/server/restful/restfultest"
        "github.com/stretchr/testify/assert"
 
-       _ "github.com/apache/servicecomb-kie/server/service/mongo"
+       _ "github.com/apache/servicecomb-kie/server/datasource/mongo"
 )
 
 func TestHistoryResource_GetRevisions(t *testing.T) {
@@ -49,7 +50,7 @@ func TestHistoryResource_GetRevisions(t *testing.T) {
                Domain:  "default",
                Project: "history_test",
        }
-       kv, err := service.KVService.Create(context.Background(), kv)
+       kv, err := 
datasource.GetBroker().GetKVDao().Create(context.Background(), kv)
        assert.NoError(t, err)
        path := fmt.Sprintf("/v1/history_test/kie/revision/%s", kv.ID)
        r, _ := http.NewRequest("GET", path, nil)
@@ -75,7 +76,7 @@ func TestHistoryResource_GetRevisions(t *testing.T) {
                        Project: "history_test",
                        Status:  kv.Status,
                }
-               kv, err = service.KVService.Update(context.Background(), 
updateKv)
+               kv, err = 
datasource.GetBroker().GetKVDao().Update(context.Background(), updateKv)
                assert.NoError(t, err)
                path := fmt.Sprintf("/v1/history_test/kie/revision/%s", kv.ID)
                r, _ := http.NewRequest("GET", path, nil)
diff --git a/server/resource/v1/kv_resource.go 
b/server/resource/v1/kv_resource.go
index 275e6f5..e3f0d92 100644
--- a/server/resource/v1/kv_resource.go
+++ b/server/resource/v1/kv_resource.go
@@ -21,13 +21,14 @@ package v1
 import (
        "encoding/json"
        "fmt"
-       kvsvc "github.com/apache/servicecomb-kie/server/service/kv"
        "net/http"
 
+       "github.com/apache/servicecomb-kie/server/datasource"
+       kvsvc "github.com/apache/servicecomb-kie/server/service/kv"
+
        "github.com/apache/servicecomb-kie/pkg/common"
        "github.com/apache/servicecomb-kie/pkg/model"
        "github.com/apache/servicecomb-kie/server/pubsub"
-       "github.com/apache/servicecomb-kie/server/service"
        goRestful "github.com/emicklei/go-restful"
        "github.com/go-chassis/cari/config"
        "github.com/go-chassis/foundation/validator"
@@ -104,7 +105,7 @@ func (r *KVResource) Put(rctx *restful.Context) {
                WriteErrResponse(rctx, config.ErrInvalidParams, err.Error())
                return
        }
-       kv, err := service.KVService.Update(rctx.Ctx, kvReq)
+       kv, err := datasource.GetBroker().GetKVDao().Update(rctx.Ctx, kvReq)
        if err != nil {
                openlog.Error(fmt.Sprintf("put [%s] err:%s", kvID, err.Error()))
                WriteErrResponse(rctx, config.ErrInternal, "update kv failed")
@@ -141,10 +142,10 @@ func (r *KVResource) Get(rctx *restful.Context) {
                WriteErrResponse(rctx, config.ErrInvalidParams, err.Error())
                return
        }
-       kv, err := service.KVService.Get(rctx.Ctx, request)
+       kv, err := datasource.GetBroker().GetKVDao().Get(rctx.Ctx, request)
        if err != nil {
                openlog.Error("kv_resource: " + err.Error())
-               if err == service.ErrKeyNotExists {
+               if err == datasource.ErrKeyNotExists {
                        WriteErrResponse(rctx, config.ErrRecordNotExists, 
err.Error())
                        return
                }
@@ -261,13 +262,13 @@ func (r *KVResource) Delete(rctx *restful.Context) {
                WriteErrResponse(rctx, config.ErrInvalidParams, err.Error())
                return
        }
-       kv, err := service.KVService.FindOneAndDelete(rctx.Ctx, kvID, domain, 
project)
+       kv, err := datasource.GetBroker().GetKVDao().FindOneAndDelete(rctx.Ctx, 
kvID, domain, project)
        if err != nil {
                openlog.Error("delete failed, ", openlog.WithTags(openlog.Tags{
                        "kvID":  kvID,
                        "error": err.Error(),
                }))
-               if err == service.ErrKeyNotExists {
+               if err == datasource.ErrKeyNotExists {
                        WriteErrResponse(rctx, config.ErrRecordNotExists, 
err.Error())
                        return
                }
@@ -301,9 +302,9 @@ func (r *KVResource) DeleteList(rctx *restful.Context) {
                WriteErrResponse(rctx, config.ErrInvalidParams, err.Error())
                return
        }
-       kvs, err := service.KVService.FindManyAndDelete(rctx.Ctx, b.IDs, 
domain, project)
+       kvs, err := 
datasource.GetBroker().GetKVDao().FindManyAndDelete(rctx.Ctx, b.IDs, domain, 
project)
        if err != nil {
-               if err == service.ErrKeyNotExists {
+               if err == datasource.ErrKeyNotExists {
                        rctx.WriteHeader(http.StatusNoContent)
                        return
                }
diff --git a/server/resource/v1/kv_resource_test.go 
b/server/resource/v1/kv_resource_test.go
index 1d7c1b3..2562ee3 100644
--- a/server/resource/v1/kv_resource_test.go
+++ b/server/resource/v1/kv_resource_test.go
@@ -20,29 +20,29 @@ package v1_test
 import (
        "bytes"
        "encoding/json"
+       "io/ioutil"
+       "net/http"
+       "net/http/httptest"
+       "strings"
+       "sync"
+       "testing"
+       "time"
+
        common2 "github.com/apache/servicecomb-kie/pkg/common"
        "github.com/apache/servicecomb-kie/pkg/model"
        "github.com/apache/servicecomb-kie/server/config"
        "github.com/apache/servicecomb-kie/server/plugin/qms"
        "github.com/apache/servicecomb-kie/server/pubsub"
        v1 "github.com/apache/servicecomb-kie/server/resource/v1"
-       "github.com/apache/servicecomb-kie/server/service"
        "github.com/go-chassis/go-archaius"
        "github.com/go-chassis/go-chassis/v2/pkg/backends/quota"
        "github.com/go-chassis/go-chassis/v2/server/restful/restfultest"
        "github.com/go-chassis/openlog"
        log "github.com/go-chassis/seclog"
        "github.com/stretchr/testify/assert"
-       "io/ioutil"
-       "net/http"
-       "net/http/httptest"
-       "strings"
-       "sync"
-       "testing"
-       "time"
 
+       _ "github.com/apache/servicecomb-kie/server/datasource/mongo"
        _ "github.com/apache/servicecomb-kie/server/plugin/qms"
-       _ "github.com/apache/servicecomb-kie/server/service/mongo"
        _ "github.com/apache/servicecomb-kie/test"
 )
 
@@ -60,15 +60,11 @@ func init() {
                ListenPeerAddr: "127.0.0.1:4000",
                AdvertiseAddr:  "127.0.0.1:4000",
        }
-       config.Configurations.DB.URI = "mongodb://kie:[email protected]:27017/kie"
-       err := service.DBInit()
-       if err != nil {
-               panic(err)
-       }
+
        pubsub.Init()
        pubsub.Start()
 
-       err = quota.Init(quota.Options{
+       err := quota.Init(quota.Options{
                Plugin: "build-in",
        })
        if err != nil {
diff --git a/server/server.go b/server/server.go
index 93215ab..c18167b 100644
--- a/server/server.go
+++ b/server/server.go
@@ -20,10 +20,10 @@ package server
 import (
        "github.com/apache/servicecomb-kie/pkg/validator"
        "github.com/apache/servicecomb-kie/server/config"
+       "github.com/apache/servicecomb-kie/server/datasource"
        "github.com/apache/servicecomb-kie/server/pubsub"
        "github.com/apache/servicecomb-kie/server/rbac"
        v1 "github.com/apache/servicecomb-kie/server/resource/v1"
-       "github.com/apache/servicecomb-kie/server/service"
        "github.com/go-chassis/go-chassis/v2"
        "github.com/go-chassis/go-chassis/v2/core/common"
        "github.com/go-chassis/openlog"
@@ -39,7 +39,7 @@ func Run() {
        if err := config.Init(); err != nil {
                openlog.Fatal(err.Error())
        }
-       if err := service.DBInit(); err != nil {
+       if err := datasource.Init(config.GetDB()); err != nil {
                openlog.Fatal(err.Error())
        }
        if err := validator.Init(); err != nil {
diff --git a/server/service/kv/kv.go b/server/service/kv/kv.go
index 2c80e6e..e821229 100644
--- a/server/service/kv/kv.go
+++ b/server/service/kv/kv.go
@@ -20,11 +20,12 @@ package kv
 import (
        "context"
        "fmt"
+
+       "github.com/apache/servicecomb-kie/server/datasource"
+
        "github.com/apache/servicecomb-kie/pkg/common"
        "github.com/apache/servicecomb-kie/pkg/model"
        "github.com/apache/servicecomb-kie/server/pubsub"
-       "github.com/apache/servicecomb-kie/server/service"
-       "github.com/apache/servicecomb-kie/server/service/mongo/session"
        "github.com/go-chassis/cari/config"
        "github.com/go-chassis/cari/pkg/errsvc"
        "github.com/go-chassis/foundation/validator"
@@ -33,24 +34,24 @@ import (
 )
 
 func ListKV(ctx context.Context, request *model.ListKVRequest) (int64, 
*model.KVResponse, *errsvc.Error) {
-       opts := []service.FindOption{
-               service.WithKey(request.Key),
-               service.WithLabels(request.Labels),
-               service.WithOffset(request.Offset),
-               service.WithLimit(request.Limit),
+       opts := []datasource.FindOption{
+               datasource.WithKey(request.Key),
+               datasource.WithLabels(request.Labels),
+               datasource.WithOffset(request.Offset),
+               datasource.WithLimit(request.Limit),
        }
        m := request.Match
        if m == common.PatternExact {
-               opts = append(opts, service.WithExactLabels())
+               opts = append(opts, datasource.WithExactLabels())
        }
        if request.Status != "" {
-               opts = append(opts, service.WithStatus(request.Status))
+               opts = append(opts, datasource.WithStatus(request.Status))
        }
-       rev, err := service.RevisionService.GetRevision(ctx, request.Domain)
+       rev, err := datasource.GetBroker().GetRevisionDao().GetRevision(ctx, 
request.Domain)
        if err != nil {
                return rev, nil, config.NewError(config.ErrInternal, 
err.Error())
        }
-       kv, err := service.KVService.List(ctx, request.Domain, request.Project, 
opts...)
+       kv, err := datasource.GetBroker().GetKVDao().List(ctx, request.Domain, 
request.Project, opts...)
        if err != nil {
                openlog.Error("common: " + err.Error())
                return rev, nil, config.NewError(config.ErrInternal, 
common.MsgDBError)
@@ -75,10 +76,10 @@ func Post(ctx context.Context, kv *model.KVDoc) 
(*model.KVDoc, *errsvc.Error) {
                openlog.Error(err.Error())
                return nil, config.NewError(config.ErrInternal, "quota check 
failed")
        }
-       kv, err = service.KVService.Create(ctx, kv)
+       kv, err = datasource.GetBroker().GetKVDao().Create(ctx, kv)
        if err != nil {
                openlog.Error(fmt.Sprintf("post err:%s", err.Error()))
-               if err == session.ErrKVAlreadyExists {
+               if err == datasource.ErrKVAlreadyExists {
                        return nil, 
config.NewError(config.ErrRecordAlreadyExists, err.Error())
                }
                return nil, config.NewError(config.ErrInternal, "create kv 
failed")
diff --git a/server/service/kv/override.go b/server/service/kv/override.go
index 9f4af1a..76f6560 100644
--- a/server/service/kv/override.go
+++ b/server/service/kv/override.go
@@ -19,6 +19,7 @@ package kv
 
 import (
        "context"
+
        "github.com/apache/servicecomb-kie/pkg/model"
        "github.com/go-chassis/cari/pkg/errsvc"
 )
diff --git a/server/service/kv/override_abort.go 
b/server/service/kv/override_abort.go
index 9b1b236..a17caac 100644
--- a/server/service/kv/override_abort.go
+++ b/server/service/kv/override_abort.go
@@ -20,6 +20,7 @@ package kv
 import (
        "context"
        "fmt"
+
        "github.com/apache/servicecomb-kie/pkg/model"
        "github.com/go-chassis/cari/config"
        "github.com/go-chassis/cari/pkg/errsvc"
diff --git a/server/service/kv/override_force.go 
b/server/service/kv/override_force.go
index e188430..a3f290f 100644
--- a/server/service/kv/override_force.go
+++ b/server/service/kv/override_force.go
@@ -20,8 +20,9 @@ package kv
 import (
        "context"
        "fmt"
+
        "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/server/service"
+       "github.com/apache/servicecomb-kie/server/datasource"
        "github.com/go-chassis/cari/config"
        "github.com/go-chassis/cari/pkg/errsvc"
        "github.com/go-chassis/openlog"
@@ -62,7 +63,7 @@ func (f *Force) Execute(ctx context.Context, kv *model.KVDoc) 
(*model.KVDoc, *er
                Project: input.Project,
                Domain:  input.Domain,
        }
-       kv, updateErr := service.KVService.Update(ctx, kvReq)
+       kv, updateErr := datasource.GetBroker().GetKVDao().Update(ctx, kvReq)
        if updateErr != nil {
                openlog.Error(fmt.Sprintf("update record [key: %s, labels: %s] 
failed", input.Key, input.Labels))
                return input, config.NewError(config.ErrInternal, 
updateErr.Error())
diff --git a/server/service/kv/override_skip.go 
b/server/service/kv/override_skip.go
index 7031e0a..0516bb8 100644
--- a/server/service/kv/override_skip.go
+++ b/server/service/kv/override_skip.go
@@ -20,6 +20,7 @@ package kv
 import (
        "context"
        "fmt"
+
        "github.com/apache/servicecomb-kie/pkg/model"
        "github.com/go-chassis/cari/config"
        "github.com/go-chassis/cari/pkg/errsvc"
diff --git a/test/init.go b/test/init.go
index 397c104..afe24ff 100644
--- a/test/init.go
+++ b/test/init.go
@@ -19,9 +19,13 @@ package test
 
 import (
        "github.com/apache/servicecomb-kie/pkg/validator"
+       "github.com/apache/servicecomb-kie/server/config"
+       "github.com/apache/servicecomb-kie/server/datasource"
        "github.com/go-chassis/go-archaius"
        "github.com/go-chassis/go-chassis/v2/security/cipher"
        _ "github.com/go-chassis/go-chassis/v2/security/cipher/plugins/plain"
+
+       _ "github.com/apache/servicecomb-kie/server/datasource/mongo"
 )
 
 func init() {
@@ -29,4 +33,11 @@ func init() {
        archaius.Set("servicecomb.cipher.plugin", "default")
        cipher.Init()
        validator.Init()
+       err := datasource.Init(config.DB{
+               URI:     "mongodb://kie:[email protected]:27017/kie",
+               Timeout: "10s",
+       })
+       if err != nil {
+               panic(err)
+       }
 }

Reply via email to