fix: 10k concurrency stress tests and fixes - #128
Conversation
Signed-off-by: Alex Lovell-Troy <alovelltroy@lanl.gov>
Signed-off-by: Alex Lovell-Troy <alovelltroy@lanl.gov>
- Introduced stress tests for memstore to validate concurrent access and defensive copies. - Added performance tests for smdclient to ensure token refresh coalescing and component information caching. - Enhanced existing tests to cover edge cases and ensure data integrity during concurrent operations. - Implemented mutex locks in smdclient for safe access to shared resources. - Updated tunnels package with stress tests for IP allocation under concurrent conditions. - Improved test coverage for peer removal operations in the tunnels package. Signed-off-by: Alex Lovell-Troy <alovelltroy@lanl.gov>
# Conflicts: # cmd/cloud-init-server/handlers.go # pkg/wgtunnel/tunnels_test.go
| s.stopOnce.Do(func() { | ||
| close(s.stopCacheRefresh) | ||
| }) | ||
| close(s.stopCacheRefresh) |
There was a problem hiding this comment.
unrelated to the PR but this looks like it might call close twice
Signed-off-by: Alex Lovell-Troy <alovelltroy@lanl.gov>
travisbcotton
left a comment
There was a problem hiding this comment.
Looks ok to me after the IP release fix
| return nil | ||
| } | ||
|
|
||
| if err := exec.Command("wg", "set", interfaceName, "peer", peer.PublicKey, "remove").Run(); err != nil { |
There was a problem hiding this comment.
One potential issue I can see is that, since the peer removal queue only stores the name[1], [2], RemovePeer() looks up the current peer key to remove. If the node reconnects while removal is queued, IpForPeer() replaces that key, and the stale removal job removes the new WireGuard session and deletes its map entry.
To mitigate, we could include both the peer name and peer key (captured at enqueue time) in the removal job.
| } | ||
|
|
||
| var componentArray base.ComponentArray | ||
| if err := s.getSMD("/hsm/v2/State/Components", &componentArray); err != nil { |
There was a problem hiding this comment.
Do we want to add ?type=Node to this endpoint so we don't copy any unnecessary components into memory?
| if err2 := s.RefreshToken(); err2 != nil { | ||
| if err2 := s.refreshTokenIfCurrent(usedToken); err2 != nil { |
There was a problem hiding this comment.
refreshTokenIfCurrent() uses the same mutex (s.accessTokenMutex) as s.currentAccessToken() above. The former locks the mutex while making the HTTP call to make the token refresh request and doesn't seem to use a timeout:
cloud-init/internal/smdclient/oidc.go
Lines 27 to 55 in bd7e138
I might be worried that this will stall all other SMD operations while the mutex is held indefinitely.
I wonder if it would be good to implement timeout/retry logic here to prevent indefinite deadlock.
There was a problem hiding this comment.
"here" above == linked code block, not the comment lines.
Description
This pull request introduces a new asynchronous, backpressured queue for removing WireGuard peers, refactors the
PhoneHomeHandlerto use this queue, and adds comprehensive stress and unit tests for the queue and for defensive copying inMemStore. Additionally, it ensures that all data returned fromMemStoreis defensively copied to prevent concurrent mutation bugs. The release workflow is updated to run new stress tests.WireGuard Peer Removal Queue:
PeerRemovalQueue, an asynchronous, backpressured queue for removing WireGuard peers, replacing direct removal calls with a bounded worker pool to prevent resource exhaustion and provide backpressure if the queue is full. (cmd/cloud-init-server/peer_removal_queue.go)PhoneHomeHandlerto usePeerRemovalQueueinstead of directly calling the WireGuard interface manager, returning HTTP 503 if the queue is full. (cmd/cloud-init-server/handlers.go,cmd/cloud-init-server/main.go) [1] [2] [3] [4] [5] [6]Testing and CI Improvements:
PeerRemovalQueueto validate queue bounds, concurrency, and handler responses under load. (cmd/cloud-init-server/peer_removal_queue_test.go,cmd/cloud-init-server/peer_removal_queue_stress_test.go) [1] [2].github/workflows/Release.yml)Defensive Copying in MemStore:
MemStoremethods to return deep defensive copies of stored data, preventing concurrent mutation and data races. New helper functions perform deep copying of maps, slices, and nested structures. (internal/memstore/ciMemStore.go) [1] [2] [3] [4] [5] [6] [7] [8]MemStoredoes not affect the underlying store. (internal/memstore/ciMemStore_stress_test.go)Dependency and Import Cleanups:
cmd/cloud-init-server/handlers.go,internal/memstore/ciMemStore.go) [1] [2]Checklist
make test(or equivalent) locally and all tests passgit commit -s) with my real name and email<filename>.licensesidecarLICENSES/directoryType of Change
For more info, see Contributing Guidelines.