Skip to content

Commit

Permalink
Merge pull request #15566 from benesch/move-app-stats
Browse files Browse the repository at this point in the history
sql,roachpb: relocate app_stats proto from sql to roachpb
  • Loading branch information
benesch authored May 1, 2017
2 parents e33c651 + 77bb924 commit 276f1cd
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 79 deletions.
3 changes: 3 additions & 0 deletions pkg/roachpb/api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions pkg/roachpb/app_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package roachpb

// GetVariance retrieves the variance of the values.
func (l *NumericStat) GetVariance(count int64) float64 {
return l.SquaredDiffs / (float64(count) - 1)
}

// Record adjusts the derived statistics for a new value.
func (l *NumericStat) Record(count int64, val float64) {
delta := val - l.Mean
l.Mean += delta / float64(count)
l.SquaredDiffs += delta * (val - l.Mean)
}
74 changes: 27 additions & 47 deletions pkg/sql/app_stats.pb.go → pkg/roachpb/app_stats.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/sql/app_stats.proto → pkg/roachpb/app_stats.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

syntax = "proto2";
package cockroach.sql;
option go_package = "sql";
option go_package = "roachpb";

import "gogoproto/gogo.proto";

Expand Down
9 changes: 4 additions & 5 deletions pkg/server/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
Expand Down Expand Up @@ -85,10 +84,10 @@ type versionInfo struct {
}

type reportingInfo struct {
Node nodeInfo `json:"node"`
Stores []storeInfo `json:"stores"`
Schema []sqlbase.TableDescriptor `json:"schema"`
QueryStats map[string]map[string]sql.StatementStatistics `json:"sqlstats"`
Node nodeInfo `json:"node"`
Stores []storeInfo `json:"stores"`
Schema []sqlbase.TableDescriptor `json:"schema"`
QueryStats map[string]map[string]roachpb.StatementStatistics `json:"sqlstats"`
}

type nodeInfo struct {
Expand Down
30 changes: 10 additions & 20 deletions pkg/sql/app_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"golang.org/x/net/context"

"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
Expand All @@ -51,7 +52,7 @@ type appStats struct {
type stmtStats struct {
syncutil.Mutex

data StatementStatistics
data roachpb.StatementStatistics
}

// StmtStatsEnable determines whether to collect per-statement
Expand Down Expand Up @@ -126,26 +127,15 @@ func (a *appStats) recordStatement(
} else if int64(automaticRetryCount) > s.data.MaxRetries {
s.data.MaxRetries = int64(automaticRetryCount)
}
s.data.NumRows.record(s.data.Count, float64(numRows))
s.data.ParseLat.record(s.data.Count, parseLat)
s.data.PlanLat.record(s.data.Count, planLat)
s.data.RunLat.record(s.data.Count, runLat)
s.data.ServiceLat.record(s.data.Count, svcLat)
s.data.OverheadLat.record(s.data.Count, ovhLat)
s.data.NumRows.Record(s.data.Count, float64(numRows))
s.data.ParseLat.Record(s.data.Count, parseLat)
s.data.PlanLat.Record(s.data.Count, planLat)
s.data.RunLat.Record(s.data.Count, runLat)
s.data.ServiceLat.Record(s.data.Count, svcLat)
s.data.OverheadLat.Record(s.data.Count, ovhLat)
s.Unlock()
}

// Retrieve the variance of the values.
func (l *NumericStat) getVariance(count int64) float64 {
return l.SquaredDiffs / (float64(count) - 1)
}

func (l *NumericStat) record(count int64, val float64) {
delta := val - l.Mean
l.Mean += delta / float64(count)
l.SquaredDiffs += delta * (val - l.Mean)
}

// getStatsForStmt retrieves the per-stmt stat object.
func (a *appStats) getStatsForStmt(key stmtKey) *stmtStats {
a.Lock()
Expand Down Expand Up @@ -309,7 +299,7 @@ func scrubStmtStatKey(vt virtualSchemaHolder, key string) (string, bool) {

// AppStatementStatistics is a map: for each app name (as set in sql sessions),
// it maps statements to their collected statistics.
type AppStatementStatistics map[string]map[string]StatementStatistics
type AppStatementStatistics map[string]map[string]roachpb.StatementStatistics

// GetScrubbedStmtStats returns the statement statistics by app, with the
// queries scrubbed of their identifiers. Any statements which cannot be
Expand All @@ -321,7 +311,7 @@ func (e *Executor) GetScrubbedStmtStats() AppStatementStatistics {
for appName, a := range e.sqlStats.apps {
hashedApp := HashAppName(appName)
a.Lock()
m := make(map[string]StatementStatistics)
m := make(map[string]roachpb.StatementStatistics)
for q, stats := range a.stmts {
scrubbed, ok := scrubStmtStatKey(vt, q.stmt)
if ok {
Expand Down
12 changes: 6 additions & 6 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,17 +432,17 @@ CREATE TABLE crdb_internal.node_statement_statistics (
parser.NewDInt(parser.DInt(s.data.MaxRetries)),
errString,
parser.NewDFloat(parser.DFloat(s.data.NumRows.Mean)),
parser.NewDFloat(parser.DFloat(s.data.NumRows.getVariance(s.data.Count))),
parser.NewDFloat(parser.DFloat(s.data.NumRows.GetVariance(s.data.Count))),
parser.NewDFloat(parser.DFloat(s.data.ParseLat.Mean)),
parser.NewDFloat(parser.DFloat(s.data.ParseLat.getVariance(s.data.Count))),
parser.NewDFloat(parser.DFloat(s.data.ParseLat.GetVariance(s.data.Count))),
parser.NewDFloat(parser.DFloat(s.data.PlanLat.Mean)),
parser.NewDFloat(parser.DFloat(s.data.PlanLat.getVariance(s.data.Count))),
parser.NewDFloat(parser.DFloat(s.data.PlanLat.GetVariance(s.data.Count))),
parser.NewDFloat(parser.DFloat(s.data.RunLat.Mean)),
parser.NewDFloat(parser.DFloat(s.data.RunLat.getVariance(s.data.Count))),
parser.NewDFloat(parser.DFloat(s.data.RunLat.GetVariance(s.data.Count))),
parser.NewDFloat(parser.DFloat(s.data.ServiceLat.Mean)),
parser.NewDFloat(parser.DFloat(s.data.ServiceLat.getVariance(s.data.Count))),
parser.NewDFloat(parser.DFloat(s.data.ServiceLat.GetVariance(s.data.Count))),
parser.NewDFloat(parser.DFloat(s.data.OverheadLat.Mean)),
parser.NewDFloat(parser.DFloat(s.data.OverheadLat.getVariance(s.data.Count))),
parser.NewDFloat(parser.DFloat(s.data.OverheadLat.GetVariance(s.data.Count))),
)
s.Unlock()
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/sql/jobs.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 276f1cd

Please sign in to comment.