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 a83f0ee  SCB-2094 Implementation of Mongo heartbeat plugin (#742)
a83f0ee is described below

commit a83f0eef0d50db4278f03228a63af2e2c5ec60a5
Author: robotLJW <[email protected]>
AuthorDate: Mon Nov 16 15:53:55 2020 +0800

    SCB-2094 Implementation of Mongo heartbeat plugin (#742)
---
 datasource/mongo/heartbeat/healthcheck.go          | 27 ++++++++++
 .../heartbeat/heartbeatchecker/heartbeatchecker.go | 39 ++++++++++++++
 datasource/mongo/heartbeat/manager.go              | 62 ++++++++++++++++++++++
 datasource/mongo/heartbeat/manager_test.go         | 42 +++++++++++++++
 datasource/mongo/heartbeat/options.go              | 23 ++++++++
 datasource/mongo/heartbeat/types.go                | 20 +++++++
 datasource/mongo/ms.go                             |  3 +-
 7 files changed, 215 insertions(+), 1 deletion(-)

diff --git a/datasource/mongo/heartbeat/healthcheck.go 
b/datasource/mongo/heartbeat/healthcheck.go
new file mode 100644
index 0000000..c7ce92d
--- /dev/null
+++ b/datasource/mongo/heartbeat/healthcheck.go
@@ -0,0 +1,27 @@
+/*
+ * 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 heartbeat
+
+import (
+       "context"
+       pb "github.com/apache/servicecomb-service-center/pkg/registry"
+)
+
+type HealthCheck interface {
+       Heartbeat(ctx context.Context, request *pb.HeartbeatRequest) 
(*pb.HeartbeatResponse, error)
+}
diff --git a/datasource/mongo/heartbeat/heartbeatchecker/heartbeatchecker.go 
b/datasource/mongo/heartbeat/heartbeatchecker/heartbeatchecker.go
new file mode 100644
index 0000000..cefbf9b
--- /dev/null
+++ b/datasource/mongo/heartbeat/heartbeatchecker/heartbeatchecker.go
@@ -0,0 +1,39 @@
+/*
+ * 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 heartbeatchecker
+
+import (
+       "context"
+       
"github.com/apache/servicecomb-service-center/datasource/mongo/heartbeat"
+       pb "github.com/apache/servicecomb-service-center/pkg/registry"
+)
+
+func init() {
+       heartbeat.Install("heartbeatchecker", NewHeartBeatChecker)
+}
+
+type HeartBeatChecker struct {
+}
+
+func NewHeartBeatChecker(opts heartbeat.Options) (heartbeat.HealthCheck, 
error) {
+       return &HeartBeatChecker{}, nil
+}
+
+func (h *HeartBeatChecker) Heartbeat(ctx context.Context, request 
*pb.HeartbeatRequest) (*pb.HeartbeatResponse, error) {
+       return nil, nil
+}
diff --git a/datasource/mongo/heartbeat/manager.go 
b/datasource/mongo/heartbeat/manager.go
new file mode 100644
index 0000000..b32d260
--- /dev/null
+++ b/datasource/mongo/heartbeat/manager.go
@@ -0,0 +1,62 @@
+/*
+ * 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 heartbeat
+
+import (
+       "fmt"
+       "github.com/apache/servicecomb-service-center/pkg/log"
+)
+
+type healthCheckEngine func(opts Options) (HealthCheck, error)
+
+var (
+       plugins             = make(map[ImplName]healthCheckEngine)
+       healthCheckInstance HealthCheck
+)
+
+// load plugins configuration into plugins
+func Install(pluginImplName string, engineFunc healthCheckEngine) {
+       plugins[ImplName(pluginImplName)] = engineFunc
+}
+
+// construct plugin instance
+func Init(opts Options) error {
+       inst, err := New(opts)
+       if err != nil {
+               return err
+       }
+       healthCheckInstance = inst
+       log.Info(fmt.Sprintf("healthcheck plugin [%s] enabled", 
opts.PluginImplName))
+       return nil
+}
+
+func New(opts Options) (HealthCheck, error) {
+       if opts.PluginImplName == "" {
+               return nil, fmt.Errorf("plugin implement name is nil")
+       }
+       f, ok := plugins[opts.PluginImplName]
+       if !ok {
+               return nil, fmt.Errorf("plugin implement not supported 
[#{opts.PluginImplName}]")
+       }
+       return f(opts)
+}
+
+// Instance is the instance of HealthCheck
+func Instance() HealthCheck {
+       return healthCheckInstance
+}
diff --git a/datasource/mongo/heartbeat/manager_test.go 
b/datasource/mongo/heartbeat/manager_test.go
new file mode 100644
index 0000000..607c3a4
--- /dev/null
+++ b/datasource/mongo/heartbeat/manager_test.go
@@ -0,0 +1,42 @@
+/*
+ * 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 heartbeat_test
+
+import (
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+
+       
"github.com/apache/servicecomb-service-center/datasource/mongo/heartbeat"
+       _ 
"github.com/apache/servicecomb-service-center/datasource/mongo/heartbeat/heartbeatchecker"
+)
+
+func TestInit(t *testing.T) {
+       t.Run("init heartbeat plugin, should not pass", func(t *testing.T) {
+               pluginName := heartbeat.ImplName("unknown")
+               err := heartbeat.Init(heartbeat.Options{PluginImplName: 
pluginName})
+               assert.Error(t, err)
+       })
+       t.Run("install and init heartbeat plugin, should pass", func(t 
*testing.T) {
+               pluginName := heartbeat.ImplName("heartbeatchecker")
+               err := heartbeat.Init(heartbeat.Options{
+                       PluginImplName: pluginName,
+               })
+               assert.NoError(t, err)
+       })
+}
diff --git a/datasource/mongo/heartbeat/options.go 
b/datasource/mongo/heartbeat/options.go
new file mode 100644
index 0000000..5d8b164
--- /dev/null
+++ b/datasource/mongo/heartbeat/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 heartbeat
+
+// Options contains configuration for plugins
+type Options struct {
+       PluginImplName ImplName
+}
diff --git a/datasource/mongo/heartbeat/types.go 
b/datasource/mongo/heartbeat/types.go
new file mode 100644
index 0000000..cbd5d9d
--- /dev/null
+++ b/datasource/mongo/heartbeat/types.go
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package heartbeat
+
+type ImplName string
diff --git a/datasource/mongo/ms.go b/datasource/mongo/ms.go
index 8cad99e..7570b25 100644
--- a/datasource/mongo/ms.go
+++ b/datasource/mongo/ms.go
@@ -19,6 +19,7 @@ package mongo
 
 import (
        "context"
+       
"github.com/apache/servicecomb-service-center/datasource/mongo/heartbeat"
        pb "github.com/apache/servicecomb-service-center/pkg/registry"
 )
 
@@ -110,7 +111,7 @@ func (ds *DataSource) UnregisterInstance(ctx 
context.Context, request *pb.Unregi
 }
 
 func (ds *DataSource) Heartbeat(ctx context.Context, request 
*pb.HeartbeatRequest) (*pb.HeartbeatResponse, error) {
-       return &pb.HeartbeatResponse{}, nil
+       return heartbeat.Instance().Heartbeat(ctx, request)
 }
 
 func (ds *DataSource) HeartbeatSet(ctx context.Context, request 
*pb.HeartbeatSetRequest) (*pb.HeartbeatSetResponse, error) {

Reply via email to