-
Notifications
You must be signed in to change notification settings - Fork 125
/
tidb.go
166 lines (140 loc) · 3.79 KB
/
tidb.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
// Copyright 2024 PingCAP, Inc. Licensed under Apache-2.0.
package decorator
import (
"context"
"encoding/hex"
"fmt"
"sync"
"time"
"go.etcd.io/etcd/clientv3"
"go.uber.org/fx"
"github.com/pingcap/tidb-dashboard/pkg/config"
"github.com/pingcap/tidb-dashboard/pkg/keyvisual/region"
"github.com/pingcap/tidb-dashboard/pkg/tidb"
"github.com/pingcap/tidb-dashboard/pkg/tidb/model"
)
// TiDBLabelStrategy implements the LabelStrategy interface. It obtains Label Information from TiDB.
func TiDBLabelStrategy(lc fx.Lifecycle, wg *sync.WaitGroup, etcdClient *clientv3.Client, tidbClient *tidb.Client) LabelStrategy {
s := &tidbLabelStrategy{
EtcdClient: etcdClient,
tidbClient: tidbClient,
SchemaVersion: -1,
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
wg.Add(1)
go func() {
defer wg.Done()
s.Background(ctx)
}()
return nil
},
})
return s
}
type tableDetail struct {
Name string
DB string
ID int64
Indices map[int64]string
}
type tidbLabelStrategy struct {
Config *config.Config
EtcdClient *clientv3.Client
TableMap sync.Map
tidbClient *tidb.Client
SchemaVersion int64
TidbAddress []string
}
type tidbLabeler struct {
TableMap *sync.Map
Buffer model.KeyInfoBuffer
}
func (s *tidbLabelStrategy) ReloadConfig(cfg *config.KeyVisualConfig) {}
func (s *tidbLabelStrategy) Background(ctx context.Context) {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s.updateMap(ctx)
}
}
}
func (s *tidbLabelStrategy) NewLabeler() Labeler {
return &tidbLabeler{
TableMap: &s.TableMap,
}
}
// CrossBorder does not allow cross tables or cross indexes within a table.
func (e *tidbLabeler) CrossBorder(startKey, endKey string) bool {
startInfo, _ := e.Buffer.DecodeKey(region.Bytes(startKey))
startIsMeta, startTableID := startInfo.MetaOrTable()
startIndex := startInfo.IndexInfo()
endInfo, _ := e.Buffer.DecodeKey(region.Bytes(endKey))
endIsMeta, endTableID := endInfo.MetaOrTable()
endIndex := endInfo.IndexInfo()
if startIsMeta || endIsMeta {
return startIsMeta != endIsMeta
}
if startTableID != endTableID {
return true
}
return startIndex != endIndex
}
// Label will parse the ID information of the table and index.
func (e *tidbLabeler) Label(keys []string) []LabelKey {
labelKeys := make([]LabelKey, len(keys))
for i, key := range keys {
labelKeys[i] = e.label(key)
}
if keys[0] == "" {
labelKeys[0] = globalStart
}
endIndex := len(keys) - 1
if keys[endIndex] == "" {
labelKeys[endIndex] = globalEnd
}
return labelKeys
}
func (e *tidbLabeler) label(key string) (label LabelKey) {
keyBytes := region.Bytes(key)
label.Key = hex.EncodeToString(keyBytes)
keyInfo, _ := e.Buffer.DecodeKey(keyBytes)
isMeta, tableID := keyInfo.MetaOrTable()
if isMeta {
label.Labels = append(label.Labels, "meta")
return
}
var detail *tableDetail
if v, ok := e.TableMap.Load(tableID); ok {
detail = v.(*tableDetail)
label.Labels = append(label.Labels, detail.DB, detail.Name)
} else {
label.Labels = append(label.Labels, fmt.Sprintf("table_%d", tableID))
}
if isCommonHandle, rowID := keyInfo.RowInfo(); isCommonHandle {
label.Labels = append(label.Labels, "row")
} else if rowID != 0 {
label.Labels = append(label.Labels, fmt.Sprintf("row_%d", rowID))
} else if indexID := keyInfo.IndexInfo(); indexID != 0 {
if detail == nil {
label.Labels = append(label.Labels, fmt.Sprintf("index_%d", indexID))
} else if name, ok := detail.Indices[indexID]; ok {
label.Labels = append(label.Labels, name)
} else {
label.Labels = append(label.Labels, fmt.Sprintf("index_%d", indexID))
}
}
return
}
var globalStart = LabelKey{
Key: "",
Labels: []string{"meta"},
}
var globalEnd = LabelKey{
Key: "",
Labels: []string{},
}