-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a288840
commit a251c22
Showing
8 changed files
with
325 additions
and
138 deletions.
There are no files selected for viewing
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
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
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,102 @@ | ||
// Copyright 2018 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package schedule | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/pingcap/pd/server/core" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Limiter is a counter that limits the number of operators. | ||
type Limiter struct { | ||
sync.RWMutex | ||
counts map[OperatorKind]map[uint64]uint64 | ||
} | ||
|
||
// NewLimiter creates a schedule limiter. | ||
func NewLimiter() *Limiter { | ||
return &Limiter{ | ||
counts: make(map[OperatorKind]map[uint64]uint64), | ||
} | ||
} | ||
|
||
// AddOperator increases the count by kind. | ||
func (l *Limiter) AddOperator(op *Operator, region *core.RegionInfo) { | ||
l.Lock() | ||
defer l.Unlock() | ||
|
||
if _, ok := l.counts[op.Kind()]; !ok { | ||
l.counts[op.Kind()] = make(map[uint64]uint64) | ||
} | ||
|
||
for _, store := range op.InvolvedStores(region) { | ||
l.counts[op.Kind()][store]++ | ||
} | ||
} | ||
|
||
// RemoveOperator decreases the count by kind. | ||
func (l *Limiter) RemoveOperator(op *Operator) { | ||
l.Lock() | ||
defer l.Unlock() | ||
|
||
for _, store := range op.InvolvedStores(nil) { | ||
_, ok := l.counts[op.Kind()][store] | ||
if ok { | ||
if l.counts[op.Kind()][store] == 0 { | ||
log.Fatal("the limiter is already 0, no operators need to remove") | ||
} | ||
l.counts[op.Kind()][store]-- | ||
} else { | ||
log.Fatalf("operator count decrease on nonexisted store %d", store) | ||
} | ||
} | ||
} | ||
|
||
// OperatorCount gets the max count of operators of all stores filtered by mask. | ||
func (l *Limiter) OperatorCount(mask OperatorKind) uint64 { | ||
l.RLock() | ||
defer l.RUnlock() | ||
|
||
var max uint64 | ||
counts := make(map[uint64]uint64) | ||
for k, stores := range l.counts { | ||
if k&mask != 0 { | ||
for storeID, count := range stores { | ||
counts[storeID] += count | ||
if max < counts[storeID] { | ||
max = counts[storeID] | ||
} | ||
} | ||
} | ||
} | ||
return max | ||
} | ||
|
||
// StoreOperatorCount gets the count of operators for specific store filtered by mask. | ||
func (l *Limiter) StoreOperatorCount(mask OperatorKind, storeID uint64) uint64 { | ||
l.RLock() | ||
defer l.RUnlock() | ||
|
||
var total uint64 | ||
for k, stores := range l.counts { | ||
if k&mask != 0 { | ||
if count, ok := stores[storeID]; ok { | ||
total += count | ||
} | ||
} | ||
} | ||
return total | ||
} |
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,65 @@ | ||
// Copyright 2018 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package schedule | ||
|
||
import ( | ||
. "github.com/pingcap/check" | ||
) | ||
|
||
var _ = Suite(&testLimiterSuite{}) | ||
|
||
type testLimiterSuite struct{} | ||
|
||
func (s *testLimiterSuite) TestOperatorCount(c *C) { | ||
l := NewLimiter() | ||
c.Assert(l.OperatorCount(OpLeader), Equals, uint64(0)) | ||
c.Assert(l.OperatorCount(OpRegion), Equals, uint64(0)) | ||
|
||
ops := []*Operator{} | ||
|
||
// init region and operator | ||
for i := uint64(1); i <= 3; i++ { | ||
op := newTestOperator(i, OpLeader|OpRegion, TransferLeader{FromStore: i + 1, ToStore: 1}) | ||
region := newTestRegion(i, i+1, [2]uint64{1, 1}, [2]uint64{i + 1, i + 1}) | ||
l.AddOperator(op, region) | ||
ops = append(ops, op) | ||
} | ||
|
||
c.Assert(l.OperatorCount(OpLeader), Equals, uint64(3)) | ||
c.Assert(l.StoreOperatorCount(OpLeader, 1), Equals, uint64(3)) | ||
c.Assert(l.StoreOperatorCount(OpLeader, 2), Equals, uint64(1)) | ||
c.Assert(l.StoreOperatorCount(OpLeader, 3), Equals, uint64(1)) | ||
c.Assert(l.StoreOperatorCount(OpLeader, 4), Equals, uint64(1)) | ||
|
||
l.RemoveOperator(ops[0]) | ||
c.Assert(l.OperatorCount(OpLeader|OpRegion), Equals, uint64(2)) | ||
c.Assert(l.StoreOperatorCount(OpLeader|OpRegion, 1), Equals, uint64(2)) | ||
c.Assert(l.StoreOperatorCount(OpLeader|OpRegion, 2), Equals, uint64(0)) | ||
c.Assert(l.StoreOperatorCount(OpLeader|OpRegion, 3), Equals, uint64(1)) | ||
c.Assert(l.StoreOperatorCount(OpLeader|OpRegion, 4), Equals, uint64(1)) | ||
|
||
l.RemoveOperator(ops[1]) | ||
c.Assert(l.OperatorCount(OpRegion), Equals, uint64(1)) | ||
c.Assert(l.StoreOperatorCount(OpRegion, 1), Equals, uint64(1)) | ||
c.Assert(l.StoreOperatorCount(OpRegion, 2), Equals, uint64(0)) | ||
c.Assert(l.StoreOperatorCount(OpRegion, 3), Equals, uint64(0)) | ||
c.Assert(l.StoreOperatorCount(OpRegion, 4), Equals, uint64(1)) | ||
|
||
l.RemoveOperator(ops[2]) | ||
c.Assert(l.OperatorCount(OpLeader), Equals, uint64(0)) | ||
c.Assert(l.StoreOperatorCount(OpLeader, 1), Equals, uint64(0)) | ||
c.Assert(l.StoreOperatorCount(OpLeader, 2), Equals, uint64(0)) | ||
c.Assert(l.StoreOperatorCount(OpLeader, 3), Equals, uint64(0)) | ||
c.Assert(l.StoreOperatorCount(OpLeader, 4), Equals, uint64(0)) | ||
} |
Oops, something went wrong.