/
main.go
180 lines (157 loc) · 4.12 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
// Copyright 2017 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"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/BurntSushi/toml"
log "github.com/pingcap/log"
"github.com/pingcap/pd/pkg/logutil"
"github.com/pingcap/pd/server"
"github.com/pingcap/pd/server/api"
"github.com/pingcap/pd/server/schedule"
"github.com/pingcap/pd/tools/pd-simulator/simulator"
"github.com/pingcap/pd/tools/pd-simulator/simulator/cases"
"github.com/pingcap/pd/tools/pd-simulator/simulator/simutil"
"go.uber.org/zap"
// Register schedulers.
_ "github.com/pingcap/pd/server/schedulers"
// Register namespace classifiers.
_ "github.com/pingcap/pd/table"
)
var (
pdAddr = flag.String("pd", "", "pd address")
configFile = flag.String("config", "", "config file")
caseName = flag.String("case", "", "case name")
serverLogLevel = flag.String("serverLog", "fatal", "pd server log level.")
simLogLevel = flag.String("simLog", "fatal", "simulator log level.")
)
func main() {
flag.Parse()
simutil.InitLogger(*simLogLevel)
schedule.Simulating = true
if *caseName == "" {
if *pdAddr != "" {
simutil.Logger.Fatal("need to specify one config name")
}
for simCase := range cases.CaseMap {
run(simCase)
}
} else {
run(*caseName)
}
}
func run(simCase string) {
simConfig := simulator.NewSimConfig()
if *configFile != "" {
if _, err := toml.DecodeFile(*configFile, simConfig); err != nil {
simutil.Logger.Fatal("fail to decode file ", zap.Error(err))
}
}
simConfig.Adjust()
if *pdAddr != "" {
simStart(*pdAddr, simCase, simConfig)
} else {
_, local, clean := NewSingleServer()
err := local.Run(context.Background())
if err != nil {
simutil.Logger.Fatal("run server error", zap.Error(err))
}
for {
if local.IsLeader() {
break
}
time.Sleep(100 * time.Millisecond)
}
simStart(local.GetAddr(), simCase, simConfig, clean)
}
}
// NewSingleServer creates a pd server for simulator.
func NewSingleServer() (*server.Config, *server.Server, server.CleanupFunc) {
cfg := server.NewTestSingleConfig()
cfg.Log.Level = *serverLogLevel
err := logutil.InitLogger(&cfg.Log)
if err != nil {
log.Fatal("initialize logger error", zap.Error(err))
}
s, err := server.CreateServer(cfg, api.NewHandler)
if err != nil {
panic("create server failed")
}
cleanup := func() {
s.Close()
cleanServer(cfg)
}
return cfg, s, cleanup
}
func cleanServer(cfg *server.Config) {
// Clean data directory
os.RemoveAll(cfg.DataDir)
}
func simStart(pdAddr string, simCase string, simConfig *simulator.SimConfig, clean ...server.CleanupFunc) {
start := time.Now()
driver, err := simulator.NewDriver(pdAddr, simCase, simConfig)
if err != nil {
simutil.Logger.Fatal("create driver error", zap.Error(err))
}
err = driver.Prepare()
if err != nil {
simutil.Logger.Fatal("simulator prepare error", zap.Error(err))
}
var runInternal bool
if len(clean) != 0 {
runInternal = true
}
var tickInterval time.Duration
if runInternal {
tickInterval = simConfig.SimTickInterval.Duration
} else {
tickInterval = simConfig.NormTickInterval.Duration
}
tick := time.NewTicker(tickInterval)
defer tick.Stop()
sc := make(chan os.Signal, 1)
signal.Notify(sc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
simResult := "FAIL"
EXIT:
for {
select {
case <-tick.C:
driver.Tick()
if driver.Check() {
simResult = "OK"
break EXIT
}
case <-sc:
break EXIT
}
}
driver.Stop()
if runInternal {
clean[0]()
}
fmt.Printf("%s [%s] total iteration: %d, time cost: %v\n", simResult, simCase, driver.TickCount(), time.Since(start))
driver.PrintStatistics()
if simResult != "OK" {
os.Exit(1)
}
}