-
Notifications
You must be signed in to change notification settings - Fork 725
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
*: store balance weight. #713
Changes from 4 commits
a5ce0d9
89ea840
14fc9da
fd2a7b0
0345308
ddac54a
772b0ce
934f558
5b82f54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -514,6 +514,24 @@ func (c *RaftCluster) BuryStore(storeID uint64, force bool) error { | |
return cluster.putStore(store) | ||
} | ||
|
||
// SetStoreWeight sets up a store's leader/region balance weight. | ||
func (c *RaftCluster) SetStoreWeight(storeID uint64, leader, region float64) error { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
store := c.cachedCluster.getStore(storeID) | ||
if store == nil { | ||
return errors.Trace(errStoreNotFound(storeID)) | ||
} | ||
|
||
if err := c.s.kv.saveStoreWeight(storeID, leader, region); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider moving this inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return errors.Trace(err) | ||
} | ||
|
||
store.LeaderWeight, store.RegionWeight = leader, region | ||
return c.cachedCluster.putStore(store) | ||
} | ||
|
||
func (c *RaftCluster) checkStores() { | ||
cluster := c.cachedCluster | ||
for _, store := range cluster.getMetaStores() { | ||
|
@@ -565,10 +583,10 @@ func (c *RaftCluster) collectMetrics() { | |
storageCapacity += s.Stats.GetCapacity() | ||
|
||
// Balance score. | ||
minLeaderScore = math.Min(minLeaderScore, s.leaderScore()) | ||
maxLeaderScore = math.Max(maxLeaderScore, s.leaderScore()) | ||
minRegionScore = math.Min(minRegionScore, s.regionScore()) | ||
maxRegionScore = math.Max(maxRegionScore, s.regionScore()) | ||
minLeaderScore = math.Min(minLeaderScore, s.LeaderScore()) | ||
maxLeaderScore = math.Max(maxLeaderScore, s.LeaderScore()) | ||
minRegionScore = math.Min(minRegionScore, s.RegionScore()) | ||
maxRegionScore = math.Max(maxRegionScore, s.RegionScore()) | ||
} | ||
|
||
metrics := make(map[string]float64) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import ( | |
"fmt" | ||
"math" | ||
"path" | ||
"strconv" | ||
"time" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
|
@@ -40,18 +41,20 @@ var ( | |
|
||
// kv wraps all kv operations, keep it stateless. | ||
type kv struct { | ||
s *Server | ||
client *clientv3.Client | ||
clusterPath string | ||
configPath string | ||
s *Server | ||
client *clientv3.Client | ||
clusterPath string | ||
configPath string | ||
schedulePath string | ||
} | ||
|
||
func newKV(s *Server) *kv { | ||
return &kv{ | ||
s: s, | ||
client: s.client, | ||
clusterPath: path.Join(s.rootPath, "raft"), | ||
configPath: path.Join(s.rootPath, "config"), | ||
s: s, | ||
client: s.client, | ||
clusterPath: path.Join(s.rootPath, "raft"), | ||
configPath: path.Join(s.rootPath, "config"), | ||
schedulePath: path.Join(s.rootPath, "schedule"), | ||
} | ||
} | ||
|
||
|
@@ -69,6 +72,14 @@ func (kv *kv) clusterStatePath(option string) string { | |
return path.Join(kv.clusterPath, "status", option) | ||
} | ||
|
||
func (kv *kv) storeLeaderWeightPath(storeID uint64) string { | ||
return path.Join(kv.schedulePath, "store_weight", fmt.Sprintf("%020d", storeID), "leader") | ||
} | ||
|
||
func (kv *kv) storeRegionWeightPath(storeID uint64) string { | ||
return path.Join(kv.schedulePath, "store_weight", fmt.Sprintf("%020d", storeID), "region") | ||
} | ||
|
||
func (kv *kv) getRaftClusterBootstrapTime() (time.Time, error) { | ||
data, err := kv.load(kv.clusterStatePath("raft_bootstrap_time")) | ||
if err != nil { | ||
|
@@ -169,8 +180,20 @@ func (kv *kv) loadStores(stores *storesInfo, rangeLimit int64) error { | |
return errors.Trace(err) | ||
} | ||
|
||
storeInfo := newStoreInfo(store) | ||
leaderWeight, err := kv.loadFloatWithDefaultValue(kv.storeLeaderWeightPath(storeInfo.GetId()), 1.0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider extracting a |
||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
storeInfo.LeaderWeight = leaderWeight | ||
regionWeight, err := kv.loadFloatWithDefaultValue(kv.storeRegionWeightPath(storeInfo.GetId()), 1.0) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
storeInfo.RegionWeight = regionWeight | ||
|
||
nextID = store.GetId() + 1 | ||
stores.setStore(newStoreInfo(store)) | ||
stores.setStore(storeInfo) | ||
} | ||
|
||
if len(resp.Kvs) < int(rangeLimit) { | ||
|
@@ -179,6 +202,33 @@ func (kv *kv) loadStores(stores *storesInfo, rangeLimit int64) error { | |
} | ||
} | ||
|
||
func (kv *kv) saveStoreWeight(storeID uint64, leader, region float64) error { | ||
leaderValue := strconv.FormatFloat(leader, 'f', -1, 64) | ||
if err := kv.save(kv.storeLeaderWeightPath(storeID), leaderValue); err != nil { | ||
return errors.Trace(err) | ||
} | ||
regionValue := strconv.FormatFloat(region, 'f', -1, 64) | ||
if err := kv.save(kv.storeRegionWeightPath(storeID), regionValue); err != nil { | ||
return errors.Trace(err) | ||
} | ||
return nil | ||
} | ||
|
||
func (kv *kv) loadFloatWithDefaultValue(path string, def float64) (float64, error) { | ||
res, err := kvGet(kv.client, path) | ||
if err != nil { | ||
return 0, errors.Trace(err) | ||
} | ||
if len(res.Kvs) == 0 { | ||
return def, nil | ||
} | ||
val, err := strconv.ParseFloat(string(res.Kvs[0].Value), 64) | ||
if err != nil { | ||
return 0, errors.Trace(err) | ||
} | ||
return val, nil | ||
} | ||
|
||
func (kv *kv) loadRegions(regions *regionsInfo, rangeLimit int64) error { | ||
nextID := uint64(0) | ||
endRegion := kv.regionPath(math.MaxUint64) | ||
|
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.
s/0.5 0.5/0.5 0.9/