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

sessionctx/binloginfo: fix uncomment pre_split_regions ddl-querys in binlog #11762

Merged
merged 14 commits into from
Aug 19, 2019

Conversation

crazycs520
Copy link
Contributor

@crazycs520 crazycs520 commented Aug 17, 2019

What problem does this PR solve?

When tidb write binlog query, TiDB should comment some special syntax for compatibility. eg:
SHARD_ROW_ID_BITS, PRE_SPLIT_REGIONS.

But before this PR, TiDB won't comment PRE_SPLIT_REGIONS, This PR use to fix this problem.

Before :

create table t1 (id int ) shard_row_id_bits=2 pre_split_regions=2;

// the write binlog query is, and mysql execute below sql will got error.
create table t1 (id int ) /*!90000 shard_row_id_bits=2 */ pre_split_regions=2 ;

This PR

create table t1 (id int ) shard_row_id_bits=2 pre_split_regions=2;
// the write binlog query is:
create table t1 (id int ) /*!90000 shard_row_id_bits=2 pre_split_regions=2 */;

What is changed and how it works?

Check List

Tests

  • Unit test

Code changes

  • Has exported function/method change

Side effects

Related changes

  • Need to cherry-pick to the release branch

Release note

  • Fix the issue that the TiDB special syntax pre_split_regions was uncommented in create table statement when write binlog query. This may be cause downstream database error.

@sre-bot
Copy link
Contributor

sre-bot commented Aug 17, 2019

Thanks for your PR.
This PR will be closed by bot because you already had 2 opened PRs, close or merge them before submitting a new one.
#11702 , #11721

@crazycs520
Copy link
Contributor Author

/run-all-tests

@codecov
Copy link

codecov bot commented Aug 17, 2019

Codecov Report

Merging #11762 into master will decrease coverage by 0.029%.
The diff coverage is 100%.

@@               Coverage Diff                @@
##             master     #11762        +/-   ##
================================================
- Coverage   81.4796%   81.4506%   -0.0291%     
================================================
  Files           434        434                
  Lines         93627      93475       -152     
================================================
- Hits          76287      76136       -151     
- Misses        11880      11888         +8     
+ Partials       5460       5451         -9

sessionctx/binloginfo/binloginfo.go Outdated Show resolved Hide resolved
sessionctx/binloginfo/binloginfo.go Outdated Show resolved Hide resolved
sessionctx/binloginfo/binloginfo.go Outdated Show resolved Hide resolved
@crazycs520
Copy link
Contributor Author

/run-all-tests

if strings.Contains(ddlQuery, specialPrefix) {
return ddlQuery
}
loc := shardPat.FindStringIndex(strings.ToUpper(ddlQuery))
if loc == nil {
ddlQuery = addSpecialCommentByRegexp(ddlQuery, shardPat)
Copy link
Member

Choose a reason for hiding this comment

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

Rather than adding special comments, then combining them by extracting in regexp pattern, how about firstly storing all them in some struct, say, a slice and then generating the final comment at once?

The latter approach has 2 advantages:

  1. It has better performance with less regexp matching.
  2. If we have more special comments in the future, they will be easy to handle

upperQuery := strings.ToUpper(ddlQuery)
var specialComments []string
minIdx := math.MaxInt64
for _, reg := range regs {
Copy link
Contributor

Choose a reason for hiding this comment

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

For SQL : create table t6 (id int ) shard_row_id_bits=2 shard_row_id_bits=2 pre_split_regions=2;
If it comes with the same Regexps in SQL, will the comments duplicate here?
Should do the check and test.

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. Done.

@AilinKid
Copy link
Contributor

/Rest LGTM
By the way, @bb7133 's comments make sense.

@crazycs520
Copy link
Contributor Author

/rebuild

@crazycs520
Copy link
Contributor Author

/run-all-tests

1 similar comment
@crazycs520
Copy link
Contributor Author

/run-all-tests

var shardPat = regexp.MustCompile(`SHARD_ROW_ID_BITS\s*=\s*\d+`)
var shardPat = regexp.MustCompile(`SHARD_ROW_ID_BITS\s*=\s*\d+\s*`)
var preSplitPat = regexp.MustCompile(`PRE_SPLIT_REGIONS\s*=\s*\d+\s*`)
var redundantCommentPat = regexp.MustCompile(` \*\/\s*\/\*!90000`)
Copy link
Member

Choose a reason for hiding this comment

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

Now this variable is redudant

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. thanks

@bb7133
Copy link
Member

bb7133 commented Aug 19, 2019

hi @crazycs520 , CI failed with the following message:

FAIL: index_change_test.go:54: testIndexChangeSuite.TestIndexChange

index_change_test.go:126:
    c.Check(errors.ErrorStack(checkErr), Equals, "")
... obtained string = "" +
...     "job's row count 0 != 3\n" +
...     "github.com/pingcap/tidb/ddl.(*testIndexChangeSuite).TestIndexChange.func1\n" +
...     "\t/go/src/github.com/pingcap/tidb/ddl/index_change_test.go:112\n" +
...     "github.com/pingcap/tidb/ddl.(*TestDDLCallback).OnJobUpdated\n" +
...     "\t/go/src/github.com/pingcap/tidb/ddl/callback_test.go:72\n" +
...     "github.com/pingcap/tidb/ddl.(*worker).handleDDLJobQueue\n" +
...     "\t/go/src/github.com/pingcap/tidb/ddl/ddl_worker.go:443\n" +
...     "github.com/pingcap/tidb/ddl.(*worker).start\n" +
...     "\t/go/src/github.com/pingcap/tidb/ddl/ddl_worker.go:139\n" +
...     "github.com/pingcap/tidb/ddl.(*ddl).start.func1\n" +
...     "\t/go/src/github.com/pingcap/tidb/ddl/ddl.go:440\n" +
...     "github.com/pingcap/tidb/util.WithRecovery\n" +
...     "\t/go/src/github.com/pingcap/tidb/util/misc.go:81\n" +
...     "runtime.goexit\n" +
...     "\t/usr/local/go/src/runtime/asm_amd64.s:1337"
... expected string = ""

index_change_test.go:171:
    c.Check(errors.ErrorStack(checkErr), Equals, "")
... obtained string = "" +
...     "job's row count 0 != 3\n" +
...     "github.com/pingcap/tidb/ddl.(*testIndexChangeSuite).TestIndexChange.func1\n" +
...     "\t/go/src/github.com/pingcap/tidb/ddl/index_change_test.go:112\n" +
...     "github.com/pingcap/tidb/ddl.(*TestDDLCallback).OnJobUpdated\n" +
...     "\t/go/src/github.com/pingcap/tidb/ddl/callback_test.go:72\n" +
...     "github.com/pingcap/tidb/ddl.(*worker).handleDDLJobQueue\n" +
...     "\t/go/src/github.com/pingcap/tidb/ddl/ddl_worker.go:443\n" +
...     "github.com/pingcap/tidb/ddl.(*worker).start\n" +
...     "\t/go/src/github.com/pingcap/tidb/ddl/ddl_worker.go:139\n" +
...     "github.com/pingcap/tidb/ddl.(*ddl).start.func1\n" +
...     "\t/go/src/github.com/pingcap/tidb/ddl/ddl.go:440\n" +
...     "github.com/pingcap/tidb/util.WithRecovery\n" +
...     "\t/go/src/github.com/pingcap/tidb/util/misc.go:81\n" +
...     "runtime.goexit\n" +
...     "\t/usr/local/go/src/runtime/asm_amd64.s:1337"
... expected string = ""

PTAL, thanks

@crazycs520
Copy link
Contributor Author

/run-all-tests

Copy link
Member

@bb7133 bb7133 left a comment

Choose a reason for hiding this comment

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

LGTM

return addSpecialCommentByRegexps(ddlQuery, shardPat, preSplitPat)
}

func addSpecialCommentByRegexps(ddlQuery string, regs ...*regexp.Regexp) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add some comments for this function.

Copy link
Contributor

@tangenta tangenta left a comment

Choose a reason for hiding this comment

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

The code is write-only..
LGTM

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.

LGTM

@crazycs520 crazycs520 added the status/LGT3 The PR has already had 3 LGTM. label Aug 19, 2019
@crazycs520
Copy link
Contributor Author

/run-all-tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/binlog sig/sql-infra SIG: SQL Infra status/LGT3 The PR has already had 3 LGTM.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants