-
Notifications
You must be signed in to change notification settings - Fork 288
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
relay/meta(dm): fix potential data races after saving GTID #4455
Conversation
[REVIEW NOTIFICATION] This pull request has been approved by:
To complete the pull request process, please ask the reviewers in the list to review by filling The full list of commands accepted by this bot can be found here. Reviewer can indicate their review by submitting an approval review. |
/cc @glorv @lance6716 |
* clone GTID before saving into meta to make meta's GTID independent * add a UT to verify close pingcap#4166
@@ -212,7 +212,7 @@ func (lm *LocalMeta) Save(pos mysql.Position, gset gtid.Set) error { | |||
lm.BinlogGTID = "" | |||
} else { | |||
lm.BinlogGTID = gset.String() | |||
lm.gset = gset | |||
lm.gset = gset.Clone() // need to clone and set, in order to avoid the local meta's gset and the input gset referencing the same object, causing contentions later |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe just add a lock in func (r *Relay) SaveMeta(pos mysql.Position, gset gtid.Set)
is more intuitive 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a lock on (*Relay).SaveMeta()
won't work. Because the data race happens when the doing the (gtid.Set).Set()
and (gtid.Set).Clone()
on the same object. Even if (*Relay).SaveMeta()
is executed serially, data race will still happen when the input gtid.Set
object is also referenced by the LocalMeta
afterwards.
Actually, there are two solutions on the problem. The first one is to add a locking mechanism on corresponding methods in gtid.Set
implementation. The second one is to ensure that the gtid.Set
object inside LocalMeta
is independent.
The first method involves many modifications and may have some performance penalty. What's more, some current mechanism in LocalMeta
tends to favor the second method. For example, (*LocalMeta).GTID()
clones the gtid.Set
before return, which intends to isolate the returning gtid.Set
object from the object inside LocalMeta
.
So considering only the gtid.Set
object inside LocalMeta
is accessed frequently, and currently there are no places where the gtid.Set
inside LocalMeta
is updated directly (which means (*LocalMeta).gset
is always updated by assigning a new object, rather than calling (*LocalMeta).gset.Set()
), I used the second method to fix this problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks explain this reason i will read more carefully later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this commets help, i got the reason of datarace now ~
/run-verify |
Codecov Report
Flags with carried forward coverage won't be shown. Click here to find out more. @@ Coverage Diff @@
## master #4455 +/- ##
================================================
- Coverage 55.8077% 55.6402% -0.1675%
================================================
Files 495 494 -1
Lines 61246 61283 +37
================================================
- Hits 34180 34098 -82
- Misses 23614 23750 +136
+ Partials 3452 3435 -17 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
restLGTM
@@ -212,7 +212,7 @@ func (lm *LocalMeta) Save(pos mysql.Position, gset gtid.Set) error { | |||
lm.BinlogGTID = "" | |||
} else { | |||
lm.BinlogGTID = gset.String() | |||
lm.gset = gset | |||
lm.gset = gset.Clone() // need to clone and set, in order to avoid the local meta's gset and the input gset referencing the same object, causing contentions later |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this commets help, i got the reason of datarace now ~
@@ -234,3 +235,65 @@ func (r *testMetaSuite) TestLocalMeta(c *C) { | |||
currentDir := lm.Dir() | |||
c.Assert(strings.HasSuffix(currentDir, cs.uuidWithSuffix), IsTrue) | |||
} | |||
|
|||
func (r *testMetaSuite) TestLocalMetaPotentialDataRace(c *C) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could please use testify to rewrite this test? see #2164
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK,I'll submit another PR to rewrite the test
don't forget to sign cla #4455 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
/merge |
This pull request has been accepted and is ready to merge. Commit hash: 9607554
|
@dsdashun: Your PR was out of date, I have automatically updated it for you. At the same time I will also trigger all tests for you: /run-all-tests If the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes. 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. |
What problem does this PR solve?
Issue Number: close #4166
What is changed and how it works?
When a GTID set is saved into
LocalMeta
, clone this GTID set first and then do the updating.Otherwise, after a GTID set is saved into
LocalMeta
, the input GTID set and the one insideLocalMeta
are referencing the same object. It'll cause potential data race when modifying the input GTID set object and reading the GTID set insideLocalMeta
happen simultaneously.Check List
Tests
Code changes
N/A
Side effects
N/A
Related changes
N/A
Release note