Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schedule: use public stores cache in placement rules cache #5877

Merged
merged 6 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions server/schedule/placement/region_rule_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,24 @@ import (
// 6. any store label is changed
// 7. any store state is changed
type RegionRuleFitCacheManager struct {
mu syncutil.RWMutex
caches map[uint64]*RegionRuleFitCache
mu syncutil.RWMutex
regionCaches map[uint64]*regionRuleFitCache
storeCaches map[uint64]*storeCache
}

// NewRegionRuleFitCacheManager returns RegionRuleFitCacheManager
func NewRegionRuleFitCacheManager() *RegionRuleFitCacheManager {
return &RegionRuleFitCacheManager{
caches: map[uint64]*RegionRuleFitCache{},
regionCaches: make(map[uint64]*regionRuleFitCache),
storeCaches: make(map[uint64]*storeCache),
}
}

// Invalid invalid cache by regionID
// Invalid cache by regionID
func (manager *RegionRuleFitCacheManager) Invalid(regionID uint64) {
manager.mu.Lock()
defer manager.mu.Unlock()
delete(manager.caches, regionID)
delete(manager.regionCaches, regionID)
}

// CheckAndGetCache checks whether the region and rules are changed for the stored cache
Expand All @@ -63,7 +65,7 @@ func (manager *RegionRuleFitCacheManager) CheckAndGetCache(region *core.RegionIn
}
manager.mu.RLock()
defer manager.mu.RUnlock()
if cache, ok := manager.caches[region.GetID()]; ok && cache.bestFit != nil {
if cache, ok := manager.regionCaches[region.GetID()]; ok && cache.bestFit != nil {
if cache.IsUnchanged(region, rules, stores) {
return true, cache.bestFit
}
Expand All @@ -79,26 +81,26 @@ func (manager *RegionRuleFitCacheManager) SetCache(region *core.RegionInfo, fit
manager.mu.Lock()
defer manager.mu.Unlock()
fit.SetCached(true)
manager.caches[region.GetID()] = toRegionRuleFitCache(region, fit)
manager.regionCaches[region.GetID()] = manager.toRegionRuleFitCache(region, fit)
}

// RegionRuleFitCache stores regions RegionFit result and involving variables
type RegionRuleFitCache struct {
// regionRuleFitCache stores regions RegionFit result and involving variables
type regionRuleFitCache struct {
region regionCache
regionStores []storeCache
regionStores []*storeCache
rules []ruleCache
bestFit *RegionFit
}

// IsUnchanged checks whether the region and rules unchanged for the cache
func (cache *RegionRuleFitCache) IsUnchanged(region *core.RegionInfo, rules []*Rule, stores []*core.StoreInfo) bool {
func (cache *regionRuleFitCache) IsUnchanged(region *core.RegionInfo, rules []*Rule, stores []*core.StoreInfo) bool {
if !ValidateRegion(region) || !ValidateStores(stores) {
return false
}
return cache.isRegionUnchanged(region) && rulesEqual(cache.rules, rules) && storesEqual(cache.regionStores, stores)
}

func (cache *RegionRuleFitCache) isRegionUnchanged(region *core.RegionInfo) bool {
func (cache *regionRuleFitCache) isRegionUnchanged(region *core.RegionInfo) bool {
return region.GetLeader().StoreId == cache.region.leaderStoreID &&
cache.region.epochEqual(region)
}
Expand All @@ -114,7 +116,7 @@ func rulesEqual(ruleCaches []ruleCache, rules []*Rule) bool {
})
}

func storesEqual(a []storeCache, b []*core.StoreInfo) bool {
func storesEqual(a []*storeCache, b []*core.StoreInfo) bool {
if len(a) != len(b) {
return false
}
Expand All @@ -125,10 +127,10 @@ func storesEqual(a []storeCache, b []*core.StoreInfo) bool {
})
}

func toRegionRuleFitCache(region *core.RegionInfo, fit *RegionFit) *RegionRuleFitCache {
return &RegionRuleFitCache{
func (manager *RegionRuleFitCacheManager) toRegionRuleFitCache(region *core.RegionInfo, fit *RegionFit) *regionRuleFitCache {
return &regionRuleFitCache{
region: toRegionCache(region),
regionStores: toStoreCacheList(fit.regionStores),
regionStores: manager.toStoreCacheList(fit.regionStores),
rules: toRuleCacheList(fit.rules),
bestFit: fit,
}
Expand Down Expand Up @@ -175,17 +177,22 @@ func (s storeCache) storeEqual(store *core.StoreInfo) bool {
labelEqual(s.labels, store.GetLabels())
}

func toStoreCacheList(stores []*core.StoreInfo) (c []storeCache) {
func (manager *RegionRuleFitCacheManager) toStoreCacheList(stores []*core.StoreInfo) (c []*storeCache) {
for _, s := range stores {
m := make(map[string]string)
for _, label := range s.GetLabels() {
m[label.GetKey()] = label.GetValue()
sCache, ok := manager.storeCaches[s.GetID()]
if !ok || !sCache.storeEqual(s) {
m := make(map[string]string)
for _, label := range s.GetLabels() {
m[label.GetKey()] = label.GetValue()
}
sCache = &storeCache{
storeID: s.GetID(),
labels: m,
state: s.GetState(),
}
manager.storeCaches[s.GetID()] = sCache
}
c = append(c, storeCache{
storeID: s.GetID(),
labels: m,
state: s.GetState(),
})
c = append(c, sCache)
}
return c
}
Expand Down
32 changes: 28 additions & 4 deletions server/schedule/placement/region_rule_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package placement
import (
"testing"
"time"
"unsafe"

"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
Expand All @@ -29,7 +30,8 @@ func TestRegionRuleFitCache(t *testing.T) {
originRegion := mockRegion(3, 0)
originRules := addExtraRules(0)
originStores := mockStores(3)
cache := mockRegionRuleFitCache(originRegion, originRules, originStores)
cacheManager := NewRegionRuleFitCacheManager()
cache := cacheManager.mockRegionRuleFitCache(originRegion, originRules, originStores)
testCases := []struct {
name string
region *core.RegionInfo
Expand Down Expand Up @@ -192,10 +194,30 @@ func TestRegionRuleFitCache(t *testing.T) {
re.False(cache.IsUnchanged(originRegion, originRules, originStores))
}

func mockRegionRuleFitCache(region *core.RegionInfo, rules []*Rule, regionStores []*core.StoreInfo) *RegionRuleFitCache {
return &RegionRuleFitCache{
func TestPublicStoreCaches(t *testing.T) {
re := require.New(t)
cacheManager := NewRegionRuleFitCacheManager()

stores1 := mockStores(3)
rules1 := addExtraRules(0)
region1 := mockRegion(3, 0)
cache1 := cacheManager.mockRegionRuleFitCache(region1, rules1, stores1)

stores2 := mockStores(3)
rules2 := addExtraRules(0)
region2 := mockRegion(3, 0)
cache2 := cacheManager.mockRegionRuleFitCache(region2, rules2, stores2)

for i, storeCache1 := range cache1.regionStores {
storeCache2 := cache2.regionStores[i]
re.Equal(unsafe.Pointer(storeCache2), unsafe.Pointer(storeCache1))
}
}

func (manager *RegionRuleFitCacheManager) mockRegionRuleFitCache(region *core.RegionInfo, rules []*Rule, regionStores []*core.StoreInfo) *regionRuleFitCache {
return &regionRuleFitCache{
region: toRegionCache(region),
regionStores: toStoreCacheList(regionStores),
regionStores: manager.toStoreCacheList(regionStores),
rules: toRuleCacheList(rules),
bestFit: &RegionFit{
regionStores: regionStores,
Expand All @@ -204,6 +226,7 @@ func mockRegionRuleFitCache(region *core.RegionInfo, rules []*Rule, regionStores
}
}

// nolint
func mockStores(num int) []*core.StoreInfo {
stores := make([]*core.StoreInfo, 0, num)
now := time.Now()
Expand All @@ -214,6 +237,7 @@ func mockStores(num int) []*core.StoreInfo {
return stores
}

// nolint
func mockStoresNoHeartbeat(num int) []*core.StoreInfo {
stores := make([]*core.StoreInfo, 0, num)
for i := 1; i <= num; i++ {
Expand Down
6 changes: 3 additions & 3 deletions server/schedule/placement/rule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,18 +353,18 @@ func (m *RuleManager) InvalidCache(regionID uint64) {
// SetPlaceholderRegionFitCache sets a placeholder region fit cache information
// Only used for testing
func (m *RuleManager) SetPlaceholderRegionFitCache(region *core.RegionInfo) {
placeholderCache := &RegionRuleFitCache{region: toRegionCache(region)}
placeholderCache := &regionRuleFitCache{region: toRegionCache(region)}
m.cache.mu.Lock()
defer m.cache.mu.Unlock()
m.cache.caches[region.GetID()] = placeholderCache
m.cache.regionCaches[region.GetID()] = placeholderCache
}

// CheckIsCachedDirectly returns whether the region's fit is cached
// Only used for testing
func (m *RuleManager) CheckIsCachedDirectly(regionID uint64) bool {
m.cache.mu.RLock()
defer m.cache.mu.RUnlock()
_, ok := m.cache.caches[regionID]
_, ok := m.cache.regionCaches[regionID]
return ok
}

Expand Down