Skip to content

Commit

Permalink
tools: support triggering an event through HTTP API (#5677)
Browse files Browse the repository at this point in the history
close #5451, ref #5468

Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx authored May 31, 2024
1 parent 632cda4 commit 19c9852
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 371 deletions.
30 changes: 4 additions & 26 deletions tools/pd-simulator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ package main
import (
"context"
"fmt"
"net/http"
"net/http/pprof"
"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/pkg/schedule/schedulers"
"github.com/tikv/pd/pkg/statistics"
Expand Down Expand Up @@ -95,8 +92,7 @@ func main() {

func run(simCase string, simConfig *sc.SimConfig) {
if *pdAddr != "" {
go runHTTPServer()
simStart(*pdAddr, simCase, simConfig)
simStart(*pdAddr, *statusAddress, simCase, simConfig)
} else {
local, clean := NewSingleServer(context.Background(), simConfig)
err := local.Run()
Expand All @@ -109,28 +105,10 @@ func run(simCase string, simConfig *sc.SimConfig) {
}
time.Sleep(100 * time.Millisecond)
}
simStart(local.GetAddr(), simCase, simConfig, clean)
simStart(local.GetAddr(), "", simCase, simConfig, clean)
}
}

func runHTTPServer() {
http.Handle("/metrics", promhttp.Handler())
// profile API
http.HandleFunc("/pprof/profile", pprof.Profile)
http.HandleFunc("/pprof/trace", pprof.Trace)
http.HandleFunc("/pprof/symbol", pprof.Symbol)
http.Handle("/pprof/heap", pprof.Handler("heap"))
http.Handle("/pprof/mutex", pprof.Handler("mutex"))
http.Handle("/pprof/allocs", pprof.Handler("allocs"))
http.Handle("/pprof/block", pprof.Handler("block"))
http.Handle("/pprof/goroutine", pprof.Handler("goroutine"))
server := &http.Server{
Addr: *statusAddress,
ReadHeaderTimeout: 3 * time.Second,
}
server.ListenAndServe()
}

// NewSingleServer creates a pd server for simulator.
func NewSingleServer(ctx context.Context, simConfig *sc.SimConfig) (*server.Server, testutil.CleanupFunc) {
err := logutil.SetupLogger(simConfig.ServerConfig.Log, &simConfig.ServerConfig.Logger, &simConfig.ServerConfig.LogProps)
Expand All @@ -157,9 +135,9 @@ func cleanServer(cfg *config.Config) {
os.RemoveAll(cfg.DataDir)
}

func simStart(pdAddr string, simCase string, simConfig *sc.SimConfig, clean ...testutil.CleanupFunc) {
func simStart(pdAddr, statusAddress string, simCase string, simConfig *sc.SimConfig, clean ...testutil.CleanupFunc) {
start := time.Now()
driver, err := simulator.NewDriver(pdAddr, simCase, simConfig)
driver, err := simulator.NewDriver(pdAddr, statusAddress, simCase, simConfig)
if err != nil {
simutil.Logger.Fatal("create driver error", zap.Error(err))
}
Expand Down
71 changes: 0 additions & 71 deletions tools/pd-simulator/simulator/cases/add_nodes.go

This file was deleted.

92 changes: 0 additions & 92 deletions tools/pd-simulator/simulator/cases/add_nodes_dynamic.go

This file was deleted.

9 changes: 5 additions & 4 deletions tools/pd-simulator/simulator/cases/balance_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/tikv/pd/pkg/core"
sc "github.com/tikv/pd/tools/pd-simulator/simulator/config"
"github.com/tikv/pd/tools/pd-simulator/simulator/info"
"github.com/tikv/pd/tools/pd-simulator/simulator/simutil"
)

func newBalanceLeader(config *sc.SimConfig) *Case {
Expand All @@ -30,7 +31,7 @@ func newBalanceLeader(config *sc.SimConfig) *Case {
replica := int(config.ServerConfig.Replication.MaxReplicas)
for i := 0; i < totalStore; i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
ID: simutil.IDAllocator.NextID(),
Status: metapb.StoreState_Up,
})
}
Expand All @@ -39,17 +40,17 @@ func newBalanceLeader(config *sc.SimConfig) *Case {
for i := 0; i < totalRegion; i++ {
peers := make([]*metapb.Peer, 0, replica)
peers = append(peers, &metapb.Peer{
Id: IDAllocator.nextID(),
Id: simutil.IDAllocator.NextID(),
StoreId: leaderStoreID,
})
for j := 1; j < replica; j++ {
peers = append(peers, &metapb.Peer{
Id: IDAllocator.nextID(),
Id: simutil.IDAllocator.NextID(),
StoreId: uint64((i+j)%(totalStore-1) + 1),
})
}
simCase.Regions = append(simCase.Regions, Region{
ID: IDAllocator.nextID(),
ID: simutil.IDAllocator.NextID(),
Peers: peers,
Leader: peers[0],
Size: 96 * units.MiB,
Expand Down
7 changes: 4 additions & 3 deletions tools/pd-simulator/simulator/cases/balance_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/tikv/pd/pkg/core"
sc "github.com/tikv/pd/tools/pd-simulator/simulator/config"
"github.com/tikv/pd/tools/pd-simulator/simulator/info"
"github.com/tikv/pd/tools/pd-simulator/simulator/simutil"
)

func newRedundantBalanceRegion(config *sc.SimConfig) *Case {
Expand All @@ -32,7 +33,7 @@ func newRedundantBalanceRegion(config *sc.SimConfig) *Case {

for i := 0; i < totalStore; i++ {
s := &Store{
ID: IDAllocator.nextID(),
ID: simutil.IDAllocator.NextID(),
Status: metapb.StoreState_Up,
}
if i%2 == 1 {
Expand All @@ -45,12 +46,12 @@ func newRedundantBalanceRegion(config *sc.SimConfig) *Case {
peers := make([]*metapb.Peer, 0, replica)
for j := 0; j < replica; j++ {
peers = append(peers, &metapb.Peer{
Id: IDAllocator.nextID(),
Id: simutil.IDAllocator.NextID(),
StoreId: uint64((i+j)%totalStore + 1),
})
}
simCase.Regions = append(simCase.Regions, Region{
ID: IDAllocator.nextID(),
ID: simutil.IDAllocator.NextID(),
Peers: peers,
Leader: peers[0],
})
Expand Down
16 changes: 0 additions & 16 deletions tools/pd-simulator/simulator/cases/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package cases

import (
"math/rand"

"github.com/pingcap/kvproto/pkg/metapb"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/schedule/placement"
Expand Down Expand Up @@ -91,9 +89,6 @@ var IDAllocator idAllocator
var CaseMap = map[string]func(*config.SimConfig) *Case{
"balance-leader": newBalanceLeader,
"redundant-balance-region": newRedundantBalanceRegion,
"add-nodes": newAddNodes,
"add-nodes-dynamic": newAddNodesDynamic,
"delete-nodes": newDeleteNodes,
"region-split": newRegionSplit,
"region-merge": newRegionMerge,
"hot-read": newHotRead,
Expand Down Expand Up @@ -121,14 +116,3 @@ func isUniform(count, meanCount int) bool {
minCount := int((1.0 - threshold) * float64(meanCount))
return minCount <= count && count <= maxCount
}

func getNoEmptyStoreNum(storeNum int, replica int) int {
noEmptyStoreNum := rand.Intn(storeNum)
if noEmptyStoreNum < replica {
return replica
}
if noEmptyStoreNum == storeNum {
return storeNum - 1
}
return noEmptyStoreNum
}
Loading

0 comments on commit 19c9852

Please sign in to comment.