-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbatch_manager.go
More file actions
1125 lines (1007 loc) · 31.4 KB
/
batch_manager.go
File metadata and controls
1125 lines (1007 loc) · 31.4 KB
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*===----------- batch_manager.go - intelligent batching for ClickHouse -----===
*
* This file implements a comprehensive batching system optimized for ClickHouse
* performance. It supports multiple batching strategies, per-table configuration,
* and automatic flush mechanisms.
*
* This file is licensed under the Apache 2 License. See LICENSE for details.
*
* Copyright (c) 2024 Andrew Grosser. All Rights Reserved.
*
*===----------------------------------------------------------------------===
*/
package main
import (
"context"
"encoding/json"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/google/uuid"
)
// BatchStrategy defines the batching approach
type BatchStrategy string
const (
StrategyTimeBasedBatch BatchStrategy = "time" // Flush every X seconds
StrategySizeBasedBatch BatchStrategy = "size" // Flush when N rows collected
StrategyHybridBatch BatchStrategy = "hybrid" // Flush on time OR size
StrategyMemoryBasedBatch BatchStrategy = "memory" // Flush when memory threshold hit
StrategyAdaptiveBatch BatchStrategy = "adaptive" // Dynamically adjust based on load
StrategyImmediateBatch BatchStrategy = "immediate" // No batching (for critical events)
)
// BatchConfig defines configuration for a specific table's batching behavior
type BatchConfig struct {
TableName string `json:"table_name"`
Strategy BatchStrategy `json:"strategy"`
MaxBatchSize int `json:"max_batch_size"` // Number of rows
MaxBatchTime time.Duration `json:"max_batch_time"` // Time threshold
MaxMemoryMB int `json:"max_memory_mb"` // Memory threshold in MB
Priority int `json:"priority"` // 1=highest, 10=lowest
RetryAttempts int `json:"retry_attempts"` // Number of retry attempts
RetryBackoffMs int `json:"retry_backoff_ms"` // Backoff between retries
EnableCompression bool `json:"enable_compression"` // Compress batch data
}
// DefaultBatchConfigs provides optimized defaults for different table types
var DefaultBatchConfigs = map[string]BatchConfig{
"events": {
TableName: "events",
Strategy: StrategyHybridBatch,
MaxBatchSize: 100000,
MaxBatchTime: 10 * time.Second,
MaxMemoryMB: 100,
Priority: 2, // Higher priority for events
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
// NOTE: events_recent is a VIEW, not a table - cannot batch insert to views
// All events are written to the events table
"mthreads": {
TableName: "mthreads",
Strategy: StrategyTimeBasedBatch,
MaxBatchSize: 10000,
MaxBatchTime: 5 * time.Second,
MaxMemoryMB: 5,
Priority: 2,
RetryAttempts: 3,
RetryBackoffMs: 200,
EnableCompression: false,
},
"mstore": {
TableName: "mstore",
Strategy: StrategyHybridBatch,
MaxBatchSize: 500,
MaxBatchTime: 3 * time.Second,
MaxMemoryMB: 8,
Priority: 4,
RetryAttempts: 2,
RetryBackoffMs: 150,
EnableCompression: true,
},
"mtriage": {
TableName: "mtriage",
Strategy: StrategyHybridBatch, // Using HybridBatch to support SETTINGS insert_deduplicate
MaxBatchSize: 50, // Increased from 10 for better batching efficiency
MaxBatchTime: 2 * time.Second, // 2 second window - balance between urgency and batching
MaxMemoryMB: 2,
Priority: 1, // Highest priority - critical for follow-ups
RetryAttempts: 5,
RetryBackoffMs: 50,
EnableCompression: false,
},
"visitors": {
TableName: "visitors",
Strategy: StrategyHybridBatch,
MaxBatchSize: 200,
MaxBatchTime: 10 * time.Second,
MaxMemoryMB: 3,
Priority: 5,
RetryAttempts: 2,
RetryBackoffMs: 300,
EnableCompression: true,
},
"payments": {
TableName: "payments",
Strategy: StrategyImmediateBatch,
MaxBatchSize: 1,
MaxBatchTime: 0,
MaxMemoryMB: 1,
Priority: 1, // Critical financial data
RetryAttempts: 5,
RetryBackoffMs: 100,
EnableCompression: false,
},
"impression_daily": {
TableName: "impression_daily",
Strategy: StrategyHybridBatch,
MaxBatchSize: 250,
MaxBatchTime: 4 * time.Second,
MaxMemoryMB: 6,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 200,
EnableCompression: true,
},
"instant": { //Use this for critical operations that need to be processed immediately, with retry system
TableName: "instant",
Strategy: StrategyImmediateBatch,
MaxBatchSize: 1,
MaxBatchTime: 0,
MaxMemoryMB: 1,
Priority: 1, // Highest priority for instant processing
RetryAttempts: 3,
RetryBackoffMs: 50,
EnableCompression: false,
},
"agreements": {
TableName: "agreements",
Strategy: StrategyHybridBatch,
MaxBatchSize: 100,
MaxBatchTime: 3 * time.Second,
MaxMemoryMB: 2,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: false,
},
"agreed": {
TableName: "agreed",
Strategy: StrategyHybridBatch,
MaxBatchSize: 100,
MaxBatchTime: 3 * time.Second,
MaxMemoryMB: 2,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: false,
},
"redirects": {
TableName: "redirects",
Strategy: StrategyHybridBatch,
MaxBatchSize: 200,
MaxBatchTime: 2 * time.Second,
MaxMemoryMB: 3,
Priority: 2,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"redirect_history": {
TableName: "redirect_history",
Strategy: StrategyHybridBatch,
MaxBatchSize: 500,
MaxBatchTime: 5 * time.Second,
MaxMemoryMB: 5,
Priority: 4,
RetryAttempts: 2,
RetryBackoffMs: 150,
EnableCompression: true,
},
"counters": {
TableName: "counters",
Strategy: StrategyHybridBatch,
MaxBatchSize: 300,
MaxBatchTime: 3 * time.Second,
MaxMemoryMB: 3,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"updates": {
TableName: "updates",
Strategy: StrategyHybridBatch,
MaxBatchSize: 200,
MaxBatchTime: 4 * time.Second,
MaxMemoryMB: 3,
Priority: 4,
RetryAttempts: 3,
RetryBackoffMs: 150,
EnableCompression: true,
},
"logs": {
TableName: "logs",
Strategy: StrategyHybridBatch,
MaxBatchSize: 1000,
MaxBatchTime: 5 * time.Second,
MaxMemoryMB: 8,
Priority: 4,
RetryAttempts: 2,
RetryBackoffMs: 200,
EnableCompression: true,
},
"ips": {
TableName: "ips",
Strategy: StrategyHybridBatch,
MaxBatchSize: 500,
MaxBatchTime: 3 * time.Second,
MaxMemoryMB: 4,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"routed": {
TableName: "routed",
Strategy: StrategyHybridBatch,
MaxBatchSize: 200,
MaxBatchTime: 2 * time.Second,
MaxMemoryMB: 3,
Priority: 2,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"hits": {
TableName: "hits",
Strategy: StrategyHybridBatch,
MaxBatchSize: 1000,
MaxBatchTime: 2 * time.Second,
MaxMemoryMB: 8,
Priority: 2,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"dailies": {
TableName: "dailies",
Strategy: StrategyTimeBasedBatch,
MaxBatchSize: 200,
MaxBatchTime: 10 * time.Second,
MaxMemoryMB: 4,
Priority: 4,
RetryAttempts: 3,
RetryBackoffMs: 200,
EnableCompression: true,
},
"outcomes": {
TableName: "outcomes",
Strategy: StrategyHybridBatch,
MaxBatchSize: 300,
MaxBatchTime: 3 * time.Second,
MaxMemoryMB: 4,
Priority: 2,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"referrers": {
TableName: "referrers",
Strategy: StrategyHybridBatch,
MaxBatchSize: 400,
MaxBatchTime: 4 * time.Second,
MaxMemoryMB: 5,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 150,
EnableCompression: true,
},
"referrals": {
TableName: "referrals",
Strategy: StrategyHybridBatch,
MaxBatchSize: 300,
MaxBatchTime: 3 * time.Second,
MaxMemoryMB: 4,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"referred": {
TableName: "referred",
Strategy: StrategyHybridBatch,
MaxBatchSize: 300,
MaxBatchTime: 3 * time.Second,
MaxMemoryMB: 4,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"hosts": {
TableName: "hosts",
Strategy: StrategyHybridBatch,
MaxBatchSize: 200,
MaxBatchTime: 5 * time.Second,
MaxMemoryMB: 3,
Priority: 4,
RetryAttempts: 3,
RetryBackoffMs: 150,
EnableCompression: true,
},
"browsers": {
TableName: "browsers",
Strategy: StrategyHybridBatch,
MaxBatchSize: 300,
MaxBatchTime: 4 * time.Second,
MaxMemoryMB: 3,
Priority: 4,
RetryAttempts: 3,
RetryBackoffMs: 150,
EnableCompression: true,
},
"nodes": {
TableName: "nodes",
Strategy: StrategyHybridBatch,
MaxBatchSize: 200,
MaxBatchTime: 5 * time.Second,
MaxMemoryMB: 3,
Priority: 4,
RetryAttempts: 3,
RetryBackoffMs: 150,
EnableCompression: true,
},
"locations": {
TableName: "locations",
Strategy: StrategyHybridBatch,
MaxBatchSize: 300,
MaxBatchTime: 4 * time.Second,
MaxMemoryMB: 4,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"aliases": {
TableName: "aliases",
Strategy: StrategyHybridBatch,
MaxBatchSize: 100,
MaxBatchTime: 5 * time.Second,
MaxMemoryMB: 2,
Priority: 4,
RetryAttempts: 3,
RetryBackoffMs: 150,
EnableCompression: true,
},
"userhosts": {
TableName: "userhosts",
Strategy: StrategyHybridBatch,
MaxBatchSize: 200,
MaxBatchTime: 4 * time.Second,
MaxMemoryMB: 3,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"usernames": {
TableName: "usernames",
Strategy: StrategyHybridBatch,
MaxBatchSize: 200,
MaxBatchTime: 4 * time.Second,
MaxMemoryMB: 3,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"emails": {
TableName: "emails",
Strategy: StrategyHybridBatch,
MaxBatchSize: 200,
MaxBatchTime: 3 * time.Second,
MaxMemoryMB: 3,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"cells": {
TableName: "cells",
Strategy: StrategyHybridBatch,
MaxBatchSize: 200,
MaxBatchTime: 4 * time.Second,
MaxMemoryMB: 3,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"reqs": {
TableName: "reqs",
Strategy: StrategyHybridBatch,
MaxBatchSize: 500,
MaxBatchTime: 3 * time.Second,
MaxMemoryMB: 5,
Priority: 3,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"sessions": {
TableName: "sessions",
Strategy: StrategyHybridBatch,
MaxBatchSize: 300,
MaxBatchTime: 3 * time.Second,
MaxMemoryMB: 4,
Priority: 2,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"visitors_latest": {
TableName: "visitors_latest",
Strategy: StrategyHybridBatch,
MaxBatchSize: 200,
MaxBatchTime: 2 * time.Second,
MaxMemoryMB: 3,
Priority: 2,
RetryAttempts: 3,
RetryBackoffMs: 100,
EnableCompression: true,
},
"ltv": {
TableName: "ltv",
Strategy: StrategyImmediateBatch,
MaxBatchSize: 1,
MaxBatchTime: 0,
MaxMemoryMB: 1,
Priority: 1, // Critical for lifetime value tracking
RetryAttempts: 5,
RetryBackoffMs: 50,
EnableCompression: false,
},
"visitor_interests": {
TableName: "visitor_interests",
Strategy: StrategyHybridBatch,
MaxBatchSize: 500, // Batch interests for performance
MaxBatchTime: 3 * time.Second, // Flush every 3 seconds
MaxMemoryMB: 5,
Priority: 3, // Medium priority (not critical like payments)
RetryAttempts: 3,
RetryBackoffMs: 150,
EnableCompression: true, // Compress interest data (JSON fields)
},
}
// BatchItem represents a single item to be batched
type BatchItem struct {
ID uuid.UUID `json:"id"`
TableName string `json:"table_name"`
SQL string `json:"sql"`
Args []interface{} `json:"args"`
Data map[string]interface{} `json:"data"`
Priority int `json:"priority"`
Timestamp time.Time `json:"timestamp"`
Size int `json:"size"` // Estimated memory size in bytes
}
// BatchMetrics tracks batching performance
type BatchMetrics struct {
TotalBatches int64 `json:"total_batches"`
TotalItems int64 `json:"total_items"`
FailedBatches int64 `json:"failed_batches"`
AvgBatchSize int64 `json:"avg_batch_size"`
AvgFlushLatencyMs int64 `json:"avg_flush_latency_ms"`
QueuedItems int64 `json:"queued_items"`
MemoryUsageMB int64 `json:"memory_usage_mb"`
LastFlushTime int64 `json:"last_flush_time"`
}
// TableBatch represents a batch for a specific table
type TableBatch struct {
Config BatchConfig `json:"config"`
Items []BatchItem `json:"items"`
Size int `json:"size"` // Total memory size in bytes
CreatedAt time.Time `json:"created_at"`
LastItemAt time.Time `json:"last_item_at"`
mu sync.RWMutex `json:"-"`
flushTimer *time.Timer `json:"-"`
}
// BatchManager orchestrates all batching operations
type BatchManager struct {
session clickhouse.Conn
batches map[string]*TableBatch
configs map[string]BatchConfig
metrics *BatchMetrics
stopChan chan bool
flushChan chan string // Table name to flush
priorityChan chan BatchItem // High priority items
mu sync.RWMutex
running bool
adaptiveMode bool
AppConfig *Configuration
// Adaptive batching variables
lastLoad int64
avgLatency int64
loadSamples []int64
}
// NewBatchManager creates a new batch manager with default configurations
func NewBatchManager(session clickhouse.Conn, appConfig *Configuration) *BatchManager {
bm := &BatchManager{
session: session,
batches: make(map[string]*TableBatch),
configs: make(map[string]BatchConfig),
metrics: &BatchMetrics{},
stopChan: make(chan bool),
flushChan: make(chan string, 500),
priorityChan: make(chan BatchItem, 1000),
adaptiveMode: true,
loadSamples: make([]int64, 0, 100),
AppConfig: appConfig,
}
// Initialize with default configurations
for tableName, config := range DefaultBatchConfigs {
bm.configs[tableName] = config
bm.batches[tableName] = &TableBatch{
Config: config,
Items: make([]BatchItem, 0, config.MaxBatchSize),
CreatedAt: time.Now(),
}
}
return bm
}
// Start begins the batch processing goroutines
func (bm *BatchManager) Start() error {
bm.mu.Lock()
if bm.running {
bm.mu.Unlock()
return fmt.Errorf("batch manager is already running")
}
bm.running = true
// Start main batch processor
go bm.processBatches()
// Start priority item processor
go bm.processPriorityItems()
// Start adaptive optimizer (if enabled)
if bm.adaptiveMode {
go bm.adaptiveOptimizer()
}
// Release write lock before acquiring read lock to avoid deadlock
bm.mu.Unlock()
// Start flush timers for each table
bm.mu.RLock()
for tableName, batch := range bm.batches {
if batch.Config.Strategy == StrategyTimeBasedBatch || batch.Config.Strategy == StrategyHybridBatch {
bm.startFlushTimer(tableName)
}
}
bm.mu.RUnlock()
fmt.Println("✅ Batch Manager started with", len(bm.configs), "table configurations")
return nil
}
// Stop gracefully shuts down the batch manager
func (bm *BatchManager) Stop() error {
bm.mu.Lock()
defer bm.mu.Unlock()
if !bm.running {
return nil
}
fmt.Println("🛑 Stopping Batch Manager...")
// Stop all timers
for _, batch := range bm.batches {
if batch.flushTimer != nil {
batch.flushTimer.Stop()
}
}
// Flush all remaining batches
bm.flushAllBatches()
// Signal stop
close(bm.stopChan)
bm.running = false
fmt.Println("✅ Batch Manager stopped gracefully")
return nil
}
// AddItem adds an item to the appropriate batch
func (bm *BatchManager) AddItem(item BatchItem) error {
start := time.Now()
defer func() {
latency := time.Since(start).Milliseconds()
atomic.AddInt64(&bm.metrics.AvgFlushLatencyMs, latency)
}()
// Validate item
if item.TableName == "" {
return NewTrackerError(ErrorTypeValidation, "AddItem", "table_name is required", false)
}
// Check if table is configured
config, exists := bm.configs[item.TableName]
if !exists {
return NewTrackerError(ErrorTypeValidation, "AddItem", fmt.Sprintf("no configuration for table %s", item.TableName), false)
}
// Set defaults
item.ID = uuid.New()
item.Timestamp = time.Now()
if item.Priority == 0 {
item.Priority = config.Priority
}
// Estimate memory size
item.Size = bm.estimateItemSize(item)
// Handle immediate strategy or high priority items
if config.Strategy == StrategyImmediateBatch || item.Priority == 1 {
select {
case bm.priorityChan <- item:
return nil
default:
// Priority channel full, process immediately
return bm.processItemImmediately(item)
}
}
// Add to batch
return bm.addToBatch(item)
}
// addToBatch adds an item to the appropriate table batch
func (bm *BatchManager) addToBatch(item BatchItem) error {
bm.mu.RLock()
batch, exists := bm.batches[item.TableName]
bm.mu.RUnlock()
if !exists {
return NewTrackerError(ErrorTypeValidation, "addToBatch", fmt.Sprintf("batch not found for table %s", item.TableName), false)
}
batch.mu.Lock()
defer batch.mu.Unlock()
// Add item to batch
batch.Items = append(batch.Items, item)
batch.Size += item.Size
batch.LastItemAt = time.Now()
// Update metrics
atomic.AddInt64(&bm.metrics.QueuedItems, 1)
atomic.AddInt64(&bm.metrics.MemoryUsageMB, int64(item.Size/1024/1024))
// Check if batch should be flushed
shouldFlush := false
switch batch.Config.Strategy {
case StrategySizeBasedBatch:
shouldFlush = len(batch.Items) >= batch.Config.MaxBatchSize
case StrategyMemoryBasedBatch:
shouldFlush = batch.Size >= (batch.Config.MaxMemoryMB * 1024 * 1024)
case StrategyHybridBatch:
shouldFlush = len(batch.Items) >= batch.Config.MaxBatchSize ||
batch.Size >= (batch.Config.MaxMemoryMB*1024*1024)
case StrategyAdaptiveBatch:
shouldFlush = bm.shouldFlushAdaptive(batch)
}
if shouldFlush {
// Trigger async flush - completely non-blocking to prevent HTTP hangs
select {
case bm.flushChan <- item.TableName:
// Successfully queued for flushing
default:
// Channel full - completely skip this flush to prevent any blocking
// The timeout-based flush or next batch will handle it
}
}
return nil
}
// processBatches handles the main batch processing loop
func (bm *BatchManager) processBatches() {
for {
select {
case <-bm.stopChan:
return
case tableName := <-bm.flushChan:
if err := bm.flushBatch(tableName); err != nil {
globalMetrics.UpdateErrorCount()
fmt.Printf("[ERROR] Failed to flush batch for table %s: %v\n", tableName, err)
}
case <-time.After(100 * time.Millisecond):
// Check for timeout-based flushes
bm.checkTimeoutFlushes()
}
}
}
// processPriorityItems handles high-priority items immediately
func (bm *BatchManager) processPriorityItems() {
for {
select {
case <-bm.stopChan:
return
case item := <-bm.priorityChan:
if bm.AppConfig.Debug {
fmt.Printf("[DEBUG] Processing priority item for table: %s\n", item.TableName)
}
if err := bm.processItemImmediately(item); err != nil {
globalMetrics.UpdateErrorCount()
fmt.Printf("[ERROR] Failed to process priority item: %v\n", err)
} else if bm.AppConfig.Debug {
fmt.Printf("[DEBUG] Successfully processed priority item for table: %s\n", item.TableName)
}
}
}
}
// processItemImmediately processes a single item without batching
func (bm *BatchManager) processItemImmediately(item BatchItem) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
start := time.Now()
if bm.AppConfig.Debug {
sqlPreview := item.SQL
if len(sqlPreview) > 100 {
sqlPreview = sqlPreview[:100] + "..."
}
fmt.Printf("[DEBUG] Executing SQL for table %s: %s\n", item.TableName, sqlPreview)
}
err := bm.session.Exec(ctx, item.SQL, item.Args...)
latency := time.Since(start)
if bm.AppConfig.Debug {
fmt.Printf("[DEBUG] Exec result for %s: error=%v, latency=%v\n", item.TableName, err, latency)
}
if err != nil {
return NewTrackerError(ErrorTypeQuery, "processItemImmediately",
fmt.Sprintf("failed to execute immediate query for table %s: %v", item.TableName, err), true)
}
// Update metrics
atomic.AddInt64(&bm.metrics.TotalBatches, 1)
atomic.AddInt64(&bm.metrics.TotalItems, 1)
atomic.AddInt64(&bm.metrics.AvgFlushLatencyMs, latency.Milliseconds())
return nil
}
// flushBatch flushes a specific table's batch
func (bm *BatchManager) flushBatch(tableName string) error {
bm.mu.RLock()
batch, exists := bm.batches[tableName]
bm.mu.RUnlock()
if !exists {
return fmt.Errorf("batch not found for table %s", tableName)
}
batch.mu.Lock()
// Check if batch is empty
if len(batch.Items) == 0 {
batch.mu.Unlock()
return nil
}
// Copy items and reset batch
items := make([]BatchItem, len(batch.Items))
copy(items, batch.Items)
batch.Items = batch.Items[:0] // Reset slice but keep capacity
batch.Size = 0
batch.CreatedAt = time.Now()
batch.mu.Unlock()
// Process batch
return bm.executeBatch(tableName, items)
}
// executeBatch executes a batch of items for a specific table
func (bm *BatchManager) executeBatch(tableName string, items []BatchItem) error {
if len(items) == 0 {
return nil
}
start := time.Now()
config := bm.configs[tableName]
// Create batch context with timeout (reduced to prevent hanging)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Prepare batch insert
batch, err := bm.session.PrepareBatch(ctx, bm.buildBatchSQL(tableName, items[0]))
if err != nil {
atomic.AddInt64(&bm.metrics.FailedBatches, 1)
return NewTrackerError(ErrorTypeQuery, "executeBatch",
fmt.Sprintf("failed to prepare batch for table %s: %v", tableName, err), true)
}
// Add all items to batch
for i, item := range items {
if err := batch.Append(item.Args...); err != nil {
atomic.AddInt64(&bm.metrics.FailedBatches, 1)
fmt.Printf("[ERROR] Batch append failed for table %s, item %d: %v\n", tableName, i, err)
if bm.AppConfig.Debug {
fmt.Printf("[DEBUG] Failed item args: %+v\n", item.Args)
}
return NewTrackerError(ErrorTypeQuery, "executeBatch",
fmt.Sprintf("failed to append to batch for table %s: %v", tableName, err), true)
}
}
// Execute batch with retry logic
var execErr error
for attempt := 0; attempt <= config.RetryAttempts; attempt++ {
execErr = batch.Send()
if execErr == nil {
break
}
if attempt < config.RetryAttempts {
time.Sleep(time.Duration(config.RetryBackoffMs) * time.Millisecond)
}
}
if execErr != nil {
atomic.AddInt64(&bm.metrics.FailedBatches, 1)
// Log detailed error information
fmt.Printf("[ERROR] Batch execution failed for table %s: %v\n", tableName, execErr)
if bm.AppConfig.Debug {
fmt.Printf("[DEBUG] SQL: %s\n", bm.buildBatchSQL(tableName, items[0]))
fmt.Printf("[DEBUG] Number of items: %d\n", len(items))
// Log first few items for debugging
for i, item := range items {
if i >= 3 { // Only log first 3 items to avoid spam
fmt.Printf("[DEBUG] ... and %d more items\n", len(items)-3)
break
}
fmt.Printf("[DEBUG] Item %d args: %+v\n", i, item.Args)
}
}
return NewTrackerError(ErrorTypeQuery, "executeBatch",
fmt.Sprintf("failed to execute batch for table %s after %d attempts: %v",
tableName, config.RetryAttempts+1, execErr), true)
}
// Update metrics
latency := time.Since(start)
atomic.AddInt64(&bm.metrics.TotalBatches, 1)
atomic.AddInt64(&bm.metrics.TotalItems, int64(len(items)))
atomic.AddInt64(&bm.metrics.AvgFlushLatencyMs, latency.Milliseconds())
atomic.AddInt64(&bm.metrics.QueuedItems, -int64(len(items)))
atomic.StoreInt64(&bm.metrics.LastFlushTime, time.Now().Unix())
// Update average batch size
totalBatches := atomic.LoadInt64(&bm.metrics.TotalBatches)
if totalBatches > 0 {
avgSize := atomic.LoadInt64(&bm.metrics.TotalItems) / totalBatches
atomic.StoreInt64(&bm.metrics.AvgBatchSize, avgSize)
}
// Debug logging for batch flush
if bm.AppConfig.Debug {
fmt.Printf("[BATCH] Flushed %d items to table %s in %v\n", len(items), tableName, latency)
}
return nil
}
// buildBatchSQL constructs the batch insert SQL for a table
func (bm *BatchManager) buildBatchSQL(tableName string, sampleItem BatchItem) string {
// Use the SQL from the sample item, but this could be enhanced
// to build optimized batch SQL based on table schema
return sampleItem.SQL
}
// checkTimeoutFlushes checks for batches that have exceeded their time threshold
func (bm *BatchManager) checkTimeoutFlushes() {
now := time.Now()
bm.mu.RLock()
defer bm.mu.RUnlock()
for tableName, batch := range bm.batches {
if batch.Config.Strategy == StrategyTimeBasedBatch || batch.Config.Strategy == StrategyHybridBatch {
batch.mu.RLock()
shouldFlush := len(batch.Items) > 0 &&
now.Sub(batch.CreatedAt) >= batch.Config.MaxBatchTime
batch.mu.RUnlock()
if shouldFlush {
select {
case bm.flushChan <- tableName:
default:
// Channel full, skip this flush
}
}
}
}
}
// flushAllBatches flushes all pending batches (used during shutdown)
func (bm *BatchManager) flushAllBatches() {
bm.mu.RLock()
tableNames := make([]string, 0, len(bm.batches))
for tableName := range bm.batches {
tableNames = append(tableNames, tableName)
}
bm.mu.RUnlock()
for _, tableName := range tableNames {
if err := bm.flushBatch(tableName); err != nil {
fmt.Printf("[ERROR] Failed to flush batch during shutdown for table %s: %v\n", tableName, err)
}
}
}
// startFlushTimer starts a timer for time-based flushing
func (bm *BatchManager) startFlushTimer(tableName string) {
batch := bm.batches[tableName]
if batch.flushTimer != nil {
batch.flushTimer.Stop()
}
batch.flushTimer = time.AfterFunc(batch.Config.MaxBatchTime, func() {
select {
case bm.flushChan <- tableName:
default:
// Channel full, try again later
bm.startFlushTimer(tableName)
}
})
}
// estimateItemSize estimates the memory size of a batch item
func (bm *BatchManager) estimateItemSize(item BatchItem) int {
// Basic estimation - could be enhanced with more accurate measurement
size := len(item.SQL)
size += len(item.TableName)
// Estimate args size
for _, arg := range item.Args {
switch v := arg.(type) {
case string:
size += len(v)
case []byte:
size += len(v)
default:
size += 8 // Rough estimate for other types
}
}
// Estimate data size (JSON approximation)
if item.Data != nil {