Skip to content

Commit

Permalink
tools: fix the inconsistency of available space (#5670)
Browse files Browse the repository at this point in the history
ref #5468

Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx authored Nov 14, 2022
1 parent 674375a commit e6eef7c
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 123 deletions.
7 changes: 2 additions & 5 deletions tools/pd-simulator/simulator/cases/add_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ func newAddNodes() *Case {

for i := 1; i <= storeNum; i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 900 * units.GiB,
Version: "2.1.0",
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
})
}

Expand Down
7 changes: 2 additions & 5 deletions tools/pd-simulator/simulator/cases/add_nodes_dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ func newAddNodesDynamic() *Case {

for i := 1; i <= int(noEmptyStoreNum); i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 900 * units.GiB,
Version: "2.1.0",
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
})
}

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

for i := 1; i <= storeNum; i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 900 * units.GiB,
Version: "2.1.0",
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,20 @@ func newRedundantBalanceRegion() *Case {
var simCase Case

storeNum := simutil.CaseConfigure.StoreNum
regionNum := simutil.CaseConfigure.RegionNum * storeNum / 3
regionNum := simutil.CaseConfigure.RegionNum
if storeNum == 0 || regionNum == 0 {
storeNum, regionNum = 6, 4000
}

for i := 0; i < storeNum; i++ {
s := &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
}
if i%2 == 1 {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 980 * units.GiB,
Version: "2.1.0",
})
} else {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 1 * units.TiB,
Version: "2.1.0",
})
s.HasExtraUsedSpace = true
}
simCase.Stores = append(simCase.Stores, s)
}

for i := 0; i < regionNum; i++ {
Expand Down
16 changes: 8 additions & 8 deletions tools/pd-simulator/simulator/cases/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import (

// Store is used to simulate tikv.
type Store struct {
ID uint64
Status metapb.StoreState
Labels []*metapb.StoreLabel
Capacity uint64
Available uint64
LeaderWeight float32
RegionWeight float32
Version string
ID uint64
Status metapb.StoreState
Labels []*metapb.StoreLabel
Capacity uint64
LeaderWeight float32
RegionWeight float32
Version string
HasExtraUsedSpace bool
}

// Region is used to simulate a region.
Expand Down
7 changes: 2 additions & 5 deletions tools/pd-simulator/simulator/cases/delete_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ func newDeleteNodes() *Case {
noEmptyStoreNum := storeNum - 1
for i := 1; i <= storeNum; i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 900 * units.GiB,
Version: "2.1.0",
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
})
}

Expand Down
41 changes: 13 additions & 28 deletions tools/pd-simulator/simulator/cases/diagnose_label_isolation.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,14 @@ func newLabelNotMatch1() *Case {
for i := 0; i < num1; i++ {
id := IDAllocator.nextID()
simCase.Stores = append(simCase.Stores, &Store{
ID: id,
Status: metapb.StoreState_Up,
Capacity: 100 * units.GiB,
Available: 50 * units.GiB,
Version: "2.1.0",
Labels: []*metapb.StoreLabel{{Key: "host", Value: fmt.Sprintf("host%d", id)}},
ID: id,
Status: metapb.StoreState_Up,
Labels: []*metapb.StoreLabel{{Key: "host", Value: fmt.Sprintf("host%d", id)}},
})
}
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 100 * units.GiB,
Available: 50 * units.GiB,
Version: "2.1.0",
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
})

for i := 0; i < regionNum; i++ {
Expand Down Expand Up @@ -103,23 +97,17 @@ func newLabelIsolation1() *Case {
for i := 0; i < num1; i++ {
id := IDAllocator.nextID()
simCase.Stores = append(simCase.Stores, &Store{
ID: id,
Status: metapb.StoreState_Up,
Capacity: 1000 * units.GiB,
Available: 500 * units.GiB,
Version: "2.1.0",
Labels: []*metapb.StoreLabel{{Key: "host", Value: fmt.Sprintf("host%d", id)}},
ID: id,
Status: metapb.StoreState_Up,
Labels: []*metapb.StoreLabel{{Key: "host", Value: fmt.Sprintf("host%d", id)}},
})
}
id := IDAllocator.GetID() + 1
for i := 0; i < num2; i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1000 * units.GiB,
Available: 500 * units.GiB,
Version: "2.1.0",
Labels: []*metapb.StoreLabel{{Key: "host", Value: fmt.Sprintf("host%d", id)}},
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Labels: []*metapb.StoreLabel{{Key: "host", Value: fmt.Sprintf("host%d", id)}},
})
}

Expand Down Expand Up @@ -174,11 +162,8 @@ func newLabelIsolation2() *Case {
for i := 0; i < storeNum; i++ {
id := IDAllocator.nextID()
simCase.Stores = append(simCase.Stores, &Store{
ID: id,
Status: metapb.StoreState_Up,
Capacity: 100 * units.GiB,
Available: 50 * units.GiB,
Version: "2.1.0",
ID: id,
Status: metapb.StoreState_Up,
})
}
simCase.Stores[0].Labels = []*metapb.StoreLabel{{Key: "dc", Value: "dc1"}, {Key: "zone", Value: "zone1"}, {Key: "host", Value: "host1"}}
Expand Down
14 changes: 4 additions & 10 deletions tools/pd-simulator/simulator/cases/diagnose_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ func newRule1() *Case {
for i := 0; i < storeNum; i++ {
id := IDAllocator.nextID()
simCase.Stores = append(simCase.Stores, &Store{
ID: id,
Status: metapb.StoreState_Up,
Capacity: 1000 * units.GiB,
Available: 500 * units.GiB,
Version: "2.1.0",
ID: id,
Status: metapb.StoreState_Up,
})
}
simCase.Stores[0].Labels = []*metapb.StoreLabel{{Key: "region", Value: "region2"}, {Key: "idc", Value: "idc1"}}
Expand Down Expand Up @@ -154,11 +151,8 @@ func newRule2() *Case {
for i := 0; i < storeNum; i++ {
id := IDAllocator.nextID()
simCase.Stores = append(simCase.Stores, &Store{
ID: id,
Status: metapb.StoreState_Up,
Capacity: 1000 * units.GiB,
Available: 500 * units.GiB,
Version: "2.1.0",
ID: id,
Status: metapb.StoreState_Up,
})
}
simCase.Stores[0].Labels = []*metapb.StoreLabel{{Key: "region", Value: "region1"}}
Expand Down
7 changes: 2 additions & 5 deletions tools/pd-simulator/simulator/cases/hot_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ func newHotRead() *Case {
// Initialize the cluster
for i := 1; i <= storeNum; i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 900 * units.GiB,
Version: "2.1.0",
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
})
}

Expand Down
7 changes: 2 additions & 5 deletions tools/pd-simulator/simulator/cases/hot_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ func newHotWrite() *Case {
// Initialize the cluster
for i := 1; i <= storeNum; i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 900 * units.GiB,
Version: "2.1.0",
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
})
}

Expand Down
7 changes: 2 additions & 5 deletions tools/pd-simulator/simulator/cases/import_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ func newImportData() *Case {
// Initialize the cluster
for i := 1; i <= 10; i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 900 * units.GiB,
Version: "2.1.0",
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
})
}

Expand Down
7 changes: 2 additions & 5 deletions tools/pd-simulator/simulator/cases/makeup_down_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ func newMakeupDownReplicas() *Case {
noEmptyStoreNum := storeNum - 1
for i := 1; i <= storeNum; i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 900 * units.GiB,
Version: "2.1.0",
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
})
}

Expand Down
7 changes: 2 additions & 5 deletions tools/pd-simulator/simulator/cases/region_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ func newRegionMerge() *Case {
storeNum, regionNum := getStoreNum(), getRegionNum()
for i := 1; i <= storeNum; i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 900 * units.GiB,
Version: "2.1.0",
ID: IDAllocator.nextID(),
Status: metapb.StoreState_Up,
})
}

Expand Down
7 changes: 2 additions & 5 deletions tools/pd-simulator/simulator/cases/region_split.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ func newRegionSplit() *Case {
storeNum := getStoreNum()
for i := 1; i <= storeNum; i++ {
simCase.Stores = append(simCase.Stores, &Store{
ID: uint64(i),
Status: metapb.StoreState_Up,
Capacity: 1 * units.TiB,
Available: 900 * units.GiB,
Version: "2.1.0",
ID: uint64(i),
Status: metapb.StoreState_Up,
})
}
peers := []*metapb.Peer{
Expand Down
10 changes: 6 additions & 4 deletions tools/pd-simulator/simulator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ import (
"github.com/tikv/pd/pkg/typeutil"
"github.com/tikv/pd/server/config"
"github.com/tikv/pd/server/schedule/placement"
"github.com/tikv/pd/server/versioninfo"
)

const (
// tick
defaultSimTickInterval = 100 * time.Millisecond
// store
defaultStoreIOMBPerSecond = 40
defaultStoreVersion = "2.1.0"
defaultStoreHeartbeat = 10 * time.Second
defaultRegionHeartbeat = 1 * time.Minute
defaultRegionSplitKeys = 960000
defaultRegionSplitSize = 96 * units.MiB
defaultCapacity = 3000 * units.GiB
defaultCapacity = 1 * units.TiB
defaultExtraUsedSpace = 0
// server
defaultLeaderLease = 3
defaultTSOSaveInterval = 200 * time.Millisecond
Expand All @@ -63,7 +64,7 @@ type SimConfig struct {
// RaftStore the configuration for raft store.
type RaftStore struct {
Capacity typeutil.ByteSize `toml:"capacity" json:"capacity"`
Available typeutil.ByteSize `toml:"available" json:"available"`
ExtraUsedSpace typeutil.ByteSize `toml:"extra-used-space" json:"extra-used-space"`
RegionHeartBeatInterval typeutil.Duration `toml:"pd-heartbeat-tick-interval" json:"pd-heartbeat-tick-interval"`
StoreHeartBeatInterval typeutil.Duration `toml:"pd-store-heartbeat-tick-interval" json:"pd-store-heartbeat-tick-interval"`
}
Expand Down Expand Up @@ -125,10 +126,11 @@ func adjustByteSize(v *typeutil.ByteSize, defValue typeutil.ByteSize) {
func (sc *SimConfig) Adjust(meta *toml.MetaData) error {
adjustDuration(&sc.SimTickInterval, defaultSimTickInterval)
adjustInt64(&sc.StoreIOMBPerSecond, defaultStoreIOMBPerSecond)
adjustString(&sc.StoreVersion, defaultStoreVersion)
adjustString(&sc.StoreVersion, versioninfo.PDReleaseVersion)
adjustDuration(&sc.RaftStore.RegionHeartBeatInterval, defaultRegionHeartbeat)
adjustDuration(&sc.RaftStore.StoreHeartBeatInterval, defaultStoreHeartbeat)
adjustByteSize(&sc.RaftStore.Capacity, defaultCapacity)
adjustByteSize(&sc.RaftStore.ExtraUsedSpace, defaultExtraUsedSpace)
adjustUint64(&sc.Coprocessor.RegionSplitKey, defaultRegionSplitKeys)
adjustByteSize(&sc.Coprocessor.RegionSplitSize, defaultRegionSplitSize)

Expand Down
12 changes: 12 additions & 0 deletions tools/pd-simulator/simulator/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func (d *Driver) Prepare() error {
d.raftEngine = NewRaftEngine(d.simCase, d.conn, d.simConfig)
d.eventRunner = NewEventRunner(d.simCase.Events, d.raftEngine)

d.updateNodeAvailable()

// Bootstrap.
store, region, err := d.GetBootstrapInfo(d.raftEngine)
if err != nil {
Expand Down Expand Up @@ -218,3 +220,13 @@ func (d *Driver) GetBootstrapInfo(r *RaftEngine) (*metapb.Store, *metapb.Region,
}
return store.Store, region.GetMeta(), nil
}

func (d *Driver) updateNodeAvailable() {
for storeID, n := range d.conn.Nodes {
if n.hasExtraUsedSpace {
n.stats.StoreStats.Available = n.stats.StoreStats.Capacity - uint64(d.raftEngine.regionsInfo.GetStoreRegionSize(storeID)) - uint64(d.simConfig.RaftStore.ExtraUsedSpace)
} else {
n.stats.StoreStats.Available = n.stats.StoreStats.Capacity - uint64(d.raftEngine.regionsInfo.GetStoreRegionSize(storeID))
}
}
}
9 changes: 4 additions & 5 deletions tools/pd-simulator/simulator/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,10 @@ func (e *AddNodes) Run(raft *RaftEngine, tickCount int64) bool {

config := raft.storeConfig
s := &cases.Store{
ID: id,
Status: metapb.StoreState_Up,
Capacity: uint64(config.RaftStore.Capacity),
Available: uint64(config.RaftStore.Available),
Version: config.StoreVersion,
ID: id,
Status: metapb.StoreState_Up,
Capacity: uint64(config.RaftStore.Capacity),
Version: config.StoreVersion,
}
n, err := NewNode(s, raft.conn.pdAddr, config)
if err != nil {
Expand Down
Loading

0 comments on commit e6eef7c

Please sign in to comment.