-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test delete entry in remote Infinispan and check removals are replica…
…ted to the second site (#823) Signed-off-by: Michal Hajas <[email protected]> Signed-off-by: Alexander Schwartz <[email protected]> Co-authored-by: Alexander Schwartz <[email protected]>
- Loading branch information
Showing
11 changed files
with
414 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,64 +18,82 @@ | |
|
||
package org.keycloak.benchmark.cache; | ||
|
||
import java.util.UUID; | ||
|
||
import jakarta.ws.rs.DefaultValue; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.NotFoundException; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.PathParam; | ||
import jakarta.ws.rs.Produces; | ||
|
||
import org.infinispan.Cache; | ||
import org.infinispan.commons.CacheConfigurationException; | ||
import jakarta.ws.rs.QueryParam; | ||
import org.infinispan.commons.api.BasicCache; | ||
import org.jboss.logging.Logger; | ||
import org.jboss.resteasy.reactive.NoCache; | ||
import org.keycloak.benchmark.dataset.TaskResponse; | ||
import org.keycloak.connections.infinispan.InfinispanConnectionProvider; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.utils.MediaType; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Marek Posolda</a> | ||
*/ | ||
public class CacheResource { | ||
public abstract class CacheResource { | ||
|
||
protected static final Logger logger = Logger.getLogger(CacheResource.class); | ||
|
||
private final Cache<Object, Object> cache; | ||
|
||
public CacheResource(KeycloakSession session, String cacheName) { | ||
InfinispanConnectionProvider provider = session.getProvider(InfinispanConnectionProvider.class); | ||
try { | ||
this.cache = provider.getCache(cacheName); | ||
} catch (CacheConfigurationException cce) { | ||
logger.error(cce.getMessage()); | ||
throw new NotFoundException("Cache does not exists"); | ||
} | ||
} | ||
public abstract BasicCache<Object, Object> getCache(); | ||
|
||
|
||
@GET | ||
@Path("/clear") | ||
@NoCache | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public TaskResponse clear() { | ||
cache.clear(); | ||
logger.infof("Cache %s cleared successfully", cache.getName()); | ||
return TaskResponse.statusMessage("Cache " + cache.getName() + " cleared successfully"); | ||
getCache().clear(); | ||
logger.infof("Cache %s cleared successfully", getCache().getName()); | ||
return TaskResponse.statusMessage("Cache " + getCache().getName() + " cleared successfully"); | ||
} | ||
|
||
@GET | ||
@Path("/contains/{id}") | ||
@NoCache | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public boolean contains(@PathParam("id") String id) { | ||
if (cache.containsKey(id)) { | ||
if (getCache().containsKey(id)) { | ||
return true; | ||
} else if (id.length() == 36) { | ||
try { | ||
UUID uuid = UUID.fromString(id); | ||
return getCache().containsKey(uuid); | ||
} catch (IllegalArgumentException iae) { | ||
logger.warnf("Given string %s not an UUID", id); | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@GET | ||
@Path("/size") | ||
@NoCache | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public int size() { | ||
return getCache().size(); | ||
} | ||
|
||
protected abstract BasicCache<Object, Object> decorateCacheForRemovalAndSkipListenersIfTrue(boolean skipListeners); | ||
|
||
@GET | ||
@Path("/remove/{id}") | ||
@NoCache | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public boolean remove(@PathParam("id") String id, @QueryParam("skipListeners") @DefaultValue("false") boolean skipListeners) { | ||
if (decorateCacheForRemovalAndSkipListenersIfTrue(skipListeners).remove(id) != null) { | ||
return true; | ||
} else if (id.length() == 36) { | ||
try { | ||
UUID uuid = UUID.fromString(id); | ||
return cache.containsKey(uuid); | ||
return decorateCacheForRemovalAndSkipListenersIfTrue(skipListeners).remove(uuid) != null; | ||
} catch (IllegalArgumentException iae) { | ||
logger.warnf("Given string %s not an UUID", id); | ||
return false; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
dataset/src/main/java/org/keycloak/benchmark/cache/EmbeddedCacheResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.keycloak.benchmark.cache; | ||
|
||
import jakarta.ws.rs.NotFoundException; | ||
import org.infinispan.Cache; | ||
import org.infinispan.commons.CacheConfigurationException; | ||
import org.infinispan.commons.api.BasicCache; | ||
import org.keycloak.connections.infinispan.InfinispanConnectionProvider; | ||
import org.keycloak.models.KeycloakSession; | ||
|
||
public class EmbeddedCacheResource extends CacheResource { | ||
|
||
private final Cache<Object, Object> cache; | ||
|
||
public EmbeddedCacheResource(KeycloakSession session, String cacheName) { | ||
InfinispanConnectionProvider provider = session.getProvider(InfinispanConnectionProvider.class); | ||
try { | ||
this.cache = provider.getCache(cacheName); | ||
} catch (CacheConfigurationException cce) { | ||
logger.error(cce.getMessage()); | ||
throw new NotFoundException("Cache does not exists"); | ||
} | ||
} | ||
|
||
@Override | ||
public BasicCache<Object, Object> getCache() { | ||
return cache; | ||
} | ||
|
||
@Override | ||
protected BasicCache<Object, Object> decorateCacheForRemovalAndSkipListenersIfTrue(boolean skipListeners) { | ||
return skipListeners | ||
? cache.getAdvancedCache().withFlags(org.infinispan.context.Flag.SKIP_CACHE_STORE) | ||
: cache.getAdvancedCache(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,29 +18,21 @@ | |
|
||
package org.keycloak.benchmark.cache; | ||
|
||
import java.util.UUID; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.NotFoundException; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.PathParam; | ||
import jakarta.ws.rs.Produces; | ||
|
||
import org.infinispan.Cache; | ||
import org.infinispan.client.hotrod.Flag; | ||
import org.infinispan.client.hotrod.RemoteCache; | ||
import org.infinispan.commons.CacheConfigurationException; | ||
import org.infinispan.commons.api.BasicCache; | ||
import org.jboss.logging.Logger; | ||
import org.jboss.resteasy.reactive.NoCache; | ||
import org.keycloak.benchmark.dataset.TaskResponse; | ||
import org.keycloak.connections.infinispan.InfinispanConnectionProvider; | ||
import org.keycloak.connections.infinispan.InfinispanUtil; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.utils.MediaType; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Marek Posolda</a> | ||
*/ | ||
public class RemoteCacheResource { | ||
public class RemoteCacheResource extends CacheResource { | ||
|
||
protected static final Logger logger = Logger.getLogger(RemoteCacheResource.class); | ||
|
||
|
@@ -65,33 +57,15 @@ public RemoteCacheResource(KeycloakSession session, String cacheName) { | |
} | ||
|
||
|
||
@GET | ||
@Path("/clear") | ||
@NoCache | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public TaskResponse clear() { | ||
remoteCache.clear(); | ||
logger.infof("Remote cache %s cleared successfully", remoteCache.getName()); | ||
return TaskResponse.statusMessage("Remote cache " + remoteCache.getName() + " cleared successfully"); | ||
@Override | ||
public BasicCache<Object, Object> getCache() { | ||
return remoteCache; | ||
} | ||
|
||
@GET | ||
@Path("/contains/{id}") | ||
@NoCache | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public boolean contains(@PathParam("id") String id) { | ||
if (remoteCache.containsKey(id)) { | ||
return true; | ||
} else if (id.length() == 36) { | ||
try { | ||
UUID uuid = UUID.fromString(id); | ||
return remoteCache.containsKey(uuid); | ||
} catch (IllegalArgumentException iae) { | ||
logger.warnf("Given string %s not an UUID", id); | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
@Override | ||
protected BasicCache<Object, Object> decorateCacheForRemovalAndSkipListenersIfTrue(boolean skipListeners) { | ||
return skipListeners | ||
? remoteCache.withFlags(Flag.SKIP_LISTENER_NOTIFICATION, Flag.FORCE_RETURN_VALUE) | ||
: remoteCache.withFlags(Flag.FORCE_RETURN_VALUE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.