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

pitr: add ingest recorder to repair indexes #41670

Merged
merged 18 commits into from
Mar 14, 2023

Conversation

Leavrth
Copy link
Contributor

@Leavrth Leavrth commented Feb 22, 2023

What problem does this PR solve?

Issue Number: close #41668 ref #38045

Problem Summary:
PiTR is not compatible with accelerated indexing

What is changed and how it works?

  1. record the ingest indexes from history ddl
  2. drop the indexes
  3. re-add the indexes

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

upstream SQL

CREATE TABLE test.pairs(x int auto_increment primary key, y int DEFAULT RAND(), z int DEFAULT RAND());
INSERT INTO test.pairs VALUES (), ... (skip 500), ();
ALTER TABLE test.pairs ADD INDEX i1(y, z) USING HASH COMMENT "edelw;fe?fewfe\nefwe" INVISIBLE;
ALTER TABLE test.pairs ADD UNIQUE KEY u1(x, y) USING RTREE VISIBLE;
ALTER TABLE test.pairs ADD INDEX i2(y, (z + 1)) USING BTREE COMMENT "123";
ALTER TABLE test.pairs ADD UNIQUE KEY u2(x, (y+1)) USING HASH COMMENT "243";

CREATE TABLE test.pairs2(x int, y int, z varchar(20));
INSERT INTO test.pairs2 VALUES (1,1,"1"), ... (skip 500), (500,500,"500");
ALTER TABLE test.pairs2 ADD INDEX i1(y, z(10));
ALTER TABLE test.pairs2 ADD UNIQUE KEY u1(y, z(10), (y * 2)) USING RTREE VISIBLE;
ALTER TABLE test.pairs2 ADD PRIMARY KEY (x) USING HASH;

CREATE TABLE test.pairs3(id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(10), custinfo JSON);
INSERT INTO test.pairs3 VALUES (1, 'pingcap', '{"zipcode": [1,2]}');
ALTER TABLE test.pairs3 ADD INDEX zips2((CAST(custinfo->'$.zipcode' AS UNSIGNED ARRAY)));

CREATE TABLE test.pairs4(x int auto_increment primary key, y int DEFAULT RAND(), z int DEFAULT RAND());
INSERT INTO test.pairs4 VALUES (), ... (skip 500), ();
ALTER TABLE test.pairs4 ADD INDEX i1(y, z) USING HASH COMMENT "edelw;fe?fewfe\nefwe" INVISIBLE;
ALTER TABLE test.pairs4 ADD UNIQUE KEY u1(x, y) USING RTREE VISIBLE;
ALTER TABLE test.pairs4 ADD INDEX i2(y, (z + 1)) USING BTREE COMMENT "123";
ALTER TABLE test.pairs4 ADD UNIQUE KEY u2(x, (y+1)) USING HASH COMMENT "243";
ALTER TABLE test.pairs4 DROP INDEX i1;
ALTER TABLE test.pairs4 DROP INDEX u1;
ALTER TABLE test.pairs4 DROP INDEX i2;
ALTER TABLE test.pairs4 DROP INDEX u2;

CREATE TABLE test.pairs5(x int, y int, z varchar(20));
INSERT INTO test.pairs5 VALUES (1,1,"1"), ... (skip 500), (500,500,"500");
ALTER TABLE test.pairs5 ADD INDEX i1(y, z(10));
ALTER TABLE test.pairs5 ADD UNIQUE KEY u1(y, z(10), (y * 2)) USING RTREE VISIBLE;
ALTER TABLE test.pairs5 ADD PRIMARY KEY (x) USING HASH;
ALTER TABLE test.pairs5 DROP INDEX i1;
ALTER TABLE test.pairs5 DROP INDEX u1;
ALTER TABLE test.pairs5 DROP INDEX `PRIMARY`;

CREATE TABLE test.pairs6(id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, `nam``;e` int, `cust``;info` JSON);
INSERT INTO test.pairs6 VALUES (1, 1, '{"zipcode": [1,2]}');
ALTER TABLE test.pairs6 ADD INDEX zips2((CAST(`cust``;info`->'$.zipcode' AS UNSIGNED ARRAY)));
ALTER TABLE test.pairs6 ADD INDEX i1(`nam``;e`, (`nam``;e` * 2));
RENAME TABLE test.pairs6 TO test.pairs7;
ALTER TABLE test.pairs7 RENAME INDEX i1 to i2;

restore repair ingest index SQL from BR log

ALTER TABLE `test`.`pairs` DROP INDEX `i1`
ALTER TABLE `test`.`pairs` ADD INDEX `i1`(`y`,`z`) USING HASH COMMENT 'edelw;fe?fewfe\\nefwe' INVISIBLE
ALTER TABLE `test`.`pairs` DROP INDEX `u1`
ALTER TABLE `test`.`pairs` ADD UNIQUE KEY `u1`(`x`,`y`) USING RTREE VISIBLE
ALTER TABLE `test`.`pairs` DROP INDEX `i2`
ALTER TABLE `test`.`pairs` ADD INDEX `i2`(`y`,(`z` + 1)) USING BTREE COMMENT '123' VISIBLE
ALTER TABLE `test`.`pairs` DROP INDEX `u2`
ALTER TABLE `test`.`pairs` ADD UNIQUE KEY `u2`(`x`,(`y` + 1)) USING HASH COMMENT '243' VISIBLE
ALTER TABLE `test`.`pairs2` DROP INDEX `PRIMARY`
ALTER TABLE `test`.`pairs2` ADD PRIMARY KEY (`x`) NONCLUSTERED USING HASH VISIBLE
ALTER TABLE `test`.`pairs2` DROP INDEX `i1`
ALTER TABLE `test`.`pairs2` ADD INDEX `i1`(`y`,`z`(10)) USING BTREE VISIBLE
ALTER TABLE `test`.`pairs2` DROP INDEX `u1`
ALTER TABLE `test`.`pairs2` ADD UNIQUE KEY `u1`(`y`,`z`(10),(`y` * 2)) USING RTREE VISIBLE
ALTER TABLE `test`.`pairs3` DROP INDEX `zips2`
ALTER TABLE `test`.`pairs3` ADD INDEX `zips2`((cast(json_extract(`custinfo`, _utf8'$.zipcode') as unsigned array))) USING BTREE VISIBLE
ALTER TABLE `test`.`pairs7` DROP INDEX `zips2`
ALTER TABLE `test`.`pairs7` ADD INDEX `zips2`((cast(json_extract(`cust``;info`, _utf8'$.zipcode') as unsigned array))) USING BTREE VISIBLE
ALTER TABLE `test`.`pairs7` DROP INDEX `i2`
ALTER TABLE `test`.`pairs7` ADD INDEX `i2`(`nam``;e`,(`nam``;e` * 2)) USING BTREE VISIBLE

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

Signed-off-by: Leavrth <jianjun.liao@outlook.com>
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Feb 22, 2023

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • YuJuncen
  • tangenta

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 22, 2023
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
const (
alterTableDropIndexFormat = "ALTER TABLE `%s`.`%s` DROP INDEX `%s`;"
alterTableAddIndexFormat = "ALTER TABLE `%s`.`%s` ADD INDEX `%s`(%s);"
alterTableAddPrimaryFormat = "ALTER TABLE `%s`.`%s` ADD PRIMARY KEY (%s)"
Copy link
Collaborator

Choose a reason for hiding this comment

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

here, currently, do not support clusterd PK online changed. also should care able add unique index.

var addSQL string
if info.IsPrimary {
addSQL = fmt.Sprintf(alterTableAddPrimaryFormat, info.SchemaName, info.TableName, info.ColumnList)
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add generate uk statement

Signed-off-by: Leavrth <jianjun.liao@outlook.com>
@ti-chi-bot ti-chi-bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 27, 2023
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
@ti-chi-bot ti-chi-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Feb 27, 2023
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
@ti-chi-bot ti-chi-bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Feb 28, 2023
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
Comment on lines 2588 to 2595
log.Debug("repair ingest sql", zap.String("drop", dropSQL))
if err := rc.db.se.Execute(ctx, dropSQL); err != nil {
return errors.Trace(err)
}
log.Debug("repair ingest sql", zap.String("add", addSQL))
if err := rc.db.se.ExecuteInternal(ctx, addSQL, info.IndexInfo.Comment); err != nil {
return errors.Trace(err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the debug log left here on purpose? If not, please remove them.

Why does dropSQL use Execute() and addSQL() use ExecuteInternal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the Comment has some escape characters such as " and \n, so here use ExecuteInter to send with args instead of plaintext.
The function pattern is as follow:
Execute(context, sql string)
ExecuteInternal(context, sql string, args...)

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, both use ExecuteInternal.

br/pkg/restore/ingestrec/ingest_recorder.go Show resolved Hide resolved
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
)

if info.IsPrimary {
addSQL = fmt.Sprintf(alterTableAddPrimaryFormat, info.SchemaName, info.TableName, info.ColumnList)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we escape here? Perhaps some guys like to name their indices like(How absurd!):

CREATE INDEX `idx``; DROP DATABASE business; --`(some_row);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😂

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. test updated

Signed-off-by: Leavrth <jianjun.liao@outlook.com>
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
// RangeFilterFromIngestRecorder rewrites the table id of items in the ingestRecorder
// TODO: need to implement the range filter out feature
func (rc *Client) RangeFilterFromIngestRecorder(recorder *ingestrec.IngestRecorder, rewriteRules map[int64]*RewriteRules) error {
filter := rtree.NewRangeTree()
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the filter used for?

@Leavrth
Copy link
Contributor Author

Leavrth commented Mar 13, 2023

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 1b57703

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Mar 13, 2023
@Leavrth Leavrth removed the status/can-merge Indicates a PR has been approved by a committer. label Mar 13, 2023
@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Mar 13, 2023
@Leavrth
Copy link
Contributor Author

Leavrth commented Mar 13, 2023

/hold

@ti-chi-bot ti-chi-bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 13, 2023
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
@ti-chi-bot ti-chi-bot removed the status/can-merge Indicates a PR has been approved by a committer. label Mar 13, 2023
@Leavrth
Copy link
Contributor Author

Leavrth commented Mar 13, 2023

add progress for repair ingest index
2884e86f-ed72-4e50-b76e-2f838fea2ef8

@Leavrth Leavrth removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 13, 2023
@Leavrth
Copy link
Contributor Author

Leavrth commented Mar 14, 2023

/retest

1 similar comment
@Leavrth
Copy link
Contributor Author

Leavrth commented Mar 14, 2023

/retest

Copy link
Contributor

@YuJuncen YuJuncen left a comment

Choose a reason for hiding this comment

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

LGTM for the PB part

@Leavrth
Copy link
Contributor Author

Leavrth commented Mar 14, 2023

/retest

1 similar comment
@Leavrth
Copy link
Contributor Author

Leavrth commented Mar 14, 2023

/retest

@Leavrth
Copy link
Contributor Author

Leavrth commented Mar 14, 2023

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 739bef9

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Mar 14, 2023
@ti-chi-bot ti-chi-bot merged commit c8e6876 into pingcap:master Mar 14, 2023
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.3: #42217.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Mar 14, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@BornChanger
Copy link
Contributor

/cherry-pick release-6.5

@ti-chi-bot
Copy link
Member

@BornChanger: new pull request created to branch release-6.5: #46418.

In response to this:

/cherry-pick release-6.5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Aug 25, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
ti-chi-bot bot pushed a commit that referenced this pull request Sep 8, 2023
Leavrth added a commit to Leavrth/tidb that referenced this pull request Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

compatibility between pitr and accelerated indexing
6 participants