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; 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.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager; import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.RemoteCacheManagerAdmin; import org.infinispan.client.hotrod.RemoteCacheManagerAdmin;
@ -29,7 +28,6 @@ import org.infinispan.query.remote.client.ProtobufMetadataManagerConstants;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory; 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.locking.HotRodLocksUtils;
import org.keycloak.models.map.storage.hotRod.common.HotRodEntityDescriptor; import org.keycloak.models.map.storage.hotRod.common.HotRodEntityDescriptor;
import org.keycloak.models.map.storage.hotRod.common.CommonPrimitivesProtoSchemaInitializer; 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."); LOG.infof("HotRod client configuration was successful.");
} catch (LockAcquiringTimeoutException e) {
throw new RuntimeException(e);
} finally { } finally {
if (!HotRodLocksUtils.removeWithInstanceIdentifier(locksCache, HOT_ROD_INIT_LOCK_NAME)) { if (!HotRodLocksUtils.removeWithInstanceIdentifier(locksCache, HOT_ROD_INIT_LOCK_NAME)) {
throw new RuntimeException("Cannot release HotRod init lock"); throw new RuntimeException("Cannot release HotRod init lock");

View file

@ -17,7 +17,6 @@
package org.keycloak.models.map.storage.hotRod.locking; package org.keycloak.models.map.storage.hotRod.locking;
import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCache; import org.infinispan.client.hotrod.RemoteCache;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.keycloak.models.KeycloakSession; 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.UserModel;
import org.keycloak.models.UserSessionModel; import org.keycloak.models.UserSessionModel;
import org.keycloak.models.locking.GlobalLockProvider; 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.MapProtocolMapperEntity;
import org.keycloak.models.map.client.MapProtocolMapperEntityImpl; import org.keycloak.models.map.client.MapProtocolMapperEntityImpl;
import org.keycloak.models.map.common.DeepCloner; import org.keycloak.models.map.common.DeepCloner;
@ -516,13 +515,9 @@ public class JpaMapStorageProviderFactory implements
} }
private void update(Class<?> modelType, Connection connection, KeycloakSession session) { private void update(Class<?> modelType, Connection connection, KeycloakSession session) {
try { session.getProvider(GlobalLockProvider.class).withLock(modelType.getName(), lockedSession -> {
session.getProvider(GlobalLockProvider.class).withLock(modelType.getName(), lockedSession -> { lockedSession.getProvider(MapJpaUpdaterProvider.class).update(modelType, connection, config.get("schema"));
lockedSession.getProvider(MapJpaUpdaterProvider.class).update(modelType, connection, config.get("schema")); return null;
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.KeycloakSessionFactory;
import org.keycloak.models.dblock.DBLockGlobalLockProvider; import org.keycloak.models.dblock.DBLockGlobalLockProvider;
import org.keycloak.models.locking.GlobalLockProvider; import org.keycloak.models.locking.GlobalLockProvider;
import org.keycloak.models.locking.LockAcquiringTimeoutException;
import org.keycloak.provider.EnvironmentDependentProviderFactory; import org.keycloak.provider.EnvironmentDependentProviderFactory;
import org.keycloak.provider.ProviderConfigProperty; import org.keycloak.provider.ProviderConfigProperty;
import org.keycloak.provider.ProviderConfigurationBuilder; 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) { private void update(Connection connection, String schema, KeycloakSession session, JpaUpdaterProvider updater) {
GlobalLockProvider globalLock = session.getProvider(GlobalLockProvider.class); GlobalLockProvider globalLock = session.getProvider(GlobalLockProvider.class);
try { globalLock.withLock(DBLockGlobalLockProvider.DATABASE, innerSession -> {
globalLock.withLock(DBLockGlobalLockProvider.DATABASE, innerSession -> { updater.update(connection, schema);
updater.update(connection, schema); return null;
return null; });
});
} catch (LockAcquiringTimeoutException e) {
throw new RuntimeException("Acquiring database failed.", e);
}
} }
private void export(Connection connection, String schema, File databaseUpdateFile, KeycloakSession session, private void export(Connection connection, String schema, File databaseUpdateFile, KeycloakSession session,
JpaUpdaterProvider updater) { JpaUpdaterProvider updater) {
GlobalLockProvider globalLock = session.getProvider(GlobalLockProvider.class); GlobalLockProvider globalLock = session.getProvider(GlobalLockProvider.class);
try { globalLock.withLock(DBLockGlobalLockProvider.DATABASE, innerSession -> {
globalLock.withLock(DBLockGlobalLockProvider.DATABASE, innerSession -> { updater.export(connection, schema, databaseUpdateFile);
updater.export(connection, schema, databaseUpdateFile); return null;
return null; });
});
} catch (LockAcquiringTimeoutException e) {
throw new RuntimeException("Acquiring database failed.", e);
}
} }
@Override @Override

View file

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

View file

@ -33,7 +33,6 @@ import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.models.UserProvider; import org.keycloak.models.UserProvider;
import org.keycloak.models.locking.GlobalLockProvider; import org.keycloak.models.locking.GlobalLockProvider;
import org.keycloak.models.locking.LockAcquiringTimeoutException;
import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.models.utils.KeycloakModelUtils;
import org.keycloak.models.utils.PostMigrationEvent; import org.keycloak.models.utils.PostMigrationEvent;
import org.keycloak.models.utils.RepresentationToModel; import org.keycloak.models.utils.RepresentationToModel;
@ -143,11 +142,7 @@ public class KeycloakApplication extends Application {
@Override @Override
public void run(KeycloakSession session) { public void run(KeycloakSession session) {
GlobalLockProvider locks = session.getProvider(GlobalLockProvider.class); GlobalLockProvider locks = session.getProvider(GlobalLockProvider.class);
try { exportImportManager[0] = locks.withLock(GlobalLockProvider.Constants.KEYCLOAK_BOOT, innerSession -> bootstrap());
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,26 +67,22 @@ public class GlobalLocksTest extends KeycloakModelTest {
GlobalLockProvider lockProvider = s.getProvider(GlobalLockProvider.class); GlobalLockProvider lockProvider = s.getProvider(GlobalLockProvider.class);
LOG.infof("Iteration %d entered session", index); LOG.infof("Iteration %d entered session", index);
try { lockProvider.withLock(LOCK_NAME, Duration.ofSeconds(60), innerSession -> {
lockProvider.withLock(LOCK_NAME, Duration.ofSeconds(60), innerSession -> { LOG.infof("Iteration %d entered locked block", index);
LOG.infof("Iteration %d entered locked block", index);
// Locked block // Locked block
int c = counter.getAndIncrement(); int c = counter.getAndIncrement();
try { try {
Thread.sleep(rand.nextInt(100)); Thread.sleep(rand.nextInt(100));
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
throw new RuntimeException(e); throw new RuntimeException(e);
} }
resultingList.add(c); resultingList.add(c);
return null; return null;
}); });
} catch (LockAcquiringTimeoutException e) {
throw new RuntimeException(e);
}
})); }));
assertThat(resultingList, hasSize(numIterations)); assertThat(resultingList, hasSize(numIterations));
@ -145,20 +141,16 @@ public class GlobalLocksTest extends KeycloakModelTest {
.forEach(i -> .forEach(i ->
inComittedTransaction(s -> { inComittedTransaction(s -> {
GlobalLockProvider lockProvider = s.getProvider(GlobalLockProvider.class); GlobalLockProvider lockProvider = s.getProvider(GlobalLockProvider.class);
try { lockProvider.withLock("LOCK_" + i, session -> {
lockProvider.withLock("LOCK_" + i, session -> { locksAcquired.countDown();
locksAcquired.countDown(); try {
try { testFinished.await();
testFinished.await(); } catch (InterruptedException e) {
} catch (InterruptedException e) { Thread.currentThread().interrupt();
Thread.currentThread().interrupt(); throw new RuntimeException(e);
throw new RuntimeException(e); }
} return null;
return null; });
});
} catch (LockAcquiringTimeoutException e) {
throw new RuntimeException(e);
}
}) })
); );
}); });