Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: Reorder show ranges output to be clearer #40501

Merged
merged 1 commit into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pkg/sql/delegate/show_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ func (d *delegator) delegateShowRanges(n *tree.ShowRanges) (tree.Statement, erro
END AS end_key,
range_id,
range_size / 1000000 as range_size_mb,
replicas,
lease_holder,
replica_localities,
gossip_nodes.locality as lease_holder_locality,
replicas,
replica_localities
FROM %[1]s.crdb_internal.ranges AS r
LEFT JOIN crdb_internal.gossip_nodes ON lease_holder = node_id
WHERE database_name=%[2]s
ORDER BY table_name, r.start_key
`
Expand All @@ -71,10 +73,12 @@ SELECT
CASE WHEN r.end_key >= x'%s' THEN NULL ELSE crdb_internal.pretty_key(r.end_key, 2) END AS end_key,
range_id,
range_size / 1000000 as range_size_mb,
replicas,
lease_holder,
gossip_nodes.locality as lease_holder_locality,
replicas,
replica_localities
FROM crdb_internal.ranges AS r
LEFT JOIN crdb_internal.gossip_nodes ON lease_holder = node_id
WHERE (r.start_key < x'%s')
AND (r.end_key > x'%s') ORDER BY r.start_key
`,
Expand Down
23 changes: 16 additions & 7 deletions pkg/sql/show_ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,29 @@ func TestShowRangesWithLocality(t *testing.T) {
sqlDB.Exec(t, `CREATE TABLE t (x INT PRIMARY KEY)`)
sqlDB.Exec(t, `ALTER TABLE t SPLIT AT SELECT i FROM generate_series(0, 20) AS g(i)`)

const replicasColIdx = 4
const localitiesColIdx = 6
const leaseHolderIdx = 0
const leaseHolderLocalityIdx = 1
const replicasColIdx = 2
const localitiesColIdx = 3
replicas := make([]int, 3)

result := sqlDB.QueryStr(t, `SHOW RANGES FROM TABLE t`)
q := `SELECT lease_holder, lease_holder_locality, replicas, replica_localities from [SHOW RANGES FROM TABLE t]`
result := sqlDB.QueryStr(t, q)
// Because StartTestCluster changes the locality no matter what the
// arguments are, we expect whatever the test server sets up.
for _, row := range result {
// Verify the leaseholder localities.
leaseHolder := row[leaseHolderIdx]
leaseHolderLocalityExpected := fmt.Sprintf(`region=test,dc=dc%s`, leaseHolder)
if row[leaseHolderLocalityIdx] != leaseHolderLocalityExpected {
t.Fatalf("expected %s found %s", leaseHolderLocalityExpected, row[leaseHolderLocalityIdx])
}

// Verify the replica localities.
_, err := fmt.Sscanf(row[replicasColIdx], "{%d,%d,%d}", &replicas[0], &replicas[1], &replicas[2])
if err != nil {
t.Fatal(err)
}

// Because StartTestCluster changes the locality no matter what the
// arguments are, we expect whatever the test server sets up.
var builder strings.Builder
builder.WriteString("{")
for i, replica := range replicas {
Expand All @@ -59,7 +69,6 @@ func TestShowRangesWithLocality(t *testing.T) {
}
builder.WriteString("}")
expected := builder.String()

if row[localitiesColIdx] != expected {
t.Fatalf("expected %s found %s", expected, row[localitiesColIdx])
}
Expand Down