Skip to content

Commit fea6fe5

Browse files
craig[bot]golgeekOriSavir
committed
148008: roachprod: improve instances filtering when listing on IBM r=DarrylWong a=golgeek Cloud instance listing on IBM was previously filtering out VMs that lacked the `roachprod` tag. However, the IBM API occasionally returns a successful response for tagging requests without actually acting on it. This inconsistency resulted in some VMs being skipped in the garbage colelction process because lacking the required tags. This PR removes the dependency on the `roachprod` tag, and all resources within the `roachprod` resource group are now considered game for garbage collection. Epic: none Release note: None 148217: sql: Index routine create virtual tables by routine IDs. r=OriSavir a=OriSavir Previously, the internal routine virtual tables, `crdb_internal.create_function_statements` and `crdb_internal.create_procedure_statements` were not indexed. As such, in the functions to query for a routine create statement by the routine ID, which are necessary for `SHOW CREATE ALL ROUTINES`, the query was on a virtual table that was not indexed and, thus, inefficient. This commit addresses that by adding a function that can be used to build rows of the virtual tables directly from the routine IDs and, thus, can act as a populate function for virtual indexes. This function was added onto the virtual tables and now the query is more efficient. Epic: CRDB-49582 Release note: None Co-authored-by: Ludovic Leroux <[email protected]> Co-authored-by: Oriel Savir <[email protected]>
3 parents 75e7f32 + 6c401b8 + 61ca525 commit fea6fe5

File tree

7 files changed

+114
-24
lines changed

7 files changed

+114
-24
lines changed

pkg/bench/rttanalysis/orm_queries_bench_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,14 @@ func buildNTables(n int) string {
10271027
return b.String()
10281028
}
10291029

1030+
func buildNFunctions(n int) string {
1031+
b := strings.Builder{}
1032+
for i := 0; i < n; i++ {
1033+
b.WriteString(fmt.Sprintf("CREATE FUNCTION fn%d() RETURNS int AS 'SELECT 1' LANGUAGE SQL;\n", i))
1034+
}
1035+
return b.String()
1036+
}
1037+
10301038
func buildNTypes(n int) string {
10311039
b := strings.Builder{}
10321040
for i := 0; i < n; i++ {

pkg/bench/rttanalysis/testdata/benchmark_expectations

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,6 @@ exp,benchmark
131131
2,UseManyRoles/use_50_roles
132132
1,VirtualTableQueries/select_crdb_internal.invalid_objects_with_1_fk
133133
1,VirtualTableQueries/select_crdb_internal.tables_with_1_fk
134+
0,VirtualTableQueries/show_create_all_routines
134135
5,VirtualTableQueries/virtual_table_cache_with_point_lookups
135136
10,VirtualTableQueries/virtual_table_cache_with_schema_change

pkg/bench/rttanalysis/virtual_table_bench_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,12 @@ SELECT * FROM crdb_internal.tables;
5959
SELECT * FROM t1;
6060
SELECT * FROM t2;`,
6161
},
62+
// This test checks the performance of the crdb_internal virtual tables used by
63+
// SHOW CREATE ALL ROUTINES.
64+
{
65+
Name: "show_create_all_routines",
66+
Setup: buildNFunctions(100),
67+
Stmt: `SHOW CREATE ALL ROUTINES`,
68+
},
6269
})
6370
}

pkg/roachprod/vm/ibm/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ func (p *Provider) listRegion(l *logger.Logger, r string, opts vm.ListOptions) (
833833
}
834834
}
835835

836-
valid, reason := i.isRoachprodAndValidStatus()
836+
valid, reason := i.isValidStatus()
837837
if !valid {
838838
l.Printf("WARN: discarding instance %s in region %s (%s)", *i.instance.CRN, r, reason)
839839
continue

pkg/roachprod/vm/ibm/ibm_extended_types.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -485,25 +485,11 @@ func (i *instance) getPrimaryNetworkInterface() (*vpcv1NetworkInterface, error)
485485
return i.networkInterface, nil
486486
}
487487

488-
// isRoachprodAndValidStatus checks if a roahcprod instance in valid status.
489-
func (i *instance) isRoachprodAndValidStatus() (bool, string) {
488+
// isValidStatus checks if an instance has a valid status.
489+
func (i *instance) isValidStatus() (bool, string) {
490490
if i == nil || i.instance == nil || i.instance.Status == nil {
491491
return false, InstanceNotInitialized
492492
}
493-
494-
tags, err := i.getTagsAsMap()
495-
if err != nil {
496-
return false, InstanceInvalidTags
497-
}
498-
499-
if tags[vm.TagRoachprod] != "true" {
500-
return false, InstanceNotRochprod
501-
}
502-
503-
// if *i.instance.Status != "pending" && *i.instance.Status != "running" {
504-
// return false, InstanceInvalidStatus
505-
// }
506-
507493
return true, ""
508494
}
509495

@@ -585,6 +571,11 @@ func (i *instance) toVM() vm.VM {
585571
vpcID = i.provider.config.regions[region].vpcID
586572
}
587573

574+
// Check if the instance is in a valid state.
575+
if core.StringNilMapper(i.instance.Status) == "failed" {
576+
vmErrors = append(vmErrors, errors.New("instance is in failed state"))
577+
}
578+
588579
// Gather tags
589580
tags, err := i.getTagsAsMap()
590581
if err != nil {

pkg/sql/crdb_internal.go

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3912,6 +3912,79 @@ func createRoutinePopulate(
39123912
}
39133913
}
39143914

3915+
func createRoutinePopulateByFnIndex(
3916+
ctx context.Context,
3917+
unwrappedConstraint tree.Datum,
3918+
p *planner,
3919+
db catalog.DatabaseDescriptor,
3920+
addRow func(...tree.Datum) error,
3921+
) (matched bool, err error) {
3922+
// In here, we start from the DatabaseDescriptor and a routine ID and populate the remainder of a row
3923+
// in the virtual table. This function, thus, is used to populate when indexing into the routine virtual tables,
3924+
// `crdb_internal.create_function_statements` and `crdb_internal.create_procedure_statements`, helping
3925+
// optimize the queries for the create statements output in `SHOW CREATE ALL ROUTINES`.
3926+
fnID := descpb.ID(tree.MustBeDInt(unwrappedConstraint))
3927+
fnDesc, err := p.Descriptors().ByIDWithoutLeased(p.txn).WithoutNonPublic().Get().Function(ctx, fnID)
3928+
if err != nil || fnDesc == nil {
3929+
if errors.Is(err, catalog.ErrDescriptorNotFound) || fnDesc == nil {
3930+
return false, nil
3931+
}
3932+
return false, err
3933+
}
3934+
scID := fnDesc.GetParentSchemaID()
3935+
sc, err := p.Descriptors().ByIDWithoutLeased(p.txn).WithoutNonPublic().Get().Schema(ctx, scID)
3936+
if err != nil || sc == nil {
3937+
return false, err
3938+
}
3939+
scName := sc.GetName()
3940+
dbName := db.GetName()
3941+
dbID := db.GetID()
3942+
3943+
treeNode, err := fnDesc.ToCreateExpr()
3944+
treeNode.Name.ObjectNamePrefix = tree.ObjectNamePrefix{
3945+
ExplicitSchema: true,
3946+
SchemaName: tree.Name(scName),
3947+
}
3948+
if err != nil {
3949+
return false, err
3950+
}
3951+
for i := range treeNode.Options {
3952+
if body, ok := treeNode.Options[i].(tree.RoutineBodyStr); ok {
3953+
bodyStr := string(body)
3954+
bodyStr, err = formatFunctionQueryTypesForDisplay(ctx, p.EvalContext(), &p.semaCtx, p.SessionData(), bodyStr, fnDesc.GetLanguage())
3955+
if err != nil {
3956+
return false, err
3957+
}
3958+
bodyStr, err = formatQuerySequencesForDisplay(ctx, &p.semaCtx, bodyStr, true /* multiStmt */, fnDesc.GetLanguage())
3959+
if err != nil {
3960+
return false, err
3961+
}
3962+
bodyStr = strings.TrimSpace(bodyStr)
3963+
stmtStrs := strings.Split(bodyStr, "\n")
3964+
for i := range stmtStrs {
3965+
if stmtStrs[i] != "" {
3966+
stmtStrs[i] = "\t" + stmtStrs[i]
3967+
}
3968+
}
3969+
p := &treeNode.Options[i]
3970+
*p = tree.RoutineBodyStr("\n" + strings.Join(stmtStrs, "\n") + "\n")
3971+
}
3972+
}
3973+
createStatement := tree.AsString(treeNode)
3974+
if err := addRow(
3975+
tree.NewDInt(tree.DInt(dbID)), // database_id
3976+
tree.NewDString(dbName), // database_name
3977+
tree.NewDInt(tree.DInt(scID)), // schema_id
3978+
tree.NewDString(scName), // schema_name
3979+
tree.NewDInt(tree.DInt(fnDesc.GetID())), // function_id
3980+
tree.NewDString(fnDesc.GetName()), // function_name
3981+
tree.NewDString(createStatement), // create_statement
3982+
); err != nil {
3983+
return false, err
3984+
}
3985+
return true, nil
3986+
}
3987+
39153988
var crdbInternalCreateFunctionStmtsTable = virtualSchemaTable{
39163989
comment: "CREATE statements for all user-defined functions",
39173990
schema: `
@@ -3922,10 +3995,15 @@ CREATE TABLE crdb_internal.create_function_statements (
39223995
schema_name STRING,
39233996
function_id INT,
39243997
function_name STRING,
3925-
create_statement STRING
3926-
)
3927-
`,
3998+
create_statement STRING,
3999+
INDEX (function_id)
4000+
)`,
39284001
populate: createRoutinePopulate(false /* procedure */),
4002+
indexes: []virtualIndex{
4003+
{
4004+
populate: createRoutinePopulateByFnIndex,
4005+
},
4006+
},
39294007
}
39304008

39314009
var crdbInternalCreateProcedureStmtsTable = virtualSchemaTable{
@@ -3938,10 +4016,15 @@ CREATE TABLE crdb_internal.create_procedure_statements (
39384016
schema_name STRING,
39394017
procedure_id INT,
39404018
procedure_name STRING,
3941-
create_statement STRING
3942-
)
3943-
`,
4019+
create_statement STRING,
4020+
INDEX (procedure_id)
4021+
)`,
39444022
populate: createRoutinePopulate(true /* procedure */),
4023+
indexes: []virtualIndex{
4024+
{
4025+
populate: createRoutinePopulateByFnIndex,
4026+
},
4027+
},
39454028
}
39464029

39474030
func createTriggerPopulate(

pkg/sql/opt/exec/execbuilder/testdata/explain_analyze

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ quality of service: regular
176176
│ │ └── • virtual table
177177
│ │ sql nodes: <hidden>
178178
│ │ regions: <hidden>
179-
│ │ actual row count: 364
179+
│ │ actual row count: 366
180180
│ │ execution time: 0µs
181181
│ │ table: pg_class@primary
182182
│ │

0 commit comments

Comments
 (0)