-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtableDetailsApi.ts
612 lines (590 loc) · 18.2 KB
/
tableDetailsApi.ts
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
// Copyright 2023 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.
import {
executeInternalSql,
formatApiResult,
LARGE_RESULT_SIZE,
LONG_TIMEOUT,
SqlApiQueryResponse,
SqlApiResponse,
SqlExecutionErrorMessage,
SqlExecutionRequest,
SqlStatement,
SqlTxnResult,
txnResultIsEmpty,
} from "./sqlApi";
import moment from "moment";
import { fromHexString, withTimeout } from "./util";
import { Format, Identifier, Join, SQL } from "./safesql";
import { cockroach } from "@cockroachlabs/crdb-protobuf-client";
import { IndexUsageStatistic, recommendDropUnusedIndex } from "../insights";
const { ZoneConfig } = cockroach.config.zonepb;
const { ZoneConfigurationLevel } = cockroach.server.serverpb;
type ZoneConfigType = cockroach.config.zonepb.ZoneConfig;
type ZoneConfigLevelType = cockroach.server.serverpb.ZoneConfigurationLevel;
export function newTableDetailsResponse(): TableDetailsResponse {
return {
id_resp: { table_id: "" },
create_stmt_resp: { statement: "" },
grants_resp: { grants: [] },
schema_details: { columns: [], indexes: [] },
zone_config_resp: {
configure_zone_statement: "",
zone_config: new ZoneConfig({
inherited_constraints: true,
inherited_lease_preferences: true,
}),
zone_config_level: ZoneConfigurationLevel.CLUSTER,
},
heuristics_details: { stats_last_created_at: null },
stats: {
ranges_data: {
range_count: 0,
live_bytes: 0,
total_bytes: 0,
live_percentage: 0,
// Note: we are currently populating this with replica ids which do not map 1 to 1
replica_count: 0,
node_count: 0,
node_ids: [],
},
pebble_data: {
approximate_disk_bytes: 0,
},
index_stats: {
has_index_recommendations: false,
},
},
};
}
export type TableDetailsResponse = {
id_resp: SqlApiQueryResponse<TableIdRow>;
create_stmt_resp: SqlApiQueryResponse<TableCreateStatementRow>;
grants_resp: SqlApiQueryResponse<TableGrantsResponse>;
schema_details: SqlApiQueryResponse<TableSchemaDetails>;
zone_config_resp: SqlApiQueryResponse<TableZoneConfigResponse>;
heuristics_details: SqlApiQueryResponse<TableHeuristicDetailsRow>;
stats: TableDetailsStats;
error?: SqlExecutionErrorMessage;
};
// Table ID.
type TableIdRow = {
table_id: string;
};
const getTableId: TableDetailsQuery<TableIdRow> = {
createStmt: (dbName, tableName) => {
const escFullTableName = Join(
[new Identifier(dbName).SQLString(), tableName],
new SQL("."),
);
return {
sql: `SELECT $1::regclass::oid as table_id`,
arguments: [escFullTableName.SQLString()],
};
},
addToTableDetail: (
txn_result: SqlTxnResult<TableIdRow>,
resp: TableDetailsResponse,
) => {
if (!txnResultIsEmpty(txn_result)) {
resp.id_resp.table_id = txn_result.rows[0].table_id;
} else {
txn_result.error = new Error("fetchTableId: unexpected empty results");
}
if (txn_result.error) {
resp.id_resp.error = txn_result.error;
}
},
};
// Table create statement.
type TableCreateStatementRow = { statement: string };
const getTableCreateStatement: TableDetailsQuery<TableCreateStatementRow> = {
createStmt: (dbName, tableName) => {
const escFullTableName = Join(
[new Identifier(dbName).SQLString(), tableName],
new SQL("."),
);
return {
sql: Format(`SELECT create_statement FROM [SHOW CREATE %1]`, [
escFullTableName,
]),
};
},
addToTableDetail: (
txn_result: SqlTxnResult<TableCreateStatementRow>,
resp: TableDetailsResponse,
) => {
if (!txnResultIsEmpty(txn_result)) {
resp.create_stmt_resp.statement = txn_result.rows[0].statement;
} else {
txn_result.error = new Error(
"getTableCreateStatement: unexpected empty results",
);
}
if (txn_result.error) {
resp.id_resp.error = txn_result.error;
}
},
};
// Table grants.
type TableGrantsResponse = {
grants: TableGrantsRow[];
};
type TableGrantsRow = {
user: string;
privileges: string[];
};
const getTableGrants: TableDetailsQuery<TableGrantsRow> = {
createStmt: (dbName, tableName) => {
const escFullTableName = Join(
[new Identifier(dbName).SQLString(), tableName],
new SQL("."),
);
return {
sql: Format(
`SELECT grantee as user, array_agg(privilege_type) as privileges
FROM [SHOW GRANTS ON TABLE %1] group by grantee`,
[escFullTableName],
),
};
},
addToTableDetail: (
txn_result: SqlTxnResult<TableGrantsRow>,
resp: TableDetailsResponse,
) => {
if (!txnResultIsEmpty(txn_result)) {
resp.grants_resp.grants = txn_result.rows;
}
if (txn_result.error) {
resp.id_resp.error = txn_result.error;
}
},
};
// Table schema details.
type TableSchemaDetails = TableSchemaDetailsRow & {
error?: Error;
};
type TableSchemaDetailsRow = {
columns: string[];
indexes: string[];
};
const getTableSchemaDetails: TableDetailsQuery<TableSchemaDetailsRow> = {
createStmt: (dbName, tableName) => {
const escFullTableName = Join(
[new Identifier(dbName).SQLString(), tableName],
new SQL("."),
);
return {
sql: Format(
`WITH
columns AS (SELECT column_name FROM [SHOW COLUMNS FROM %1]),
indexes AS (SELECT index_name FROM [SHOW INDEX FROM %1])
SELECT
distinct(column_name) as columns,
distinct(index_name) as indexes
FROM columns CROSS JOIN indexes`,
[escFullTableName],
),
};
},
addToTableDetail: (
txn_result: SqlTxnResult<TableSchemaDetailsRow>,
resp: TableDetailsResponse,
) => {
if (!txnResultIsEmpty(txn_result)) {
resp.schema_details.columns = txn_result.rows[0].columns;
resp.schema_details.indexes = txn_result.rows[0].indexes;
}
if (txn_result.error) {
resp.schema_details.error = txn_result.error;
}
},
};
// Table zone config.
type TableZoneConfigResponse = {
configure_zone_statement: string;
zone_config: ZoneConfigType;
zone_config_level: ZoneConfigLevelType;
error?: Error;
};
type TableZoneConfigStatementRow = { raw_config_sql: string };
const getTableZoneConfigStmt: TableDetailsQuery<TableZoneConfigStatementRow> = {
createStmt: (dbName, tableName) => {
const escFullTableName = Join(
[new Identifier(dbName).SQLString(), tableName],
new SQL("."),
);
return {
sql: Format(
`SELECT raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR TABLE %1]`,
[escFullTableName],
),
};
},
addToTableDetail: (
txn_result: SqlTxnResult<TableZoneConfigStatementRow>,
resp: TableDetailsResponse,
) => {
if (!txnResultIsEmpty(txn_result)) {
resp.zone_config_resp.configure_zone_statement =
txn_result.rows[0].raw_config_sql;
}
if (txn_result.error) {
resp.id_resp.error = txn_result.error;
}
},
};
type TableZoneConfigRow = {
database_zone_config_hex_string: string;
table_zone_config_hex_string: string;
};
const getTableZoneConfig: TableDetailsQuery<TableZoneConfigRow> = {
createStmt: (dbName, tableName) => {
const escFullTableName = Join(
[new Identifier(dbName).SQLString(), tableName],
new SQL("."),
);
return {
sql: `SELECT
encode(crdb_internal.get_zone_config((SELECT crdb_internal.get_database_id($1))), 'hex') as database_zone_config_hex_string,
encode(crdb_internal.get_zone_config((SELECT $2::regclass::int)), 'hex') as table_zone_config_hex_string`,
arguments: [dbName, escFullTableName.SQLString()],
};
},
addToTableDetail: (
txn_result: SqlTxnResult<TableZoneConfigRow>,
resp: TableDetailsResponse,
) => {
if (!txnResultIsEmpty(txn_result)) {
let hexString = "";
let configLevel = ZoneConfigurationLevel.CLUSTER;
const row = txn_result.rows[0];
// Check that database_zone_config_bytes is not null
// and not empty.
if (
row.database_zone_config_hex_string &&
row.database_zone_config_hex_string.length !== 0
) {
hexString = row.database_zone_config_hex_string;
configLevel = ZoneConfigurationLevel.DATABASE;
}
// Fall back to table_zone_config_bytes if we don't have database_zone_config_bytes.
if (
hexString === "" &&
row.table_zone_config_hex_string &&
row.table_zone_config_hex_string.length !== 0
) {
hexString = row.table_zone_config_hex_string;
configLevel = ZoneConfigurationLevel.TABLE;
}
// Try to decode the zone config bytes response.
try {
// Parse the bytes from the hex string.
const zoneConfigBytes = fromHexString(hexString);
// Decode the bytes using ZoneConfig protobuf.
resp.zone_config_resp.zone_config = ZoneConfig.decode(
new Uint8Array(zoneConfigBytes),
);
resp.zone_config_resp.zone_config_level = configLevel;
} catch (e) {
console.error(
`Table Details API - encountered an error decoding zone config string: ${hexString}`,
);
// Catch and assign the error if we encounter one decoding.
resp.zone_config_resp.error = e;
resp.zone_config_resp.zone_config_level =
ZoneConfigurationLevel.UNKNOWN;
}
}
if (txn_result.error) {
resp.id_resp.error = txn_result.error;
}
},
};
// Table heuristics details.
type TableHeuristicDetailsRow = {
stats_last_created_at: moment.Moment;
};
const getTableHeuristicsDetails: TableDetailsQuery<TableHeuristicDetailsRow> = {
createStmt: (dbName, tableName) => {
const escFullTableName = Join(
[new Identifier(dbName).SQLString(), tableName],
new SQL("."),
);
return {
sql: Format(
`SELECT max(created) AS created FROM [SHOW STATISTICS FOR TABLE %1]`,
[escFullTableName],
),
};
},
addToTableDetail: (
txn_result: SqlTxnResult<TableHeuristicDetailsRow>,
resp: TableDetailsResponse,
) => {
if (!txnResultIsEmpty(txn_result)) {
resp.heuristics_details.stats_last_created_at =
txn_result.rows[0].stats_last_created_at;
}
if (txn_result.error) {
resp.schema_details.error = txn_result.error;
}
},
};
// Table details stats.
type TableDetailsStats = {
ranges_data: TableRangesData;
pebble_data: TablePebbleData;
index_stats: TableIndexUsageStats;
};
type TablePebbleData = {
approximate_disk_bytes: number;
};
type TableRangesData = SqlApiQueryResponse<{
range_count: number;
live_bytes: number;
total_bytes: number;
live_percentage: number;
node_ids: number[];
node_count: number;
replica_count: number;
error?: Error;
}>;
// Table span stats.
type TableSpanStatsRow = {
approximate_disk_bytes: number;
live_bytes: number;
total_bytes: number;
range_count: number;
live_percentage: number;
};
const getTableSpanStats: TableDetailsQuery<TableSpanStatsRow> = {
createStmt: (dbName, tableName) => {
const escFullTableName = Join(
[new Identifier(dbName).SQLString(), tableName],
new SQL("."),
);
return {
sql: `SELECT
range_count,
approximate_disk_bytes,
live_bytes,
total_bytes,
live_percentage
FROM crdb_internal.tenant_span_stats(
(SELECT crdb_internal.get_database_id($1)),
(SELECT $2::regclass::int))`,
arguments: [dbName, escFullTableName.SQLString()],
};
},
addToTableDetail: (
txn_result: SqlTxnResult<TableSpanStatsRow>,
resp: TableDetailsResponse,
) => {
if (txn_result && txn_result.error) {
resp.stats.ranges_data.error = txn_result.error;
}
if (txnResultIsEmpty(txn_result)) {
return;
}
if (txn_result.rows.length === 1) {
const row = txn_result.rows[0];
resp.stats.pebble_data.approximate_disk_bytes =
row.approximate_disk_bytes;
resp.stats.ranges_data.range_count = row.range_count;
resp.stats.ranges_data.live_bytes = row.live_bytes;
resp.stats.ranges_data.total_bytes = row.total_bytes;
resp.stats.ranges_data.live_percentage = row.live_percentage;
} else {
txn_result.error = new Error(
"getTableSpanStats: unexpected number of rows (expected 1)",
);
}
if (txn_result.error) {
resp.id_resp.error = txn_result.error;
}
},
};
// Table replicas.
type TableReplicasRow = {
replicas: number[];
};
const getTableReplicas: TableDetailsQuery<TableReplicasRow> = {
createStmt: (dbName, tableName) => {
const escFullTableName = Join(
[new Identifier(dbName).SQLString(), tableName],
new SQL("."),
);
return {
sql: Format(
`WITH tableId AS (SELECT $1::regclass::int as table_id)
SELECT
r.replicas
FROM %1.crdb_internal.table_spans as ts
JOIN tableId ON ts.descriptor_id = tableId.table_id
JOIN crdb_internal.ranges_no_leases as r ON ts.start_key < r.end_key AND ts.end_key > r.start_key`,
[new Identifier(dbName)],
),
arguments: [escFullTableName.SQLString()],
};
},
addToTableDetail: (
txn_result: SqlTxnResult<TableReplicasRow>,
resp: TableDetailsResponse,
) => {
// Build set of unique replicas for this database.
const replicas = new Set<number>();
// If rows are not null or empty...
if (!txnResultIsEmpty(txn_result)) {
txn_result.rows.forEach(row => {
row.replicas.forEach(replicas.add, replicas);
});
// Currently we use replica ids as node ids.
resp.stats.ranges_data.replica_count = replicas.size;
resp.stats.ranges_data.node_count = replicas.size;
resp.stats.ranges_data.node_ids = Array.from(replicas.values());
}
if (txn_result.error) {
resp.stats.ranges_data.error = txn_result.error;
}
},
};
// Table index usage stats.
type TableIndexUsageStats = SqlApiQueryResponse<{
has_index_recommendations: boolean;
error?: Error;
}>;
const getTableIndexUsageStats: TableDetailsQuery<IndexUsageStatistic> = {
createStmt: (dbName, tableName) => {
const escFullTableName = Join(
[new Identifier(dbName).SQLString(), tableName],
new SQL("."),
);
return {
sql: Format(
`WITH
cs AS (SELECT value FROM crdb_internal.cluster_settings WHERE variable = 'sql.index_recommendation.drop_unused_duration'),
tableId AS (SELECT $1::regclass::int as table_id)
SELECT * FROM (SELECT
ti.created_at,
us.last_read,
us.total_reads,
cs.value as unused_threshold,
cs.value::interval as interval_threshold,
now() - COALESCE(us.last_read AT TIME ZONE 'UTC', COALESCE(ti.created_at, '0001-01-01')) as unused_interval
FROM %1.crdb_internal.index_usage_statistics AS us
JOIN tableId ON us.table_id = tableId.table_id
JOIN %1.crdb_internal.table_indexes AS ti ON (
us.index_id = ti.index_id AND
tableId.table_id = ti.descriptor_id AND
ti.index_type = 'secondary'
)
CROSS JOIN cs
WHERE $2 != 'system')
WHERE unused_interval > interval_threshold
ORDER BY total_reads DESC`,
[new Identifier(dbName)],
),
arguments: [escFullTableName.SQLString(), dbName],
};
},
addToTableDetail: (
txn_result: SqlTxnResult<IndexUsageStatistic>,
resp: TableDetailsResponse,
) => {
resp.stats.index_stats.has_index_recommendations = txn_result.rows?.some(
row => recommendDropUnusedIndex(row),
);
if (txn_result.error) {
resp.stats.index_stats.error = txn_result.error;
}
},
};
type TableDetailsQuery<RowType> = {
createStmt: (dbName: string, tableName: string) => SqlStatement;
addToTableDetail: (
response: SqlTxnResult<RowType>,
tableDetail: TableDetailsResponse,
) => void;
};
export type TableDetailsRow =
| TableIdRow
| TableGrantsRow
| TableSchemaDetailsRow
| TableCreateStatementRow
| TableZoneConfigStatementRow
| TableHeuristicDetailsRow
| TableSpanStatsRow
| IndexUsageStatistic
| TableZoneConfigRow
| TableReplicasRow;
const tableDetailQueries: TableDetailsQuery<TableDetailsRow>[] = [
getTableId,
getTableGrants,
getTableSchemaDetails,
getTableCreateStatement,
getTableZoneConfigStmt,
getTableHeuristicsDetails,
getTableSpanStats,
getTableIndexUsageStats,
getTableZoneConfig,
getTableReplicas,
];
export function createTableDetailsReq(
dbName: string,
tableName: string,
): SqlExecutionRequest {
return {
execute: true,
statements: tableDetailQueries.map(query =>
query.createStmt(dbName, tableName),
),
max_result_size: LARGE_RESULT_SIZE,
timeout: LONG_TIMEOUT,
database: dbName,
};
}
export type TableDetailsReqParams = {
database: string;
// Note: table name is expected in the following format: "schemaName"."tableName"
table: string;
};
export async function getTableDetails(
params: TableDetailsReqParams,
timeout?: moment.Duration,
): Promise<SqlApiResponse<TableDetailsResponse>> {
return withTimeout(fetchTableDetails(params.database, params.table), timeout);
}
async function fetchTableDetails(
databaseName: string,
tableName: string,
): Promise<SqlApiResponse<TableDetailsResponse>> {
const detailsResponse: TableDetailsResponse = newTableDetailsResponse();
const req: SqlExecutionRequest = createTableDetailsReq(
databaseName,
tableName,
);
const resp = await executeInternalSql<TableDetailsRow>(req);
resp.execution.txn_results.forEach(txn_result => {
if (txn_result.rows) {
const query: TableDetailsQuery<TableDetailsRow> =
tableDetailQueries[txn_result.statement - 1];
query.addToTableDetail(txn_result, detailsResponse);
}
});
if (resp.error) {
detailsResponse.error = resp.error;
}
return formatApiResult<TableDetailsResponse>(
detailsResponse,
detailsResponse.error,
"retrieving table details information",
);
}