Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] support refresh the config of syncer without redeploy servicec… #1467

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
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 @@
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 @@
Mode []string `yaml:"mode"`
}

func Init() error {
err := archaius.AddFile(filepath.Join(util.GetAppRoot(), "conf", "syncer.yaml"))
func Init() (error, bool) {

Check warning on line 49 in syncer/config/config.go

View workflow job for this annotation

GitHub Actions / lint

error-return: error should be the last type when returning multiple items (revive)
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
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) {

Check warning on line 65 in syncer/config/config.go

View workflow job for this annotation

GitHub Actions / lint

error-return: error should be the last type when returning multiple items (revive)
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 {

Check failure on line 42 in syncer/server/server.go

View workflow job for this annotation

GitHub Actions / lint

S1000: should use for range instead of for { select {} } (gosimple)
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
Loading