forked from bsm/sarama-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partitions.go
213 lines (177 loc) · 3.91 KB
/
partitions.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
package cluster
import (
"sort"
"sync"
"gopkg.in/Shopify/sarama.v1"
)
type partitionConsumer struct {
pcm sarama.PartitionConsumer
state partitionState
mutex sync.Mutex
closed bool
dying, dead chan none
}
func newPartitionConsumer(manager sarama.Consumer, topic string, partition int32, info offsetInfo, defaultOffset int64) (*partitionConsumer, error) {
pcm, err := manager.ConsumePartition(topic, partition, info.NextOffset(defaultOffset))
// Resume from default offset, if requested offset is out-of-range
if err == sarama.ErrOffsetOutOfRange {
info.Offset = -1
pcm, err = manager.ConsumePartition(topic, partition, defaultOffset)
}
if err != nil {
return nil, err
}
return &partitionConsumer{
pcm: pcm,
state: partitionState{Info: info},
dying: make(chan none),
dead: make(chan none),
}, nil
}
func (c *partitionConsumer) Loop(messages chan<- *sarama.ConsumerMessage, errors chan<- error) {
for {
select {
case msg := <-c.pcm.Messages():
select {
case messages <- msg:
case <-c.dying:
close(c.dead)
return
}
case err := <-c.pcm.Errors():
select {
case errors <- err:
case <-c.dying:
close(c.dead)
return
}
case <-c.dying:
close(c.dead)
return
}
}
}
func (c *partitionConsumer) Close() (err error) {
if c.closed {
return
}
c.closed = true
close(c.dying)
<-c.dead
if e := c.pcm.Close(); e != nil {
err = e
}
return
}
func (c *partitionConsumer) State() partitionState {
if c == nil {
return partitionState{}
}
c.mutex.Lock()
state := c.state
c.mutex.Unlock()
return state
}
func (c *partitionConsumer) MarkCommitted(offset int64) {
if c == nil {
return
}
c.mutex.Lock()
if offset == c.state.Info.Offset {
c.state.Dirty = false
}
c.mutex.Unlock()
}
func (c *partitionConsumer) MarkOffset(offset int64, metadata string) {
if c == nil {
return
}
c.mutex.Lock()
if offset > c.state.Info.Offset {
c.state.Info.Offset = offset
c.state.Info.Metadata = metadata
c.state.Dirty = true
}
c.mutex.Unlock()
}
// --------------------------------------------------------------------
type partitionState struct {
Info offsetInfo
Dirty bool
}
// --------------------------------------------------------------------
type partitionMap struct {
data map[topicPartition]*partitionConsumer
mutex sync.RWMutex
}
func newPartitionMap() *partitionMap {
return &partitionMap{
data: make(map[topicPartition]*partitionConsumer),
}
}
func (m *partitionMap) Fetch(topic string, partition int32) *partitionConsumer {
m.mutex.RLock()
pc, _ := m.data[topicPartition{topic, partition}]
m.mutex.RUnlock()
return pc
}
func (m *partitionMap) Store(topic string, partition int32, pc *partitionConsumer) {
m.mutex.Lock()
m.data[topicPartition{topic, partition}] = pc
m.mutex.Unlock()
}
func (m *partitionMap) HasDirty() bool {
m.mutex.RLock()
defer m.mutex.RUnlock()
for _, pc := range m.data {
if state := pc.State(); state.Dirty {
return true
}
}
return false
}
func (m *partitionMap) Snapshot() map[topicPartition]partitionState {
m.mutex.RLock()
defer m.mutex.RUnlock()
snap := make(map[topicPartition]partitionState, len(m.data))
for tp, pc := range m.data {
snap[tp] = pc.State()
}
return snap
}
func (m *partitionMap) Stop() (err error) {
m.mutex.RLock()
size := len(m.data)
errs := make(chan error, size)
for tp := range m.data {
go func(p *partitionConsumer) {
errs <- p.Close()
}(m.data[tp])
}
m.mutex.RUnlock()
for i := 0; i < size; i++ {
if e := <-errs; e != nil {
err = e
}
}
return
}
func (m *partitionMap) Clear() {
m.mutex.Lock()
for tp := range m.data {
delete(m.data, tp)
}
m.mutex.Unlock()
}
func (m *partitionMap) Info() map[string][]int32 {
info := make(map[string][]int32)
m.mutex.RLock()
for tp := range m.data {
info[tp.Topic] = append(info[tp.Topic], tp.Partition)
}
m.mutex.RUnlock()
for topic := range info {
sort.Sort(int32Slice(info[topic]))
}
return info
}