Skip to content
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
30 changes: 26 additions & 4 deletions go/vt/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,30 @@ func KeyRangeEqual(left, right *topodatapb.KeyRange) bool {
if right == nil {
return len(left.Start) == 0 && len(left.End) == 0
}
return bytes.Equal(left.Start, right.Start) &&
bytes.Equal(left.End, right.End)
return bytes.Equal(addPadding(left.Start), addPadding(right.Start)) &&
bytes.Equal(addPadding(left.End), addPadding(right.End))
}

// addPadding adds padding to make sure keyrange represents an 8 byte integer.
// From Vitess docs:
// A hash vindex produces an 8-byte number.
// This means that all numbers less than 0x8000000000000000 will fall in shard -80.
// Any number with the highest bit set will be >= 0x8000000000000000, and will therefore
// belong to shard 80-.
// This means that from a keyrange perspective -80 == 00-80 == 0000-8000 == 000000-800000
// If we don't add this padding, we could run into issues when transitioning from keyranges
// that use 2 bytes to 4 bytes.
func addPadding(kr []byte) []byte {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it's converting a key from a "short" version into something normalized? It might make sense to expose this as a function on the KeyRange struct or as a (KeyRange) -> KeyRange then make that an exported function from the package.

I don't think this is a blocking comment; this PR seems safe/good to merge is.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The (very reasonable) counterpoint here is that it's a bit late to start talking about a normalized keyrange format; i don't really know if it's worth trying to go down that road at this point unless this comes up again.

paddedKr := make([]byte, 8)

for i := 0; i < len(kr); i++ {
paddedKr = append(paddedKr, kr[i])
}

for i := len(kr); i < 8; i++ {
paddedKr = append(paddedKr, 0)
}
return paddedKr
}

// KeyRangeStartSmaller returns true if right's keyrange start is _after_ left's start
Expand All @@ -217,7 +239,7 @@ func KeyRangeStartEqual(left, right *topodatapb.KeyRange) bool {
if right == nil {
return len(left.Start) == 0
}
return bytes.Equal(left.Start, right.Start)
return bytes.Equal(addPadding(left.Start), addPadding(right.Start))
}

// KeyRangeEndEqual returns true if both key ranges have the same end
Expand All @@ -228,7 +250,7 @@ func KeyRangeEndEqual(left, right *topodatapb.KeyRange) bool {
if right == nil {
return len(left.End) == 0
}
return bytes.Equal(left.End, right.End)
return bytes.Equal(addPadding(left.End), addPadding(right.End))
}

// For more info on the following functions, see:
Expand Down
161 changes: 161 additions & 0 deletions go/vt/key/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,167 @@ func TestKeyRangeAdd(t *testing.T) {
}
}

func TestKeyRangeEndEqual(t *testing.T) {
testcases := []struct {
first string
second string
out bool
}{{
first: "",
second: "",
out: true,
}, {
first: "",
second: "-80",
out: false,
}, {
first: "40-",
second: "10-",
out: true,
}, {
first: "-8000",
second: "-80",
out: true,
}, {
first: "-8000",
second: "-8000000000000000",
out: true,
}, {
first: "-80",
second: "-8000",
out: true,
}}
stringToKeyRange := func(spec string) *topodatapb.KeyRange {
if spec == "" {
return nil
}
parts := strings.Split(spec, "-")
if len(parts) != 2 {
panic("invalid spec")
}
kr, err := ParseKeyRangeParts(parts[0], parts[1])
if err != nil {
panic(err)
}
return kr
}

for _, tcase := range testcases {
first := stringToKeyRange(tcase.first)
second := stringToKeyRange(tcase.second)
out := KeyRangeEndEqual(first, second)
if out != tcase.out {
t.Fatalf("KeyRangeEndEqual(%q, %q) expected %t, got %t", tcase.first, tcase.second, tcase.out, out)
}
}
}

func TestKeyRangeStartEqual(t *testing.T) {
testcases := []struct {
first string
second string
out bool
}{{
first: "",
second: "",
out: true,
}, {
first: "",
second: "-80",
out: true,
}, {
first: "40-",
second: "20-",
out: false,
}, {
first: "-8000",
second: "-80",
out: true,
}, {
first: "-8000",
second: "-8000000000000000",
out: true,
}, {
first: "-80",
second: "-8000",
out: true,
}}
stringToKeyRange := func(spec string) *topodatapb.KeyRange {
if spec == "" {
return nil
}
parts := strings.Split(spec, "-")
if len(parts) != 2 {
panic("invalid spec")
}
kr, err := ParseKeyRangeParts(parts[0], parts[1])
if err != nil {
panic(err)
}
return kr
}

for _, tcase := range testcases {
first := stringToKeyRange(tcase.first)
second := stringToKeyRange(tcase.second)
out := KeyRangeStartEqual(first, second)
if out != tcase.out {
t.Fatalf("KeyRangeStartEqual(%q, %q) expected %t, got %t", tcase.first, tcase.second, tcase.out, out)
}
}
}

func TestKeyRangeEqual(t *testing.T) {
testcases := []struct {
first string
second string
out bool
}{{
first: "",
second: "",
out: true,
}, {
first: "",
second: "-80",
out: false,
}, {
first: "-8000",
second: "-80",
out: true,
}, {
first: "-8000",
second: "-8000000000000000",
out: true,
}, {
first: "-80",
second: "-8000",
out: true,
}}
stringToKeyRange := func(spec string) *topodatapb.KeyRange {
if spec == "" {
return nil
}
parts := strings.Split(spec, "-")
if len(parts) != 2 {
panic("invalid spec")
}
kr, err := ParseKeyRangeParts(parts[0], parts[1])
if err != nil {
panic(err)
}
return kr
}

for _, tcase := range testcases {
first := stringToKeyRange(tcase.first)
second := stringToKeyRange(tcase.second)
out := KeyRangeEqual(first, second)
if out != tcase.out {
t.Fatalf("KeyRangeEqual(%q, %q) expected %t, got %t", tcase.first, tcase.second, tcase.out, out)
}
}
}

func TestEvenShardsKeyRange_Error(t *testing.T) {
testCases := []struct {
i, n int
Expand Down
12 changes: 12 additions & 0 deletions go/vt/topotools/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ func TestValidateForReshard(t *testing.T) {
sources: []string{"-80", "80-"},
targets: []string{"-40", "40-"},
out: "",
}, {
sources: []string{"52-53"},
targets: []string{"5200-5240", "5240-5280", "5280-52c0", "52c0-5300"},
out: "",
}, {
sources: []string{"5200-5300"},
targets: []string{"520000-524000", "524000-528000", "528000-52c000", "52c000-530000"},
out: "",
}, {
sources: []string{"-80", "80-"},
targets: []string{"-4000000000000000", "4000000000000000-8000000000000000", "8000000000000000-80c0000000000000", "80c0000000000000-"},
out: "",
}, {
sources: []string{"80-", "-80"},
targets: []string{"-40", "40-"},
Expand Down