|
| 1 | +/* |
| 2 | +Copyright 2024 The Vitess Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "bufio" |
| 21 | + "context" |
| 22 | + "encoding/hex" |
| 23 | + "fmt" |
| 24 | + "log" |
| 25 | + "os" |
| 26 | + "strconv" |
| 27 | + "strings" |
| 28 | + |
| 29 | + flag "github.com/spf13/pflag" |
| 30 | + |
| 31 | + "vitess.io/vitess/go/vt/topo" |
| 32 | + |
| 33 | + "vitess.io/vitess/go/sqltypes" |
| 34 | + "vitess.io/vitess/go/vt/key" |
| 35 | + "vitess.io/vitess/go/vt/proto/topodata" |
| 36 | + "vitess.io/vitess/go/vt/vtgate/vindexes" |
| 37 | +) |
| 38 | + |
| 39 | +/* |
| 40 | + * This tool reads a list of values from stdin and prints the |
| 41 | + * corresponding keyspace ID and shard for each value. It uses the given vindex |
| 42 | + * and shard ranges to determine the shard. The vindex is expected to be a |
| 43 | + * single-column vindex. The shard ranges are specified as a comma-separated |
| 44 | + * list of key ranges, example "-80,80-". |
| 45 | + * If you have uniformly distributed shards, you can specify the total number |
| 46 | + * of shards using the -total_shards flag, and the tool will generate the shard ranges |
| 47 | + * using the same logic as the Vitess operator does (using the key.GenerateShardRanges() function). |
| 48 | + * |
| 49 | + * Example usage: |
| 50 | + * echo "1\n2\n3" | go run shard-from-id.go -vindex=hash -shards=-80,80- |
| 51 | + * |
| 52 | + * Currently tested only for integer values and hash/xxhash vindexes. |
| 53 | + */ |
| 54 | + |
| 55 | +func mapShard(allShards []*topodata.ShardReference, ksid key.DestinationKeyspaceID) (string, error) { |
| 56 | + foundShard := "" |
| 57 | + addShard := func(shard string) error { |
| 58 | + foundShard = shard |
| 59 | + return nil |
| 60 | + } |
| 61 | + if err := ksid.Resolve(allShards, addShard); err != nil { |
| 62 | + return "", fmt.Errorf("failed to resolve keyspace ID: %v:: %s", ksid.String(), err) |
| 63 | + } |
| 64 | + |
| 65 | + if foundShard == "" { |
| 66 | + return "", fmt.Errorf("no shard found for keyspace ID: %v", ksid) |
| 67 | + } |
| 68 | + return foundShard, nil |
| 69 | +} |
| 70 | + |
| 71 | +func selectShard(vindex vindexes.Vindex, value sqltypes.Value, allShards []*topodata.ShardReference) (string, key.DestinationKeyspaceID, error) { |
| 72 | + ctx := context.Background() |
| 73 | + |
| 74 | + destinations, err := vindexes.Map(ctx, vindex, nil, [][]sqltypes.Value{{value}}) |
| 75 | + if err != nil { |
| 76 | + return "", nil, fmt.Errorf("failed to map value to keyspace ID: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + if len(destinations) != 1 { |
| 80 | + return "", nil, fmt.Errorf("unexpected number of destinations: %d", len(destinations)) |
| 81 | + } |
| 82 | + |
| 83 | + ksid, ok := destinations[0].(key.DestinationKeyspaceID) |
| 84 | + if !ok { |
| 85 | + return "", nil, fmt.Errorf("unexpected destination type: %T", destinations[0]) |
| 86 | + } |
| 87 | + |
| 88 | + foundShard, err := mapShard(allShards, ksid) |
| 89 | + if err != nil { |
| 90 | + return "", nil, fmt.Errorf("failed to map shard, original value %v, keyspace id %s: %w", value, ksid, err) |
| 91 | + } |
| 92 | + return foundShard, ksid, nil |
| 93 | +} |
| 94 | + |
| 95 | +func getValue(valueStr, valueType string) (sqltypes.Value, error) { |
| 96 | + var value sqltypes.Value |
| 97 | + |
| 98 | + switch valueType { |
| 99 | + case "int": |
| 100 | + valueInt, err := strconv.ParseInt(valueStr, 10, 64) |
| 101 | + if err != nil { |
| 102 | + return value, fmt.Errorf("failed to parse int value: %w", err) |
| 103 | + } |
| 104 | + value = sqltypes.NewInt64(valueInt) |
| 105 | + case "uint": |
| 106 | + valueUint, err := strconv.ParseUint(valueStr, 10, 64) |
| 107 | + if err != nil { |
| 108 | + return value, fmt.Errorf("failed to parse uint value: %w", err) |
| 109 | + } |
| 110 | + value = sqltypes.NewUint64(valueUint) |
| 111 | + case "string": |
| 112 | + value = sqltypes.NewVarChar(valueStr) |
| 113 | + default: |
| 114 | + return value, fmt.Errorf("unsupported value type: %s", valueType) |
| 115 | + } |
| 116 | + |
| 117 | + return value, nil |
| 118 | +} |
| 119 | + |
| 120 | +func getShardMap(shardsCSV *string) []*topodata.ShardReference { |
| 121 | + var allShards []*topodata.ShardReference |
| 122 | + |
| 123 | + for _, shard := range strings.Split(*shardsCSV, ",") { |
| 124 | + _, keyRange, err := topo.ValidateShardName(shard) |
| 125 | + if err != nil { |
| 126 | + log.Fatalf("invalid shard range: %s", shard) |
| 127 | + } |
| 128 | + allShards = append(allShards, &topodata.ShardReference{ |
| 129 | + Name: shard, |
| 130 | + KeyRange: keyRange, |
| 131 | + }) |
| 132 | + } |
| 133 | + return allShards |
| 134 | +} |
| 135 | + |
| 136 | +type output struct { |
| 137 | + Value string |
| 138 | + KeyspaceID string |
| 139 | + Shard string |
| 140 | +} |
| 141 | + |
| 142 | +func processValues(scanner *bufio.Scanner, shardsCSV *string, vindexName string, valueType string) ([]output, error) { |
| 143 | + allShards := getShardMap(shardsCSV) |
| 144 | + |
| 145 | + vindex, err := vindexes.CreateVindex(vindexName, vindexName, nil) |
| 146 | + if err != nil { |
| 147 | + return nil, fmt.Errorf("failed to create vindex: %v", err) |
| 148 | + } |
| 149 | + var outputs []output |
| 150 | + for scanner.Scan() { |
| 151 | + valueStr := scanner.Text() |
| 152 | + if valueStr == "" { |
| 153 | + continue |
| 154 | + } |
| 155 | + value, err := getValue(valueStr, valueType) |
| 156 | + if err != nil { |
| 157 | + return nil, fmt.Errorf("failed to get value for: %v, value_type %s:: %v", valueStr, valueType, err) |
| 158 | + } |
| 159 | + shard, ksid, err := selectShard(vindex, value, allShards) |
| 160 | + if err != nil { |
| 161 | + // ignore errors so that we can go ahead with the computation for other values |
| 162 | + continue |
| 163 | + } |
| 164 | + outputs = append(outputs, output{Value: valueStr, KeyspaceID: hex.EncodeToString(ksid), Shard: shard}) |
| 165 | + } |
| 166 | + return outputs, nil |
| 167 | +} |
| 168 | + |
| 169 | +func printOutput(outputs []output) { |
| 170 | + fmt.Println("value,keyspaceID,shard") |
| 171 | + for _, output := range outputs { |
| 172 | + fmt.Printf("%s,%s,%s\n", output.Value, output.KeyspaceID, output.Shard) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func main() { |
| 177 | + // Explicitly configuring the logger since it was flaky in displaying logs locally without this. |
| 178 | + log.SetOutput(os.Stderr) |
| 179 | + log.SetFlags(log.LstdFlags) |
| 180 | + log.SetPrefix("LOG: ") |
| 181 | + |
| 182 | + vindexName := flag.String("vindex", "xxhash", "name of the vindex") |
| 183 | + shardsCSV := flag.String("shards", "", "comma-separated list of shard ranges") |
| 184 | + totalShards := flag.Int("total_shards", 0, "total number of uniformly distributed shards") |
| 185 | + valueType := flag.String("value_type", "int", "type of the value (int, uint, or string)") |
| 186 | + flag.Parse() |
| 187 | + |
| 188 | + if *totalShards > 0 { |
| 189 | + if *shardsCSV != "" { |
| 190 | + log.Fatalf("cannot specify both total_shards and shards") |
| 191 | + } |
| 192 | + shardArr, err := key.GenerateShardRanges(*totalShards) |
| 193 | + if err != nil { |
| 194 | + log.Fatalf("failed to generate shard ranges: %v", err) |
| 195 | + } |
| 196 | + *shardsCSV = strings.Join(shardArr, ",") |
| 197 | + } |
| 198 | + if *shardsCSV == "" { |
| 199 | + log.Fatal("shards or total_shards must be specified") |
| 200 | + } |
| 201 | + scanner := bufio.NewScanner(os.Stdin) |
| 202 | + outputs, err := processValues(scanner, shardsCSV, *vindexName, *valueType) |
| 203 | + if err != nil { |
| 204 | + log.Fatalf("failed to process values: %v", err) |
| 205 | + } |
| 206 | + printOutput(outputs) |
| 207 | +} |
0 commit comments