-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
statement_diagnostics_requests.go
264 lines (232 loc) · 7.79 KB
/
statement_diagnostics_requests.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
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package server
import (
"context"
"fmt"
"time"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/errors"
)
type stmtDiagnosticsRequest struct {
ID int
StatementFingerprint string
Completed bool
StatementDiagnosticsID int
RequestedAt time.Time
// Zero value indicates that there is no minimum latency set on the request.
MinExecutionLatency time.Duration
// Zero value indicates that the request never expires.
ExpiresAt time.Time
}
type stmtDiagnostics struct {
ID int
StatementFingerprint string
CollectedAt time.Time
}
func (request *stmtDiagnosticsRequest) toProto() serverpb.StatementDiagnosticsReport {
resp := serverpb.StatementDiagnosticsReport{
Id: int64(request.ID),
Completed: request.Completed,
StatementFingerprint: request.StatementFingerprint,
StatementDiagnosticsId: int64(request.StatementDiagnosticsID),
RequestedAt: request.RequestedAt,
MinExecutionLatency: request.MinExecutionLatency,
ExpiresAt: request.ExpiresAt,
}
return resp
}
func (diagnostics *stmtDiagnostics) toProto() serverpb.StatementDiagnostics {
resp := serverpb.StatementDiagnostics{
Id: int64(diagnostics.ID),
StatementFingerprint: diagnostics.StatementFingerprint,
CollectedAt: diagnostics.CollectedAt,
}
return resp
}
// CreateStatementDiagnosticsReport creates a statement diagnostics
// request in the `system.statement_diagnostics_requests` table
// to trace the next query matching the provided fingerprint.
func (s *statusServer) CreateStatementDiagnosticsReport(
ctx context.Context, req *serverpb.CreateStatementDiagnosticsReportRequest,
) (*serverpb.CreateStatementDiagnosticsReportResponse, error) {
ctx = propagateGatewayMetadata(ctx)
ctx = s.AnnotateCtx(ctx)
if _, err := s.privilegeChecker.requireViewActivityPermission(ctx); err != nil {
return nil, err
}
response := &serverpb.CreateStatementDiagnosticsReportResponse{
Report: &serverpb.StatementDiagnosticsReport{},
}
err := s.stmtDiagnosticsRequester.InsertRequest(
ctx, req.StatementFingerprint, req.MinExecutionLatency, req.ExpiresAfter,
)
if err != nil {
return nil, err
}
response.Report.StatementFingerprint = req.StatementFingerprint
return response, nil
}
// CancelStatementDiagnosticsReport cancels the statement diagnostics request by
// updating the corresponding row from the system.statement_diagnostics_requests
// table to be expired.
func (s *statusServer) CancelStatementDiagnosticsReport(
ctx context.Context, req *serverpb.CancelStatementDiagnosticsReportRequest,
) (*serverpb.CancelStatementDiagnosticsReportResponse, error) {
ctx = propagateGatewayMetadata(ctx)
ctx = s.AnnotateCtx(ctx)
if _, err := s.privilegeChecker.requireViewActivityPermission(ctx); err != nil {
return nil, err
}
var response serverpb.CancelStatementDiagnosticsReportResponse
err := s.stmtDiagnosticsRequester.CancelRequest(ctx, req.StatementFingerprint)
if err != nil {
response.Canceled = false
response.Error = err.Error()
} else {
response.Canceled = true
}
return &response, nil
}
// StatementDiagnosticsRequests retrieves all statement diagnostics
// requests in the `system.statement_diagnostics_requests` table that
// have not yet expired.
func (s *statusServer) StatementDiagnosticsRequests(
ctx context.Context, req *serverpb.StatementDiagnosticsReportsRequest,
) (*serverpb.StatementDiagnosticsReportsResponse, error) {
ctx = propagateGatewayMetadata(ctx)
ctx = s.AnnotateCtx(ctx)
if _, err := s.privilegeChecker.requireViewActivityPermission(ctx); err != nil {
return nil, err
}
var err error
// TODO(yuzefovich): remove this version gating in 22.2.
var extraColumns string
if s.admin.server.st.Version.IsActive(ctx, clusterversion.AlterSystemStmtDiagReqs) {
extraColumns = `,
min_execution_latency,
expires_at`
}
// TODO(davidh): Add pagination to this request.
it, err := s.internalExecutor.QueryIteratorEx(ctx, "stmt-diag-get-all", nil, /* txn */
sessiondata.InternalExecutorOverride{
User: security.RootUserName(),
},
fmt.Sprintf(`SELECT
id,
statement_fingerprint,
completed,
statement_diagnostics_id,
requested_at%s
FROM
system.statement_diagnostics_requests`, extraColumns))
if err != nil {
return nil, err
}
var requests []stmtDiagnosticsRequest
var ok bool
for ok, err = it.Next(ctx); ok; ok, err = it.Next(ctx) {
row := it.Cur()
id := int(*row[0].(*tree.DInt))
statementFingerprint := string(*row[1].(*tree.DString))
completed := bool(*row[2].(*tree.DBool))
req := stmtDiagnosticsRequest{
ID: id,
StatementFingerprint: statementFingerprint,
Completed: completed,
}
if row[3] != tree.DNull {
sdi := int(*row[3].(*tree.DInt))
req.StatementDiagnosticsID = sdi
}
if requestedAt, ok := row[4].(*tree.DTimestampTZ); ok {
req.RequestedAt = requestedAt.Time
}
if extraColumns != "" {
if minExecutionLatency, ok := row[5].(*tree.DInterval); ok {
req.MinExecutionLatency = time.Duration(minExecutionLatency.Duration.Nanos())
}
if expiresAt, ok := row[6].(*tree.DTimestampTZ); ok {
req.ExpiresAt = expiresAt.Time
// Don't return already expired requests.
if req.ExpiresAt.Before(time.Now()) {
continue
}
}
}
requests = append(requests, req)
}
if err != nil {
return nil, err
}
response := &serverpb.StatementDiagnosticsReportsResponse{
Reports: make([]serverpb.StatementDiagnosticsReport, len(requests)),
}
for i, request := range requests {
response.Reports[i] = request.toProto()
}
return response, nil
}
// StatementDiagnostics retrieves a statement diagnostics instance
// identified by the given ID. These are in the
// `system.statement_diagnostics` table.
//
// This is generated once the trace is completed on a request created
// by the CreateStatementDiagnosticsRequest call and is linked to
// the original request with its ID.
func (s *statusServer) StatementDiagnostics(
ctx context.Context, req *serverpb.StatementDiagnosticsRequest,
) (*serverpb.StatementDiagnosticsResponse, error) {
ctx = propagateGatewayMetadata(ctx)
ctx = s.AnnotateCtx(ctx)
if _, err := s.privilegeChecker.requireViewActivityPermission(ctx); err != nil {
return nil, err
}
var err error
row, err := s.internalExecutor.QueryRowEx(ctx, "stmt-diag-get-one", nil, /* txn */
sessiondata.InternalExecutorOverride{
User: security.RootUserName(),
},
`SELECT
id,
statement_fingerprint,
collected_at
FROM
system.statement_diagnostics
WHERE
id = $1`, req.StatementDiagnosticsId)
if err != nil {
return nil, err
}
if row == nil {
return nil, errors.Newf(
"requested a statement diagnostic (%d) that does not exist",
req.StatementDiagnosticsId,
)
}
diagnostics := stmtDiagnostics{
ID: int(req.StatementDiagnosticsId),
}
if statementFingerprint, ok := row[1].(*tree.DString); ok {
diagnostics.StatementFingerprint = statementFingerprint.String()
}
if collectedAt, ok := row[2].(*tree.DTimestampTZ); ok {
diagnostics.CollectedAt = collectedAt.Time
}
diagnosticsProto := diagnostics.toProto()
response := &serverpb.StatementDiagnosticsResponse{
Diagnostics: &diagnosticsProto,
}
return response, nil
}