-
Notifications
You must be signed in to change notification settings - Fork 8
fix: 10k concurrency stress tests and fixes #128
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
+1,291
−56
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e0b237
Addressing a race condition in the Wireguard Tunnel manager
alexlovelltroy e818a7d
Use a cursor to optimize IP allocation
alexlovelltroy b24e7b3
Add stress and performance tests for memstore and smdclient
alexlovelltroy e2c3662
Merge branch 'main' into bugfix/cloud-init-wireguard-map-race
alexlovelltroy bd7e138
Reallocate IPs when they are released
alexlovelltroy 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
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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package main | ||
|
|
||
| import "github.com/rs/zerolog/log" | ||
|
|
||
| const ( | ||
| defaultPeerRemovalWorkers = 2 | ||
| defaultPeerRemovalBuffer = 64 | ||
| ) | ||
|
|
||
| type peerRemover interface { | ||
| RemovePeer(peerName string) error | ||
| } | ||
|
|
||
| type PeerRemovalQueue struct { | ||
| remover peerRemover | ||
| jobs chan string | ||
| } | ||
|
|
||
| func NewPeerRemovalQueue(remover peerRemover) *PeerRemovalQueue { | ||
| return newPeerRemovalQueue(remover, defaultPeerRemovalWorkers, defaultPeerRemovalBuffer) | ||
| } | ||
|
|
||
| func newPeerRemovalQueue(remover peerRemover, workers int, buffer int) *PeerRemovalQueue { | ||
| queue := &PeerRemovalQueue{ | ||
| remover: remover, | ||
| jobs: make(chan string, buffer), | ||
| } | ||
| for range workers { | ||
| go queue.work() | ||
| } | ||
| return queue | ||
| } | ||
|
|
||
| func (q *PeerRemovalQueue) TryEnqueue(peerName string) bool { | ||
| if q == nil || q.remover == nil { | ||
| return true | ||
| } | ||
| select { | ||
| case q.jobs <- peerName: | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func (q *PeerRemovalQueue) work() { | ||
| for peerName := range q.jobs { | ||
| if err := q.remover.RemovePeer(peerName); err != nil { | ||
| log.Error().Err(err).Str("peer", peerName).Msg("failed to remove WireGuard peer") | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| //go:build stress | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestStressPhoneHomeQueueBackpressure10K(t *testing.T) { | ||
| remover := newBlockingPeerRemover() | ||
| queue := newPeerRemovalQueue(remover, defaultPeerRemovalWorkers, defaultPeerRemovalBuffer) | ||
| handler := PhoneHomeHandler(queue, &phoneHomeSMDClient{}) | ||
|
|
||
| for range defaultPeerRemovalWorkers { | ||
| recorder := httptest.NewRecorder() | ||
| handler(recorder, phoneHomeRequest(t)) | ||
| if recorder.Code != http.StatusOK { | ||
| t.Fatalf("worker-fill response status = %d, want %d", recorder.Code, http.StatusOK) | ||
| } | ||
| } | ||
| for range defaultPeerRemovalWorkers { | ||
| select { | ||
| case <-remover.started: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("worker did not start removal") | ||
| } | ||
| } | ||
|
|
||
| const requestCount = 10_000 | ||
| var okCount atomic.Int64 | ||
| var unavailableCount atomic.Int64 | ||
| var ready sync.WaitGroup | ||
| var start sync.WaitGroup | ||
| var done sync.WaitGroup | ||
| ready.Add(requestCount) | ||
| start.Add(1) | ||
| done.Add(requestCount) | ||
|
|
||
| for range requestCount { | ||
| go func() { | ||
| defer done.Done() | ||
| ready.Done() | ||
| start.Wait() | ||
|
|
||
| recorder := httptest.NewRecorder() | ||
| handler(recorder, phoneHomeRequest(t)) | ||
| switch recorder.Code { | ||
| case http.StatusOK: | ||
| okCount.Add(1) | ||
| case http.StatusServiceUnavailable: | ||
| unavailableCount.Add(1) | ||
| default: | ||
| t.Errorf("response status = %d, want %d or %d", recorder.Code, http.StatusOK, http.StatusServiceUnavailable) | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| ready.Wait() | ||
| start.Done() | ||
| done.Wait() | ||
|
|
||
| if got := okCount.Load(); got != defaultPeerRemovalBuffer { | ||
| t.Fatalf("accepted removals = %d, want %d", got, defaultPeerRemovalBuffer) | ||
| } | ||
| if got := unavailableCount.Load(); got != requestCount-defaultPeerRemovalBuffer { | ||
| t.Fatalf("backpressured removals = %d, want %d", got, requestCount-defaultPeerRemovalBuffer) | ||
| } | ||
| close(remover.release) | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/go-chi/chi/v5" | ||
| "github.com/openchami/cloud-init/internal/smdclient" | ||
| ) | ||
|
|
||
| type blockingPeerRemover struct { | ||
| started chan struct{} | ||
| release chan struct{} | ||
| removed chan string | ||
| } | ||
|
|
||
| func newBlockingPeerRemover() *blockingPeerRemover { | ||
| return &blockingPeerRemover{ | ||
| started: make(chan struct{}, 16), | ||
| release: make(chan struct{}), | ||
| removed: make(chan string, 16), | ||
| } | ||
| } | ||
|
|
||
| func (r *blockingPeerRemover) RemovePeer(peerName string) error { | ||
| r.started <- struct{}{} | ||
| <-r.release | ||
| r.removed <- peerName | ||
| return nil | ||
| } | ||
|
|
||
| type phoneHomeSMDClient struct { | ||
| smdclient.FakeSMDClient | ||
| } | ||
|
|
||
| func (phoneHomeSMDClient) IDfromIP(string) (string, error) { | ||
| return "x0c0s0b0n0", nil | ||
| } | ||
|
|
||
| func (phoneHomeSMDClient) IPfromID(string) (string, error) { | ||
| return "10.1.0.1", nil | ||
| } | ||
|
|
||
| func TestPeerRemovalQueueBoundsWork(t *testing.T) { | ||
| remover := newBlockingPeerRemover() | ||
| queue := newPeerRemovalQueue(remover, 1, 1) | ||
|
|
||
| if !queue.TryEnqueue("peer-1") { | ||
| t.Fatal("first enqueue unexpectedly failed") | ||
| } | ||
| select { | ||
| case <-remover.started: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("worker did not start first removal") | ||
| } | ||
| if !queue.TryEnqueue("peer-2") { | ||
| t.Fatal("buffered enqueue unexpectedly failed") | ||
| } | ||
| if queue.TryEnqueue("peer-3") { | ||
| t.Fatal("enqueue succeeded when worker and buffer were saturated") | ||
| } | ||
|
|
||
| close(remover.release) | ||
| for range 2 { | ||
| select { | ||
| case <-remover.removed: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("queued removal did not finish") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestPhoneHomeHandlerReturnsUnavailableWhenRemovalQueueFull(t *testing.T) { | ||
| remover := newBlockingPeerRemover() | ||
| queue := newPeerRemovalQueue(remover, 1, 1) | ||
| handler := PhoneHomeHandler(queue, &phoneHomeSMDClient{}) | ||
|
|
||
| first := httptest.NewRecorder() | ||
| handler(first, phoneHomeRequest(t)) | ||
| if first.Code != http.StatusOK { | ||
| t.Fatalf("first response status = %d, want %d", first.Code, http.StatusOK) | ||
| } | ||
| select { | ||
| case <-remover.started: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("worker did not start first removal") | ||
| } | ||
|
|
||
| second := httptest.NewRecorder() | ||
| handler(second, phoneHomeRequest(t)) | ||
| if second.Code != http.StatusOK { | ||
| t.Fatalf("second response status = %d, want %d", second.Code, http.StatusOK) | ||
| } | ||
|
|
||
| third := httptest.NewRecorder() | ||
| handler(third, phoneHomeRequest(t)) | ||
| if third.Code != http.StatusServiceUnavailable { | ||
| t.Fatalf("third response status = %d, want %d", third.Code, http.StatusServiceUnavailable) | ||
| } | ||
| close(remover.release) | ||
| } | ||
|
|
||
| func TestPhoneHomeHandlerWithoutWireGuardStillReturnsOK(t *testing.T) { | ||
| handler := PhoneHomeHandler(nil, &phoneHomeSMDClient{}) | ||
| recorder := httptest.NewRecorder() | ||
| handler(recorder, phoneHomeRequest(t)) | ||
| if recorder.Code != http.StatusOK { | ||
| t.Fatalf("response status = %d, want %d", recorder.Code, http.StatusOK) | ||
| } | ||
| } | ||
|
|
||
| func phoneHomeRequest(t *testing.T) *http.Request { | ||
| t.Helper() | ||
| r := httptest.NewRequest(http.MethodPost, "/phone-home/x0c0s0b0n0", nil) | ||
| r.RemoteAddr = "10.1.0.1:12345" | ||
| rctx := chi.NewRouteContext() | ||
| rctx.URLParams.Add("id", "x0c0s0b0n0") | ||
| return r.WithContext(contextWithRoute(r.Context(), rctx)) | ||
| } | ||
|
|
||
| func contextWithRoute(ctx context.Context, rctx *chi.Context) context.Context { | ||
| return context.WithValue(ctx, chi.RouteCtxKey, rctx) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.