-
Notifications
You must be signed in to change notification settings - Fork 86
fix(gateway): prevent API routes from randomly disappearing after concurrent deploys causing 404s #2747
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
Open
tricktron
wants to merge
4
commits into
wso2:main
Choose a base branch
from
tricktron:api-publish-race-bug
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(gateway): prevent API routes from randomly disappearing after concurrent deploys causing 404s #2747
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c67cd63
test(xds): add failing test for concurrent UpdateSnapshot race
tricktron a61706b
fix(xds): add mutex to SnapshotManager.UpdateSnapshot
tricktron 641d92f
refactor(xds): remove dead code, clean up test comments
tricktron 50f94bf
fix(eventlistener): remove goroutine from updateSnapshot
tricktron 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
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,182 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package xds | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3" | ||
| "github.com/envoyproxy/go-control-plane/pkg/resource/v3" | ||
| api "github.com/wso2/api-platform/gateway/gateway-controller/pkg/api/management" | ||
| "github.com/wso2/api-platform/gateway/gateway-controller/pkg/metrics" | ||
| "github.com/wso2/api-platform/gateway/gateway-controller/pkg/models" | ||
| "github.com/wso2/api-platform/gateway/gateway-controller/pkg/storage" | ||
| ) | ||
|
|
||
| func makeRestAPI(uuid, name, ctx string) *models.StoredConfig { | ||
| cfg := api.RestAPI{ | ||
| Kind: api.RestAPIKindRestApi, | ||
| Metadata: api.Metadata{Name: name}, | ||
| Spec: api.APIConfigData{ | ||
| DisplayName: name, | ||
| Version: "v1.0", | ||
| Context: ctx, | ||
| Upstream: struct { | ||
| Main api.Upstream `json:"main" yaml:"main"` | ||
| Sandbox *api.Upstream `json:"sandbox,omitempty" yaml:"sandbox,omitempty"` | ||
| }{ | ||
| Main: api.Upstream{Url: api.Ptr("http://backend:8080")}, | ||
| }, | ||
| Operations: []api.Operation{ | ||
| {Method: api.Ptr(api.OperationMethodGET), Path: api.Ptr("/resource")}, | ||
| }, | ||
| }, | ||
| } | ||
| return &models.StoredConfig{ | ||
| UUID: uuid, | ||
| Kind: models.KindRestApi, | ||
| Handle: name, | ||
| DisplayName: name, | ||
| Version: "v1.0", | ||
| DesiredState: models.StateDeployed, | ||
| Configuration: cfg, | ||
| SourceConfiguration: cfg, | ||
| } | ||
| } | ||
|
|
||
| // TestConcurrentUpdateSnapshot reproduces a race where two goroutines call | ||
| // UpdateSnapshot and the final snapshot only contains api-one instead of | ||
| // both api-one and api-two. The slower goroutine reads the store before | ||
| // api-two is added, but writes last with a higher version, overwriting | ||
| // the correct snapshot. | ||
| // | ||
| // A test hook forces this interleaving: | ||
| // | ||
| // Step 1: A: GetAll() → [api-one] | ||
| // ↓ hook blocks A | ||
| // Step 2: Main: store.Add(api-two) | ||
| // Step 3: B: GetAll() → [api-one, api-two] | ||
| // SetSnapshot(v2, both) ✓ | ||
| // ↓ hook releases A | ||
| // Step 4: A: SetSnapshot(v3, [api-one]) ✗ api-two lost | ||
| // | ||
| // Without mutex: A wins (higher version, stale data) → FAIL | ||
| // With mutex: B can't run while A holds the lock. A finishes first, | ||
| // then B reads the full store and writes the final snapshot → PASS | ||
| func TestConcurrentUpdateSnapshot(t *testing.T) { | ||
| t.Run("stale GetAll cannot overwrite a newer complete snapshot", func(t *testing.T) { | ||
| metrics.Init() | ||
| store := storage.NewConfigStore() | ||
|
|
||
| if err := store.Add(makeRestAPI("uuid-api-1", "api-one", "/api-one")); err != nil { | ||
| t.Fatalf("Add api-one: %v", err) | ||
| } | ||
|
|
||
| sm := NewSnapshotManager(store, createTestLogger(), testRouterConfig(), nil, testConfig()) | ||
|
|
||
| // Step 1: A reads store, blocks in hook | ||
| aGotAll := make(chan struct{}) | ||
| bDone := make(chan struct{}) | ||
|
|
||
| // Only block the first caller (A); let B pass through. | ||
| var hooked atomic.Bool | ||
| sm.afterGetAll = func() { | ||
| if !hooked.CompareAndSwap(false, true) { | ||
| return | ||
| } | ||
| close(aGotAll) | ||
| select { | ||
| case <-bDone: | ||
| // pre-fix path: B raced past A → stale overwrite will occur | ||
| case <-time.After(200 * time.Millisecond): | ||
| // post-fix path: mutex held, B is queued behind A | ||
| } | ||
| } | ||
|
|
||
| errA := make(chan error, 1) | ||
| go func() { | ||
| errA <- sm.UpdateSnapshot(context.Background(), "corr-A") | ||
| }() | ||
| <-aGotAll | ||
|
|
||
| // Step 2: add api-two behind A's back | ||
| if err := store.Add(makeRestAPI("uuid-api-2", "api-two", "/api-two")); err != nil { | ||
| t.Fatalf("Add api-two: %v", err) | ||
| } | ||
|
|
||
| // Step 3: B sees [api-one, api-two], completes, releases A | ||
| errB := make(chan error, 1) | ||
| go func() { | ||
| err := sm.UpdateSnapshot(context.Background(), "corr-B") | ||
| close(bDone) | ||
| errB <- err | ||
| }() | ||
|
|
||
| // Step 4: A resumes with stale [api-one], overwrites B's snapshot | ||
| if err := <-errA; err != nil { | ||
| t.Fatalf("goroutine A UpdateSnapshot: %v", err) | ||
| } | ||
| if err := <-errB; err != nil { | ||
| t.Fatalf("goroutine B UpdateSnapshot: %v", err) | ||
| } | ||
|
|
||
| assertSnapshotContainsAPIs(t, sm, []string{"/api-one", "/api-two"}) | ||
| }) | ||
| } | ||
|
|
||
| func assertSnapshotContainsAPIs(t *testing.T, sm *SnapshotManager, expectedContexts []string) { | ||
| t.Helper() | ||
|
|
||
| snap, err := sm.GetCache().GetSnapshot("router-node") | ||
| if err != nil { | ||
| t.Fatalf("GetSnapshot: %v", err) | ||
| } | ||
|
|
||
| var routePaths []string | ||
| for _, res := range snap.GetResources(resource.RouteType) { | ||
| routeCfg, ok := res.(*route.RouteConfiguration) | ||
| if !ok { | ||
| continue | ||
| } | ||
| for _, vh := range routeCfg.GetVirtualHosts() { | ||
| for _, r := range vh.GetRoutes() { | ||
| if regex, ok := r.GetMatch().GetPathSpecifier().(*route.RouteMatch_SafeRegex); ok { | ||
| routePaths = append(routePaths, regex.SafeRegex.GetRegex()) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for _, ctx := range expectedContexts { | ||
| found := false | ||
| for _, p := range routePaths { | ||
| if strings.Contains(p, ctx) { | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| t.Errorf("snapshot after concurrent UpdateSnapshot is missing %s; got routes: %v", ctx, routePaths) | ||
| } | ||
| } | ||
| } |
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.