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

sqlstats: only include local region in statement_statistics #102192

Merged
merged 1 commit into from
Apr 26, 2023
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
3 changes: 0 additions & 3 deletions pkg/ccl/multiregionccl/testdata/secondary_region
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
skip issue-num=98020
----

new-cluster localities=us-east-1,us-east-1,us-west-1,us-west-1,us-central-1,us-central-1,us-central-1,eu-west-1,eu-west-1,eu-west-1
----

Expand Down
55 changes: 5 additions & 50 deletions pkg/sql/executor_statement_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ package sql

import (
"context"
"sort"
"strconv"
"sync"

"github.com/cockroachdb/cockroach/pkg/sql/appstatspb"
"github.com/cockroachdb/cockroach/pkg/sql/contentionpb"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/idxrecommendations"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessionphase"
"github.com/cockroachdb/cockroach/pkg/sql/sqlinstance"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/log"
Expand Down Expand Up @@ -189,7 +186,11 @@ func (ex *connExecutor) recordStatementSummary(
}

nodes := util.CombineUnique(getNodesFromPlanner(planner), []int64{nodeID})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally: ExtractNodesFromSpan returns an unordered set already (intsets.Fast). Then ForEach converts it into a sequence. Then the caller uses util.CombineUnique to transform the sequence back into a set. This is just silly and these steps should be combined.

(cc @j82w )

regions := getRegionsForNodes(ctx, nodes, planner.DistSQLPlanner().sqlAddressResolver)

regions := []string{}
if region, ok := ex.server.cfg.Locality.Find("region"); ok {
regions = append(regions, region)
}

recordedStmtStats := sqlstats.RecordedStmtStats{
SessionID: ex.sessionID,
Expand Down Expand Up @@ -327,49 +328,3 @@ func getNodesFromPlanner(planner *planner) []int64 {
}
return nodes
}

var regionsPool = sync.Pool{
New: func() interface{} {
return make(map[string]struct{})
},
}

func getRegionsForNodes(
ctx context.Context, nodeIDs []int64, resolver sqlinstance.AddressResolver,
) []string {
if resolver == nil {
return nil
}

instances, err := resolver.GetAllInstances(ctx)
if err != nil {
return nil
}

regions := regionsPool.Get().(map[string]struct{})
defer func() {
for region := range regions {
delete(regions, region)
}
regionsPool.Put(regions)
}()

for _, instance := range instances {
for _, node := range nodeIDs {
// TODO(todd): Using int64 for nodeIDs was inappropriate, see #95088.
if int32(instance.InstanceID) == int32(node) {
if region, ok := instance.Locality.Find("region"); ok {
regions[region] = struct{}{}
}
break
}
}
}

result := make([]string, 0, len(regions))
for region := range regions {
result = append(result, region)
}
sort.Strings(result)
return result
}