-
Notifications
You must be signed in to change notification settings - Fork 9
/
tableAwareBrokerSelector.go
49 lines (44 loc) · 1.08 KB
/
tableAwareBrokerSelector.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
package pinot
import (
"fmt"
"math/rand"
"strings"
"sync"
)
const (
offlineSuffix = "_OFFLINE"
realtimeSuffix = "_REALTIME"
)
type tableAwareBrokerSelector struct {
tableBrokerMap map[string]([]string)
allBrokerList []string
rwMux sync.RWMutex
}
func (s *tableAwareBrokerSelector) selectBroker(table string) (string, error) {
tableName := extractTableName(table)
var brokerList []string
if tableName == "" {
s.rwMux.RLock()
brokerList = s.allBrokerList
s.rwMux.RUnlock()
if len(brokerList) == 0 {
return "", fmt.Errorf("No available broker found")
}
} else {
var found bool
s.rwMux.RLock()
brokerList, found = s.tableBrokerMap[tableName]
s.rwMux.RUnlock()
if !found {
return "", fmt.Errorf("Unable to find the table: %s", table)
}
if len(brokerList) == 0 {
return "", fmt.Errorf("No available broker found for table: %s", table)
}
}
// #nosec G404
return brokerList[rand.Intn(len(brokerList))], nil
}
func extractTableName(table string) string {
return strings.Replace(strings.Replace(table, offlineSuffix, "", 1), realtimeSuffix, "", 1)
}