From ffd4351fb4ca9536065c6c0259a11d00baf6bacd Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Tue, 10 Jan 2023 17:19:53 +0800 Subject: [PATCH] resolve the conflicts Signed-off-by: Ryan Leung --- pkg/core/store_test.go | 25 +++- server/core/test_util.go | 210 --------------------------- server/schedulers/balance_witness.go | 2 +- 3 files changed, 22 insertions(+), 215 deletions(-) delete mode 100644 server/core/test_util.go diff --git a/pkg/core/store_test.go b/pkg/core/store_test.go index 1a27ed7ab64a..9e41dbbf4c11 100644 --- a/pkg/core/store_test.go +++ b/pkg/core/store_test.go @@ -183,13 +183,13 @@ func TestLowSpaceScoreV2(t *testing.T) { small: newStoreInfoWithAvailable(2, 60*units.GiB, 100*units.GiB, 1), }, { // store1's capacity is less than store2's capacity, but store2 has more available space, - bigger: NewStoreInfoWithAvailable(1, 2*units.GiB, 100*units.GiB, 3), - small: NewStoreInfoWithAvailable(2, 100*units.GiB, 10*1000*units.GiB, 3), + bigger: newStoreInfoWithAvailable(1, 2*units.GiB, 100*units.GiB, 3), + small: newStoreInfoWithAvailable(2, 100*units.GiB, 10*1000*units.GiB, 3), }, { // store2 has extra file size (70GB), it can balance region from store1 to store2. // See https://github.com/tikv/pd/issues/5790 - small: NewStoreInfoWithDisk(1, 400*units.MiB, 6930*units.GiB, 7000*units.GiB, 400), - bigger: NewStoreInfoWithAvailable(2, 1500*units.GiB, 7000*units.GiB, 1.32), + small: newStoreInfoWithDisk(1, 400*units.MiB, 6930*units.GiB, 7000*units.GiB, 400), + bigger: newStoreInfoWithAvailable(2, 1500*units.GiB, 7000*units.GiB, 1.32), delta: 37794, }} for _, v := range testdata { @@ -216,3 +216,20 @@ func newStoreInfoWithAvailable(id, available, capacity uint64, amp float64) *Sto ) return store } + +// newStoreInfoWithDisk is created with all disk infos. +func newStoreInfoWithDisk(id, used, available, capacity, regionSize uint64) *StoreInfo { + stats := &pdpb.StoreStats{} + stats.Capacity = capacity + stats.Available = available + stats.UsedSize = used + store := NewStoreInfo( + &metapb.Store{ + Id: id, + }, + SetStoreStats(stats), + SetRegionCount(int(regionSize/96)), + SetRegionSize(int64(regionSize)), + ) + return store +} diff --git a/server/core/test_util.go b/server/core/test_util.go deleted file mode 100644 index 055623edca40..000000000000 --- a/server/core/test_util.go +++ /dev/null @@ -1,210 +0,0 @@ -// Copyright 2016 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 core - -import ( - "math" - "math/rand" - "time" - - "github.com/docker/go-units" - "github.com/pingcap/kvproto/pkg/metapb" - "github.com/pingcap/kvproto/pkg/pdpb" -) - -// SplitRegions split a set of RegionInfo by the middle of regionKey -func SplitRegions(regions []*RegionInfo) []*RegionInfo { - results := make([]*RegionInfo, 0, len(regions)*2) - for _, region := range regions { - start, end := byte(0), byte(math.MaxUint8) - if len(region.GetStartKey()) > 0 { - start = region.GetStartKey()[0] - } - if len(region.GetEndKey()) > 0 { - end = region.GetEndKey()[0] - } - middle := []byte{start/2 + end/2} - left := region.Clone() - left.meta.Id = region.GetID() + uint64(len(regions)) - left.meta.EndKey = middle - left.meta.RegionEpoch.Version++ - right := region.Clone() - right.meta.Id = region.GetID() + uint64(len(regions)*2) - right.meta.StartKey = middle - right.meta.RegionEpoch.Version++ - results = append(results, left, right) - } - return results -} - -// MergeRegions merge a set of RegionInfo by regionKey -func MergeRegions(regions []*RegionInfo) []*RegionInfo { - results := make([]*RegionInfo, 0, len(regions)/2) - for i := 0; i < len(regions); i += 2 { - left := regions[i] - right := regions[i] - if i+1 < len(regions) { - right = regions[i+1] - } - region := &RegionInfo{meta: &metapb.Region{ - Id: left.GetID(), - StartKey: left.GetStartKey(), - EndKey: right.GetEndKey(), - Peers: left.meta.Peers, - }} - if left.GetRegionEpoch().GetVersion() > right.GetRegionEpoch().GetVersion() { - region.meta.RegionEpoch = left.GetRegionEpoch() - } else { - region.meta.RegionEpoch = right.GetRegionEpoch() - } - region.meta.RegionEpoch.Version++ - region.leader = left.leader - results = append(results, region) - } - return results -} - -// NewTestRegionInfo creates a RegionInfo for test. -func NewTestRegionInfo(start, end []byte) *RegionInfo { - return &RegionInfo{meta: &metapb.Region{ - StartKey: start, - EndKey: end, - RegionEpoch: &metapb.RegionEpoch{}, - }} -} - -// NewStoreInfoWithDisk is created with all disk infos. -func NewStoreInfoWithDisk(id, used, available, capacity, regionSize uint64) *StoreInfo { - stats := &pdpb.StoreStats{} - stats.Capacity = capacity - stats.Available = available - stats.UsedSize = used - store := NewStoreInfo( - &metapb.Store{ - Id: id, - }, - SetStoreStats(stats), - SetRegionCount(int(regionSize/96)), - SetRegionSize(int64(regionSize)), - ) - return store -} - -// NewStoreInfoWithAvailable is created with available and capacity -func NewStoreInfoWithAvailable(id, available, capacity uint64, amp float64) *StoreInfo { - usedSize := capacity - available - regionSize := (float64(usedSize) * amp) / units.MiB - return NewStoreInfoWithDisk(id, usedSize, available, capacity, uint64(regionSize)) -} - -// NewStoreInfoWithLabel is create a store with specified labels. -func NewStoreInfoWithLabel(id uint64, regionCount int, labels map[string]string) *StoreInfo { - storeLabels := make([]*metapb.StoreLabel, 0, len(labels)) - for k, v := range labels { - storeLabels = append(storeLabels, &metapb.StoreLabel{ - Key: k, - Value: v, - }) - } - stats := &pdpb.StoreStats{} - stats.Capacity = units.GiB / units.MiB - stats.Available = units.GiB / units.MiB - store := NewStoreInfo( - &metapb.Store{ - Id: id, - Labels: storeLabels, - }, - SetStoreStats(stats), - SetRegionCount(regionCount), - SetRegionSize(int64(regionCount)*10), - ) - return store -} - -// NewStoreInfoWithSizeCount is create a store with size and count. -func NewStoreInfoWithSizeCount(id uint64, regionCount, leaderCount int, regionSize, leaderSize int64) *StoreInfo { - stats := &pdpb.StoreStats{} - stats.Capacity = units.GiB / units.MiB - stats.Available = units.GiB / units.MiB - store := NewStoreInfo( - &metapb.Store{ - Id: id, - }, - SetStoreStats(stats), - SetRegionCount(regionCount), - SetRegionSize(regionSize), - SetLeaderCount(leaderCount), - SetLeaderSize(leaderSize), - ) - return store -} - -// RandomKindReadQuery returns query stat with random query kind, only used for unit test. -func RandomKindReadQuery(queryRead uint64) *pdpb.QueryStats { - r := rand.New(rand.NewSource(time.Now().UnixNano())) - switch r.Intn(3) { - case 0: - return &pdpb.QueryStats{ - Coprocessor: queryRead, - } - case 1: - return &pdpb.QueryStats{ - Scan: queryRead, - } - case 2: - return &pdpb.QueryStats{ - Get: queryRead, - } - default: - return &pdpb.QueryStats{} - } -} - -// RandomKindWriteQuery returns query stat with random query kind, only used for unit test. -func RandomKindWriteQuery(queryWrite uint64) *pdpb.QueryStats { - r := rand.New(rand.NewSource(time.Now().UnixNano())) - switch r.Intn(7) { - case 0: - return &pdpb.QueryStats{ - Put: queryWrite, - } - case 1: - return &pdpb.QueryStats{ - Delete: queryWrite, - } - case 2: - return &pdpb.QueryStats{ - DeleteRange: queryWrite, - } - case 3: - return &pdpb.QueryStats{ - AcquirePessimisticLock: queryWrite, - } - case 4: - return &pdpb.QueryStats{ - Rollback: queryWrite, - } - case 5: - return &pdpb.QueryStats{ - Prewrite: queryWrite, - } - case 6: - return &pdpb.QueryStats{ - Commit: queryWrite, - } - default: - return &pdpb.QueryStats{} - } -} diff --git a/server/schedulers/balance_witness.go b/server/schedulers/balance_witness.go index 8903752321dd..046d854a8b0a 100644 --- a/server/schedulers/balance_witness.go +++ b/server/schedulers/balance_witness.go @@ -25,11 +25,11 @@ import ( "github.com/gorilla/mux" "github.com/pingcap/log" "github.com/prometheus/client_golang/prometheus" + "github.com/tikv/pd/pkg/core" "github.com/tikv/pd/pkg/errs" "github.com/tikv/pd/pkg/storage/endpoint" "github.com/tikv/pd/pkg/utils/reflectutil" "github.com/tikv/pd/pkg/utils/syncutil" - "github.com/tikv/pd/server/core" "github.com/tikv/pd/server/schedule" "github.com/tikv/pd/server/schedule/filter" "github.com/tikv/pd/server/schedule/operator"