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

sql: add cluster setting to disable RU estimation #92968

Merged
merged 1 commit into from
Dec 6, 2022
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
11 changes: 7 additions & 4 deletions pkg/ccl/multitenantccl/tenantcostclient/tenant_side.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,10 +790,13 @@ func (c *tenantSideCostController) OnResponseWait(
}

// Record the number of RUs consumed by the IO request.
if sp := tracing.SpanFromContext(ctx); sp != nil && sp.RecordingType() != tracingpb.RecordingOff {
sp.RecordStructured(&roachpb.TenantConsumption{
RU: float64(totalRU),
})
if multitenant.TenantRUEstimateEnabled.Get(&c.settings.SV) {
if sp := tracing.SpanFromContext(ctx); sp != nil &&
sp.RecordingType() != tracingpb.RecordingOff {
sp.RecordStructured(&roachpb.TenantConsumption{
RU: float64(totalRU),
})
}
}

c.mu.Lock()
Expand Down
1 change: 1 addition & 0 deletions pkg/multitenant/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_library(
"//pkg/kv",
"//pkg/multitenant/tenantcostmodel",
"//pkg/roachpb",
"//pkg/settings",
"//pkg/sql/sqlliveness",
"//pkg/sql/sqlutil",
"//pkg/util/metric",
Expand Down
10 changes: 10 additions & 0 deletions pkg/multitenant/cost_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/multitenant/tenantcostmodel"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness"
"github.com/cockroachdb/cockroach/pkg/util/stop"
)
Expand Down Expand Up @@ -131,3 +132,12 @@ type TenantSideExternalIORecorder interface {
type exemptCtxValueType struct{}

var exemptCtxValue interface{} = exemptCtxValueType{}

// TenantRUEstimateEnabled determines whether EXPLAIN ANALYZE should return an
// estimate for the number of RUs consumed by tenants.
var TenantRUEstimateEnabled = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.tenant_ru_estimation.enabled",
"determines whether explain analyze should return an estimate for the query's RU consumption",
true,
)
2 changes: 1 addition & 1 deletion pkg/multitenant/multitenantcpu/cpu_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (h *CPUUsageHelper) StartCollection(

// EndCollection should be called at the end of execution for a flow in order to
// get the estimated number of RUs consumed due to CPU usage. It returns zero
// for non-tenants.
// for non-tenants. It is a no-op if StartCollection was never called.
func (h *CPUUsageHelper) EndCollection(ctx context.Context) (ruFomCPU float64) {
if h.costController == nil || h.costController.GetCostConfig() == nil {
return 0
Expand Down
11 changes: 7 additions & 4 deletions pkg/sql/conn_executor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/multitenant"
"github.com/cockroachdb/cockroach/pkg/multitenant/multitenantcpu"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
Expand Down Expand Up @@ -1090,9 +1091,11 @@ func (ex *connExecutor) dispatchToExecutionEngine(
ex.sessionTracing.TracePlanStart(ctx, stmt.AST.StatementTag())
ex.statsCollector.PhaseTimes().SetSessionPhaseTime(sessionphase.PlannerStartLogicalPlan, timeutil.Now())

if server := ex.server.cfg.DistSQLSrv; server != nil {
// Begin measuring CPU usage for tenants. This is a no-op for non-tenants.
ex.cpuStatsCollector.StartCollection(ctx, server.TenantCostController)
if multitenant.TenantRUEstimateEnabled.Get(ex.server.cfg.SV()) {
if server := ex.server.cfg.DistSQLSrv; server != nil {
// Begin measuring CPU usage for tenants. This is a no-op for non-tenants.
ex.cpuStatsCollector.StartCollection(ctx, server.TenantCostController)
}
}

// If adminAuditLogging is enabled, we want to check for HasAdminRole
Expand Down Expand Up @@ -1325,7 +1328,7 @@ func populateQueryLevelStatsAndRegions(
} else {
// If this query is being run by a tenant, record the RUs consumed by CPU
// usage and network egress to the client.
if cfg.DistSQLSrv != nil {
if multitenant.TenantRUEstimateEnabled.Get(cfg.SV()) && cfg.DistSQLSrv != nil {
if costController := cfg.DistSQLSrv.TenantCostController; costController != nil {
if costCfg := costController.GetCostConfig(); costCfg != nil {
networkEgressRUEstimate := costCfg.PGWireEgressCost(topLevelStats.networkEgressEstimate)
Expand Down
4 changes: 3 additions & 1 deletion pkg/sql/distsql_running.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangecache"
"github.com/cockroachdb/cockroach/pkg/multitenant"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/rpc/nodedialer"
Expand Down Expand Up @@ -749,7 +750,8 @@ func (dsp *DistSQLPlanner) Run(

recv.outputTypes = plan.GetResultTypes()
recv.contendedQueryMetric = dsp.distSQLSrv.Metrics.ContendedQueriesCount
if dsp.distSQLSrv.TenantCostController != nil && planCtx.planner != nil {
if multitenant.TenantRUEstimateEnabled.Get(&dsp.st.SV) &&
dsp.distSQLSrv.TenantCostController != nil && planCtx.planner != nil {
if instrumentation := planCtx.planner.curPlan.instrumentation; instrumentation != nil {
// Only collect the network egress estimate for a tenant that is running
// EXPLAIN ANALYZE, since the overhead is non-negligible.
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/flowinfra/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
deps = [
"//pkg/base",
"//pkg/kv",
"//pkg/multitenant",
"//pkg/roachpb",
"//pkg/settings",
"//pkg/sql/catalog/colinfo",
Expand Down
4 changes: 3 additions & 1 deletion pkg/sql/flowinfra/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"unsafe"

"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/multitenant"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra/execopnode"
Expand Down Expand Up @@ -420,7 +421,8 @@ func (f *FlowBase) StartInternal(ctx context.Context, processors []execinfra.Pro

f.status = flowRunning

if !f.Gateway && f.CollectStats {
if multitenant.TenantRUEstimateEnabled.Get(&f.Cfg.Settings.SV) &&
!f.Gateway && f.CollectStats {
// Remote flows begin collecting CPU usage here, and finish when the last
// outbox finishes. Gateway flows are handled by the connExecutor.
f.FlowCtx.TenantCPUMonitor.StartCollection(ctx, f.Cfg.TenantCostController)
Expand Down
4 changes: 3 additions & 1 deletion pkg/sql/instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/multitenant"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/settings"
Expand Down Expand Up @@ -254,7 +255,8 @@ func (ih *instrumentationHelper) Setup(
ih.codec = cfg.Codec
ih.origCtx = ctx
ih.evalCtx = p.EvalContext()
ih.isTenant = cfg.DistSQLSrv != nil && cfg.DistSQLSrv.TenantCostController != nil
ih.isTenant = multitenant.TenantRUEstimateEnabled.Get(cfg.SV()) && cfg.DistSQLSrv != nil &&
cfg.DistSQLSrv.TenantCostController != nil

switch ih.outputMode {
case explainAnalyzeDebugOutput:
Expand Down