-
Notifications
You must be signed in to change notification settings - Fork 42
feat: retry catalog sync with exponential backoff to speed up cold-start recovery #706
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
Closed
Closed
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| '@openchoreo/backstage-plugin-catalog-backend-module': patch | ||
| '@openchoreo/openchoreo-auth': patch | ||
| '@openchoreo/backstage-plugin-common': patch | ||
| --- | ||
|
|
||
| Recover the OpenChoreo catalog sync quickly after a failed run instead of waiting a full sync interval. Previously a failure (e.g. the IdP/API being briefly unreachable during a cluster cold start) aborted the periodic full sync, leaving the catalog empty until the next scheduled run (default 5 minutes) later. | ||
|
|
||
| The entity provider now retries a failed sync with capped exponential backoff — starting at `openchoreo.schedule.retryInterval` (new, default 30s) and doubling on each consecutive failure up to `schedule.frequency` — so it recovers within ~30s of the backend returning, backs off gracefully during a sustained outage instead of hammering, and never stops reconciling (the catalog self-heals whenever the backend comes back). | ||
|
|
||
| Client-credentials token acquisition also retries transient failures (network errors and 5xx, but not 4xx) with exponential backoff, and the catalog API client now fails fast when a service token cannot be obtained rather than silently issuing unauthenticated requests that are guaranteed to 401. |
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
103 changes: 103 additions & 0 deletions
103
packages/openchoreo-auth/src/ClientCredentialsProvider.test.ts
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,103 @@ | ||
| import { ClientCredentialsProvider } from './ClientCredentialsProvider'; | ||
| import { OpenChoreoAuthConfig } from './types'; | ||
|
|
||
| const authConfig: OpenChoreoAuthConfig = { | ||
| clientId: 'test-client', | ||
| clientSecret: 'test-secret', | ||
| tokenUrl: 'http://idp.test/oauth2/token', | ||
| scopes: [], | ||
| }; | ||
|
|
||
| function mkLogger() { | ||
| return { | ||
| info: jest.fn(), | ||
| debug: jest.fn(), | ||
| warn: jest.fn(), | ||
| error: jest.fn(), | ||
| child: jest.fn().mockReturnThis(), | ||
| } as any; | ||
| } | ||
|
|
||
| // `access_token` is intentionally not a real JWT; decodeJwt throws and the | ||
| // provider falls back to `expires_in`, which is all these tests need. | ||
| const okResponse = () => ({ | ||
| ok: true, | ||
| status: 200, | ||
| statusText: 'OK', | ||
| json: async () => ({ access_token: 'access-token', expires_in: 3600 }), | ||
| text: async () => '', | ||
| }); | ||
|
|
||
| const errorResponse = (status: number) => ({ | ||
| ok: false, | ||
| status, | ||
| statusText: 'Error', | ||
| json: async () => ({}), | ||
| text: async () => 'error body', | ||
| }); | ||
|
|
||
| describe('ClientCredentialsProvider', () => { | ||
| const realFetch = global.fetch; | ||
|
|
||
| afterEach(() => { | ||
| global.fetch = realFetch; | ||
| jest.useRealTimers(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| function mockFetch(...outcomes: Array<object | Error>) { | ||
| const fn = jest.fn(); | ||
| for (const outcome of outcomes) { | ||
| if (outcome instanceof Error) { | ||
| fn.mockRejectedValueOnce(outcome); | ||
| } else { | ||
| fn.mockResolvedValueOnce(outcome); | ||
| } | ||
| } | ||
| global.fetch = fn as any; | ||
| return fn; | ||
| } | ||
|
|
||
| it('returns the token on first success without retrying', async () => { | ||
| const fetchMock = mockFetch(okResponse()); | ||
| const provider = new ClientCredentialsProvider(authConfig, mkLogger()); | ||
|
|
||
| await expect(provider.getToken()).resolves.toBe('access-token'); | ||
| expect(fetchMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('retries a network error and then succeeds', async () => { | ||
| jest.useFakeTimers(); | ||
| const fetchMock = mockFetch(new Error('fetch failed'), okResponse()); | ||
| const provider = new ClientCredentialsProvider(authConfig, mkLogger()); | ||
|
|
||
| const tokenPromise = provider.getToken(); | ||
| await jest.advanceTimersByTimeAsync(1000); // first backoff | ||
| await expect(tokenPromise).resolves.toBe('access-token'); | ||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('does not retry a 4xx response', async () => { | ||
| const fetchMock = mockFetch(errorResponse(401)); | ||
| const provider = new ClientCredentialsProvider(authConfig, mkLogger()); | ||
|
|
||
| await expect(provider.getToken()).rejects.toThrow(/401/); | ||
| expect(fetchMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('retries a 5xx response up to the max attempts, then throws', async () => { | ||
| jest.useFakeTimers(); | ||
| const fetchMock = mockFetch( | ||
| errorResponse(503), | ||
| errorResponse(503), | ||
| errorResponse(503), | ||
| ); | ||
| const provider = new ClientCredentialsProvider(authConfig, mkLogger()); | ||
|
|
||
| const tokenPromise = provider.getToken(); | ||
| tokenPromise.catch(() => {}); // avoid unhandled rejection while timers advance | ||
| await jest.advanceTimersByTimeAsync(1000 + 2000); // both backoffs | ||
| await expect(tokenPromise).rejects.toThrow(/503/); | ||
| expect(fetchMock).toHaveBeenCalledTimes(3); | ||
| }); | ||
| }); |
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
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.