Skip to content

Commit

Permalink
support pd dev
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <jinhao.hu@pingcap.com>
  • Loading branch information
HuSharp committed May 21, 2024
1 parent 2fabb74 commit b9ff645
Show file tree
Hide file tree
Showing 77 changed files with 2,246 additions and 1,779 deletions.
24 changes: 5 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ BUILD_BIN_PATH := $(ROOT_PATH)/bin

build: pd-server pd-ctl pd-recover

tools: pd-tso-bench pd-heartbeat-bench regions-dump stores-dump pd-api-bench pd-ut
tools: pd-dev

PD_SERVER_DEP :=
ifeq ($(SWAGGER), 1)
Expand Down Expand Up @@ -110,28 +110,14 @@ pd-server-basic:
# Tools
pd-ctl:
cd tools && GOEXPERIMENT=$(BUILD_GOEXPERIMENT) CGO_ENABLED=$(BUILD_TOOL_CGO_ENABLED) go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/pd-ctl pd-ctl/main.go
pd-tso-bench:
cd tools && CGO_ENABLED=0 go build -o $(BUILD_BIN_PATH)/pd-tso-bench pd-tso-bench/main.go
pd-api-bench:
cd tools && CGO_ENABLED=0 go build -o $(BUILD_BIN_PATH)/pd-api-bench pd-api-bench/main.go
pd-dev: pd-xprog
cd tools && GOEXPERIMENT=$(BUILD_GOEXPERIMENT) CGO_ENABLED=$(BUILD_TOOL_CGO_ENABLED) go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/pd-dev pd-dev/main.go
pd-recover:
cd tools && GOEXPERIMENT=$(BUILD_GOEXPERIMENT) CGO_ENABLED=$(BUILD_TOOL_CGO_ENABLED) go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/pd-recover pd-recover/main.go
pd-analysis:
cd tools && CGO_ENABLED=0 go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/pd-analysis pd-analysis/main.go
pd-heartbeat-bench:
cd tools && CGO_ENABLED=0 go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/pd-heartbeat-bench pd-heartbeat-bench/main.go
simulator:
cd tools && CGO_ENABLED=0 go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/pd-simulator pd-simulator/main.go
regions-dump:
cd tools && CGO_ENABLED=0 go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/regions-dump regions-dump/main.go
stores-dump:
cd tools && CGO_ENABLED=0 go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/stores-dump stores-dump/main.go
pd-ut: pd-xprog
cd tools && GOEXPERIMENT=$(BUILD_GOEXPERIMENT) CGO_ENABLED=$(BUILD_TOOL_CGO_ENABLED) go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/pd-ut pd-ut/ut.go pd-ut/coverProfile.go
pd-xprog:
cd tools && GOEXPERIMENT=$(BUILD_GOEXPERIMENT) CGO_ENABLED=$(BUILD_TOOL_CGO_ENABLED) go build -tags xprog -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/xprog pd-ut/xprog.go
cd tools && GOEXPERIMENT=$(BUILD_GOEXPERIMENT) CGO_ENABLED=$(BUILD_TOOL_CGO_ENABLED) go build -tags xprog -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o $(BUILD_BIN_PATH)/xprog pd-dev/pd-ut/xprog.go

.PHONY: pd-ctl pd-tso-bench pd-recover pd-analysis pd-heartbeat-bench simulator regions-dump stores-dump pd-api-bench pd-ut
.PHONY: pd-ctl pd-recover pd-dev

#### Docker image ####

Expand Down
82 changes: 0 additions & 82 deletions tools/pd-analysis/main.go

This file was deleted.

70 changes: 70 additions & 0 deletions tools/pd-dev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# pd-dev

pd-dev is a tool for developing and testing PD. It provides the following functions:

- [pd-api-bench](./pd-api-bench/README.md): A tool for testing the performance of PD's API.
- [pd-tso-bench](./pd-tso-bench/README.md): A tool for testing the performance of PD's TSO.
- [pd-heartbeat-bench](./pd-heartbeat-bench/README.md): A tool for testing the performance of PD's heartbeat.
- [pd-simulator](./pd-simulator/README.md): A tool for simulating the PD cluster.
- [regions-dump](./regions-dump/README.md): A tool for dumping the region information of the PD cluster.
- [stores-dump](./stores-dump/README.md): A tool for dumping the store information of the PD cluster.

## Build

1. [Go](https://golang.org/) Version 1.21 or later
2. In the root directory of the [PD project](https://github.com/tikv/pd), use the `make pd-dev` command to compile and generate `bin/pd-dev`

## Usage

This section describes how to use the `pd-dev` tool.

### Cases

Please read related README files for more details, we support the following cases:

`./pd-dev --mode api`

`./pd-dev --mode tso`

`./pd-dev --mode heartbeat`

`./pd-dev --mode simulator`

`./pd-dev --mode regions-dump`

`./pd-dev --mode stores-dump`

`./pd-dev --mode analysis`

`./pd-dev --mode backup`

`./pd-dev --mode ut`

### flag description

--pd string
> pd address (default "127.0.0.1:2379")
--cacert string
> path of file that contains list of trusted SSL CAs
--cert string
> path of file that contains X509 certificate in PEM format
--key string
> path of file that contains X509 key in PEM format
--config string
> path of configuration file
### TLS

You can use the following command to generate a certificate for testing TLS:

```shell
mkdir cert
./cert_opt.sh generate cert
./bin/pd-dev --mode api -http-cases GetRegionStatus-1+1,GetMinResolvedTS-1+1 -client 1 -debug true -cacert ./cert/ca.pem -cert ./cert/pd-server.pem -key ./cert/pd-server-key.pem
./cert_opt.sh cleanup cert
rm -rf cert
```
100 changes: 100 additions & 0 deletions tools/pd-dev/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2024 TiKV Project Authors.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/pingcap/log"
flag "github.com/spf13/pflag"
analysis "github.com/tikv/pd/tools/pd-dev/pd-analysis"
api "github.com/tikv/pd/tools/pd-dev/pd-api-bench"
backup "github.com/tikv/pd/tools/pd-dev/pd-backup"
heartbeat "github.com/tikv/pd/tools/pd-dev/pd-heartbeat-bench"
simulator "github.com/tikv/pd/tools/pd-dev/pd-simulator"
tso "github.com/tikv/pd/tools/pd-dev/pd-tso-bench"
ut "github.com/tikv/pd/tools/pd-dev/pd-ut"
region "github.com/tikv/pd/tools/pd-dev/regions-dump"
store "github.com/tikv/pd/tools/pd-dev/stores-dump"
"go.uber.org/zap"
)

func switchDevelopmentMode(ctx context.Context, mode string) {
log.Info("pd-dev start", zap.String("mode", mode))
switch mode {
case "api":
api.Run(ctx)
case "heartbeat":
heartbeat.Run(ctx)
case "tso":
tso.Run(ctx)
case "simulator":
simulator.Run(ctx)
case "regions-dump":
region.Run()
case "stores-dump":
store.Run()
case "analysis":
analysis.Run()
case "backup":
backup.Run()
case "ut":
ut.Run()
default:
log.Fatal("please input a mode to run, or provide a config file to run all modes")
}
}

func main() {
ctx, cancel := context.WithCancel(context.Background())

sc := make(chan os.Signal, 1)
signal.Notify(sc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
var sig os.Signal
go func() {
sig = <-sc
cancel()
}()

// parse first argument
if len(os.Args) < 2 {
log.Fatal("please input a mode to run, or provide a config file to run all modes")
}

var mode string
fs := flag.NewFlagSet("pd-dev", flag.ContinueOnError)
fs.StringVar(&mode, "mode", "", "mode to run")
fs.Parse(os.Args[1:])
switchDevelopmentMode(ctx, mode)

log.Info("pd-dev Exit")
switch sig {
case syscall.SIGTERM:
exit(0)
default:
exit(1)
}
}

func exit(code int) {
os.Exit(code)
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *TransferCounter) Init(storeNum, regionNum int) {
c.loopResultCount = c.loopResultCount[:0]
}

// AddTarget is be used to add target of edge in graph mat.
// AddTarget be used to add target of edge in graph mat.
// Firstly add a new peer and then delete the old peer of the scheduling,
// So in the statistics, also firstly add the target and then add the source.
func (c *TransferCounter) AddTarget(regionID, targetStoreID uint64) {
Expand All @@ -83,7 +83,7 @@ func (c *TransferCounter) AddTarget(regionID, targetStoreID uint64) {
c.regionMap[regionID] = targetStoreID
}

// AddSource is be used to add source of edge in graph mat.
// AddSource be used to add source of edge in graph mat.
func (c *TransferCounter) AddSource(regionID, sourceStoreID uint64) {
c.mutex.Lock()
defer c.mutex.Unlock()
Expand Down
51 changes: 51 additions & 0 deletions tools/pd-dev/pd-analysis/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2024 TiKV Project Authors.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package analysis

import (
flag "github.com/spf13/pflag"
"github.com/tikv/pd/tools/pd-dev/util"
)

// Config is the heartbeat-bench configuration.
type Config struct {
*util.GeneralConfig
Input string `toml:"input" json:"input"`
Output string `toml:"output" json:"output"`
Style string `toml:"style" json:"style"`
Operator string `toml:"operator" json:"operator"`
Start string `toml:"start" json:"start"`
End string `toml:"end" json:"end"`
}

// NewConfig return a set of settings.
func newConfig() *Config {
cfg := &Config{
GeneralConfig: &util.GeneralConfig{},
}
cfg.FlagSet = flag.NewFlagSet("pd-analysis", flag.ContinueOnError)
fs := cfg.FlagSet
cfg.GeneralConfig = util.NewGeneralConfig(fs)
fs.ParseErrorsWhitelist.UnknownFlags = true

fs.StringVar(&cfg.Input, "input", "", "input pd log file, required")
fs.StringVar(&cfg.Output, "output", "", "output file, default output to stdout")
fs.StringVar(&cfg.Style, "style", "", "analysis style, e.g. transfer-counter")
fs.StringVar(&cfg.Operator, "operator", "", "operator style, e.g. balance-region, balance-leader, transfer-hot-read-leader, move-hot-read-region, transfer-hot-write-leader, move-hot-write-region")
fs.StringVar(&cfg.Start, "start", "", "start time, e.g. 2019/09/10 12:20:07, default: total file")
fs.StringVar(&cfg.End, "end", "", "end time, e.g. 2019/09/10 14:20:07, default: total file")

return cfg
}
Loading

0 comments on commit b9ff645

Please sign in to comment.