-
Notifications
You must be signed in to change notification settings - Fork 13
/
options.go
240 lines (207 loc) · 6.79 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
package bulkerlib
import (
"fmt"
"github.com/jitsucom/bulker/bulkerlib/types"
"github.com/jitsucom/bulker/jitsubase/jsoniter"
types2 "github.com/jitsucom/bulker/jitsubase/types"
"github.com/jitsucom/bulker/jitsubase/utils"
)
type StreamOption func(*StreamOptions)
var optionsRegistry = make(map[string]ParseableOption)
// Not used by bulker. Just added here to be treated as known options and don't print errors
var ignoredOptions = []string{"functions", "streams", "dataLayout", "events", "debugTill", "hosts", "schedule", "timezone", "storageKey", "tableNamePrefix", "multithreading"}
var (
BatchSizeOption = ImplementationOption[int]{
Key: "batchSize",
DefaultValue: 0,
ParseFunc: utils.ParseInt,
}
// BatchFrequencyOption frequency of running batches in minutes
BatchFrequencyOption = ImplementationOption[float64]{
Key: "frequency",
DefaultValue: 0,
ParseFunc: utils.ParseFloat,
}
RetryBatchSizeOption = ImplementationOption[int]{
Key: "retryBatchSize",
DefaultValue: 0,
ParseFunc: utils.ParseInt,
}
// RetryFrequencyOption frequency of running retry consumer in minutes
RetryFrequencyOption = ImplementationOption[float64]{
Key: "retryFrequency",
DefaultValue: 0,
ParseFunc: utils.ParseFloat,
}
ModeOption = ImplementationOption[BulkMode]{Key: "mode", ParseFunc: func(serialized any) (BulkMode, error) {
switch v := serialized.(type) {
case string:
if v == "stream" {
return Stream, nil
} else if v == "batch" {
return Batch, nil
} else {
return Unknown, fmt.Errorf("unknown mode: %s", v)
}
default:
return Unknown, fmt.Errorf("invalid value type of mode option: %T", v)
}
},
}
PrimaryKeyOption = ImplementationOption[types2.Set[string]]{
Key: "primaryKey",
DefaultValue: types2.Set[string]{},
AdvancedParseFunc: func(o *ImplementationOption[types2.Set[string]], serializedValue any) (StreamOption, error) {
switch v := serializedValue.(type) {
case []string:
return withPrimaryKey(o, v...), nil
case string:
if v == "" {
return func(options *StreamOptions) {}, nil
}
return withPrimaryKey(o, v), nil
default:
return nil, fmt.Errorf("failed to parse 'primaryKey' option: %v incorrect type: %T expected string or []string", v, v)
}
},
}
DeduplicateOption = ImplementationOption[bool]{
Key: "deduplicate",
DefaultValue: false,
ParseFunc: utils.ParseBool,
}
PartitionIdOption = ImplementationOption[string]{
Key: "partitionId",
ParseFunc: utils.ParseString,
}
// TimestampOption - field name that contains timestamp. For creating sorting indexes or partitions by that field in destination tables
TimestampOption = ImplementationOption[string]{
Key: "timestampColumn",
ParseFunc: utils.ParseString,
}
SchemaOption = ImplementationOption[types.Schema]{
Key: "schema",
ParseFunc: func(serialized any) (types.Schema, error) {
switch v := serialized.(type) {
case types.Schema:
return v, nil
case string:
schema := types.Schema{}
err := jsoniter.Unmarshal([]byte(v), &schema)
if err != nil {
return types.Schema{}, fmt.Errorf("failed to parse schema: %v", err)
}
return schema, nil
default:
return types.Schema{}, fmt.Errorf("invalid value type of schema option: %T", v)
}
},
}
)
func init() {
RegisterOption(&ModeOption)
RegisterOption(&BatchSizeOption)
RegisterOption(&BatchFrequencyOption)
RegisterOption(&RetryFrequencyOption)
RegisterOption(&RetryBatchSizeOption)
RegisterOption(&PrimaryKeyOption)
RegisterOption(&DeduplicateOption)
RegisterOption(&PartitionIdOption)
RegisterOption(&TimestampOption)
RegisterOption(&SchemaOption)
dummyParse := func(_ any) (any, error) { return nil, nil }
for _, ignoredOption := range ignoredOptions {
RegisterOption(&ImplementationOption[any]{Key: ignoredOption, ParseFunc: dummyParse})
}
}
func RegisterOption[V any](option *ImplementationOption[V]) {
optionsRegistry[option.Key] = option
}
func ParseOption(name string, serialized any) (StreamOption, error) {
option, ok := optionsRegistry[name]
if !ok {
return nil, fmt.Errorf("unknown option %s", name)
}
return option.Parse(serialized)
}
type StreamOptions struct {
// Implementation options - map by option key. Values are parsed and validated
// Don't access this map directly, use 'Get' method of specific option instance. E.g. `PartitionIdOption.Get(&so)`
valuesMap map[string]any
// options slice. To pass to CreateStream method
Options []StreamOption
}
func (so *StreamOptions) Add(option StreamOption) {
option(so)
so.Options = append(so.Options, option)
}
type ParseableOption interface {
Parse(serialized any) (StreamOption, error)
}
type ImplementationOption[V any] struct {
Key string
DefaultValue V
ParseFunc func(serialized any) (V, error)
AdvancedParseFunc func(*ImplementationOption[V], any) (StreamOption, error)
}
func (io *ImplementationOption[V]) Parse(serializedValue any) (StreamOption, error) {
if io.ParseFunc != nil {
val, err := io.ParseFunc(serializedValue)
if err != nil {
return nil, fmt.Errorf("failed to parse '%s' option: %v", io.Key, err)
}
return func(options *StreamOptions) {
io.Set(options, val)
}, nil
} else {
return io.AdvancedParseFunc(io, serializedValue)
}
}
func (io *ImplementationOption[V]) Get(so *StreamOptions) V {
opt, ok := so.valuesMap[io.Key].(V)
if ok {
return opt
}
return io.DefaultValue
}
func (io *ImplementationOption[V]) Set(so *StreamOptions, value V) {
if so.valuesMap == nil {
so.valuesMap = map[string]any{io.Key: value}
} else {
so.valuesMap[io.Key] = value
}
}
func WithOption[T any](o *ImplementationOption[T], value T) StreamOption {
return func(options *StreamOptions) {
o.Set(options, value)
}
}
func withPrimaryKey(o *ImplementationOption[types2.Set[string]], pkFields ...string) StreamOption {
return func(options *StreamOptions) {
set := o.Get(options)
if len(set) == 0 {
o.Set(options, types2.NewSet(pkFields...))
} else {
set.PutAll(pkFields)
}
}
}
func WithPrimaryKey(pkFields ...string) StreamOption {
return withPrimaryKey(&PrimaryKeyOption, pkFields...)
}
// WithDeduplicate - when true merge rows on primary keys collision.
func WithDeduplicate() StreamOption {
return WithOption(&DeduplicateOption, true)
}
// WithPartition settings for bulker.ReplacePartition mode only
// partitionId - value of `__partition_id` for current BulkerStream e.g. id of current partition
// TODO: For bigquery require string in special format
func WithPartition(partitionId string) StreamOption {
return WithOption(&PartitionIdOption, partitionId)
}
func WithTimestamp(timestampField string) StreamOption {
return WithOption(&TimestampOption, timestampField)
}
func WithSchema(schema types.Schema) StreamOption {
return WithOption(&SchemaOption, schema)
}