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

Lock-free broadcaster #91602

Merged
merged 1 commit into from
Aug 28, 2020
Merged

Conversation

sxllwx
Copy link
Member

@sxllwx sxllwx commented May 30, 2020

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

lock-free broadcaster, use chan to ensure thread safety.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Hello the maintainer of kubernetes. When I read the implementation of the broadcaster, I found this comment // TODO: see if this lock is needed now that new watchers go through the incoming channel.. I think that the current implementation of the Broadcaster requires this mutex.
One of the reasons is that although the chan named "incoming" was passed when the watcher was added, the channel was not passed when the watcher was closed. This may cause a critical condition.
Pull Request implementation is to close the watcher and the broadcaster through the "incoming" chan, so the use of the mutex can be safely deleted. When I benchmarked, I found that the performance of sending messages to the broadcaster has improved to some extent.
The following are my test results:

current impl

$ go test -test.bench=".*" -count=16                                   
goos: linux
goarch: amd64
pkg: k8s.io/apimachinery/pkg/watch
BenchmarkBroadCaster-8   	13309177	        87.9 ns/op
BenchmarkBroadCaster-8   	13411250	        91.6 ns/op
BenchmarkBroadCaster-8   	13588131	        87.9 ns/op
BenchmarkBroadCaster-8   	13501332	        88.0 ns/op
BenchmarkBroadCaster-8   	13539446	        88.3 ns/op
BenchmarkBroadCaster-8   	13581561	        88.5 ns/op
BenchmarkBroadCaster-8   	13566960	        87.4 ns/op
BenchmarkBroadCaster-8   	13513597	        87.4 ns/op
BenchmarkBroadCaster-8   	13460937	        87.7 ns/op
BenchmarkBroadCaster-8   	13634308	        87.5 ns/op
BenchmarkBroadCaster-8   	13473927	        87.9 ns/op
BenchmarkBroadCaster-8   	13501562	        88.1 ns/op
BenchmarkBroadCaster-8   	13294520	        88.1 ns/op
BenchmarkBroadCaster-8   	13609996	        89.1 ns/op
BenchmarkBroadCaster-8   	13558424	        87.8 ns/op
BenchmarkBroadCaster-8   	13607326	        91.1 ns/op
PASS
ok  	k8s.io/apimachinery/pkg/watch	20.567s

pull request impl

$ go test -test.bench=".*" -count=16                                   
goos: linux
goarch: amd64
pkg: k8s.io/apimachinery/pkg/watch
BenchmarkBroadCaster-8   	15557988	        75.8 ns/op
BenchmarkBroadCaster-8   	15815998	        75.9 ns/op
BenchmarkBroadCaster-8   	15683714	        76.1 ns/op
BenchmarkBroadCaster-8   	15733852	        76.4 ns/op
BenchmarkBroadCaster-8   	15718236	        75.8 ns/op
BenchmarkBroadCaster-8   	15794850	        75.7 ns/op
BenchmarkBroadCaster-8   	15728948	        76.1 ns/op
BenchmarkBroadCaster-8   	15745780	        75.9 ns/op
BenchmarkBroadCaster-8   	15789714	        75.7 ns/op
BenchmarkBroadCaster-8   	15730546	        78.4 ns/op
BenchmarkBroadCaster-8   	15748945	        76.0 ns/op
BenchmarkBroadCaster-8   	15606589	        78.1 ns/op
BenchmarkBroadCaster-8   	15013153	        79.9 ns/op
BenchmarkBroadCaster-8   	14925436	        79.9 ns/op
BenchmarkBroadCaster-8   	14981156	        79.7 ns/op
BenchmarkBroadCaster-8   	15015866	        79.9 ns/op
PASS
ok  	k8s.io/apimachinery/pkg/watch	20.462s

Thank you for reading ~

Does this PR introduce a user-facing change?:

None

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels May 30, 2020
@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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 kubernetes/test-infra repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels May 30, 2020
@k8s-ci-robot
Copy link
Contributor

Welcome @sxllwx!

It looks like this is your first PR to kubernetes/kubernetes 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kubernetes has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label May 30, 2020
@k8s-ci-robot
Copy link
Contributor

Hi @sxllwx. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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 kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels May 30, 2020
@sxllwx
Copy link
Member Author

sxllwx commented May 30, 2020

/check-cla

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels May 30, 2020
@sxllwx
Copy link
Member Author

sxllwx commented May 30, 2020

/assign @deads2k

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels May 31, 2020
@sttts sttts added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jun 2, 2020
@k8s-ci-robot k8s-ci-robot removed the needs-priority Indicates a PR lacks a `priority/foo` label and requires one. label Jun 2, 2020
@sxllwx
Copy link
Member Author

sxllwx commented Jun 2, 2020

/retest

@sxllwx
Copy link
Member Author

sxllwx commented Jun 2, 2020

/test pull-kubernetes-e2e-kind

@@ -196,7 +193,10 @@ func (m *Broadcaster) Action(action EventType, obj runtime.Object) {
// have received the data yet as it can remain sitting in the buffered
// channel.
func (m *Broadcaster) Shutdown() {
close(m.incoming)
m.blockQueue(func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

this change takes the serial close and then wait and makes the the close async, not guaranteed before the wait.

Copy link
Contributor

Choose a reason for hiding this comment

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

this change takes the serial close and then wait and makes the the close async, not guaranteed before the wait.

Reading through, I think the change still works.

Copy link
Member Author

Choose a reason for hiding this comment

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

😄

func (b *Broadcaster) blockQueue(f func()) {
func (m *Broadcaster) blockQueue(f func()) {
select {
case <-m.stopped:
Copy link
Contributor

Choose a reason for hiding this comment

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

previously, sending after shutdown would result in a write to a closed channel, right? Can you make a unit test to prove that it used to panic?

Copy link
Member Author

Choose a reason for hiding this comment

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

blockQueue is called by Watch, WatchWithPrefix, stopWatching, Shutdown. The Action method sends data directly to incoming chan instead of calling blockQueue. There is a subtle bug here. If the Broadcaster has been closed, calling Watch will get a watch.Interface with a value of nil instead of Panic as before. If I call panic here, calling Watcher's Stop method after closing the Broadcaster will cause Panic. In order to maintain the consistency of the interface, in the Watch && WatchWithPrefix method, determine whether broadcasterWatcher is nil to decide whether to call panic.

@@ -96,10 +94,15 @@ func (obj functionFakeRuntimeObject) DeepCopyObject() runtime.Object {
// The purpose of this terrible hack is so that watchers added after an event
// won't ever see that event, and will always see any event after they are
// added.
Copy link
Contributor

Choose a reason for hiding this comment

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

update the comment on the Watch and WatchforPrefix, to indicate that it blocks until the watch is accepted.

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

@deads2k
Copy link
Contributor

deads2k commented Jun 16, 2020

minor comments, there is a clear benchmark benefit here.

@deads2k
Copy link
Contributor

deads2k commented Jun 16, 2020

/approve

@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jun 16, 2020
@@ -127,18 +129,20 @@ func (m *Broadcaster) Watch() Interface {
}
m.watchers[id] = w
})
if w == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comment saying this matches previous behavior, but that we're open to reassessing in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@@ -156,26 +160,27 @@ func (m *Broadcaster) WatchWithPrefix(queuedEvents []Event) Interface {
w.result <- e
}
})
if w == nil {
panic("broadcaster already stopped")
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comment saying this matches previous behavior, but that we're open to reassessing in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@deads2k
Copy link
Contributor

deads2k commented Jun 22, 2020

/lgtm
needs squash

/hold

Holding for a squash and a comment. Ping me after and I'll retag.

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. labels Jun 22, 2020
@sxllwx
Copy link
Member Author

sxllwx commented Jul 13, 2020

/assign @deads2k

@sxllwx
Copy link
Member Author

sxllwx commented Aug 5, 2020

@deads2k PTAL

@deads2k
Copy link
Contributor

deads2k commented Aug 5, 2020

/lgtm
/approve
/hold cancel

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Aug 5, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: deads2k, sxllwx

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

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to fejta).

Review the full test history for this PR.

Silence the bot with an /lgtm cancel or /hold comment for consistent failures.

@justaugustus justaugustus added this to the v1.20 milestone Aug 27, 2020
@justaugustus
Copy link
Member

/test pull-kubernetes-e2e-kind-ipv6

@k8s-ci-robot k8s-ci-robot merged commit 16bd0bc into kubernetes:master Aug 28, 2020
@aojea aojea mentioned this pull request Mar 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants