-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduck.go
375 lines (356 loc) · 9.92 KB
/
duck.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
package quacfka
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/loicalleyne/couac"
"github.com/panjf2000/ants/v2"
"github.com/spf13/cast"
)
type duckConf struct {
quack *couac.Quacker
path string
pathPrefix string
driverPath string
destTable string
duckConnCount atomic.Int32
destTableIndex atomic.Int32
}
type duckJob struct {
quack *couac.Quacker
destTable string
rChan chan Record
wg *sync.WaitGroup
}
type (
DuckOption func(duckConfig)
duckConfig *duckConf
)
func WithPath(p string) DuckOption {
return func(cfg duckConfig) {
cfg.path = p
}
}
func WithPathPrefix(p string) DuckOption {
return func(cfg duckConfig) {
cfg.pathPrefix = p
}
}
func WithDriverPath(p string) DuckOption {
return func(cfg duckConfig) {
cfg.driverPath = p
}
}
func WithDestinationTable(p string) DuckOption {
return func(cfg duckConfig) {
cfg.destTable = p
}
}
func WithDuckConnections(p int) DuckOption {
return func(cfg duckConfig) {
cfg.duckConnCount.Store(int32(p))
}
}
func (o *Orchestrator[T]) ConfigureDuck(opts ...DuckOption) error {
var err error
if o.duckConf != nil && o.duckConf.quack != nil {
o.duckConf.quack.Close()
}
d := new(duckConf)
d.duckConnCount.Store(1)
d.destTableIndex.Store(-1)
for _, opt := range opts {
opt(d)
}
if d.duckConnCount.Load() < 1 {
d.duckConnCount.Store(1)
return errors.New("quacfka: duckdb connection count must be >= 1")
}
err = o.configureDuck(d)
if err != nil {
return fmt.Errorf("quacfka: duckdb open error %w", err)
}
o.duckConf = d
return nil
}
func (o *Orchestrator[T]) configureDuck(d *duckConf) error {
var err error
// Configure and create database handle
var cOpts []couac.Option
// Using WithPathPrefix() to set a duckdb file path prefix overrides WithPath() path
if d.pathPrefix != "" {
if d.destTableIndex.Load() > 9 {
d.destTableIndex.Store(-1)
}
duckPrefixedPath := d.pathPrefix + "_" + cast.ToString(d.destTableIndex.Load()+1) + "_" + time.Now().Format("2006-01-02_15-04-05") + ".db"
cOpts = append(cOpts, couac.WithPath(duckPrefixedPath))
} else {
if d.path != "" {
cOpts = append(cOpts, couac.WithPath(d.path))
}
}
if d.driverPath != "" {
cOpts = append(cOpts, couac.WithDriverPath(d.driverPath))
}
d.quack, err = couac.NewDuck(cOpts...)
if err != nil {
return fmt.Errorf("duckdb config error: %w", err)
}
o.Metrics.duckFiles.Add(1)
return nil
}
func (o *Orchestrator[T]) DuckIngestWithRotate(ctx context.Context, w *sync.WaitGroup) {
defer w.Done()
var rwg sync.WaitGroup
for !(o.rChanClosed && o.rChanRecs.Load() == 0) {
if o.rChanRecs.Load() > 0 {
rwg.Add(1)
go o.DuckIngest(context.Background(), &rwg)
rwg.Wait()
if debugLog != nil {
debugLog("db size: %d\n", o.CurrentDBSize())
}
o.Metrics.recordBytes.Store(0)
// record cumulative size of duckdb files
o.Metrics.duckFilesSizeMB.Add(o.CurrentDBSize())
o.duckConf.quack.Close()
o.duckPaths <- o.duckConf.quack.Path()
if !(o.rChanClosed && o.rChanRecs.Load() == 0) {
o.configureDuck(o.duckConf)
}
}
}
}
func (o *Orchestrator[T]) DuckIngest(ctx context.Context, w *sync.WaitGroup) {
defer w.Done()
dpool, _ := ants.NewPoolWithFuncGeneric[*duckJob](o.DuckConnCount(), o.adbcInsert, ants.WithPreAlloc(true))
defer dpool.Release()
var cwg sync.WaitGroup
d := new(duckJob)
d.quack = o.duckConf.quack
d.destTable = o.duckConf.destTable
d.rChan = o.rChan
d.wg = &cwg
// Inserting one Arrow record ensures table is created if it does not already exist
duck, err := d.quack.NewConnection()
if err != nil {
if errorLog != nil {
errorLog("quacfka: new connection: %v", err)
}
return
}
defer duck.Close()
select {
case record, ok := <-o.rChan:
if !ok {
o.rChanClosed = true
}
if record.Raw != nil {
if len(o.opt.customArrow) > 0 {
for _, a := range o.opt.customArrow {
record.Raw.Retain()
modRec := a.CustomFunc(ctx, a.DestinationTable, record.Raw)
_, err := duck.IngestCreateAppend(ctx, a.DestinationTable, modRec)
if err != nil {
if errorLog != nil {
errorLog("quacfka: duck ingestcreateappend %v\n", err)
}
modRec.Release()
record.Raw.Release()
continue
}
modRec.Release()
record.Raw.Release()
}
}
numRows := record.Raw.NumRows()
_, err = duck.IngestCreateAppend(ctx, d.destTable, record.Raw)
if err != nil {
if errorLog != nil {
errorLog("quacfka: duck ingestcreateappend %v\n", err)
}
} else {
o.rChanRecs.Add(-1)
o.Metrics.recordsInserted.Add(numRows)
}
}
if record.Norm != nil {
numRows := record.Norm.NumRows()
_, err = duck.IngestCreateAppend(ctx, d.destTable+"_norm", record.Norm)
if err != nil {
if errorLog != nil {
errorLog("quacfka: duck ingestcreateappend %v\n", err)
}
} else {
o.Metrics.normRecordsInserted.Add(numRows)
}
}
default:
}
if o.shouldRotateFile(ctx, duck) {
o.duckPaths <- o.duckConf.quack.Path()
return
}
// Start using duck inserter pool
for dpool.Running() < o.DuckConnCount() && !(o.rChanClosed && o.rChanRecs.Load() == 0) {
cwg.Add(1)
dpool.Invoke(d)
if debugLog != nil {
debugLog("quacfka: duck pool size %d\n", dpool.Running())
}
if o.shouldRotateFile(ctx, duck) {
break
}
}
cwg.Wait()
if o.opt.fileRotateThresholdMB == 0 && o.duckConf.quack.Path() != "" {
o.duckPaths <- o.duckConf.quack.Path()
o.duckConf.quack.Close()
}
}
func (o *Orchestrator[T]) shouldRotateFile(ctx context.Context, duck *couac.QuackCon) bool {
if o.opt.fileRotateThresholdMB > 0 && o.duckConf.quack.Path() != "" && checkDuckDBSizeMB(ctx, duck) >= o.opt.fileRotateThresholdMB {
return true
}
return false
}
func (o *Orchestrator[T]) adbcInsert(c *duckJob) {
var tick time.Time
path := c.quack.Path()
defer c.wg.Done()
duck, err := c.quack.NewConnection()
if err != nil {
if errorLog != nil {
errorLog("quacfka: new connection: %v", err)
}
return
}
defer duck.Close()
ctx := context.Background()
var numRows int64
for record := range c.rChan {
o.rChanRecs.Add(-1)
dbSizeBeforeInsert := checkDuckDBSizeMB(ctx, duck)
if debugLog != nil {
tick = time.Now()
debugLog("quacfka: duck inserter - pull record - %d\n", o.rChanRecs.Load())
}
numRows = record.Raw.NumRows()
record.Raw.Retain()
// Custom Arrow data manipulation
if len(o.opt.customArrow) > 0 {
var cNumRows int64
for _, a := range o.opt.customArrow {
record.Raw.Retain()
modRec := a.CustomFunc(ctx, a.DestinationTable, record.Raw)
cNumRows = cNumRows + modRec.NumRows()
_, err := duck.IngestCreateAppend(ctx, a.DestinationTable, modRec)
if err != nil {
if errorLog != nil {
errorLog("quacfka: duck custom arrow ingestcreateappend %v\n", err)
}
modRec.Release()
record.Raw.Release()
continue
}
modRec.Release()
record.Raw.Release()
}
o.Metrics.customRecordsInserted.Add(cNumRows)
if debugLog != nil {
debugLog("quacfka: duckdb - custom arrow rows ingested: %d -%d ms- %f rows/sec\n", cNumRows, time.Since(tick).Milliseconds(), (float64(numRows) / float64(time.Since(tick).Seconds())))
}
}
// Normalizer data insertion
if record.Norm != nil {
tock := time.Now()
nNumRows := record.Norm.NumRows()
_, err = duck.IngestCreateAppend(ctx, c.destTable+"_norm", record.Norm)
if err != nil {
if errorLog != nil {
errorLog("quacfka: duck normalizer ingestcreateappend %v\n", err)
}
} else {
o.Metrics.normRecordsInserted.Add(numRows)
if debugLog != nil {
debugLog("quacfka: duckdb - normalizer arrow rows ingested: %d -%d ms- %f rows/sec\n", nNumRows, time.Since(tock).Milliseconds(), (float64(numRows) / float64(time.Since(tick).Seconds())))
}
}
}
// Insert main record
_, err := duck.IngestCreateAppend(ctx, c.destTable, record.Raw)
if err != nil {
if errorLog != nil {
errorLog("quacfka: duck ingestcreateappend %v\n", err)
}
} else {
o.Metrics.recordsInserted.Add(numRows)
if debugLog != nil {
debugLog("quacfka: duckdb - rows ingested: %d -%d ms- %f rows/sec\n", numRows, time.Since(tick).Milliseconds(), (float64(numRows) / float64(time.Since(tick).Seconds())))
}
}
// If file rotation is enabled, exit every time threshold is met.
if o.opt.fileRotateThresholdMB > 0 && path != "" {
var s uint64
for _, c := range record.Raw.Columns() {
s = s + c.Data().SizeInBytes()
}
o.Metrics.recordBytes.Add(int64(s))
dbSizeAfterInsert := checkDuckDBSizeMB(ctx, duck)
if dbSizeAfterInsert+(dbSizeAfterInsert-dbSizeBeforeInsert)/int64(o.DuckConnCount()-1) >= o.opt.fileRotateThresholdMB {
record.Raw.Release()
break
}
}
record.Raw.Release()
}
}
func (o *Orchestrator[T]) CurrentDBSize() int64 {
if o.duckConf.quack == nil {
return 0
}
duck, err := o.duckConf.quack.NewConnection()
if err != nil {
if errorLog != nil {
errorLog("quacfka: new connection: %v", err)
}
return -1
}
defer duck.Close()
size := checkDuckDBSizeMB(context.Background(), duck)
return size
}
func checkDuckDBSizeMB(ctx context.Context, duck *couac.QuackCon) int64 {
var sizeBytes int64
var err error
recReader, statement, _, err := duck.Query(ctx, "CALL pragma_database_size()")
if err != nil {
return -1
}
defer statement.Close()
for recReader.Next() {
record := recReader.Record()
for i := 0; i < int(record.NumRows()); i++ {
block_size := record.Column(2).GetOneForMarshal(i)
total_blocks := record.Column(3).GetOneForMarshal(i)
wal := record.Column(6).ValueStr(i)
walBytes := strings.Split(wal, " ")[0]
if len(strings.Split(wal, " ")) > 1 {
switch walUnit := strings.Split(wal, " ")[1]; walUnit {
case "KiB":
sizeBytes = sizeBytes + cast.ToInt64(walBytes)*1024
case "MiB":
sizeBytes = sizeBytes + cast.ToInt64(walBytes)*1024*1024
case "GiB":
sizeBytes = sizeBytes + cast.ToInt64(walBytes)*1024*1024*1024
}
}
sizeBytes = sizeBytes + (block_size.(int64) * total_blocks.(int64))
}
}
return sizeBytes / 1024 / 1024
}