-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraymanager.go
85 lines (76 loc) · 2.24 KB
/
graymanager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package finder
import (
"encoding/json"
"strings"
common "github.com/xfyun/finder-go/common"
storageCommon "github.com/xfyun/finder-go/storage/common"
"github.com/xfyun/finder-go/log"
)
const (
grayNodePathPrefix = "/gray"
)
func ParseGrayConfigData(serverId string, data []byte) (map[string]string, bool) {
//解析数据,解析出pushId和后面的数据
_, fData, err := common.DecodeValue(data)
if err != nil {
log.Log.Infof(" [getGrayData] DecodeValue 出错 %s", err)
return nil, false
}
var grayConfigMaps []map[string]interface{}
if err := json.Unmarshal(fData, &grayConfigMaps); err != nil {
log.Log.Infof(" [getGrayData] 使用json反序列化数据 %s %s %s", fData, " 出错 ", err)
return nil, false
}
//如何解析数据,会不会出现一个server在两个灰度组的情况
garyConfig := make(map[string]string)
for _, value := range grayConfigMaps {
groupId := value["group_id"]
serverStr := value["server_list"].([]interface{})[0].(string)
serverList := strings.Split(serverStr, ",")
for _, server := range serverList {
garyConfig[server] = groupId.(string)
}
}
return garyConfig, true
}
func GetGrayConfigData(f *ConfigFinder, path string, callback storageCommon.ChangedCallback) error {
var serverId string = f.config.MeteData.Address
if !strings.HasSuffix(path, grayNodePathPrefix) {
path += grayNodePathPrefix
}
//节点不存在如何处理?
var data []byte
var err error
if callback != nil {
data, err = f.storageMgr.GetDataWithWatchV2(path, callback)
} else {
data, err = f.storageMgr.GetData(path)
}
if err != nil {
if strings.Compare(err.Error(), common.ZK_NODE_DOSE_NOT_EXIST) == 0 {
//创建节点
err := f.storageMgr.SetPath(path)
if err != nil {
log.Log.Infof(" [getGrayData] 根据 path: %s %s %s", path, "创建节点出错:", err)
}
if callback != nil {
//监听对应的节点
data, err = f.storageMgr.GetDataWithWatchV2(path, callback)
} else {
data, err = f.storageMgr.GetData(path)
}
return nil
}
return err
}
if data == nil || len(data) == 0 {
return nil
}
if grayConfig, ok := ParseGrayConfigData(serverId, data); ok {
for key, value := range grayConfig {
f.grayConfig.Store(key, value)
}
return nil
}
return err
}