zhangmeng
2020-01-13 94e9f50569bd20a697edb36711d017de1c19d1a5
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
package cache
 
import (
    "fmt"
    "strconv"
 
    "basic.com/dbapi.git"
    "basic.com/pubsub/cache.git/shardmap"
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/gopherdiscovery.git"
    "github.com/gogo/protobuf/proto"
)
 
const (
    PREFIX_TASKSDKRULE = "TASKSDKRULE_"
)
 
var cMap *shardmap.ShardMap
 
func Init(initChan chan bool, dbIp string, surveyPort int, pubSubPort int) {
    cMap = shardmap.New(uint8(32))
    urlSurvey := "tcp://" + dbIp + ":" + strconv.Itoa(surveyPort)
    urlPubSub := "tcp://" + dbIp + ":" + strconv.Itoa(pubSubPort)
    client, _ := gopherdiscovery.ClientWithSub(urlSurvey, urlPubSub, "analysisProc")
    recvMsg := client.HeartBeatMsg()
    fmt.Println(<-recvMsg)
 
    initCacheData(initChan)
 
    peers, _ := client.Peers()
    for b := range peers {
        fmt.Println("peerMsg:", b)
        updateData(b)
    }
}
 
func initCacheData(initChan chan bool) {
    initTaskSdkRule()
    initChan <- true
}
 
func initTaskSdkRule() {
    var api dbapi.TaskSdkRuleApi
 
    b, rules := api.FindAllTaskSdkRules()
    if b {
        if rules != nil {
            for _, tRule := range rules {
                cMap.Set(PREFIX_TASKSDKRULE+tRule.TaskId, tRule.SdkRules)
            }
        }
    }
}
 
func updateData(b []byte) {
    newUpdateMsg := &protomsg.DbChangeMessage{}
    if err := proto.Unmarshal(b, newUpdateMsg); err != nil {
        fmt.Println("dbChangeMsg unmarshal err:", err)
        return
    }
    switch newUpdateMsg.Table {
    case protomsg.TableChanged_T_TaskSdkRule:
        initTaskSdkRule()
    default:
 
    }
}
 
func GetTaskSdkRules(taskId string) []*protomsg.SdkRuleSet {
    r, b := cMap.Get(PREFIX_TASKSDKRULE + taskId)
    if b {
        return r.([]*protomsg.SdkRuleSet)
    } else {
        return nil
    }
}