Skip to content

Commit

Permalink
sql/telemetry: add sync.Pool for SampledQuery objects
Browse files Browse the repository at this point in the history
Add a pool for SampledQuery objects. At high logging frequencies
we can avoid an allocation per statement.

Epic: none

Release note: None
  • Loading branch information
xinhaoz committed Jan 24, 2024
1 parent d0d20af commit f741c2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/sql/exec_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,10 @@ func (p *planner) maybeLogStatementInternal(
})
}

sampledQuery := eventpb.SampledQuery{
sampledQuery := getSampledQuery()
defer releaseSampledQuery(sampledQuery)

*sampledQuery = eventpb.SampledQuery{
CommonSQLExecDetails: execDetails,
SkippedQueries: skippedQueries,
CostEstimate: p.curPlan.instrumentation.costEstimate,
Expand Down Expand Up @@ -431,7 +434,7 @@ func (p *planner) maybeLogStatementInternal(
SchemaChangerMode: p.curPlan.instrumentation.schemaChangerMode.String(),
}

p.logOperationalEventsOnlyExternally(ctx, &sampledQuery)
p.logOperationalEventsOnlyExternally(ctx, sampledQuery)
}
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/sql/telemetry_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
package sql

import (
"sync"
"sync/atomic"
"time"

"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/execstats"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
Expand Down Expand Up @@ -96,6 +98,23 @@ var telemetrySamplingMode = settings.RegisterEnumSetting(
settings.WithPublic,
)

// SampledQuery objects are short-lived but can be
// allocated frequently if logging frequency is high.
var sampledQueryPool = sync.Pool{
New: func() interface{} {
return new(eventpb.SampledQuery)
},
}

func getSampledQuery() *eventpb.SampledQuery {
return sampledQueryPool.Get().(*eventpb.SampledQuery)
}

func releaseSampledQuery(sq *eventpb.SampledQuery) {
*sq = eventpb.SampledQuery{}
sampledQueryPool.Put(sq)
}

// TelemetryLoggingMetrics keeps track of the last time at which an event
// was sampled to the telemetry channel, and the number of skipped events
// since the last sampled event.
Expand Down

0 comments on commit f741c2f

Please sign in to comment.