/
cluster.go
134 lines (122 loc) · 2.95 KB
/
cluster.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
package cluster
import (
"errors"
"fmt"
"sync"
"h12.io/kpax/model"
"h12.io/kpax/proto"
)
var (
ErrLeaderNotFound = errors.New("leader not found")
ErrCoordNotFound = errors.New("coordinator not found")
ErrNoBrokerFound = errors.New("no broker found")
)
type (
C struct {
topics *topicPartitions
pool *brokerPool
mu sync.Mutex
}
NewBrokerFunc func(addr string) model.Broker
)
func New(newBroker NewBrokerFunc, brokers []string) model.Cluster {
c := &C{
topics: newTopicPartitions(),
pool: newBrokerPool(newBroker),
}
for _, addr := range brokers {
c.pool.AddAddr(addr)
}
return c
}
func (c *C) Coordinator(group string) (model.Broker, error) {
if coord, err := c.pool.GetCoordinator(group); err == nil {
return coord, nil
}
if err := c.updateCoordinator(group); err != nil {
return nil, err
}
return c.pool.GetCoordinator(group)
}
func (c *C) CoordinatorIsDown(group string) {
c.pool.DeleteCoordinator(group)
}
func (c *C) Leader(topic string, partition int32) (model.Broker, error) {
if leader, err := c.pool.GetLeader(topic, partition); err == nil {
return leader, nil
}
if err := c.updateFromTopicMetadata(topic); err != nil {
return nil, err
}
return c.pool.GetLeader(topic, partition)
}
func (c *C) LeaderIsDown(topic string, partition int32) {
c.pool.DeleteLeader(topic, partition)
}
func (c *C) Partitions(topic string) ([]int32, error) {
partitions := c.topics.getPartitions(topic)
if len(partitions) > 0 {
return partitions, nil
}
if err := c.updateFromTopicMetadata(topic); err != nil {
return nil, err
}
partitions = c.topics.getPartitions(topic)
if len(partitions) > 0 {
return partitions, nil
}
return nil, fmt.Errorf("topic %s not found", topic)
}
func (c *C) updateCoordinator(group string) error {
brokers, err := c.pool.Brokers()
if err != nil {
return err
}
var merr MultiError
for _, broker := range brokers {
coord, err := proto.GroupCoordinator(group).Fetch(broker)
if err != nil {
merr.Add(err)
continue
}
c.pool.SetCoordinator(group, coord.NodeID, coord.Addr())
return nil
}
return merr
}
func (c *C) updateFromTopicMetadata(topic string) error {
brokers, err := c.pool.Brokers()
if err != nil {
return err
}
var merr MultiError
for _, broker := range brokers {
// no retry, fail fast
m, err := proto.Metadata(topic).Fetch(broker)
if err != nil {
merr.Add(err)
continue
}
for i := range m.Brokers {
b := &m.Brokers[i]
c.pool.Add(b.NodeID, b.Addr())
}
for i := range m.TopicMetadatas {
t := &m.TopicMetadatas[i]
if t.TopicName == topic {
partitions := make([]int32, len(t.PartitionMetadatas))
for i := range t.PartitionMetadatas {
partition := &t.PartitionMetadatas[i]
partitions[i] = partition.PartitionID
if err := c.pool.SetLeader(topic, partition.PartitionID, partition.Leader); err != nil {
merr.Add(err)
continue
}
}
c.topics.addPartitions(topic, partitions)
return nil
}
}
}
return merr
}