Skip to content

Commit 6282e16

Browse files
authored
Merge pull request #723 from planetscale/florent-hex-width-shard-range
allow specifying the hex width to use when generating shard ranges
2 parents 76cf5da + 61201eb commit 6282e16

File tree

12 files changed

+114
-23
lines changed

12 files changed

+114
-23
lines changed

deploy/crds/planetscale.com_vitessclusters.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,12 @@ spec:
21402140
type: object
21412141
equal:
21422142
properties:
2143+
hexWidth:
2144+
default: 0
2145+
format: int32
2146+
maximum: 65536
2147+
minimum: 0
2148+
type: integer
21432149
parts:
21442150
format: int32
21452151
maximum: 65536

deploy/crds/planetscale.com_vitesskeyspaces.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,12 @@ spec:
681681
type: object
682682
equal:
683683
properties:
684+
hexWidth:
685+
default: 0
686+
format: int32
687+
maximum: 65536
688+
minimum: 0
689+
type: integer
684690
parts:
685691
format: int32
686692
maximum: 65536

docs/api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5806,6 +5806,18 @@ migration, and then remove the old partitioning.</p>
58065806
</tr>
58075807
<tr>
58085808
<td>
5809+
<code>hexWidth</code><br>
5810+
<em>
5811+
int32
5812+
</em>
5813+
</td>
5814+
<td>
5815+
<p>HexWidth is the number of hex characters to use for the shard range start and end.
5816+
If not set or set to 0, it will be automatically computed based on the number of requested shards.</p>
5817+
</td>
5818+
</tr>
5819+
<tr>
5820+
<td>
58095821
<code>shardTemplate</code><br>
58105822
<em>
58115823
<a href="#planetscale.com/v2.VitessShardTemplate">

docs/api/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5808,6 +5808,18 @@ <h3 id="planetscale.com/v2.VitessKeyspaceEqualPartitioning">VitessKeyspaceEqualP
58085808
</tr>
58095809
<tr>
58105810
<td>
5811+
<code>hexWidth</code><br>
5812+
<em>
5813+
int32
5814+
</em>
5815+
</td>
5816+
<td>
5817+
<p>HexWidth is the number of hex characters to use for the shard range start and end.
5818+
If not set or set to 0, it will be automatically computed based on the number of requested shards.</p>
5819+
</td>
5820+
</tr>
5821+
<tr>
5822+
<td>
58115823
<code>shardTemplate</code><br>
58125824
<em>
58135825
<a href="#planetscale.com/v2.VitessShardTemplate">

pkg/apis/planetscale/v2/vitesskeyspace_methods.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
func (p *VitessKeyspaceEqualPartitioning) KeyRanges() []VitessKeyRange {
3535
// Invariant: number of parts must be between 1-65536. This is enforced via
3636
// the CRD.
37-
ranges, err := key.GenerateShardRanges(int(p.Parts), 0)
37+
ranges, err := key.GenerateShardRanges(int(p.Parts), int(p.HexWidth))
3838
if err != nil {
3939
panic(fmt.Sprintf("could not generate shard range with %d parts: %v", p.Parts, err))
4040
}

pkg/apis/planetscale/v2/vitesskeyspace_methods_test.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import (
2323

2424
func TestTranslationToVitessKeyRange(t *testing.T) {
2525
table := []struct {
26-
parts int32
27-
want []VitessKeyRange
26+
hexWidth int32
27+
parts int32
28+
want []VitessKeyRange
2829
}{
2930
{
3031
parts: 1,
@@ -62,10 +63,40 @@ func TestTranslationToVitessKeyRange(t *testing.T) {
6263
{"e0", ""},
6364
},
6465
},
66+
{
67+
hexWidth: 4,
68+
parts: 7,
69+
want: []VitessKeyRange{
70+
{"", "2492"},
71+
{"2492", "4924"},
72+
{"4924", "6db6"},
73+
{"6db6", "9249"},
74+
{"9249", "b6db"},
75+
{"b6db", "db6d"},
76+
{"db6d", ""},
77+
},
78+
},
79+
{
80+
hexWidth: 4,
81+
parts: 8,
82+
want: []VitessKeyRange{
83+
{"", "2000"},
84+
{"2000", "4000"},
85+
{"4000", "6000"},
86+
{"6000", "8000"},
87+
{"8000", "a000"},
88+
{"a000", "c000"},
89+
{"c000", "e000"},
90+
{"e000", ""},
91+
},
92+
},
6593
}
6694

6795
for _, test := range table {
68-
p := VitessKeyspaceEqualPartitioning{Parts: test.parts}
96+
p := VitessKeyspaceEqualPartitioning{
97+
HexWidth: test.hexWidth,
98+
Parts: test.parts,
99+
}
69100
if got, want := p.KeyRanges(), test.want; !reflect.DeepEqual(got, want) {
70101
t.Errorf("KeyRanges(%v) = %#v; want %#v", test.parts, got, want)
71102
}

pkg/apis/planetscale/v2/vitesskeyspace_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,13 @@ type VitessKeyspaceEqualPartitioning struct {
357357
// +kubebuilder:validation:Maximum=65536
358358
Parts int32 `json:"parts"`
359359

360+
// HexWidth is the number of hex characters to use for the shard range start and end.
361+
// If not set or set to 0, it will be automatically computed based on the number of requested shards.
362+
// +kubebuilder:default=0
363+
// +kubebuilder:validation:Minimum=0
364+
// +kubebuilder:validation:Maximum=65536
365+
HexWidth int32 `json:"hexWidth,omitempty"`
366+
360367
// ShardTemplate is the configuration used for each equal-sized shard.
361368
// If you need shards that don't all share the same configuration,
362369
// use custom partitioning instead.

test/endtoend/operator/302_new_shards.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ spec:
140140
storage: 10Gi
141141
- equal:
142142
parts: 2
143+
hexWidth: 4
143144
shardTemplate:
144145
databaseInitScriptSecret:
145146
name: example-cluster-config

test/endtoend/operator/306_down_shard_0.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ spec:
105105
partitionings:
106106
- equal:
107107
parts: 2
108+
hexWidth: 4
108109
shardTemplate:
109110
databaseInitScriptSecret:
110111
name: example-cluster-config

test/endtoend/operator/401_scheduled_backups.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ spec:
4040
strategies:
4141
- name: customer_80-x
4242
keyspace: "customer"
43-
shard: "80-"
43+
shard: "8000-"
4444
- name: customer_x-80
4545
keyspace: "customer"
46-
shard: "-80"
46+
shard: "-8000"
4747
images:
4848
vtctld: vitess/lite:mysql80
4949
vtgate: vitess/lite:mysql80
@@ -139,6 +139,7 @@ spec:
139139
partitionings:
140140
- equal:
141141
parts: 2
142+
hexWidth: 4
142143
shardTemplate:
143144
databaseInitScriptSecret:
144145
name: example-cluster-config

0 commit comments

Comments
 (0)