forked from nikepan/clickhouse-bulk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (105 loc) · 2.84 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
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
)
var version = "unknown"
var date = "unknown"
type clickhouseConfig struct {
Servers []string `json:"servers"`
DownTimeout int `json:"down_timeout"`
}
type config struct {
Listen string `json:"listen"`
Clickhouse clickhouseConfig `json:"clickhouse"`
FlushCount int `json:"flush_count"`
FlushInterval int `json:"flush_interval"`
DumpDir string `json:"dump_dir"`
Debug bool `json:"debug"`
}
// SafeQuit - safe prepare to quit
func SafeQuit(collect *Collector, sender Sender) {
collect.FlushAll()
if count := sender.Len(); count > 0 {
log.Printf("Sending %+v tables\n", count)
}
for !sender.Empty() && !collect.Empty() {
collect.WaitFlush()
}
collect.WaitFlush()
}
func main() {
log.SetOutput(os.Stdout)
configFile := flag.String("config", "config.json", "config file (json)")
flag.Parse()
if flag.Arg(0) == "version" {
log.Printf("clickhouse-bulk ver. %+v (%+v)\n", version, date)
return
}
cnf := config{}
err := ReadJSON(*configFile, &cnf)
if err != nil {
log.Printf("Config file %+v not found. Use config.sample.json\n", *configFile)
err := ReadJSON("config.sample.json", &cnf)
if err != nil {
log.Fatalf("Read config: %+v\n", err.Error())
}
}
serversList := os.Getenv("CLICKHOUSE_SERVERS")
if serversList != "" {
cnf.Clickhouse.Servers = strings.Split(serversList, ",")
log.Printf("use servers: %+v\n", serversList)
}
flushCount := os.Getenv("CLICKHOUSE_FLUSH_COUNT")
if flushCount != "" {
cnf.FlushCount, err = strconv.Atoi(flushCount)
if err != nil {
log.Fatalf("Wrong flush count env: %+v\n", err.Error())
}
}
flushInterval := os.Getenv("CLICKHOUSE_FLUSH_INTERVAL")
if flushInterval != "" {
cnf.FlushInterval, err = strconv.Atoi(flushInterval)
if err != nil {
log.Fatalf("Wrong flush interval env: %+v\n", err.Error())
}
}
dumper := new(FileDumper)
dumper.Path = cnf.DumpDir
sender := NewClickhouse(cnf.Clickhouse.DownTimeout)
sender.Dumper = dumper
for _, url := range cnf.Clickhouse.Servers {
sender.AddServer(url)
}
collect := NewCollector(sender, cnf.FlushCount, cnf.FlushInterval)
// send collected data on SIGTERM and exit
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
srv := InitServer(cnf.Listen, collect, cnf.Debug)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
go func() {
for {
_ = <-signals
log.Printf("STOP signal\n")
if err := srv.Shutdown(ctx); err != nil {
log.Printf("Shutdown error %+v\n", err)
SafeQuit(collect, sender)
os.Exit(1)
}
}
}()
err = srv.Start()
if err != nil {
log.Printf("ListenAndServe: %+v\n", err)
SafeQuit(collect, sender)
os.Exit(1)
}
}