-
Notifications
You must be signed in to change notification settings - Fork 8
feat(backend): run scheduled tasks with ShedLock #6705
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
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c4f02c5
feat(backend): run scheduled tasks on a single replica with ShedLock
theosanderson 9a20161
Update schema documentation based on migration changes
actions-user a52f401
Your commit message
anna-parker 0169f4e
Update backend/src/main/resources/db/migration/V1.31__add_shedlock_ta…
anna-parker 0bc9cb8
Update schema documentation based on migration changes
actions-user 16cf025
Apply suggestion from @claude[bot]
anna-parker 3898c3c
feat: add a little test
anna-parker c8e1f7b
more tests
anna-parker ca65205
feat(backend): enforce single run per interval with ShedLock lockAtLe…
d44579d
fix(backend): derive pipeline lock floor from the configured interval
1507c1b
fix(backend): schedule stale-cleanup with fixedDelay to avoid lock-ex…
f78613c
docs(backend): trim verbose ShedLock comments
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
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
11 changes: 11 additions & 0 deletions
11
backend/src/main/resources/db/migration/V1.31__add_shedlock_table.sql
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 @@ | ||
| -- Table used by ShedLock to coordinate scheduled tasks across backend replicas. | ||
| -- Only one replica can hold the lock for a given task name at a time, so scheduled | ||
| -- tasks run once per interval regardless of how many replicas are deployed. | ||
| -- Schema as required by net.javacrumbs.shedlock:shedlock-provider-jdbc-template. | ||
| CREATE TABLE shedlock ( | ||
| name VARCHAR(64) NOT NULL, | ||
| lock_until TIMESTAMP WITH TIME ZONE NOT NULL, | ||
| locked_at TIMESTAMP WITH TIME ZONE NOT NULL, | ||
| locked_by VARCHAR(255) NOT NULL, | ||
| PRIMARY KEY (name) | ||
| ); | ||
|
anna-parker marked this conversation as resolved.
|
||
62 changes: 62 additions & 0 deletions
62
backend/src/test/kotlin/org/loculus/backend/service/submission/ShedLockIntegrationTest.kt
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,62 @@ | ||
| package org.loculus.backend.service.submission | ||
|
|
||
| import net.javacrumbs.shedlock.core.LockConfiguration | ||
| import net.javacrumbs.shedlock.core.LockProvider | ||
| import org.hamcrest.CoreMatchers.`is` | ||
| import org.hamcrest.MatcherAssert.assertThat | ||
| import org.junit.jupiter.api.Test | ||
| import org.loculus.backend.config.BackendSpringProperty | ||
| import org.loculus.backend.controller.EndpointTest | ||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.jdbc.core.JdbcTemplate | ||
| import java.time.Duration | ||
| import java.time.Instant | ||
|
|
||
| @EndpointTest( | ||
| properties = [ | ||
| "${BackendSpringProperty.STALE_AFTER_SECONDS}=0", | ||
| "${BackendSpringProperty.CLEAN_UP_RUN_EVERY_SECONDS}=3600", | ||
| ], | ||
| ) | ||
| class ShedLockIntegrationTest( | ||
| @Autowired val jdbcTemplate: JdbcTemplate, | ||
| @Autowired val lockProvider: LockProvider, | ||
| @Autowired val cleanUpStaleSequencesInProcessingTask: CleanUpStaleSequencesInProcessingTask, | ||
| ) { | ||
| @Test | ||
| fun `WHEN a scheduled task is invoked through the Spring proxy THEN a shedlock row is created`() { | ||
| cleanUpStaleSequencesInProcessingTask.task() | ||
|
|
||
| val count = jdbcTemplate.queryForObject( | ||
| "SELECT COUNT(*) FROM shedlock WHERE name = ?", | ||
| Int::class.java, | ||
| "cleanUpStaleSequencesInProcessing", | ||
| ) | ||
| assertThat(count, `is`(1)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `WHEN a lock is released within lockAtLeastFor THEN it cannot be re-acquired yet`() { | ||
| // A unique lock name no scheduled task uses, so the background scheduler can't interfere. | ||
| val lockName = "shedLockIntegrationTestLock" | ||
| val lockAtMostFor = Duration.ofMinutes(5) | ||
| val lockAtLeastFor = Duration.ofMinutes(1) | ||
|
|
||
| val firstLock = lockProvider.lock( | ||
| LockConfiguration(Instant.now(), lockName, lockAtMostFor, lockAtLeastFor), | ||
| ) | ||
| assertThat("the lock should be acquired when free", firstLock.isPresent, `is`(true)) | ||
|
|
||
| // Release immediately - because lockAtLeastFor has not elapsed, the lock stays held. | ||
| firstLock.get().unlock() | ||
|
|
||
| val secondLock = lockProvider.lock( | ||
| LockConfiguration(Instant.now(), lockName, lockAtMostFor, lockAtLeastFor), | ||
| ) | ||
| assertThat( | ||
| "re-acquisition within lockAtLeastFor should be refused", | ||
| secondLock.isPresent, | ||
| `is`(false), | ||
| ) | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
.../src/test/kotlin/org/loculus/backend/service/submission/ShedLockPropertyResolutionTest.kt
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,48 @@ | ||
| package org.loculus.backend.service.submission | ||
|
|
||
| import org.hamcrest.CoreMatchers.`is` | ||
| import org.hamcrest.MatcherAssert.assertThat | ||
| import org.junit.jupiter.api.Test | ||
| import org.loculus.backend.config.BackendSpringProperty | ||
| import org.springframework.core.env.MapPropertySource | ||
| import org.springframework.core.env.StandardEnvironment | ||
|
|
||
| /** | ||
| * Verifies the placeholder strings compiled into the `@SchedulerLock` annotations resolve as | ||
| * intended. The DB-backed [ShedLockIntegrationTest] always runs with `atLeast` overridden to PT0S, | ||
| * so it cannot exercise the production default; this lightweight test covers that path. | ||
| */ | ||
| class ShedLockPropertyResolutionTest { | ||
|
|
||
| // Must mirror the lockAtLeastFor string in UseNewerProcessingPipelineVersionTask exactly. | ||
| private val pipelineLockAtLeast = | ||
| "\${loculus.locks.useNewerProcessingPipelineVersion.atLeast:" + | ||
| "PT\${${BackendSpringProperty.PIPELINE_VERSION_UPGRADE_CHECK_INTERVAL_SECONDS}}S}" | ||
|
|
||
| private fun resolve(expression: String, props: Map<String, Any>): String { | ||
| val env = StandardEnvironment() | ||
| env.propertySources.addFirst(MapPropertySource("test", props)) | ||
| return env.resolvePlaceholders(expression) | ||
| } | ||
|
|
||
| @Test | ||
| fun `WHEN no lock override is set THEN lockAtLeastFor falls back to the configured interval`() { | ||
| val resolved = resolve( | ||
| pipelineLockAtLeast, | ||
| mapOf(BackendSpringProperty.PIPELINE_VERSION_UPGRADE_CHECK_INTERVAL_SECONDS to "3"), | ||
| ) | ||
| assertThat(resolved, `is`("PT3S")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `WHEN a lock override is set THEN it takes precedence over the interval default`() { | ||
| val resolved = resolve( | ||
| pipelineLockAtLeast, | ||
| mapOf( | ||
| BackendSpringProperty.PIPELINE_VERSION_UPGRADE_CHECK_INTERVAL_SECONDS to "3", | ||
| "loculus.locks.useNewerProcessingPipelineVersion.atLeast" to "PT0S", | ||
| ), | ||
| ) | ||
| assertThat(resolved, `is`("PT0S")) | ||
| } | ||
| } |
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.
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.
This hard coding is problematic when the task starts taking longer on big instances
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.
Ugh this is actually configurable but bypassing our explicit config variables
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.
yeah agree it could be confusing to have this set in two ways. Would be happy to discard one