-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
779 lines (674 loc) · 17.3 KB
/
main.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
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
package main
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"log"
"net"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
)
func startUdpServer(packetQueue chan<- *ParsedPacket) {
addr := net.UDPAddr{
Port: 9030,
IP: net.ParseIP("0.0.0.0"),
}
conn, err := net.ListenUDP("udp", &addr)
if err != nil {
panic(err)
}
defer conn.Close()
log.Println("Listening for UDP on", conn.LocalAddr())
var buf [1024 * 8]byte
for {
rlen, _, err := conn.ReadFromUDP(buf[:])
if err != nil {
fmt.Println("read error", err)
continue
}
// log.Println("Received", rlen, "bytes from", remote, "error:", err)
parsed := parsePacket(buf[:rlen])
packetQueue <- parsed
}
}
func startTcpServer(packetQueue chan<- *ParsedPacket) {
addr := net.TCPAddr{
Port: 9030,
IP: net.ParseIP("0.0.0.0"),
}
conn, err := net.ListenTCP("tcp", &addr)
if err != nil {
panic(err)
}
defer conn.Close()
log.Println("Listening for TCP on", conn.Addr().String())
for {
// Listen for an incoming connection.
conn, err := conn.Accept()
if err != nil {
fmt.Println("Error accepting")
panic(err)
}
fmt.Println("New tcp client")
go handleTcpConn(conn, packetQueue)
}
}
func handleTcpConn(conn net.Conn, packetQueue chan<- *ParsedPacket) {
defer conn.Close()
const DEADLINE = 30
headerBuff := make([]byte, 2)
OK := []byte("K")
ERROR_RESP := []byte("E")
for {
conn.SetDeadline(time.Now().Add(DEADLINE * time.Second))
headerLen, err := io.ReadFull(conn, headerBuff)
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
fmt.Println("Tcp client timed out")
} else {
fmt.Println("Error reading tcp header:", err)
}
return
}
if headerLen != 2 {
fmt.Println("Tcp header was only", headerLen, "bytes")
return
}
payloadSize := int(binary.BigEndian.Uint16(headerBuff))
if payloadSize > 1024*8 {
fmt.Println("Tcp payload too big", payloadSize)
return
}
// fmt.Println("Got tcp header with size", payloadSize)
payloadBuff := make([]byte, payloadSize)
bytesRead, err := io.ReadFull(conn, payloadBuff)
if err != nil {
fmt.Println("Tcp read error", err)
return
}
// fmt.Println("Got tcp payload with size", bytesRead)
if bytesRead == payloadSize {
parsed := parsePacket(payloadBuff[:payloadSize])
packetQueue <- parsed
if parsed != nil {
_, err = conn.Write(OK)
} else {
_, err = conn.Write(ERROR_RESP)
}
if err != nil {
fmt.Println("Tcp write error", err)
return
}
}
}
}
func main() {
var ctx = context.Background()
var dbConn, err = clickhouse.Open(&clickhouse.Options{
Addr: []string{"127.0.0.1:9000"},
Auth: clickhouse.Auth{
Database: "default",
Username: "default",
Password: "",
},
//Debug: true,
DialTimeout: time.Second,
MaxOpenConns: 10,
MaxIdleConns: 5,
ConnMaxLifetime: time.Hour,
})
if err != nil {
log.Panicln(err)
return
}
defer dbConn.Close()
if err := dbConn.Ping(ctx); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
fmt.Printf("[%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
} else {
fmt.Println(err)
}
return
}
queuedForSendingMap := make(map[string][]ParsedPacket)
queuedUnparsed := make(chan *ParsedPacket, 100)
lastSend := time.Now()
columnInfo := make(ColumnByTableAndName)
go func() {
startUdpServer(queuedUnparsed)
}()
go func() {
startTcpServer(queuedUnparsed)
}()
var failedParses = 0
var successParses = 0
for packet := range queuedUnparsed {
if packet == nil {
failedParses++
continue
}
successParses++
key := packet.PacketKey()
queuedForSendingMap[key] = append(queuedForSendingMap[key], *packet)
now := time.Now()
timeSinceLast := now.Sub(lastSend)
if timeSinceLast > time.Second*20 {
fmt.Println("Flushing", successParses, "rows", failedParses, "packet parse errors")
failedParses = 0
successParses = 0
// fmt.Println("Flushing queue of size", len(queuedForSendingMap))
lastSend = now
for key, queueSet := range queuedForSendingMap {
if len(columnInfo) == 0 {
columnInfo, err = fetchColumnOrder(dbConn)
if err != nil {
fmt.Println("Could not get columns", err)
}
}
if queueSet != nil && len(columnInfo) > 0 {
go func(innerQueueSet []ParsedPacket) {
for i := 0; i < 5; i++ {
shouldRetry := insertIntoDb(dbConn, innerQueueSet, columnInfo)
if !shouldRetry {
return
}
fmt.Println("Retrying insert in 5 seconds")
time.Sleep(time.Second * 5)
}
}(queueSet)
}
delete(queuedForSendingMap, key)
}
for range queuedForSendingMap {
fmt.Println("QUEUE IS NOT EMPTY AFTER FLUSH")
}
}
}
}
type ParsedPacket struct {
tableName string
columns []string
values []interface{}
}
type ColValue struct {
column string
value interface{}
}
func (p *ParsedPacket) PacketKey() string {
return fmt.Sprintf("%s:%s", p.tableName, strings.Join(p.columns, ","))
}
func parsePacket(x []byte) *ParsedPacket {
if x[0] != '{' {
//fmt.Println("Old log format:", string(x[:150]))
return parseOldPacket(string(x))
}
var arbitrary_json map[string]interface{}
err := json.Unmarshal([]byte(x), &arbitrary_json)
if err != nil {
fmt.Println("Parse packet error", err)
return nil
}
if arbitrary_json == nil {
fmt.Println("Parse json returned nil")
return nil
}
tableName, tableNameOk := arbitrary_json["_t"].(string)
delete(arbitrary_json, "_t")
if !tableNameOk || tableName == "" {
fmt.Println("Table name is empty")
return nil
}
items := make([]ColValue, 0, len(arbitrary_json))
for columnName, v := range arbitrary_json {
items = append(items, ColValue{column: columnName, value: v})
}
sort.Slice(items, func(i, j int) bool {
return items[i].column < items[j].column
})
columns := make([]string, 0, len(items))
for _, v := range items {
columns = append(columns, v.column)
}
values := make([]interface{}, 0, len(items))
for _, v := range items {
values = append(values, v.value)
}
return &ParsedPacket{
tableName: tableName,
columns: columns,
values: values,
}
}
type ColumnByTableAndName = map[string]map[string]ColumnInfo
type ColumnInfo struct {
Table string
ColType string
Name string
Position uint64
}
func fetchColumnOrder(dbConn driver.Conn) (ColumnByTableAndName, error) {
fmt.Println("Fetching column info...")
rows, err := dbConn.Query(context.Background(), `
SELECT table, position, type, name
FROM system.columns
WHERE database = 'default'`)
result := make(ColumnByTableAndName)
if err != nil {
return result, err
}
for rows.Next() {
var (
table string
pos uint64
colType string
name string
)
if err := rows.Scan(&table, &pos, &colType, &name); err != nil {
return result, err
}
if result[table] == nil {
result[table] = make(map[string]ColumnInfo)
}
result[table][name] = ColumnInfo{
Table: table,
Position: pos,
ColType: strings.Replace(strings.Replace(strings.Replace(strings.Replace(colType, "(3)", "", 1), ")", "", 1), "LowCardinality(", "", 1), "Nullable(", "", 1),
Name: name,
}
}
fmt.Println("...Fetched columns for", len(result), "tables")
return result, err
}
func insertIntoDb(dbConn driver.Conn, rows []ParsedPacket, columnInfo ColumnByTableAndName) bool {
columnNames := rows[0].columns
tableName := rows[0].tableName
columns := columnInfo[tableName]
if columns == nil {
fmt.Println("No column info found for table", tableName)
return false
}
if len(columns) != len(columnNames) {
fmt.Println("Column missmatch in", tableName)
columnNameMap := make(map[string]bool)
for _, v := range columnNames {
columnNameMap[v] = true
if _, ok := columns[v]; !ok {
fmt.Println("Packet col does not exist in db", tableName, v)
}
}
for key := range columns {
if _, ok := columnNameMap[key]; !ok {
fmt.Println("Db column missing from packet", tableName, key)
}
}
return false
}
// fmt.Println("Inserting", len(rows), "rows into", tableName, "...")
tx, err := dbConn.PrepareBatch(context.Background(), fmt.Sprintf("INSERT INTO %s", tableName))
if err != nil {
fmt.Println("Could not start transaction", err)
return true
}
if tx == nil {
fmt.Println("Could not start transaction")
return true
}
defer tx.Abort()
for colIndex, colName := range columnNames {
colInfo, ok := columns[colName]
if ok {
col := tx.Column(int(colInfo.Position) - 1)
if col == nil {
fmt.Println("Col is nil", tableName, colName)
return true
}
// fmt.Println("Converting column", colName, "to", colInfo.ColType)
if colInfo.ColType == "Float32" {
items := make([]float32, len(rows))
for i, row := range rows {
val := row.values[colIndex]
switch v := val.(type) {
case int:
case int64:
case float32:
case float64:
items[i] = float32(v)
case bool:
if v {
items[i] = 1
} else {
items[i] = 0
}
case string:
v2, err := strconv.ParseFloat(v, 32)
if err == nil {
items[i] = float32(v2)
} else {
items[i] = 0
fmt.Println("Invalid float number", tableName, colName, v)
}
default:
items[i] = 0
fmt.Println("Invalid col conversion", tableName, colName, v)
}
}
err = col.Append(items)
if err != nil {
fmt.Println("Add column error", tableName, err)
return true
}
} else if colInfo.ColType == "String" {
items := make([]string, len(rows))
for i, row := range rows {
val := row.values[colIndex]
switch v := val.(type) {
case int:
case int64:
items[i] = strconv.FormatInt(int64(v), 10)
case float32:
case float64:
items[i] = strconv.FormatFloat(float64(v), 'f', 3, 64)
case string:
items[i] = v
default:
items[i] = ""
fmt.Println("Invalid col conversion", tableName, colName, v)
}
}
err = col.Append(items)
if err != nil {
fmt.Println("Add column error", tableName, err)
return true
}
} else if colInfo.ColType == "DateTime" || colInfo.ColType == "DateTime64" {
items := make([]time.Time, len(rows))
for i, row := range rows {
val := row.values[colIndex]
switch v := val.(type) {
case int:
case int64:
items[i] = time.UnixMilli(int64(v))
case float32:
case float64:
items[i] = time.UnixMilli(int64(v))
case string:
date, err := time.Parse(time.RFC3339, strings.Replace(v, " ", "T", 1)+"Z")
if err != nil {
fmt.Println("Invalid date", tableName, colName, v, err)
}
items[i] = date
default:
fmt.Println("Invalid col conversion", tableName, colName, v)
}
}
err = col.Append(items)
if err != nil {
fmt.Println("Add column error", tableName, err)
return true
}
} else if colInfo.ColType == "UInt16" {
items := make([]uint16, len(rows))
for i, row := range rows {
val := row.values[colIndex]
switch v := val.(type) {
case int:
case int64:
case float32:
case float64:
items[i] = uint16(v)
case bool:
if v {
items[i] = 1
} else {
items[i] = 0
}
case string:
v2, err := strconv.ParseUint(v, 10, 16)
if err == nil {
items[i] = uint16(v2)
} else {
items[i] = 0
fmt.Println("Invalid uint16 number", tableName, colName, v)
}
default:
items[i] = 0
fmt.Println("Invalid col conversion", tableName, colName, v)
}
}
err = col.Append(items)
if err != nil {
fmt.Println("Add column error", tableName, err)
return true
}
} else if colInfo.ColType == "UInt32" {
items := make([]uint32, len(rows))
for i, row := range rows {
val := row.values[colIndex]
switch v := val.(type) {
case int:
case int64:
case float32:
case float64:
items[i] = uint32(v)
case bool:
if v {
items[i] = 1
} else {
items[i] = 0
}
case string:
v2, err := strconv.ParseUint(v, 10, 32)
if err == nil {
items[i] = uint32(v2)
} else {
items[i] = 0
fmt.Println("Invalid uint32 number", tableName, colName, v)
}
default:
items[i] = 0
fmt.Println("Invalid col conversion", tableName, colName, v)
}
}
err = col.Append(items)
if err != nil {
fmt.Println("Add column error", tableName, err)
return true
}
} else if colInfo.ColType == "Int32" {
items := make([]int32, len(rows))
for i, row := range rows {
val := row.values[colIndex]
switch v := val.(type) {
case int:
case int64:
case float32:
case float64:
items[i] = int32(v)
case bool:
if v {
items[i] = 1
} else {
items[i] = 0
}
case string:
v2, err := strconv.ParseInt(v, 10, 32)
if err == nil {
items[i] = int32(v2)
} else {
items[i] = 0
fmt.Println("Invalid int32 number", tableName, colName, v)
}
default:
items[i] = 0
fmt.Println("Invalid col conversion", tableName, colName, v)
}
}
err = col.Append(items)
if err != nil {
fmt.Println("Add column error", tableName, err)
return true
}
} else if colInfo.ColType == "UInt8" {
items := make([]uint8, len(rows))
for i, row := range rows {
val := row.values[colIndex]
switch v := val.(type) {
case int:
case int64:
case float32:
case float64:
items[i] = uint8(v)
case bool:
if v {
items[i] = 1
} else {
items[i] = 0
}
case string:
v2, err := strconv.ParseUint(v, 10, 8)
if err == nil {
items[i] = uint8(v2)
} else {
items[i] = 0
fmt.Println("Invalid uint8 number", tableName, colName, v)
}
default:
items[i] = 0
fmt.Println("Invalid col conversion", tableName, colName, v)
}
}
err = col.Append(items)
if err != nil {
fmt.Println("Add column error", tableName, err)
return true
}
} else if colInfo.ColType == "UInt64" {
items := make([]uint64, len(rows))
for i, row := range rows {
val := row.values[colIndex]
switch v := val.(type) {
case int:
case int64:
case float32:
case float64:
items[i] = uint64(v)
case bool:
if v {
items[i] = 1
} else {
items[i] = 0
}
case string:
v2, err := strconv.ParseUint(v, 10, 64)
if err == nil {
items[i] = uint64(v2)
} else {
items[i] = 0
fmt.Println("Invalid uint64 number", tableName, colName, v)
}
default:
items[i] = 0
fmt.Println("Invalid col conversion", tableName, colName, v)
}
}
err = col.Append(items)
if err != nil {
fmt.Println("Add column error", tableName, err)
return true
}
} else {
fmt.Println("Unknown column type", tableName, colInfo.ColType)
return true
}
} else {
fmt.Println("Column does not exist in db table", tableName, colName)
}
}
// fmt.Println("Sending...")
err = tx.Send()
if err != nil {
fmt.Println("Commit error", tableName, err)
return true
}
fmt.Println("Saved", len(rows), "rows to", tableName)
return false
}
func SplitWithEscaping(s, separator, escape string) []string {
s = strings.ReplaceAll(s, escape+separator, "\x00")
tokens := strings.Split(s, separator)
for i, token := range tokens {
tokens[i] = strings.ReplaceAll(token, "\x00", separator)
}
return tokens
}
var regColumnName, regErr = regexp.Compile("[^a-zA-Z_]+")
func parseOldPacket(x string) *ParsedPacket {
if regErr != nil {
log.Fatal(regErr)
return nil
}
if !strings.HasSuffix(x, "\n") {
fmt.Println("No newline at end")
return nil
}
x = strings.TrimSuffix(x, "\n")
parts := SplitWithEscaping(x, ",", "\\")
tableName := regColumnName.ReplaceAllString(parts[0], "")
if tableName == "" {
fmt.Println("Table name is empty")
return nil
}
isColumn := true
columnName := ""
items := make([]ColValue, 0, (len(parts)-1)/2)
for _, v := range parts[1:] {
isString := strings.HasPrefix(v, "\"")
v = strings.ReplaceAll(v, "\\\"", "\x00")
v = strings.ReplaceAll(v, "\"", "")
v = strings.ReplaceAll(v, "\x00", "\"")
if isColumn {
columnName = regColumnName.ReplaceAllString(v, "")
} else {
if isString {
items = append(items, ColValue{column: columnName, value: v})
} else if v == "t" {
items = append(items, ColValue{column: columnName, value: true})
} else if v == "f" {
items = append(items, ColValue{column: columnName, value: false})
} else if v == "n" {
items = append(items, ColValue{column: columnName, value: nil})
} else {
parsed, err := strconv.ParseFloat(v, 32)
if err == nil {
items = append(items, ColValue{column: columnName, value: parsed})
} else {
fmt.Println("Old format col error", tableName, columnName)
}
}
}
isColumn = !isColumn
}
sort.Slice(items, func(i, j int) bool {
return items[i].column < items[j].column
})
columns := make([]string, 0, len(items))
for _, v := range items {
columns = append(columns, v.column)
}
values := make([]interface{}, 0, len(items))
for _, v := range items {
values = append(values, v.value)
}
return &ParsedPacket{
tableName: tableName,
columns: columns,
values: values,
}
}