Skip to content

Commit 8ca9b05

Browse files
craig[bot]qiyanghe1998Renato CostamsbutlerYevgeniy Miretskiy
committed
106525: sql: add trie tree based workload index recommendations r=qiyanghe1998 a=qiyanghe1998 #### sql: add trie tree based workload index recommendations This commit adds the trie and the logic for getting the workload index recommendations. In addition, it fills the gap between built-in functions and backend implementation for workload index recommendations. The whole process consists of collecting candidates and finding representative indexes. All the index recommendations in the table `system.statement_statistics` (satisfying some time requirement) will be collected as the candidates and then inserted to the trie. The trie is designed for all the indexes of one table. The indexed columns will be regarded as the key to insert into the tree in their original orders. The storing part will be attached to the node after the insertion of indexed columns. The general idea of finding representative indexes is to use all the indexes represented by the leaf nodes. One optimization is to use the remove the storings that are covered by some leaf nodes. Next, we will push down all the storings attached to the internal nodes to the shallowest leaf nodes (You can find the reasons in RFC). Finally, all the indexes represented by the leaf nodes will be returned. As for the `DROP INDEX`, since we collect all the indexes represented by the leaf nodes (a superset of dropped indexes), so we can directly drop all of them. Release note (sql change): new builtin functions `workload_index_recs()` and `workload_index_recs(timestamptz)`, return workload level index recommendations (columns of string, each string represent an index recommendation) from statement level index recommendations (as candidates) in `system.statement_statistics`. If the timestamptz is given, it will only consider those candidates who is generated after that timestampsz. Epic: None 107743: sql: Add new builtin generator function `crdb_internal.scan_storage_internal_keys()` r=RahulAggarwal1016 a=RahulAggarwal1016 This new builtin is used to gather specific pebble metrics for a node and store id (within an given keyspan). The builtin returns information about the different types of keys (including snapshot pinned keys) and bytes. Informs: cockroachdb#94659 Release-note: None 108516: backupccl: fix error message for descriptor version mismatch r=adityamaru a=renatolabs Expected and actual versions were swapped. Epic: none Release note: None 108520: backupccl: remove spurious print line in test r=msbutler a=msbutler Print statements should not be in commited code Epic: none Release note: none 108531: roachtest: Ensure tpcc workloads runs for a bit r=miretskiy a=miretskiy An issue in roachtest cockroachdb#108530 prevents clean test termination when calling Wait() on a test monitor that did not have at least 1 task started. This cause `cdc/kafka-oauth` test to hang. Add a '30s' duration to the tpcc task to go around this problem. Fixes cockroachdb#108507 Release note: None Co-authored-by: qiyanghe1998 <[email protected]> Co-authored-by: craig[bot] <[email protected]> Co-authored-by: Renato Costa <[email protected]> Co-authored-by: Michael Butler <[email protected]> Co-authored-by: Yevgeniy Miretskiy <[email protected]>
6 parents e3efe11 + 0f2b7e6 + 08dbd7e + 50c308c + 02c51b2 + 496948e commit 8ca9b05

File tree

25 files changed

+1563
-71
lines changed

25 files changed

+1563
-71
lines changed

docs/generated/sql/functions.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,11 +1386,7 @@ the locality flag on node startup. Returns an error if no region is set.</p>
13861386
</span></td><td>Immutable</td></tr>
13871387
<tr><td><a name="workload_index_recs"></a><code>workload_index_recs() &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns set of index recommendations</p>
13881388
</span></td><td>Immutable</td></tr>
1389-
<tr><td><a name="workload_index_recs"></a><code>workload_index_recs(budget: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns set of index recommendations</p>
1390-
</span></td><td>Immutable</td></tr>
13911389
<tr><td><a name="workload_index_recs"></a><code>workload_index_recs(timestamptz: <a href="timestamp.html">timestamptz</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns set of index recommendations</p>
1392-
</span></td><td>Immutable</td></tr>
1393-
<tr><td><a name="workload_index_recs"></a><code>workload_index_recs(timestamptz: <a href="timestamp.html">timestamptz</a>, budget: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns set of index recommendations</p>
13941390
</span></td><td>Immutable</td></tr></tbody>
13951391
</table>
13961392

pkg/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ ALL_TESTS = [
486486
"//pkg/sql/opt/testutils/opttester:opttester_test",
487487
"//pkg/sql/opt/testutils/testcat:testcat_test",
488488
"//pkg/sql/opt/testutils:testutils_test",
489+
"//pkg/sql/opt/workloadindexrec:workloadindexrec_test",
489490
"//pkg/sql/opt/xform:xform_test",
490491
"//pkg/sql/opt:opt_test",
491492
"//pkg/sql/parser:parser_disallowed_imports_test",
@@ -1892,6 +1893,8 @@ GO_TARGETS = [
18921893
"//pkg/sql/opt/testutils/testexpr:testexpr",
18931894
"//pkg/sql/opt/testutils:testutils",
18941895
"//pkg/sql/opt/testutils:testutils_test",
1896+
"//pkg/sql/opt/workloadindexrec:workloadindexrec",
1897+
"//pkg/sql/opt/workloadindexrec:workloadindexrec_test",
18951898
"//pkg/sql/opt/xform:xform",
18961899
"//pkg/sql/opt/xform:xform_test",
18971900
"//pkg/sql/opt:opt",

pkg/ccl/backupccl/datadriven_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,6 @@ func runTestDataDriven(t *testing.T, testFilePathFromWorkspace string) {
848848
err = ds.getSQLDB(t, cluster, user).QueryRow(filePathQuery).Scan(&filePath)
849849
require.NoError(t, err)
850850
fullPath := filepath.Join(ds.getIODir(t, cluster), parsedURI.Path, filePath)
851-
print(fullPath)
852851
data, err := os.ReadFile(fullPath)
853852
require.NoError(t, err)
854853
data[20] ^= 1

pkg/ccl/backupccl/restore_job.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2375,7 +2375,7 @@ func prefetchDescriptors(
23752375
if got[i].GetVersion() != expVersion[id] {
23762376
return nstree.Catalog{}, errors.Errorf(
23772377
"version mismatch for descriptor %d, expected version %d, got %v",
2378-
got[i].GetID(), got[i].GetVersion(), expVersion[id],
2378+
got[i].GetID(), expVersion[id], got[i].GetVersion(),
23792379
)
23802380
}
23812381
all.UpsertDescriptor(got[i])

pkg/cmd/roachtest/tests/cdc.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,10 @@ func registerCDC(r registry.Registry) {
13271327
ct := newCDCTester(ctx, t, c)
13281328
defer ct.Close()
13291329

1330-
ct.runTPCCWorkload(tpccArgs{warehouses: 1})
1330+
// Run tpcc workload for tiny bit. Roachtest monitor does not
1331+
// like when there are no tasks that were started with the monitor
1332+
// (This can be removed once #108530 resolved).
1333+
ct.runTPCCWorkload(tpccArgs{warehouses: 1, duration: "30s"})
13311334

13321335
kafkaNode := ct.kafkaSinkNode()
13331336
kafka := kafkaManager{

pkg/kv/kvserver/api.proto

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ message GetTableMetricsResponse{
8989
repeated storage.enginepb.SSTableMetricsInfo table_metrics = 1 [(gogoproto.nullable) = false, (gogoproto.customname) = "TableMetrics"];
9090
}
9191

92+
// ScanStorageInternalKeysRequest retrieves metrics about keys within a range belonging
93+
// to a particular node and store.
94+
message ScanStorageInternalKeysRequest {
95+
StoreRequestHeader header = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
96+
roachpb.Span span = 2 [(gogoproto.nullable) = false];
97+
int64 MegabytesPerSecond = 3;
98+
}
99+
100+
message ScanStorageInternalKeysResponse {
101+
repeated storage.enginepb.StorageInternalKeysMetrics advanced_metrics = 1 [(gogoproto.nullable) = false, (gogoproto.customname) = "AdvancedPebbleMetrics"];
102+
}
103+
92104
message CompactEngineSpanResponse {
93105
}
94106

pkg/kv/kvserver/storage_engine_client.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,33 @@ func (c *StorageEngineClient) GetTableMetrics(
7676
return resp.TableMetrics, nil
7777
}
7878

79+
// ScanStorageInternalKeys is a tree.ScanStorageInternalKeys
80+
func (c *StorageEngineClient) ScanStorageInternalKeys(
81+
ctx context.Context, nodeID, storeID int32, startKey, endKey []byte, megabytesPerSecond int64,
82+
) ([]enginepb.StorageInternalKeysMetrics, error) {
83+
conn, err := c.nd.Dial(ctx, roachpb.NodeID(nodeID), rpc.DefaultClass)
84+
if err != nil {
85+
return []enginepb.StorageInternalKeysMetrics{}, errors.Wrapf(err, "could not dial node ID %d", nodeID)
86+
}
87+
88+
client := NewPerStoreClient(conn)
89+
req := &ScanStorageInternalKeysRequest{
90+
StoreRequestHeader: StoreRequestHeader{
91+
NodeID: roachpb.NodeID(nodeID),
92+
StoreID: roachpb.StoreID(storeID),
93+
},
94+
Span: roachpb.Span{Key: roachpb.Key(startKey), EndKey: roachpb.Key(endKey)},
95+
MegabytesPerSecond: megabytesPerSecond,
96+
}
97+
98+
resp, err := client.ScanStorageInternalKeys(ctx, req)
99+
100+
if err != nil {
101+
return []enginepb.StorageInternalKeysMetrics{}, err
102+
}
103+
return resp.AdvancedPebbleMetrics, nil
104+
}
105+
79106
// SetCompactionConcurrency is a tree.CompactionConcurrencyFunc.
80107
func (c *StorageEngineClient) SetCompactionConcurrency(
81108
ctx context.Context, nodeID, storeID int32, compactionConcurrency uint64,

pkg/kv/kvserver/storage_services.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ service PerReplica {
4444
service PerStore {
4545
rpc CompactEngineSpan(cockroach.kv.kvserver.CompactEngineSpanRequest) returns (cockroach.kv.kvserver.CompactEngineSpanResponse) {}
4646
rpc GetTableMetrics(cockroach.kv.kvserver.GetTableMetricsRequest) returns (cockroach.kv.kvserver.GetTableMetricsResponse) {}
47+
rpc ScanStorageInternalKeys(cockroach.kv.kvserver.ScanStorageInternalKeysRequest) returns (cockroach.kv.kvserver.ScanStorageInternalKeysResponse) {}
4748
rpc SetCompactionConcurrency(cockroach.kv.kvserver.CompactionConcurrencyRequest) returns (cockroach.kv.kvserver.CompactionConcurrencyResponse) {}
4849
}

pkg/kv/kvserver/stores_server.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,24 @@ func (is Server) GetTableMetrics(
173173
return resp, err
174174
}
175175

176+
func (is Server) ScanStorageInternalKeys(
177+
ctx context.Context, req *ScanStorageInternalKeysRequest,
178+
) (*ScanStorageInternalKeysResponse, error) {
179+
resp := &ScanStorageInternalKeysResponse{}
180+
err := is.execStoreCommand(ctx, req.StoreRequestHeader,
181+
func(ctx context.Context, s *Store) error {
182+
metrics, err := s.TODOEngine().ScanStorageInternalKeys(req.Span.Key, req.Span.EndKey, req.MegabytesPerSecond)
183+
184+
if err != nil {
185+
return err
186+
}
187+
188+
resp.AdvancedPebbleMetrics = metrics
189+
return nil
190+
})
191+
return resp, err
192+
}
193+
176194
// SetCompactionConcurrency implements PerStoreServer. It changes the compaction
177195
// concurrency of a store. While SetCompactionConcurrency is safe for concurrent
178196
// use, it adds uncertainty about the compaction concurrency actually set on

pkg/server/server_sql.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -961,16 +961,17 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) {
961961
ClientCertExpirationCache: security.NewClientCertExpirationCache(
962962
ctx, cfg.Settings, cfg.stopper, &timeutil.DefaultTimeSource{}, rootSQLMemoryMonitor,
963963
),
964-
RootMemoryMonitor: rootSQLMemoryMonitor,
965-
TestingKnobs: sqlExecutorTestingKnobs,
966-
CompactEngineSpanFunc: storageEngineClient.CompactEngineSpan,
967-
CompactionConcurrencyFunc: storageEngineClient.SetCompactionConcurrency,
968-
GetTableMetricsFunc: storageEngineClient.GetTableMetrics,
969-
TraceCollector: traceCollector,
970-
TenantUsageServer: cfg.tenantUsageServer,
971-
KVStoresIterator: cfg.kvStoresIterator,
972-
InspectzServer: cfg.inspectzServer,
973-
RangeDescIteratorFactory: cfg.rangeDescIteratorFactory,
964+
RootMemoryMonitor: rootSQLMemoryMonitor,
965+
TestingKnobs: sqlExecutorTestingKnobs,
966+
CompactEngineSpanFunc: storageEngineClient.CompactEngineSpan,
967+
CompactionConcurrencyFunc: storageEngineClient.SetCompactionConcurrency,
968+
GetTableMetricsFunc: storageEngineClient.GetTableMetrics,
969+
ScanStorageInternalKeysFunc: storageEngineClient.ScanStorageInternalKeys,
970+
TraceCollector: traceCollector,
971+
TenantUsageServer: cfg.tenantUsageServer,
972+
KVStoresIterator: cfg.kvStoresIterator,
973+
InspectzServer: cfg.inspectzServer,
974+
RangeDescIteratorFactory: cfg.rangeDescIteratorFactory,
974975
SyntheticPrivilegeCache: syntheticprivilegecache.New(
975976
cfg.Settings, cfg.stopper, cfg.db,
976977
serverCacheMemoryMonitor.MakeBoundAccount(),

0 commit comments

Comments
 (0)