forked from liftbridge-io/go-liftbridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.go
286 lines (250 loc) · 7.06 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
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
package liftbridge
import (
"context"
"errors"
"fmt"
"math/rand"
"sync"
"time"
proto "github.com/liftbridge-io/liftbridge-api/go"
)
// StreamInfo contains information for a Liftbridge stream.
type StreamInfo struct {
subject string
name string
partitions map[int32]*PartitionInfo
}
// GetPartition returns the partition info for the given partition id or nil if
// no such partition exists.
func (s *StreamInfo) GetPartition(id int32) *PartitionInfo {
return s.partitions[id]
}
// Partitions returns a map containing partition IDs and partitions for the
// stream.
func (s *StreamInfo) Partitions() map[int32]*PartitionInfo {
return s.partitions
}
// PartitionInfo contains information for a Liftbridge stream partition.
type PartitionInfo struct {
id int32
leader *BrokerInfo
replicas []*BrokerInfo
isr []*BrokerInfo
paused bool
}
// ID of the partition.
func (p *PartitionInfo) ID() int32 {
return p.id
}
// Replicas returns the list of brokers replicating the partition.
func (p *PartitionInfo) Replicas() []*BrokerInfo {
return p.replicas
}
// ISR returns the list of replicas currently in the in-sync replica set.
func (p *PartitionInfo) ISR() []*BrokerInfo {
return p.isr
}
// Leader returns the broker acting as leader for this partition or nil if
// there is no leader.
func (p *PartitionInfo) Leader() *BrokerInfo {
return p.leader
}
// Paused returns true if this partition is paused.
func (p *PartitionInfo) Paused() bool {
return p.paused
}
// BrokerInfo contains information for a Liftbridge cluster node.
type BrokerInfo struct {
id string
host string
port int32
}
// ID of the broker.
func (b *BrokerInfo) ID() string {
return b.id
}
// Host of the broker server.
func (b *BrokerInfo) Host() string {
return b.host
}
// Port of the broker server.
func (b *BrokerInfo) Port() int32 {
return b.port
}
// Addr returns <host>:<port> for the broker server.
func (b *BrokerInfo) Addr() string {
return fmt.Sprintf("%s:%d", b.host, b.port)
}
// Metadata contains an immutable snapshot of information for a cluster and
// subset of streams.
type Metadata struct {
lastUpdated time.Time
brokers map[string]*BrokerInfo
addrs map[string]struct{}
streams map[string]*StreamInfo
}
func newMetadata(brokers map[string]*BrokerInfo, streams map[string]*StreamInfo) *Metadata {
addrs := make(map[string]struct{}, len(brokers))
for _, broker := range brokers {
addrs[broker.Addr()] = struct{}{}
}
return &Metadata{
lastUpdated: time.Now(),
brokers: brokers,
addrs: addrs,
streams: streams,
}
}
// LastUpdated returns the time when this metadata was last updated from the
// server.
func (m *Metadata) LastUpdated() time.Time {
return m.lastUpdated
}
// Brokers returns a list of the cluster nodes.
func (m *Metadata) Brokers() []*BrokerInfo {
brokers := make([]*BrokerInfo, 0, len(m.brokers))
for _, broker := range m.brokers {
brokers = append(brokers, broker)
}
return brokers
}
// Addrs returns the list of known broker addresses.
func (m *Metadata) Addrs() []string {
addrs := make([]string, 0, len(m.addrs))
for addr := range m.addrs {
addrs = append(addrs, addr)
}
return addrs
}
// GetStream returns the given stream or nil if unknown.
func (m *Metadata) GetStream(name string) *StreamInfo {
return m.streams[name]
}
// Streams returns the list of known streams.
func (m *Metadata) Streams() []*StreamInfo {
streams := make([]*StreamInfo, 0, len(m.streams))
for _, stream := range m.streams {
streams = append(streams, stream)
}
return streams
}
// PartitionCountForStream returns the number of partitions for the given
// stream.
func (m *Metadata) PartitionCountForStream(stream string) int32 {
info := m.GetStream(stream)
if info == nil {
return 0
}
return int32(len(info.partitions))
}
// hasStreamMetadata indicates if the Metadata has info for the given stream.
func (m *Metadata) hasStreamMetadata(stream string) bool {
return m.GetStream(stream) != nil
}
type metadataCache struct {
mu sync.RWMutex
metadata *Metadata
bootstrapAddrs []string
doRPC func(func(proto.APIClient) error) error
}
func newMetadataCache(addrs []string, doRPC func(func(proto.APIClient) error) error) *metadataCache {
return &metadataCache{
metadata: &Metadata{},
bootstrapAddrs: addrs,
doRPC: doRPC,
}
}
// update fetches the latest cluster metadata, including stream and broker
// information.
func (m *metadataCache) update(ctx context.Context) (*Metadata, error) {
var resp *proto.FetchMetadataResponse
if err := m.doRPC(func(client proto.APIClient) (err error) {
resp, err = client.FetchMetadata(ctx, &proto.FetchMetadataRequest{})
return err
}); err != nil {
return nil, err
}
brokers := make(map[string]*BrokerInfo, len(resp.Brokers))
for _, broker := range resp.Brokers {
brokers[broker.Id] = &BrokerInfo{
id: broker.Id,
host: broker.Host,
port: broker.Port,
}
}
streams := make(map[string]*StreamInfo)
for _, streamMetadata := range resp.StreamMetadata {
stream := &StreamInfo{
subject: streamMetadata.Subject,
name: streamMetadata.Name,
partitions: make(map[int32]*PartitionInfo, len(streamMetadata.Partitions)),
}
for _, partition := range streamMetadata.Partitions {
replicas := make([]*BrokerInfo, 0, len(partition.Replicas))
for _, replica := range partition.Replicas {
replicas = append(replicas, brokers[replica])
}
isr := make([]*BrokerInfo, 0, len(partition.Isr))
for _, replica := range partition.Isr {
isr = append(isr, brokers[replica])
}
stream.partitions[partition.Id] = &PartitionInfo{
id: partition.Id,
leader: brokers[partition.Leader],
replicas: replicas,
isr: isr,
paused: partition.Paused,
}
}
streams[stream.name] = stream
}
metadata := newMetadata(brokers, streams)
m.mu.Lock()
m.metadata = metadata
m.mu.Unlock()
return metadata, nil
}
// getAddrs returns a list of all broker addresses.
func (m *metadataCache) getAddrs() []string {
m.mu.RLock()
var (
metadata = m.metadata
bootstrapAddrs = m.bootstrapAddrs
)
m.mu.RUnlock()
addrs := metadata.Addrs()
for _, addr := range bootstrapAddrs {
addrs = append(addrs, addr)
}
return addrs
}
// getAddr returns the broker address for the given stream partition.
func (m *metadataCache) getAddr(stream string, partitionID int32, readISRReplica bool) (string, error) {
m.mu.RLock()
metadata := m.metadata
m.mu.RUnlock()
st := metadata.GetStream(stream)
if st == nil {
return "", errors.New("no known stream")
}
partition := st.GetPartition(partitionID)
if partition == nil {
return "", errors.New("no known partition")
}
// Request to subscribe to a random ISR
if readISRReplica {
replicasISR := partition.ISR()
randomReplica := replicasISR[rand.Intn(len(replicasISR))]
return randomReplica.Addr(), nil
}
if partition.Leader() == nil {
return "", errors.New("no known leader for partition")
}
return partition.Leader().Addr(), nil
}
// get returns the current Metadata.
func (m *metadataCache) get() *Metadata {
m.mu.RLock()
defer m.mu.RUnlock()
return m.metadata
}