This is an automated email from the ASF dual-hosted git repository.

robotljw 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 bc15e4e  [feat] add instance and heartbeat sync func when db mode is 
etcd
     new 11cabb3  Merge pull request #1211 from robotLJW/master
bc15e4e is described below

commit bc15e4e9b6a9b6b81cd130855a67b44b409dfdcf
Author: robotljw <[email protected]>
AuthorDate: Sun Jan 9 21:46:20 2022 +0800

    [feat] add instance and heartbeat sync func when db mode is etcd
---
 datasource/common.go                              | 13 +++++----
 datasource/etcd/ms.go                             | 17 ++++++++++--
 go.mod                                            |  3 +--
 go.sum                                            |  2 --
 pkg/util/uuid.go                                  |  5 ++--
 scripts/release/LICENSE                           |  8 +++---
 server/service/gov/mock/mock.go                   |  5 ++--
 pkg/util/uuid.go => syncer/service/event/event.go | 33 ++++++++++++++++++-----
 8 files changed, 60 insertions(+), 26 deletions(-)

diff --git a/datasource/common.go b/datasource/common.go
index 5149fb2..8445a83 100644
--- a/datasource/common.go
+++ b/datasource/common.go
@@ -32,11 +32,14 @@ const (
        RegistryAppID         = "default"
        Provider              = "p"
 
-       ResourceAccount    = "account"
-       ResourceRole       = "role"
-       ResourceDependency = "dependency"
-       ResourceService    = "service"
-       ResourceKV         = "kv"
+       ResourceAccount      = "account"
+       ResourceRole         = "role"
+       ResourceDependency   = "dependency"
+       ResourceService      = "service"
+       ResourceKV           = "kv"
+       ResourceInstance     = "instance"
+       ResourceHeartbeat    = "heartbeat"
+       ResourceHeartbeatSet = "heartbeatSet"
 )
 
 // WrapErrResponse is temp func here to wait finish to refact the discosvc pkg
diff --git a/datasource/etcd/ms.go b/datasource/etcd/ms.go
index 220030c..e82d949 100644
--- a/datasource/etcd/ms.go
+++ b/datasource/etcd/ms.go
@@ -27,6 +27,7 @@ import (
 
        pb "github.com/go-chassis/cari/discovery"
        "github.com/go-chassis/cari/pkg/errsvc"
+       "github.com/go-chassis/cari/sync"
        "github.com/go-chassis/foundation/gopool"
        "github.com/jinzhu/copier"
        "github.com/little-cui/etcdadpt"
@@ -44,6 +45,7 @@ import (
        "github.com/apache/servicecomb-service-center/server/core"
        "github.com/apache/servicecomb-service-center/server/plugin/uuid"
        quotasvc 
"github.com/apache/servicecomb-service-center/server/service/quota"
+       "github.com/apache/servicecomb-service-center/syncer/service/event"
 )
 
 var (
@@ -599,12 +601,19 @@ func (ds *MetadataManager) registerInstance(ctx 
context.Context, request *pb.Reg
                        instanceFlag, instanceID, remoteIP), nil)
                return "", pb.NewError(pb.ErrServiceNotExists, "Service does 
not exist.")
        }
-
+       sendEvent(sync.CreateAction, datasource.ResourceInstance, request)
        log.Info(fmt.Sprintf("register instance %s, instanceID %s, operator %s",
                instanceFlag, instanceID, remoteIP))
        return instanceID, nil
 }
 
+func sendEvent(action string, resourceType string, resource interface{}) {
+       if !datasource.EnableSync {
+               return
+       }
+       event.Publish(action, resourceType, resource)
+}
+
 func (ds *MetadataManager) calcInstanceTTL(instance *pb.MicroServiceInstance) 
int64 {
        if instance.HealthCheck == nil {
                instance.HealthCheck = &pb.HealthCheck{
@@ -1074,6 +1083,7 @@ func (ds *MetadataManager) UpdateInstanceStatus(ctx 
context.Context, request *pb
                return resp, nil
        }
 
+       sendEvent(sync.UpdateAction, datasource.ResourceInstance, 
copyInstanceRef)
        log.Info(fmt.Sprintf("update instance[%s] status successfully", 
updateStatusFlag))
        return &pb.UpdateInstanceStatusResponse{
                Response: pb.CreateResponse(pb.ResponseSuccess, "Update service 
instance status successfully."),
@@ -1113,6 +1123,7 @@ func (ds *MetadataManager) UpdateInstanceProperties(ctx 
context.Context, request
                return resp, nil
        }
 
+       sendEvent(sync.UpdateAction, datasource.ResourceInstance, 
copyInstanceRef)
        log.Info(fmt.Sprintf("update instance[%s] properties successfully", 
instanceFlag))
        return &pb.UpdateInstancePropsResponse{
                Response: pb.CreateResponse(pb.ResponseSuccess, "Update service 
instance properties successfully."),
@@ -1160,6 +1171,7 @@ func (ds *MetadataManager) HeartbeatSet(ctx 
context.Context, request *pb.Heartbe
                        Instances: instanceHbRstArr,
                }, nil
        }
+       sendEvent(sync.UpdateAction, datasource.ResourceHeartbeatSet, request)
        log.Error(fmt.Sprintf("batch update heartbeats failed, %v", 
request.Instances), nil)
        return &pb.HeartbeatSetResponse{
                Response:  pb.CreateResponse(pb.ErrInstanceNotExists, 
"Heartbeat set failed."),
@@ -1280,7 +1292,7 @@ func (ds *MetadataManager) UnregisterInstance(ctx 
context.Context, request *pb.U
                }
                return resp, nil
        }
-
+       sendEvent(sync.DeleteAction, datasource.ResourceInstance, request)
        log.Info(fmt.Sprintf("unregister instance[%s], operator %s", 
instanceFlag, remoteIP))
        return &pb.UnregisterInstanceResponse{
                Response: pb.CreateResponse(pb.ResponseSuccess, "Unregister 
service instance successfully."),
@@ -1312,6 +1324,7 @@ func (ds *MetadataManager) Heartbeat(ctx context.Context, 
request *pb.HeartbeatR
                log.Info(fmt.Sprintf("heartbeat successful, renew instance[%s] 
ttl to %d. operator %s",
                        instanceFlag, ttl, remoteIP))
        }
+       sendEvent(sync.UpdateAction, datasource.ResourceHeartbeat, request)
        return &pb.HeartbeatResponse{
                Response: pb.CreateResponse(pb.ResponseSuccess,
                        "Update service instance heartbeat successfully."),
diff --git a/go.mod b/go.mod
index bd620de..7b0ca3f 100644
--- a/go.mod
+++ b/go.mod
@@ -21,6 +21,7 @@ require (
        github.com/go-chassis/go-chassis/v2 v2.3.0
        github.com/go-chassis/kie-client v0.1.1-0.20210926011742-97eed4281056
        github.com/go-chassis/openlog v1.1.3
+       github.com/gofrs/uuid v4.0.0+incompatible
        github.com/golang-jwt/jwt v3.2.1+incompatible
        github.com/gorilla/websocket v1.4.3-0.20210424162022-e8629af678b7
        github.com/iancoleman/strcase v0.1.2
@@ -40,7 +41,6 @@ require (
        github.com/prometheus/procfs v0.6.0
        github.com/robfig/cron/v3 v3.0.1
        github.com/rs/cors v1.7.0 // v1.1
-       github.com/satori/go.uuid v1.1.0
        github.com/spf13/cobra v1.1.3
        github.com/spf13/pflag v1.0.5
        github.com/stretchr/testify v1.7.0
@@ -96,7 +96,6 @@ require (
        github.com/go-openapi/jsonreference v0.19.3 // indirect
        github.com/go-openapi/swag v0.19.5 // indirect
        github.com/go-stack/stack v1.8.0 // indirect
-       github.com/gofrs/uuid v4.0.0+incompatible // indirect
        github.com/gogo/protobuf v1.3.2 // indirect
        github.com/golang/protobuf v1.5.2 // indirect
        github.com/golang/snappy v0.0.1 // indirect
diff --git a/go.sum b/go.sum
index 2c66fa5..272656a 100644
--- a/go.sum
+++ b/go.sum
@@ -571,8 +571,6 @@ github.com/rs/cors v1.7.0/go.mod 
h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
 github.com/russross/blackfriday/v2 v2.0.1 
h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
 github.com/russross/blackfriday/v2 v2.0.1/go.mod 
h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod 
h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
-github.com/satori/go.uuid v1.1.0 
h1:B9KXyj+GzIpJbV7gmr873NsY6zpbxNy24CBtGrk7jHo=
-github.com/satori/go.uuid v1.1.0/go.mod 
h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
 github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod 
h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
 github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 
h1:X+yvsM2yrEktyI+b2qND5gpH8YhURn0k8OCaeRnkINo=
 github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644/go.mod 
h1:nkxAfR/5quYxwPZhyDxgasBMnRtBZd0FCEpawpjMUFg=
diff --git a/pkg/util/uuid.go b/pkg/util/uuid.go
index 1ebd276..dc25293 100644
--- a/pkg/util/uuid.go
+++ b/pkg/util/uuid.go
@@ -20,11 +20,12 @@ package util
 import (
        "strings"
 
-       uuid "github.com/satori/go.uuid"
+       "github.com/gofrs/uuid"
 )
 
 const DASH = "-"
 
 func GenerateUUID() string {
-       return strings.Replace(uuid.NewV1().String(), string(DASH), "", -1)
+       id, _ := uuid.NewV4()
+       return strings.Replace(id.String(), string(DASH), "", -1)
 }
diff --git a/scripts/release/LICENSE b/scripts/release/LICENSE
index a983724..8360326 100644
--- a/scripts/release/LICENSE
+++ b/scripts/release/LICENSE
@@ -260,11 +260,11 @@ For details, see https://github.com/beorn7/perks
 You can find a copy of the License at licenses/LICENSE-beorn7-perks
 
 ================================================================
-For satori/go.uuid (879c5887cd475cd7864858769793b2ceb0d44feb)
+For gofrs/uuid (028e8409cdd0ed11a2b5bb3feb1ae2285ebb94fa)
 ================================================================
-This product bundles satori/go.uuid which is licensed under the MIT License.
-For details, see https://github.com/satori/go.uuid
-You can find a copy of the License at licenses/LICENSE-satori-go.uuid
+This product bundles gofrs/uuid which is licensed under the MIT License.
+For details, see https://github.com/gofrs/uuid
+You can find a copy of the License at licenses/LICENSE-gofrs-uuid
 
 ================================================================
 For rs/cors (8dd4211afb5d08dbb39a533b9bb9e4b486351df6)
diff --git a/server/service/gov/mock/mock.go b/server/service/gov/mock/mock.go
index 1414087..990ffcb 100644
--- a/server/service/gov/mock/mock.go
+++ b/server/service/gov/mock/mock.go
@@ -23,7 +23,7 @@ import (
        "fmt"
        "log"
 
-       uuid "github.com/satori/go.uuid"
+       "github.com/gofrs/uuid"
 
        "github.com/apache/servicecomb-service-center/pkg/gov"
        "github.com/apache/servicecomb-service-center/server/config"
@@ -40,7 +40,8 @@ const MatchGroup = "match-group"
 var PolicyNames = []string{"retry", "rateLimiting", "circuitBreaker", 
"bulkhead"}
 
 func (d *Distributor) Create(ctx context.Context, kind, project string, p 
*gov.Policy) ([]byte, error) {
-       p.ID = uuid.NewV4().String()
+       id, _ := uuid.NewV4()
+       p.ID = id.String()
        p.Kind = kind
        log.Println(fmt.Sprintf("create %v", &p))
        d.lbPolicies[p.GovernancePolicy.ID] = p
diff --git a/pkg/util/uuid.go b/syncer/service/event/event.go
similarity index 53%
copy from pkg/util/uuid.go
copy to syncer/service/event/event.go
index 1ebd276..4e028b1 100644
--- a/pkg/util/uuid.go
+++ b/syncer/service/event/event.go
@@ -15,16 +15,35 @@
  * limitations under the License.
  */
 
-package util
+package event
 
 import (
-       "strings"
+       "encoding/json"
+       "fmt"
 
-       uuid "github.com/satori/go.uuid"
-)
+       guuid "github.com/gofrs/uuid"
 
-const DASH = "-"
+       v1 "github.com/apache/servicecomb-service-center/api/sync/v1"
+       "github.com/apache/servicecomb-service-center/pkg/log"
+)
 
-func GenerateUUID() string {
-       return strings.Replace(uuid.NewV1().String(), string(DASH), "", -1)
+func Publish(action string, resourceType string, resource interface{}) {
+       eventID, err := guuid.NewV4()
+       if err != nil {
+               log.Error("fail to create eventID", err)
+               return
+       }
+       resourceValue, err := json.Marshal(resource)
+       if err != nil {
+               log.Error("fail to marshal the resource", err)
+               return
+       }
+       event := v1.Event{
+               Id:      eventID.String(),
+               Action:  action,
+               Subject: resourceType,
+               Value:   resourceValue,
+       }
+       log.Info(fmt.Sprintf("success to send event %s", event.Subject))
+       // TODO to send event
 }

Reply via email to