forked from lovoo/goka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
406 lines (341 loc) · 11.1 KB
/
options.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package goka
import (
"fmt"
"hash"
"hash/fnv"
"path/filepath"
"github.com/lovoo/goka/kafka"
"github.com/lovoo/goka/logger"
"github.com/lovoo/goka/storage"
)
// UpdateCallback is invoked upon arrival of a message for a table partition.
// The partition storage shall be updated in the callback.
type UpdateCallback func(s storage.Storage, partition int32, key string, value []byte) error
///////////////////////////////////////////////////////////////////////////////
// default values
///////////////////////////////////////////////////////////////////////////////
const (
defaultBaseStoragePath = "/tmp/goka"
defaultClientID = "goka"
)
// DefaultProcessorStoragePath is the default path where processor state
// will be stored.
func DefaultProcessorStoragePath(group Group) string {
return filepath.Join(defaultBaseStoragePath, "processor", string(group))
}
// DefaultViewStoragePath returns the default path where view state will be stored.
func DefaultViewStoragePath() string {
return filepath.Join(defaultBaseStoragePath, "view")
}
// DefaultUpdate is the default callback used to update the local storage with
// from the table topic in Kafka. It is called for every message received
// during recovery of processors and during the normal operation of views.
// DefaultUpdate can be used in the function passed to WithUpdateCallback and
// WithViewCallback.
func DefaultUpdate(s storage.Storage, partition int32, key string, value []byte) error {
if value == nil {
return s.Delete(key)
}
return s.Set(key, value)
}
// DefaultHasher returns an FNV hasher builder to assign keys to partitions.
func DefaultHasher() func() hash.Hash32 {
return func() hash.Hash32 {
return fnv.New32a()
}
}
///////////////////////////////////////////////////////////////////////////////
// processor options
///////////////////////////////////////////////////////////////////////////////
// ProcessorOption defines a configuration option to be used when creating a processor.
type ProcessorOption func(*poptions)
// processor options
type poptions struct {
log logger.Logger
clientID string
updateCallback UpdateCallback
partitionChannelSize int
hasher func() hash.Hash32
nilHandling NilHandling
builders struct {
storage storage.Builder
consumer kafka.ConsumerBuilder
producer kafka.ProducerBuilder
topicmgr kafka.TopicManagerBuilder
}
}
// WithUpdateCallback defines the callback called upon recovering a message
// from the log.
func WithUpdateCallback(cb UpdateCallback) ProcessorOption {
return func(o *poptions) {
o.updateCallback = cb
}
}
// WithClientID defines the client ID used to identify with Kafka.
func WithClientID(clientID string) ProcessorOption {
return func(o *poptions) {
o.clientID = clientID
}
}
// WithStorageBuilder defines a builder for the storage of each partition.
func WithStorageBuilder(sb storage.Builder) ProcessorOption {
return func(o *poptions) {
o.builders.storage = sb
}
}
// WithTopicManagerBuilder replaces the default topic manager builder.
func WithTopicManagerBuilder(tmb kafka.TopicManagerBuilder) ProcessorOption {
return func(o *poptions) {
o.builders.topicmgr = tmb
}
}
// WithConsumerBuilder replaces the default consumer builder.
func WithConsumerBuilder(cb kafka.ConsumerBuilder) ProcessorOption {
return func(o *poptions) {
o.builders.consumer = cb
}
}
// WithProducerBuilder replaces the default producer builder.
func WithProducerBuilder(pb kafka.ProducerBuilder) ProcessorOption {
return func(o *poptions) {
o.builders.producer = pb
}
}
// WithPartitionChannelSize replaces the default partition channel size.
// This is mostly used for testing by setting it to 0 to have synchronous behavior
// of goka.
func WithPartitionChannelSize(size int) ProcessorOption {
return func(o *poptions) {
o.partitionChannelSize = size
}
}
// WithLogger sets the logger the processor should use. By default, processors
// use the standard library logger.
func WithLogger(log logger.Logger) ProcessorOption {
return func(o *poptions) {
o.log = log
}
}
// WithHasher sets the hash function that assigns keys to partitions.
func WithHasher(hasher func() hash.Hash32) ProcessorOption {
return func(o *poptions) {
o.hasher = hasher
}
}
// NilHandling defines how nil messages should be handled by the processor.
type NilHandling int
const (
// NilIgnore drops any message with nil value.
NilIgnore NilHandling = 0 + iota
// NilProcess passes the nil value to ProcessCallback.
NilProcess
// NilDecode passes the nil value to decoder before calling ProcessCallback.
NilDecode
)
// WithNilHandling configures how the processor should handle messages with nil
// value. By default the processor ignores nil messages.
func WithNilHandling(nh NilHandling) ProcessorOption {
return func(o *poptions) {
o.nilHandling = nh
}
}
type Tester interface {
StorageBuilder() storage.Builder
ConsumerBuilder() kafka.ConsumerBuilder
ProducerBuilder() kafka.ProducerBuilder
TopicManagerBuilder() kafka.TopicManagerBuilder
}
// WithTester configures all external connections of a processor, ie, storage,
// consumer and producer
func WithTester(t Tester) ProcessorOption {
return func(o *poptions) {
o.builders.storage = t.StorageBuilder()
o.builders.consumer = t.ConsumerBuilder()
o.builders.producer = t.ProducerBuilder()
o.builders.topicmgr = t.TopicManagerBuilder()
o.partitionChannelSize = 0
}
}
func (opt *poptions) applyOptions(group string, opts ...ProcessorOption) error {
opt.clientID = defaultClientID
opt.log = logger.Default()
opt.hasher = DefaultHasher()
for _, o := range opts {
o(opt)
}
// StorageBuilder should always be set as a default option in NewProcessor
if opt.builders.storage == nil {
return fmt.Errorf("StorageBuilder not set")
}
if opt.builders.consumer == nil {
opt.builders.consumer = kafka.DefaultConsumerBuilder
}
if opt.builders.producer == nil {
opt.builders.producer = kafka.DefaultProducerBuilder
}
if opt.builders.topicmgr == nil {
opt.builders.topicmgr = kafka.DefaultTopicManagerBuilder
}
return nil
}
///////////////////////////////////////////////////////////////////////////////
// view options
///////////////////////////////////////////////////////////////////////////////
// ViewOption defines a configuration option to be used when creating a view.
type ViewOption func(*voptions)
type voptions struct {
log logger.Logger
clientID string
tableCodec Codec
updateCallback UpdateCallback
partitionChannelSize int
hasher func() hash.Hash32
restartable bool
builders struct {
storage storage.Builder
consumer kafka.ConsumerBuilder
topicmgr kafka.TopicManagerBuilder
}
}
// WithViewLogger sets the logger the view should use. By default, views
// use the standard library logger.
func WithViewLogger(log logger.Logger) ViewOption {
return func(o *voptions) {
o.log = log
}
}
// WithViewCallback defines the callback called upon recovering a message
// from the log.
func WithViewCallback(cb UpdateCallback) ViewOption {
return func(o *voptions) {
o.updateCallback = cb
}
}
// WithViewStorageBuilder defines a builder for the storage of each partition.
func WithViewStorageBuilder(sb storage.Builder) ViewOption {
return func(o *voptions) {
o.builders.storage = sb
}
}
// WithViewConsumerBuilder replaces default view consumer.
func WithViewConsumerBuilder(cb kafka.ConsumerBuilder) ViewOption {
return func(o *voptions) {
o.builders.consumer = cb
}
}
// WithViewTopicManagerBuilder replaces the default topic manager.
func WithViewTopicManagerBuilder(tmb kafka.TopicManagerBuilder) ViewOption {
return func(o *voptions) {
o.builders.topicmgr = tmb
}
}
// WithViewPartitionChannelSize replaces the default partition channel size.
// This is mostly used for testing by setting it to 0 to have synchronous behavior
// of goka.
func WithViewPartitionChannelSize(size int) ViewOption {
return func(o *voptions) {
o.partitionChannelSize = size
}
}
// WithViewHasher sets the hash function that assigns keys to partitions.
func WithViewHasher(hasher func() hash.Hash32) ViewOption {
return func(o *voptions) {
o.hasher = hasher
}
}
// WithViewClientID defines the client ID used to identify with Kafka.
func WithViewClientID(clientID string) ViewOption {
return func(o *voptions) {
o.clientID = clientID
}
}
// WithViewRestartable defines the view can be restarted, even when Run()
// returns errors. If the view is restartable, the client must call Terminate()
// to release all resources, ie, close the local storage.
func WithViewRestartable() ViewOption {
return func(o *voptions) {
o.restartable = true
}
}
func (opt *voptions) applyOptions(topic Table, opts ...ViewOption) error {
opt.clientID = defaultClientID
opt.log = logger.Default()
opt.hasher = DefaultHasher()
for _, o := range opts {
o(opt)
}
// StorageBuilder should always be set as a default option in NewView
if opt.builders.storage == nil {
return fmt.Errorf("StorageBuilder not set")
}
if opt.builders.consumer == nil {
opt.builders.consumer = kafka.DefaultConsumerBuilder
}
if opt.builders.topicmgr == nil {
opt.builders.topicmgr = kafka.DefaultTopicManagerBuilder
}
return nil
}
///////////////////////////////////////////////////////////////////////////////
// emitter options
///////////////////////////////////////////////////////////////////////////////
// EmitterOption defines a configuration option to be used when creating an
// emitter.
type EmitterOption func(*eoptions)
// emitter options
type eoptions struct {
log logger.Logger
clientID string
hasher func() hash.Hash32
builders struct {
topicmgr kafka.TopicManagerBuilder
producer kafka.ProducerBuilder
}
}
// WithEmitterLogger sets the logger the emitter should use. By default,
// emitters use the standard library logger.
func WithEmitterLogger(log logger.Logger) EmitterOption {
return func(o *eoptions) {
o.log = log
}
}
// WithEmitterClientID defines the client ID used to identify with kafka.
func WithEmitterClientID(clientID string) EmitterOption {
return func(o *eoptions) {
o.clientID = clientID
}
}
// WithEmitterTopicManagerBuilder replaces the default topic manager builder.
func WithEmitterTopicManagerBuilder(tmb kafka.TopicManagerBuilder) EmitterOption {
return func(o *eoptions) {
o.builders.topicmgr = tmb
}
}
// WithEmitterProducerBuilder replaces the default producer builder.
func WithEmitterProducerBuilder(pb kafka.ProducerBuilder) EmitterOption {
return func(o *eoptions) {
o.builders.producer = pb
}
}
// WithEmitterHasher sets the hash function that assigns keys to partitions.
func WithEmitterHasher(hasher func() hash.Hash32) EmitterOption {
return func(o *eoptions) {
o.hasher = hasher
}
}
func (opt *eoptions) applyOptions(opts ...EmitterOption) error {
opt.clientID = defaultClientID
opt.log = logger.Default()
opt.hasher = DefaultHasher()
for _, o := range opts {
o(opt)
}
// config not set, use default one
if opt.builders.producer == nil {
opt.builders.producer = kafka.DefaultProducerBuilder
}
if opt.builders.topicmgr == nil {
opt.builders.topicmgr = kafka.DefaultTopicManagerBuilder
}
return nil
}