Skip to content

Commit

Permalink
server: add SQLInstanceInfo to diagnosticspb package
Browse files Browse the repository at this point in the history
This commit contains preparatory changes for supporting tenants in our
diagnostics reporting code. Tenants run one or more SQL-only instances
rather than full CRDB nodes.

This commit is intended to be backported to a 20.2 patch release, so it
isolates changes that might affect other production code that's unrelated
to tenants. That includes adding an optional field to the DiagnosticsReport
proto and updating BuildReportingURL to use it. It also renames sqlServer
to SQLServer, so that it can be returned from the exported StartTenant
method.

Release note: None
  • Loading branch information
andy-kimball committed Jan 7, 2021
1 parent de11f50 commit e24720b
Show file tree
Hide file tree
Showing 9 changed files with 890 additions and 156 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/mt_start_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func runStartSQL(cmd *cobra.Command, args []string) error {
tempStorageMaxSizeBytes,
)

addr, httpAddr, err := server.StartTenant(
_, addr, httpAddr, err := server.StartTenant(
ctx,
stopper,
clusterName,
Expand Down
24 changes: 24 additions & 0 deletions pkg/server/diagnostics/reporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2016 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package diagnostics

import (
"time"

"github.com/cockroachdb/cockroach/pkg/settings"
)

// ReportFrequency is the interval at which diagnostics data should be reported.
var ReportFrequency = settings.RegisterPublicNonNegativeDurationSetting(
"diagnostics.reporting.interval",
"interval at which diagnostics data should be reported",
time.Hour,
)
44 changes: 35 additions & 9 deletions pkg/server/diagnosticspb/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"net/url"
"strconv"

"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
)
Expand Down Expand Up @@ -60,6 +62,7 @@ type TestingKnobs struct {
// ClusterInfo contains cluster information that will become part of URLs.
type ClusterInfo struct {
ClusterID uuid.UUID
TenantID roachpb.TenantID
IsInsecure bool
IsInternal bool
}
Expand All @@ -71,36 +74,59 @@ func BuildUpdatesURL(clusterInfo *ClusterInfo, nodeInfo *NodeInfo, knobs *Testin
if knobs != nil && knobs.OverrideUpdatesURL != nil {
url = *knobs.OverrideUpdatesURL
}
return addInfoToURL(url, clusterInfo, nodeInfo)
report := &DiagnosticReport{Node: *nodeInfo}
return addInfoToURL(url, clusterInfo, report)
}

// BuildReportingURL creates a URL to report diagnostics.
// BuildReportingURL creates a URL to report diagnostics. If this is a CRDB
// node, then nodeInfo is filled (and nodeInfo.NodeID is non-zero). Otherwise,
// this is a SQL-only tenant and sqlInfo is filled.
//
// If an empty updates URL is set (via empty environment variable), returns nil.
func BuildReportingURL(clusterInfo *ClusterInfo, nodeInfo *NodeInfo, knobs *TestingKnobs) *url.URL {
func BuildReportingURL(
clusterInfo *ClusterInfo, report *DiagnosticReport, knobs *TestingKnobs,
) *url.URL {
url := reportingURL
if knobs != nil && knobs.OverrideReportingURL != nil {
url = *knobs.OverrideReportingURL
}
return addInfoToURL(url, clusterInfo, nodeInfo)
return addInfoToURL(url, clusterInfo, report)
}

func addInfoToURL(url *url.URL, clusterInfo *ClusterInfo, nodeInfo *NodeInfo) *url.URL {
func addInfoToURL(url *url.URL, clusterInfo *ClusterInfo, report *DiagnosticReport) *url.URL {
if url == nil {
return nil
}
result := *url
q := result.Query()
b := &nodeInfo.Build

// If NodeID is non-zero, then maintain backwards-compatibility by using the
// NodeInfo fields.
// TODO(andyk): Update this to always use other report fields, once they're
// guaranteed to be populated by all callers.
var b build.Info
if report.Node.NodeID != 0 {
// SQLInstanceID is always set to the NodeID for CRDB nodes.
b = report.Node.Build
q.Set("nodeid", strconv.Itoa(int(report.Node.NodeID)))
q.Set("sqlid", strconv.Itoa(int(report.Node.NodeID)))
q.Set("uptime", strconv.Itoa(int(report.Node.Uptime)))
q.Set("licensetype", report.Node.LicenseType)
} else {
b = report.Env.Build
q.Set("sqlid", strconv.Itoa(int(report.SQL.SQLInstanceID)))
q.Set("uptime", strconv.Itoa(int(report.SQL.Uptime)))
q.Set("licensetype", report.Env.LicenseType)
}

q.Set("version", b.Tag)
q.Set("platform", b.Platform)
q.Set("uuid", clusterInfo.ClusterID.String())
q.Set("nodeid", strconv.Itoa(int(nodeInfo.NodeID)))
q.Set("uptime", strconv.Itoa(int(nodeInfo.Uptime)))
q.Set("tenantid", clusterInfo.TenantID.String())
q.Set("insecure", strconv.FormatBool(clusterInfo.IsInsecure))
q.Set("internal", strconv.FormatBool(clusterInfo.IsInternal))
q.Set("buildchannel", b.Channel)
q.Set("envchannel", b.EnvChannel)
q.Set("licensetype", nodeInfo.LicenseType)
result.RawQuery = q.Encode()
return &result
}
Loading

0 comments on commit e24720b

Please sign in to comment.