Skip to content

Commit

Permalink
cli/zip: emit SQL table data using JSON by default
Browse files Browse the repository at this point in the history
Release note (backward-incompatible change): The command `cockroach
debug zip` now stores data retrieved from SQL tables in the remote
cluster using the JSON format by default. The previous version
behavior can be obtained by passing `--format=tsv` explicitely.

The JSON data is easier to explore programmatically; and it
can also be explored in a web browser using the
[JsonDiscovery extension](https://github.com/discoveryjs/browser-extension-json-discovery).

Release note (cli change): `cockroach debug zip` now supports the
command-line flag `--format` to select the format used to store SQL
table data, in the same way as `cockroach sql`. In contrast to
`cockroach sql` however, its default value is `json` (resulting in
files named `.json`) and the default is not dependent on whether
the terminal is interactive.
  • Loading branch information
knz committed May 12, 2023
1 parent 06201b9 commit 35738d4
Show file tree
Hide file tree
Showing 14 changed files with 1,133 additions and 1,111 deletions.
1 change: 1 addition & 0 deletions pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ func init() {
statementBundleRecreateCmd,
debugListFilesCmd,
debugJobTraceFromClusterCmd,
debugZipCmd,
},
demoCmd.Commands()...)
tableOutputCommands = append(tableOutputCommands, nodeCmds...)
Expand Down
302 changes: 151 additions & 151 deletions pkg/cli/testdata/zip/partial1

Large diffs are not rendered by default.

214 changes: 107 additions & 107 deletions pkg/cli/testdata/zip/partial1_excluded

Large diffs are not rendered by default.

214 changes: 107 additions & 107 deletions pkg/cli/testdata/zip/partial2

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions pkg/cli/testdata/zip/specialnames
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
zip
----
[cluster] retrieving SQL data for crdb_internal.cluster_database_privileges... writing output: debug/crdb_internal.cluster_database_privileges.txt... done
[cluster] retrieving SQL data for crdb_internal.table_indexes... writing output: debug/crdb_internal.table_indexes.txt... done
[cluster] retrieving SQL data for system.database_role_settings... writing output: debug/system.database_role_settings.txt... done
[cluster] retrieving SQL data for system.table_statistics... writing output: debug/system.table_statistics.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_database_privileges... writing output: debug/crdb_internal.cluster_database_privileges.json... done
[cluster] retrieving SQL data for crdb_internal.table_indexes... writing output: debug/crdb_internal.table_indexes.json... done
[cluster] retrieving SQL data for system.database_role_settings... writing output: debug/system.database_role_settings.json... done
[cluster] retrieving SQL data for system.table_statistics... writing output: debug/system.table_statistics.json... done
170 changes: 85 additions & 85 deletions pkg/cli/testdata/zip/testzip

Large diffs are not rendered by default.

258 changes: 129 additions & 129 deletions pkg/cli/testdata/zip/testzip_concurrent

Large diffs are not rendered by default.

170 changes: 85 additions & 85 deletions pkg/cli/testdata/zip/testzip_include_range_info

Large diffs are not rendered by default.

366 changes: 183 additions & 183 deletions pkg/cli/testdata/zip/testzip_shared_process_tenant

Large diffs are not rendered by default.

196 changes: 98 additions & 98 deletions pkg/cli/testdata/zip/testzip_tenant_separate_process

Large diffs are not rendered by default.

292 changes: 146 additions & 146 deletions pkg/cli/testdata/zip/unavailable

Large diffs are not rendered by default.

23 changes: 19 additions & 4 deletions pkg/cli/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (zc *debugZipContext) forAllNodes(

type nodeLivenesses = map[roachpb.NodeID]livenesspb.NodeLivenessStatus

func runDebugZip(_ *cobra.Command, args []string) (retErr error) {
func runDebugZip(cmd *cobra.Command, args []string) (retErr error) {
if err := zipCtx.files.validate(); err != nil {
return err
}
Expand Down Expand Up @@ -241,8 +241,13 @@ func runDebugZip(_ *cobra.Command, args []string) (retErr error) {
cliCtx.IsInteractive = false
sqlExecCtx.TerminalOutput = false
sqlExecCtx.ShowTimes = false
// Use a streaming format to avoid accumulating all rows in RAM.
sqlExecCtx.TableDisplayFormat = clisqlexec.TableDisplayTSV

if !cmd.Flags().Changed(cliflags.TableDisplayFormat.Name) {
// Use a streaming format to avoid accumulating all rows in RAM.
sqlExecCtx.TableDisplayFormat = clisqlexec.TableDisplayJSON
}

zr.sqlOutputFilenameExtension = computeSQLOutputFilenameExtension(sqlExecCtx.TableDisplayFormat)

sqlConn, err := makeTenantSQLClient("cockroach zip", useSystemDb, tenant.TenantName)
// The zip output is sent directly into a text file, so the results should
Expand Down Expand Up @@ -382,7 +387,7 @@ func (zc *debugZipContext) dumpTableDataForZip(
const maxRetries = 5
suffix := ""
for numRetries := 1; numRetries <= maxRetries; numRetries++ {
name := baseName + suffix + ".txt"
name := baseName + suffix + "." + zc.clusterPrinter.sqlOutputFilenameExtension
s.progress("writing output: %s", name)
sqlErr := func() error {
zc.z.Lock()
Expand Down Expand Up @@ -433,3 +438,13 @@ func (zc *debugZipContext) dumpTableDataForZip(
func sanitizeFilename(f string) string {
return strings.TrimPrefix(f, `"".`)
}

func computeSQLOutputFilenameExtension(tfmt clisqlexec.TableDisplayFormat) string {
switch tfmt {
case clisqlexec.TableDisplayTSV:
// Backward-compatibility with previous versions.
return "txt"
default:
return tfmt.String()
}
}
4 changes: 4 additions & 0 deletions pkg/cli/zip_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ type zipReporter struct {
// withPrefix(), start(), info() are only valid while inItem is false,
// whereas progress(), done() and fail() are only valid while inItem is true.
inItem bool

// The file name extension for SQL table contents
// retrieved via RunQueryAndFormatResults().
sqlOutputFilenameExtension string
}

func (zc *zipContext) newZipReporter(format string, args ...interface{}) *zipReporter {
Expand Down
26 changes: 14 additions & 12 deletions pkg/cli/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,11 @@ func TestZipRetries(t *testing.T) {
}()

zr := zipCtx.newZipReporter("test")
zr.sqlOutputFilenameExtension = "json"
zc := debugZipContext{
z: z,
timeout: 3 * time.Second,
z: z,
clusterPrinter: zr,
timeout: 3 * time.Second,
}
if err := zc.dumpTableDataForZip(
zr,
Expand All @@ -590,16 +592,16 @@ func TestZipRetries(t *testing.T) {
for _, f := range r.File {
fmt.Fprintln(&fileList, f.Name)
}
const expected = `test/generate_series(1,15000) as t(x).txt
test/generate_series(1,15000) as t(x).txt.err.txt
test/generate_series(1,15000) as t(x).1.txt
test/generate_series(1,15000) as t(x).1.txt.err.txt
test/generate_series(1,15000) as t(x).2.txt
test/generate_series(1,15000) as t(x).2.txt.err.txt
test/generate_series(1,15000) as t(x).3.txt
test/generate_series(1,15000) as t(x).3.txt.err.txt
test/generate_series(1,15000) as t(x).4.txt
test/generate_series(1,15000) as t(x).4.txt.err.txt
const expected = `test/generate_series(1,15000) as t(x).json
test/generate_series(1,15000) as t(x).json.err.txt
test/generate_series(1,15000) as t(x).1.json
test/generate_series(1,15000) as t(x).1.json.err.txt
test/generate_series(1,15000) as t(x).2.json
test/generate_series(1,15000) as t(x).2.json.err.txt
test/generate_series(1,15000) as t(x).3.json
test/generate_series(1,15000) as t(x).3.json.err.txt
test/generate_series(1,15000) as t(x).4.json
test/generate_series(1,15000) as t(x).4.json.err.txt
`
assert.Equal(t, expected, fileList.String())
}
Expand Down

0 comments on commit 35738d4

Please sign in to comment.