You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ze...@apache.org on 2019/06/21 06:37:17 UTC
[servicecomb-service-center] branch master updated: save syncdata
to etcd
This is an automated email from the ASF dual-hosted git repository.
zenlin 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 e50a9e7 save syncdata to etcd
new 8415d94 Merge pull request #560 from ChinX/master
e50a9e7 is described below
commit e50a9e72b693bb3a01f530e97f91abc9f8a0d7b2
Author: chinx <c5...@126.com>
AuthorDate: Fri Jun 21 10:02:18 2019 +0800
save syncdata to etcd
---
syncer/etcd/storage.go | 42 ++++++++++++++++++++++++++++++++++++------
1 file changed, 36 insertions(+), 6 deletions(-)
diff --git a/syncer/etcd/storage.go b/syncer/etcd/storage.go
index 36995a3..5d45190 100644
--- a/syncer/etcd/storage.go
+++ b/syncer/etcd/storage.go
@@ -32,6 +32,8 @@ import (
var (
// mappingsKey the key of instances mapping in etcd
mappingsKey = "/syncer/v1/mappings"
+ // servicesKey the key of service in etcd
+ servicesKey = "/syncer/v1/services"
)
type storage struct {
@@ -65,16 +67,12 @@ func (s *storage) getPrefixKey(prefix string, handler func(key, val []byte) (nex
// UpdateData Update data to storage
func (s *storage) UpdateData(data *pb.SyncData) {
- s.lock.Lock()
- s.data = data
- s.lock.Unlock()
+ s.UpdateServices(data.Services)
}
// GetData Get data from storage
func (s *storage) GetData() (data *pb.SyncData) {
- s.lock.RLock()
- data = s.data
- s.lock.RUnlock()
+ data = &pb.SyncData{Services: s.GetServices()}
return
}
@@ -94,6 +92,38 @@ next:
}
}
+// UpdateServices Update services to storage
+func (s *storage) UpdateServices(services []*pb.SyncService) {
+ for _, val := range services {
+ key := servicesKey + "/" + val.Service.ServiceId
+ data, err := proto.Marshal(val)
+ if err != nil {
+ log.Errorf(err, "Proto marshal failed: %s", err)
+ continue
+ }
+ _, err = s.client.Put(context.Background(), key, util.BytesToStringWithNoCopy(data))
+ if err != nil {
+ log.Errorf(err, "Save service to etcd failed: %s", err)
+ }
+ }
+}
+
+// GetServices Get services from storage
+func (s *storage) GetServices() (services []*pb.SyncService) {
+ services = make([]*pb.SyncService, 0, 10)
+ s.getPrefixKey(servicesKey, func(key, val []byte) (next bool) {
+ next = true
+ item := &pb.SyncService{}
+ if err := proto.Unmarshal(val, item); err != nil {
+ log.Errorf(err, "Proto unmarshal '%s' failed: %s", val, err)
+ return
+ }
+ services = append(services, item)
+ return
+ })
+ return
+}
+
// UpdateMapByCluster update map to storage by clusterName of other cluster
func (s *storage) UpdateMapByCluster(clusterName string, mapping pb.SyncMapping) {
newMaps := make(pb.SyncMapping, 0, len(mapping))