-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduck.go
503 lines (476 loc) · 13.9 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package quacfka
import (
"context"
"errors"
"fmt"
"os"
"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
runner *DuckRunner
}
type duckJob struct {
quack *couac.Quacker
destTable string
rChan chan Record
wg *sync.WaitGroup
}
type DuckRunner struct {
parent *duckConf
path string
queryFunc func(*DuckRunner) error
queries []string
exec bool
deleteDBOnDone bool
err error
}
// AddQueries adds queries to a runner and sets whether the runner should use RunExec instead of RunFunc to run the queries (RunExec should be used when no results from queries are expected).
func (d *DuckRunner) AddQueries(queries []string, exec bool) {
d.queries = append(d.queries, queries...)
d.exec = exec
}
func (d *DuckRunner) Err() error { return d.err }
func (d *DuckRunner) IsDeleteDBOnDone() bool { return d.deleteDBOnDone }
func (d *DuckRunner) Path() string { return d.path }
func (d *DuckRunner) Queries() []string { return d.queries }
func (d *DuckRunner) IsExec() bool { return d.exec }
func (d *DuckRunner) SetFunc(f func(*DuckRunner) error) { d.queryFunc = f }
func (d *DuckRunner) SetErr(err error) { d.err = errors.Join(d.err, err) }
func (d *DuckRunner) SetPath(p string) { d.path = p }
func (d *DuckRunner) SetDeleteOnDone(b bool) { d.deleteDBOnDone = b }
func (d *DuckRunner) GetDB() *couac.Quacker {
if d.parent != nil && d.parent.quack != nil {
return d.parent.quack
}
return nil
}
// Run runs the defined queries, if exec is set to true does not expect queries to return any results
// otherwise will use query function to coordinate queries.
// Exec set to true is meant for running queries that aggregate to another table as well
// as EXPORT/COPY TO statements.
func (d *DuckRunner) Run(ctx context.Context) error {
if ctx.Err() != nil {
return ctx.Err()
}
if len(d.Queries()) == 0 {
return fmt.Errorf("no queries specified")
}
if d.parent == nil {
return fmt.Errorf("runner has no parent")
}
db := d.GetDB()
if db == nil {
return fmt.Errorf("runner parent db not open")
}
if !d.exec && d.queryFunc == nil {
return fmt.Errorf("runner query func not set")
}
d.path = db.Path()
switch d.exec {
case false:
err := d.queryFunc(d)
if err != nil {
if errorLog != nil {
errorLog("quacfka: runfunc %s error: %w", d.Path(), err)
}
d.SetErr(fmt.Errorf("quacfka: runfunc %s error: %w", d.Path(), err))
return fmt.Errorf("quacfka: runfunc %s error: %w", d.Path(), err)
}
case true:
conn, err := db.NewConnection()
if err != nil {
if errorLog != nil {
errorLog("quacfka: runexec newconnection %v\n", err)
}
d.err = fmt.Errorf("quacfka: runexec newconnection %w", err)
return fmt.Errorf("quacfka: runexec newconnection %w", err)
}
for _, query := range d.queries {
_, err := conn.Exec(ctx, query)
if err != nil {
if errorLog != nil {
errorLog("quacfka: runexec exec %v\n", err)
}
d.err = fmt.Errorf("quacfka: runexec exec %w", err)
return fmt.Errorf("quacfka: runexec exec %w", err)
}
}
}
if d.deleteDBOnDone {
db.Close()
err := os.Remove(d.path)
if err != nil {
if errorLog != nil {
errorLog("quacfka: runner remove %v - %s\n", err, d.path)
}
d.SetErr(fmt.Errorf("quacfka: runner remove %w - %s", err, d.path))
return fmt.Errorf("quacfka: runner remove %w - %s", err, d.path)
}
}
return nil
}
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 WithDuckRunner(p *DuckRunner) DuckOption {
return func(cfg duckConfig) {
p.parent = cfg
cfg.runner = 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.duckMetrics.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)
o.Metrics.duckMetrics.duckStart()
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
duckDBSize := o.CurrentDBSize()
o.Metrics.duckMetrics.duckFilesSizeMB.Add(duckDBSize)
// runner to run queries on db before close
if o.duckConf.runner != nil {
err := o.duckConf.runner.Run(ctx)
if err != nil {
if errorLog != nil {
errorLog("quacfka: duckdb runner error - %w\n", err)
}
o.err = errors.Join(o.err, err)
}
}
if o.duckConf.runner == nil || (o.duckConf.runner != nil && !o.duckConf.runner.IsDeleteDBOnDone()) {
o.duckConf.quack.Close()
// send duckPath to channel for external consumption
if o.opt.withDuckPathsChan {
o.duckPaths <- o.duckConf.quack.Path()
}
}
o.Metrics.duckMetrics.duckStop(duckDBSize)
if !(o.rChanClosed && o.rChanRecs.Load() == 0) {
o.configureDuck(o.duckConf)
}
}
}
close(o.duckPaths)
}
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 len(o.opt.customArrow) > 0 {
if record.Raw != nil {
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()
}
}
}
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)
}
}
if !o.opt.withoutDuckIngestRaw && record.Raw != nil {
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.Metrics.recordsInserted.Add(numRows)
}
}
o.rChanRecs.Add(-1)
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()
close(o.duckPaths)
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()
// Custom Arrow data manipulation
if len(o.opt.customArrow) > 0 {
if record.Raw != nil {
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
if !o.opt.withoutDuckIngestRaw {
_, 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 != "" {
dbSizeAfterInsert := checkDuckDBSizeMB(ctx, duck)
if dbSizeAfterInsert+(dbSizeAfterInsert-dbSizeBeforeInsert)/int64(o.DuckConnCount()-1) >= o.opt.fileRotateThresholdMB {
break
}
}
}
}
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
}