Merge pull request #1211 from robotLJW/master

[feat] add instance and heartbeat sync func when db mode is etcd
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 @@
 	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 @@
 
 	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 @@
 	"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 @@
 			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 @@
 		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 @@
 		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 @@
 			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 @@
 		}
 		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 @@
 		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 @@
 	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 @@
 	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 @@
 	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/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 @@
 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 @@
 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 @@
 	"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 @@
 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/syncer/service/event/event.go b/syncer/service/event/event.go
new file mode 100644
index 0000000..4e028b1
--- /dev/null
+++ b/syncer/service/event/event.go
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package event
+
+import (
+	"encoding/json"
+	"fmt"
+
+	guuid "github.com/gofrs/uuid"
+
+	v1 "github.com/apache/servicecomb-service-center/api/sync/v1"
+	"github.com/apache/servicecomb-service-center/pkg/log"
+)
+
+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
+}