Repository navigation
Leave DBOS unlaunched when launch() fails #662
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
158 changes: 158 additions & 0 deletions
158
transact/src/test/java/dev/dbos/transact/FailedLaunchTest.java
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,158 @@ | ||
| package dev.dbos.transact; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import dev.dbos.transact.config.DBOSConfig; | ||
| import dev.dbos.transact.execution.DBOSLifecycleListener; | ||
| import dev.dbos.transact.execution.QueueService; | ||
| import dev.dbos.transact.migrations.MigrationManager; | ||
| import dev.dbos.transact.utils.PgContainer; | ||
| import dev.dbos.transact.workflow.Workflow; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import org.junit.jupiter.api.AutoClose; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.Timeout; | ||
|
|
||
| /** | ||
| * A launch that throws leaves the instance as if it had never been launched: a later launch tries | ||
| * again from the start, and other calls fail as they do before any launch. | ||
| */ | ||
| public class FailedLaunchTest { | ||
|
|
||
| @AutoClose final PgContainer pgContainer = new PgContainer(); | ||
|
|
||
| @AutoClose DBOS dbos; | ||
|
|
||
| private FailedLaunchService proxy; | ||
|
|
||
| private void create(DBOSConfig config) { | ||
| dbos = new DBOS(config); | ||
| proxy = dbos.registerProxy(FailedLaunchService.class, new FailedLaunchServiceImpl()); | ||
| } | ||
|
|
||
| private void assertNotLaunched() { | ||
| var e = assertThrows(IllegalStateException.class, () -> dbos.getWorkflowStatus("any")); | ||
| assertTrue(e.getMessage().contains("before DBOS is launched"), e.getMessage()); | ||
| } | ||
|
|
||
| // Migrates a schema from empty, which takes minutes on a slow CockroachDB runner, as in | ||
| // CustomSchemaTest. | ||
| @Test | ||
| @Timeout(value = 5, unit = TimeUnit.MINUTES) | ||
| public void aLaunchRefusedForAnUnmigratedSchemaCanBeRetriedOnceItIsMigrated() throws Exception { | ||
| // A schema no migration has run on, which a launch with migrations off must refuse. | ||
| var schema = "failed_launch_%d".formatted(System.nanoTime()); | ||
| var config = pgContainer.dbosConfig().withDatabaseSchema(schema).withMigrate(false); | ||
| create(config); | ||
|
|
||
| var first = assertThrows(IllegalStateException.class, dbos::launch); | ||
| assertNotLaunched(); | ||
| // Tried again for real, not skipped because an executor is already in place. | ||
| var second = assertThrows(IllegalStateException.class, dbos::launch); | ||
| assertEquals(first.getMessage(), second.getMessage()); | ||
|
|
||
| MigrationManager.runMigrations(config.withMigrate(true)); | ||
| dbos.launch(); | ||
| var handle = | ||
| dbos.startWorkflow(() -> proxy.echo("after"), new StartWorkflowOptions("echo-after")); | ||
| assertEquals("after", handle.getResult()); | ||
| } | ||
|
|
||
| @Test | ||
| public void aLaunchThatFailsPartWayShutsDownWhatItStartedAndCanBeRetried() throws Exception { | ||
| create(pgContainer.dbosConfig()); | ||
| var listener = new FailingOnceListener(); | ||
| dbos.integration().registerLifecycleListener(listener); | ||
|
|
||
| var e = assertThrows(IllegalStateException.class, dbos::launch); | ||
| assertEquals("listener failed", e.getMessage()); | ||
| assertEquals(1, listener.shutDowns.get(), "what the failed launch started was shut down"); | ||
| assertNotLaunched(); | ||
|
|
||
| dbos.launch(); | ||
| assertEquals(2, listener.launches.get()); | ||
| var handle = dbos.startWorkflow(() -> proxy.echo("again"), new StartWorkflowOptions("again")); | ||
| assertEquals("again", handle.getResult()); | ||
| } | ||
|
|
||
| @Test | ||
| public void aShutdownStepThatThrowsLeavesNothingElseRunning() throws Exception { | ||
| create(pgContainer.dbosConfig()); | ||
| var listener = new FailingShutdownListener(); | ||
| dbos.integration().registerLifecycleListener(listener); | ||
|
|
||
| var e = assertThrows(IllegalStateException.class, dbos::launch); | ||
| assertEquals("listener failed", e.getMessage()); | ||
| // The listener's shutdown failed, and the launch's own failure is still the one thrown. | ||
| assertEquals(1, e.getSuppressed().length); | ||
| assertEquals("shutdown failed", e.getSuppressed()[0].getMessage()); | ||
| assertTrue( | ||
| listener.failedLaunchQueues.isStopped(), | ||
| "the services after the failing step were still stopped"); | ||
| assertNotLaunched(); | ||
|
|
||
| dbos.launch(); | ||
| var queues = DBOSTestAccess.getQueueService(dbos); | ||
| var thrown = assertThrows(AssertionError.class, dbos::shutdown); | ||
| assertEquals("shutdown failed", thrown.getMessage()); | ||
| assertTrue(queues.isStopped(), "a normal shutdown also stops what comes after the failure"); | ||
| assertNotLaunched(); | ||
| } | ||
|
|
||
| /** | ||
| * Fails the first launch, and its own shutdown on every launch, with an Error, which nothing in | ||
| * DBOS catches for it. | ||
| */ | ||
| private static final class FailingShutdownListener implements DBOSLifecycleListener { | ||
| final AtomicInteger launches = new AtomicInteger(); | ||
| QueueService failedLaunchQueues; | ||
|
|
||
| @Override | ||
| public void dbosLaunched(DBOS dbos) { | ||
| if (launches.incrementAndGet() == 1) { | ||
| failedLaunchQueues = DBOSTestAccess.getQueueService(dbos); | ||
| throw new IllegalStateException("listener failed"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void dbosShutDown() { | ||
| throw new AssertionError("shutdown failed"); | ||
| } | ||
| } | ||
|
|
||
| /** Fails the first launch, after the executor has started its services, and not the next. */ | ||
| private static final class FailingOnceListener implements DBOSLifecycleListener { | ||
| final AtomicInteger launches = new AtomicInteger(); | ||
| final AtomicInteger shutDowns = new AtomicInteger(); | ||
|
|
||
| @Override | ||
| public void dbosLaunched(DBOS dbos) { | ||
| if (launches.incrementAndGet() == 1) { | ||
| throw new IllegalStateException("listener failed"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void dbosShutDown() { | ||
| shutDowns.incrementAndGet(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| interface FailedLaunchService { | ||
| String echo(String value); | ||
| } | ||
|
|
||
| class FailedLaunchServiceImpl implements FailedLaunchService { | ||
| @Override | ||
| @Workflow | ||
| public String echo(String value) { | ||
| return value; | ||
| } | ||
| } |
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.