forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_feature_usage.go
263 lines (239 loc) · 9.1 KB
/
data_feature_usage.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
// Copyright 2021 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 telemetry
import (
"context"
"sync"
"time"
"github.com/cznic/mathutil"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/store/tikv/metrics"
"github.com/pingcap/tidb/util/sqlexec"
)
type featureUsageInfo struct {
TxnUsageInfo *TxnUsageInfo `json:"txnUsageInfo"`
CoprCacheUsed []*CoprCacheUsedWindowItem `json:"coprCacheUsed"`
ClusterIndexUsed map[string]bool `json:"clusterIndexUsed"`
TiFlashUsed []*TiFlashUsageItem `json:"tiFlashUsed"`
}
// FeatureTaskChan is the update task channel for telemetry feature data.
var FeatureTaskChan = make(chan *FeatureTask, 1<<16)
// FeatureTask is the update task for telemetry feature data.
type FeatureTask struct {
TiFlashPushDown bool
TiFLashExchangePushDown bool
CoprRespTimes uint64
CoprCacheHitNum uint64
}
// UpdateFeature update feature data for one task.
func UpdateFeature(task *FeatureTask) {
CoprocessorCacheTelemetry.Lock.Lock()
length := len(CoprocessorCacheTelemetry.MinuteWindow)
if length == 0 || time.Since(*CoprocessorCacheTelemetry.MinuteWindow[length-1].BeginAt) >= time.Minute {
var i int
for i = 0; i < length && time.Since(*CoprocessorCacheTelemetry.MinuteWindow[i].BeginAt) >= ReportInterval; i++ {
}
if i < length {
CoprocessorCacheTelemetry.MinuteWindow = CoprocessorCacheTelemetry.MinuteWindow[i:]
}
CoprocessorCacheTelemetry.MinuteWindow = append(CoprocessorCacheTelemetry.MinuteWindow, CoprCacheUsedWindowItem{})
length -= i - 1
CoprocessorCacheTelemetry.MinuteWindow[length-1].BeginAt = new(time.Time)
*CoprocessorCacheTelemetry.MinuteWindow[length-1].BeginAt = time.Now()
}
if task.CoprRespTimes > 0 {
ratio := float64(task.CoprCacheHitNum) / float64(task.CoprRespTimes)
switch {
case ratio >= 0:
CoprocessorCacheTelemetry.MinuteWindow[length-1].P0++
fallthrough
case ratio >= 0.01:
CoprocessorCacheTelemetry.MinuteWindow[length-1].P1++
fallthrough
case ratio >= 0.1:
CoprocessorCacheTelemetry.MinuteWindow[length-1].P10++
fallthrough
case ratio >= 0.2:
CoprocessorCacheTelemetry.MinuteWindow[length-1].P20++
fallthrough
case ratio >= 0.4:
CoprocessorCacheTelemetry.MinuteWindow[length-1].P40++
fallthrough
case ratio >= 0.8:
CoprocessorCacheTelemetry.MinuteWindow[length-1].P80++
fallthrough
case ratio >= 1:
CoprocessorCacheTelemetry.MinuteWindow[length-1].P100++
}
} else {
CoprocessorCacheTelemetry.MinuteWindow[length-1].P0++
}
CoprocessorCacheTelemetry.Lock.Unlock()
TiFlashUsageTelemetry.Lock.Lock()
length = len(TiFlashUsageTelemetry.MinuteWindow)
if length == 0 || time.Since(*TiFlashUsageTelemetry.MinuteWindow[length-1].BeginAt) >= time.Minute {
var i int
for i = 0; i < length && time.Since(*TiFlashUsageTelemetry.MinuteWindow[i].BeginAt) >= ReportInterval; i++ {
}
TiFlashUsageTelemetry.MinuteWindow = TiFlashUsageTelemetry.MinuteWindow[i:]
TiFlashUsageTelemetry.MinuteWindow = append(TiFlashUsageTelemetry.MinuteWindow, TiFlashUsageItem{})
length -= i - 1
TiFlashUsageTelemetry.MinuteWindow[length-1].BeginAt = new(time.Time)
*TiFlashUsageTelemetry.MinuteWindow[length-1].BeginAt = time.Now()
}
TiFlashUsageTelemetry.MinuteWindow[length-1].TotalNumbers++
if task.TiFlashPushDown {
TiFlashUsageTelemetry.MinuteWindow[length-1].TiFlashPushDownNumbers++
}
if task.TiFLashExchangePushDown {
TiFlashUsageTelemetry.MinuteWindow[length-1].TiFlashExchangePushDownNumbers++
}
TiFlashUsageTelemetry.Lock.Unlock()
}
// CoprocessorCacheTelemetry is to save the global coprocessor cache telemetry data.
var CoprocessorCacheTelemetry = struct {
MinuteWindow []CoprCacheUsedWindowItem
Lock sync.RWMutex
}{Lock: sync.RWMutex{}}
// CoprCacheUsedWindowItem is the coprocessor cache telemetry data struct.
type CoprCacheUsedWindowItem struct {
P0 uint64 `json:"gte0"`
P1 uint64 `json:"gte1"`
P10 uint64 `json:"gte10"`
P20 uint64 `json:"gte20"`
P40 uint64 `json:"gte40"`
P80 uint64 `json:"gte80"`
P100 uint64 `json:"gte100"`
BeginAt *time.Time `json:"beginAt"`
}
// TiFlashUsageTelemetry is to save the global TiFlash usage telemetry data.
var TiFlashUsageTelemetry = struct {
MinuteWindow []TiFlashUsageItem
Lock sync.RWMutex
}{Lock: sync.RWMutex{}}
// TiFlashUsageItem is the TiFlash usage telemetry data struct.
type TiFlashUsageItem struct {
TotalNumbers uint64 `json:"totalNumbers"`
TiFlashPushDownNumbers uint64 `json:"tiFlashPushDownNumbers"`
TiFlashExchangePushDownNumbers uint64 `json:"tiFlashExchangePushDownNumbers"`
BeginAt *time.Time `json:"beginAt"`
}
func getTelemetryFeatureUsageInfo(ctx sessionctx.Context) (*featureUsageInfo, error) {
// init
usageInfo := featureUsageInfo{
CoprCacheUsed: make([]*CoprCacheUsedWindowItem, 0, ReportInterval/time.Hour),
ClusterIndexUsed: make(map[string]bool),
TiFlashUsed: make([]*TiFlashUsageItem, 0, ReportInterval/time.Hour),
}
// coprocessor cache
CoprocessorCacheTelemetry.Lock.Lock()
maxLen := 0
timeNow := time.Now()
for _, window := range CoprocessorCacheTelemetry.MinuteWindow {
timeSince := timeNow.Sub(*window.BeginAt)
if timeSince >= ReportInterval {
continue
}
maxLen = mathutil.Max(maxLen, int(timeSince/time.Hour))
i := maxLen - int(timeSince/time.Hour)
if len(usageInfo.CoprCacheUsed) <= i {
usageInfo.CoprCacheUsed = append(usageInfo.CoprCacheUsed, &CoprCacheUsedWindowItem{})
}
usageInfo.CoprCacheUsed[i].P0 += window.P0
usageInfo.CoprCacheUsed[i].P1 += window.P1
usageInfo.CoprCacheUsed[i].P10 += window.P10
usageInfo.CoprCacheUsed[i].P20 += window.P20
usageInfo.CoprCacheUsed[i].P40 += window.P40
usageInfo.CoprCacheUsed[i].P80 += window.P80
usageInfo.CoprCacheUsed[i].P100 += window.P100
if usageInfo.CoprCacheUsed[i].BeginAt == nil {
usageInfo.CoprCacheUsed[i].BeginAt = window.BeginAt
}
}
CoprocessorCacheTelemetry.Lock.Unlock()
// TiFlash usage
TiFlashUsageTelemetry.Lock.Lock()
maxLen = 0
for _, window := range TiFlashUsageTelemetry.MinuteWindow {
timeSince := timeNow.Sub(*window.BeginAt)
if timeSince >= ReportInterval {
continue
}
maxLen = mathutil.Max(maxLen, int(timeSince/time.Hour))
i := maxLen - int(timeSince/time.Hour)
if len(usageInfo.TiFlashUsed) <= i {
usageInfo.TiFlashUsed = append(usageInfo.TiFlashUsed, &TiFlashUsageItem{})
}
usageInfo.TiFlashUsed[i].TotalNumbers += window.TotalNumbers
usageInfo.TiFlashUsed[i].TiFlashPushDownNumbers += window.TiFlashPushDownNumbers
usageInfo.TiFlashUsed[i].TiFlashExchangePushDownNumbers += window.TiFlashExchangePushDownNumbers
if usageInfo.TiFlashUsed[i].BeginAt == nil {
usageInfo.TiFlashUsed[i].BeginAt = window.BeginAt
}
}
TiFlashUsageTelemetry.Lock.Unlock()
// cluster index
exec := ctx.(sqlexec.RestrictedSQLExecutor)
stmt, err := exec.ParseWithParams(context.TODO(), `
SELECT left(sha2(TABLE_NAME, 256), 6) name, TIDB_PK_TYPE
FROM information_schema.tables
WHERE table_schema not in ('INFORMATION_SCHEMA', 'METRICS_SCHEMA', 'PERFORMANCE_SCHEMA', 'mysql')
ORDER BY name
limit 10000`)
if err != nil {
return nil, err
}
rows, _, err := exec.ExecRestrictedStmt(context.TODO(), stmt)
if err != nil {
return nil, err
}
for _, row := range rows {
if row.Len() < 2 {
continue
}
isClustered := false
if row.GetString(1) == "CLUSTERED" {
isClustered = true
}
usageInfo.ClusterIndexUsed[row.GetString(0)] = isClustered
}
// transaction related feature
usageInfo.TxnUsageInfo = GetTxnUsageInfo(ctx)
return &usageInfo, nil
}
// TxnUsageInfo records the usage info of transaction related features, including
// async-commit, 1PC and counters of transactions committed with different protocols.
type TxnUsageInfo struct {
AsyncCommitUsed bool `json:"asyncCommitUsed"`
OnePCUsed bool `json:"onePCUsed"`
TxnCommitCounter metrics.TxnCommitCounter `json:"txnCommitCounter"`
}
var initialTxnCommitCounter metrics.TxnCommitCounter
// GetTxnUsageInfo gets the usage info of transaction related features. It's exported for tests.
func GetTxnUsageInfo(ctx sessionctx.Context) *TxnUsageInfo {
asyncCommitUsed := false
if val, err := variable.GetGlobalSystemVar(ctx.GetSessionVars(), variable.TiDBEnableAsyncCommit); err == nil {
asyncCommitUsed = val == variable.BoolOn
}
onePCUsed := false
if val, err := variable.GetGlobalSystemVar(ctx.GetSessionVars(), variable.TiDBEnable1PC); err == nil {
onePCUsed = val == variable.BoolOn
}
curr := metrics.GetTxnCommitCounter()
diff := curr.Sub(initialTxnCommitCounter)
return &TxnUsageInfo{asyncCommitUsed, onePCUsed, diff}
}
func postReportTxnUsage() {
initialTxnCommitCounter = metrics.GetTxnCommitCounter()
}