-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Tool to determine mapping from vindex and value to shard #17290
Merged
rohit-nayak-ps
merged 10 commits into
vitessio:main
from
planetscale:rohit/shard-from-ids
Nov 29, 2024
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7de1507
New tool to get the shard which would be routed to, for a specific vi…
rohit-nayak-ps 0ad3578
Some refactor and improvements
rohit-nayak-ps 03ec401
Add docs. Default vindex is now xxhash
rohit-nayak-ps 772bf39
Fixed setting of Shard start/end which resulted in wrong shards being…
rohit-nayak-ps 3af6cc8
Self-review
rohit-nayak-ps 201a033
goimports
rohit-nayak-ps ad73582
Compute allShards once
rohit-nayak-ps 9937aa8
Address review comments. Add unit test
rohit-nayak-ps 1ad2d64
goimports
rohit-nayak-ps 98ee23c
Remove binary added by mistake
rohit-nayak-ps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Copyright 2024 The Vitess Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
build: | ||
go build map-shard-for-value.go | ||
|
||
test: | ||
echo "1\n-1\n99" | go run map-shard-for-value.go --total_shards=4 --vindex=xxhash | ||
|
||
clean: | ||
rm -f map-shard-for-value |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"encoding/hex" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
flag "github.com/spf13/pflag" | ||
|
||
"vitess.io/vitess/go/vt/topo" | ||
|
||
"vitess.io/vitess/go/sqltypes" | ||
"vitess.io/vitess/go/vt/key" | ||
"vitess.io/vitess/go/vt/proto/topodata" | ||
"vitess.io/vitess/go/vt/vtgate/vindexes" | ||
) | ||
|
||
/* | ||
* This tool reads a list of values from stdin and prints the | ||
* corresponding keyspace ID and shard for each value. It uses the given vindex | ||
* and shard ranges to determine the shard. The vindex is expected to be a | ||
* single-column vindex. The shard ranges are specified as a comma-separated | ||
* list of key ranges, example "-80,80-". | ||
* If you have uniformly distributed shards, you can specify the total number | ||
* of shards using the -total_shards flag, and the tool will generate the shard ranges | ||
* using the same logic as the Vitess operator does (using the key.GenerateShardRanges() function). | ||
* | ||
* Example usage: | ||
* echo "1\n2\n3" | go run shard-from-id.go -vindex=hash -shards=-80,80- | ||
* | ||
* Currently tested only for integer values and hash/xxhash vindexes. | ||
*/ | ||
|
||
func mapShard(allShards []*topodata.ShardReference, ksid key.DestinationKeyspaceID) (string, error) { | ||
foundShard := "" | ||
addShard := func(shard string) error { | ||
foundShard = shard | ||
return nil | ||
} | ||
if err := ksid.Resolve(allShards, addShard); err != nil { | ||
return "", fmt.Errorf("failed to resolve keyspace ID: %v:: %s", ksid.String(), err) | ||
} | ||
|
||
if foundShard == "" { | ||
return "", fmt.Errorf("no shard found for keyspace ID: %v", ksid) | ||
} | ||
return foundShard, nil | ||
} | ||
|
||
func selectShard(vindex vindexes.Vindex, value sqltypes.Value, allShards []*topodata.ShardReference) (string, key.DestinationKeyspaceID, error) { | ||
ctx := context.Background() | ||
|
||
destinations, err := vindexes.Map(ctx, vindex, nil, [][]sqltypes.Value{{value}}) | ||
if err != nil { | ||
return "", nil, fmt.Errorf("failed to map value to keyspace ID: %w", err) | ||
} | ||
|
||
if len(destinations) != 1 { | ||
return "", nil, fmt.Errorf("unexpected number of destinations: %d", len(destinations)) | ||
} | ||
|
||
ksid, ok := destinations[0].(key.DestinationKeyspaceID) | ||
if !ok { | ||
return "", nil, fmt.Errorf("unexpected destination type: %T", destinations[0]) | ||
} | ||
|
||
foundShard, err := mapShard(allShards, ksid) | ||
if err != nil { | ||
return "", nil, fmt.Errorf("failed to map shard, original value %v, keyspace id %s: %w", value, ksid, err) | ||
} | ||
return foundShard, ksid, nil | ||
} | ||
|
||
func getValue(valueStr, valueType string) (sqltypes.Value, error) { | ||
var value sqltypes.Value | ||
|
||
switch valueType { | ||
case "int": | ||
valueInt, err := strconv.ParseInt(valueStr, 10, 64) | ||
if err != nil { | ||
return value, fmt.Errorf("failed to parse int value: %w", err) | ||
} | ||
value = sqltypes.NewInt64(valueInt) | ||
case "uint": | ||
valueUint, err := strconv.ParseUint(valueStr, 10, 64) | ||
if err != nil { | ||
return value, fmt.Errorf("failed to parse uint value: %w", err) | ||
} | ||
value = sqltypes.NewUint64(valueUint) | ||
case "string": | ||
value = sqltypes.NewVarChar(valueStr) | ||
default: | ||
return value, fmt.Errorf("unsupported value type: %s", valueType) | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
func getShardMap(shardsCSV *string) []*topodata.ShardReference { | ||
var allShards []*topodata.ShardReference | ||
|
||
for _, shard := range strings.Split(*shardsCSV, ",") { | ||
_, keyRange, err := topo.ValidateShardName(shard) | ||
if err != nil { | ||
log.Fatalf("invalid shard range: %s", shard) | ||
} | ||
allShards = append(allShards, &topodata.ShardReference{ | ||
Name: shard, | ||
KeyRange: keyRange, | ||
}) | ||
} | ||
return allShards | ||
} | ||
|
||
type output struct { | ||
Value string | ||
KeyspaceID string | ||
Shard string | ||
} | ||
|
||
func processValues(scanner *bufio.Scanner, shardsCSV *string, vindexName string, valueType string) ([]output, error) { | ||
allShards := getShardMap(shardsCSV) | ||
|
||
vindex, err := vindexes.CreateVindex(vindexName, vindexName, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create vindex: %v", err) | ||
} | ||
var outputs []output | ||
for scanner.Scan() { | ||
valueStr := scanner.Text() | ||
if valueStr == "" { | ||
continue | ||
} | ||
value, err := getValue(valueStr, valueType) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get value for: %v, value_type %s:: %v", valueStr, valueType, err) | ||
} | ||
shard, ksid, err := selectShard(vindex, value, allShards) | ||
if err != nil { | ||
// ignore errors so that we can go ahead with the computation for other values | ||
continue | ||
} | ||
outputs = append(outputs, output{Value: valueStr, KeyspaceID: hex.EncodeToString(ksid), Shard: shard}) | ||
} | ||
return outputs, nil | ||
} | ||
|
||
func printOutput(outputs []output) { | ||
fmt.Println("value,keyspaceID,shard") | ||
for _, output := range outputs { | ||
fmt.Printf("%s,%s,%s\n", output.Value, output.KeyspaceID, output.Shard) | ||
} | ||
} | ||
|
||
func main() { | ||
// Explicitly configuring the logger since it was flaky in displaying logs locally without this. | ||
log.SetOutput(os.Stderr) | ||
log.SetFlags(log.LstdFlags) | ||
log.SetPrefix("LOG: ") | ||
|
||
vindexName := flag.String("vindex", "xxhash", "name of the vindex") | ||
shardsCSV := flag.String("shards", "", "comma-separated list of shard ranges") | ||
totalShards := flag.Int("total_shards", 0, "total number of uniformly distributed shards") | ||
valueType := flag.String("value_type", "int", "type of the value (int, uint, or string)") | ||
flag.Parse() | ||
|
||
if *totalShards > 0 { | ||
if *shardsCSV != "" { | ||
log.Fatalf("cannot specify both total_shards and shards") | ||
} | ||
shardArr, err := key.GenerateShardRanges(*totalShards) | ||
if err != nil { | ||
log.Fatalf("failed to generate shard ranges: %v", err) | ||
} | ||
*shardsCSV = strings.Join(shardArr, ",") | ||
} | ||
if *shardsCSV == "" { | ||
log.Fatal("shards or total_shards must be specified") | ||
} | ||
scanner := bufio.NewScanner(os.Stdin) | ||
outputs, err := processValues(scanner, shardsCSV, *vindexName, *valueType) | ||
if err != nil { | ||
log.Fatalf("failed to process values: %v", err) | ||
} | ||
printOutput(outputs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
## Map Shard for Value Tool | ||
|
||
### Overview | ||
|
||
The `map-shard-for-value` tool maps a given value to a specific shard. This tool helps in determining | ||
which shard a particular value belongs to, based on the vindex algorithm and shard ranges. | ||
|
||
### Features | ||
- | ||
|
||
- Allows specifying the vindex type (e.g., `hash`, `xxhash`). | ||
- Allows specifying the shard list of (for uniformly distributed shard ranges) the total number of shards to generate. | ||
- Designed as a _filter_: Reads input values from `stdin` and outputs the corresponding shard information, so it can be | ||
used to map values from a file or another program. | ||
|
||
### Usage | ||
|
||
```sh | ||
make build | ||
``` | ||
|
||
```sh | ||
echo "1\n-1\n99" | ./map-shard-for-value --total_shards=4 --vindex=xxhash | ||
value,keyspaceID,shard | ||
1,d46405367612b4b7,c0- | ||
-1,d8e2a6a7c8c7623d,c0- | ||
99,200533312244abca,-40 | ||
|
||
echo "1\n-1\n99" | ./map-shard-for-value --vindex=hash --shards="-80,80-" | ||
value,keyspaceID,shard | ||
1,166b40b44aba4bd6,-80 | ||
-1,355550b2150e2451,-80 | ||
99,2c40ad56f4593c47,-80 | ||
``` | ||
|
||
#### Flags | ||
|
||
- `--vindex`: Specifies the name of the vindex to use (e.g., `hash`, `xxhash`) (default `xxhash`) | ||
|
||
One (and only one) of these is required: | ||
|
||
- `--shards`: Comma-separated list of shard ranges | ||
- `--total_shards`: Total number of shards, only if shards are uniformly distributed | ||
|
||
Optional: | ||
- `--value_type`: Type of the value to map, one of int, uint, string (default `int`) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestProcess(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
shardsCSV string | ||
vindexType string | ||
values []int | ||
valueType string | ||
expected []output | ||
} | ||
testCases := []testCase{ | ||
{ | ||
name: "hash,2 shards", | ||
shardsCSV: "-80,80-", | ||
vindexType: "hash", | ||
values: []int{1, 99}, | ||
valueType: "int", | ||
expected: []output{ | ||
{ | ||
Value: "1", | ||
KeyspaceID: "166b40b44aba4bd6", | ||
Shard: "-80", | ||
}, | ||
{ | ||
Value: "99", | ||
KeyspaceID: "2c40ad56f4593c47", | ||
Shard: "-80", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "xxhash,4 shards", | ||
shardsCSV: "-40,40-80,80-c0,c0-", | ||
vindexType: "xxhash", | ||
values: []int{1, 99}, | ||
valueType: "int", | ||
expected: []output{ | ||
{ | ||
Value: "1", | ||
KeyspaceID: "d46405367612b4b7", | ||
Shard: "c0-", | ||
}, | ||
{ | ||
Value: "99", | ||
KeyspaceID: "200533312244abca", | ||
Shard: "-40", | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var input strings.Builder | ||
for _, num := range tc.values { | ||
fmt.Fprintf(&input, "%d\n", num) | ||
} | ||
reader := strings.NewReader(input.String()) | ||
scanner := bufio.NewScanner(reader) | ||
got, err := processValues(scanner, &tc.shardsCSV, tc.vindexType, tc.valueType) | ||
require.NoError(t, err) | ||
require.EqualValues(t, tc.expected, got) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this file can be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, not sure how that got committed. Good catch, removed!