-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (112 loc) · 2.83 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
// Copyright 2016 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 (
"context"
"flag"
"os"
"os/signal"
"syscall"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
log "github.com/pingcap/log"
"github.com/pingcap/pd/pkg/logutil"
"github.com/pingcap/pd/pkg/metricutil"
"github.com/pingcap/pd/server"
"github.com/pingcap/pd/server/api"
"github.com/pkg/errors"
"go.uber.org/zap"
// Register schedulers.
_ "github.com/pingcap/pd/server/schedulers"
// Register namespace classifiers.
_ "github.com/pingcap/pd/table"
)
func main() {
cfg := server.NewConfig()
err := cfg.Parse(os.Args[1:])
if cfg.Version {
server.PrintPDInfo()
exit(0)
}
defer logutil.LogPanic()
switch errors.Cause(err) {
case nil:
case flag.ErrHelp:
exit(0)
default:
log.Fatal("parse cmd flags error", zap.Error(err))
}
if cfg.ConfigCheck {
server.PrintConfigCheckMsg(cfg)
exit(0)
}
// New zap logger
err = cfg.SetupLogger()
if err == nil {
log.ReplaceGlobals(cfg.GetZapLogger(), cfg.GetZapLogProperties())
} else {
log.Fatal("initialize logger error", zap.Error(err))
}
// Flushing any buffered log entries
defer log.Sync()
// The old logger
err = logutil.InitLogger(&cfg.Log)
if err != nil {
log.Fatal("initialize logger error", zap.Error(err))
}
server.LogPDInfo()
for _, msg := range cfg.WarningMsgs {
log.Warn(msg)
}
// TODO: Make it configurable if it has big impact on performance.
grpc_prometheus.EnableHandlingTimeHistogram()
metricutil.Push(&cfg.Metric)
err = server.PrepareJoinCluster(cfg)
if err != nil {
log.Fatal("join meet error", zap.Error(err))
}
svr, err := server.CreateServer(cfg, api.NewHandler)
if err != nil {
log.Fatal("create server failed", zap.Error(err))
}
if err = server.InitHTTPClient(svr); err != nil {
log.Fatal("initial http client for api handler failed", zap.Error(err))
}
sc := make(chan os.Signal, 1)
signal.Notify(sc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
ctx, cancel := context.WithCancel(context.Background())
var sig os.Signal
go func() {
sig = <-sc
cancel()
}()
if err := svr.Run(ctx); err != nil {
log.Fatal("run server failed", zap.Error(err))
}
<-ctx.Done()
log.Info("Got signal to exit", zap.String("signal", sig.String()))
svr.Close()
switch sig {
case syscall.SIGTERM:
exit(0)
default:
exit(1)
}
}
func exit(code int) {
log.Sync()
os.Exit(code)
}