Skip to content

Commit

Permalink
fix: JVM dev mode startup race with databases (#3875)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas authored Dec 30, 2024
1 parent 2d3c40b commit 3cb858a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public void generateSchema(CombinedIndexBuildItem index,
DefaultOptionalBuildItem defaultOptionalBuildItem,
List<SchemaContributorBuildItem> schemaContributorBuildItems,
LaunchModeBuildItem launchModeBuildItem,
ShutdownContextBuildItem shutdownContextBuildItem,
CommentsBuildItem comments) throws Exception {
String moduleName = moduleNameBuildItem.getModuleName();

Expand Down Expand Up @@ -263,7 +264,7 @@ public void generateSchema(CombinedIndexBuildItem index,
// Delete the runner info file if it already exists
Files.deleteIfExists(path);
// This method tells the runtime not to actually start until we have updated runner details
recorder.handleDevModeRunnerStart(runnerInfo);
recorder.handleDevModeRunnerStart(runnerInfo, shutdownContextBuildItem);
}
}
recorder.loadModuleContextOnStartup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.jboss.logging.Logger;

import io.quarkus.runtime.LaunchMode;
import xyz.block.ftl.LeaseClient;
import xyz.block.ftl.LeaseFailedException;
import xyz.block.ftl.LeaseHandle;
Expand All @@ -19,6 +20,7 @@ public class FTLController implements LeaseClient {
private volatile FTLRunnerConnection runnerConnection;

private static volatile FTLController controller;
private volatile boolean haveRunnerInfo = false;
/**
* The details of how to connect to the runners proxy. For dev mode this needs to be determined after startup,
* which is why this needs to be pluggable.
Expand All @@ -40,6 +42,9 @@ public static FTLController instance() {

FTLController() {
this.moduleName = System.getProperty("ftl.module.name");
if (LaunchMode.current() != LaunchMode.DEVELOPMENT) {
haveRunnerInfo = true;
}
}

public void registerDatabase(String name, GetDeploymentContextResponse.DbType type) {
Expand All @@ -53,6 +58,13 @@ public byte[] getSecret(String secretName) {
private FTLRunnerConnection getRunnerConnection() {
if (runnerConnection == null) {
synchronized (this) {
while (!haveRunnerInfo) {
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
if (runnerConnection == null) {
runnerConnection = new FTLRunnerConnection(runnerDetails.getProxyAddress(),
runnerDetails.getDeploymentKey(), moduleName);
Expand All @@ -74,8 +86,25 @@ public void waitForDevModeStart(Path runnerInfo) {
}
runnerDetails.close();
runnerDetails = new DevModeRunnerDetails(runnerInfo);
haveRunnerInfo = true;
this.notifyAll();
}
}

public void devModeShutdown() {
synchronized (this) {
if (runnerConnection != null) {
try {
runnerConnection.close();
} catch (Exception e) {
log.error("Failed to close runner connection", e);
}
runnerConnection = null;
}
runnerDetails.close();
runnerDetails = DefaultRunnerDetails.INSTANCE;
haveRunnerInfo = false;
}
}

public byte[] getConfig(String config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,14 @@ public void registerDatabase(String dbKind, GetDeploymentContextResponse.DbType
FTLController.instance().registerDatabase(dbKind, name);
}

public void handleDevModeRunnerStart(String runnerInfo) {
public void handleDevModeRunnerStart(String runnerInfo, ShutdownContext shutdownContext) {
FTLController.instance().waitForDevModeStart(Path.of(runnerInfo));
shutdownContext.addShutdownTask(new Runnable() {
@Override
public void run() {
FTLController.instance().devModeShutdown();
}
});
}

public void loadModuleContextOnStartup() {
Expand Down

0 comments on commit 3cb858a

Please sign in to comment.