-
Notifications
You must be signed in to change notification settings - Fork 8
/
table.go
301 lines (262 loc) · 7.71 KB
/
table.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
// Copyright 2022 Block, Inc.
package waitiotable
import (
"context"
"database/sql"
"fmt"
"math"
"time"
"github.com/cashapp/blip"
"github.com/cashapp/blip/errors"
)
const (
DOMAIN = "wait.io.table"
OPT_EXCLUDE = "exclude"
OPT_INCLUDE = "include"
OPT_TRUNCATE_TABLE = "truncate-table"
OPT_TRUNCATE_TIMEOUT = "truncate-timeout"
OPT_ALL = "all"
OPT_EXCLUDE_DEFAULT = "mysql.*,information_schema.*,performance_schema.*,sys.*"
TRUNCATE_QUERY = "TRUNCATE TABLE performance_schema.table_io_waits_summary_by_table"
ERR_TRUNCATE_FAILED = "truncate-timeout"
LOCKWAIT_QUERY = "SET @@session.lock_wait_timeout=%d"
)
var (
columnNames = []string{
"sum_timer_wait",
"min_timer_wait",
"avg_timer_wait",
"max_timer_wait",
"count_read",
"sum_timer_read",
"min_timer_read",
"avg_timer_read",
"max_timer_read",
"count_write",
"sum_timer_write",
"min_timer_write",
"avg_timer_write",
"max_timer_write",
"count_fetch",
"sum_timer_fetch",
"min_timer_fetch",
"avg_timer_fetch",
"max_timer_fetch",
"count_insert",
"sum_timer_insert",
"min_timer_insert",
"avg_timer_insert",
"max_timer_insert",
"count_update",
"sum_timer_update",
"min_timer_update",
"avg_timer_update",
"max_timer_update",
"count_delete",
"sum_timer_delete",
"min_timer_delete",
"avg_timer_delete",
"max_timer_delete",
}
columnExists map[string]struct{}
)
func init() {
columnExists = make(map[string]struct{}, len(columnNames))
for _, name := range columnNames {
columnExists[name] = struct{}{}
}
}
type tableOptions struct {
query string
truncate bool
truncateTimeout time.Duration
stop bool
truncateErrPolicy *errors.TruncateErrorPolicy
lockWaitQuery string
}
// Table collects table io for domain wait.io.table.
type Table struct {
db *sql.DB
// --
options map[string]*tableOptions
}
// Verify collector implements blip.Collector interface.
var _ blip.Collector = &Table{}
// NewTable makes a new Table collector,
func NewTable(db *sql.DB) *Table {
return &Table{
db: db,
options: map[string]*tableOptions{},
}
}
// Domain returns the Blip metric domain name (DOMAIN const).
func (t *Table) Domain() string {
return DOMAIN
}
// Help returns the output for blip --print-domains.
func (t *Table) Help() blip.CollectorHelp {
return blip.CollectorHelp{
Domain: DOMAIN,
Description: "Table IO Waits",
Options: map[string]blip.CollectorHelpOption{
OPT_INCLUDE: {
Name: OPT_INCLUDE,
Desc: "Comma-separated list of database or table names to include (overrides option " + OPT_EXCLUDE + ")",
},
OPT_EXCLUDE: {
Name: OPT_EXCLUDE,
Desc: "Comma-separated list of database or table names to exclude (ignored if " + OPT_INCLUDE + " is set)",
Default: OPT_EXCLUDE_DEFAULT,
},
OPT_TRUNCATE_TABLE: {
Name: OPT_TRUNCATE_TABLE,
Desc: "If the source table should be truncated to reset data after each retrieval",
Default: "yes",
Values: map[string]string{
"yes": "Truncate source table after each retrieval",
"no": "Do not truncate source table after each retrieval",
},
},
OPT_TRUNCATE_TIMEOUT: {
Name: OPT_TRUNCATE_TIMEOUT,
Desc: "The amount of time to attempt to truncate the source table before timing out",
Default: "250ms",
},
OPT_ALL: {
Name: OPT_ALL,
Desc: "Collect all metrics",
Default: "no",
Values: map[string]string{
"yes": "All metrics (ignore metrics list)",
"no": "Specified metrics",
},
},
},
Groups: []blip.CollectorKeyValue{
{Key: "db", Value: "the database name for the corresponding table io, or empty string for all dbs"},
{Key: "tbl", Value: "the table name for the corresponding table io, or empty string for all tables"},
},
Errors: map[string]blip.CollectorHelpError{
ERR_TRUNCATE_FAILED: {
Name: ERR_TRUNCATE_FAILED,
Handles: "Truncation failures on 'performance_schema.table_io_waits_summary_by_table'",
Default: errors.NewPolicy("").String(),
},
},
}
}
// Prepare prepares the collector for the given plan.
func (t *Table) Prepare(ctx context.Context, plan blip.Plan) (func(), error) {
LEVEL:
for _, level := range plan.Levels {
o := tableOptions{}
dom, ok := level.Collect[DOMAIN]
if !ok {
continue LEVEL // not collected in this level
}
if dom.Options == nil {
dom.Options = make(map[string]string)
}
if _, ok := dom.Options[OPT_EXCLUDE]; !ok {
dom.Options[OPT_EXCLUDE] = OPT_EXCLUDE_DEFAULT
}
o.query = TableIoWaitQuery(dom.Options, dom.Metrics)
if truncate, ok := dom.Options[OPT_TRUNCATE_TABLE]; ok && truncate == "no" {
o.truncate = false
} else {
o.truncate = true // default
}
if truncateTimeout, ok := dom.Options[OPT_TRUNCATE_TIMEOUT]; ok && o.truncate {
if duration, err := time.ParseDuration(truncateTimeout); err != nil {
return nil, fmt.Errorf("Invalid truncate duration: %v", err)
} else {
o.truncateTimeout = duration
}
} else {
o.truncateTimeout = 250 * time.Millisecond // default
}
if o.truncate {
// Setup our lock wait timeout. It needs to be at least as long
// as our truncate timeout, but the granularity of the lock wait
// timeout is seconds, so we round up to the nearest second that is
// greater than our truncate timeout.
lockWaitTimeout := math.Ceil(o.truncateTimeout.Seconds())
if lockWaitTimeout < 1.0 {
lockWaitTimeout = 1
}
o.lockWaitQuery = fmt.Sprintf(LOCKWAIT_QUERY, int64(lockWaitTimeout))
o.truncateErrPolicy = errors.NewTruncateErrorPolicy(dom.Errors[ERR_TRUNCATE_FAILED])
blip.Debug("error policy: %s=%s", ERR_TRUNCATE_FAILED, o.truncateErrPolicy.Policy)
}
t.options[level.Name] = &o
}
return nil, nil
}
func (t *Table) Collect(ctx context.Context, levelName string) ([]blip.MetricValue, error) {
o, ok := t.options[levelName]
if !ok {
return nil, nil
}
if o.stop {
blip.Debug("stopped by previous error")
return nil, nil
}
rows, err := t.db.QueryContext(ctx, o.query)
if err != nil {
return nil, err
}
defer rows.Close()
var (
metrics []blip.MetricValue
dbName string
tblName string
values []interface{}
)
cols, err := rows.Columns()
if err != nil {
return nil, fmt.Errorf("Failed to get columns for wait.io.table: %v", err)
}
values = make([]interface{}, len(cols))
values[0] = new(string)
values[1] = new(string)
for i := 2; i < len(cols); i++ {
values[i] = new(int64)
}
for rows.Next() {
if err = rows.Scan(values...); err != nil {
return nil, err
}
dbName = *values[0].(*string)
tblName = *values[1].(*string)
for i := 2; i < len(cols); i++ {
m := blip.MetricValue{
Name: cols[i],
Type: blip.COUNTER,
Group: map[string]string{"db": dbName, "tbl": tblName},
}
m.Value = float64(*values[i].(*int64))
metrics = append(metrics, m)
}
}
if o.truncate {
conn, err := t.db.Conn(ctx)
if err == nil {
defer conn.Close()
// Set `lock_wait_timeout` to prevent our query from begin blocked for too long
// due to metadata locking. We treat a failure to set the lock wait timeout
// the same as a truncate timeout, as not setting creates a risk of having a thread
// hang for an extended period of time.
_, err = conn.ExecContext(ctx, o.lockWaitQuery)
if err == nil {
trCtx, cancelFn := context.WithTimeout(ctx, o.truncateTimeout)
defer cancelFn()
_, err = conn.ExecContext(trCtx, TRUNCATE_QUERY)
}
}
// Process any errors (or lack thereof) with the TruncateErrorPolicy as there is special handling
// for the metric values that need to be applied, even if there is not an error. See comments
// in `TruncateErrorPolicy` for more details.
return o.truncateErrPolicy.TruncateError(err, &o.stop, metrics)
}
return metrics, err
}