Make LockAcquiringTimeoutException a RuntimeException

Closes #16690
This commit is contained in:
Alexander Schwartz 2023-01-30 11:23:14 +01:00 committed by Michal Hajas
parent 33ff9ef17e
commit c6aba2e3de
7 changed files with 37 additions and 71 deletions

View file

@ -16,7 +16,6 @@
*/
package org.keycloak.models.map.storage.hotRod.connections;
import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.RemoteCacheManagerAdmin;
@ -29,7 +28,6 @@ import org.infinispan.query.remote.client.ProtobufMetadataManagerConstants;
import org.jboss.logging.Logger;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.locking.LockAcquiringTimeoutException;
import org.keycloak.models.map.storage.hotRod.locking.HotRodLocksUtils;
import org.keycloak.models.map.storage.hotRod.common.HotRodEntityDescriptor;
import org.keycloak.models.map.storage.hotRod.common.CommonPrimitivesProtoSchemaInitializer;
@ -155,8 +153,6 @@ public class DefaultHotRodConnectionProviderFactory implements HotRodConnectionP
}
LOG.infof("HotRod client configuration was successful.");
} catch (LockAcquiringTimeoutException e) {
throw new RuntimeException(e);
} finally {
if (!HotRodLocksUtils.removeWithInstanceIdentifier(locksCache, HOT_ROD_INIT_LOCK_NAME)) {
throw new RuntimeException("Cannot release HotRod init lock");

View file

@ -17,7 +17,6 @@
package org.keycloak.models.map.storage.hotRod.locking;
import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCache;
import org.jboss.logging.Logger;
import org.keycloak.models.KeycloakSession;

View file

@ -76,7 +76,6 @@ import org.keycloak.models.UserLoginFailureModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.UserSessionModel;
import org.keycloak.models.locking.GlobalLockProvider;
import org.keycloak.models.locking.LockAcquiringTimeoutException;
import org.keycloak.models.map.client.MapProtocolMapperEntity;
import org.keycloak.models.map.client.MapProtocolMapperEntityImpl;
import org.keycloak.models.map.common.DeepCloner;
@ -516,13 +515,9 @@ public class JpaMapStorageProviderFactory implements
}
private void update(Class<?> modelType, Connection connection, KeycloakSession session) {
try {
session.getProvider(GlobalLockProvider.class).withLock(modelType.getName(), lockedSession -> {
lockedSession.getProvider(MapJpaUpdaterProvider.class).update(modelType, connection, config.get("schema"));
return null;
});
} catch (LockAcquiringTimeoutException e) {
throw new RuntimeException("Acquiring " + modelType.getName() + " failed.", e);
}
}
}

View file

@ -52,7 +52,6 @@ import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.dblock.DBLockGlobalLockProvider;
import org.keycloak.models.locking.GlobalLockProvider;
import org.keycloak.models.locking.LockAcquiringTimeoutException;
import org.keycloak.provider.EnvironmentDependentProviderFactory;
import org.keycloak.provider.ProviderConfigProperty;
import org.keycloak.provider.ProviderConfigurationBuilder;
@ -290,27 +289,19 @@ public class LegacyJpaConnectionProviderFactory extends AbstractJpaConnectionPro
private void update(Connection connection, String schema, KeycloakSession session, JpaUpdaterProvider updater) {
GlobalLockProvider globalLock = session.getProvider(GlobalLockProvider.class);
try {
globalLock.withLock(DBLockGlobalLockProvider.DATABASE, innerSession -> {
updater.update(connection, schema);
return null;
});
} catch (LockAcquiringTimeoutException e) {
throw new RuntimeException("Acquiring database failed.", e);
}
}
private void export(Connection connection, String schema, File databaseUpdateFile, KeycloakSession session,
JpaUpdaterProvider updater) {
GlobalLockProvider globalLock = session.getProvider(GlobalLockProvider.class);
try {
globalLock.withLock(DBLockGlobalLockProvider.DATABASE, innerSession -> {
updater.export(connection, schema, databaseUpdateFile);
return null;
});
} catch (LockAcquiringTimeoutException e) {
throw new RuntimeException("Acquiring database failed.", e);
}
}
@Override

View file

@ -22,9 +22,7 @@ import java.time.Instant;
/**
* This exception is thrown when acquiring a lock times out.
*/
public final class LockAcquiringTimeoutException extends Exception {
private LockAcquiringTimeoutException() {}
public final class LockAcquiringTimeoutException extends RuntimeException {
/**
*

View file

@ -33,7 +33,6 @@ import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.UserProvider;
import org.keycloak.models.locking.GlobalLockProvider;
import org.keycloak.models.locking.LockAcquiringTimeoutException;
import org.keycloak.models.utils.KeycloakModelUtils;
import org.keycloak.models.utils.PostMigrationEvent;
import org.keycloak.models.utils.RepresentationToModel;
@ -143,11 +142,7 @@ public class KeycloakApplication extends Application {
@Override
public void run(KeycloakSession session) {
GlobalLockProvider locks = session.getProvider(GlobalLockProvider.class);
try {
exportImportManager[0] = locks.withLock(GlobalLockProvider.Constants.KEYCLOAK_BOOT, innerSession -> bootstrap());
} catch (LockAcquiringTimeoutException e) {
throw new RuntimeException("Acquiring keycloak-boot lock failed.", e);
}
}
});

View file

@ -67,7 +67,6 @@ public class GlobalLocksTest extends KeycloakModelTest {
GlobalLockProvider lockProvider = s.getProvider(GlobalLockProvider.class);
LOG.infof("Iteration %d entered session", index);
try {
lockProvider.withLock(LOCK_NAME, Duration.ofSeconds(60), innerSession -> {
LOG.infof("Iteration %d entered locked block", index);
@ -84,9 +83,6 @@ public class GlobalLocksTest extends KeycloakModelTest {
resultingList.add(c);
return null;
});
} catch (LockAcquiringTimeoutException e) {
throw new RuntimeException(e);
}
}));
assertThat(resultingList, hasSize(numIterations));
@ -145,7 +141,6 @@ public class GlobalLocksTest extends KeycloakModelTest {
.forEach(i ->
inComittedTransaction(s -> {
GlobalLockProvider lockProvider = s.getProvider(GlobalLockProvider.class);
try {
lockProvider.withLock("LOCK_" + i, session -> {
locksAcquired.countDown();
try {
@ -156,9 +151,6 @@ public class GlobalLocksTest extends KeycloakModelTest {
}
return null;
});
} catch (LockAcquiringTimeoutException e) {
throw new RuntimeException(e);
}
})
);
});