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

outliers: collect statement fingerprint id #80989

Merged
merged 1 commit into from
May 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
8 changes: 5 additions & 3 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6083,9 +6083,10 @@ CREATE TABLE crdb_internal.cluster_locks (
var crdbInternalNodeExecutionOutliersTable = virtualSchemaTable{
schema: `
CREATE TABLE crdb_internal.node_execution_outliers (
session_id STRING NOT NULL,
transaction_id UUID NOT NULL,
statement_id STRING NOT NULL
session_id STRING NOT NULL,
transaction_id UUID NOT NULL,
statement_id STRING NOT NULL,
statement_fingerprint_id BYTES NOT NULL
);`,
populate: func(ctx context.Context, p *planner, db catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) (err error) {
p.extendedEvalCtx.statsProvider.IterateOutliers(ctx, func(
Expand All @@ -6095,6 +6096,7 @@ CREATE TABLE crdb_internal.node_execution_outliers (
tree.NewDString(hex.EncodeToString(o.Session.ID)),
tree.NewDUuid(tree.DUuid{UUID: *o.Transaction.ID}),
tree.NewDString(hex.EncodeToString(o.Statement.ID)),
tree.NewDBytes(tree.DBytes(sqlstatsutil.EncodeUint64ToBytes(uint64(o.Statement.FingerprintID)))),
))
})
return err
Expand Down
6 changes: 4 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/create_statements
Original file line number Diff line number Diff line change
Expand Up @@ -870,11 +870,13 @@ CREATE TABLE crdb_internal.node_distsql_flows (
CREATE TABLE crdb_internal.node_execution_outliers (
session_id STRING NOT NULL,
transaction_id UUID NOT NULL,
statement_id STRING NOT NULL
statement_id STRING NOT NULL,
statement_fingerprint_id BYTES NOT NULL
) CREATE TABLE crdb_internal.node_execution_outliers (
session_id STRING NOT NULL,
transaction_id UUID NOT NULL,
statement_id STRING NOT NULL
statement_id STRING NOT NULL,
statement_fingerprint_id BYTES NOT NULL
) {} {}
CREATE TABLE crdb_internal.node_inflight_trace_spans (
trace_id INT8 NOT NULL,
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/sqlstats/outliers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
importpath = "github.com/cockroachdb/cockroach/pkg/sql/sqlstats/outliers",
visibility = ["//visibility:public"],
deps = [
"//pkg/roachpb",
"//pkg/settings",
"//pkg/settings/cluster",
"//pkg/sql/clusterunique",
Expand All @@ -30,6 +31,7 @@ go_test(
],
embed = [":outliers"],
deps = [
"//pkg/roachpb",
"//pkg/settings/cluster",
"//pkg/sql/clusterunique",
"//pkg/util/uuid",
Expand Down
7 changes: 6 additions & 1 deletion pkg/sql/sqlstats/outliers/outliers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package outliers
import (
"context"

"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/clusterunique"
Expand Down Expand Up @@ -73,7 +74,10 @@ func New(st *cluster.Settings) *Registry {

// ObserveStatement notifies the registry of a statement execution.
func (r *Registry) ObserveStatement(
sessionID clusterunique.ID, statementID clusterunique.ID, latencyInSeconds float64,
sessionID clusterunique.ID,
statementID clusterunique.ID,
statementFingerprintID roachpb.StmtFingerprintID,
latencyInSeconds float64,
) {
if !r.enabled() {
return
Expand All @@ -82,6 +86,7 @@ func (r *Registry) ObserveStatement(
defer r.mu.Unlock()
r.mu.statements[sessionID] = append(r.mu.statements[sessionID], &Outlier_Statement{
ID: statementID.GetBytes(),
FingerprintID: statementFingerprintID,
LatencyInSeconds: latencyInSeconds,
})
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/sql/sqlstats/outliers/outliers.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ message Outlier {

message Statement {
bytes id = 1 [(gogoproto.customname) = "ID"];
double latency_in_seconds = 2;
uint64 fingerprint_id = 2 [(gogoproto.customname) = "FingerprintID",
(gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/roachpb.StmtFingerprintID"];
double latency_in_seconds = 3;
}

Session session = 1;
Expand Down
16 changes: 11 additions & 5 deletions pkg/sql/sqlstats/outliers/outliers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"testing"
"time"

"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/clusterunique"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/outliers"
Expand All @@ -30,12 +31,13 @@ func TestOutliers(t *testing.T) {
sessionID := clusterunique.IDFromBytes([]byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
txnID := uuid.FastMakeV4()
stmtID := clusterunique.IDFromBytes([]byte("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))
stmtFptID := roachpb.StmtFingerprintID(100)

t.Run("detection", func(t *testing.T) {
st := cluster.MakeTestingClusterSettings()
outliers.LatencyThreshold.Override(ctx, &st.SV, 1*time.Second)
registry := outliers.New(st)
registry.ObserveStatement(sessionID, stmtID, 2)
registry.ObserveStatement(sessionID, stmtID, stmtFptID, 2)
registry.ObserveTransaction(sessionID, txnID)

expected := []*outliers.Outlier{{
Expand All @@ -47,6 +49,7 @@ func TestOutliers(t *testing.T) {
},
Statement: &outliers.Outlier_Statement{
ID: stmtID.GetBytes(),
FingerprintID: stmtFptID,
LatencyInSeconds: 2,
},
}}
Expand All @@ -66,7 +69,7 @@ func TestOutliers(t *testing.T) {
st := cluster.MakeTestingClusterSettings()
outliers.LatencyThreshold.Override(ctx, &st.SV, 0)
registry := outliers.New(st)
registry.ObserveStatement(sessionID, stmtID, 2)
registry.ObserveStatement(sessionID, stmtID, stmtFptID, 2)
registry.ObserveTransaction(sessionID, txnID)

var actual []*outliers.Outlier
Expand All @@ -83,7 +86,7 @@ func TestOutliers(t *testing.T) {
st := cluster.MakeTestingClusterSettings()
outliers.LatencyThreshold.Override(ctx, &st.SV, 1*time.Second)
registry := outliers.New(st)
registry.ObserveStatement(sessionID, stmtID, 0.5)
registry.ObserveStatement(sessionID, stmtID, stmtFptID, 0.5)
registry.ObserveTransaction(sessionID, txnID)

var actual []*outliers.Outlier
Expand All @@ -100,12 +103,13 @@ func TestOutliers(t *testing.T) {
otherSessionID := clusterunique.IDFromBytes([]byte("cccccccccccccccccccccccccccccccc"))
otherTxnID := uuid.FastMakeV4()
otherStmtID := clusterunique.IDFromBytes([]byte("dddddddddddddddddddddddddddddddd"))
otherStmtFptID := roachpb.StmtFingerprintID(101)

st := cluster.MakeTestingClusterSettings()
outliers.LatencyThreshold.Override(ctx, &st.SV, 1*time.Second)
registry := outliers.New(st)
registry.ObserveStatement(sessionID, stmtID, 2)
registry.ObserveStatement(otherSessionID, otherStmtID, 3)
registry.ObserveStatement(sessionID, stmtID, stmtFptID, 2)
registry.ObserveStatement(otherSessionID, otherStmtID, otherStmtFptID, 3)
registry.ObserveTransaction(sessionID, txnID)
registry.ObserveTransaction(otherSessionID, otherTxnID)

Expand All @@ -118,6 +122,7 @@ func TestOutliers(t *testing.T) {
},
Statement: &outliers.Outlier_Statement{
ID: stmtID.GetBytes(),
FingerprintID: stmtFptID,
LatencyInSeconds: 2,
},
}, {
Expand All @@ -129,6 +134,7 @@ func TestOutliers(t *testing.T) {
},
Statement: &outliers.Outlier_Statement{
ID: otherStmtID.GetBytes(),
FingerprintID: otherStmtFptID,
LatencyInSeconds: 3,
},
}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sqlstats/ssmemstorage/ss_mem_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (s *Container) RecordStatement(
}
}

s.outliersRegistry.ObserveStatement(value.SessionID, value.StatementID, value.ServiceLatency)
s.outliersRegistry.ObserveStatement(value.SessionID, value.StatementID, stmtFingerprintID, value.ServiceLatency)

return stats.ID, nil
}
Expand Down