Skip to content

Commit

Permalink
support triggering an event through HTTP API
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Nov 3, 2022
1 parent 6fcb528 commit 47af78a
Show file tree
Hide file tree
Showing 18 changed files with 217 additions and 451 deletions.
27 changes: 4 additions & 23 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/server"
"github.com/tikv/pd/server/api"
Expand Down Expand Up @@ -98,8 +95,7 @@ func main() {

func run(simCase string, simConfig *simulator.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 @@ -112,25 +108,10 @@ func run(simCase string, simConfig *simulator.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"))
// nolint
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 @@ -162,9 +143,9 @@ func cleanServer(cfg *config.Config) {
os.RemoveAll(cfg.DataDir)
}

func simStart(pdAddr string, simCase string, simConfig *simulator.SimConfig, clean ...server.CleanupFunc) {
func simStart(pdAddr, statusAddress string, simCase string, simConfig *simulator.SimConfig, clean ...server.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
77 changes: 0 additions & 77 deletions tools/pd-simulator/simulator/cases/add_nodes.go

This file was deleted.

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

This file was deleted.

10 changes: 5 additions & 5 deletions tools/pd-simulator/simulator/cases/balance_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newBalanceLeader() *Case {

for i := 1; i <= storeNum; i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
ID: simutil.IDAllocator.NextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 900 * units.GiB,
Expand All @@ -40,12 +40,12 @@ func newBalanceLeader() *Case {

for i := 0; i < storeNum*regionNum/3; i++ {
peers := []*metapb.Peer{
{Id: IDAllocator.nextID(), StoreId: uint64(storeNum)},
{Id: IDAllocator.nextID(), StoreId: uint64((i+1)%(storeNum-1)) + 1},
{Id: IDAllocator.nextID(), StoreId: uint64((i+2)%(storeNum-1)) + 1},
{Id: simutil.IDAllocator.NextID(), StoreId: uint64(storeNum)},
{Id: simutil.IDAllocator.NextID(), StoreId: uint64((i+1)%(storeNum-1)) + 1},
{Id: simutil.IDAllocator.NextID(), StoreId: uint64((i+2)%(storeNum-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
39 changes: 0 additions & 39 deletions tools/pd-simulator/simulator/cases/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,37 +61,10 @@ type Case struct {
Labels typeutil.StringSlice
}

// IDAllocator is used to alloc unique ID.
type idAllocator struct {
id uint64
}

// nextID gets the next unique ID.
func (a *idAllocator) nextID() uint64 {
a.id++
return a.id
}

// ResetID resets the IDAllocator.
func (a *idAllocator) ResetID() {
a.id = 0
}

// GetID gets the current ID.
func (a *idAllocator) GetID() uint64 {
return a.id
}

// IDAllocator is used to alloc unique ID.
var IDAllocator idAllocator

// CaseMap is a mapping of the cases to the their corresponding initialize functions.
var CaseMap = map[string]func() *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 All @@ -113,10 +86,6 @@ func NewCase(name string) *Case {
return nil
}

func leaderAndRegionIsUniform(leaderCount, regionCount, regionNum int, threshold float64) bool {
return isUniform(leaderCount, regionNum/3, threshold) && isUniform(regionCount, regionNum, threshold)
}

func isUniform(count, meanCount int, threshold float64) bool {
maxCount := int((1.0 + threshold) * float64(meanCount))
minCount := int((1.0 - threshold) * float64(meanCount))
Expand All @@ -138,11 +107,3 @@ func getRegionNum() int {
}
return regionNum
}

func getNoEmptyStoreNum(storeNum int, noEmptyRatio float64) uint64 {
noEmptyStoreNum := uint64(float64(storeNum) * noEmptyRatio)
if noEmptyStoreNum < 3 || noEmptyStoreNum == uint64(storeNum) {
noEmptyStoreNum = 3
}
return noEmptyStoreNum
}
Loading

0 comments on commit 47af78a

Please sign in to comment.