Skip to content

Commit

Permalink
simulator: use tiup playground to deploy pd-simulator (#5289)
Browse files Browse the repository at this point in the history
close #5288

simulator: use tiup playground to deploy pd-simulator.

Signed-off-by: bufferflies <1045931706@qq.com>

Co-authored-by: ShuNing <nolouch@gmail.com>
  • Loading branch information
bufferflies and nolouch authored Jul 13, 2022
1 parent 1f5dcb3 commit 16f8ed7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
45 changes: 29 additions & 16 deletions tools/pd-simulator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ package main

import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/BurntSushi/toml"
"github.com/pingcap/log"
"github.com/prometheus/client_golang/prometheus/promhttp"
flag "github.com/spf13/pflag"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/api"
"github.com/tikv/pd/server/config"
Expand All @@ -49,9 +51,12 @@ var (
regionNum = flag.Int("regionNum", 0, "regionNum of one store")
storeNum = flag.Int("storeNum", 0, "storeNum")
enableTransferRegionCounter = flag.Bool("enableTransferRegionCounter", false, "enableTransferRegionCounter")
statusAddress = flag.String("status-addr", "0.0.0.0:20180", "status address")
)

func main() {
// ignore some undefined flag
flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
flag.Parse()

simutil.InitLogger(*simLogLevel, *simLogFile)
Expand All @@ -61,19 +66,6 @@ func main() {
analysis.GetTransferCounter().Init(simutil.CaseConfigure.StoreNum, simutil.CaseConfigure.RegionNum)
}

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(*serverLogLevel)
var meta toml.MetaData
var err error
Expand All @@ -85,12 +77,29 @@ func run(simCase string) {
if err = simConfig.Adjust(&meta); err != nil {
simutil.Logger.Fatal("failed to adjust simulator configuration", zap.Error(err))
}
if len(*caseName) == 0 {
*caseName = simConfig.CaseName
}

if *caseName == "" {
if *pdAddr != "" {
simutil.Logger.Fatal("need to specify one config name")
}
for simCase := range cases.CaseMap {
run(simCase, simConfig)
}
} else {
run(*caseName, simConfig)
}
}

func run(simCase string, simConfig *simulator.SimConfig) {
if *pdAddr != "" {
go runMetrics()
simStart(*pdAddr, simCase, simConfig)
} else {
local, clean := NewSingleServer(context.Background(), simConfig)
err = local.Run()
err := local.Run()
if err != nil {
simutil.Logger.Fatal("run server error", zap.Error(err))
}
Expand All @@ -104,6 +113,11 @@ func run(simCase string) {
}
}

func runMetrics() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(*statusAddress, nil)
}

// NewSingleServer creates a pd server for simulator.
func NewSingleServer(ctx context.Context, simConfig *simulator.SimConfig) (*server.Server, server.CleanupFunc) {
err := simConfig.ServerConfig.SetupLogger()
Expand Down Expand Up @@ -146,7 +160,6 @@ func simStart(pdAddr string, simCase string, simConfig *simulator.SimConfig, cle
if err != nil {
simutil.Logger.Fatal("simulator prepare error", zap.Error(err))
}

tickInterval := simConfig.SimTickInterval.Duration

tick := time.NewTicker(tickInterval)
Expand Down
1 change: 1 addition & 0 deletions tools/pd-simulator/simulator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
// SimConfig is the simulator configuration.
type SimConfig struct {
// tick
CaseName string `toml:"case-name"`
SimTickInterval typeutil.Duration `toml:"sim-tick-interval"`
// store
StoreCapacityGB uint64 `toml:"store-capacity"`
Expand Down
18 changes: 18 additions & 0 deletions tools/pd-simulator/simulator/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package simulator

import "github.com/prometheus/client_golang/prometheus"

var (
snapDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "tikv",
Subsystem: "raftstore",
Name: "snapshot_duration_seconds",
Help: "Bucketed histogram of processing time (s) of handled snap requests.",
Buckets: prometheus.ExponentialBuckets(0.0005, 2, 20),
}, []string{"store", "type"})
)

func init() {
prometheus.MustRegister(snapDuration)
}

0 comments on commit 16f8ed7

Please sign in to comment.