-
Notifications
You must be signed in to change notification settings - Fork 18
/
brokers.go
389 lines (340 loc) · 12.6 KB
/
brokers.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//TODO referesh metadata when running into error
package healer
import (
"errors"
"fmt"
"strings"
"sync"
"time"
)
type Brokers struct {
config *BrokerConfig
bootstrapServers string
brokersInfo map[int32]*BrokerInfo
brokers map[int32]*Broker
controllerID int32
mutex sync.Locker
closeChan chan struct{}
}
// Close close all brokers
func (brokers *Brokers) Close() {
close(brokers.closeChan)
for _, broker := range brokers.brokers {
broker.Close()
}
}
// BrokersInfo returns brokers info, it is a private member and should not be changed from outside
func (brokers *Brokers) BrokersInfo() map[int32]*BrokerInfo {
return brokers.brokersInfo
}
// NewBrokersWithConfig create a new broker with config
func NewBrokersWithConfig(bootstrapServers string, config *BrokerConfig) (*Brokers, error) {
logger.Info("create new brokers", "bootstrapServers", bootstrapServers, "config", config)
clientID := "healer-newbrokers"
for _, brokerAddr := range strings.Split(bootstrapServers, ",") {
broker, err := NewBroker(brokerAddr, -1, config)
// TODO conn not established?
if err != nil {
logger.Error(err, "create broker", "address", brokerAddr)
} else {
brokers, err := newBrokersFromOne(broker, clientID, config)
if err != nil {
logger.Error(err, "could not create broker list", "address", broker)
} else {
brokers.bootstrapServers = bootstrapServers
return brokers, nil
}
}
}
return nil, fmt.Errorf("could not get any available broker from %s", bootstrapServers)
}
// NewBrokers create a new broker with default config
func NewBrokers(bootstrapServers string) (*Brokers, error) {
return NewBrokersWithConfig(bootstrapServers, DefaultBrokerConfig())
}
// get all brokers meda info from MetaData api
// it DON'T create connection to each broker
func newBrokersFromOne(broker *Broker, clientID string, config *BrokerConfig) (*Brokers, error) {
brokers := &Brokers{
config: config,
brokersInfo: make(map[int32]*BrokerInfo),
brokers: make(map[int32]*Broker),
mutex: &sync.Mutex{},
closeChan: make(chan struct{}, 0),
}
topics := make([]string, 0)
metadataResponse, err := broker.requestMetaData(clientID, topics)
if err != nil {
return nil, err
}
if len(metadataResponse.Brokers) == 0 {
return nil, errors.New("no brokers in getmetadata response")
}
brokers.mutex.Lock()
defer brokers.mutex.Unlock()
brokers.controllerID = metadataResponse.ControllerID
for _, brokerInfo := range metadataResponse.Brokers {
brokers.brokersInfo[brokerInfo.NodeID] = brokerInfo
if broker.GetAddress() == brokerInfo.NetAddress() {
brokers.brokers[brokerInfo.NodeID] = broker
}
}
for nodeID, broker := range brokers.brokersInfo {
logger.V(3).Info("brokers in cluster", "nodeID", nodeID, "host", broker.Host, "port", broker.Port)
}
return brokers, nil
}
// Controller return controller broker id
func (brokers *Brokers) Controller() int32 {
return brokers.controllerID
}
func (brokers *Brokers) GetController() (*Broker, error) {
return brokers.GetBroker(brokers.Controller())
}
func (brokers *Brokers) refreshMetadata() bool {
topics := make([]string, 0)
clientID := "healer-refresh-metadata"
// from origianl bootstrapServers
for _, brokerAddr := range strings.Split(brokers.bootstrapServers, ",") {
broker, err := NewBroker(brokerAddr, -1, brokers.config)
if err != nil {
logger.Error(err, "create broker failed", "brokerAddress", brokerAddr)
continue
}
metadataResponse, err := broker.requestMetaData(clientID, topics)
if len(metadataResponse.Brokers) == 0 {
logger.Info("no broker returned from metadata response")
broker.Close()
continue
}
brokersInfo := make(map[int32]*BrokerInfo)
for _, brokerInfo := range metadataResponse.Brokers {
brokersInfo[brokerInfo.NodeID] = brokerInfo
}
brokers.brokersInfo = brokersInfo
for nodeID, broker := range brokers.brokersInfo {
logger.V(3).Info("brokers in cluster", "nodeID", nodeID, "host", broker.Host, "port", broker.Port)
}
}
logger.Info("update metadata from latest brokersInfo")
// from latest brokersinfo
for _, brokerInfo := range brokers.brokersInfo {
brokerAddr := brokerInfo.NetAddress()
broker, err := NewBroker(brokerAddr, -1, brokers.config)
if err != nil {
logger.Error(err, "create broker failed", "brokerAddress", brokerAddr)
continue
}
metadataResponse, err := broker.requestMetaData(clientID, topics)
if len(metadataResponse.Brokers) == 0 {
logger.Info("no broker returned from metadata response")
continue
}
brokersInfo := make(map[int32]*BrokerInfo)
for _, brokerInfo := range metadataResponse.Brokers {
brokersInfo[brokerInfo.NodeID] = brokerInfo
}
brokers.brokersInfo = brokersInfo
for nodeID, broker := range brokers.brokersInfo {
logger.V(3).Info("brokers in cluster", "nodeID", nodeID, "host", broker.Host, "port", broker.Port)
}
return true
}
return false
}
// TODO merge with GetBroker
func (brokers *Brokers) NewBroker(nodeID int32) (*Broker, error) {
if _, ok := brokers.brokersInfo[nodeID]; !ok {
logger.Info("could not get broker from cache, referesh medadata", "nodeID", nodeID)
if !brokers.refreshMetadata() {
logger.Info("failed to refresh metadata")
}
}
// try again after refereshing metadata
if brokerInfo, ok := brokers.brokersInfo[nodeID]; ok {
broker, err := NewBroker(brokerInfo.NetAddress(), brokerInfo.NodeID, brokers.config)
if err == nil {
return broker, nil
}
return nil, fmt.Errorf("could not create broker for node[%d](%s):%w", nodeID, brokerInfo.NetAddress(), err)
} else {
return nil, fmt.Errorf("could not get broker info with nodeID[%d]", nodeID)
}
}
// GetBroker returns broker from cache or create a new one
func (brokers *Brokers) GetBroker(nodeID int32) (*Broker, error) {
brokers.mutex.Lock()
defer brokers.mutex.Unlock()
if broker, ok := brokers.brokers[nodeID]; ok {
return broker, nil
}
if _, ok := brokers.brokersInfo[nodeID]; !ok {
logger.Info("could not get broker from cache, referesh medadata", "nodeID", nodeID)
if !brokers.refreshMetadata() {
logger.Info("failed to refresh metadata")
}
}
if brokerInfo, ok := brokers.brokersInfo[nodeID]; ok {
broker, err := NewBroker(brokerInfo.NetAddress(), brokerInfo.NodeID, brokers.config)
if err == nil {
brokers.brokers[nodeID] = broker
return broker, nil
} else {
return nil, fmt.Errorf("could not init broker for node[%d](%s):%w", nodeID, brokerInfo.NetAddress(), err)
}
} else {
return nil, fmt.Errorf("could not get broker info with nodeID[%d]", nodeID)
}
}
func (brokers *Brokers) RequestMetaData(clientID string, topics []string) (r MetadataResponse, err error) {
for _, brokerInfo := range brokers.brokersInfo {
broker, err := brokers.GetBroker(brokerInfo.NodeID)
if err != nil {
logger.Error(err, "get broker failed", "host", brokerInfo.Host, "port", brokerInfo.Port)
continue
}
r, err = broker.requestMetaData(clientID, topics)
if err == nil {
return r, nil
}
var e KafkaError
if errors.As(err, &e) {
return r, err
}
logger.Error(err, "get metadata failed", "topics", topics, "brokerAddress", broker.address)
time.Sleep(time.Millisecond * 200)
}
return r, &noAvaliableBrokers
}
// RequestOffsets return the offset values array. return all partitions if partitionID < 0
func (brokers *Brokers) RequestOffsets(clientID, topic string, partitionID int32, timeValue int64, offsets uint32) ([]OffsetsResponse, error) {
// have to find which leader own the partition by request metadata
// TODO cache
metadataResponse, err := brokers.RequestMetaData(clientID, []string{topic})
if err != nil {
return nil, fmt.Errorf("could not get metadata of topic[%s]:%w", topic, err)
}
// TODO only one topic
topicMetadata := metadataResponse.TopicMetadatas[0]
if partitionID >= 0 {
for _, x := range topicMetadata.PartitionMetadatas {
if partitionID == x.PartitionID {
if leader, err := brokers.GetBroker(x.Leader); err != nil {
return nil, fmt.Errorf("could not find leader of %s[%d]:%w", topic, partitionID, err)
} else {
offsetsResponse, err := leader.requestOffsets(clientID, topic, []int32{partitionID}, timeValue, offsets)
if err != nil {
return nil, err
} else {
return []OffsetsResponse{offsetsResponse}, nil
}
}
}
}
return nil, fmt.Errorf("could not find partition %d in topic %s", partitionID, topic)
}
// try to get all partition offsets
offsetsRequestsMapping := make(map[int32][]int32, 0) //nodeID: partitionIDs
for _, x := range topicMetadata.PartitionMetadatas {
offsetsRequestsMapping[x.Leader] = append(offsetsRequestsMapping[x.Leader], x.PartitionID)
}
rst := make([]OffsetsResponse, 0)
for leaderID, partitionIDs := range offsetsRequestsMapping {
if leader, err := brokers.GetBroker(leaderID); err != nil {
return nil, fmt.Errorf("could not find leader of %s[%v]:%w", topic, partitionIDs, err)
} else {
offsetsResponse, err := leader.requestOffsets(clientID, topic, partitionIDs, timeValue, offsets)
if err != nil {
// TODO display error for the partition and go on?
return nil, err
}
rst = append(rst, offsetsResponse)
}
}
return rst, nil
}
func (brokers *Brokers) findLeader(clientID, topic string, partitionID int32) (int32, error) {
metadataResponse, err := brokers.RequestMetaData(clientID, []string{topic})
var e KafkaError
if err != nil && !errors.As(err, &e) {
return -1, fmt.Errorf("could not get metadata of topic %s: %w", topic, err)
}
partitionMetadatas := metadataResponse.TopicMetadatas[0].PartitionMetadatas
for _, partitionMetadata := range partitionMetadatas {
if partitionMetadata.PartitionID == partitionID {
return partitionMetadata.Leader, nil
}
}
return -1, fmt.Errorf("could not find out leader of topic %s", topic)
}
// FindCoordinator try to requests FindCoordinator from all brokers and returns response
func (brokers *Brokers) FindCoordinator(clientID, groupID string) (r FindCoordinatorResponse, err error) {
var broker *Broker
for _, brokerInfo := range brokers.brokersInfo {
broker, err = brokers.GetBroker(brokerInfo.NodeID)
if err != nil {
logger.Error(err, "get broker failed", "nodeId", brokerInfo.NodeID)
continue
}
r, err = broker.findCoordinator(clientID, groupID)
if err != nil {
logger.Error(err, "could not find coordinator", "coordinator", broker.address)
} else {
return
}
}
return FindCoordinatorResponse{}, fmt.Errorf("could not find coordinator from all brokers")
}
// ListPartitionReassignments requests ListPartitionReassignments from controller and returns response
func (brokers *Brokers) ListPartitionReassignments(req ListPartitionReassignmentsRequest) (r *ListPartitionReassignmentsResponse, err error) {
controller, err := brokers.GetBroker(brokers.Controller())
if err != nil {
return r, fmt.Errorf("could not create controller broker: %w", err)
}
resp, err := controller.RequestAndGet(req)
if err != nil {
return r, fmt.Errorf("could not get ListPartitionReassignments response from controller: %w", err)
}
return resp.(*ListPartitionReassignmentsResponse), nil
}
// AlterPartitionReassignments requests AlterPartitionReassignments from controller and returns response
func (brokers *Brokers) AlterPartitionReassignments(req *AlterPartitionReassignmentsRequest) (r *AlterPartitionReassignmentsResponse, err error) {
controller, err := brokers.GetBroker(brokers.Controller())
if err != nil {
return r, fmt.Errorf("could not create controller broker: %w", err)
}
resp, err := controller.RequestAndGet(req)
if err != nil {
return r, fmt.Errorf("could not get AlterPartitionReassignments response from controller: %w", err)
}
return resp.(*AlterPartitionReassignmentsResponse), nil
}
// ElectLeaders requests ElectLeaders from controller and returns response
func (brokers *Brokers) ElectLeaders(req *ElectLeadersRequest) (r *ElectLeadersResponse, err error) {
controller, err := brokers.GetBroker(brokers.Controller())
if err != nil {
return r, fmt.Errorf("could not create controller broker: %w", err)
}
resp, err := controller.RequestAndGet(req)
if err != nil {
return r, fmt.Errorf("could not get ElectLeaders response from controller: %w", err)
}
return resp.(*ElectLeadersResponse), nil
}
// Request try to do request from all brokers until get the response
func (brokers *Brokers) Request(req Request) (Response, error) {
for _, brokerInfo := range brokers.brokersInfo {
broker, err := brokers.GetBroker(brokerInfo.NodeID)
if err != nil {
continue
}
resp, err := broker.RequestAndGet(req)
if err != nil {
logger.Error(err, "request failed", "request", req.API(), "brokerAddress", broker.address)
continue
} else {
return resp, nil
}
}
return nil, fmt.Errorf("could not request %d from all brokers", req.API())
}