You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2022/01/20 14:45:29 UTC

[servicecomb-service-center] branch master updated: Feature: sync the SC service (#1239)

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


The following commit(s) were added to refs/heads/master by this push:
     new 62a82d2  Feature: sync the SC service (#1239)
62a82d2 is described below

commit 62a82d2835df9e3faea8933237891805aba70d17
Author: little-cui <su...@qq.com>
AuthorDate: Thu Jan 20 22:45:19 2022 +0800

    Feature: sync the SC service (#1239)
---
 server/core/microservice.go         |  6 -----
 server/core/microservice_test.go    |  1 -
 server/handler/context/context.go   | 19 ++-------------
 server/service/registry/registry.go | 19 ++++++++++++---
 server/service/sync/sync.go         | 48 +++++++++++++++++++++++++++++++++++++
 syncer/rpc/server.go                |  5 ++--
 syncer/service/sync/sync.go         |  7 +-----
 test/test.go                        | 14 +++++------
 8 files changed, 76 insertions(+), 43 deletions(-)

diff --git a/server/core/microservice.go b/server/core/microservice.go
index 35ece9d..b104c38 100644
--- a/server/core/microservice.go
+++ b/server/core/microservice.go
@@ -101,12 +101,6 @@ func getEndpoints() []string {
 	return []string{endpoint}
 }
 
-func AddDefaultContextValue(ctx context.Context) context.Context {
-	return util.WithNoCache(util.SetContext(util.SetDomainProject(ctx,
-		datasource.RegistryDomain, datasource.RegistryProject),
-		CtxScSelf, true))
-}
-
 func RegisterGlobalServices() {
 	for _, s := range strings.Split(config.GetRegistry().GlobalVisible, ",") {
 		if len(s) > 0 {
diff --git a/server/core/microservice_test.go b/server/core/microservice_test.go
index 379a9ff..c404ad1 100644
--- a/server/core/microservice_test.go
+++ b/server/core/microservice_test.go
@@ -20,7 +20,6 @@ import (
 	"testing"
 
 	"github.com/apache/servicecomb-service-center/datasource"
-
 	"github.com/apache/servicecomb-service-center/server/config"
 	"github.com/go-chassis/cari/discovery"
 )
diff --git a/server/handler/context/context.go b/server/handler/context/context.go
index f40cb57..b623850 100644
--- a/server/handler/context/context.go
+++ b/server/handler/context/context.go
@@ -21,10 +21,9 @@ import (
 	"net/http"
 
 	"github.com/apache/servicecomb-service-center/pkg/chain"
-	"github.com/apache/servicecomb-service-center/pkg/log"
 	"github.com/apache/servicecomb-service-center/pkg/rest"
 	"github.com/apache/servicecomb-service-center/pkg/util"
-	"github.com/apache/servicecomb-service-center/server/config"
+	syncsvc "github.com/apache/servicecomb-service-center/server/service/sync"
 )
 
 const (
@@ -33,20 +32,6 @@ const (
 	queryCacheOnly = "cacheOnly"
 )
 
-const (
-	syncEnable = "1"
-)
-
-var enableSync string
-
-func init() {
-	enable := config.GetBool("sync.enableOnStart", false)
-	if enable {
-		enableSync = syncEnable
-	}
-	log.Info("enableSync is " + enableSync)
-}
-
 type Handler struct {
 }
 
@@ -64,7 +49,7 @@ func (c *Handler) Handle(i *chain.Invocation) {
 		v4.Write(r)
 	}
 
-	i.WithContext(util.CtxEnableSync, enableSync)
+	syncsvc.SetContext(i.Context())
 
 	c.commonQueryToContext(i)
 
diff --git a/server/service/registry/registry.go b/server/service/registry/registry.go
index 47b9597..a7628ec 100644
--- a/server/service/registry/registry.go
+++ b/server/service/registry/registry.go
@@ -22,14 +22,27 @@ import (
 	"fmt"
 	"time"
 
+	"github.com/apache/servicecomb-service-center/datasource"
 	"github.com/apache/servicecomb-service-center/pkg/log"
+	"github.com/apache/servicecomb-service-center/pkg/util"
 	"github.com/apache/servicecomb-service-center/server/core"
 	discosvc "github.com/apache/servicecomb-service-center/server/service/disco"
+	"github.com/apache/servicecomb-service-center/server/service/sync"
 	pb "github.com/go-chassis/cari/discovery"
 	"github.com/go-chassis/cari/pkg/errsvc"
 	"github.com/go-chassis/foundation/gopool"
 )
 
+func addDefaultContextValue(ctx context.Context) context.Context {
+	ctx = util.WithNoCache(ctx)
+	// set default domain/project
+	ctx = util.SetDomainProject(ctx, datasource.RegistryDomain, datasource.RegistryProject)
+	// register without quota check
+	ctx = util.SetContext(ctx, core.CtxScSelf, true)
+	// is a sync operation
+	return sync.SetContext(ctx)
+}
+
 func SelfRegister(ctx context.Context) error {
 	err := selfRegister(ctx)
 	if err != nil {
@@ -41,7 +54,7 @@ func SelfRegister(ctx context.Context) error {
 }
 
 func selfRegister(pCtx context.Context) error {
-	ctx := core.AddDefaultContextValue(pCtx)
+	ctx := addDefaultContextValue(pCtx)
 	err := registerService(ctx)
 	if err != nil {
 		return err
@@ -96,7 +109,7 @@ func registerInstance(ctx context.Context) error {
 }
 
 func selfHeartBeat(pCtx context.Context) error {
-	ctx := core.AddDefaultContextValue(pCtx)
+	ctx := addDefaultContextValue(pCtx)
 	err := discosvc.SendHeartbeat(ctx, core.HeartbeatRequest())
 	if err != nil {
 		log.Error("send heartbeat failed", err)
@@ -133,7 +146,7 @@ func SelfUnregister(pCtx context.Context) error {
 	if len(core.Instance.InstanceId) == 0 {
 		return nil
 	}
-	ctx := core.AddDefaultContextValue(pCtx)
+	ctx := addDefaultContextValue(pCtx)
 	err := discosvc.UnregisterInstance(ctx, core.UnregisterInstanceRequest())
 	if err != nil {
 		log.Error(fmt.Sprintf("unregister service center instance[%s/%s] failed",
diff --git a/server/service/sync/sync.go b/server/service/sync/sync.go
new file mode 100644
index 0000000..5b6f26e
--- /dev/null
+++ b/server/service/sync/sync.go
@@ -0,0 +1,48 @@
+/*
+ * 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 sync
+
+import (
+	"context"
+	"sync"
+
+	"github.com/apache/servicecomb-service-center/pkg/util"
+	"github.com/apache/servicecomb-service-center/server/config"
+)
+
+var (
+	enable bool
+	once   sync.Once
+)
+
+func Enable() bool {
+	once.Do(func() {
+		if config.GetBool("sync.enableOnStart", false) {
+			enable = true
+		}
+	})
+	return enable
+}
+
+func SetContext(ctx context.Context) context.Context {
+	var val string
+	if Enable() {
+		val = "1"
+	}
+	return util.SetContext(ctx, util.CtxEnableSync, val)
+}
diff --git a/syncer/rpc/server.go b/syncer/rpc/server.go
index 6be80e9..5601b0a 100644
--- a/syncer/rpc/server.go
+++ b/syncer/rpc/server.go
@@ -26,8 +26,8 @@ import (
 	"github.com/apache/servicecomb-service-center/syncer/service/replicator/resource"
 
 	"github.com/apache/servicecomb-service-center/pkg/log"
-	"github.com/apache/servicecomb-service-center/server/config"
 	v1sync "github.com/apache/servicecomb-service-center/syncer/api/v1"
+	"github.com/apache/servicecomb-service-center/syncer/config"
 )
 
 const (
@@ -75,8 +75,7 @@ func (s *Server) Health(_ context.Context, _ *v1sync.HealthRequest) (*v1sync.Hea
 		LocalTimestamp: time.Now().UnixNano(),
 	}
 	// TODO enable to close syncer
-	syncerEnabled := config.GetBool("sync.enableOnStart", false)
-	if !syncerEnabled {
+	if !config.GetConfig().Sync.EnableOnStart {
 		resp.Status = HealthStatusClose
 		return resp, nil
 	}
diff --git a/syncer/service/sync/sync.go b/syncer/service/sync/sync.go
index 314fe3b..e967b19 100644
--- a/syncer/service/sync/sync.go
+++ b/syncer/service/sync/sync.go
@@ -1,7 +1,6 @@
 package sync
 
 import (
-	"context"
 	"fmt"
 
 	// glint
@@ -11,8 +10,6 @@ import (
 	"github.com/apache/servicecomb-service-center/syncer/service/event"
 	"github.com/apache/servicecomb-service-center/syncer/service/replicator"
 	"github.com/apache/servicecomb-service-center/syncer/service/task"
-
-	"github.com/go-chassis/foundation/gopool"
 )
 
 func Init() {
@@ -21,9 +18,7 @@ func Init() {
 		return
 	}
 
-	gopool.Go(func(ctx context.Context) {
-		Work()
-	})
+	Work()
 }
 
 func Work() {
diff --git a/test/test.go b/test/test.go
index 7f564b7..2300c8b 100644
--- a/test/test.go
+++ b/test/test.go
@@ -22,22 +22,22 @@ import (
 	"context"
 	"time"
 
-	"github.com/go-chassis/cari/db"
-	"github.com/go-chassis/cari/db/config"
-	"github.com/go-chassis/go-archaius"
-	"github.com/little-cui/etcdadpt"
+	_ "github.com/apache/servicecomb-service-center/syncer/init"
 
 	_ "github.com/apache/servicecomb-service-center/server/init"
 
-	_ "github.com/apache/servicecomb-service-center/eventbase/bootstrap"
+	_ "github.com/apache/servicecomb-service-center/syncer/bootstrap"
+
 	_ "github.com/apache/servicecomb-service-center/server/bootstrap"
-	_ "github.com/go-chassis/cari/db/bootstrap"
-	_ "github.com/go-chassis/go-chassis-extension/protocol/grpc/server"
 
 	"github.com/apache/servicecomb-service-center/datasource"
 	edatasource "github.com/apache/servicecomb-service-center/eventbase/datasource"
 	"github.com/apache/servicecomb-service-center/server/metrics"
 	"github.com/apache/servicecomb-service-center/server/service/registry"
+	"github.com/go-chassis/cari/db"
+	"github.com/go-chassis/cari/db/config"
+	"github.com/go-chassis/go-archaius"
+	"github.com/little-cui/etcdadpt"
 )
 
 func init() {