-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
region_handler.go
641 lines (573 loc) · 17.5 KB
/
region_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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// 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 server
import (
"bytes"
"encoding/json"
"math"
"net/http"
"strconv"
"time"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/juju/errors"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/tikvrpc"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/util/codec"
goctx "golang.org/x/net/context"
)
const (
pDBName = "db"
pTableName = "table"
pRegionID = "regionID"
pRecordID = "recordID"
pStartTS = "startTS"
)
const (
headerContentType = "Content-Type"
contentTypeJSON = "application/json"
)
type kvStore interface {
GetRegionCache() *tikv.RegionCache
SendReq(bo *tikv.Backoffer, req *tikvrpc.Request, regionID tikv.RegionVerID, timeout time.Duration) (*tikvrpc.Response, error)
}
type regionHandlerTool struct {
bo *tikv.Backoffer
regionCache *tikv.RegionCache
store kvStore
}
// RegionHandler is the common field for http region handler. It contains
// some common functions for all handlers.
type regionHandler struct {
*regionHandlerTool
}
// tableRegionsHandler is the handler for list table's regions.
type tableRegionsHandler struct {
*regionHandler
}
// MvccTxnHandler is the handler for txn debugger
type mvccTxnHandler struct {
*regionHandler
op string
}
const (
opMvccGetByKey = "key"
opMvccGetByTxn = "txn"
)
// newRegionHandler checks and prepares for region handler.
// It would panic when any error happens.
func (s *Server) newRegionHandler() (hanler *regionHandler) {
var tikvStore kvStore
store, ok := s.driver.(*TiDBDriver)
if !ok {
panic("Invalid KvStore with illegal driver")
}
if tikvStore, ok = store.store.(kvStore); !ok {
panic("Invalid KvStore with illegal store")
}
regionCache := tikvStore.GetRegionCache()
// init backOffer && infoSchema.
backOffer := tikv.NewBackoffer(500, goctx.Background())
tool := ®ionHandlerTool{
regionCache: regionCache,
bo: backOffer,
store: tikvStore,
}
return ®ionHandler{tool}
}
// TableRegions is the response data for list table's regions.
// It contains regions list for record and indices.
type TableRegions struct {
TableName string `json:"name"`
TableID int64 `json:"id"`
RecordRegions []RegionMeta `json:"record_regions"`
Indices []IndexRegions `json:"indices"`
}
// RegionMeta contains a region's peer detail
type RegionMeta struct {
ID uint64 `json:"region_id"`
Leader *metapb.Peer `json:"leader"`
Peers []*metapb.Peer `json:"peers"`
RegionEpoch *metapb.RegionEpoch `json:"region_epoch"`
}
// IndexRegions is the region info for one index.
type IndexRegions struct {
Name string `json:"name"`
ID int64 `json:"id"`
Regions []RegionMeta `json:"regions"`
}
// RegionDetail is the response data for get region by ID
// it includes indices and records detail in current region.
type RegionDetail struct {
RegionID uint64 `json:"region_id"`
StartKey []byte `json:"start_key"`
EndKey []byte `json:"end_key"`
Frames []FrameItem `json:"frames"`
}
// addIndex insert a index into RegionDetail.
func (rt *RegionDetail) addIndex(dbName, tName string, tID int64, indexName string, indexID int64) {
rt.Frames = append(rt.Frames, FrameItem{
DBName: dbName,
TableName: tName,
TableID: tID,
IndexName: indexName,
IndexID: indexID,
IsRecord: false,
})
}
// addRecord insert a table's record into RegionDetail.
func (rt *RegionDetail) addRecord(dbName, tName string, tID int64) {
rt.Frames = append(rt.Frames, FrameItem{
DBName: dbName,
TableName: tName,
TableID: tID,
IsRecord: true,
})
}
// addTableInRange insert a table into RegionDetail
// with index's id in range [startID,endID]. Table's
// record would be included when endID is MaxInt64.
func (rt *RegionDetail) addTableInRange(dbName string, curTable *model.TableInfo, startID, endID int64) {
tName := curTable.Name.String()
tID := curTable.ID
for _, index := range curTable.Indices {
if index.ID >= startID && index.ID <= endID {
rt.addIndex(
dbName,
tName,
tID,
index.Name.String(),
index.ID)
}
}
if endID == math.MaxInt64 {
rt.addRecord(dbName, tName, tID)
}
}
// FrameItem includes a index's or record's meta data with table's info.
type FrameItem struct {
DBName string `json:"db_name"`
TableName string `json:"table_name"`
TableID int64 `json:"table_id"`
IsRecord bool `json:"is_record"`
IndexName string `json:"index_name,omitempty"`
IndexID int64 `json:"index_id,omitempty"`
}
// RegionFrameRange contains a frame range info which the region covered.
type RegionFrameRange struct {
first *FrameItem // start frame of the region
last *FrameItem // end frame of the region
region *tikv.KeyLocation // the region
}
func (t *regionHandlerTool) getRegionsMeta(regionIDs []uint64) ([]RegionMeta, error) {
regions := make([]RegionMeta, len(regionIDs))
for i, regionID := range regionIDs {
meta, leader, err := t.regionCache.PDClient().GetRegionByID(goctx.TODO(), regionID)
if err != nil {
return nil, err
}
regions[i] = RegionMeta{
ID: regionID,
Leader: leader,
Peers: meta.Peers,
RegionEpoch: meta.RegionEpoch,
}
}
return regions, nil
}
// ServeHTTP handles request of list a table's regions.
func (rh tableRegionsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// parse params
params := mux.Vars(req)
dbName := params[pDBName]
tableName := params[pTableName]
schema, err := rh.schema()
if err != nil {
rh.writeError(w, err)
return
}
// get table's schema.
table, err := schema.TableByName(model.NewCIStr(dbName), model.NewCIStr(tableName))
if err != nil {
rh.writeError(w, err)
return
}
tableID := table.Meta().ID
// for record
startKey, endKey := tablecodec.GetTableHandleKeyRange(tableID)
recordRegionIDs, err := rh.regionCache.ListRegionIDsInKeyRange(rh.bo, startKey, endKey)
if err != nil {
rh.writeError(w, err)
return
}
recordRegions, err := rh.getRegionsMeta(recordRegionIDs)
if err != nil {
rh.writeError(w, err)
return
}
// for indices
indices := make([]IndexRegions, len(table.Indices()))
for i, index := range table.Indices() {
indexID := index.Meta().ID
indices[i].Name = index.Meta().Name.String()
indices[i].ID = indexID
startKey, endKey := tablecodec.GetTableIndexKeyRange(tableID, indexID)
rIDs, err := rh.regionCache.ListRegionIDsInKeyRange(rh.bo, startKey, endKey)
if err != nil {
rh.writeError(w, err)
return
}
indices[i].Regions, err = rh.getRegionsMeta(rIDs)
if err != nil {
rh.writeError(w, err)
return
}
}
tableRegions := &TableRegions{
TableName: tableName,
TableID: tableID,
Indices: indices,
RecordRegions: recordRegions,
}
rh.writeData(w, tableRegions)
}
// ServeHTTP handles request of get region by ID.
func (rh regionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// parse and check params
params := mux.Vars(req)
if _, ok := params[pRegionID]; !ok {
startKey := []byte{'m'}
endKey := []byte{'n'}
recordRegionIDs, err := rh.regionCache.ListRegionIDsInKeyRange(rh.bo, startKey, endKey)
if err != nil {
rh.writeError(w, err)
return
}
recordRegions, err := rh.getRegionsMeta(recordRegionIDs)
if err != nil {
rh.writeError(w, err)
return
}
rh.writeData(w, recordRegions)
return
}
regionIDInt, err := strconv.ParseInt(params[pRegionID], 0, 64)
if err != nil {
rh.writeError(w, err)
return
}
regionID := uint64(regionIDInt)
// locate region
region, err := rh.regionCache.LocateRegionByID(rh.bo, regionID)
if err != nil {
rh.writeError(w, err)
return
}
frameRange, err := NewRegionFrameRange(region)
if err != nil {
rh.writeError(w, err)
return
}
// create RegionDetail from RegionFrameRange
regionDetail := &RegionDetail{
RegionID: regionID,
StartKey: region.StartKey,
EndKey: region.EndKey,
}
schema, err := rh.schema()
if err != nil {
rh.writeError(w, err)
return
}
// Since we need a database's name for each frame, and a table's database name can not
// get from table's ID directly. Above all, here do dot process like
// `for id in [frameRange.firstTableID,frameRange.endTableID]`
// on [frameRange.firstTableID,frameRange.endTableID] is small enough.
for _, db := range schema.AllSchemas() {
for _, table := range db.Tables {
start, end := frameRange.getIndexRangeForTable(table.ID)
regionDetail.addTableInRange(db.Name.String(), table, start, end)
}
}
rh.writeData(w, regionDetail)
}
func (rh *regionHandler) writeError(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusBadRequest)
_, err = w.Write([]byte(err.Error()))
terror.Log(errors.Trace(err))
}
func (rh *regionHandler) writeData(w http.ResponseWriter, data interface{}) {
js, err := json.Marshal(data)
if err != nil {
rh.writeError(w, err)
return
}
log.Info(string(js))
// write response
w.Header().Set(headerContentType, contentTypeJSON)
w.WriteHeader(http.StatusOK)
_, err = w.Write(js)
terror.Log(errors.Trace(err))
}
// NewFrameItemFromRegionKey creates a FrameItem with region's startKey or endKey,
// returns err when key is illegal.
func NewFrameItemFromRegionKey(key []byte) (frame *FrameItem, err error) {
_, key, err = codec.DecodeBytes(key)
if err != nil {
return
}
frame = &FrameItem{}
frame.TableID, frame.IndexID, frame.IsRecord, err = tablecodec.DecodeKeyHead(key)
if err == nil || bytes.HasPrefix(key, tablecodec.TablePrefix()) {
return
}
// key start with tablePrefix must be either record key or index key
// That's means table's record key and index key are always together
// in the continuous interval. And for key with prefix smaller than
// tablePrefix, is smaller than all tables. While for key with prefix
// bigger than tablePrefix, means is bigger than all tables.
err = nil
if bytes.Compare(key, tablecodec.TablePrefix()) < 0 {
frame.TableID = math.MinInt64
frame.IndexID = math.MinInt64
frame.IsRecord = false
return
}
// bigger than tablePrefix, means is bigger than all tables.
frame.TableID = math.MaxInt64
frame.TableID = math.MaxInt64
frame.IsRecord = true
return
}
// NewRegionFrameRange init a NewRegionFrameRange with region info.
func NewRegionFrameRange(region *tikv.KeyLocation) (idxRange *RegionFrameRange, err error) {
var first, last *FrameItem
// check and init first frame
if len(region.StartKey) > 0 {
first, err = NewFrameItemFromRegionKey(region.StartKey)
if err != nil {
return
}
} else { // empty startKey means start with -infinite
first = &FrameItem{
IndexID: int64(math.MinInt64),
IsRecord: false,
TableID: int64(math.MinInt64),
}
}
// check and init last frame
if len(region.EndKey) > 0 {
last, err = NewFrameItemFromRegionKey(region.EndKey)
if err != nil {
return
}
} else { // empty endKey means end with +infinite
last = &FrameItem{
TableID: int64(math.MaxInt64),
IndexID: int64(math.MaxInt64),
IsRecord: true,
}
}
idxRange = &RegionFrameRange{
region: region,
first: first,
last: last,
}
return idxRange, nil
}
// getFirstIdxIdRange return the first table's index range.
// 1. [start,end] means index id in [start,end] are needed,while record key is not in.
// 2. [start,~) means index's id in [start,~) are needed including record key index.
// 3. (~,~) means only record key index is needed
func (ir *RegionFrameRange) getFirstIdxIDRange() (start, end int64) {
start = int64(math.MinInt64)
end = int64(math.MaxInt64)
if ir.first.IsRecord {
start = end // need record key only,
return
}
start = ir.first.IndexID
if ir.first.TableID != ir.last.TableID || ir.last.IsRecord {
return // [start,~)
}
end = ir.last.IndexID // [start,end]
return
}
// getLastInxIdRange return the last table's index range.
// (~,end] means index's id in (~,end] are legal, record key index not included.
// (~,~) means all indexes are legal include record key index.
func (ir *RegionFrameRange) getLastInxIDRange() (start, end int64) {
start = int64(math.MinInt64)
end = int64(math.MaxInt64)
if ir.last.IsRecord {
return
}
end = ir.last.IndexID
return
}
// getIndexRangeForTable return the legal index range for table with tableID.
// end=math.MaxInt64 means record key index is included.
func (ir *RegionFrameRange) getIndexRangeForTable(tableID int64) (start, end int64) {
switch tableID {
case ir.firstTableID():
return ir.getFirstIdxIDRange()
case ir.lastTableID():
return ir.getLastInxIDRange()
default:
if tableID < ir.lastTableID() && tableID > ir.firstTableID() {
return int64(math.MinInt64), int64(math.MaxInt64)
}
}
return int64(math.MaxInt64), int64(math.MinInt64)
}
func (ir RegionFrameRange) firstTableID() int64 {
return ir.first.TableID
}
func (ir RegionFrameRange) lastTableID() int64 {
return ir.last.TableID
}
// ServeHTTP handles request of list a table's regions.
func (rh mvccTxnHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var data interface{}
params := mux.Vars(req)
var err error
if rh.op == opMvccGetByKey {
data, err = rh.handleMvccGetByKey(params)
} else {
data, err = rh.handleMvccGetByTxn(params)
}
if err != nil {
rh.writeError(w, err)
} else {
rh.writeData(w, data)
}
}
func (rh mvccTxnHandler) handleMvccGetByKey(params map[string]string) (interface{}, error) {
recordID, err := strconv.ParseInt(params[pRecordID], 0, 64)
if err != nil {
return nil, err
}
tableID, err := rh.getTableID(params[pDBName], params[pTableName])
if err != nil {
return nil, err
}
return rh.getMvccByRecordID(tableID, recordID)
}
func (rh *mvccTxnHandler) handleMvccGetByTxn(params map[string]string) (interface{}, error) {
startTS, err := strconv.ParseInt(params[pStartTS], 0, 64)
if err != nil {
return nil, err
}
startKey := []byte("")
endKey := []byte("")
dbName := params[pDBName]
if len(dbName) > 0 {
tableID, err := rh.getTableID(params[pDBName], params[pTableName])
if err != nil {
return nil, err
}
startKey = tablecodec.EncodeTablePrefix(tableID)
endKey = tablecodec.EncodeRowKeyWithHandle(tableID, math.MaxInt64)
}
return rh.getMvccByStartTs(uint64(startTS), startKey, endKey)
}
func (t *regionHandlerTool) getMvccByRecordID(tableID, recordID int64) (*kvrpcpb.MvccGetByKeyResponse, error) {
encodeKey := tablecodec.EncodeRowKeyWithHandle(tableID, recordID)
keyLocation, err := t.regionCache.LocateKey(t.bo, encodeKey)
if err != nil {
return nil, err
}
tikvReq := &tikvrpc.Request{
Type: tikvrpc.CmdMvccGetByKey,
MvccGetByKey: &kvrpcpb.MvccGetByKeyRequest{
Key: encodeKey,
},
}
kvResp, err := t.store.SendReq(t.bo, tikvReq, keyLocation.Region, time.Minute)
log.Info(string(encodeKey), keyLocation.Region, string(keyLocation.StartKey), string(keyLocation.EndKey), kvResp, err)
if err != nil {
return nil, err
}
return kvResp.MvccGetByKey, nil
}
func (t *regionHandlerTool) getMvccByStartTs(startTS uint64, startKey, endKey []byte) (*kvrpcpb.MvccGetByStartTsResponse, error) {
for {
curRegion, err := t.regionCache.LocateKey(t.bo, startKey)
if err != nil {
log.Error(startTS, startKey, err)
return nil, errors.Trace(err)
}
tikvReq := &tikvrpc.Request{
Type: tikvrpc.CmdMvccGetByStartTs,
MvccGetByStartTs: &kvrpcpb.MvccGetByStartTsRequest{
StartTs: startTS,
},
}
tikvReq.Context.Priority = kvrpcpb.CommandPri_Low
kvResp, err := t.store.SendReq(t.bo, tikvReq, curRegion.Region, time.Hour)
log.Info(startTS, string(startKey), curRegion.Region, string(curRegion.StartKey), string(curRegion.EndKey), kvResp)
if err != nil {
log.Error(startTS, string(startKey), curRegion.Region, string(curRegion.StartKey), string(curRegion.EndKey), err)
return nil, err
}
data := kvResp.MvccGetByStartTS
if err := data.GetRegionError(); err != nil {
log.Warn(startTS, string(startKey), curRegion.Region, string(curRegion.StartKey), string(curRegion.EndKey), err)
continue
}
if len(data.GetError()) > 0 {
log.Error(startTS, string(startKey), curRegion.Region, string(curRegion.StartKey), string(curRegion.EndKey), data.GetError())
return nil, errors.New(data.GetError())
}
key := data.GetKey()
if len(key) > 0 {
return data, nil
}
if len(endKey) > 0 && curRegion.Contains(endKey) {
return nil, nil
}
if len(curRegion.EndKey) == 0 {
return nil, nil
}
startKey = curRegion.EndKey
}
}
func (t *regionHandlerTool) getTableID(dbName, tableName string) (int64, error) {
schema, err := t.schema()
if err != nil {
return 0, err
}
table, err := schema.TableByName(model.NewCIStr(dbName), model.NewCIStr(tableName))
if err != nil {
return 0, err
}
return table.Meta().ID, nil
}
func (t *regionHandlerTool) schema() (infoschema.InfoSchema, error) {
session, err := tidb.CreateSession(t.store.(kv.Storage))
if err != nil {
err = errors.Trace(err)
return nil, err
}
return sessionctx.GetDomain(session.(context.Context)).InfoSchema(), nil
}