forked from hanguofeng/taiji
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathunbound_arbiter.go
209 lines (171 loc) · 5.46 KB
/
unbound_arbiter.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
package main
import (
"sync"
"sync/atomic"
"time"
"github.com/Shopify/sarama"
"github.com/golang/glog"
)
type UnboundArbiter struct {
*StartStopControl
// input/output
offsets chan int64
messages chan *sarama.ConsumerMessage
// parent
manager *PartitionManager
// config
config *CallbackItemConfig
arbiterConfig ArbiterConfig
// unbound window context
offsetBase int64
offsetWindow []bool
// stat variables
inflight uint64
uncommit uint64
processed uint64
startTime time.Time
}
func NewUnboundArbiter() Arbiter {
return &UnboundArbiter{
StartStopControl: NewStartStopControl(),
}
}
func (*UnboundArbiter) PreferredTransporterWorkerNum(workerNum int) int {
if workerNum <= 0 {
return 256
} else {
return workerNum
}
}
func (ua *UnboundArbiter) OffsetChannel() chan<- int64 {
return ua.offsets
}
func (ua *UnboundArbiter) MessageChannel() <-chan *sarama.ConsumerMessage {
return ua.messages
}
func (ua *UnboundArbiter) Init(config *CallbackItemConfig, arbiterConfig ArbiterConfig, manager *PartitionManager) error {
ua.manager = manager
ua.config = config
ua.arbiterConfig = arbiterConfig
return nil
}
func (ua *UnboundArbiter) Run() error {
if err := ua.ensureStart(); err != nil {
return err
}
defer ua.markStop()
consumer := ua.manager.GetKafkaPartitionConsumer()
ua.messages = make(chan *sarama.ConsumerMessage, 256)
ua.offsets = make(chan int64, 256)
ua.offsetBase = int64(-1)
ua.offsetWindow = make([]bool, 0, 10)
// reset stat variables
atomic.StoreUint64(&ua.inflight, 0)
atomic.StoreUint64(&ua.uncommit, 0)
atomic.StoreUint64(&ua.processed, 0)
ua.startTime = time.Now().Local()
ua.markReady()
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
arbiterOffsetLoop:
for {
select {
case <-ua.WaitForCloseChannel():
glog.V(1).Infof("Stop event triggered [url:%s]", ua.config.Url)
break arbiterOffsetLoop
case offset := <-ua.offsets:
glog.V(1).Infof("Read offset from Transporter [topic:%s][partition:%d][url:%s][offset:%d]",
ua.manager.Topic, ua.manager.Partition, ua.config.Url, offset)
if offset >= 0 {
// extend uncommit offset window
if offset-ua.offsetBase >= int64(len(ua.offsetWindow)) {
glog.V(1).Infof("Extend offsetWindow [topic:%s][partition:%d][url:%s][offset:%d][offsetBase:%d][offsetWindowSize:%d]",
ua.manager.Topic, ua.manager.Partition, ua.config.Url, offset, ua.offsetBase, len(ua.offsetWindow))
// extend offsetWindow
newOffsetWindow := make([]bool, (offset-ua.offsetBase+1)*2)
copy(newOffsetWindow, ua.offsetWindow)
ua.offsetWindow = newOffsetWindow
}
// decrement inflight, increment uncommit
atomic.AddUint64(&ua.inflight, ^uint64(0))
atomic.AddUint64(&ua.uncommit, 1)
ua.offsetWindow[offset-ua.offsetBase] = true
if offset == ua.offsetBase {
// rebase
advanceCount := 0
for advanceCount = 0; advanceCount != len(ua.offsetWindow); advanceCount++ {
if !ua.offsetWindow[advanceCount] {
break
}
}
// trigger offset commit
ua.manager.GetCallbackManager().GetOffsetManager().CommitOffset(
ua.manager.Topic, ua.manager.Partition, ua.offsetBase+int64(advanceCount)-1)
// rebase to idx + offsetBase
// TODO, use ring buffer instead of array slicing
ua.offsetBase += int64(advanceCount)
ua.offsetWindow = ua.offsetWindow[advanceCount:]
// decrement uncommit
atomic.AddUint64(&ua.uncommit, ^uint64(advanceCount-1))
glog.V(1).Infof("Fast-forward offsetBase [topic:%s][partition:%d][url:%s][originOffsetBase:%d][offsetBase:%d][offsetWindowSize:%d][advance:%d]",
ua.manager.Topic, ua.manager.Partition, ua.config.Url, ua.offsetBase-int64(advanceCount),
ua.offsetBase, len(ua.offsetWindow), advanceCount)
}
glog.V(1).Infof("Current unbound offset window status [topic:%s][partition:%d][url:%s][offsetBase:%d][inflight:%d][uncommit:%d]",
ua.manager.Topic, ua.manager.Partition, ua.config.Url, ua.offsetBase,
atomic.LoadUint64(&ua.inflight),
atomic.LoadUint64(&ua.uncommit))
}
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
offsetBaseInit := sync.Once{}
arbiterMessageLoop:
for {
select {
case <-ua.WaitForCloseChannel():
glog.V(1).Infof("Stop event triggered [url:%s]", ua.config.Url)
break arbiterMessageLoop
case message := <-consumer.Messages():
glog.V(1).Infof("Read message from PartitionConsumer [topic:%s][partition:%d][url:%s][offset:%d]",
message.Topic, message.Partition, ua.config.Url, message.Offset)
// set base
offsetBaseInit.Do(func() {
if -1 == ua.offsetBase {
ua.offsetBase = message.Offset
}
})
// feed message to transporter
select {
case <-ua.WaitForCloseChannel():
glog.V(1).Infof("Stop event triggered [url:%s]", ua.config.Url)
break arbiterMessageLoop
case ua.messages <- message:
}
// increment counter
atomic.AddUint64(&ua.inflight, 1)
atomic.AddUint64(&ua.processed, 1)
}
}
}()
wg.Wait()
return nil
}
func (ua *UnboundArbiter) GetStat() interface{} {
result := make(map[string]interface{})
result["inflight"] = atomic.LoadUint64(&ua.inflight)
result["uncommit"] = atomic.LoadUint64(&ua.uncommit)
result["processed"] = atomic.LoadUint64(&ua.processed)
result["window_base"] = ua.offsetBase
result["window_size"] = len(ua.offsetWindow)
result["start_time"] = ua.startTime
return result
}
func init() {
RegisterArbiter("Unbound", NewUnboundArbiter)
}