-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
133 lines (117 loc) · 3.18 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
package main
import (
"context"
"flag"
"fmt"
"io"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/BurntSushi/toml"
uuid "github.com/satori/go.uuid"
log "github.com/siddontang/go-log/log"
"github.com/siddontang/go-mysql/replication"
)
func init() {
logHandler, _ := log.NewStreamHandler(os.Stdout)
logger := log.New(logHandler, 1|4)
log.SetDefaultLogger(logger)
}
var (
Config = newConfiguration()
Version, GitCommit string
wg sync.WaitGroup
)
func main() {
flagSet := flag.NewFlagSet("proxysq_binlog", flag.ExitOnError)
configFile := flagSet.String("config", "/etc/proxysql-binlog.cnf", "Path to config file")
debug := flagSet.Bool("debug", false, "Debug mode")
version := flagSet.Bool("version", false, "Print version")
flagSet.Parse(os.Args[1:])
if *version {
fmt.Println("Version:\t", Version)
fmt.Println("Git commit:\t", GitCommit)
os.Exit(0)
}
log.SetLevel(log.LevelInfo)
if _, err := toml.DecodeFile(*configFile, &Config); err != nil {
log.Fatalf("unable to decode coniguration file %s: %v", *configFile, err)
}
if Config.General.LogFile != "" {
logFile, err := os.OpenFile(Config.General.LogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0640)
if err != nil {
log.Fatalf("unable to open log file %s: %v", Config.General.LogFile, err)
}
mw := io.MultiWriter(os.Stdout, logFile)
logHandler, _ := log.NewStreamHandler(mw)
logger := log.New(logHandler, 1|4)
log.SetDefaultLogger(logger)
defer logHandler.Close()
}
if strings.ToLower(Config.General.LogLevel) == "debug" {
*debug = true
}
if *debug {
log.SetLevel(log.LevelDebug)
}
dispatcher := NewDispatcher()
quit := make(chan os.Signal)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
err := ListenAndServeTCP(Config.General.ListenAddress, dispatcher, ctx)
if err != nil {
log.Fatalf("unable to start TCP server: %v", err)
}
metrics := NewMetricsServer()
go func() {
createReader:
binlogReader, err := NewBinlogReader()
if err != nil {
log.Fatalf("unable to start binlog reader: %v", err)
}
binlogStreamer, err := binlogReader.StartSync()
if err != nil {
log.Fatalf("unable to start binlog streamer: %v", err)
}
wg.Add(1)
for {
ev, err := binlogStreamer.GetEvent(ctx)
if err != nil {
if err == ctx.Err() {
log.Info("stopping BinlogReader")
binlogReader.Close()
wg.Done()
return
}
log.Errorf("error during getting binlog event: %v", err)
readerErrors.Inc()
binlogReader.Close()
wg.Done()
goto createReader
}
if ev.Header.EventType.String() == "GTIDEvent" {
event := >IDEvent{}
gtidEvent, _ := ev.Event.(*replication.GTIDEvent)
u, err := uuid.FromBytes(gtidEvent.SID)
if err != nil {
log.Errorf("Unable to parse GTID: %v", err)
readerErrors.Inc()
continue
}
event.gtidSID = UUID(u)
event.gtidGNO = gtidEvent.GNO
log.Debugf("got gtid event: %s", fmt.Sprintf("%s:%d", event.gtidSID.String(), event.gtidGNO))
gtidProcessed.Inc()
dispatcher.Events <- event
}
}
}()
go signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
log.Infof("got %s signal. Stopping", <-quit)
cancel()
metrics.Close()
wg.Wait()
os.Exit(0)
}