-
Notifications
You must be signed in to change notification settings - Fork 49
/
carbon-clickhouse.go
170 lines (138 loc) · 3.33 KB
/
carbon-clickhouse.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
package main
import (
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/signal"
"runtime/debug"
"syscall"
"time"
"github.com/lomik/carbon-clickhouse/carbon"
"github.com/lomik/carbon-clickhouse/helper/RowBinary"
"github.com/lomik/zapwriter"
"go.uber.org/zap"
_ "net/http/pprof"
)
// Version of carbon-clickhouse
const Version = "0.11.8"
func httpServe(addr string) (func(), error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return nil, err
}
listener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
return nil, err
}
go http.Serve(listener, nil)
return func() { listener.Close() }, nil
}
func main() {
var err error
/* CONFIG start */
configFile := flag.String("config", "/etc/carbon-clickhouse/carbon-clickhouse.conf", "Filename of config")
printDefaultConfig := flag.Bool("config-print-default", false, "Print default config")
checkConfig := flag.Bool("check-config", false, "Check config and exit")
exactConfig := flag.Bool("exact-config", false, "Ensure that all config params are contained in the target struct.")
printVersion := flag.Bool("version", false, "Print version")
cat := flag.String("cat", "", "Print RowBinary file in TabSeparated format")
bincat := flag.String("recover", "", "Read all good records from corrupted data file. Write binary data to stdout")
flag.Parse()
if *printVersion {
fmt.Print(Version)
return
}
if *cat != "" {
reader, err := RowBinary.NewReader(*cat, false)
if err != nil {
log.Fatal(err)
}
for {
metric, err := reader.ReadRecord()
if err != nil {
if err == io.EOF {
return
}
log.Fatal(err)
}
fmt.Printf("%s\t%#v\t%d\t%s\t%d\n",
string(metric),
reader.Value(),
reader.Timestamp(),
reader.DaysString(),
reader.Version(),
)
}
}
if *bincat != "" {
reader, err := RowBinary.NewReader(*bincat, false)
if err != nil {
log.Fatal(err)
}
io.Copy(os.Stdout, reader)
return
}
if *printDefaultConfig {
if err = carbon.PrintDefaultConfig(); err != nil {
log.Fatal(err)
}
return
}
app := carbon.New(*configFile)
if err = app.ParseConfig(*exactConfig); err != nil {
log.Fatal(err)
}
// config parsed successfully. Exit in check-only mode
if *checkConfig {
return
}
cfg := app.Config
if err = zapwriter.ApplyConfig(cfg.Logging); err != nil {
log.Fatal(err)
}
mainLogger := zapwriter.Logger("main")
/* CONFIG end */
// pprof
if cfg.Pprof.Enabled {
_, err = httpServe(cfg.Pprof.Listen)
if err != nil {
mainLogger.Fatal("pprof listen failed", zap.Error(err))
}
}
if err = app.Start(); err != nil {
mainLogger.Fatal("app start failed", zap.Error(err))
} else {
mainLogger.Info("app started")
}
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
for {
s := <-c
switch s {
case syscall.SIGUSR1:
mainLogger.Info("SIGUSR1 received. Clear tree cache")
app.Reset()
case syscall.SIGUSR2:
mainLogger.Info("SIGUSR2 received. Ignoring")
case syscall.SIGHUP:
mainLogger.Info("SIGHUP received. Ignoring")
case syscall.SIGTERM, syscall.SIGINT:
mainLogger.Info("shutting down")
app.Stop()
}
}
}()
go func() {
for {
debug.FreeOSMemory()
time.Sleep(30 * time.Minute)
}
}()
app.Loop()
mainLogger.Info("app stopped")
}