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

ddl, executor : add auto_random column option #13127

Merged
merged 3 commits into from
Dec 23, 2019

Conversation

tangenta
Copy link
Contributor

@tangenta tangenta commented Nov 5, 2019

Related parser PR: pingcap/parser#571

What problem does this PR solve?

A column attribute named auto_random is added, which is used to mitigate the hot spots on a single server when high-volume writes happen in the table with integer primary key.

If the primary key constraint of a table is specified on an integer column c, the row values in c are directly used as the row id instead of implicit allocation during TiDB's key-value construction.
In this case, we say PKIsHandle.

This PR adds a keyword, auto_random. The usage is as follows:

create table t(a bigint auto_random(3) primary key, b varchar(10))
create table t(a int auto_random(3), b char, primary key (a))
create table t(a int auto_random, b char, primary key (a))

The number n following auto_random means the highest nth bits of the column value is reserved for sharding. Omitting this value is equivalent to setting it to 5. In other words, auto_random has the same meaning as auto_random(5).

Similar to auto_increment, users can also omit the column value when inserting a row, at which point the value is allocated by TiDB automatically.

There are some features about these allocated values:

  • The values are NOT guaranteed to be monotonically increasing or continuous.
  • Each of these values is unique.
  • The highest nth bits of each value is hashed according to the StartTS of a transaction.

Users can also specify the column value explicitly. In this case, TiDB will save the value intact.

Note: before using auto_random, allow-auto-random needs to be set to true in the [experimental] section of the configuration file.

Limitations:

  • Modification on the column type with auto_random is not supported.
  • Modification on the value of auto_random is strongly not recommended.
  • The addition or removal of auto_random column attribute is not supported.
  • auto_increment and auto_random are not allowed to be specified in the same column at the same time.

What is changed and how it works?

  • Change the configuration definition
  • Add some checks in buildTableInfo and getModifiableColumnJob.
  • Adjust row value when doing an insertion.

Check List

Tests

  • Unit test
  • Integration test

Code changes

  • Has exported function/method change
  • Has exported variable/fields change
  • Has interface methods change

Side effects

  • Possible performance regression
  • Increased code complexity

Related changes

  • Need to update the documentation

Release note

@wjhuang2016 wjhuang2016 added the sig/execution SIG execution label Nov 5, 2019
@tangenta tangenta force-pushed the auto-shard branch 6 times, most recently from 84e1b0d to c5fd251 Compare November 11, 2019 09:53
@zimulala zimulala self-requested a review November 11, 2019 12:06
@tangenta tangenta force-pushed the auto-shard branch 3 times, most recently from d7e632b to 15887eb Compare November 11, 2019 14:20
@tangenta tangenta force-pushed the auto-shard branch 2 times, most recently from b502cb4 to cb7933d Compare November 11, 2019 16:17
@codecov
Copy link

codecov bot commented Nov 11, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@09cb919). Click here to learn what that means.
The diff coverage is 77.7777%.

@@             Coverage Diff             @@
##             master     #13127   +/-   ##
===========================================
  Coverage          ?   80.1654%           
===========================================
  Files             ?        483           
  Lines             ?     121626           
  Branches          ?          0           
===========================================
  Hits              ?      97502           
  Misses            ?      16351           
  Partials          ?       7773

@AilinKid AilinKid changed the title ddl,executor: add auto_shard_bits column option ddl, executor : add auto_shard_bits column option. Nov 12, 2019
@AilinKid AilinKid self-requested a review November 12, 2019 05:56
Copy link
Contributor

@AilinKid AilinKid left a comment

Choose a reason for hiding this comment

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

Haven't finished review yet

config/config.go Show resolved Hide resolved
config/config.go Outdated Show resolved Hide resolved
@@ -792,6 +807,62 @@ func getAutoRecordID(d types.Datum, target *types.FieldType, isInsert bool) (int
return recordID, nil
}

func (e *InsertValues) adjustAutoShardDatum(ctx context.Context, d types.Datum, hasValue bool, c *table.Column) (types.Datum, error) {
if !hasValue || d.IsNull() {
Copy link
Contributor

Choose a reason for hiding this comment

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

should take retry into consideration.

Copy link
Contributor Author

@tangenta tangenta Nov 17, 2019

Choose a reason for hiding this comment

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

Can we simply ignore the retry, just allocate another id instead?

Copy link
Member

Choose a reason for hiding this comment

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

It's better to cache the auto shard id in retryInfo, but we can do that in another PR, after all this PR is very large already.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe can plus lazy batch alloc than alloc one by one in next pr, by now, it's ok here.

executor/insert_common.go Outdated Show resolved Hide resolved
executor/insert_common.go Outdated Show resolved Hide resolved
config/config.go Outdated Show resolved Hide resolved
config/config.go Outdated Show resolved Hide resolved
config/config.go Outdated Show resolved Hide resolved
ddl/db_integration_test.go Outdated Show resolved Hide resolved
config/config.go Outdated Show resolved Hide resolved
@tangenta tangenta force-pushed the auto-shard branch 2 times, most recently from c610f8e to 0ed14db Compare November 18, 2019 08:50
config/config.go Outdated Show resolved Hide resolved
executor/ddl_test.go Outdated Show resolved Hide resolved
executor/ddl_test.go Outdated Show resolved Hide resolved
ddl/ddl_api.go Outdated Show resolved Hide resolved
ddl/ddl_api.go Outdated Show resolved Hide resolved
ddl/ddl_api.go Outdated Show resolved Hide resolved
@@ -792,6 +807,62 @@ func getAutoRecordID(d types.Datum, target *types.FieldType, isInsert bool) (int
return recordID, nil
}

func (e *InsertValues) adjustAutoShardDatum(ctx context.Context, d types.Datum, hasValue bool, c *table.Column) (types.Datum, error) {
if !hasValue || d.IsNull() {
Copy link
Member

Choose a reason for hiding this comment

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

It's better to cache the auto shard id in retryInfo, but we can do that in another PR, after all this PR is very large already.

executor/insert_common.go Outdated Show resolved Hide resolved
table/tables/tables.go Outdated Show resolved Hide resolved
Copy link
Contributor

@crazycs520 crazycs520 left a comment

Choose a reason for hiding this comment

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

LGTM

@crazycs520
Copy link
Contributor

/run-all-tests

@bb7133 bb7133 added status/LGT3 The PR has already had 3 LGTM. status/can-merge Indicates a PR has been approved by a committer. and removed status/LGT2 Indicates that a PR has LGTM 2. labels Dec 23, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Dec 23, 2019

Your auto merge job has been accepted, waiting for 14177

@sre-bot
Copy link
Contributor

sre-bot commented Dec 23, 2019

/run-all-tests

@tangenta tangenta changed the title ddl, executor : add auto_random column option. ddl, executor : add auto_random column option Dec 23, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Dec 23, 2019

@tangenta merge failed.

@tangenta
Copy link
Contributor Author

/run-unit-test

2 similar comments
@tangenta
Copy link
Contributor Author

/run-unit-test

@tangenta
Copy link
Contributor Author

/run-unit-test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/execution SIG execution sig/sql-infra SIG: SQL Infra status/can-merge Indicates a PR has been approved by a committer. status/LGT3 The PR has already had 3 LGTM. type/new-feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants