-
Notifications
You must be signed in to change notification settings - Fork 727
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
Refactor cluster cache. #118
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c155f35
server: add cluster store/region cache.
qiuyesuifeng a089b9e
server: make golint happy.
qiuyesuifeng 5a59fd7
server: add more tests.
qiuyesuifeng 9f15186
server: tiny cleanup.
qiuyesuifeng 93ba02d
cmd: update kvproto vendor.
qiuyesuifeng cf972a4
server: make region cache independented.
qiuyesuifeng 0521075
server: tiny cleanup.
qiuyesuifeng a99d2ce
server: remove useless code.
qiuyesuifeng 59ac189
server: add clone function for RegionInfo.
qiuyesuifeng 5bbe91b
server: address comment.
qiuyesuifeng e1d9d8f
server: address comment.
qiuyesuifeng 77dd116
server: add region epoch check for region cache update.
qiuyesuifeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
241 changes: 171 additions & 70 deletions
241
cmd/vendor/github.com/pingcap/kvproto/pkg/pdpb/pdpb.pb.go
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
package server | ||
|
||
import ( | ||
"math/rand" | ||
"sync" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/pingcap/kvproto/pkg/metapb" | ||
"github.com/pingcap/kvproto/pkg/pdpb" | ||
) | ||
|
||
// RegionInfo is region cache info. | ||
type RegionInfo struct { | ||
region *metapb.Region | ||
// leader peer | ||
peer *metapb.Peer | ||
} | ||
|
||
func (r *RegionInfo) clone() *RegionInfo { | ||
return &RegionInfo{ | ||
region: proto.Clone(r.region).(*metapb.Region), | ||
peer: proto.Clone(r.peer).(*metapb.Peer), | ||
} | ||
} | ||
|
||
// RegionsInfo is regions cache info. | ||
type RegionsInfo struct { | ||
sync.RWMutex | ||
|
||
// region id -> RegionInfo | ||
leaderRegions map[uint64]*RegionInfo | ||
// store id -> regionid -> struct{} | ||
storeLeaderRegions map[uint64]map[uint64]struct{} | ||
} | ||
|
||
func newRegionsInfo() *RegionsInfo { | ||
return &RegionsInfo{ | ||
leaderRegions: make(map[uint64]*RegionInfo), | ||
storeLeaderRegions: make(map[uint64]map[uint64]struct{}), | ||
} | ||
} | ||
|
||
// randRegion random selects a region from region cache. | ||
func (r *RegionsInfo) randRegion(storeID uint64) *RegionInfo { | ||
r.RLock() | ||
defer r.RUnlock() | ||
|
||
storeRegions, ok := r.storeLeaderRegions[storeID] | ||
if !ok { | ||
return nil | ||
} | ||
|
||
idx, randIdx, randRegionID := 0, rand.Intn(len(storeRegions)), uint64(0) | ||
for regionID := range storeRegions { | ||
if idx == randIdx { | ||
randRegionID = regionID | ||
break | ||
} | ||
|
||
idx++ | ||
} | ||
|
||
region, ok := r.leaderRegions[randRegionID] | ||
if ok { | ||
return region.clone() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *RegionsInfo) addRegion(region *metapb.Region, leaderPeer *metapb.Peer) { | ||
r.Lock() | ||
defer r.Unlock() | ||
|
||
regionID := region.GetId() | ||
cacheRegion, regionExist := r.leaderRegions[regionID] | ||
if regionExist { | ||
// If region leader has been changed, then remove old region from store cache. | ||
oldLeaderPeer := cacheRegion.peer | ||
if oldLeaderPeer.GetId() != leaderPeer.GetId() { | ||
storeID := oldLeaderPeer.GetStoreId() | ||
storeRegions, storeExist := r.storeLeaderRegions[storeID] | ||
if storeExist { | ||
delete(storeRegions, regionID) | ||
if len(storeRegions) == 0 { | ||
delete(r.storeLeaderRegions, storeID) | ||
} | ||
} | ||
} | ||
} | ||
|
||
r.leaderRegions[regionID] = &RegionInfo{ | ||
region: region, | ||
peer: leaderPeer, | ||
} | ||
|
||
storeID := leaderPeer.GetStoreId() | ||
store, ok := r.storeLeaderRegions[storeID] | ||
if !ok { | ||
store = make(map[uint64]struct{}) | ||
r.storeLeaderRegions[storeID] = store | ||
} | ||
store[regionID] = struct{}{} | ||
} | ||
|
||
func (r *RegionsInfo) removeRegion(regionID uint64) { | ||
r.Lock() | ||
defer r.Unlock() | ||
|
||
cacheRegion, ok := r.leaderRegions[regionID] | ||
if ok { | ||
storeID := cacheRegion.peer.GetStoreId() | ||
storeRegions, ok := r.storeLeaderRegions[storeID] | ||
if ok { | ||
delete(storeRegions, regionID) | ||
if len(storeRegions) == 0 { | ||
delete(r.storeLeaderRegions, storeID) | ||
} | ||
} | ||
|
||
delete(r.leaderRegions, regionID) | ||
} | ||
} | ||
|
||
// StoreInfo is store cache info. | ||
type StoreInfo struct { | ||
store *metapb.Store | ||
|
||
// store capacity info. | ||
stats *pdpb.StoreStats | ||
} | ||
|
||
func (s *StoreInfo) clone() *StoreInfo { | ||
return &StoreInfo{ | ||
store: proto.Clone(s.store).(*metapb.Store), | ||
stats: proto.Clone(s.stats).(*pdpb.StoreStats), | ||
} | ||
} | ||
|
||
// usedRatio is the used capacity ratio of storage capacity. | ||
func (s *StoreInfo) usedRatio() float64 { | ||
if s.stats.GetCapacity() == 0 { | ||
return 0 | ||
} | ||
|
||
return float64(s.stats.GetCapacity()-s.stats.GetAvailable()) / float64(s.stats.GetCapacity()) | ||
} | ||
|
||
// ClusterInfo is cluster cache info. | ||
type ClusterInfo struct { | ||
sync.RWMutex | ||
|
||
meta *metapb.Cluster | ||
stores map[uint64]*StoreInfo | ||
regions *RegionsInfo | ||
} | ||
|
||
func newClusterInfo() *ClusterInfo { | ||
return &ClusterInfo{ | ||
stores: make(map[uint64]*StoreInfo), | ||
regions: newRegionsInfo(), | ||
} | ||
} | ||
|
||
func (c *ClusterInfo) addStore(store *metapb.Store) { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
storeInfo := &StoreInfo{ | ||
store: store, | ||
stats: &pdpb.StoreStats{}, | ||
} | ||
|
||
c.stores[store.GetId()] = storeInfo | ||
} | ||
|
||
func (c *ClusterInfo) removeStore(storeID uint64) { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
delete(c.stores, storeID) | ||
} | ||
|
||
func (c *ClusterInfo) getStore(storeID uint64) *StoreInfo { | ||
c.RLock() | ||
defer c.RUnlock() | ||
|
||
store, ok := c.stores[storeID] | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return store.clone() | ||
} | ||
|
||
func (c *ClusterInfo) getStores() map[uint64]*StoreInfo { | ||
c.RLock() | ||
defer c.RUnlock() | ||
|
||
stores := make(map[uint64]*StoreInfo, len(c.stores)) | ||
for key, store := range c.stores { | ||
stores[key] = store.clone() | ||
} | ||
|
||
return stores | ||
} | ||
|
||
func (c *ClusterInfo) getMetaStores() []metapb.Store { | ||
c.RLock() | ||
defer c.RUnlock() | ||
|
||
stores := make([]metapb.Store, 0, len(c.stores)) | ||
for _, store := range c.stores { | ||
stores = append(stores, *proto.Clone(store.store).(*metapb.Store)) | ||
} | ||
|
||
return stores | ||
} | ||
|
||
func (c *ClusterInfo) setMeta(meta *metapb.Cluster) { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
c.meta = meta | ||
} | ||
|
||
func (c *ClusterInfo) getMeta() *metapb.Cluster { | ||
c.RLock() | ||
defer c.RUnlock() | ||
|
||
return proto.Clone(c.meta).(*metapb.Cluster) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If version or conf version doesn't change, I think we can return directly, no need to update cache again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, we should add this check later, maybe in next PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so please add a TODO comment here.