Skip to content

Commit

Permalink
cherry pick tikv#3467 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
HunDunDM authored and ti-srebot committed Mar 12, 2021
1 parent 3f81ab7 commit bf90105
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 29 deletions.
4 changes: 4 additions & 0 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,11 @@ func (c *RaftCluster) processRegionHeartbeat(region *core.RegionInfo) error {
if c.regionStats != nil {
c.regionStats.ClearDefunctRegion(item.GetID())
}
<<<<<<< HEAD
c.labelLevelStats.ClearDefunctRegion(item.GetID(), c.GetLocationLabels())
=======
c.labelLevelStats.ClearDefunctRegion(item.GetID())
>>>>>>> 3c818178... statistics: fix the bug that the isolation level is wrong when the store lacks label (#3467)
}

// Update related stores.
Expand Down
4 changes: 4 additions & 0 deletions server/statistics/hot_peer_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ package statistics

import (
"math/rand"
<<<<<<< HEAD
=======
"time"
>>>>>>> 3c818178... statistics: fix the bug that the isolation level is wrong when the store lacks label (#3467)

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
Expand Down
56 changes: 31 additions & 25 deletions server/statistics/region_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ func (l *LabelStatistics) Observe(region *core.RegionInfo, stores []*core.StoreI
if label == regionIsolation {
return
}
l.counterDec(label)
l.labelCounter[label]--
}
l.regionLabelStats[regionID] = regionIsolation
l.counterInc(regionIsolation)
l.labelCounter[regionIsolation]++
}

// Collect collects the metrics of the label status.
Expand All @@ -208,26 +208,10 @@ func (l *LabelStatistics) Reset() {
}

// ClearDefunctRegion is used to handle the overlap region.
func (l *LabelStatistics) ClearDefunctRegion(regionID uint64, labels []string) {
func (l *LabelStatistics) ClearDefunctRegion(regionID uint64) {
if label, ok := l.regionLabelStats[regionID]; ok {
l.counterDec(label)
delete(l.regionLabelStats, regionID)
}
}

func (l *LabelStatistics) counterInc(label string) {
if label == nonIsolation {
l.labelCounter[nonIsolation]++
} else {
l.labelCounter[label]++
}
}

func (l *LabelStatistics) counterDec(label string) {
if label == nonIsolation {
l.labelCounter[nonIsolation]--
} else {
l.labelCounter[label]--
delete(l.regionLabelStats, regionID)
}
}

Expand All @@ -253,17 +237,39 @@ func getRegionLabelIsolation(stores []*core.StoreInfo, labels []string) string {
}

func notIsolatedStoresWithLabel(stores []*core.StoreInfo, label string) [][]*core.StoreInfo {
m := make(map[string][]*core.StoreInfo)
var emptyValueStores []*core.StoreInfo
valueStoresMap := make(map[string][]*core.StoreInfo)

for _, s := range stores {
labelValue := s.GetLabelValue(label)
if labelValue == "" {
continue
emptyValueStores = append(emptyValueStores, s)
} else {
valueStoresMap[labelValue] = append(valueStoresMap[labelValue], s)
}
}

if len(valueStoresMap) == 0 {
// Usually it is because all TiKVs lack this label.
if len(emptyValueStores) > 1 {
return [][]*core.StoreInfo{emptyValueStores}
}
m[labelValue] = append(m[labelValue], s)
return nil
}

var res [][]*core.StoreInfo
for _, stores := range m {
if len(stores) > 1 {
if len(emptyValueStores) == 0 {
// No TiKV lacks this label.
for _, stores := range valueStoresMap {
if len(stores) > 1 {
res = append(res, stores)
}
}
} else {
// Usually it is because some TiKVs lack this label.
// The TiKVs in each label and the TiKVs without label form a group.
for _, stores := range valueStoresMap {
stores = append(stores, emptyValueStores...)
res = append(res, stores)
}
}
Expand Down
23 changes: 19 additions & 4 deletions server/statistics/region_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,21 @@ func (t *testRegionStatisticsSuite) TestRegionLabelIsolationLevel(c *C) {
{"zone": "z1", "rack": "r2", "host": "h2"},
{"zone": "z1", "rack": "r2", "host": "h2"},
},
{
// isolated by rack
{"rack": "r1", "host": "h1"},
{"rack": "r2", "host": "h2"},
{"rack": "r3", "host": "h3"},
},
{
// isolated by host
{"zone": "z1", "rack": "r1", "host": "h1"},
{"zone": "z1", "rack": "r2", "host": "h2"},
{"zone": "z1", "host": "h3"},
},
}
res := []string{"rack", "host", "zone", "rack", "none"}
counter := map[string]int{"none": 1, "host": 1, "rack": 2, "zone": 1}
res := []string{"rack", "host", "zone", "rack", "none", "rack", "host"}
counter := map[string]int{"none": 1, "host": 2, "rack": 3, "zone": 1}
regionID := 1
f := func(labels []map[string]string, res string, locationLabels []string) {
metaStores := []*metapb.Store{
Expand Down Expand Up @@ -228,10 +240,13 @@ func (t *testRegionStatisticsSuite) TestRegionLabelIsolationLevel(c *C) {
c.Assert(label, Equals, nonIsolation)
label = getRegionLabelIsolation(nil, nil)
c.Assert(label, Equals, nonIsolation)
store := core.NewStoreInfo(&metapb.Store{Id: 1, Address: "mock://tikv-1"}, core.SetStoreLabels([]*metapb.StoreLabel{{Key: "foo", Value: "bar"}}))
label = getRegionLabelIsolation([]*core.StoreInfo{store}, locationLabels)
c.Assert(label, Equals, "zone")

regionID = 1
res = []string{"rack", "none", "zone", "rack", "none"}
counter = map[string]int{"none": 2, "host": 0, "rack": 2, "zone": 1}
res = []string{"rack", "none", "zone", "rack", "none", "rack", "none"}
counter = map[string]int{"none": 3, "host": 0, "rack": 3, "zone": 1}
locationLabels = []string{"zone", "rack"}

for i, labels := range labelsSet {
Expand Down

0 comments on commit bf90105

Please sign in to comment.