diff --git a/cmd/scserver/main.go b/cmd/scserver/main.go index d9e6cd5a6..efb8468fb 100644 --- a/cmd/scserver/main.go +++ b/cmd/scserver/main.go @@ -29,6 +29,6 @@ import ( ) func main() { - syncsvr.Run() + go syncsvr.Run() server.Run() } diff --git a/etc/conf/syncer.yaml b/etc/conf/syncer/syncer.yaml similarity index 100% rename from etc/conf/syncer.yaml rename to etc/conf/syncer/syncer.yaml diff --git a/syncer/config/config.go b/syncer/config/config.go index de01f5ece..f3065710e 100644 --- a/syncer/config/config.go +++ b/syncer/config/config.go @@ -20,10 +20,12 @@ package config import ( "fmt" "path/filepath" + "reflect" + + "github.com/go-chassis/go-archaius" "github.com/apache/servicecomb-service-center/pkg/log" "github.com/apache/servicecomb-service-center/pkg/util" - "github.com/go-chassis/go-archaius" ) var config Config @@ -44,28 +46,32 @@ type Peer struct { Mode []string `yaml:"mode"` } -func Init() error { - err := archaius.AddFile(filepath.Join(util.GetAppRoot(), "conf", "syncer.yaml")) +func Init() (error, bool) { + err := archaius.AddFile(filepath.Join(util.GetAppRoot(), "conf", "syncer", "syncer.yaml")) if err != nil { log.Warn(fmt.Sprintf("can not add syncer config file source, error: %s", err)) - return err + return err, false } - err = Reload() + err, isRefresh := Reload() if err != nil { log.Fatal("reload syncer configs failed", err) - return err + return err, false } - return nil + return nil, isRefresh } // Reload all configurations -func Reload() error { +func Reload() (error, bool) { + oldConfig := config err := archaius.UnmarshalConfig(&config) if err != nil { - return err + return err, false + } + if !reflect.DeepEqual(oldConfig, config) { + return nil, true } - return nil + return nil, false } // GetConfig return the syncer full configurations diff --git a/syncer/config/config_test.go b/syncer/config/config_test.go index 0678f82cc..4f074e323 100644 --- a/syncer/config/config_test.go +++ b/syncer/config/config_test.go @@ -25,13 +25,15 @@ import ( _ "github.com/apache/servicecomb-service-center/test" - "github.com/apache/servicecomb-service-center/syncer/config" "github.com/stretchr/testify/assert" + + "github.com/apache/servicecomb-service-center/syncer/config" ) func TestGetConfig(t *testing.T) { changeConfigPath() - assert.NoError(t, config.Init()) + err, _ := config.Init() + assert.NoError(t, err) assert.NotNil(t, config.GetConfig().Sync) } diff --git a/syncer/server/server.go b/syncer/server/server.go index c880d55fd..ea09e39fa 100644 --- a/syncer/server/server.go +++ b/syncer/server/server.go @@ -18,6 +18,11 @@ package server import ( + "time" + + "github.com/go-chassis/go-chassis/v2" + chassisServer "github.com/go-chassis/go-chassis/v2/core/server" + "github.com/apache/servicecomb-service-center/pkg/log" syncv1 "github.com/apache/servicecomb-service-center/syncer/api/v1" "github.com/apache/servicecomb-service-center/syncer/config" @@ -25,29 +30,42 @@ import ( "github.com/apache/servicecomb-service-center/syncer/rpc" "github.com/apache/servicecomb-service-center/syncer/service/admin" "github.com/apache/servicecomb-service-center/syncer/service/sync" - "github.com/go-chassis/go-chassis/v2" - chassisServer "github.com/go-chassis/go-chassis/v2/core/server" ) +const syncRefreshTime = 15 * time.Second + // Run register chassis schema and run syncer services before chassis.Run() func Run() { - if err := config.Init(); err != nil { - log.Error("syncer config init failed", err) - } + ticker := time.NewTicker(syncRefreshTime) + defer ticker.Stop() - if !config.GetConfig().Sync.EnableOnStart { - log.Warn("syncer is disabled") - return - } + for { + select { + case <-ticker.C: + err, isRefresh := config.Init() + if err != nil { + log.Error("syncer config init failed", err) + } + if !isRefresh { + return + } + + if !config.GetConfig().Sync.EnableOnStart { + log.Warn("syncer is disabled") + return + } + + chassis.RegisterSchema("grpc", rpc.NewServer(), + chassisServer.WithRPCServiceDesc(&syncv1.EventService_ServiceDesc)) - chassis.RegisterSchema("grpc", rpc.NewServer(), - chassisServer.WithRPCServiceDesc(&syncv1.EventService_ServiceDesc)) + admin.Init() - admin.Init() + sync.Init() - sync.Init() + if err := metrics.Init(); err != nil { + log.Error("syncer metrics init failed", err) + } - if err := metrics.Init(); err != nil { - log.Error("syncer metrics init failed", err) + } } } diff --git a/syncer/service/admin/health_test.go b/syncer/service/admin/health_test.go index 28dddae18..ba8cb3593 100644 --- a/syncer/service/admin/health_test.go +++ b/syncer/service/admin/health_test.go @@ -30,11 +30,12 @@ import ( _ "github.com/apache/servicecomb-service-center/test" + "github.com/stretchr/testify/assert" + v1sync "github.com/apache/servicecomb-service-center/syncer/api/v1" "github.com/apache/servicecomb-service-center/syncer/config" syncrpc "github.com/apache/servicecomb-service-center/syncer/rpc" "github.com/apache/servicecomb-service-center/syncer/service/admin" - "github.com/stretchr/testify/assert" ) type mockServer struct { @@ -132,9 +133,10 @@ func checkError(resp *admin.Resp, err error) bool { func TestHealthTotalTime(t *testing.T) { changeConfigPath() - assert.NoError(t, config.Init()) + err, _ := config.Init() + assert.NoError(t, err) now := time.Now() - _, err := admin.Health() + _, err = admin.Health() assert.NoError(t, err) healthEndTime := time.Now() if healthEndTime.Sub(now) >= time.Second*30 {