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: update limit using all operators #874

Merged
merged 2 commits into from
Dec 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
5 changes: 2 additions & 3 deletions server/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ func (c *coordinator) addOperator(op *schedule.Operator) bool {
}

c.histories.Put(regionID, op)
c.limiter.AddOperator(op)
c.operators[regionID] = op
c.limiter.UpdateCounts(c.operators)

if region := c.cluster.GetRegion(op.RegionID()); region != nil {
if step := op.Check(region); step != nil {
Expand All @@ -390,9 +390,8 @@ func (c *coordinator) removeOperator(op *schedule.Operator) {

func (c *coordinator) removeOperatorLocked(op *schedule.Operator) {
regionID := op.RegionID()
c.limiter.RemoveOperator(op)
delete(c.operators, regionID)

c.limiter.UpdateCounts(c.operators)
c.histories.Put(regionID, op)
operatorCounter.WithLabelValues(op.Desc(), "remove").Inc()
}
Expand Down
33 changes: 19 additions & 14 deletions server/coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,21 +554,26 @@ func (s *testScheduleLimiterSuite) TestOperatorCount(c *C) {
c.Assert(l.OperatorCount(core.LeaderKind), Equals, uint64(0))
c.Assert(l.OperatorCount(core.RegionKind), Equals, uint64(0))

leaderOP := newTestOperator(1, core.LeaderKind)
l.AddOperator(leaderOP)
operators := make(map[uint64]*schedule.Operator)

operators[1] = newTestOperator(1, core.LeaderKind)
l.UpdateCounts(operators)
c.Assert(l.OperatorCount(core.LeaderKind), Equals, uint64(1)) // 1:leader
operators[2] = newTestOperator(2, core.LeaderKind)
l.UpdateCounts(operators)
c.Assert(l.OperatorCount(core.LeaderKind), Equals, uint64(2)) // 1:leader, 2:leader
delete(operators, 1)
l.UpdateCounts(operators)
c.Assert(l.OperatorCount(core.LeaderKind), Equals, uint64(1)) // 2:leader

operators[1] = newTestOperator(1, core.RegionKind)
l.UpdateCounts(operators)
c.Assert(l.OperatorCount(core.RegionKind), Equals, uint64(1)) // 1:region 2:leader
c.Assert(l.OperatorCount(core.LeaderKind), Equals, uint64(1))
l.AddOperator(leaderOP)
c.Assert(l.OperatorCount(core.LeaderKind), Equals, uint64(2))
l.RemoveOperator(leaderOP)
c.Assert(l.OperatorCount(core.LeaderKind), Equals, uint64(1))

regionOP := newTestOperator(1, core.RegionKind)
l.AddOperator(regionOP)
c.Assert(l.OperatorCount(core.RegionKind), Equals, uint64(1))
l.AddOperator(regionOP)
c.Assert(l.OperatorCount(core.RegionKind), Equals, uint64(2))
l.RemoveOperator(regionOP)
c.Assert(l.OperatorCount(core.RegionKind), Equals, uint64(1))
operators[2] = newTestOperator(2, core.RegionKind)
l.UpdateCounts(operators)
c.Assert(l.OperatorCount(core.RegionKind), Equals, uint64(2)) // 1:region 2:region
c.Assert(l.OperatorCount(core.LeaderKind), Equals, uint64(0))
}

var _ = Suite(&testScheduleControllerSuite{})
Expand Down
19 changes: 7 additions & 12 deletions server/schedule/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,16 @@ func NewLimiter() *Limiter {
}
}

// AddOperator increase the count by kind
func (l *Limiter) AddOperator(op *Operator) {
// UpdateCounts updates resouce counts using current pending operators.
func (l *Limiter) UpdateCounts(operators map[uint64]*Operator) {
l.Lock()
defer l.Unlock()
l.counts[op.ResourceKind()]++
}

// RemoveOperator decrease the count by kind
func (l *Limiter) RemoveOperator(op *Operator) {
l.Lock()
defer l.Unlock()
if l.counts[op.ResourceKind()] == 0 {
log.Fatal("the limiter is already 0, no operators need to remove")
for k := range l.counts {
l.counts[k] = 0
}
for _, op := range operators {
l.counts[op.ResourceKind()]++
}
l.counts[op.ResourceKind()]--
}

// OperatorCount get the count by kind
Expand Down