forked from startreedata/pinot-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollerBasedBrokerSelector.go
136 lines (122 loc) · 3.29 KB
/
controllerBasedBrokerSelector.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
package pinot
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
const (
controllerAPIEndpoint = "/v2/brokers/tables?state=ONLINE"
defaultUpdateFreqMs = 1000
)
var (
controllerDefaultHTTPHeader = map[string]string{
"Accept": "application/json",
}
)
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type controllerBasedSelector struct {
tableAwareBrokerSelector
controllerAPIReqUrl string
config *ControllerConfig
client HTTPClient
}
func (s *controllerBasedSelector) init() error {
if s.config.UpdateFreqMs == 0 {
s.config.UpdateFreqMs = defaultUpdateFreqMs
}
u, err := getControllerRequestUrl(s.config.ControllerAddress)
if err != nil {
log.Error(err)
return err
}
s.controllerAPIReqUrl = u
err = s.updateBrokerData()
if err != nil {
log.Errorf(
"An error occurred when fetching broker data from controller API for the first time, Error: %v",
err,
)
return err
}
go s.setupInterval()
return nil
}
func (s *controllerBasedSelector) setupInterval() {
lastInvocation := time.Now()
for {
nextInvocation := lastInvocation.Add(
time.Duration(s.config.UpdateFreqMs) * time.Millisecond,
)
untilNextInvocation := time.Until(nextInvocation)
time.Sleep(untilNextInvocation)
err := s.updateBrokerData()
if err != nil {
log.Errorf("Caught exception when updating broker data, Error: %v", err)
}
lastInvocation = time.Now()
}
}
func getControllerRequestUrl(controllerAddress string) (string, error) {
tokenized := strings.Split(controllerAddress, "://")
addressWithScheme := controllerAddress
if len(tokenized) > 1 {
scheme := tokenized[0]
if scheme != "https" && scheme != "http" {
return "", fmt.Errorf(
"Unsupported controller URL scheme: %s, only http (default) and https are allowed",
scheme,
)
}
} else {
addressWithScheme = "http://" + controllerAddress
}
return strings.TrimSuffix(addressWithScheme, "/") + controllerAPIEndpoint, nil
}
func (s *controllerBasedSelector) createControllerRequest() (*http.Request, error) {
r, err := http.NewRequest("GET", s.controllerAPIReqUrl, nil)
if err != nil {
return r, fmt.Errorf(
"Caught exception when creating controller API request: %v",
err,
)
}
for k, v := range controllerDefaultHTTPHeader {
r.Header.Add(k, v)
}
for k, v := range s.config.ExtraControllerAPIHeaders {
r.Header.Add(k, v)
}
return r, nil
}
func (s *controllerBasedSelector) updateBrokerData() error {
r, err := s.createControllerRequest()
if err != nil {
return err
}
resp, err := s.client.Do(r)
if err != nil {
return fmt.Errorf("Got exceptions while sending controller API request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("An error occurred when reading controller API response: %v", err)
}
var c controllerResponse
if err = decodeJsonWithNumber(bodyBytes, &c); err != nil {
return fmt.Errorf("An error occurred when decoding controller API response: %v", err)
}
s.rwMux.Lock()
s.allBrokerList = c.extractBrokerList()
s.tableBrokerMap = c.extractTableToBrokerMap()
s.rwMux.Unlock()
return nil
}
return fmt.Errorf("Controller API returned HTTP status code %v", resp.StatusCode)
}