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

store/tikv: reduce the lock contend between sending message and re-creating streaming client #11301

Merged
merged 7 commits into from Jul 22, 2019

Conversation

tiancaiamao
Copy link
Contributor

What problem does this PR solve?

Tiny code refactor

What is changed and how it works?

We use a lock here
https://github.com/pingcap/tidb/compare/master...tiancaiamao:batch-client-lock?expand=1#diff-db62a1f8315185d693e2a911ce0d5b49L176
and here
https://github.com/pingcap/tidb/compare/master...tiancaiamao:batch-client-lock?expand=1#diff-db62a1f8315185d693e2a911ce0d5b49L214

Two write-write locks to protect the send and re-create operation.
The caller of the send operation is from the batchSendLoop, that means the whole send loop will be blocked during the streaming client re-creating.

In this commit, I provide a tryLockForSend function to try the lock first, if the batchCommandsClient is re-creating, we can try another one, instead of blocking the batchSendLoop.

Check List

Tests

  • No code

Related changes

  • Need to cherry-pick to the release branch

@tiancaiamao
Copy link
Contributor Author

PTAL @hicqu @lysu

@lysu lysu self-requested a review July 18, 2019 04:45
Copy link
Collaborator

@lysu lysu left a comment

Choose a reason for hiding this comment

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

rest LGTM

store/tikv/client_batch.go Outdated Show resolved Hide resolved
store/tikv/client_batch.go Outdated Show resolved Hide resolved
@@ -224,6 +248,7 @@ func (c *batchCommandsClient) reCreateStreamingClient(err error) bool {
zap.String("target", c.target),
)
c.client = streamClient
Copy link
Collaborator

Choose a reason for hiding this comment

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

this write becomes a write without hold "mutex", can other thread saw this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reCreateStreamingClient acquire the write lock, so other send goroutine skips this batchCommandsClient and try the next one, in tryLockForSend @lysu

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function is protected under lock already

Copy link
Collaborator

Choose a reason for hiding this comment

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

lockForRecreate set a flag to exclusive others to step into this, but it call mutex.Unlock after set flag, so this function can exclusive other access, but I still confuse its visibility 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After lockForRecreate, the reCreate flag is set. tryLockForSend will detect the flag.
Maybe you can implement a RWMutex struct with a tryRlock API, and then you'll figure out it.

@tiancaiamao
Copy link
Contributor Author

/run-all-tests

@codecov
Copy link

codecov bot commented Jul 18, 2019

Codecov Report

Merging #11301 into master will decrease coverage by 0.6611%.
The diff coverage is 85.7142%.

@@              Coverage Diff               @@
##           master     #11301        +/-   ##
==============================================
- Coverage   81.94%   81.2788%   -0.6612%     
==============================================
  Files         424        423         -1     
  Lines       92763      90256      -2507     
==============================================
- Hits        76010      73359      -2651     
- Misses      11473      11598       +125     
- Partials     5280       5299        +19

@tiancaiamao
Copy link
Contributor Author

/run-all-tests

@tiancaiamao
Copy link
Contributor Author

PTAL @lysu @hicqu

Copy link
Collaborator

@lysu lysu left a comment

Choose a reason for hiding this comment

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

LGTM

@lysu lysu added the status/LGT1 Indicates that a PR has LGTM 1. label Jul 22, 2019
@lysu lysu requested a review from hicqu July 22, 2019 03:56
@hicqu
Copy link
Contributor

hicqu commented Jul 22, 2019

rest LGTM.

store/tikv/client_batch.go Outdated Show resolved Hide resolved
@tiancaiamao tiancaiamao merged commit f03a021 into pingcap:master Jul 22, 2019
@tiancaiamao tiancaiamao deleted the batch-client-lock branch July 22, 2019 09:10
@sre-bot
Copy link
Contributor

sre-bot commented Jul 22, 2019

cherry pick to release-3.0 failed

tiancaiamao added a commit to tiancaiamao/tidb that referenced this pull request Jul 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants