-
Notifications
You must be signed in to change notification settings - Fork 702
OCPBUGS-81521: Adapt dashboard Prometheus polling interval based on query response time #16441
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
stefanonardo
wants to merge
1
commit into
openshift:main
Choose a base branch
from
stefanonardo:OCPBUGS-81521
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.
+221
−3
Open
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
98 changes: 98 additions & 0 deletions
98
frontend/public/components/utils/__tests__/adaptive-polling.spec.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,98 @@ | ||
| import { | ||
| computeAdaptiveDelay, | ||
| emaToDelay, | ||
| MIN_POLL_DELAY, | ||
| MAX_POLL_DELAY, | ||
| EMA_ALPHA, | ||
| SCALE_FACTOR, | ||
| } from '../adaptive-polling'; | ||
|
|
||
| describe('emaToDelay', () => { | ||
| it('clamps to MIN_POLL_DELAY for small EMA values', () => { | ||
| expect(emaToDelay(0)).toBe(MIN_POLL_DELAY); | ||
| expect(emaToDelay(500)).toBe(MIN_POLL_DELAY); | ||
| expect(emaToDelay(1499)).toBe(MIN_POLL_DELAY); | ||
| }); | ||
|
|
||
| it('scales proportionally for mid-range EMA values', () => { | ||
| expect(emaToDelay(2000)).toBe(20000); | ||
| expect(emaToDelay(3000)).toBe(30000); | ||
| expect(emaToDelay(4500)).toBe(45000); | ||
| }); | ||
|
|
||
| it('clamps to MAX_POLL_DELAY for large EMA values', () => { | ||
| expect(emaToDelay(6000)).toBe(MAX_POLL_DELAY); | ||
| expect(emaToDelay(10000)).toBe(MAX_POLL_DELAY); | ||
| }); | ||
|
|
||
| it('falls back to MIN_POLL_DELAY for non-finite values', () => { | ||
| expect(emaToDelay(NaN)).toBe(MIN_POLL_DELAY); | ||
| expect(emaToDelay(Infinity)).toBe(MIN_POLL_DELAY); | ||
| expect(emaToDelay(-Infinity)).toBe(MIN_POLL_DELAY); | ||
| }); | ||
| }); | ||
|
|
||
| describe('computeAdaptiveDelay', () => { | ||
| it('uses elapsed directly as EMA on first call (previousEma = 0)', () => { | ||
| const [delay, ema] = computeAdaptiveDelay(500, 0); | ||
| expect(ema).toBe(500); | ||
| expect(delay).toBe(MIN_POLL_DELAY); | ||
| }); | ||
|
|
||
| it('applies EMA smoothing with previous value', () => { | ||
| const [, ema] = computeAdaptiveDelay(4000, 3000); | ||
| const expected = EMA_ALPHA * 4000 + (1 - EMA_ALPHA) * 3000; | ||
| expect(ema).toBe(expected); | ||
| }); | ||
|
|
||
| it('returns MIN_POLL_DELAY for fast responses', () => { | ||
| const [delay] = computeAdaptiveDelay(200, 300); | ||
| expect(delay).toBe(MIN_POLL_DELAY); | ||
| }); | ||
|
|
||
| it('returns proportional delay for moderate responses', () => { | ||
| const [delay, ema] = computeAdaptiveDelay(3000, 3000); | ||
| expect(ema).toBe(3000); | ||
| expect(delay).toBe(30000); | ||
| }); | ||
|
|
||
| it('returns MAX_POLL_DELAY for very slow responses', () => { | ||
| const [delay] = computeAdaptiveDelay(10000, 8000); | ||
| expect(delay).toBe(MAX_POLL_DELAY); | ||
| }); | ||
|
|
||
| it('dampens a single outlier spike via EMA smoothing', () => { | ||
| // Stable at 1s, then a 10s spike | ||
| const [, ema1] = computeAdaptiveDelay(1000, 1000); | ||
| expect(ema1).toBe(1000); | ||
|
|
||
| const [delay, ema2] = computeAdaptiveDelay(10000, ema1); | ||
| const expected = EMA_ALPHA * 10000 + (1 - EMA_ALPHA) * 1000; | ||
| expect(ema2).toBe(expected); | ||
| // Should not jump to MAX_POLL_DELAY from a single spike | ||
| expect(delay).toBeLessThan(MAX_POLL_DELAY); | ||
| }); | ||
|
|
||
| it('recovers gradually after error backoff', () => { | ||
| const errorInput = MAX_POLL_DELAY / SCALE_FACTOR; | ||
| // Start from stable fast state | ||
| const [, emaAfterError] = computeAdaptiveDelay(errorInput, 500); | ||
| expect(emaAfterError).toBeGreaterThan(500); | ||
|
|
||
| // Follow up with a fast response — EMA should decrease | ||
| const [, emaRecovery] = computeAdaptiveDelay(500, emaAfterError); | ||
| expect(emaRecovery).toBeLessThan(emaAfterError); | ||
| }); | ||
|
|
||
| it('defaults previousEma to 0 when omitted', () => { | ||
| const [delay, ema] = computeAdaptiveDelay(2000); | ||
| expect(ema).toBe(2000); | ||
| expect(delay).toBe(20000); | ||
| }); | ||
|
|
||
| it('falls back safely for non-finite or negative elapsedMs', () => { | ||
| expect(computeAdaptiveDelay(NaN, 1000)).toEqual([MIN_POLL_DELAY, 1000]); | ||
| expect(computeAdaptiveDelay(Infinity, 1000)).toEqual([MIN_POLL_DELAY, 1000]); | ||
| expect(computeAdaptiveDelay(-1, 1000)).toEqual([MIN_POLL_DELAY, 1000]); | ||
| }); | ||
| }); |
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,34 @@ | ||
| export const MIN_POLL_DELAY = 15000; | ||
| export const MAX_POLL_DELAY = 60000; | ||
| export const EMA_ALPHA = 0.3; | ||
| export const SCALE_FACTOR = 10; | ||
|
|
||
| /** Converts a smoothed response time (EMA) to a clamped polling delay in ms. */ | ||
| export const emaToDelay = (ema: number): number => | ||
| Number.isFinite(ema) | ||
| ? Math.max(MIN_POLL_DELAY, Math.min(MAX_POLL_DELAY, Math.round(ema * SCALE_FACTOR))) | ||
| : MIN_POLL_DELAY; | ||
|
|
||
| /** | ||
| * Computes the next adaptive polling delay using an Exponential Moving Average | ||
| * of response times. Returns `[nextDelay, updatedEma]`. | ||
| * | ||
| * On first call pass `previousEma` as 0 (or omit) to seed the EMA with `elapsedMs`. | ||
| * | ||
| * With current parameters (alpha=0.3, scale=10x, 15s–60s clamp): | ||
| * ~500ms response = 15s poll (floor) | ||
| * ~2s response = 20s poll | ||
| * ~3s response = 30s poll | ||
| * ~5s response = 50s poll | ||
| * ~6s+ response = 60s poll (ceiling) | ||
| */ | ||
| export const computeAdaptiveDelay = ( | ||
| elapsedMs: number, | ||
| previousEma: number = 0, | ||
| ): [number, number] => { | ||
| if (!Number.isFinite(elapsedMs) || elapsedMs < 0) { | ||
| return [MIN_POLL_DELAY, previousEma]; | ||
| } | ||
| const ema = previousEma === 0 ? elapsedMs : EMA_ALPHA * elapsedMs + (1 - EMA_ALPHA) * previousEma; | ||
| return [emaToDelay(ema), ema]; | ||
| }; |
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.
First error can jump straight to 60s retry, causing overly aggressive backoff.
With
responseTimeEma = 0, the catch path seeds EMA withMAX_POLL_DELAY / SCALE_FACTOR, which immediately schedulesMAX_POLL_DELAY. That delays recovery after transient first-request failures.Suggested fix (seed from floor-equivalent EMA when no history)
import { computeAdaptiveDelay, emaToDelay, + MIN_POLL_DELAY, MAX_POLL_DELAY, SCALE_FACTOR, } from '../components/utils/adaptive-polling'; @@ } catch (error) { - // Feed a synthetic slow response into the EMA to gradually back off without jumping to max - [, nextEma] = computeAdaptiveDelay(MAX_POLL_DELAY / SCALE_FACTOR, responseTimeEma); + // Feed a synthetic slow response into EMA; if no history, seed from floor-equivalent EMA. + const emaSeed = + responseTimeEma > 0 ? responseTimeEma : MIN_POLL_DELAY / SCALE_FACTOR; + [, nextEma] = computeAdaptiveDelay(MAX_POLL_DELAY / SCALE_FACTOR, emaSeed); dispatch(setError(type, key, error)); dispatch(setData(type, key, null)); } finally {📝 Committable suggestion
🤖 Prompt for AI Agents