Repository navigation
Release the replication lag alert when the master link drops #543
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
jamby77
wants to merge
7
commits into
master
Choose a base branch
from
fix/replication-lag-latch
base: master
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.
+353
−3
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d16ceb1
Release the replication lag alert when the master link drops
jamby77 50e4140
Tighten replication lag latch clearing
jamby77 5603aab
Merge remote-tracking branch 'origin/master' into HEAD
jamby77 b9e0a1f
Clear replication lag latches across every webhook
jamby77 34ca3d9
Drop threshold dispatches invalidated by a clear
jamby77 9e1c24e
Keep alert generations only while a lookup is pending
jamby77 fb67640
Check alert generation in the same step that latches
jamby77 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
168 changes: 168 additions & 0 deletions
168
apps/api/src/prometheus/prometheus.replication-lag.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,168 @@ | ||
| import { ConfigService } from '@nestjs/config'; | ||
| import { WebhookEventType, type Webhook } from '@betterdb/shared'; | ||
| import { PrometheusService } from './prometheus.service'; | ||
| import { ConnectionRegistry } from '../connections/connection-registry.service'; | ||
| import { RuntimeCapabilityTracker } from '../connections/runtime-capability-tracker.service'; | ||
| import { SlowLogAnalyticsService } from '../slowlog-analytics/slowlog-analytics.service'; | ||
| import { CommandLogAnalyticsService } from '../commandlog-analytics/commandlog-analytics.service'; | ||
| import { HealthService } from '../health/health.service'; | ||
| import { StoragePort } from '../common/interfaces/storage-port.interface'; | ||
| import { WebhookDispatcherService } from '../webhooks/webhook-dispatcher.service'; | ||
| import { WebhookEventsProService } from '@proprietary/webhook-pro/webhook-events-pro.service'; | ||
|
|
||
| const CONNECTION_ID = 'conn-1'; | ||
| const OTHER_CONNECTION_ID = 'conn-2'; | ||
| const INSTANCE = { host: '10.0.0.1', port: 6379 }; | ||
|
|
||
| function webhook(id: string): Webhook { | ||
| return { | ||
| id, | ||
| name: id, | ||
| url: `https://hooks.example.com/${id}`, | ||
| enabled: true, | ||
| events: [WebhookEventType.REPLICATION_LAG], | ||
| retryPolicy: { maxRetries: 0, backoffMultiplier: 2, initialDelayMs: 1, maxDelayMs: 1 }, | ||
| thresholds: {}, | ||
| createdAt: 0, | ||
| updatedAt: 0, | ||
| }; | ||
| } | ||
|
|
||
| interface Harness { | ||
| service: PrometheusService; | ||
| fired: string[]; | ||
| } | ||
|
|
||
| function buildService(): Harness { | ||
| const config = { | ||
| get: jest.fn((_key: string, fallback?: unknown) => fallback), | ||
| } as unknown as ConfigService; | ||
| const dispatcher = new WebhookDispatcherService( | ||
| {} as never, | ||
| { getWebhooksByEvent: jest.fn().mockResolvedValue([webhook('wh-1'), webhook('wh-2')]) } as never, | ||
| config, | ||
| ); | ||
| const fired: string[] = []; | ||
| jest | ||
| .spyOn(dispatcher as never, 'dispatchPendingEvent') | ||
| .mockImplementation((async (event: { connectionId?: string }, targets?: Webhook[]) => { | ||
| fired.push(`${event.connectionId}:${targets?.[0]?.id}`); | ||
| return true; | ||
| }) as never); | ||
| const pro = new WebhookEventsProService(dispatcher, { getLicenseTier: () => 'pro' } as never); | ||
| const registry = { | ||
| getConfig: jest.fn().mockReturnValue(INSTANCE), | ||
| getName: jest.fn().mockReturnValue('primary'), | ||
| list: jest.fn().mockReturnValue([]), | ||
| } as unknown as ConnectionRegistry; | ||
| const service = new PrometheusService( | ||
| {} as StoragePort, | ||
| registry, | ||
| config, | ||
| {} as RuntimeCapabilityTracker, | ||
| {} as SlowLogAnalyticsService, | ||
| {} as CommandLogAnalyticsService, | ||
| {} as HealthService, | ||
| undefined, | ||
| pro, | ||
| ); | ||
| return { service, fired }; | ||
| } | ||
|
|
||
| async function poll( | ||
| service: PrometheusService, | ||
| replication: Record<string, string>, | ||
| connectionId = CONNECTION_ID, | ||
| ): Promise<void> { | ||
| service['updateReplicationMetrics']( | ||
| { replication } as never, | ||
| 'primary', | ||
| connectionId, | ||
| INSTANCE, | ||
| false, | ||
| ); | ||
| for (let i = 0; i < 5; i++) { | ||
| await new Promise((resolve) => setImmediate(resolve)); | ||
| } | ||
| } | ||
|
|
||
| const LAGGING = { role: 'slave', master_link_status: 'up', master_last_io_seconds_ago: '30' }; | ||
| const LINK_DOWN = { role: 'slave', master_link_status: 'down', master_last_io_seconds_ago: '30' }; | ||
| const FIRED_ONCE = [`${CONNECTION_ID}:wh-1`, `${CONNECTION_ID}:wh-2`]; | ||
| const FIRED_TWICE = [...FIRED_ONCE, ...FIRED_ONCE]; | ||
|
|
||
| describe('PrometheusService replication lag alert', () => { | ||
| it('re-fires after the master link drops and comes back', async () => { | ||
| const { service, fired } = buildService(); | ||
|
|
||
| await poll(service, LAGGING); | ||
| await poll(service, LAGGING); | ||
| expect(fired).toEqual(FIRED_ONCE); | ||
|
|
||
| await poll(service, LINK_DOWN); | ||
| await poll(service, LAGGING); | ||
| expect(fired).toEqual(FIRED_TWICE); | ||
| }); | ||
|
|
||
| it('keeps one connection latch from suppressing another', async () => { | ||
| const { service, fired } = buildService(); | ||
|
|
||
| await poll(service, LAGGING, CONNECTION_ID); | ||
| await poll(service, LAGGING, OTHER_CONNECTION_ID); | ||
| await poll(service, LINK_DOWN, OTHER_CONNECTION_ID); | ||
| await poll(service, LAGGING, CONNECTION_ID); | ||
|
|
||
| expect(fired).toEqual([...FIRED_ONCE, `${OTHER_CONNECTION_ID}:wh-1`, `${OTHER_CONNECTION_ID}:wh-2`]); | ||
| }); | ||
|
|
||
| it('keeps the latch when the link is up but the lag field is unparsable', async () => { | ||
| const { service, fired } = buildService(); | ||
|
|
||
| await poll(service, LAGGING); | ||
| await poll(service, { role: 'slave', master_link_status: 'up', master_last_io_seconds_ago: '' }); | ||
| await poll(service, { role: 'slave', master_link_status: 'up' }); | ||
| await poll(service, LAGGING); | ||
|
|
||
| expect(fired).toEqual(FIRED_ONCE); | ||
| }); | ||
|
|
||
| it('keeps the latch when INFO omits the link status', async () => { | ||
| const { service, fired } = buildService(); | ||
|
|
||
| await poll(service, LAGGING); | ||
| await poll(service, { role: 'slave' }); | ||
| await poll(service, LAGGING); | ||
|
|
||
| expect(fired).toEqual(FIRED_ONCE); | ||
| }); | ||
|
|
||
| it('re-arms the alert when the replica is promoted', async () => { | ||
| const { service, fired } = buildService(); | ||
|
|
||
| await poll(service, LAGGING); | ||
| await poll(service, { role: 'master', connected_slaves: '0', master_repl_offset: '0' }); | ||
| await poll(service, LAGGING); | ||
|
|
||
| expect(fired).toEqual(FIRED_TWICE); | ||
| }); | ||
|
|
||
| it('re-arms the alert when the node reports an unknown role', async () => { | ||
| const { service, fired } = buildService(); | ||
|
|
||
| await poll(service, LAGGING); | ||
| await poll(service, { role: 'sentinel' }); | ||
| await poll(service, LAGGING); | ||
|
|
||
| expect(fired).toEqual(FIRED_TWICE); | ||
| }); | ||
|
|
||
| it('re-arms the alert when the connection is removed', async () => { | ||
| const { service, fired } = buildService(); | ||
|
|
||
| await poll(service, LAGGING); | ||
| service.cleanupConnectionMetrics(CONNECTION_ID); | ||
| await poll(service, LAGGING); | ||
|
|
||
| expect(fired).toEqual(FIRED_TWICE); | ||
| }); | ||
| }); |
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
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.