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

* : Multiple rows insert in a statement should have consecutive autoID if needed. #11876

Merged
merged 57 commits into from
Oct 10, 2019

Conversation

AilinKid
Copy link
Contributor

@AilinKid AilinKid commented Aug 26, 2019

What problem does this PR solve?

fix issue 11696
This comes from JDBC getGeneratedKeys error in Mybatis framework

create table t (a int not null auto_increment, b int, primary key(a));

concurrent case: 
A: insert into t(b) values(...),(...),(...)...;  
B: insert into t(b) values(...),(...),(...)...;
...

consequence: 
If N rows inserted After A, when you use LAST_INSERT_ID() = lid, it follows:
You cannot guarantee lid、lid+1、lid+2、...、lid+N is the auto id  sequence in A. (cause alloc func will be called by other case)

Attention Please:
This PR guarantees the consecutive autoID in a batch. (That means users should use JDBC getGeneratedKeys feature under the circumstance that the insert rows in a statement should less than the batch size. For most case, it's adequate. )

When doing multiple batch insert, keeping the consecutive autoID will cache all rows in memory for lazy autoID batch allocation.

What is changed and how it works?

1:let allocator support AllocN(num) to allocate consecutive sequence.
2:when do insert rows, use AllocN(num) to make sure consecutive id allocation.

Check List

Tests

  • Unit test
  • Integration test

Code changes

  • Add a func AllocN(tableID, Num) in Allocator interface.

Related changes

  • Need to cherry-pick to the release branch

Release note

  • Support multiple rows insert in a statement have consecutive autoID if needed.

@codecov
Copy link

codecov bot commented Aug 26, 2019

Codecov Report

Merging #11876 into master will decrease coverage by 0.0863%.
The diff coverage is n/a.

@@               Coverage Diff                @@
##             master     #11876        +/-   ##
================================================
- Coverage   79.9776%   79.8912%   -0.0864%     
================================================
  Files           460        460                
  Lines        103609     103527        -82     
================================================
- Hits          82864      82709       -155     
- Misses        14697      14770        +73     
  Partials       6048       6048

@AilinKid AilinKid changed the title ddl : Multiple rows insert in a statement should have consecutive autoID if needed. [DNM] ddl : Multiple rows insert in a statement should have consecutive autoID if needed. Aug 29, 2019
@AilinKid AilinKid requested review from jackysp, bb7133 and francis0407 and removed request for francis0407 September 4, 2019 04:47
@jackysp
Copy link
Member

jackysp commented Sep 4, 2019

Good job!
/run-all-tests

case AutoIDNull:
// Find consecutive num.
var cnt int
var start int
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
var start int
var start int

Simply start := i is enough

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool Done! >.<

var cnt int
var start int
start = i
for i < length {
Copy link
Member

Choose a reason for hiding this comment

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

How about:

cnt := 1
for i+1 < length && e.cache[i+1].kind == AutoIDNull {
    cnt++
    i++
}

So you don't need to fix the index i

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool Done.

if e.filterErr(err) != nil {
return nil, err
}
// Distribute autoIDs to rows.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Distribute autoIDs to rows.
// Assign autoIDs to rows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

// Cause d may changed in HandleBadNull, do assignment here
rows[rowIdx][colIdx] = d
case AutoIDZero:
// Don't change value 0 to auto id, if NoAutoValueOnZero SQL mode is set.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Don't change value 0 to auto id, if NoAutoValueOnZero SQL mode is set.
// Don't change value 0 to auto id, if `NO_AUTO_VALUE_ON_ZERO` SQL mode is set.

Is this a TODO?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, that means fill 0 to autoID column, just let it be.

@@ -530,6 +675,98 @@ func (e *InsertValues) fillRow(ctx context.Context, row []types.Datum, hasValue
return row, nil
}

// fillRowLazy is quite same to fillRow() except it will cache auto increment datum for lazy batch allocation of autoID.
// This case is used in insert|replace into values (row),(row),(row)...
func (e *InsertValues) fillRowLazy(ctx context.Context, row []types.Datum, hasValue []bool, rowIdx int) ([]types.Datum, error) {
Copy link
Member

Choose a reason for hiding this comment

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

This function shares some codes with fillRow, and the two functions are with same signature, can we use a flag variable of InsertValues(for example, lazyFillAutoID) to indicate if the filling logic is lazy, and try to combine two functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice idea!

@AilinKid AilinKid force-pushed the consecutive-auto-id branch 3 times, most recently from 25dbe2d to 2e4497f Compare September 9, 2019 05:33
@AilinKid
Copy link
Contributor Author

AilinKid commented Sep 9, 2019

/run-all-tests

@AilinKid AilinKid force-pushed the consecutive-auto-id branch from 5a78aaf to 8bc9a07 Compare September 9, 2019 08:32
@AilinKid
Copy link
Contributor Author

AilinKid commented Sep 9, 2019

/run-all-tests

@AilinKid
Copy link
Contributor Author

AilinKid commented Sep 9, 2019

/rebuild

@AilinKid
Copy link
Contributor Author

AilinKid commented Sep 9, 2019

Attention!This PR need to modify mysql-test

@AilinKid
Copy link
Contributor Author

/run-all-tests

@AilinKid
Copy link
Contributor Author

/run-all-tests

@AilinKid
Copy link
Contributor Author

AilinKid commented Sep 17, 2019

@jackysp hi,shuaipeng,this PR is ready for review now, PTAL

@jackysp
Copy link
Member

jackysp commented Sep 17, 2019

The PR is really big, is there anyone else to review together?

@bb7133 bb7133 added status/can-merge Indicates a PR has been approved by a committer. needs-cherry-pick-3.1 labels Oct 10, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Oct 10, 2019

/run-all-tests

@AilinKid AilinKid changed the title ddl : Multiple rows insert in a statement should have consecutive autoID if needed. dml : Multiple rows insert in a statement should have consecutive autoID if needed. Oct 10, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Oct 10, 2019

/run-all-tests

@sre-bot
Copy link
Contributor

sre-bot commented Oct 10, 2019

@AilinKid merge failed.

@AilinKid AilinKid merged commit 01cddf6 into pingcap:master Oct 10, 2019
@AilinKid
Copy link
Contributor Author

/run-unit-test

@sre-bot
Copy link
Contributor

sre-bot commented Oct 10, 2019

cherry pick to release-2.1 failed

@sre-bot
Copy link
Contributor

sre-bot commented Oct 10, 2019

cherry pick to release-3.0 failed

@sre-bot
Copy link
Contributor

sre-bot commented Oct 10, 2019

cherry pick to release-3.1 failed

@AilinKid
Copy link
Contributor Author

AilinKid commented Oct 10, 2019

Clicking the update branch button triggers the merge? (bug?)
integration-common-test & integration-ddl-test has passed!
unit-tests with a decimal test fail! I've tried update branch and run-all-tests in other PR with all passed. (it seems random fail, maybe the environment factor)

@AilinKid AilinKid changed the title dml : Multiple rows insert in a statement should have consecutive autoID if needed. * : Multiple rows insert in a statement should have consecutive autoID if needed. Oct 10, 2019
AilinKid added a commit to AilinKid/tidb that referenced this pull request Oct 10, 2019
AilinKid added a commit to AilinKid/tidb that referenced this pull request Oct 10, 2019
AilinKid added a commit to AilinKid/tidb that referenced this pull request Oct 11, 2019
sre-bot pushed a commit that referenced this pull request Oct 13, 2019
sre-bot pushed a commit that referenced this pull request Oct 13, 2019
ngaut pushed a commit that referenced this pull request Oct 14, 2019
@you06 you06 added the sig/sql-infra SIG: SQL Infra label Mar 4, 2020
@AilinKid AilinKid deleted the consecutive-auto-id branch August 14, 2024 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants