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

planner: check index valid while forUpdateRead #22152

Merged
merged 9 commits into from
Jan 12, 2021
Merged

Conversation

you06
Copy link
Contributor

@you06 you06 commented Jan 4, 2021

What problem does this PR solve?

cherry pick #21596 to master as a temporary solution.

Issue Number: close #21498

Problem Summary:

ForUpdateRead will read data using schema version of startTS, this may lead to incorrectness, the cases are listed in #21498

What is changed and how it works?

What's Changed:

For RC and forUpdateRead in RR, check if index's state is still StatePublic.

How it Works:

This check confirms index between snapshotTS and forUpdateTS is not changed, otherwise, the index should not be used.

Related changes

  • PR to update pingcap/docs/pingcap/docs-cn:
  • Need to cherry-pick to the release branch

Check List

Tests

  • Unit test

Side effects

  • Performance regression
    • Consumes more CPU, there is a little regression testing with tpc-c

Release note

  • Fix a bug that schema change will lead to incorrectness when forUpdateRead

@you06 you06 requested a review from a team as a code owner January 4, 2021 07:45
@you06 you06 requested review from SunRunAway and removed request for a team January 4, 2021 07:45
@cfzjywxk cfzjywxk added type/bugfix This PR fixes a bug. sig/planner SIG: Planner sig/transaction SIG:Transaction needs-cherry-pick-5.0-rc and removed sig/transaction SIG:Transaction labels Jan 4, 2021
@you06 you06 requested a review from cfzjywxk January 4, 2021 09:04
@ichn-hu ichn-hu mentioned this pull request Jan 4, 2021
Copy link
Member

@jackysp jackysp left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-srebot
Copy link
Contributor

@jackysp, Thanks for your review. The bot only counts LGTMs from Reviewers and higher roles, but you're still welcome to leave your comments. See the corresponding SIG page for more information. Related SIG: planner(slack).

if err != nil {
return nil, err
}
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

why add this if else~?

it seems buildTableRefsWithCache will return tableDual when sel.From == nil

p = b.buildTableDual()

@@ -3599,7 +3608,7 @@ func (b *PlanBuilder) buildDataSource(ctx context.Context, tn *ast.TableName, as
if tblName.L == "" {
tblName = tn.Name
}
possiblePaths, err := getPossibleAccessPaths(b.ctx, b.TableHints(), tn.IndexHints, tbl, dbName, tblName)
possiblePaths, err := getPossibleAccessPaths(b.ctx, b.TableHints(), tn.IndexHints, tbl, dbName, tblName, b.isForUpdateRead, b.is.SchemaMetaVersion())
Copy link
Contributor

@lysu lysu Jan 5, 2021

Choose a reason for hiding this comment

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

buildTableRefsWithCache's cache mechanism should be taken care...(which buildDs during correlatedAggregateResolver#resolveSelect)

for sql:

create table t1(id int);
create table t2(id int);
select (select id from t2 limit 1 for update), id from t1;

the value in this line should be:

tbl b.isForUpdateRead
t1 false
t2 true

but current get:

tbl b.isForUpdateRead
t1 false
t2 false

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, added some cases for resolveSelect.

@cfzjywxk cfzjywxk added the sig/transaction SIG:Transaction label Jan 6, 2021
@github-actions github-actions bot added the sig/sql-infra SIG: SQL Infra label Jan 7, 2021
@lysu
Copy link
Contributor

lysu commented Jan 7, 2021

LGTM

@ti-srebot ti-srebot added the status/LGT1 Indicates that a PR has LGTM 1. label Jan 7, 2021
@you06
Copy link
Contributor Author

you06 commented Jan 7, 2021

I have a question about forUpdateRead.

insert into t1(id, v, v2) select (select id from t where v = 33), v, v2 from t where v = 34;
select (select id from t where v = 33), v, v2 from t where v = 34 for update;

For the above SQLs, select *** where v = 34 should use for update read, but what about the sub query of select *** where v = 33?

Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
@lysu
Copy link
Contributor

lysu commented Jan 8, 2021

I have a question about forUpdateRead.

insert into t1(id, v, v2) select (select id from t where v = 33), v, v2 from t where v = 34;
select (select id from t where v = 33), v, v2 from t where v = 34 for update;

For the above SQLs, select *** where v = 34 should use for update read, but what about the sub query of select *** where v = 33?

for mysql, ...where v = 34 should be current-read, and ...where v = 33 should be snapshot-read, so

create table t (id int primary key, v int);
insert into t values(1, 1), (2, 2);

-- t1: begin;
-- t1: select v from t where id in (1, 2) ----> v=1
-- t2: update t set v = v * 10 where id in (1, 2)
-- t1: select v, (select v from t where id = 1) vv from t where id = 2 for update  --> v = 20, vv = 1

@cfzjywxk
Copy link
Contributor

cfzjywxk commented Jan 8, 2021

I have a question about forUpdateRead.

insert into t1(id, v, v2) select (select id from t where v = 33), v, v2 from t where v = 34;
select (select id from t where v = 33), v, v2 from t where v = 34 for update;

For the above SQLs, select *** where v = 34 should use for update read, but what about the sub query of select *** where v = 33?

@lysu For TiDB I remember the subquery always use snapshot read, it that right?

@lysu
Copy link
Contributor

lysu commented Jan 8, 2021

it seems tidb work well for those test in decorrelated-subquery, but maybe it will break when correlate-subquery(for example: be rewriten into a join) 😂

lock-read in subquery is corner case...maybe we can check it later - -

MySQL [test]> select v, (select v from t where id = 1 for update) vv from t where id = 2 for update;
+------+------+
| v    | vv   |
+------+------+
|   20 |   10 |
+------+------+
1 row in set (0.003 sec)

MySQL [test]> 
MySQL [test]> 
MySQL [test]> 
MySQL [test]> select v, (select v from t where id = 1 for update) vv from t where id = 2;
+------+------+
| v    | vv   |
+------+------+
|    2 |   10 |
+------+------+
1 row in set (0.002 sec)

MySQL [test]> select v, (select v from t where id = 1) vv from t where id = 2 for update;
+------+------+
| v    | vv   |
+------+------+
|   20 |    1 |
+------+------+
1 row in set (0.003 sec)

MySQL [test]> select v, (select v from t where id = 1) vv from t where id = 2;
+------+------+
| v    | vv   |
+------+------+
|    2 |    1 |
+------+------+
1 row in set (0.003 sec)

MySQL [test]> select tidb_version();
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version()                                                                                                                                                                                                                              |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: None
Edition: Community
Git Commit Hash: None
Git Branch: None
UTC Build Time: None
GoVersion: go1.15
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.001 sec)

@cfzjywxk
Copy link
Contributor

cfzjywxk commented Jan 8, 2021

it seems tidb work well for those test in decorrelated-subquery, but maybe it will break when correlate-subquery(for example: be rewriten into a join) 😂

lock-read in subquery is corner case...- -

MySQL [test]> select v, (select v from t where id = 1 for update) vv from t where id = 2 for update;
+------+------+
| v    | vv   |
+------+------+
|   20 |   10 |
+------+------+
1 row in set (0.003 sec)

MySQL [test]> 
MySQL [test]> 
MySQL [test]> 
MySQL [test]> select v, (select v from t where id = 1 for update) vv from t where id = 2;
+------+------+
| v    | vv   |
+------+------+
|    2 |   10 |
+------+------+
1 row in set (0.002 sec)

MySQL [test]> select v, (select v from t where id = 1) vv from t where id = 2 for update;
+------+------+
| v    | vv   |
+------+------+
|   20 |    1 |
+------+------+
1 row in set (0.003 sec)

MySQL [test]> select v, (select v from t where id = 1) vv from t where id = 2;
+------+------+
| v    | vv   |
+------+------+
|    2 |    1 |
+------+------+
1 row in set (0.003 sec)

MySQL [test]> select tidb_version();
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version()                                                                                                                                                                                                                              |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: None
Edition: Community
Git Commit Hash: None
Git Branch: None
UTC Build Time: None
GoVersion: go1.15
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.001 sec)

That is a problem.. Actually if two statements are equal from the planner prospective but returns different results..
I'm confused.. How should we set our expected behaviours?

@cfzjywxk
Copy link
Contributor

cfzjywxk commented Jan 8, 2021

Seems here it's coupled with planner much more than we think for the for update statement processing.

Signed-off-by: you06 <you1474600@gmail.com>
@you06
Copy link
Contributor Author

you06 commented Jan 8, 2021

945f4cf forbid the propagate of current read for correlate-aggregate query, in another word, correlate-subquery should use snapshot read unless it has forUpdate decorated, we got the following result then.

insert into t1(id, v, v2) select (select id from t where v = 33), v, v2 from t where v = 34;
                                           └ snapshot read                   └ current read
select (select id from t where v = 33), v, v2 from t where v = 34 for update;
                      └ snapshot read             └ current read

@lysu, @cfzjywxk PTAL.

@cfzjywxk
Copy link
Contributor

cfzjywxk commented Jan 8, 2021

945f4cf forbid the propagate of current read for correlate-aggregate query, in another word, correlate-subquery should use snapshot read unless it has forUpdate decorated, we got the following result then.

insert into t1(id, v, v2) select (select id from t where v = 33), v, v2 from t where v = 34;
                                           └ snapshot read                   └ current read
select (select id from t where v = 33), v, v2 from t where v = 34 for update;
                      └ snapshot read             └ current read

@lysu, @cfzjywxk PTAL.

@you06
This place should be rewriting the subquery expression but not correlatedAggregateResolver?

Signed-off-by: you06 <you1474600@gmail.com>
@you06
Copy link
Contributor Author

you06 commented Jan 8, 2021

945f4cf forbid the propagate of current read for correlate-aggregate query, in another word, correlate-subquery should use snapshot read unless it has forUpdate decorated, we got the following result then.

insert into t1(id, v, v2) select (select id from t where v = 33), v, v2 from t where v = 34;
                                           └ snapshot read                   └ current read
select (select id from t where v = 33), v, v2 from t where v = 34 for update;
                      └ snapshot read             └ current read

@lysu, @cfzjywxk PTAL.

@you06
This place should be rewriting the subquery expression but not correlatedAggregateResolver?

Yes, and I added the case, the capture mechanism should be patches to release-4.0 also.

@cfzjywxk
Copy link
Contributor

cfzjywxk commented Jan 9, 2021

@lysu PTAL

Comment on lines 110 to 114
// capture the forUpdateRead mark
isForUpdateRead := b.isForUpdateRead
b.isForUpdateRead = false
expr, resultPlan, err := b.rewriteWithPreprocess(ctx, exprNode, p, aggMapper, nil, asScalar, nil)
b.isForUpdateRead = isForUpdateRead
Copy link
Contributor

Choose a reason for hiding this comment

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

it seems without this change, TestIssue21498 still can work

and it seems subQuery in Projection will call rewriteWithPreprocess directly

newExpr, np, err := b.rewriteWithPreprocess(ctx, field.Expr, p, mapper, windowMapper, true, nil)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, removed the capture now, at least not in this PR.

@@ -2253,6 +2253,10 @@ func (r *correlatedAggregateResolver) resolveSelect(sel *ast.SelectStmt) (err er
if err != nil {
return err
}
// do not use cache when for update read
if isForUpdateReadSelectLock(sel.LockInfo) {
useCache = false
Copy link
Contributor

Choose a reason for hiding this comment

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

after debug, it seems useCache = false only not-use-cache, but it still refill cache..

so in Access Path Selection will happen in correlatedAggregateResolver and directly use cache in later buildProjection

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems useCache = false only cache sub-sub-query when there is a select stmt inside from clause.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added some cases for sub queries, the cache problem is also related with rewrite, it may be solved together with it.

@lysu
Copy link
Contributor

lysu commented Jan 11, 2021

@you06 @cfzjywxk how about fix subQuery question in other PRs and merge this one first, it seems too many question in subquery + lock like #22345, but it's not critial :)

@lysu
Copy link
Contributor

lysu commented Jan 12, 2021

LGTM

1 similar comment
@cfzjywxk
Copy link
Contributor

LGTM

@ti-srebot ti-srebot removed the status/LGT1 Indicates that a PR has LGTM 1. label Jan 12, 2021
@ti-srebot ti-srebot added the status/LGT2 Indicates that a PR has LGTM 2. label Jan 12, 2021
@cfzjywxk
Copy link
Contributor

/merge

@ti-srebot ti-srebot added the status/can-merge Indicates a PR has been approved by a committer. label Jan 12, 2021
@ti-srebot
Copy link
Contributor

/run-all-tests

@ti-srebot ti-srebot merged commit 4088c20 into pingcap:master Jan 12, 2021
ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Jan 12, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-5.0-rc in PR #22365

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/planner SIG: Planner sig/sql-infra SIG: SQL Infra sig/transaction SIG:Transaction status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RC transactions may see inconsistent data when there are concurrent DDLs
5 participants