-
Notifications
You must be signed in to change notification settings - Fork 11
/
consumer_async.go
190 lines (160 loc) · 4.21 KB
/
consumer_async.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
package queue
import (
"fmt"
"sync"
"time"
"github.com/deepfabric/busybee/pkg/metric"
"github.com/deepfabric/busybee/pkg/pb/metapb"
"github.com/deepfabric/busybee/pkg/pb/rpcpb"
"github.com/deepfabric/busybee/pkg/storage"
"github.com/fagongzi/util/protoc"
)
var (
scanSize = uint64(128)
)
// AsyncConsumer async consumer
type AsyncConsumer interface {
// Start start the consumer
Start(cb func(uint32, uint64, [][]byte))
// Commit commit completed offset
Commit(map[uint32]uint64, func(error))
// CommitPartition commit completed offset
CommitPartition(uint32, uint64, func(error))
// Stop stop consumer
Stop()
}
type asyncConsumer struct {
sync.RWMutex
tid uint64
tenant string
group metapb.Group
store storage.Storage
consumer []byte
offsets []uint64
state metapb.TenantQueue
running bool
}
// NewAsyncConsumer create a async consumer
func NewAsyncConsumer(tid uint64, store storage.Storage, consumer []byte) (AsyncConsumer, error) {
return NewAsyncConsumerWithOffsets(tid, store, consumer, nil)
}
// NewAsyncConsumerWithOffsets create a async consumer with offsets
func NewAsyncConsumerWithOffsets(tid uint64, store storage.Storage, consumer []byte, offsets []uint64) (AsyncConsumer, error) {
return newAsyncConsumerWithGroup(tid, store, consumer, metapb.TenantInputGroup, offsets)
}
func newAsyncConsumerWithGroup(tid uint64, store storage.Storage, consumer []byte, group metapb.Group, offsets []uint64) (AsyncConsumer, error) {
value, err := store.Get(storage.TenantMetadataKey(tid))
if err != nil {
metric.IncStorageFailed()
return nil, err
}
if len(value) == 0 {
return nil, fmt.Errorf("tenant %d not created",
tid)
}
metadata := &metapb.Tenant{}
protoc.MustUnmarshal(metadata, value)
if offsets == nil {
offsets = make([]uint64, metadata.Input.Partitions, metadata.Input.Partitions)
}
return &asyncConsumer{
tid: tid,
tenant: fmt.Sprintf("%d", tid),
group: group,
store: store,
consumer: consumer,
state: metadata.Input,
offsets: offsets,
}, nil
}
// Start start the consumer
func (c *asyncConsumer) Start(cb func(uint32, uint64, [][]byte)) {
c.Lock()
defer c.Unlock()
if c.running {
return
}
c.running = true
for i := uint32(0); i < c.state.Partitions; i++ {
go func(p uint32, startWith uint64) {
tag := fmt.Sprintf("%d/%s/p(%d)/c(%s)",
c.tid,
c.group.String(),
p,
string(c.consumer))
from := startWith
key := storage.PartitionKey(c.tid, p)
resp := &rpcpb.QueueFetchResponse{}
for {
if c.stopped() {
logger.Infof("%s stopped", tag)
return
}
req := rpcpb.AcquireQueueScanRequest()
req.Key = key
req.CompletedOffset = from
req.Count = scanSize
req.Consumer = c.consumer
value, err := c.store.ExecCommandWithGroup(req, c.group)
if err != nil {
metric.IncStorageFailed()
logger.Errorf("%s failed with %+v, retry after 10s",
tag,
err)
time.Sleep(time.Second * 10)
continue
}
resp.Reset()
protoc.MustUnmarshal(resp, value)
if len(resp.Items) == 0 {
time.Sleep(time.Second)
continue
}
cb(p, resp.LastOffset, resp.Items)
from = resp.LastOffset
metric.IncEventHandled(len(resp.Items), c.tenant, c.group)
}
}(i, c.offsets[i])
}
}
func (c *asyncConsumer) Commit(completed map[uint32]uint64, ucb func(error)) {
if len(completed) == 0 {
return
}
cb := func(arg interface{}, data []byte, err error) {
if ucb != nil {
ucb(err)
}
}
for p, offset := range completed {
req := rpcpb.AcquireQueueCommitRequest()
req.ID = c.tid
req.Consumer = c.consumer
req.Partition = p
req.CompletedOffset = offset
c.store.AsyncExecCommandWithGroup(req, c.group, cb, nil)
}
}
func (c *asyncConsumer) CommitPartition(p uint32, offset uint64, ucb func(error)) {
cb := func(arg interface{}, data []byte, err error) {
if ucb != nil {
ucb(err)
}
}
req := rpcpb.AcquireQueueCommitRequest()
req.ID = c.tid
req.Consumer = c.consumer
req.Partition = p
req.CompletedOffset = offset
c.store.AsyncExecCommandWithGroup(req, c.group, cb, nil)
}
func (c *asyncConsumer) Stop() {
c.Lock()
defer c.Unlock()
c.running = false
}
func (c *asyncConsumer) stopped() bool {
c.RLock()
defer c.RUnlock()
return !c.running
}