-
Notifications
You must be signed in to change notification settings - Fork 2
/
metadata.go
228 lines (185 loc) · 6.37 KB
/
metadata.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
package client
import (
"fmt"
"github.com/yanzay/log"
"sort"
"sync"
"time"
)
// Metadata is a helper structure that provides access to topic/partition leaders and offset coordinators, caches them,
// refreshes and invalidates when necessary.
type Metadata struct {
Brokers *Brokers
kafka Client
metadataTTL time.Duration
metadata map[string]map[int32]int32
metadataExpires map[string]time.Time
metadataLock sync.RWMutex
//offset coordination part
offsetCoordinators map[string]int32
offsetCoordinatorLock sync.RWMutex
}
// NewMetadata creates a new Metadata for a given Client and Brokers with metadata cache TTL set to metadataTTL.
func NewMetadata(kafkaClient Client, brokers *Brokers, metadataTTL time.Duration) *Metadata {
return &Metadata{
Brokers: brokers,
kafka: kafkaClient,
metadataTTL: metadataTTL,
metadata: make(map[string]map[int32]int32),
metadataExpires: make(map[string]time.Time),
offsetCoordinators: make(map[string]int32),
}
}
// Leader tries to get a leader for a topic and partition from cache. Automatically refreshes if the leader information
// is missing or expired. May return an error if fails to get a leader for whatever reason.
func (m *Metadata) Leader(topic string, partition int32) (int32, error) {
topicMetadata, err := m.TopicMetadata(topic)
if err != nil {
return -1, err
}
leader, exists := topicMetadata[partition]
if !exists {
err := m.Refresh([]string{topic})
if err != nil {
m.Invalidate(topic)
return -1, err
}
topicMetadata, err = m.TopicMetadata(topic)
if err != nil {
m.Invalidate(topic)
return -1, err
}
leader, exists = topicMetadata[partition]
if !exists {
m.Invalidate(topic)
return -1, ErrFailedToGetLeader
}
}
return leader, nil
}
// TopicMetadata returns a map where keys are partitions of a topic and values are leader broker IDs.
// Automatically refreshes metadata if it is missing or expired.
// May return an error if fails to to get metadata for whatever reason.
func (m *Metadata) TopicMetadata(topic string) (map[int32]int32, error) {
topicMetadata, ttl := m.topicMetadata(topic)
if topicMetadata == nil || ttl.Add(m.metadataTTL).Before(time.Now()) {
err := m.refreshIfExpired(topic, ttl)
if err != nil {
return nil, err
}
topicMetadata, _ = m.topicMetadata(topic)
if topicMetadata != nil {
return topicMetadata, nil
}
return nil, ErrFailedToGetMetadata
}
return topicMetadata, nil
}
// PartitionsFor returns a sorted slice of partitions for a given topic.
// Automatically refreshes metadata if it is missing or expired.
// May return an error if fails to to get metadata for whatever reason.
func (m *Metadata) PartitionsFor(topic string) ([]int32, error) {
topicMetadata, err := m.TopicMetadata(topic)
if err != nil {
return nil, err
}
partitions := make([]int32, 0, len(topicMetadata))
for partition := range topicMetadata {
partitions = append(partitions, partition)
}
sort.Sort(int32Slice(partitions))
return partitions, nil
}
// Refresh forces metadata refresh for given topics.
// If the argument is empty or nil, metadata for all known topics by a Kafka cluster will be requested.
// May return an error if fails to to get metadata for whatever reason.
func (m *Metadata) Refresh(topics []string) error {
log.Infof("Refreshing metadata for topics %v", topics)
m.metadataLock.Lock()
defer m.metadataLock.Unlock()
return m.refresh(topics)
}
// Invalidate forcibly invalidates metadata cache for a given topic so that next time it is requested
// it is guaranteed to be refreshed.
func (m *Metadata) Invalidate(topic string) {
m.metadataLock.Lock()
defer m.metadataLock.Unlock()
m.metadataExpires[topic] = time.Unix(0, 0)
}
// OffsetCoordinator returns a BrokerConnection for an offset coordinator for a given group ID.
// May return an error if fails to to get metadata for whatever reason.
func (m *Metadata) OffsetCoordinator(group string) (*BrokerConnection, error) {
m.offsetCoordinatorLock.RLock()
coordinatorID, exists := m.offsetCoordinators[group]
m.offsetCoordinatorLock.RUnlock()
if !exists {
err := m.refreshOffsetCoordinator(group)
if err != nil {
return nil, err
}
m.offsetCoordinatorLock.RLock()
coordinatorID = m.offsetCoordinators[group]
m.offsetCoordinatorLock.RUnlock()
}
brokerConnection := m.Brokers.Get(coordinatorID)
if brokerConnection == nil {
return nil, fmt.Errorf("Could not find broker with node id %d", coordinatorID)
}
return brokerConnection, nil
}
func (m *Metadata) refreshOffsetCoordinator(group string) error {
m.offsetCoordinatorLock.Lock()
defer m.offsetCoordinatorLock.Unlock()
metadata, err := m.kafka.GetConsumerMetadata(group)
if err != nil {
return err
}
m.offsetCoordinators[group] = metadata.Coordinator.ID
return nil
}
func (m *Metadata) topicMetadata(topic string) (map[int32]int32, time.Time) {
m.metadataLock.RLock()
defer m.metadataLock.RUnlock()
metadataCopy := make(map[int32]int32)
for k, v := range m.metadata[topic] {
metadataCopy[k] = v
}
return metadataCopy, m.metadataExpires[topic]
}
func (m *Metadata) refreshIfExpired(topic string, ttl time.Time) error {
m.metadataLock.Lock()
defer m.metadataLock.Unlock()
if ttl.Add(m.metadataTTL).Before(time.Now()) {
return m.refresh([]string{topic})
}
return nil
}
func (m *Metadata) refresh(topics []string) error {
topicMetadataResponse, err := m.kafka.GetTopicMetadata(topics)
if err != nil {
return err
}
for _, broker := range topicMetadataResponse.Brokers {
m.Brokers.Update(broker)
}
for _, topicMetadata := range topicMetadataResponse.TopicsMetadata {
if topicMetadata.Error != ErrNoError {
return topicMetadata.Error
}
partitionLeaders := make(map[int32]int32)
for _, partitionMetadata := range topicMetadata.PartitionsMetadata {
if partitionMetadata.Error != ErrNoError {
return partitionMetadata.Error
}
partitionLeaders[partitionMetadata.PartitionID] = partitionMetadata.Leader
}
m.metadata[topicMetadata.Topic] = partitionLeaders
m.metadataExpires[topicMetadata.Topic] = time.Now()
log.Debugf("Received metadata: partitions %v for topic %s", partitionLeaders, topicMetadata.Topic)
}
return nil
}
type int32Slice []int32
func (p int32Slice) Len() int { return len(p) }
func (p int32Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p int32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }