Skip to content
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
merged 10 commits into from
Nov 29, 2024

Conversation

rohit-nayak-ps
Copy link
Contributor

@rohit-nayak-ps rohit-nayak-ps commented Nov 27, 2024

Description

This is a separate tool in the vitess tools directory which can be used as a separate binary without needing to setup a cluster. It can be used to map a list of values from the stdin and outputs a csv format giving you the related keyspace id and the shard in which it will reside. The inputs are the vindex type and the sharding scheme.

I tested this by comparing about 2K results of vexplain by specifying a test vschema file like vtexplain --shards 128 --vschema-file test-vschema.json --sql "select * from users where id = $1" --schema-file "test-schema.sql" --output-mode json | jq -r '.[].TabletActions | keys[] | split("/") | .[1]' | awk -v id="$id" '{print id "," $0}' .

Features

  • Allows specifying the vindex type (e.g., hash, xxhash).
  • Allows specifying the full shard list, as a CSV, OR (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

make build
echo "1\n-1\n99" | ./map-shard-for-value --total_shards=4 --vindex=xxhash
value,ksid,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,ksid,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

Related Issue(s)

#17292

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

…ndex and shard ranges

Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Copy link
Contributor

vitess-bot bot commented Nov 27, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Nov 27, 2024
@github-actions github-actions bot added this to the v22.0.0 milestone Nov 27, 2024
@rohit-nayak-ps rohit-nayak-ps added Type: Feature Component: Cluster management and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Nov 27, 2024
… reported for a keyspace id

Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Copy link

codecov bot commented Nov 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 67.39%. Comparing base (8648264) to head (98ee23c).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17290      +/-   ##
==========================================
- Coverage   67.40%   67.39%   -0.02%     
==========================================
  Files        1574     1574              
  Lines      253205   253251      +46     
==========================================
- Hits       170676   170673       -3     
- Misses      82529    82578      +49     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
@rohit-nayak-ps rohit-nayak-ps removed NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request labels Nov 28, 2024
@rohit-nayak-ps rohit-nayak-ps marked this pull request as ready for review November 28, 2024 09:52
@harshit-gangal
Copy link
Member

Do we have to print the ksid?
It does not seem user friendly.

Comment on lines 100 to 114
func getValue(valueStr string) (sqltypes.Value, int64, error) {
var value sqltypes.Value
valueInt, err := strconv.ParseInt(valueStr, 10, 64)
if err == nil {
value = sqltypes.NewInt64(int64(valueInt))
} else {
valueUint, err := strconv.ParseUint(valueStr, 10, 64)
if err == nil {
value = sqltypes.NewUint64(valueUint)
} else {
value = sqltypes.NewVarChar(valueStr)
}
}
return value, valueInt, err
}
Copy link
Member

Choose a reason for hiding this comment

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

we should take value type as input using cli

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 59 to 65
allShards := make([]*topodata.ShardReference, 0, len(shards))
for shard, keyRange := range shards {
allShards = append(allShards, &topodata.ShardReference{
Name: shard,
KeyRange: keyRange,
})
}
Copy link
Member

Choose a reason for hiding this comment

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

This can be done as part of a struct init and passed along to methods

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 93 to 96
foundShard, err := mapShard(shards, ksid)
if err != nil {
return "", nil, fmt.Errorf("failed to map shard: %w", err)
}
Copy link
Member

Choose a reason for hiding this comment

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

we should add the original value as well to the error message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 121 to 133
var start, end []byte
if len(parts) > 0 && parts[0] != "" {
start, err = hex.DecodeString(parts[0])
if err != nil {
log.Fatalf("failed to decode shard start: %v", err)
}
}
if len(parts) > 1 && parts[1] != "" {
end, err = hex.DecodeString(parts[1])
if err != nil {
log.Fatalf("failed to decode shard end: %v", err)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

can use method key.ParseKeyRangeParts to create *topodatapb.KeyRange

Copy link
Member

Choose a reason for hiding this comment

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

You can also look at topo.ValidateShardName method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Used topo.ValidateShardName

@mcrauwel
Copy link
Contributor

Do we have to print the ksid? It does not seem user friendly.

certainly not needed, but it doesn't hurt, easy enough to work around like this:

echo "1
2
3
4
5
6
7
8
9
10" | ./map-shard-for-value --vindex xxhash --total_shards 4 | awk -F',' '{ print $3, $1}'

Copy link
Member

@frouioui frouioui left a comment

Choose a reason for hiding this comment

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

I think it would make sense to move this to vt. We should also add some unit tests, seems like they would be easy to add.

Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
@rohit-nayak-ps
Copy link
Contributor Author

I think it would make sense to move this to vt. We should also add some unit tests, seems like they would be easy to add.

Refactored code and added a unit test.

I would prefer to keep this in vitess for now until we have had a larger discussion about where this fits. Doesn't seem to fit in the current goals ofvt.

Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Copy link
Member

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

Copy link
Contributor Author

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!

Signed-off-by: Rohit Nayak <rohit@planetscale.com>
@rohit-nayak-ps rohit-nayak-ps merged commit 9422e32 into vitessio:main Nov 29, 2024
100 checks passed
@rohit-nayak-ps rohit-nayak-ps deleted the rohit/shard-from-ids branch November 29, 2024 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants