This repository was archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 189
pessimistic: record latest done ddl to handle conflict #1626
Open
GMHDBJD
wants to merge
15
commits into
pingcap:master
Choose a base branch
from
GMHDBJD:fixPessmistic
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
69f56d4
pessimistic: record latest done ddl to handle conflict
GMHDBJD 2460943
add ut
GMHDBJD fc5f898
fix test
GMHDBJD c9c9b49
atomic put latest done ddls
GMHDBJD fd1ae14
Merge remote-tracking branch 'upstream/master' into fixPessmistic
GMHDBJD 7ed9987
Merge remote-tracking branch 'upstream/master' into fixPessmistic
GMHDBJD 37f9211
Merge remote-tracking branch 'upstream/master' into fixPessmistic
GMHDBJD 73f49f4
Merge remote-tracking branch 'upstream/master' into fixPessmistic
GMHDBJD a770b5d
address comment
GMHDBJD cb84241
Merge branch 'master' into fixPessmistic
GMHDBJD efe5ca0
Merge branch 'master' into fixPessmistic
GMHDBJD 385f8b1
Merge remote-tracking branch 'upstream/master' into fixPessmistic
GMHDBJD c4405c3
fix shfmt
GMHDBJD 672a5b7
address comment
GMHDBJD 956d9d3
Merge branch 'master' into fixPessmistic
GMHDBJD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright 2021 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package pessimism | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
|
|
||
| "go.etcd.io/etcd/clientv3" | ||
|
|
||
| "github.com/pingcap/dm/dm/common" | ||
| "github.com/pingcap/dm/pkg/etcdutil" | ||
| ) | ||
|
|
||
| // putLatestDoneDDLsOp returns a PUT etcd operation for latest done ddls. | ||
| // This operation should often be sent by DM-master. | ||
| func putLatestDoneDDLsOp(task, downSchema, downTable string, ddls []string) (clientv3.Op, error) { | ||
| data, err := json.Marshal(ddls) | ||
| if err != nil { | ||
| return clientv3.Op{}, err | ||
| } | ||
| key := common.ShardDDLPessimismDDLsKeyAdapter.Encode(task, downSchema, downTable) | ||
|
|
||
| return clientv3.OpPut(key, string(data)), nil | ||
| } | ||
|
|
||
| // PutLatestDoneDDLs puts the last done shard DDL ddls into etcd. | ||
| func PutLatestDoneDDLs(cli *clientv3.Client, task, downSchema, downTable string, ddls []string) (int64, error) { | ||
| putOp, err := putLatestDoneDDLsOp(task, downSchema, downTable, ddls) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| _, rev, err := etcdutil.DoOpsInOneTxnWithRetry(cli, putOp) | ||
| return rev, err | ||
| } | ||
|
|
||
| // GetAllLatestDoneDDLs gets all last done shard DDL ddls in etcd currently. | ||
| // k/v: task -> downSchema -> downTable -> DDLs | ||
| // This function should often be called by DM-master. | ||
| func GetAllLatestDoneDDLs(cli *clientv3.Client) (map[string]map[string]map[string][]string, int64, error) { | ||
| ctx, cancel := context.WithTimeout(cli.Ctx(), etcdutil.DefaultRequestTimeout) | ||
| defer cancel() | ||
|
|
||
| resp, err := cli.Get(ctx, common.ShardDDLPessimismDDLsKeyAdapter.Path(), clientv3.WithPrefix()) | ||
| if err != nil { | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| ddlsMap := make(map[string]map[string]map[string][]string, len(resp.Kvs)) | ||
| for _, kv := range resp.Kvs { | ||
| var ddls []string | ||
| if err2 := json.Unmarshal(kv.Value, &ddls); err2 != nil { | ||
| return nil, 0, err2 | ||
| } | ||
| keys, err2 := common.ShardDDLPessimismDDLsKeyAdapter.Decode(string(kv.Key)) | ||
| if err2 != nil { | ||
| return nil, 0, err2 | ||
| } | ||
| task := keys[0] | ||
| downSchema := keys[1] | ||
| downTable := keys[2] | ||
|
|
||
| if _, ok := ddlsMap[task]; !ok { | ||
| ddlsMap[task] = make(map[string]map[string][]string) | ||
| } | ||
| if _, ok := ddlsMap[task][downSchema]; !ok { | ||
| ddlsMap[task][downSchema] = make(map[string][]string) | ||
| } | ||
| ddlsMap[task][downSchema][downTable] = ddls | ||
| } | ||
|
|
||
| return ddlsMap, resp.Header.Revision, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright 2021 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package pessimism | ||
|
|
||
| import ( | ||
| . "github.com/pingcap/check" | ||
|
|
||
| "github.com/pingcap/dm/pkg/utils" | ||
| ) | ||
|
|
||
| func (t *testForEtcd) TestDDLsEtcd(c *C) { | ||
| defer clearTestInfoOperation(c) | ||
|
|
||
| var ( | ||
| ID1 = "test1-`foo`.`bar`" | ||
| ID2 = "test2-`foo`.`bar`" | ||
| task1, downSchema1, downTable1 = utils.ExtractAllFromLockID(ID1) | ||
| task2, downSchema2, downTable2 = utils.ExtractAllFromLockID(ID2) | ||
| DDLs1 = []string{"ALTER TABLE bar ADD COLUMN c1 INT"} | ||
| DDLs2 = []string{"ALTER TABLE bar ADD COLUMN c2 INT"} | ||
| ) | ||
|
|
||
| // put the same keys twice. | ||
| rev1, err := PutLatestDoneDDLs(etcdTestCli, task1, downSchema1, downTable1, DDLs1) | ||
| c.Assert(err, IsNil) | ||
| rev2, err := PutLatestDoneDDLs(etcdTestCli, task1, downSchema1, downTable1, DDLs1) | ||
| c.Assert(err, IsNil) | ||
| c.Assert(rev2, Greater, rev1) | ||
|
|
||
| // put another DDLs | ||
| rev3, err := PutLatestDoneDDLs(etcdTestCli, task1, downSchema1, downTable1, DDLs2) | ||
| c.Assert(err, IsNil) | ||
| c.Assert(rev3, Greater, rev2) | ||
|
|
||
| // put for another lock | ||
| rev4, err := PutLatestDoneDDLs(etcdTestCli, task2, downSchema2, downTable2, DDLs1) | ||
| c.Assert(err, IsNil) | ||
| c.Assert(rev4, Greater, rev3) | ||
|
|
||
| // get all ddls. | ||
| latestDoneDDLsMap, rev5, err := GetAllLatestDoneDDLs(etcdTestCli) | ||
| c.Assert(err, IsNil) | ||
| c.Assert(rev5, Equals, rev4) | ||
| c.Assert(latestDoneDDLsMap, HasLen, 2) | ||
| c.Assert(latestDoneDDLsMap[task1][downSchema1][downTable1], DeepEquals, DDLs2) | ||
| c.Assert(latestDoneDDLsMap[task2][downSchema2][downTable2], DeepEquals, DDLs1) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.