parent
33ff9ef17e
commit
c6aba2e3de
7 changed files with 37 additions and 71 deletions
|
@ -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");
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
session.getProvider(GlobalLockProvider.class).withLock(modelType.getName(), lockedSession -> {
|
||||
lockedSession.getProvider(MapJpaUpdaterProvider.class).update(modelType, connection, config.get("schema"));
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
globalLock.withLock(DBLockGlobalLockProvider.DATABASE, innerSession -> {
|
||||
updater.update(connection, schema);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
globalLock.withLock(DBLockGlobalLockProvider.DATABASE, innerSession -> {
|
||||
updater.export(connection, schema, databaseUpdateFile);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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 {
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
exportImportManager[0] = locks.withLock(GlobalLockProvider.Constants.KEYCLOAK_BOOT, innerSession -> bootstrap());
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -67,26 +67,22 @@ 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);
|
||||
lockProvider.withLock(LOCK_NAME, Duration.ofSeconds(60), innerSession -> {
|
||||
LOG.infof("Iteration %d entered locked block", index);
|
||||
|
||||
// Locked block
|
||||
int c = counter.getAndIncrement();
|
||||
// Locked block
|
||||
int c = counter.getAndIncrement();
|
||||
|
||||
try {
|
||||
Thread.sleep(rand.nextInt(100));
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
try {
|
||||
Thread.sleep(rand.nextInt(100));
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
resultingList.add(c);
|
||||
return null;
|
||||
});
|
||||
} catch (LockAcquiringTimeoutException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
resultingList.add(c);
|
||||
return null;
|
||||
});
|
||||
}));
|
||||
|
||||
assertThat(resultingList, hasSize(numIterations));
|
||||
|
@ -145,20 +141,16 @@ public class GlobalLocksTest extends KeycloakModelTest {
|
|||
.forEach(i ->
|
||||
inComittedTransaction(s -> {
|
||||
GlobalLockProvider lockProvider = s.getProvider(GlobalLockProvider.class);
|
||||
try {
|
||||
lockProvider.withLock("LOCK_" + i, session -> {
|
||||
locksAcquired.countDown();
|
||||
try {
|
||||
testFinished.await();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
} catch (LockAcquiringTimeoutException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
lockProvider.withLock("LOCK_" + i, session -> {
|
||||
locksAcquired.countDown();
|
||||
try {
|
||||
testFinished.await();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue