Skip to content

Commit

Permalink
cli: Enable cockroach debug tsdump to automatically create tsdump.yaml
Browse files Browse the repository at this point in the history
When an user creates a tsdump in raw format, it will also
automatically create a tsdump.yaml file containing the
storeID: nodeID mappings. This file is required by
Technical Support in order to be able to load tsdump raw
files for troubleshooting purposes.

This change will expedite the TSEs ability to investigate
issues requiring the tsdump, as it eliminates the
requirement for TSEs to also obtain the debug zip in order
to create the tsdump.yaml file.

The tsdump.yaml is only created automatically if the
flag `--format=raw` is used and the tsdump.yaml
will be automatically created in `/tmp/tsump.yaml`.

In addition to using the `--format=raw` flag, an user can
pass the `--yaml` flag, allowing the user to
chose where to save the yaml file, instead of the default
location (`/tmp`).

Release note (cli change): cockroach debug tsdump creates tsdump.yaml

Usage:

cockroach debug tsdump --host <host>:<port> \
--format raw --yaml=/some_path/tsdump.yaml > /some_path/tsdump.gob

Authored by: Daniel Almeida

Epic: none
  • Loading branch information
daniel-crlabs committed Nov 8, 2023
1 parent 6d37da2 commit a56dc25
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,7 @@ func init() {
f.Var(&debugTimeSeriesDumpOpts.to, "to", "newest timestamp to include (inclusive)")
f.StringVar(&debugTimeSeriesDumpOpts.clusterLabel, "cluster-label",
"", "prometheus label for cluster name")
f.StringVar(&debugTimeSeriesDumpOpts.yaml, "yaml", debugTimeSeriesDumpOpts.yaml, "full path to create the tsdump.yaml with storeID: nodeID mappings (raw format only). This file is required when loading the raw tsdump for troubleshooting.")

f = debugSendKVBatchCmd.Flags()
f.StringVar(&debugSendKVBatchContext.traceFormat, "trace", debugSendKVBatchContext.traceFormat,
Expand Down
46 changes: 46 additions & 0 deletions pkg/cli/tsdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/cli/clierrorplus"
"github.com/cockroachdb/cockroach/pkg/cli/clisqlclient" // Added by Daniel Almeida to enable yaml to be created
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/ts"
Expand All @@ -39,11 +40,13 @@ var debugTimeSeriesDumpOpts = struct {
format tsDumpFormat
from, to timestampValue
clusterLabel string
yaml string // Added by Daniel Almeida to enable yaml to be created
}{
format: tsDumpText,
from: timestampValue{},
to: timestampValue(timeutil.Now().Add(24 * time.Hour)),
clusterLabel: "",
yaml: "/tmp/tsdump.yaml", // Added by Daniel Almeida to enable yaml to be created. Default location is /tmp/tsdump.yaml
}

var debugTimeSeriesDumpCmd = &cobra.Command{
Expand Down Expand Up @@ -75,6 +78,11 @@ will then convert it to the --format requested in the current invocation.
if convertFile != "" {
return errors.Errorf("input file is already in raw format")
}
err := createYAML() // Daniel Almeida function to create tsdump.yaml when creating tsdump in raw format only
if err != nil {
return err
}

// Special case, we don't go through the text output code.
case tsDumpCSV:
w = csvTSWriter{w: csv.NewWriter(os.Stdout)}
Expand Down Expand Up @@ -202,6 +210,44 @@ type openMetricsWriter struct {
labels map[string]string
}

// Daniel Almeida function to obtain storeID to nodeID mappings when creating raw tsdump
// We automatically create the tsdump.yaml whenever creating a raw tsdump, as this is required for loading raw time series data
func createYAML() (resErr error) {
file, err := os.OpenFile(debugTimeSeriesDumpOpts.yaml, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return err
}
defer file.Close()

ctx := context.Background()
sqlConn, err := makeSQLClient(ctx, "tsdump-node-to-store-mapping", useSystemDb)
if err != nil {
return err
}
defer func() { resErr = errors.CombineErrors(resErr, sqlConn.Close()) }()

_, rows, err := sqlExecCtx.RunQuery(
ctx,
sqlConn,
clisqlclient.MakeQuery(`SELECT store_id || ': ' || node_id FROM crdb_internal.kv_store_status`), false)

if err != nil {
return err
}

var strStoreNodeID string
for _, row := range rows {
storeNodeID := row
strStoreNodeID = strings.Join(storeNodeID, " ")
strStoreNodeID += "\n"
_, err := file.WriteString(strStoreNodeID)
if err != nil {
return err
}
}
return nil
}

func makeOpenMetricsWriter(out io.Writer) *openMetricsWriter {
// construct labels
labelMap := make(map[string]string)
Expand Down

0 comments on commit a56dc25

Please sign in to comment.