Skip to content

Commit

Permalink
Merge #68506 #68540
Browse files Browse the repository at this point in the history
68506: util/log,docs/generated: update crdb-v2 example formatting r=taroface a=taroface

The `crdb-v2` log examples in the autogen docs were not appearing correctly when embedded on the [public docs page](https://www.cockroachlabs.com/docs/v21.1/log-formats.html#examples). As a workaround, that doc is currently using a manually altered copy of the `logformats.md` doc. This change should fix the formatting issues and enable us to embed the generated doc (see: cockroachdb/docs#11018).

Release note: none

68540: backupccl: add option to display descriptor ids to SHOW BACKUP r=rhu713 a=rhu713

Previously there weren't any descriptor ID information displayed by SHOW BACKUP. These IDs provide
useful information during triage and general debugging. To address this, a WITH option debug_ids
was added to populate additional columns in SHOW BACKUP with descriptor IDS of all objects along
with the IDs of its database and parent schema.

Release note (enterprise change): Descriptor IDs of every object are now visible in SHOW BACKUP,
along with the descriptor IDs of the object's database and parent schema. SHOW BACKUP will display
these IDs if the `WITH debug_ids` option is specified.

Co-authored-by: taroface <[email protected]>
Co-authored-by: Rui Hu <[email protected]>
  • Loading branch information
3 people committed Aug 9, 2021
3 parents aa9701d + 9709d83 + ab27b06 commit ea1ff1e
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 16 deletions.
26 changes: 18 additions & 8 deletions docs/generated/logformats.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,24 +254,34 @@ reliably that no counter was present.

Example single-line unstructured entry:

I210116 21:49:17.073282 14 server/node.go:464 ⋮ [] 23 started with engine type ‹2›
~~~
I210116 21:49:17.073282 14 server/node.go:464 ⋮ [] 23 started with engine type ‹2›
~~~

Example multi-line unstructured entry:

I210116 21:49:17.083093 14 1@cli/start.go:690 ⋮ [-] 40 node startup completed:
I210116 21:49:17.083093 14 1@cli/start.go:690 ⋮ [-] 40 +CockroachDB node starting at 2021-01-16 21:49 (took 0.0s)
~~~
I210116 21:49:17.083093 14 1@cli/start.go:690 ⋮ [-] 40 node startup completed:
I210116 21:49:17.083093 14 1@cli/start.go:690 ⋮ [-] 40 +CockroachDB node starting at 2021-01-16 21:49 (took 0.0s)
~~~

Example structured entry:

I210116 21:49:17.080713 14 1@util/log/event_log.go:32 ⋮ [] 32 ={"Timestamp":1610833757080706620,"EventType":"node_restart"}
~~~
I210116 21:49:17.080713 14 1@util/log/event_log.go:32 ⋮ [] 32 ={"Timestamp":1610833757080706620,"EventType":"node_restart"}
~~~

Example long entries broken up into multiple lines:

I210116 21:49:17.073282 14 server/node.go:464 ⋮ [] 23 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa....
I210116 21:49:17.073282 14 server/node.go:464 ⋮ [] 23 |aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
~~~
I210116 21:49:17.073282 14 server/node.go:464 ⋮ [] 23 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa....
I210116 21:49:17.073282 14 server/node.go:464 ⋮ [] 23 |aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
~~~

I210116 21:49:17.080713 14 1@util/log/event_log.go:32 ⋮ [] 32 ={"Timestamp":1610833757080706620,"EventTy...
I210116 21:49:17.080713 14 1@util/log/event_log.go:32 ⋮ [] 32 |pe":"node_restart"}
~~~
I210116 21:49:17.080713 14 1@util/log/event_log.go:32 ⋮ [] 32 ={"Timestamp":1610833757080706620,"EventTy...
I210116 21:49:17.080713 14 1@util/log/event_log.go:32 ⋮ [] 32 |pe":"node_restart"}
~~~

### Backward-compatibility notes

Expand Down
1 change: 1 addition & 0 deletions pkg/ccl/backupccl/backup_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const (
backupOptEncKMS = "kms"
backupOptWithPrivileges = "privileges"
backupOptAsJSON = "as_json"
backupOptWithDebugIDs = "debug_ids"
localityURLParam = "COCKROACH_LOCALITY"
defaultLocalityValue = "default"
)
Expand Down
58 changes: 58 additions & 0 deletions pkg/ccl/backupccl/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func showBackupPlanHook(
backupOptEncKMS: sql.KVStringOptRequireValue,
backupOptWithPrivileges: sql.KVStringOptRequireNoValue,
backupOptAsJSON: sql.KVStringOptRequireNoValue,
backupOptWithDebugIDs: sql.KVStringOptRequireNoValue,
}
optsFn, err := p.TypeAsStringOpts(ctx, backup.Options, expected)
if err != nil {
Expand Down Expand Up @@ -260,6 +261,20 @@ func backupShowerHeaders(showSchemas bool, opts map[string]string) colinfo.Resul
baseHeaders = append(baseHeaders, colinfo.ResultColumn{Name: "privileges", Typ: types.String})
baseHeaders = append(baseHeaders, colinfo.ResultColumn{Name: "owner", Typ: types.String})
}

if _, shouldShowIDs := opts[backupOptWithDebugIDs]; shouldShowIDs {
baseHeaders = append(
colinfo.ResultColumns{
baseHeaders[0],
{Name: "database_id", Typ: types.Int},
baseHeaders[1],
{Name: "parent_schema_id", Typ: types.Int},
baseHeaders[2],
{Name: "object_id", Typ: types.Int},
},
baseHeaders[3:]...,
)
}
return baseHeaders
}

Expand Down Expand Up @@ -325,6 +340,9 @@ func backupShowerDefault(
var parentSchemaName string
var descriptorType string

var dbID descpb.ID
var parentSchemaID descpb.ID

createStmtDatum := tree.DNull
dataSizeDatum := tree.DNull
rowCountDatum := tree.DNull
Expand All @@ -338,14 +356,19 @@ func backupShowerDefault(
case catalog.SchemaDescriptor:
descriptorType = "schema"
dbName = dbIDToName[desc.GetParentID()]
dbID = desc.GetParentID()
case catalog.TypeDescriptor:
descriptorType = "type"
dbName = dbIDToName[desc.GetParentID()]
dbID = desc.GetParentID()
parentSchemaName = schemaIDToName[desc.GetParentSchemaID()]
parentSchemaID = desc.GetParentSchemaID()
case catalog.TableDescriptor:
descriptorType = "table"
dbName = dbIDToName[desc.GetParentID()]
dbID = desc.GetParentID()
parentSchemaName = schemaIDToName[desc.GetParentSchemaID()]
parentSchemaID = desc.GetParentSchemaID()
descSize := descSizes[desc.GetID()]
dataSizeDatum = tree.NewDInt(tree.DInt(descSize.DataSize))
rowCountDatum = tree.NewDInt(tree.DInt(descSize.Rows))
Expand Down Expand Up @@ -386,6 +409,20 @@ func backupShowerDefault(
owner := desc.GetPrivileges().Owner().SQLIdentifier()
row = append(row, tree.NewDString(owner))
}
if _, shouldShowIDs := opts[backupOptWithDebugIDs]; shouldShowIDs {
// If showing debug IDs, interleave the IDs with the corresponding object names.
row = append(
tree.Datums{
row[0],
nullIfZero(dbID),
row[1],
nullIfZero(parentSchemaID),
row[2],
nullIfZero(desc.GetID()),
},
row[3:]...,
)
}
rows = append(rows, row)
}
for _, t := range manifest.Tenants {
Expand All @@ -407,6 +444,20 @@ func backupShowerDefault(
if _, shouldShowPrivileges := opts[backupOptWithPrivileges]; shouldShowPrivileges {
row = append(row, tree.DNull)
}
if _, shouldShowIDs := opts[backupOptWithDebugIDs]; shouldShowIDs {
// If showing debug IDs, interleave the IDs with the corresponding object names.
row = append(
tree.Datums{
row[0],
tree.DNull, // Database ID
row[1],
tree.DNull, // Parent Schema ID
row[2],
tree.NewDInt(tree.DInt(t.ID)), // Object ID
},
row[3:]...,
)
}
rows = append(rows, row)
}
}
Expand All @@ -422,6 +473,13 @@ func nullIfEmpty(s string) tree.Datum {
return tree.NewDString(s)
}

func nullIfZero(i descpb.ID) tree.Datum {
if i == 0 {
return tree.DNull
}
return tree.NewDInt(tree.DInt(i))
}

func showPrivileges(descriptor *descpb.Descriptor) string {
var privStringBuilder strings.Builder

Expand Down
65 changes: 65 additions & 0 deletions pkg/ccl/backupccl/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@ func TestShowBackupTenants(t *testing.T) {
require.Equal(t, [][]string{
{"/Tenant/10", "/Tenant/11"},
}, res)

res = systemDB.QueryStr(t, `SELECT database_id, parent_schema_id, object_id FROM [SHOW BACKUP 'nodelocal://1/t10' WITH debug_ids]`)
require.Equal(t, [][]string{
{"NULL", "NULL", "10"},
}, res)
}

func TestShowBackupPrivileges(t *testing.T) {
Expand Down Expand Up @@ -586,3 +591,63 @@ func showUpgradedForeignKeysTest(exportDir string) func(t *testing.T) {
}
}
}

func TestShowBackupWithDebugIDs(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

const numAccounts = 11
// Create test database with bank table
_, _, sqlDB, _, cleanupFn := BackupRestoreTestSetup(t, singleNode, numAccounts, InitManualReplication)
defer cleanupFn()

// add 1 type, 1 schema, and 2 tables to the database
sqlDB.Exec(t, `
SET CLUSTER SETTING sql.cross_db_fks.enabled = TRUE;
CREATE TYPE data.welcome AS ENUM ('hello', 'hi');
USE data; CREATE SCHEMA sc;
CREATE TABLE data.sc.t1 (a INT);
CREATE TABLE data.sc.t2 (a data.welcome);
`)

const full = LocalFoo + "/full"

beforeTS := sqlDB.QueryStr(t, `SELECT now()::timestamp::string`)[0][0]
sqlDB.Exec(t, fmt.Sprintf(`BACKUP DATABASE data TO $1 AS OF SYSTEM TIME '%s'`, beforeTS), full)

// extract the object IDs for the database and public schema
databaseRow := sqlDB.QueryStr(t, `
SELECT database_name, database_id, parent_schema_name, parent_schema_id, object_name, object_id, object_type
FROM [SHOW BACKUP $1 WITH debug_ids]
WHERE object_name = 'bank'`, full)
require.NotEmpty(t, databaseRow)
dbID, err := strconv.Atoi(databaseRow[0][1])
require.NoError(t, err)
publicID, err := strconv.Atoi(databaseRow[0][3])
require.NoError(t, err)

require.Greater(t, dbID, 0)
require.Greater(t, publicID, 0)

res := sqlDB.QueryStr(t, `
SELECT database_name, database_id, parent_schema_name, parent_schema_id, object_name, object_id, object_type
FROM [SHOW BACKUP $1 WITH debug_ids]
ORDER BY object_id`, full)

dbIDStr := strconv.Itoa(dbID)
publicIDStr := strconv.Itoa(publicID)
schemaIDStr := strconv.Itoa(dbID + 4)

expectedObjects := [][]string{
{"NULL", "NULL", "NULL", "NULL", "data", dbIDStr, "database"},
{"data", dbIDStr, "public", publicIDStr, "bank", strconv.Itoa(dbID + 1), "table"},
{"data", dbIDStr, "public", publicIDStr, "welcome", strconv.Itoa(dbID + 2), "type"},
{"data", dbIDStr, "public", publicIDStr, "_welcome", strconv.Itoa(dbID + 3), "type"},
{"data", dbIDStr, "NULL", "NULL", "sc", schemaIDStr, "schema"},
{"data", dbIDStr, "sc", schemaIDStr, "t1", strconv.Itoa(dbID + 5), "table"},
{"data", dbIDStr, "sc", schemaIDStr, "t2", strconv.Itoa(dbID + 6), "table"},
}

require.Equal(t, expectedObjects, res)

}
26 changes: 18 additions & 8 deletions pkg/util/log/format_crdb_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,34 @@ reliably that no counter was present.
Example single-line unstructured entry:
I210116 21:49:17.073282 14 server/node.go:464 ⋮ [] 23 started with engine type ‹2›
~~~
I210116 21:49:17.073282 14 server/node.go:464 ⋮ [] 23 started with engine type ‹2›
~~~
Example multi-line unstructured entry:
I210116 21:49:17.083093 14 1@cli/start.go:690 ⋮ [-] 40 node startup completed:
I210116 21:49:17.083093 14 1@cli/start.go:690 ⋮ [-] 40 +CockroachDB node starting at 2021-01-16 21:49 (took 0.0s)
~~~
I210116 21:49:17.083093 14 1@cli/start.go:690 ⋮ [-] 40 node startup completed:
I210116 21:49:17.083093 14 1@cli/start.go:690 ⋮ [-] 40 +CockroachDB node starting at 2021-01-16 21:49 (took 0.0s)
~~~
Example structured entry:
I210116 21:49:17.080713 14 1@util/log/event_log.go:32 ⋮ [] 32 ={"Timestamp":1610833757080706620,"EventType":"node_restart"}
~~~
I210116 21:49:17.080713 14 1@util/log/event_log.go:32 ⋮ [] 32 ={"Timestamp":1610833757080706620,"EventType":"node_restart"}
~~~
Example long entries broken up into multiple lines:
I210116 21:49:17.073282 14 server/node.go:464 ⋮ [] 23 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa....
I210116 21:49:17.073282 14 server/node.go:464 ⋮ [] 23 |aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
~~~
I210116 21:49:17.073282 14 server/node.go:464 ⋮ [] 23 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa....
I210116 21:49:17.073282 14 server/node.go:464 ⋮ [] 23 |aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
~~~
I210116 21:49:17.080713 14 1@util/log/event_log.go:32 ⋮ [] 32 ={"Timestamp":1610833757080706620,"EventTy...
I210116 21:49:17.080713 14 1@util/log/event_log.go:32 ⋮ [] 32 |pe":"node_restart"}
~~~
I210116 21:49:17.080713 14 1@util/log/event_log.go:32 ⋮ [] 32 ={"Timestamp":1610833757080706620,"EventTy...
I210116 21:49:17.080713 14 1@util/log/event_log.go:32 ⋮ [] 32 |pe":"node_restart"}
~~~
### Backward-compatibility notes
Expand Down

0 comments on commit ea1ff1e

Please sign in to comment.