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

server: move StoreInfo/ResourceKind to core directory. #719

Merged
merged 2 commits into from
Sep 4, 2017
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
7 changes: 4 additions & 3 deletions server/api/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
package api

import (
"github.com/juju/errors"
"net/http"
"strconv"

"github.com/gorilla/mux"
"github.com/juju/errors"
"github.com/pingcap/pd/server"
"github.com/pingcap/pd/server/core"
"github.com/unrolled/render"
)

Expand Down Expand Up @@ -59,8 +60,8 @@ func (h *historyHandler) GetOperatorsOfKind(w http.ResponseWriter, r *http.Reque
h.r.JSON(w, http.StatusOK, nil)
return
}
kind := server.ParseResourceKind(k)
if kind == server.UnKnownKind {
kind := core.ParseResourceKind(k)
if kind == core.UnKnownKind {
h.r.JSON(w, http.StatusInternalServerError, errUnknownOperatorKind.Error())
return
}
Expand Down
3 changes: 2 additions & 1 deletion server/api/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/pkg/typeutil"
"github.com/pingcap/pd/server"
"github.com/pingcap/pd/server/core"
"github.com/unrolled/render"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ type storeInfo struct {

const downStateName = "Down"

func newStoreInfo(store *server.StoreInfo) *storeInfo {
func newStoreInfo(store *core.StoreInfo) *storeInfo {
s := &storeInfo{
Store: &metaStore{
Store: store.Store,
Expand Down
3 changes: 2 additions & 1 deletion server/api/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/pd/server"
"github.com/pingcap/pd/server/core"
)

var _ = Suite(&testStoreSuite{})
Expand Down Expand Up @@ -229,7 +230,7 @@ func (s *testStoreSuite) TestUrlStoreFilter(c *C) {
}

func (s *testStoreSuite) TestDownState(c *C) {
store := &server.StoreInfo{
store := &core.StoreInfo{
Store: &metapb.Store{
State: metapb.StoreState_Up,
},
Expand Down
43 changes: 22 additions & 21 deletions server/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/montanaflynn/stats"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/server/core"
)

const (
Expand Down Expand Up @@ -50,10 +51,10 @@ func minBalanceDiff(count uint64) float64 {
// shouldBalance returns true if we should balance the source and target store.
// The min balance diff provides a buffer to make the cluster stable, so that we
// don't need to schedule very frequently.
func shouldBalance(source, target *StoreInfo, kind ResourceKind) bool {
sourceCount := source.resourceCount(kind)
sourceScore := source.resourceScore(kind)
targetScore := target.resourceScore(kind)
func shouldBalance(source, target *core.StoreInfo, kind core.ResourceKind) bool {
sourceCount := source.ResourceCount(kind)
sourceScore := source.ResourceScore(kind)
targetScore := target.ResourceScore(kind)
if targetScore >= sourceScore {
return false
}
Expand All @@ -62,12 +63,12 @@ func shouldBalance(source, target *StoreInfo, kind ResourceKind) bool {
return diffCount >= minBalanceDiff(sourceCount)
}

func adjustBalanceLimit(cluster *clusterInfo, kind ResourceKind) uint64 {
func adjustBalanceLimit(cluster *clusterInfo, kind core.ResourceKind) uint64 {
stores := cluster.getStores()
counts := make([]float64, 0, len(stores))
for _, s := range stores {
if s.isUp() {
counts = append(counts, float64(s.resourceCount(kind)))
if s.IsUp() {
counts = append(counts, float64(s.ResourceCount(kind)))
}
}
limit, _ := stats.StandardDeviation(stats.Float64Data(counts))
Expand All @@ -89,16 +90,16 @@ func newBalanceLeaderScheduler(opt *scheduleOption) *balanceLeaderScheduler {
return &balanceLeaderScheduler{
opt: opt,
limit: 1,
selector: newBalanceSelector(LeaderKind, filters),
selector: newBalanceSelector(core.LeaderKind, filters),
}
}

func (l *balanceLeaderScheduler) GetName() string {
return "balance-leader-scheduler"
}

func (l *balanceLeaderScheduler) GetResourceKind() ResourceKind {
return LeaderKind
func (l *balanceLeaderScheduler) GetResourceKind() core.ResourceKind {
return core.LeaderKind
}

func (l *balanceLeaderScheduler) GetResourceLimit() uint64 {
Expand Down Expand Up @@ -156,16 +157,16 @@ func newBalanceRegionScheduler(opt *scheduleOption) *balanceRegionScheduler {
rep: opt.GetReplication(),
cache: cache,
limit: 1,
selector: newBalanceSelector(RegionKind, filters),
selector: newBalanceSelector(core.RegionKind, filters),
}
}

func (s *balanceRegionScheduler) GetName() string {
return "balance-region-scheduler"
}

func (s *balanceRegionScheduler) GetResourceKind() ResourceKind {
return RegionKind
func (s *balanceRegionScheduler) GetResourceKind() core.ResourceKind {
return core.RegionKind
}

func (s *balanceRegionScheduler) GetResourceLimit() uint64 {
Expand Down Expand Up @@ -226,7 +227,7 @@ func (s *balanceRegionScheduler) transferPeer(cluster *clusterInfo, region *Regi
}
s.limit = adjustBalanceLimit(cluster, s.GetResourceKind())

return newTransferPeer(region, RegionKind, oldPeer, newPeer)
return newTransferPeer(region, core.RegionKind, oldPeer, newPeer)
}

// replicaChecker ensures region has the best replicas.
Expand Down Expand Up @@ -339,7 +340,7 @@ func (r *replicaChecker) checkDownPeer(region *RegionInfo) Operator {
log.Infof("lost the store %d,maybe you are recovering the PD cluster.", peer.GetStoreId())
return nil
}
if store.downTime() < r.opt.GetMaxStoreDownTime() {
if store.DownTime() < r.opt.GetMaxStoreDownTime() {
continue
}
if stats.GetDownSeconds() < uint64(r.opt.GetMaxStoreDownTime().Seconds()) {
Expand All @@ -357,7 +358,7 @@ func (r *replicaChecker) checkOfflinePeer(region *RegionInfo) Operator {
log.Infof("lost the store %d,maybe you are recovering the PD cluster.", peer.GetStoreId())
return nil
}
if store.isUp() {
if store.IsUp() {
continue
}

Expand All @@ -370,7 +371,7 @@ func (r *replicaChecker) checkOfflinePeer(region *RegionInfo) Operator {
if newPeer == nil {
return nil
}
return newTransferPeer(region, RegionKind, peer, newPeer)
return newTransferPeer(region, core.RegionKind, peer, newPeer)
}

return nil
Expand All @@ -393,7 +394,7 @@ func (r *replicaChecker) checkBestReplacement(region *RegionInfo) Operator {
if err != nil {
return nil
}
return newTransferPeer(region, RegionKind, oldPeer, newPeer)
return newTransferPeer(region, core.RegionKind, oldPeer, newPeer)
}

// RegionStat records each hot region's statistics
Expand Down Expand Up @@ -451,8 +452,8 @@ func (h *balanceHotRegionScheduler) GetName() string {
return "balance-hot-region-scheduler"
}

func (h *balanceHotRegionScheduler) GetResourceKind() ResourceKind {
return PriorityKind
func (h *balanceHotRegionScheduler) GetResourceKind() core.ResourceKind {
return core.PriorityKind
}

func (h *balanceHotRegionScheduler) GetResourceLimit() uint64 {
Expand All @@ -471,7 +472,7 @@ func (h *balanceHotRegionScheduler) Schedule(cluster *clusterInfo) Operator {
srcRegion, srcPeer, destPeer := h.balanceByPeer(cluster)
if srcRegion != nil {
schedulerCounter.WithLabelValues(h.GetName(), "move_peer").Inc()
return newTransferPeer(srcRegion, PriorityKind, srcPeer, destPeer)
return newTransferPeer(srcRegion, core.PriorityKind, srcPeer, destPeer)
}

// balance by leader
Expand Down
13 changes: 7 additions & 6 deletions server/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/pd/server/core"
)

type testClusterInfo struct {
Expand Down Expand Up @@ -58,15 +59,15 @@ func (c *testClusterInfo) setStoreBusy(storeID uint64, busy bool) {
}

func (c *testClusterInfo) addLeaderStore(storeID uint64, leaderCount int) {
store := newStoreInfo(&metapb.Store{Id: storeID})
store := core.NewStoreInfo(&metapb.Store{Id: storeID})
store.Stats = &pdpb.StoreStats{}
store.LastHeartbeatTS = time.Now()
store.LeaderCount = leaderCount
c.putStore(store)
}

func (c *testClusterInfo) addRegionStore(storeID uint64, regionCount int) {
store := newStoreInfo(&metapb.Store{Id: storeID})
store := core.NewStoreInfo(&metapb.Store{Id: storeID})
store.Stats = &pdpb.StoreStats{}
store.LastHeartbeatTS = time.Now()
store.RegionCount = regionCount
Expand Down Expand Up @@ -220,15 +221,15 @@ func (s *testBalanceSpeedSuite) testBalanceSpeed(c *C, tests []testBalanceSpeedC
tc.addLeaderStore(2, int(t.targetCount))
source := cluster.getStore(1)
target := cluster.getStore(2)
c.Assert(shouldBalance(source, target, LeaderKind), Equals, t.expectedResult)
c.Assert(shouldBalance(source, target, core.LeaderKind), Equals, t.expectedResult)
}

for _, t := range tests {
tc.addRegionStore(1, int(t.sourceCount))
tc.addRegionStore(2, int(t.targetCount))
source := cluster.getStore(1)
target := cluster.getStore(2)
c.Assert(shouldBalance(source, target, RegionKind), Equals, t.expectedResult)
c.Assert(shouldBalance(source, target, core.RegionKind), Equals, t.expectedResult)
}
}

Expand All @@ -240,11 +241,11 @@ func (s *testBalanceSpeedSuite) TestBalanceLimit(c *C) {
tc.addLeaderStore(3, 30)

// StandDeviation is sqrt((10^2+0+10^2)/3).
c.Assert(adjustBalanceLimit(cluster, LeaderKind), Equals, uint64(math.Sqrt(200.0/3.0)))
c.Assert(adjustBalanceLimit(cluster, core.LeaderKind), Equals, uint64(math.Sqrt(200.0/3.0)))

tc.setStoreOffline(1)
// StandDeviation is sqrt((5^2+5^2)/2).
c.Assert(adjustBalanceLimit(cluster, LeaderKind), Equals, uint64(math.Sqrt(50.0/2.0)))
c.Assert(adjustBalanceLimit(cluster, core.LeaderKind), Equals, uint64(math.Sqrt(50.0/2.0)))
}

var _ = Suite(&testBalanceLeaderSchedulerSuite{})
Expand Down
Loading