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

sctest: Make DML Injection Testing Framework More Readable #103813

Merged

Conversation

Xiang-Gu
Copy link
Contributor

@Xiang-Gu Xiang-Gu commented May 23, 2023

This commit tries to make the DML injection testing framework more readable and maintainable.
It mostly added comments and reorganized some code. No function is changed.

Epic: None
Release note: None

@Xiang-Gu Xiang-Gu requested a review from fqazi May 23, 2023 21:21
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

Reviewed all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Xiang-Gu)


pkg/sql/schemachanger/sctest/cumulative.go line 465 at r1 (raw file):

			_, err := runner.DB.ExecContext(context.Background(), boundSQL)
			if (e.expectedOutput == "" ||
				idx != len(e.stmts)-1) && err != nil {

Think we still need the checking that only the last statement is allowed to create an error.


pkg/sql/schemachanger/sctest/cumulative.go line 873 at r1 (raw file):

		_, db, cleanup := newCluster(t, &scexec.TestingKnobs{
			BeforeStage: func(p scplan.Plan, stageIdx int) error {
				if !clusterCreated {

Make this safer by using an atomic

@Xiang-Gu Xiang-Gu changed the title sctest: Added comments and light refactoring to DML injection tests sctest: Make DML Injection Testing Framework More Readable May 24, 2023
Copy link
Contributor Author

@Xiang-Gu Xiang-Gu left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @fqazi)


pkg/sql/schemachanger/sctest/cumulative.go line 465 at r1 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

Think we still need the checking that only the last statement is allowed to create an error.

This does not really make much sense to me. I understood what you want to do if there are >1 stmts to execute in one DML injection block:

  1. Run each one in its own transaction ("runner.DB.ExecContext")
  2. If it's not the last stmt, and it encounters an error, we then either fail the test (if !rewrite) or record the error (if rewrite)
  3. If it's the last stmt, and it encounters an error, we then either fail the test (if !rewrite) or record the error (if rewrite)

I expect the user (who is writing those DML injection blocks in the test file) to have only 1 stmt in a DML injection block if they expect the that DML returns an error.

Such a restriction is also somewhat artificial. Instead, I think we should execute all the stmts specified in one block at once in one transaction (i.e., treat them as one line with multiple stmts separated by semi-colons). That will require slightly more changes to the code (e.g. stageExecStmt.stmts []string will change to stageExecStmt.stmt string and it will be the literal string specified in the block, strip the "\n", and join them with ";"). I am not keen to make this change in this PR though.


pkg/sql/schemachanger/sctest/cumulative.go line 873 at r1 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

Make this safer by using an atomic

done

@Xiang-Gu Xiang-Gu force-pushed the enhancement/refactor-DML-injection-test branch from 9ccef9d to 657c769 Compare May 24, 2023 15:33
@Xiang-Gu Xiang-Gu marked this pull request as ready for review May 24, 2023 15:34
@Xiang-Gu Xiang-Gu requested a review from a team as a code owner May 24, 2023 15:34
@Xiang-Gu
Copy link
Contributor Author

TFTR!

bors r+

@fqazi
Copy link
Collaborator

fqazi commented May 24, 2023

This does not really make much sense to me. I understood what you want to do if there are >1 stmts to execute in one DML injection block:

  1. Run each one in its own transaction ("runner.DB.ExecContext")
  2. If it's not the last stmt, and it encounters an error, we then either fail the test (if !rewrite) or record the error (if rewrite)
  3. If it's the last stmt, and it encounters an error, we then either fail the test (if !rewrite) or record the error (if rewrite)

I expect the user (who is writing those DML injection blocks in the test file) to have only 1 stmt in a DML injection block if they expect the that DML returns an error.

Such a restriction is also somewhat artificial. Instead, I think we should execute all the stmts specified in one block at once in one transaction (i.e., treat them as one line with multiple stmts separated by semi-colons). That will require slightly more changes to the code (e.g. stageExecStmt.stmts []string will change to stageExecStmt.stmt string and it will be the literal string specified in the block, strip the "\n", and join them with ";"). I am not keen to make this change in this PR though.

Thats the existing behaviour here, so I'm afraid we are going to have regressions that will get missed (i.e. if the first insert returns an error vs the second):

add_column_default_unique:stage-exec phase=PostCommitPhase stage=5:14 schemaChangeExecError=(.*duplicate key value violates unique constraint.*)
add_column_default_unique-INSERT INTO db.public.tbl VALUES($stageKey);
add_column_default_unique-INSERT INTO db.public.tbl VALUES($stageKey + 1);

Longer term, this should be one transaction, but we also will need to probably improve the error keys. We need to know the exact failure points in cases like this.

@Xiang-Gu
Copy link
Contributor Author

bors r-

@craig
Copy link
Contributor

craig bot commented May 24, 2023

Canceled.

@Xiang-Gu Xiang-Gu force-pushed the enhancement/refactor-DML-injection-test branch from 657c769 to fd1cd3e Compare May 24, 2023 15:58
@Xiang-Gu
Copy link
Contributor Author

bors r+

@Xiang-Gu
Copy link
Contributor Author

bors r-

@craig
Copy link
Contributor

craig bot commented May 24, 2023

Canceled.

This commit tries to make the DML injection testing framework for
readable and maintainable.

Release note: None
@Xiang-Gu Xiang-Gu force-pushed the enhancement/refactor-DML-injection-test branch from fd1cd3e to 3a84cff Compare May 24, 2023 21:54
Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 3 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained

@Xiang-Gu
Copy link
Contributor Author

TFTR!

bors r+

@craig
Copy link
Contributor

craig bot commented May 25, 2023

Build succeeded:

@craig craig bot merged commit df21930 into cockroachdb:master May 25, 2023
@Xiang-Gu Xiang-Gu deleted the enhancement/refactor-DML-injection-test branch May 25, 2023 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants