Skip to content

Commit

Permalink
sql: Sort replicas in crdb_internal.ranges table by store ID
Browse files Browse the repository at this point in the history
This matches the behavior of `SHOW RANGES`, and is the only way for the
replicas to be meaningfully ordered in a consistent way. It's odd that
the order doesn't even match the order in which the replicas are added.
For example, I could see this sequence in the logs for a range:

Starting replicas: [s4]
Adding s1 results in: [s4,s1]
Adding s2 results in: [s4,s1,s2]
Removing s4 results in: [s2,s1]

Release note (sql change): The entries in the replicas array column of
the crdb_internal.ranges virtual table are now always sorted by store
ID.
  • Loading branch information
a-robinson committed Sep 19, 2018
1 parent b9bfb5f commit daf82ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 9 additions & 2 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1591,12 +1591,19 @@ CREATE TABLE crdb_internal.ranges (
if err := r.ValueProto(&desc); err != nil {
return nil, err
}

var replicas []int
for _, rd := range desc.Replicas {
replicas = append(replicas, int(rd.StoreID))
}
sort.Ints(replicas)
arr := tree.NewDArray(types.Int)
for _, replica := range desc.Replicas {
if err := arr.Append(tree.NewDInt(tree.DInt(replica.StoreID))); err != nil {
for _, replica := range replicas {
if err := arr.Append(tree.NewDInt(tree.DInt(replica))); err != nil {
return nil, err
}
}

var dbName, tableName, indexName string
if _, id, err := keys.DecodeTablePrefix(desc.StartKey.AsRawKey()); err == nil {
parent := parents[id]
Expand Down
10 changes: 5 additions & 5 deletions pkg/sql/logictest/testdata/logic_test/ranges
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@ SELECT * FROM crdb_internal.ranges
----
range_id start_key start_pretty end_key end_pretty database table index replicas lease_holder
1 · /Min [189 137 137] /Table/53/1/1 · · · {1} 1
2 [189 137 137] /Table/53/1/1 [189 137 141 137] /Table/53/1/5/1 test t · {4,3} 3
11 [189 137 141 137] /Table/53/1/5/1 [189 137 141 138] /Table/53/1/5/2 test t · {3,1,2} 1
12 [189 137 141 138] /Table/53/1/5/2 [189 137 141 139] /Table/53/1/5/3 test t · {3,5,2} 5
13 [189 137 141 139] /Table/53/1/5/3 [189 137 143 144 254 190 137 145] /Table/53/1/7/8/#/54/1/9 test t · {4,1,2} 4
14 [189 137 143 144 254 190 137 145] /Table/53/1/7/8/#/54/1/9 [189 137 146] /Table/53/1/10 test t · {4,1,2} 4
2 [189 137 137] /Table/53/1/1 [189 137 141 137] /Table/53/1/5/1 test t · {3,4} 3
11 [189 137 141 137] /Table/53/1/5/1 [189 137 141 138] /Table/53/1/5/2 test t · {1,2,3} 1
12 [189 137 141 138] /Table/53/1/5/2 [189 137 141 139] /Table/53/1/5/3 test t · {2,3,5} 5
13 [189 137 141 139] /Table/53/1/5/3 [189 137 143 144 254 190 137 145] /Table/53/1/7/8/#/54/1/9 test t · {1,2,4} 4
14 [189 137 143 144 254 190 137 145] /Table/53/1/7/8/#/54/1/9 [189 137 146] /Table/53/1/10 test t · {1,2,4} 4
3 [189 137 146] /Table/53/1/10 [189 137 147] /Table/53/1/11 test t · {1} 1
8 [189 137 147] /Table/53/1/11 [189 137 151 152 254 191 138] /Table/53/1/15/16/#/55/2 test t · {1} 1
9 [189 137 151 152 254 191 138] /Table/53/1/15/16/#/55/2 [189 138 144] /Table/53/2/8 test t · {1} 1
Expand Down

0 comments on commit daf82ad

Please sign in to comment.