forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
414 lines (357 loc) · 11.4 KB
/
handler.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright 2016 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 server
import (
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/juju/errors"
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/schedule"
)
var (
errNotBootstrapped = errors.New("TiKV cluster not bootstrapped, please start TiKV first")
errOperatorNotFound = errors.New("operator not found")
)
// Handler is a helper to export methods to handle API/RPC requests.
type Handler struct {
s *Server
opt *scheduleOption
}
func newHandler(s *Server) *Handler {
return &Handler{s: s, opt: s.scheduleOpt}
}
func (h *Handler) getCoordinator() (*coordinator, error) {
cluster := h.s.GetRaftCluster()
if cluster == nil {
return nil, errors.Trace(errNotBootstrapped)
}
return cluster.coordinator, nil
}
// GetSchedulers returns all names of schedulers.
func (h *Handler) GetSchedulers() ([]string, error) {
c, err := h.getCoordinator()
if err != nil {
return nil, errors.Trace(err)
}
return c.getSchedulers(), nil
}
// GetHotWriteRegions gets all hot write regions status
func (h *Handler) GetHotWriteRegions() *core.StoreHotRegionInfos {
c, err := h.getCoordinator()
if err != nil {
return nil
}
return c.getHotWriteRegions()
}
// GetHotReadRegions gets all hot read regions status
func (h *Handler) GetHotReadRegions() *core.StoreHotRegionInfos {
c, err := h.getCoordinator()
if err != nil {
return nil
}
return c.getHotReadRegions()
}
// GetHotWriteStores gets all hot write stores status
func (h *Handler) GetHotWriteStores() map[uint64]uint64 {
return h.s.cluster.cachedCluster.getStoresWriteStat()
}
// GetHotReadStores gets all hot write stores status
func (h *Handler) GetHotReadStores() map[uint64]uint64 {
return h.s.cluster.cachedCluster.getStoresReadStat()
}
// AddScheduler adds a scheduler.
func (h *Handler) AddScheduler(s schedule.Scheduler, args ...string) error {
c, err := h.getCoordinator()
if err != nil {
return errors.Trace(err)
}
if err = c.addScheduler(s, s.GetInterval(), args...); err != nil {
log.Errorf("can not add scheduler %v: %v", s.GetName(), err)
} else if err = c.opt.persist(c.kv); err != nil {
log.Errorf("can not persist scheduler config: %v", err)
}
return errors.Trace(err)
}
// RemoveScheduler removes a scheduler by name.
func (h *Handler) RemoveScheduler(name string) error {
c, err := h.getCoordinator()
if err != nil {
return errors.Trace(err)
}
if err = c.removeScheduler(name); err != nil {
log.Errorf("can not remove scheduler %v: %v", name, err)
} else if err = c.opt.persist(c.kv); err != nil {
log.Errorf("can not persist scheduler config: %v", err)
}
return errors.Trace(err)
}
// AddBalanceLeaderScheduler adds a balance-leader-scheduler.
func (h *Handler) AddBalanceLeaderScheduler() error {
s, err := schedule.CreateScheduler("balance-leader", h.opt)
if err != nil {
return errors.Trace(err)
}
log.Infof("create scheduler %s", s.GetName())
return h.AddScheduler(s)
}
// AddBalanceRegionScheduler adds a balance-region-scheduler.
func (h *Handler) AddBalanceRegionScheduler() error {
s, err := schedule.CreateScheduler("balance-region", h.opt)
if err != nil {
return errors.Trace(err)
}
log.Infof("create scheduler %s", s.GetName())
return h.AddScheduler(s)
}
// AddBalanceHotRegionScheduler adds a balance-hot-region-scheduler.
func (h *Handler) AddBalanceHotRegionScheduler() error {
s, err := schedule.CreateScheduler("hot-region", h.opt)
if err != nil {
return errors.Trace(err)
}
log.Infof("create scheduler %s", s.GetName())
return h.AddScheduler(s)
}
// AddGrantLeaderScheduler adds a grant-leader-scheduler.
func (h *Handler) AddGrantLeaderScheduler(storeID uint64) error {
s, err := schedule.CreateScheduler("grant-leader", h.opt, strconv.FormatUint(storeID, 10))
if err != nil {
return errors.Trace(err)
}
log.Infof("create scheduler %s", s.GetName())
return h.AddScheduler(s, strconv.FormatUint(storeID, 10))
}
// AddEvictLeaderScheduler adds an evict-leader-scheduler.
func (h *Handler) AddEvictLeaderScheduler(storeID uint64) error {
s, err := schedule.CreateScheduler("evict-leader", h.opt, strconv.FormatUint(storeID, 10))
if err != nil {
return errors.Trace(err)
}
log.Infof("create scheduler %s", s.GetName())
return h.AddScheduler(s, strconv.FormatUint(storeID, 10))
}
// AddShuffleLeaderScheduler adds a shuffle-leader-scheduler.
func (h *Handler) AddShuffleLeaderScheduler() error {
s, err := schedule.CreateScheduler("shuffle-leader", h.opt)
if err != nil {
return errors.Trace(err)
}
log.Infof("create scheduler %s", s.GetName())
return h.AddScheduler(s)
}
// AddShuffleRegionScheduler adds a shuffle-region-scheduler.
func (h *Handler) AddShuffleRegionScheduler() error {
s, err := schedule.CreateScheduler("shuffle-region", h.opt)
if err != nil {
return errors.Trace(err)
}
log.Infof("create scheduler %s", s.GetName())
return h.AddScheduler(s)
}
// GetOperator returns the region operator.
func (h *Handler) GetOperator(regionID uint64) (*schedule.Operator, error) {
c, err := h.getCoordinator()
if err != nil {
return nil, errors.Trace(err)
}
op := c.getOperator(regionID)
if op == nil {
return nil, errOperatorNotFound
}
return op, nil
}
// RemoveOperator removes the region operator.
func (h *Handler) RemoveOperator(regionID uint64) error {
c, err := h.getCoordinator()
if err != nil {
return errors.Trace(err)
}
op := c.getOperator(regionID)
if op == nil {
return errOperatorNotFound
}
c.removeOperator(op)
return nil
}
// GetOperators returns the running operators.
func (h *Handler) GetOperators() ([]*schedule.Operator, error) {
c, err := h.getCoordinator()
if err != nil {
return nil, errors.Trace(err)
}
return c.getOperators(), nil
}
// GetAdminOperators returns the running admin operators.
func (h *Handler) GetAdminOperators() ([]*schedule.Operator, error) {
return h.GetOperatorsOfKind(core.AdminKind)
}
// GetLeaderOperators returns the running leader operators.
func (h *Handler) GetLeaderOperators() ([]*schedule.Operator, error) {
return h.GetOperatorsOfKind(core.LeaderKind)
}
// GetRegionOperators returns the running region operators.
func (h *Handler) GetRegionOperators() ([]*schedule.Operator, error) {
return h.GetOperatorsOfKind(core.RegionKind)
}
// GetOperatorsOfKind returns the running operators of the kind.
func (h *Handler) GetOperatorsOfKind(kind core.ResourceKind) ([]*schedule.Operator, error) {
ops, err := h.GetOperators()
if err != nil {
return nil, errors.Trace(err)
}
var results []*schedule.Operator
for _, op := range ops {
if op.ResourceKind() == kind {
results = append(results, op)
}
}
return results, nil
}
// GetHistoryOperators returns history operators
func (h *Handler) GetHistoryOperators() ([]*schedule.Operator, error) {
c, err := h.getCoordinator()
if err != nil {
return nil, errors.Trace(err)
}
return c.getHistories(), nil
}
// GetHistoryOperatorsOfKind returns history operators by Kind
func (h *Handler) GetHistoryOperatorsOfKind(kind core.ResourceKind) ([]*schedule.Operator, error) {
c, err := h.getCoordinator()
if err != nil {
return nil, errors.Trace(err)
}
return c.getHistoriesOfKind(kind), nil
}
// AddTransferLeaderOperator adds an operator to transfer leader to the store.
func (h *Handler) AddTransferLeaderOperator(regionID uint64, storeID uint64) error {
c, err := h.getCoordinator()
if err != nil {
return errors.Trace(err)
}
region := c.cluster.GetRegion(regionID)
if region == nil {
return errRegionNotFound(regionID)
}
newLeader := region.GetStorePeer(storeID)
if newLeader == nil {
return errors.Errorf("region has no peer in store %v", storeID)
}
step := schedule.TransferLeader{FromStore: region.Leader.GetStoreId(), ToStore: newLeader.GetStoreId()}
op := schedule.NewOperator("adminTransferLeader", regionID, core.AdminKind, step)
c.addOperator(op)
return nil
}
// AddTransferRegionOperator adds an operator to transfer region to the stores.
func (h *Handler) AddTransferRegionOperator(regionID uint64, storeIDs map[uint64]struct{}) error {
c, err := h.getCoordinator()
if err != nil {
return errors.Trace(err)
}
region := c.cluster.GetRegion(regionID)
if region == nil {
return errRegionNotFound(regionID)
}
var steps []schedule.OperatorStep
// Add missing peers.
for id := range storeIDs {
if c.cluster.GetStore(id) == nil {
return core.ErrStoreNotFound(id)
}
if region.GetStorePeer(id) != nil {
continue
}
peer, err := c.cluster.AllocPeer(id)
if err != nil {
return errors.Trace(err)
}
steps = append(steps, schedule.AddPeer{ToStore: id, PeerID: peer.Id})
}
// Remove redundant peers.
for _, peer := range region.GetPeers() {
if _, ok := storeIDs[peer.GetStoreId()]; ok {
continue
}
steps = append(steps, schedule.RemovePeer{FromStore: peer.GetStoreId()})
}
op := schedule.NewOperator("adminMoveRegion", regionID, core.AdminKind, steps...)
c.addOperator(op)
return nil
}
// AddTransferPeerOperator adds an operator to transfer peer.
func (h *Handler) AddTransferPeerOperator(regionID uint64, fromStoreID, toStoreID uint64) error {
c, err := h.getCoordinator()
if err != nil {
return errors.Trace(err)
}
region := c.cluster.GetRegion(regionID)
if region == nil {
return errRegionNotFound(regionID)
}
oldPeer := region.GetStorePeer(fromStoreID)
if oldPeer == nil {
return errors.Errorf("region has no peer in store %v", fromStoreID)
}
if c.cluster.GetStore(toStoreID) == nil {
return core.ErrStoreNotFound(toStoreID)
}
newPeer, err := c.cluster.AllocPeer(toStoreID)
if err != nil {
return errors.Trace(err)
}
op := schedule.CreateMovePeerOperator("adminMovePeer", region, core.AdminKind, fromStoreID, toStoreID, newPeer.GetId())
c.addOperator(op)
return nil
}
// AddAddPeerOperator adds an operator to add peer.
func (h *Handler) AddAddPeerOperator(regionID uint64, toStoreID uint64) error {
c, err := h.getCoordinator()
if err != nil {
return errors.Trace(err)
}
region := c.cluster.GetRegion(regionID)
if region == nil {
return errRegionNotFound(regionID)
}
if region.GetStorePeer(toStoreID) != nil {
return errors.Errorf("region already has peer in store %v", toStoreID)
}
if c.cluster.GetStore(toStoreID) == nil {
return core.ErrStoreNotFound(toStoreID)
}
newPeer, err := c.cluster.AllocPeer(toStoreID)
if err != nil {
return errors.Trace(err)
}
step := schedule.AddPeer{ToStore: toStoreID, PeerID: newPeer.GetId()}
op := schedule.NewOperator("adminAddPeer", regionID, core.AdminKind, step)
c.addOperator(op)
return nil
}
// AddRemovePeerOperator adds an operator to remove peer.
func (h *Handler) AddRemovePeerOperator(regionID uint64, fromStoreID uint64) error {
c, err := h.getCoordinator()
if err != nil {
return errors.Trace(err)
}
region := c.cluster.GetRegion(regionID)
if region == nil {
return errRegionNotFound(regionID)
}
if region.GetStorePeer(fromStoreID) == nil {
return errors.Errorf("region has no peer in store %v", fromStoreID)
}
op := schedule.CreateRemovePeerOperator("adminRemovePeer", region, fromStoreID)
c.addOperator(op)
return nil
}