forked from startreedata/pinot-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamicBrokerSelector.go
149 lines (135 loc) · 4.17 KB
/
dynamicBrokerSelector.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package pinot
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
zk "github.com/samuel/go-zookeeper/zk"
log "github.com/sirupsen/logrus"
)
const (
brokerExternalViewPath = "EXTERNALVIEW/brokerResource"
)
type ReadZNode func(path string) ([]byte, error)
type dynamicBrokerSelector struct {
tableAwareBrokerSelector
zkConfig *ZookeeperConfig
zkConn *zk.Conn
externalViewZnodeWatch <-chan zk.Event
readZNode ReadZNode
externalViewZkPath string
}
type externalView struct {
ID string `json:"id"`
SimpleFields map[string]string `json:"simpleFields"`
MapFields map[string](map[string]string) `json:"mapFields"`
ListFields map[string]([]string) `json:"listFields"`
}
func (s *dynamicBrokerSelector) init() error {
var err error
s.zkConn, _, err = zk.Connect(s.zkConfig.ZookeeperPath, time.Duration(s.zkConfig.SessionTimeoutSec)*time.Second)
if err != nil {
log.Errorf("Failed to connect to zookeeper: %v\n", s.zkConfig.ZookeeperPath)
return err
}
s.readZNode = func(path string) ([]byte, error) {
if s.zkConn == nil {
return nil, fmt.Errorf("Zk Connection hasn't been initialized.")
}
node, _, err := s.zkConn.Get(s.externalViewZkPath)
if err != nil {
log.Errorf("Failed to read zk: %s, ExternalView path: %s\n", s.zkConfig.ZookeeperPath, s.externalViewZkPath)
return nil, err
}
return node, nil
}
s.externalViewZkPath = s.zkConfig.PathPrefix + "/" + brokerExternalViewPath
_, _, s.externalViewZnodeWatch, err = s.zkConn.GetW(s.externalViewZkPath)
if err != nil {
log.Errorf("Failed to set a watcher on ExternalView path: %s, Error: %v\n", strings.Join(append(s.zkConfig.ZookeeperPath, s.externalViewZkPath), ""), err)
return err
}
if err = s.refreshExternalView(); err != nil {
return err
}
go s.setupWatcher()
return nil
}
func (s *dynamicBrokerSelector) setupWatcher() {
for {
select {
case ev := <-s.externalViewZnodeWatch:
if ev.Err != nil {
log.Error("GetW watcher error", ev.Err)
} else if ev.Type == zk.EventNodeDataChanged {
s.refreshExternalView()
}
break
}
time.Sleep(100 * time.Millisecond)
}
}
func (s *dynamicBrokerSelector) refreshExternalView() error {
if s.readZNode == nil {
return fmt.Errorf("No method defined to read from a ZNode.")
}
node, err := s.readZNode(s.externalViewZkPath)
if err != nil {
return err
}
ev, err := getExternalView(node)
if err != nil {
return err
}
newTableBrokerMap, newAllBrokerList := generateNewBrokerMappingExternalView(ev)
s.rwMux.Lock()
s.tableBrokerMap = newTableBrokerMap
s.allBrokerList = newAllBrokerList
s.rwMux.Unlock()
return nil
}
func getExternalView(evBytes []byte) (*externalView, error) {
var ev externalView
if err := json.Unmarshal(evBytes, &ev); err != nil {
log.Errorf("Failed to unmarshal ExternalView: %s, Error: %v\n", evBytes, err)
return nil, err
}
return &ev, nil
}
func generateNewBrokerMappingExternalView(ev *externalView) (map[string]([]string), []string) {
tableBrokerMap := map[string]([]string){}
allBrokerList := []string{}
for table, brokerMapping := range ev.MapFields {
tableName := extractTableName(table)
tableBrokerMap[tableName] = extractBrokers(brokerMapping)
allBrokerList = append(allBrokerList, tableBrokerMap[tableName]...)
}
return tableBrokerMap, allBrokerList
}
func extractBrokers(brokerMap map[string]string) []string {
brokerList := []string{}
for brokerName, status := range brokerMap {
if status == "ONLINE" {
host, port, err := extractBrokerHostPort(brokerName)
if err == nil {
brokerList = append(brokerList, strings.Join([]string{host, port}, ":"))
}
}
}
return brokerList
}
func extractBrokerHostPort(brokerKey string) (string, string, error) {
splits := strings.Split(brokerKey, "_")
if len(splits) < 2 {
err := fmt.Errorf("Invalid Broker Key: %s, should be in the format of Broker_[hostname]_[port]", brokerKey)
log.Error(err)
return "", "", err
}
_, err := strconv.Atoi(splits[len(splits)-1])
if err != nil {
log.Errorf("Failed to parse broker port:%s to integer", splits[len(splits)-1])
return "", "", err
}
return splits[len(splits)-2], splits[len(splits)-1], nil
}