forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
212 lines (186 loc) · 6.2 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
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"net"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/juju/errors"
"github.com/ngaut/log"
"github.com/ngaut/systimemon"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/perfschema"
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/server"
"github.com/pingcap/tidb/sessionctx/binloginfo"
"github.com/pingcap/tidb/store/localstore/boltdb"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/util/printer"
"github.com/pingcap/tipb/go-binlog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
"google.golang.org/grpc"
)
var (
version = flag.Bool("V", false, "print version information and exit")
store = flag.String("store", "goleveldb", "registered store name, [memory, goleveldb, boltdb, tikv]")
storePath = flag.String("path", "/tmp/tidb", "tidb storage path")
logLevel = flag.String("L", "info", "log level: info, debug, warn, error, fatal")
host = flag.String("host", "0.0.0.0", "tidb server host")
port = flag.String("P", "4000", "tidb server port")
statusPort = flag.String("status", "10080", "tidb server status port")
lease = flag.String("lease", "1s", "schema lease duration, very dangerous to change only if you know what you do")
socket = flag.String("socket", "", "The socket file to use for connection.")
enablePS = flag.Bool("perfschema", false, "If enable performance schema.")
reportStatus = flag.Bool("report-status", true, "If enable status report HTTP service.")
logFile = flag.String("log-file", "", "log file path")
joinCon = flag.Int("join-concurrency", 5, "the number of goroutines that participate joining.")
crossJoin = flag.Bool("cross-join", true, "whether support cartesian product or not.")
metricsAddr = flag.String("metrics-addr", "", "prometheus pushgateway address, leaves it empty will disable prometheus push.")
metricsInterval = flag.Int("metrics-interval", 15, "prometheus client push interval in second, set \"0\" to disable prometheus push.")
binlogSocket = flag.String("binlog-socket", "", "socket file to write binlog")
)
func main() {
tidb.RegisterLocalStore("boltdb", boltdb.Driver{})
tidb.RegisterStore("tikv", tikv.Driver{})
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
if *version {
printer.PrintRawTiDBInfo()
os.Exit(0)
}
leaseDuration := parseLease()
tidb.SetSchemaLease(leaseDuration)
cfg := &server.Config{
Addr: fmt.Sprintf("%s:%s", *host, *port),
LogLevel: *logLevel,
StatusAddr: fmt.Sprintf(":%s", *statusPort),
Socket: *socket,
ReportStatus: *reportStatus,
}
// set log options
if len(*logFile) > 0 {
err := log.SetOutputByName(*logFile)
if err != nil {
log.Fatal(errors.ErrorStack(err))
}
log.SetRotateByDay()
log.SetHighlighting(false)
}
if joinCon != nil && *joinCon > 0 {
plan.JoinConcurrency = *joinCon
}
plan.AllowCartesianProduct = *crossJoin
// Call this before setting log level to make sure that TiDB info could be printed.
printer.PrintTiDBInfo()
log.SetLevelByString(cfg.LogLevel)
store := createStore()
if *enablePS {
perfschema.EnablePerfSchema()
}
if *binlogSocket != "" {
createBinlogClient()
}
// Create a session to load information schema.
se, err := tidb.CreateSession(store)
if err != nil {
log.Fatal(errors.ErrorStack(err))
}
se.Close()
var driver server.IDriver
driver = server.NewTiDBDriver(store)
var svr *server.Server
svr, err = server.NewServer(cfg, driver)
if err != nil {
log.Fatal(errors.ErrorStack(err))
}
sc := make(chan os.Signal, 1)
signal.Notify(sc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
sig := <-sc
log.Infof("Got signal [%d] to exit.", sig)
svr.Close()
os.Exit(0)
}()
go systimemon.StartMonitor(time.Now, func() {
log.Error("error: system time jump backward")
})
pushMetric(*metricsAddr, time.Duration(*metricsInterval)*time.Second)
log.Error(svr.Run())
}
func createStore() kv.Storage {
fullPath := fmt.Sprintf("%s://%s", *store, *storePath)
store, err := tidb.NewStore(fullPath)
if err != nil {
log.Fatal(errors.ErrorStack(err))
}
return store
}
func createBinlogClient() {
dialerOpt := grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
})
clientCon, err := grpc.Dial(*binlogSocket, dialerOpt, grpc.WithInsecure())
if err != nil {
log.Fatal(errors.ErrorStack(err))
}
binloginfo.PumpClient = binlog.NewPumpClient(clientCon)
}
// Prometheus push.
const zeroDuration = time.Duration(0)
// PushMetric pushs metircs in background.
func pushMetric(addr string, interval time.Duration) {
if interval == zeroDuration || len(addr) == 0 {
log.Info("disable Prometheus push client")
return
}
log.Infof("start Prometheus push client with server addr %s and interval %d", addr, interval)
go prometheusPushClient(addr, interval)
}
// PrometheusPushClient pushs metrics to Prometheus Pushgateway.
func prometheusPushClient(addr string, interval time.Duration) {
// TODO: TiDB do not have uniq name, so we use host+port to compose a name.
job := "tidb"
for {
err := push.AddFromGatherer(
job, push.HostnameGroupingKey(),
addr,
prometheus.DefaultGatherer,
)
if err != nil {
log.Errorf("could not push metrics to Prometheus Pushgateway: %v", err)
}
time.Sleep(interval)
}
}
// parseLease parses lease argument string.
func parseLease() time.Duration {
dur, err := time.ParseDuration(*lease)
if err != nil {
dur, err = time.ParseDuration(*lease + "s")
}
if err != nil || dur < 0 {
log.Fatalf("invalid lease duration %s", *lease)
}
return dur
}