/
scheduler.go
90 lines (76 loc) · 2.74 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2017 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 (
"fmt"
"time"
"github.com/juju/errors"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/server/core"
)
// options for interval of schedulers
const (
MaxScheduleInterval = time.Minute
MinScheduleInterval = time.Millisecond * 10
MinSlowScheduleInterval = time.Second * 3
)
// Cluster provides an overview of a cluster's regions distribution.
type Cluster interface {
RandFollowerRegion(storeID uint64) *core.RegionInfo
RandLeaderRegion(storeID uint64) *core.RegionInfo
GetStores() []*core.StoreInfo
GetStore(id uint64) *core.StoreInfo
GetRegion(id uint64) *core.RegionInfo
GetRegionStores(region *core.RegionInfo) []*core.StoreInfo
GetFollowerStores(region *core.RegionInfo) []*core.StoreInfo
GetLeaderStore(region *core.RegionInfo) *core.StoreInfo
BlockStore(id uint64) error
UnblockStore(id uint64)
IsRegionHot(id uint64) bool
RegionWriteStats() []*core.RegionStat
RegionReadStats() []*core.RegionStat
// TODO: it should be removed. Schedulers don't need to know anything
// about peers.
AllocPeer(storeID uint64) (*metapb.Peer, error)
}
// Scheduler is an interface to schedule resources.
type Scheduler interface {
GetName() string
// GetType should in accordance with the name passing to schedule.RegisterScheduler()
GetType() string
GetInterval() time.Duration
GetResourceKind() core.ResourceKind
GetResourceLimit() uint64
Prepare(cluster Cluster) error
Cleanup(cluster Cluster)
Schedule(cluster Cluster) *Operator
}
// CreateSchedulerFunc is for creating scheudler.
type CreateSchedulerFunc func(opt Options, args []string) (Scheduler, error)
var schedulerMap = make(map[string]CreateSchedulerFunc)
// RegisterScheduler binds a scheduler creator. It should be called in init()
// func of a package.
func RegisterScheduler(name string, createFn CreateSchedulerFunc) {
if _, ok := schedulerMap[name]; ok {
panic(fmt.Sprintf("duplicated scheduler name: %v", name))
}
schedulerMap[name] = createFn
}
// CreateScheduler creates a scheduler with registered creator func.
func CreateScheduler(name string, opt Options, args ...string) (Scheduler, error) {
fn, ok := schedulerMap[name]
if !ok {
return nil, errors.Errorf("create func of %v is not registered", name)
}
return fn(opt, args)
}