This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
/
main.go
315 lines (260 loc) · 7.88 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
package cmd
import (
"bytes"
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof" // this is effectively a main package
"os"
"os/signal"
"runtime"
"sync/atomic"
"syscall"
"time"
"github.com/vkcom/engine-go/srvfunc"
"github.com/vkcom/kittenhouse/core/clickhouse"
"github.com/vkcom/kittenhouse/core/destination"
"github.com/vkcom/kittenhouse/core/inmem"
"github.com/vkcom/kittenhouse/core/persist"
)
var (
// StartServerCallback is the callback you need to use to use your own RPC protocol instead of HTTP
StartServerCallback func(host string, port uint) error
// Build* can be filled in during build using go build -ldflags
BuildTime string
BuildOSUname string
BuildCommit string
buildVersion string // concatination of Build* into a single string
)
var (
argv struct {
reverse bool
host string
port uint
help bool
version bool
markAsDone bool
user string
group string
log string
maxOpenFiles uint64
nProc uint
pprofHostPort string
chHost string
config string
dir string
maxSendSize int64
maxFileSize int64
rotateIntervalSec int64
}
logFd *os.File
)
const (
FlagPersistent = 1 << iota
FlagDebug
FlagRowBinary
ErrCodeNotSupported = 300
heartbeatInterval = time.Hour
maxUDPPacketSize = 2048 // even less, actually
debug = false
)
func init() {
buildVersion = fmt.Sprintf(`kittenhouse compiled at %s by %s after %s on %s`, BuildTime, runtime.Version(),
BuildCommit, BuildOSUname,
)
log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)
// actions
flag.BoolVar(&argv.help, `h`, false, `show this help`)
flag.BoolVar(&argv.version, `version`, false, `show version`)
flag.BoolVar(&argv.reverse, `reverse`, false, `start reverse proxy server instead (ch-addr is used as clickhouse host-port)`)
// common options
flag.StringVar(&argv.host, `host`, `0.0.0.0`, `listening host`)
flag.UintVar(&argv.port, `port`, 13338, `listening port. REQUIRED`)
flag.UintVar(&argv.port, `p`, 13338, `listening port. REQUIRED`)
flag.StringVar(&argv.user, `u`, `kitten`, "setuid user (if needed)")
flag.StringVar(&argv.group, `g`, `kitten`, "setgid user (if needed)")
flag.StringVar(&argv.log, `l`, "", "log file (if needed)")
flag.StringVar(&argv.chHost, `ch-addr`, `127.0.0.1:8123`, `default clickhouse host:port`)
flag.UintVar(&argv.nProc, `cores`, uint(0), `max cpu cores usage`)
flag.StringVar(&argv.pprofHostPort, `pprof`, ``, `host:port for http pprof`)
flag.Uint64Var(&argv.maxOpenFiles, `max-open-files`, 262144, `open files limit`)
// local proxy options
flag.StringVar(&argv.config, `c`, ``, `path to routing config`)
flag.StringVar(&argv.dir, `dir`, `/tmp/kittenhouse`, `dir for persistent logs`)
flag.Int64Var(&argv.maxSendSize, `max-send-size`, 1<<20, `max batch size to be sent to clickhouse in bytes`)
flag.Int64Var(&argv.maxFileSize, `max-file-size`, 50<<20, `max file size in bytes`)
flag.Int64Var(&argv.rotateIntervalSec, `rotate-interval-sec`, 1800, `how often to rotate files`)
flag.BoolVar(&argv.markAsDone, `mark-as-done`, false, `rename files to *.done instead of deleting them upon successful delivery`)
flag.Parse()
}
func updateThread(ch chan os.Signal) {
for range ch {
updateConfig()
reopenLog()
}
}
var (
oldConf destination.Map
configUpdateTs int32 // UNIX ts
configHash atomic.Value // string
)
func updateConfig() {
log.Println("Updating config")
var newConf destination.Map
var ts time.Time
var confHash string
var err error
if argv.config != "" {
newConf, ts, confHash, err = parseConfigFile(argv.config)
if err != nil {
log.Printf("Error: Bad config: %s", err.Error())
return
}
} else {
ts = time.Now()
newConf, confHash, err = parseConfig(bytes.NewBufferString(`* ` + argv.chHost))
if err != nil {
log.Printf("Error: Bad default config: %s", err.Error())
return
}
}
if oldConf != nil {
for _, settings := range oldConf {
settings.Destroy()
}
}
oldConf = newConf
atomic.StoreInt32(&configUpdateTs, int32(ts.Unix()))
configHash.Store(confHash)
inmem.UpdateDestinationsConfig(newConf)
persist.UpdateDestinationsConfig(newConf)
clickhouse.UpdateDestinationsConfig(newConf)
}
func reopenLog() {
if argv.log == "" {
return
}
var err error
logFd, err = srvfunc.LogRotate(logFd, argv.log)
if err != nil {
os.Stderr.WriteString(fmt.Sprintf(`Cannot log to file "%s": %s`, argv.log, err.Error()))
return
}
log.SetOutput(logFd)
}
func tryIncreaseRlimit() {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Printf("Could not get rlimit: %s", err.Error())
return
}
rLimit.Max = argv.maxOpenFiles
rLimit.Cur = argv.maxOpenFiles
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Printf("Could not set new rlimit: %s", err.Error())
return
}
}
func avgCPU(old, new syscall.Timeval, duration time.Duration) float32 {
if duration <= 0 {
return 0
}
return float32(float64(new.Nano()-old.Nano()) / float64(duration*time.Nanosecond))
}
func heartbeatThread() {
var oldRusage syscall.Rusage
syscall.Getrusage(syscall.RUSAGE_SELF, &oldRusage)
prevTs := time.Now()
for {
var curRusage syscall.Rusage
syscall.Getrusage(syscall.RUSAGE_SELF, &curRusage)
var rss uint64
if st, _ := srvfunc.GetMemStat(0); st != nil {
rss = st.Res
}
dur := time.Since(prevTs)
configHashStr, _ := configHash.Load().(string)
persist.Heartbeat(
buildVersion,
BuildCommit,
atomic.LoadInt32(&configUpdateTs),
configHashStr,
rss,
avgCPU(oldRusage.Utime, curRusage.Utime, dur),
avgCPU(oldRusage.Stime, curRusage.Stime, dur),
)
time.Sleep(heartbeatInterval)
}
}
// Main is actual main function for kittenhouse but allows to register certain hooks beforehand.
func Main() {
if argv.version {
fmt.Fprint(os.Stderr, buildVersion, "\n")
return
} else if argv.help {
flag.Usage()
return
}
if argv.nProc > 0 {
runtime.GOMAXPROCS(int(argv.nProc))
} else {
argv.nProc = uint(runtime.NumCPU())
}
if argv.pprofHostPort != `` {
go func() {
if err := http.ListenAndServe(argv.pprofHostPort, nil); err != nil {
log.Printf(`pprof listen fail: %s`, err.Error())
}
}()
}
tryIncreaseRlimit()
if argv.group != "" {
if err := srvfunc.ChangeGroup(argv.group); err != nil {
log.Fatalf("Could not change group to %s: %s", argv.group, err.Error())
}
}
if argv.user != "" {
if err := srvfunc.ChangeUser(argv.user); err != nil {
log.Fatalf("Could not change user to %s: %s", argv.user, err.Error())
}
}
if argv.reverse {
listenAddr := fmt.Sprintf("%s:%d", argv.host, argv.port)
log.Printf("Starting reverse proxy at %s (proxy to %s)", listenAddr, argv.chHost)
err := clickhouse.RunReverseProxy(listenAddr, argv.chHost)
log.Fatalf("Could not run reverse proxy: %s", err.Error())
}
clickhouse.Init()
persist.Init(persist.Config{
Dir: argv.dir,
MaxSendSize: argv.maxSendSize,
MaxFileSize: argv.maxFileSize,
RotateInterval: time.Duration(argv.rotateIntervalSec) * time.Second,
MarkAsDone: argv.markAsDone,
Port: argv.port,
})
persist.InternalLog("start", "", 0, "", "version: "+buildVersion+" args:"+fmt.Sprint(os.Args))
updCh := make(chan os.Signal, 10)
signal.Notify(updCh, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGUSR2)
reopenLog()
updateConfig()
go updateThread(updCh)
go heartbeatThread()
go func() {
if err := StartServerCallback(argv.host, argv.port); err != nil {
log.Fatalf("Could not listen rpc: %s", err.Error())
}
log.Printf("Listening %s:%d (TCP)", argv.host, argv.port)
}()
go listenUDP()
sigChan := make(chan os.Signal, 10)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
<-sigChan
log.Printf("Flushing offsets map")
if err := persist.FlushAcknowlegedOffsetsMap(); err != nil {
log.Printf("Could not flush acknowledged offsets map: %s", err.Error())
}
}