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-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new a2d0f3f  SCB-2094 Add system management APIs (#724)
a2d0f3f is described below

commit a2d0f3f139245771d7fd8d22355815edca27b587
Author: little-cui <[email protected]>
AuthorDate: Wed Oct 21 11:50:55 2020 +0800

    SCB-2094 Add system management APIs (#724)
---
 datasource/datasource.go                |  1 +
 datasource/etcd/system.go               | 52 +++++++++++++++++++++++++++++++++
 datasource/etcd/system_test.go          | 52 +++++++++++++++++++++++++++++++++
 datasource/{datasource.go => system.go} | 13 +++++----
 4 files changed, 113 insertions(+), 5 deletions(-)

diff --git a/datasource/datasource.go b/datasource/datasource.go
index bf9254b..4b76556 100644
--- a/datasource/datasource.go
+++ b/datasource/datasource.go
@@ -19,6 +19,7 @@ package datasource
 
 // DataSource is the DAO layer
 type DataSource interface {
+       SystemManager
        AccountManager
        DependencyManager
        MetadataManager
diff --git a/datasource/etcd/system.go b/datasource/etcd/system.go
new file mode 100644
index 0000000..0116c5d
--- /dev/null
+++ b/datasource/etcd/system.go
@@ -0,0 +1,52 @@
+/*
+ * 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 etcd
+
+import (
+       "context"
+       "github.com/apache/servicecomb-service-center/pkg/gopool"
+       "github.com/apache/servicecomb-service-center/pkg/model"
+       "github.com/apache/servicecomb-service-center/server/core/backend"
+       "github.com/apache/servicecomb-service-center/server/plugin/discovery"
+)
+
+func (ds *DataSource) DumpCache(ctx context.Context, cache *model.Cache) {
+       gopool.New(ctx, gopool.Configure().Workers(2)).
+               Do(func(_ context.Context) { 
setValue(backend.Store().Service(), &cache.Microservices) }).
+               Do(func(_ context.Context) { 
setValue(backend.Store().ServiceIndex(), &cache.Indexes) }).
+               Do(func(_ context.Context) { 
setValue(backend.Store().ServiceAlias(), &cache.Aliases) }).
+               Do(func(_ context.Context) { 
setValue(backend.Store().ServiceTag(), &cache.Tags) }).
+               Do(func(_ context.Context) { 
setValue(backend.Store().RuleIndex(), &cache.RuleIndexes) }).
+               Do(func(_ context.Context) { setValue(backend.Store().Rule(), 
&cache.Rules) }).
+               Do(func(_ context.Context) { 
setValue(backend.Store().DependencyRule(), &cache.DependencyRules) }).
+               Do(func(_ context.Context) { 
setValue(backend.Store().SchemaSummary(), &cache.Summaries) }).
+               Do(func(_ context.Context) { 
setValue(backend.Store().Instance(), &cache.Instances) }).
+               Done()
+}
+
+func setValue(e discovery.Adaptor, setter model.Setter) {
+       e.Cache().ForEach(func(k string, kv *discovery.KeyValue) (next bool) {
+               setter.SetValue(&model.KV{
+                       Key:         k,
+                       Rev:         kv.ModRevision,
+                       Value:       kv.Value,
+                       ClusterName: kv.ClusterName,
+               })
+               return true
+       })
+}
diff --git a/datasource/etcd/system_test.go b/datasource/etcd/system_test.go
new file mode 100644
index 0000000..cb437cd
--- /dev/null
+++ b/datasource/etcd/system_test.go
@@ -0,0 +1,52 @@
+/*
+ * 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 etcd
+
+import (
+       "github.com/apache/servicecomb-service-center/datasource"
+       "github.com/apache/servicecomb-service-center/pkg/model"
+       mgr "github.com/apache/servicecomb-service-center/server/plugin"
+       
"github.com/apache/servicecomb-service-center/server/plugin/discovery/etcd"
+       etcd2 
"github.com/apache/servicecomb-service-center/server/plugin/registry/etcd"
+       "github.com/astaxie/beego"
+       "github.com/stretchr/testify/assert"
+       "testing"
+)
+
+func init() {
+       beego.AppConfig.Set("registry_plugin", "etcd")
+       mgr.RegisterPlugin(mgr.Plugin{mgr.REGISTRY, "etcd", etcd2.NewRegistry})
+       mgr.RegisterPlugin(mgr.Plugin{mgr.DISCOVERY, "buildin", 
etcd.NewRepository})
+       mgr.RegisterPlugin(mgr.Plugin{mgr.DISCOVERY, "etcd", 
etcd.NewRepository})
+       datasource.Install("etcd", func(opts datasource.Options) 
(datasource.DataSource, error) {
+               return NewDataSource(opts), nil
+       })
+       err := datasource.Init(datasource.Options{
+               Endpoint:       "",
+               PluginImplName: "etcd",
+       })
+       if err != nil {
+               panic("failed to register etcd auth plugin")
+       }
+}
+
+func TestAdminService_Dump(t *testing.T) {
+       t.Log("execute 'dump' operation,when get all,should be passed")
+       var cache model.Cache
+       datasource.Instance().DumpCache(getContext(), &cache)
+       assert.Equal(t, len(cache.Indexes), len(cache.Microservices))
+}
diff --git a/datasource/datasource.go b/datasource/system.go
similarity index 78%
copy from datasource/datasource.go
copy to datasource/system.go
index bf9254b..541b225 100644
--- a/datasource/datasource.go
+++ b/datasource/system.go
@@ -17,9 +17,12 @@
 
 package datasource
 
-// DataSource is the DAO layer
-type DataSource interface {
-       AccountManager
-       DependencyManager
-       MetadataManager
+import (
+       "context"
+       "github.com/apache/servicecomb-service-center/pkg/model"
+)
+
+// SystemManager contains the APIs of system management
+type SystemManager interface {
+       DumpCache(ctx context.Context, cache *model.Cache)
 }

Reply via email to