Repository navigation
Let a system job drop its finished runs and add a non-waiting lock #2707
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
Draft
ravikiranvm
wants to merge
1
commit into
main
Choose a base branch
from
ops-5063
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.
Draft
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
63 changes: 63 additions & 0 deletions
63
packages/server/api/test/unit/helper/system-jobs/redis-system-job.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,63 @@ | ||
| const mockAdd = jest.fn(); | ||
| const mockGetJobs = jest.fn(); | ||
|
|
||
| jest.mock('bullmq', () => ({ | ||
| Queue: jest.fn().mockImplementation(() => ({ | ||
| add: mockAdd, | ||
| getJobs: mockGetJobs, | ||
| waitUntilReady: jest.fn().mockResolvedValue(undefined), | ||
| })), | ||
| Worker: jest.fn().mockImplementation(() => ({ | ||
| waitUntilReady: jest.fn().mockResolvedValue(undefined), | ||
| })), | ||
| })); | ||
|
|
||
| jest.mock('@openops/server-shared', () => ({ | ||
| createRedisClient: jest.fn().mockReturnValue({}), | ||
| logger: { info: jest.fn(), debug: jest.fn() }, | ||
| })); | ||
|
|
||
| import { SystemJobName } from '../../../../src/app/helper/system-jobs/common'; | ||
| import { redisSystemJobSchedulerService } from '../../../../src/app/helper/system-jobs/redis-system-job'; | ||
|
|
||
| describe('redisSystemJobSchedulerService.upsertJob', () => { | ||
| beforeEach(async () => { | ||
| jest.clearAllMocks(); | ||
| mockGetJobs.mockResolvedValue([]); | ||
| await redisSystemJobSchedulerService.init(); | ||
| }); | ||
|
|
||
| it('keeps finished runs by default', async () => { | ||
| await redisSystemJobSchedulerService.upsertJob({ | ||
| job: { name: SystemJobName.OAUTH_CLEANUP, data: {} }, | ||
| schedule: { type: 'repeated', cron: '0 * * * *' }, | ||
| }); | ||
|
|
||
| expect(mockAdd).toHaveBeenCalledWith( | ||
| SystemJobName.OAUTH_CLEANUP, | ||
| {}, | ||
| { repeat: { pattern: '0 * * * *', tz: 'UTC' } }, | ||
| ); | ||
| }); | ||
|
|
||
| it('has the queue drop finished runs of a job that asks for it', async () => { | ||
| await redisSystemJobSchedulerService.upsertJob({ | ||
| job: { | ||
| name: SystemJobName.OAUTH_CLEANUP, | ||
| data: {}, | ||
| discardFinishedRuns: true, | ||
| }, | ||
| schedule: { type: 'repeated', cron: '*/5 * * * *' }, | ||
| }); | ||
|
|
||
| expect(mockAdd).toHaveBeenCalledWith( | ||
| SystemJobName.OAUTH_CLEANUP, | ||
| {}, | ||
| { | ||
| repeat: { pattern: '*/5 * * * *', tz: 'UTC' }, | ||
| removeOnComplete: true, | ||
| removeOnFail: true, | ||
| }, | ||
| ); | ||
| }); | ||
| }); |
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,94 @@ | ||
| const mockAcquire = jest.fn(); | ||
|
|
||
| class MockResourceLockedError extends Error {} | ||
|
|
||
| class MockExecutionError extends Error { | ||
| attempts: Promise<{ votesAgainst: Map<unknown, Error> }>[]; | ||
|
|
||
| constructor(reasons: Error[]) { | ||
| super('The operation was unable to achieve a quorum.'); | ||
| this.attempts = [ | ||
| Promise.resolve({ | ||
| votesAgainst: new Map(reasons.map((reason, index) => [index, reason])), | ||
| }), | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| jest.mock('redlock', () => ({ | ||
| __esModule: true, | ||
| default: jest.fn().mockImplementation(() => ({ acquire: mockAcquire })), | ||
| ExecutionError: MockExecutionError, | ||
| ResourceLockedError: MockResourceLockedError, | ||
| })); | ||
|
|
||
| jest.mock('../src/lib/cache/redis-connection', () => ({ | ||
| createRedisClient: jest.fn().mockReturnValue({}), | ||
| })); | ||
|
|
||
| const mockHandleException = jest.fn(); | ||
| jest.mock('../src/lib/exception-handler', () => ({ | ||
| exceptionHandler: { handle: mockHandleException }, | ||
| })); | ||
|
|
||
| jest.mock('../src/lib/logger', () => ({ | ||
| logger: { debug: jest.fn(), info: jest.fn() }, | ||
| })); | ||
|
|
||
| jest.mock('../src/lib/system', () => ({ | ||
| AppSystemProp: { QUEUE_MODE: 'QUEUE_MODE' }, | ||
| QueueMode: { REDIS: 'REDIS', MEMORY: 'MEMORY' }, | ||
| system: { getOrThrow: jest.fn().mockReturnValue('REDIS') }, | ||
| })); | ||
|
|
||
| import { distributedLock, initializeLock } from '../src/lib/distributed-lock'; | ||
|
|
||
| describe('distributedLock.tryAcquireLock (Redis)', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| initializeLock(); | ||
| }); | ||
|
|
||
| it('asks for the lock once, with no retries, and returns it', async () => { | ||
| const lock = { release: jest.fn() }; | ||
| mockAcquire.mockResolvedValue(lock); | ||
|
|
||
| const result = await distributedLock.tryAcquireLock({ | ||
| key: 'job', | ||
| timeout: 5000, | ||
| }); | ||
|
|
||
| expect(mockAcquire).toHaveBeenCalledWith(['job'], 5000, { retryCount: 0 }); | ||
| expect(result).toBe(lock); | ||
| }); | ||
|
|
||
| it('returns null when the lock is held', async () => { | ||
| mockAcquire.mockRejectedValue( | ||
| new MockExecutionError([new MockResourceLockedError('locked')]), | ||
| ); | ||
|
|
||
| const result = await distributedLock.tryAcquireLock({ key: 'job' }); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(mockHandleException).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rethrows when Redis refused the request for another reason', async () => { | ||
| const failure = new MockExecutionError([new Error('READONLY replica')]); | ||
| mockAcquire.mockRejectedValue(failure); | ||
|
|
||
| await expect(distributedLock.tryAcquireLock({ key: 'job' })).rejects.toBe( | ||
| failure, | ||
| ); | ||
| expect(mockHandleException).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rethrows a failure that is not about the lock at all', async () => { | ||
| mockAcquire.mockRejectedValue(new Error('connection refused')); | ||
|
|
||
| await expect( | ||
| distributedLock.tryAcquireLock({ key: 'job' }), | ||
| ).rejects.toThrow('connection refused'); | ||
| expect(mockHandleException).toHaveBeenCalled(); | ||
| }); | ||
| }); |
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,36 @@ | ||
| import { memoryLock } from '../src/lib/memory-lock'; | ||
|
|
||
| describe('memoryLock.tryAcquire', () => { | ||
| it('takes a free lock', async () => { | ||
| const lock = await memoryLock.tryAcquire('try-free'); | ||
|
|
||
| expect(lock).not.toBeNull(); | ||
| await lock?.release(); | ||
| }); | ||
|
|
||
| it('returns null without waiting while the lock is held', async () => { | ||
| const held = await memoryLock.tryAcquire('try-held'); | ||
|
|
||
| expect(await memoryLock.tryAcquire('try-held')).toBeNull(); | ||
|
|
||
| await held?.release(); | ||
| }); | ||
|
|
||
| it('takes the lock again once it was released', async () => { | ||
| const first = await memoryLock.tryAcquire('try-released'); | ||
| await first?.release(); | ||
|
|
||
| const second = await memoryLock.tryAcquire('try-released'); | ||
|
|
||
| expect(second).not.toBeNull(); | ||
| await second?.release(); | ||
| }); | ||
|
|
||
| it('returns null while a waiting acquire holds the same key', async () => { | ||
| const held = await memoryLock.acquire('try-shared', 1000); | ||
|
|
||
| expect(await memoryLock.tryAcquire('try-shared')).toBeNull(); | ||
|
|
||
| await held.release(); | ||
| }); | ||
| }); |
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.