-
Notifications
You must be signed in to change notification settings - Fork 379
capacity: fix duplicate topology (attempt #2) #1450
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
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-csi:master
from
huww98:fix-duplicate-capacity-2
Apr 22, 2026
+59
−11
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ import ( | |
| "reflect" | ||
| "sort" | ||
| "sync" | ||
| "sync/atomic" | ||
|
|
||
| v1 "k8s.io/api/core/v1" | ||
| storagev1 "k8s.io/api/storage/v1" | ||
|
|
@@ -159,6 +160,7 @@ type nodeTopology struct { | |
| nodeInformer coreinformersv1.NodeInformer | ||
| csiNodeInformer storageinformersv1.CSINodeInformer | ||
| queue workqueue.TypedRateLimitingInterface[string] | ||
| hasSynced atomic.Bool | ||
|
|
||
| mutex sync.Mutex | ||
| // segments hold a list of all currently known topology segments. | ||
|
|
@@ -201,23 +203,32 @@ func (nt *nodeTopology) List() []*Segment { | |
| return segments | ||
| } | ||
|
|
||
| // RunWorker starts a worker that processes topology updates from the queue. | ||
| // | ||
| // It must only be called once per instance. Calling it more than once would | ||
| // result in simultaneous sync() calls that produce duplicate topology segments | ||
| // and pass them to callbacks. Consumers depend on the address of | ||
| // the same topology segment to be consistent for efficient hashing. | ||
| func (nt *nodeTopology) RunWorker(ctx context.Context) { | ||
| klog.Info("Started node topology worker") | ||
| defer klog.Info("Shutting node topology worker") | ||
|
|
||
| if !cache.WaitForCacheSync(ctx.Done(), | ||
| nt.nodeInformer.Informer().HasSynced, nt.csiNodeInformer.Informer().HasSynced) { | ||
| return | ||
| } | ||
|
|
||
| go func() { | ||
| <-ctx.Done() | ||
| nt.queue.ShutDown() | ||
| }() | ||
|
Comment on lines
+221
to
+224
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. synctest checks for leaked goroutines. So I have to clean it up. |
||
| nt.queue.Add("") // Initial sync to ensure HasSynced() will become true. | ||
| for nt.processNextWorkItem(ctx) { | ||
| } | ||
| } | ||
|
|
||
| func (nt *nodeTopology) HasSynced() bool { | ||
| if nt.nodeInformer.Informer().HasSynced() && | ||
| nt.csiNodeInformer.Informer().HasSynced() { | ||
| // Now that both informers are up-to-date, use that | ||
| // information to update our own view of the world. | ||
| nt.sync(context.Background()) | ||
| return true | ||
| } | ||
| return false | ||
| return nt.hasSynced.Load() | ||
| } | ||
|
|
||
| func (nt *nodeTopology) processNextWorkItem(ctx context.Context) bool { | ||
|
|
@@ -227,6 +238,7 @@ func (nt *nodeTopology) processNextWorkItem(ctx context.Context) bool { | |
| } | ||
| defer nt.queue.Done(obj) | ||
| nt.sync(ctx) | ||
| nt.hasSynced.Store(true) | ||
| return true | ||
| } | ||
|
|
||
|
|
||
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
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.
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.
Why is important that
topologyInformergets started here vs. where it was started before?If it's important, then let's add a comment explainining why. If it's not important, then let's not change it.
Uh oh!
There was an error while loading. Please reload this page.
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.
All other informers are started here. It is nature to start it here.
I think we should only start
topologyInformerafterfactoryForNamespace.Start, to avoidcache.WaitForCacheSyncwaiting for not-started informers. Wasting a little CPU when waiting for leader election.There is already a comment just before:
Do you think this is enough?
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.
I think both approaches would be fine and in general I prefer to avoid drive-by enhancements that aren't related to what a PR is primarily trying to do (in this case "fix duplicate topology"). It causes churn and makes reviews harder.
But we can keep it.