Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/scserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ import (
)

func main() {
syncsvr.Run()
go syncsvr.Run()
server.Run()
}
File renamed without changes.
26 changes: 16 additions & 10 deletions syncer/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要定期刷新,go-chassis集成了动态配置框架go-archaius,使用方式如下
endpoint = archaius.GetString("syncer.endpoint", "")

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
Expand Down
6 changes: 4 additions & 2 deletions syncer/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
48 changes: 33 additions & 15 deletions syncer/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,54 @@
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"
"github.com/apache/servicecomb-service-center/syncer/metrics"
"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)
}
}
}
8 changes: 5 additions & 3 deletions syncer/service/admin/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down