8000 fix: refining startup, and adding a log on async start error (#48733) by shawkins · Pull Request #48974 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
package org.keycloak.quarkus.runtime;

import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.ForkJoinPool;
import java.util.function.Consumer;

import jakarta.enterprise.context.ApplicationScoped;

Expand Down Expand Up @@ -56,6 +58,7 @@
public class KeycloakMain implements QuarkusApplication {

private static AbstractNonServerCommand COMMAND;
private static Consumer<Throwable> ERROR_HANDLER;

static {
InfinispanUtils.configureVirtualThreads();
Expand Down Expand Up @@ -122,6 +125,9 @@ private static void ensureForkJoinPoolThreadFactoryHasBeenSetToQuarkus() {

public static void start(Picocli picocli, AbstractNonServerCommand command, ExecutionExceptionHandler errorHandler) {
COMMAND = command; // it would be nice to not do this statically - start quarkus with an instance of KeycloakMain, rather than a class for example
ERROR_HANDLER = cause -> errorHandler.error(picocli.getErrWriter(),
String.format("Failed to start server in (%s) mode", getKeycloakModeFromProfile(org.keycloak.common.util.Environment.getProfile())),
cause.getCause());
try {
Quarkus.run(KeycloakMain.class, (exitCode, cause) -> {
if (cause != null) {
Expand All @@ -135,6 +141,9 @@ public static void start(Picocli picocli, AbstractNonServerCommand command, Exec
errorHandler.error(picocli.getErrWriter(),
String.format("Unexpected error when starting the server in (%s) mode", getKeycloakModeFromProfile(org.keycloak.common.util.Environment.getProfile())),
cause.getCause());
} finally {
ERROR_HANDLER = null;
COMMAND = null;
}
picocli.exit(CommandLine.ExitCode.SOFTWARE);
}
Expand All @@ -158,5 +167,10 @@ public int run(String... args) throws Exception {

return ApplicationLifecycleManager.getExitCode();
}


public static void asyncExit(int exitCode, Throwable t) {
Optional.ofNullable(ERROR_HANDLER).ifPresent(h -> h.accept(t));
Quarkus.asyncExit(exitCode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.keycloak.quarkus.runtime.integration.jaxrs;

import java.util.concurrent.CompletableFuture;

import jakarta.enterprise.event.Observes;
import jakarta.ws.rs.ApplicationPath;

Expand All @@ -25,6 +27,7 @@
import org.keycloak.connections.jpa.JpaConnectionProvider;
import org.keycloak.models.KeycloakSession;
import org.keycloak.quarkus.runtime.Environment;
import org.keycloak.quarkus.runtime.KeycloakMain;
import org.keycloak.quarkus.runtime.configuration.Configuration;
import org.keycloak.quarkus.runtime.configuration.MicroProfileConfigProvider;
import org.keycloak.quarkus.runtime.configuration.PropertyMappingInterceptor;
Expand All @@ -35,14 +38,16 @@
import org.keycloak.services.resources.KeycloakApplication;
import org.keycloak.utils.StringUtil;

import io.quarkus.runtime.Quarkus;
import io.quarkus.arc.Arc;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
import io.smallrye.common.annotation.Blocking;
import org.eclipse.microprofile.context.ManagedExecutor;
import org.jboss.logging.Logger;

import static org.keycloak.common.util.Environment.isDevMode;
import static org.keycloak.common.util.Environment.isNonServerMode;
import static org.keycloak.quarkus.runtime.Environment.isTestLaunchMode;

@ApplicationPath("/")
@Blocking
Expand All @@ -61,13 +66,20 @@ protected String getDataDir() {
});
}

@Override
protected void exit(Throwable cause) {
Quarkus.asyncExit(1);
}

void onStartupEvent(@Observes StartupEvent event) {
startup();
var asyncBootstrap = Configuration.getOptionalKcValue(ServerOptions.SERVER_ASYNC_BOOTSTRAP)
.map(Boolean::parseBoolean)
.orElse(Boolean.TRUE);
// skip async bootstrap in dev and non-server mode
if (isDevMode() || isNonServerMode() || isTestLaunchMode() || !asyncBootstrap) {
startup();
} else {
ManagedExecutor executor = Arc.container().instance(ManagedExecutor.class).get();
CompletableFuture.runAsync(this::startup, executor).exceptionally(cause -> {
KeycloakMain.asyncExit(1, cause);
return null;
});
}
}

void onShutdownEvent(@Observes ShutdownEvent event) {
Expand All @@ -84,11 +96,6 @@ protected void initKeycloakSessionFactory(QuarkusKeycloakSessionFactory quarkusK
quarkusKeycloakSessionFactory.init();
}

@Override
protected void initAndStart() {
// no need - is handled by Quarkus logic and onStartupEvent
}

@Override
protected void createTemporaryAdmin(KeycloakSession session) {
var adminUsername = getOption(BootstrapAdminOptions.USERNAME.getKey(), KEYCLOAK_ADMIN_ENV_VAR);
Expand All @@ -110,15 +117,6 @@ protected void createTemporaryAdmin(KeycloakSession session) {
}
}

@Override
protected boolean supportsAsyncInitialization() {
var asyncBootstrap = Configuration.getOptionalKcValue(ServerOptions.SERVER_ASYNC_BOOTSTRAP)
.map(Boolean::parseBoolean)
.orElse(Boolean.TRUE);
// skip async bootstrap in dev and non-server mode
return !isDevMode() && !isNonServerMode() && asyncBootstrap;
}

@Override
protected int getTransactionTimeout(QuarkusKeycloakSessionFactory sessionFactory) {
return ((QuarkusJpaConnectionProviderFactory) sessionFactory.getProviderFactory(JpaConnectionProvider.class)).getMigrationTransactionTimeout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
public class TestRealmResourceFactory implements RealmResourceProviderFactory {
public static final String ID = "test-resources";
private boolean fail;

@Override
public RealmResourceProvider create(KeycloakSession session) {
Expand All @@ -36,12 +37,14 @@ public RealmResourceProvider create(KeycloakSession session) {

@Override
public void init(Config.Scope config) {

fail = Boolean.parseBoolean(config.get("fail", null));
}

@Override
public void postInit(KeycloakSessionFactory factory) {

if (fail) {
throw new RuntimeException("I've failed");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@

package org.keycloak.it.cli.dist;

import java.util.concurrent.TimeUnit;

import org.keycloak.it.junit5.extension.CLIResult;
import org.keycloak.it.junit5.extension.DistributionTest;
import org.keycloak.it.junit5.extension.DryRun;
import org.keycloak.it.junit5.extension.RawDistOnly;
import org.keycloak.it.junit5.extension.TestProvider;
import org.keycloak.it.junit5.extension.WithEnvVars;
import org.keycloak.it.resource.realm.TestRealmResourceTestProvider;
import org.keycloak.it.utils.KeycloakDistribution;
import org.keycloak.it.utils.RawKeycloakDistribution;

import io.quarkus.test.junit.main.Launch;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Tag;
Expand Down Expand Up @@ -269,4 +275,19 @@ void testRuntimeValuesAreNotCaptured(KeycloakDistribution dist) {
cliResult.assertNoBuild();
cliResult.assertStarted();
}

@RawDistOnly(reason = "Containers are immutable")
@Test
@TestProvider(TestRealmResourceTestProvider.class)
void testAsyncBootstrapFails(KeycloakDistribution dist) {
RawKeycloakDistribution rawDist = dist.unwrap(RawKeycloakDistribution.class);
dist.setManualStop(true);
CLIResult result = dist.run("start", "--server-async-bootstrap=true", "--hostname-strict=false", "--db=dev-file", "--http-enabled=true", "--spi-realm-restapi-extension--test-resources--fail=true");
Awaitility.await().atMost(2, TimeUnit.MINUTES).until(() -> !rawDist.isRunning());
dist.stop();
result.assertMessage("Failed to start server");
result.assertMessage("I've failed");
assertEquals(1, dist.getExitCode());
}

}
10 changes: 6 additions & 4 deletions quarkus/tests/junit5/src/main/java/org/keycloak/Keycloak.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,20 @@ public static Path initTempDirectory(String name) {
String buildDir = System.getProperty("project.build.directory");
if (buildDir == null) {
try {
return Files.createTempDirectory(name).toAbsolutePath();
Path tempDirectory = Files.createTempDirectory(name);
tempDirectory.toFile().deleteOnExit();
return tempDirectory.toAbsolutePath();
} catch (IOException e) {
throw new RuntimeException("Could not create temporary directory", e);
}
} else {
Path homeDir = Path.of(buildDir, name);
Path tempDirectory = Path.of(buildDir, name);
try {
FileUtils.deleteDirectory(homeDir.toFile());
FileUtils.deleteDirectory(tempDirectory.toFile());
} catch (IOException e) {
e.printStackTrace();
}
return homeDir;
return tempDirectory;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public X509Certificate[] getAcceptedIssuers() {
return socketFactory;
}

private boolean isRunning() {
public boolean isRunning() {
return keycloak != null && keycloak.isAlive();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import java.io.File;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import jakarta.ws.rs.core.Application;
Expand Down Expand Up @@ -53,16 +51,6 @@ public abstract class KeycloakApplication<KSF extends KeycloakSessionFactory> ex
// Set to true when bootstrap is completed. It never changes back to false.
private static volatile boolean bootstrapCompleted = false;

public KeycloakApplication() {
try {
initTmpDirectory();
logger.debugv("Application: {0}", this.getClass().getName());
initAndStart();
} catch (Throwable t) {
exit(t);
}
}

public static String getTmpDirectory() {
return System.getProperty(KC_TMPDIR, System.getProperty("java.io.tmpdir"));
}
Expand All @@ -79,36 +67,20 @@ protected void initTmpDirectory() {
System.setProperty(KC_TMPDIR, tmpDir.getAbsolutePath());
}

protected abstract void exit(Throwable t);

protected abstract String getDataDir();

protected void startup() {
// synchronized to prevent shutdown while running bootstrapping
protected synchronized void startup() {
logger.debugv("Application: {0}", this.getClass().getName());
initTmpDirectory();
Profile.getInstance().logUnsupportedFeatures();
CryptoIntegration.init(KeycloakApplication.class.getClassLoader());
var ksf = createSessionFactory();
sessionFactory = ksf;

if (supportsAsyncInitialization()) {
final var executor = Executors.newSingleThreadExecutor();
CompletableFuture.runAsync(() -> runBootstrap(ksf), executor)
.exceptionally(throwable -> {
exit(throwable);
return null;
})
.thenRun(executor::shutdown);
return;
}

runBootstrap(ksf);
}

protected boolean supportsAsyncInitialization() {
return false;
}

// synchronized to prevent shutdown while running bootstrapping
private synchronized void runBootstrap(KSF keycloakSessionFactory) {
private void runBootstrap(KSF keycloakSessionFactory) {
var startTime = System.nanoTime();

initKeycloakSessionFactory(keycloakSessionFactory);
Expand Down Expand Up @@ -176,8 +148,6 @@ protected ExportImportManager bootstrap(KeycloakSession session) {

protected abstract void createTemporaryAdmin(KeycloakSession session);

protected abstract void initAndStart();

protected abstract KSF createSessionFactory();

protected abstract void initKeycloakSessionFactory(KSF ksf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public class ResteasyKeycloakApplication extends KeycloakApplication<ResteasyKey
protected Set<Class<?>> classes = new HashSet<>();

public ResteasyKeycloakApplication() {
Config.init(new JsonConfigProviderFactory().create()
.orElseThrow(() -> new RuntimeException("Failed to load Keycloak configuration")));
Profile.configure(
new PropertiesProfileConfigResolver(System.getProperties()),
new PropertiesFileProfileConfigResolver()
);
classes.add(RealmsResource.class);
if (Profile.isFeatureEnabled(Profile.Feature.ADMIN_API)) {
classes.add(AdminRoot.class);
Expand All @@ -67,18 +73,14 @@ public ResteasyKeycloakApplication() {
// an endpoint for the load balancer to gather information whether this site should receive requests or not.
classes.add(LoadBalancerResource.class);
}
startup();
}

@Override
protected String getDataDir() {
return System.getProperty("project.build.directory");
}

@Override
protected void exit(Throwable cause) {
throw new RuntimeException(cause);
}

@Override
public Set<Class<?>> getClasses() {
return classes;
Expand All @@ -104,17 +106,6 @@ protected void createTemporaryAdmin(KeycloakSession session) {
// do nothing
}

@Override
protected void initAndStart() {
Config.init(new JsonConfigProviderFactory().create()
.orElseThrow(() -> new RuntimeException("Failed to load Keycloak configuration")));
Profile.configure(
new PropertiesProfileConfigResolver(System.getProperties()),
new PropertiesFileProfileConfigResolver()
);
startup();
}

@Override
protected ExportImportManager bootstrap(KeycloakSession session) {
// created a nested transaction as this triggers DefaultJpaConnectionProviderFactory.lazyInit
Expand Down
Loading
0