From 4b61afc27fe3c2431d265444fbff3cbdc7d901b7 Mon Sep 17 00:00:00 2001 From: tornado-ssy <64736788+tornado-ssy@users.noreply.github.com> Date: Sat, 25 May 2024 10:04:57 +0800 Subject: [PATCH] fix some problems (#1470) * [fix] fix the problem that the config will be synced many times when updated in some condition(mulit-engine,one engine has a late task of undate one microservice) * [fix] fix the problem that get the microservice of sc itself occur a error will cause the failure of healthCheck --------- Co-authored-by: songshiyuan 00649746 --- server/service/registry/registry.go | 12 +++++++----- syncer/service/replicator/resource/config.go | 18 +++++++++++++----- syncer/service/replicator/resource/resource.go | 6 ++++++ syncer/service/task/manager.go | 6 +++++- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/server/service/registry/registry.go b/server/service/registry/registry.go index a7628ec2e..6cf707a43 100644 --- a/server/service/registry/registry.go +++ b/server/service/registry/registry.go @@ -22,15 +22,16 @@ import ( "fmt" "time" + pb "github.com/go-chassis/cari/discovery" + "github.com/go-chassis/cari/pkg/errsvc" + "github.com/go-chassis/foundation/gopool" + "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 { @@ -75,11 +76,12 @@ func registerService(ctx context.Context) error { } log.Warn(fmt.Sprintf("service center service[%s] already registered", serviceID)) - core.Service, err = discosvc.GetService(ctx, core.GetServiceRequest(serviceID)) + service, err := discosvc.GetService(ctx, core.GetServiceRequest(serviceID)) if err != nil { log.Error(fmt.Sprintf("query service center service[%s] info failed", serviceID), err) return err } + core.Service = service return nil } @@ -131,7 +133,7 @@ func autoSelfHeartBeat() { if err == nil { continue } - //服务不存在,创建服务 + // 服务不存在,创建服务 err = selfRegister(ctx) if err != nil { log.Error(fmt.Sprintf("retry to register[%s/%s/%s/%s] failed", diff --git a/syncer/service/replicator/resource/config.go b/syncer/service/replicator/resource/config.go index d431f5a28..c48b89ddb 100644 --- a/syncer/service/replicator/resource/config.go +++ b/syncer/service/replicator/resource/config.go @@ -21,9 +21,11 @@ import ( "context" "errors" "fmt" + "sync" kiemodel "github.com/apache/servicecomb-kie/pkg/model" kiedb "github.com/apache/servicecomb-kie/server/datasource" + "github.com/apache/servicecomb-service-center/pkg/log" "github.com/apache/servicecomb-service-center/pkg/util" "github.com/apache/servicecomb-service-center/server/config" @@ -32,12 +34,10 @@ import ( const Config = "config" +var configOnce sync.Once + func NewConfig(e *v1sync.Event) Resource { - kind := config.GetString("registry.kind", "etcd", config.WithStandby("registry_plugin")) - err := kiedb.Init(kind) - if err != nil { - log.Fatal(fmt.Sprintf("kie datasource[%s] init failed", kind), err) - } + configOnce.Do(initKieResouece) c := &kvConfig{ event: e, } @@ -45,6 +45,14 @@ func NewConfig(e *v1sync.Event) Resource { return c } +func initKieResouece() { + kind := config.GetString("registry.kind", "etcd", config.WithStandby("registry_plugin")) + err := kiedb.Init(kind) + if err != nil { + log.Fatal(fmt.Sprintf("kie datasource[%s] init failed", kind), err) + } +} + type kvConfig struct { defaultFailHandler diff --git a/syncer/service/replicator/resource/resource.go b/syncer/service/replicator/resource/resource.go index fe1a128b0..25f199403 100644 --- a/syncer/service/replicator/resource/resource.go +++ b/syncer/service/replicator/resource/resource.go @@ -23,10 +23,12 @@ import ( "errors" "fmt" "strconv" + "time" "github.com/apache/servicecomb-service-center/eventbase/datasource" "github.com/apache/servicecomb-service-center/eventbase/model" "github.com/apache/servicecomb-service-center/eventbase/service/tombstone" + "github.com/apache/servicecomb-service-center/pkg/log" v1sync "github.com/apache/servicecomb-service-center/syncer/api/v1" @@ -49,6 +51,7 @@ const ( ResultStatusMicroNonExist = "microNonExist" ResultStatusInstNonExist = "instNonExist" ResultStatusNonImplement = "nonImplement" + oneDaySecond = 86400 ) var codeDescriber = map[int32]string{ @@ -344,6 +347,9 @@ func (o *checker) needOperate(ctx context.Context) *Result { if len(o.resourceID) == 0 { return nil } + if o.event.Timestamp+oneDaySecond >= time.Now().Unix() { + return SkipResult() + } ts, err := o.tombstoneLoader.get(ctx, &model.GetTombstoneRequest{ ResourceType: o.event.Subject, diff --git a/syncer/service/task/manager.go b/syncer/service/task/manager.go index e9cf5fb80..4e884eab6 100644 --- a/syncer/service/task/manager.go +++ b/syncer/service/task/manager.go @@ -25,6 +25,7 @@ import ( "time" "github.com/apache/servicecomb-service-center/eventbase/service/task" + "github.com/apache/servicecomb-service-center/pkg/log" "github.com/apache/servicecomb-service-center/pkg/util" v1sync "github.com/apache/servicecomb-service-center/syncer/api/v1" @@ -254,7 +255,10 @@ func (m *manager) handleResult(res *event.Result) { if res.Error != nil || res.Data.Code == resource.Fail { log.Error(fmt.Sprintf("get task %s result, return error", res.ID), res.Error) m.cache.Range(func(key, value interface{}) bool { - m.cache.Delete(key) + if key == res.ID { + m.cache.Delete(key) + return false + } return true }) return