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 2021/12/16 01:53:21 UTC

[servicecomb-service-center] branch master updated: Feature: Add schema ref (#1180)

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 c7915ad  Feature: Add schema ref (#1180)
c7915ad is described below

commit c7915adad51c9f7dd067c84d4f670494d43a5a27
Author: little-cui <su...@qq.com>
AuthorDate: Thu Dec 16 09:53:12 2021 +0800

    Feature: Add schema ref (#1180)
---
 datasource/etcd/schema.go                 | 68 ++++++++++++++++++++++++++++++
 datasource/manager.go                     | 16 +++++++
 datasource/mongo/schema.go                | 66 +++++++++++++++++++++++++++++
 datasource/{manager.go => schema/init.go} | 47 +++++++--------------
 datasource/schema/options.go              | 23 +++++++++++
 datasource/schema/schema.go               | 69 +++++++++++++++++++++++++++++++
 etc/conf/app.yaml                         |  3 ++
 7 files changed, 259 insertions(+), 33 deletions(-)

diff --git a/datasource/etcd/schema.go b/datasource/etcd/schema.go
new file mode 100644
index 0000000..2aa7bc2
--- /dev/null
+++ b/datasource/etcd/schema.go
@@ -0,0 +1,68 @@
+/*
+ * 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/datasource/schema"
+)
+
+func init() {
+	schema.Install("etcd", NewSchemaDAO)
+	schema.Install("embeded_etcd", NewSchemaDAO)
+	schema.Install("embedded_etcd", NewSchemaDAO)
+}
+
+func NewSchemaDAO(opts schema.Options) (schema.DAO, error) {
+	return &SchemaDAO{}, nil
+}
+
+type SchemaDAO struct{}
+
+func (SchemaDAO) GetRef(ctx context.Context, ref *schema.Ref) (*schema.Ref, error) {
+	panic("implement me")
+}
+
+func (SchemaDAO) PutRef(ctx context.Context, ref *schema.Ref) error {
+	panic("implement me")
+}
+
+func (SchemaDAO) DeleteRef(ctx context.Context, ref ...*schema.Ref) error {
+	panic("implement me")
+}
+
+func (SchemaDAO) GetContent(ctx context.Context, hash *schema.ContentRequest) (string, error) {
+	panic("implement me")
+}
+
+func (SchemaDAO) PutContent(ctx context.Context, content *schema.Content) error {
+	panic("implement me")
+}
+
+func (SchemaDAO) DeleteContent(ctx context.Context, hash ...*schema.ContentRequest) error {
+	panic("implement me")
+}
+
+func (SchemaDAO) ListHash(ctx context.Context) ([]*schema.Content, error) {
+	panic("implement me")
+}
+
+func (SchemaDAO) ExistRef(ctx context.Context, hash *schema.ContentRequest) (*schema.Ref, error) {
+	panic("implement me")
+}
diff --git a/datasource/manager.go b/datasource/manager.go
index 5e4d5d6..9ac3e5d 100644
--- a/datasource/manager.go
+++ b/datasource/manager.go
@@ -20,6 +20,7 @@ package datasource
 import (
 	"fmt"
 
+	"github.com/apache/servicecomb-service-center/datasource/schema"
 	"github.com/apache/servicecomb-service-center/pkg/log"
 )
 
@@ -42,6 +43,21 @@ func Init(opts Options) error {
 		return nil
 	}
 
+	err := initDatasource(opts)
+	if err != nil {
+		return err
+	}
+
+	err = schema.Init(schema.Options{
+		Kind: opts.Kind,
+	})
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func initDatasource(opts Options) error {
 	dataSourceEngine, ok := plugins[opts.Kind]
 	if !ok {
 		return fmt.Errorf("plugin implement not supported [%s]", opts.Kind)
diff --git a/datasource/mongo/schema.go b/datasource/mongo/schema.go
new file mode 100644
index 0000000..a9a4497
--- /dev/null
+++ b/datasource/mongo/schema.go
@@ -0,0 +1,66 @@
+/*
+ * 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 mongo
+
+import (
+	"context"
+
+	"github.com/apache/servicecomb-service-center/datasource/schema"
+)
+
+func init() {
+	schema.Install("mongo", NewSchemaDAO)
+}
+
+func NewSchemaDAO(opts schema.Options) (schema.DAO, error) {
+	return &SchemaDAO{}, nil
+}
+
+type SchemaDAO struct{}
+
+func (SchemaDAO) GetRef(ctx context.Context, ref *schema.Ref) (*schema.Ref, error) {
+	panic("implement me")
+}
+
+func (SchemaDAO) PutRef(ctx context.Context, ref *schema.Ref) error {
+	panic("implement me")
+}
+
+func (SchemaDAO) DeleteRef(ctx context.Context, ref ...*schema.Ref) error {
+	panic("implement me")
+}
+
+func (SchemaDAO) GetContent(ctx context.Context, hash *schema.ContentRequest) (string, error) {
+	panic("implement me")
+}
+
+func (SchemaDAO) PutContent(ctx context.Context, content *schema.Content) error {
+	panic("implement me")
+}
+
+func (SchemaDAO) DeleteContent(ctx context.Context, hash ...*schema.ContentRequest) error {
+	panic("implement me")
+}
+
+func (SchemaDAO) ListHash(ctx context.Context) ([]*schema.Content, error) {
+	panic("implement me")
+}
+
+func (SchemaDAO) ExistRef(ctx context.Context, hash *schema.ContentRequest) (*schema.Ref, error) {
+	panic("implement me")
+}
diff --git a/datasource/manager.go b/datasource/schema/init.go
similarity index 51%
copy from datasource/manager.go
copy to datasource/schema/init.go
index 5e4d5d6..2a6e6d1 100644
--- a/datasource/manager.go
+++ b/datasource/schema/init.go
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package datasource
+package schema
 
 import (
 	"fmt"
@@ -23,59 +23,40 @@ import (
 	"github.com/apache/servicecomb-service-center/pkg/log"
 )
 
-type dataSourceEngine func(opts Options) (DataSource, error)
+type engine func(opts Options) (DAO, error)
 
 var (
-	plugins        = make(map[string]dataSourceEngine)
-	dataSourceInst DataSource
+	plugins  = make(map[string]engine)
+	instance DAO
 )
 
-// load plugins configuration into plugins
-func Install(pluginImplName string, engineFunc dataSourceEngine) {
+// Install load plugins configuration into plugins
+func Install(pluginImplName string, engineFunc engine) {
 	plugins[pluginImplName] = engineFunc
 }
 
 // Init construct storage plugin instance
-// invoked by sc main process
+// invoked by sc main process.
 func Init(opts Options) error {
 	if opts.Kind == "" {
 		return nil
 	}
 
-	dataSourceEngine, ok := plugins[opts.Kind]
+	engineFunc, ok := plugins[opts.Kind]
 	if !ok {
 		return fmt.Errorf("plugin implement not supported [%s]", opts.Kind)
 	}
+
 	var err error
-	dataSourceInst, err = dataSourceEngine(opts)
+	instance, err = engineFunc(opts)
 	if err != nil {
 		return err
 	}
-	log.Info(fmt.Sprintf("datasource plugin [%s] enabled", opts.Kind))
+	log.Info(fmt.Sprintf("schema plugin [%s] enabled", opts.Kind))
+
 	return nil
 }
 
-func GetSCManager() SCManager {
-	return dataSourceInst.SCManager()
-}
-func GetMetadataManager() MetadataManager {
-	return dataSourceInst.MetadataManager()
-}
-func GetSystemManager() SystemManager {
-	return dataSourceInst.SystemManager()
-}
-func GetRoleManager() RoleManager {
-	return dataSourceInst.RoleManager()
-}
-func GetAccountManager() AccountManager {
-	return dataSourceInst.AccountManager()
-}
-func GetAccountLockManager() AccountLockManager {
-	return dataSourceInst.AccountLockManager()
-}
-func GetDependencyManager() DependencyManager {
-	return dataSourceInst.DependencyManager()
-}
-func GetMetricsManager() MetricsManager {
-	return dataSourceInst.MetricsManager()
+func Instance() DAO {
+	return instance
 }
diff --git a/datasource/schema/options.go b/datasource/schema/options.go
new file mode 100644
index 0000000..fbdba26
--- /dev/null
+++ b/datasource/schema/options.go
@@ -0,0 +1,23 @@
+/*
+ * 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 schema
+
+//Options contains configuration for plugins
+type Options struct {
+	Kind string
+}
diff --git a/datasource/schema/schema.go b/datasource/schema/schema.go
new file mode 100644
index 0000000..7b0a924
--- /dev/null
+++ b/datasource/schema/schema.go
@@ -0,0 +1,69 @@
+/*
+ * 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 schema
+
+import (
+	"context"
+	"crypto/md5"
+	"encoding/hex"
+)
+
+type RefRequest struct {
+	Domain    string
+	Project   string
+	ServiceID string `json:"serviceId" bson:"service_id"`
+	SchemaID  string `json:"schemaId" bson:"schema_id"`
+}
+
+type Ref struct {
+	Domain    string
+	Project   string
+	ServiceID string `json:"serviceId" bson:"service_id"`
+	SchemaID  string `json:"schemaId" bson:"schema_id"`
+	Hash      string
+}
+
+type ContentRequest struct {
+	Domain  string
+	Project string
+	Hash    string
+}
+
+type Content struct {
+	Domain  string
+	Project string
+	Hash    string
+	Content string
+}
+
+type DAO interface {
+	GetRef(ctx context.Context, ref *Ref) (*Ref, error)
+	PutRef(ctx context.Context, ref *Ref) error
+	DeleteRef(ctx context.Context, ref ...*Ref) error
+	// GetContent get a schema content, hash is the result of MD5(schemaId+': '+content), see: Hash
+	GetContent(ctx context.Context, hash *ContentRequest) (string, error)
+	PutContent(ctx context.Context, content *Content) error
+	DeleteContent(ctx context.Context, hash ...*ContentRequest) error
+	// ListHash return Content list without content
+	ListHash(ctx context.Context) ([]*Content, error)
+	ExistRef(ctx context.Context, hash *ContentRequest) (*Ref, error)
+}
+
+func Hash(schemaID, content string) string {
+	return hex.EncodeToString(md5.New().Sum([]byte(schemaID + ": " + content)))
+}
diff --git a/etc/conf/app.yaml b/etc/conf/app.yaml
index 333086f..8d190e9 100644
--- a/etc/conf/app.yaml
+++ b/etc/conf/app.yaml
@@ -166,6 +166,9 @@ registry:
     disable: false
     # if want disable modification of Schema, SchemaNotEditable set true
     notEditable: false
+    # remove the schema without refs every 7d
+    retire:
+      interval: 7d
   # enable to register sc itself when startup
   selfRegister: 1