Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-24.1: sql: add gauge for object count #138839

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generated/metrics/metrics.html
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,7 @@
<tr><td>APPLICATION</td><td>sql.savepoint.started.count</td><td>Number of SQL SAVEPOINT statements started</td><td>SQL Statements</td><td>COUNTER</td><td>COUNT</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
<tr><td>APPLICATION</td><td>sql.savepoint.started.count.internal</td><td>Number of SQL SAVEPOINT statements started (internal queries)</td><td>SQL Internal Statements</td><td>COUNTER</td><td>COUNT</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
<tr><td>APPLICATION</td><td>sql.schema.invalid_objects</td><td>Gauge of detected invalid objects within the system.descriptor table (measured by querying crdb_internal.invalid_objects)</td><td>Objects</td><td>GAUGE</td><td>COUNT</td><td>AVG</td><td>NONE</td></tr>
<tr><td>APPLICATION</td><td>sql.schema_changer.object_count</td><td>Counter of the number of objects in the cluster</td><td>Objects</td><td>GAUGE</td><td>COUNT</td><td>AVG</td><td>NONE</td></tr>
<tr><td>APPLICATION</td><td>sql.schema_changer.permanent_errors</td><td>Counter of the number of permanent errors experienced by the schema changer</td><td>Errors</td><td>COUNTER</td><td>COUNT</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
<tr><td>APPLICATION</td><td>sql.schema_changer.retry_errors</td><td>Counter of the number of retriable errors experienced by the schema changer</td><td>Errors</td><td>COUNTER</td><td>COUNT</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
<tr><td>APPLICATION</td><td>sql.schema_changer.running</td><td>Gauge of currently running schema changes</td><td>Schema changes</td><td>GAUGE</td><td>COUNT</td><td>AVG</td><td>NONE</td></tr>
Expand Down
17 changes: 17 additions & 0 deletions pkg/sql/catalog/descs/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ func (tc *Collection) HasUncommittedDescriptors() bool {
return tc.uncommitted.uncommitted.Len() > 0
}

// HasUncommittedNewOrDroppedDescriptors returns true if the collection contains
// any uncommitted descriptors that are newly created or dropped.
func (tc *Collection) HasUncommittedNewOrDroppedDescriptors() bool {
isNewDescriptor := false
err := tc.uncommitted.iterateUncommittedByID(func(desc catalog.Descriptor) error {
if desc.GetVersion() == 1 || desc.Dropped() {
isNewDescriptor = true
return iterutil.StopIteration()
}
return nil
})
if err != nil {
return false
}
return isNewDescriptor
}

// HasUncommittedTypes returns true if the Collection contains uncommitted
// types.
func (tc *Collection) HasUncommittedTypes() (has bool) {
Expand Down
7 changes: 6 additions & 1 deletion pkg/sql/conn_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4028,7 +4028,12 @@ func (ex *connExecutor) txnStateTransitionsApplyWrapper(
}(); err != nil {
return ai, err
}

if ex.extraTxnState.descCollection.HasUncommittedNewOrDroppedDescriptors() {
execCfg := ex.planner.ExecCfg()
if err := UpdateDescriptorCount(ex.Ctx(), execCfg, execCfg.SchemaChangerMetrics); err != nil {
log.Warningf(ex.Ctx(), "failed to scan descriptor table: %v", err)
}
}
fallthrough
case txnRollback:
ex.resetExtraTxnState(ex.Ctx(), advInfo.txnEvent, payloadErr)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/isql/isql_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type Executor interface {
) (int, error)

// QueryRow executes the supplied SQL statement and returns a single row, or
// nil if no row is found, or an error if more that one row is returned.
// nil if no row is found, or an error if more than one row is returned.
//
// QueryRow is deprecated. Use QueryRowEx() instead.
QueryRow(
Expand Down
17 changes: 17 additions & 0 deletions pkg/sql/schema_changer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3341,3 +3341,20 @@ func (p *planner) CanCreateCrossDBSequenceRef() error {
}
return nil
}

// UpdateDescriptorCount updates our sql.schema_changer.object_count gauge with
// a fresh count of objects in the system.descriptor table.
func UpdateDescriptorCount(
ctx context.Context, execCfg *ExecutorConfig, metric *SchemaChangerMetrics,
) error {
return DescsTxn(ctx, execCfg, func(ctx context.Context, txn isql.Txn, col *descs.Collection) error {
row, err := txn.QueryRow(ctx, "sql-schema-changer-object-count", txn.KV(),
`SELECT count(*) FROM system.descriptor`)
if err != nil {
return err
}
count := *row[0].(*tree.DInt)
metric.ObjectCount.Update(int64(count))
return nil
})
}
8 changes: 8 additions & 0 deletions pkg/sql/schema_changer_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ var (
Measurement: "Errors",
Unit: metric.Unit_COUNT,
}
metaObjects = metric.Metadata{
Name: "sql.schema_changer.object_count",
Help: "Counter of the number of objects in the cluster",
Measurement: "Objects",
Unit: metric.Unit_COUNT,
}
)

// SchemaChangerMetrics are metrics corresponding to the schema changer.
Expand All @@ -48,6 +54,7 @@ type SchemaChangerMetrics struct {
PermanentErrors *metric.Counter
ConstraintErrors telemetry.Counter
UncategorizedErrors telemetry.Counter
ObjectCount *metric.Gauge
}

// MetricStruct makes SchemaChangerMetrics a metric.Struct.
Expand All @@ -64,5 +71,6 @@ func NewSchemaChangerMetrics() *SchemaChangerMetrics {
PermanentErrors: metric.NewCounter(metaPermanentErrors),
ConstraintErrors: sqltelemetry.SchemaChangeErrorCounter("constraint_violation"),
UncategorizedErrors: sqltelemetry.SchemaChangeErrorCounter("uncategorized"),
ObjectCount: metric.NewGauge(metaObjects),
}
}