Skip to content

Commit

Permalink
Merge pull request wildfly#17260 from pferraro/WFLY-18568
Browse files Browse the repository at this point in the history
WFLY-18568 (Revised) Disable cache operation client events if near cache is disabled.
  • Loading branch information
pferraro committed Oct 7, 2023
2 parents 0a5a3dc + 245c707 commit 2a7f565
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

package org.wildfly.clustering.ee.hotrod;

import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.configuration.NearCacheMode;
import org.wildfly.clustering.ee.Batcher;
import org.wildfly.clustering.ee.cache.CacheConfiguration;
import org.wildfly.clustering.ee.cache.CacheProperties;
Expand All @@ -20,6 +22,19 @@ public interface HotRodConfiguration extends CacheConfiguration {
@Override
<CK, CV> RemoteCache<CK, CV> getCache();

default Flag[] getIgnoreReturnFlags() {
return this.getNearCacheMode().enabled() ? new Flag[0] : new Flag[] { Flag.SKIP_LISTENER_NOTIFICATION };
}

default Flag[] getForceReturnFlags() {
return this.getNearCacheMode().enabled() ? new Flag[] { Flag.FORCE_RETURN_VALUE } : new Flag[] { Flag.FORCE_RETURN_VALUE, Flag.SKIP_LISTENER_NOTIFICATION };
}

default NearCacheMode getNearCacheMode() {
RemoteCache<?, ?> cache = this.getCache();
return cache.getRemoteCacheContainer().getConfiguration().remoteCaches().get(cache.getName()).nearCacheMode();
}

@Override
default CacheProperties getCacheProperties() {
return new RemoteCacheProperties(this.getCache());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.function.BiFunction;
import java.util.function.Function;

import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCache;
import org.wildfly.clustering.ee.Mutator;
import org.wildfly.clustering.ee.MutatorFactory;
Expand All @@ -22,15 +23,17 @@
public class RemoteCacheComputeMutatorFactory<K, V, O> implements MutatorFactory<K, O> {

private final RemoteCache<K, V> cache;
private final Flag[] flags;
private final Function<O, BiFunction<Object, V, V>> functionFactory;

public RemoteCacheComputeMutatorFactory(RemoteCache<K, V> cache, Function<O, BiFunction<Object, V, V>> functionFactory) {
public RemoteCacheComputeMutatorFactory(RemoteCache<K, V> cache, Flag[] flags, Function<O, BiFunction<Object, V, V>> functionFactory) {
this.cache = cache;
this.flags = flags;
this.functionFactory = functionFactory;
}

@Override
public Mutator createMutator(K key, O operand) {
return new RemoteCacheEntryComputeMutator<>(this.cache, key, this.functionFactory.apply(operand));
return new RemoteCacheEntryComputeMutator<>(this.cache, this.flags, key, this.functionFactory.apply(operand));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.function.BiFunction;
import java.util.function.Supplier;

import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCache;
import org.wildfly.clustering.ee.Mutator;
import org.wildfly.common.function.Functions;
Expand All @@ -23,16 +24,18 @@
public class RemoteCacheEntryComputeMutator<K, V> implements Mutator {

private final RemoteCache<K, V> cache;
private final Flag[] flags;
private final K key;
private final BiFunction<Object, V, V> function;
private final Supplier<Duration> maxIdle;

public RemoteCacheEntryComputeMutator(RemoteCache<K, V> cache, K key, BiFunction<Object, V, V> function) {
this(cache, key, function, Functions.constantSupplier(Duration.ZERO));
public RemoteCacheEntryComputeMutator(RemoteCache<K, V> cache, Flag[] flags, K key, BiFunction<Object, V, V> function) {
this(cache, flags, key, function, Functions.constantSupplier(Duration.ZERO));
}

public RemoteCacheEntryComputeMutator(RemoteCache<K, V> cache, K key, BiFunction<Object, V, V> function, Supplier<Duration> maxIdle) {
public RemoteCacheEntryComputeMutator(RemoteCache<K, V> cache, Flag[] flags, K key, BiFunction<Object, V, V> function, Supplier<Duration> maxIdle) {
this.cache = cache;
this.flags = flags;
this.key = key;
this.function = function;
this.maxIdle = maxIdle;
Expand All @@ -46,6 +49,6 @@ public void mutate() {
if (nanos > 0) {
seconds += 1;
}
this.cache.compute(this.key, this.function, 0, TimeUnit.SECONDS, seconds, TimeUnit.SECONDS);
this.cache.withFlags(this.flags).compute(this.key, this.function, 0, TimeUnit.SECONDS, seconds, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCache;
import org.wildfly.clustering.ee.Mutator;
import org.wildfly.common.function.Functions;
Expand All @@ -20,16 +21,18 @@
public class RemoteCacheEntryMutator<K, V> implements Mutator {

private final RemoteCache<K, V> cache;
private final Flag[] flags;
private final K id;
private final V value;
private final Supplier<Duration> maxIdle;

public RemoteCacheEntryMutator(RemoteCache<K, V> cache, K id, V value) {
this(cache, id, value, Functions.constantSupplier(Duration.ZERO));
public RemoteCacheEntryMutator(RemoteCache<K, V> cache, Flag[] flags, K id, V value) {
this(cache, flags, id, value, Functions.constantSupplier(Duration.ZERO));
}

public RemoteCacheEntryMutator(RemoteCache<K, V> cache, K id, V value, Supplier<Duration> maxIdle) {
public RemoteCacheEntryMutator(RemoteCache<K, V> cache, Flag[] flags, K id, V value, Supplier<Duration> maxIdle) {
this.cache = cache;
this.flags = flags;
this.id = id;
this.value = value;
this.maxIdle = maxIdle;
Expand All @@ -43,6 +46,6 @@ public void mutate() {
if (nanos > 0) {
seconds += 1;
}
this.cache.put(this.id, this.value, 0, TimeUnit.SECONDS, seconds, TimeUnit.SECONDS);
this.cache.withFlags(this.flags).put(this.id, this.value, 0, TimeUnit.SECONDS, seconds, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.time.Duration;
import java.util.function.Function;

import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCache;
import org.wildfly.clustering.ee.Mutator;
import org.wildfly.clustering.ee.MutatorFactory;
Expand All @@ -19,19 +20,21 @@
public class RemoteCacheMutatorFactory<K, V> implements MutatorFactory<K, V> {

private final RemoteCache<K, V> cache;
private final Flag[] flags;
private final Function<V, Duration> maxIdle;

public RemoteCacheMutatorFactory(RemoteCache<K, V> cache) {
this(cache, null);
public RemoteCacheMutatorFactory(RemoteCache<K, V> cache, Flag[] flags) {
this(cache, flags, null);
}

public RemoteCacheMutatorFactory(RemoteCache<K, V> cache, Function<V, Duration> maxIdle) {
public RemoteCacheMutatorFactory(RemoteCache<K, V> cache, Flag[] flags, Function<V, Duration> maxIdle) {
this.cache = cache;
this.flags = flags;
this.maxIdle = maxIdle;
}

@Override
public Mutator createMutator(K key, V value) {
return (this.maxIdle != null) ? new RemoteCacheEntryMutator<>(this.cache, key, value, () -> this.maxIdle.apply(value)) : new RemoteCacheEntryMutator<>(this.cache, key, value);
return (this.maxIdle != null) ? new RemoteCacheEntryMutator<>(this.cache, this.flags, key, value, () -> this.maxIdle.apply(value)) : new RemoteCacheEntryMutator<>(this.cache, this.flags, key, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
import java.util.function.Supplier;

import org.infinispan.client.hotrod.DefaultTemplate;
import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.configuration.NearCacheMode;
import org.infinispan.client.hotrod.configuration.RemoteCacheConfiguration;
import org.infinispan.client.hotrod.configuration.RemoteCacheConfigurationBuilder;
import org.infinispan.client.hotrod.configuration.TransactionMode;
import org.jboss.as.clustering.controller.CapabilityServiceConfigurator;
Expand Down Expand Up @@ -136,10 +134,7 @@ public Immutability getImmutability() {

@Override
public <K, V> RemoteCache<K, V> getCache() {
RemoteCache<K, V> cache = this.cache.get();
RemoteCacheConfiguration configuration = cache.getRemoteCacheContainer().getConfiguration().remoteCaches().get(cache.getName());
// Disable cache operation client events if near cache is disabled
return configuration.nearCacheMode().enabled() ? cache : cache.withFlags(Flag.SKIP_LISTENER_NOTIFICATION);
return this.cache.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.ServiceTarget;
import org.wildfly.clustering.ee.cache.tx.TransactionBatch;
import org.wildfly.clustering.ee.hotrod.HotRodConfiguration;
import org.wildfly.clustering.infinispan.client.service.RemoteCacheServiceConfigurator;
import org.wildfly.clustering.service.ServiceConfigurator;
import org.wildfly.clustering.service.ServiceSupplierDependency;
import org.wildfly.clustering.service.SimpleServiceNameProvider;
import org.wildfly.clustering.service.SupplierDependency;
import org.wildfly.clustering.web.hotrod.sso.HotRodSSOManagerFactory;
import org.wildfly.clustering.web.hotrod.sso.HotRodSSOManagerFactoryConfiguration;
import org.wildfly.clustering.web.sso.SSOManagerFactory;

/**
* @author Paul Ferraro
*/
public class HotRodSSOManagerFactoryServiceConfigurator<A, D, S> extends SimpleServiceNameProvider implements CapabilityServiceConfigurator, HotRodSSOManagerFactoryConfiguration {
public class HotRodSSOManagerFactoryServiceConfigurator<A, D, S> extends SimpleServiceNameProvider implements CapabilityServiceConfigurator, HotRodConfiguration {

private final String name;
private final HotRodSSOManagementConfiguration config;
Expand Down Expand Up @@ -73,7 +73,7 @@ public ServiceBuilder<?> build(ServiceTarget target) {

@SuppressWarnings("unchecked")
@Override
public <K, V> RemoteCache<K, V> getRemoteCache() {
public <K, V> RemoteCache<K, V> getCache() {
return (RemoteCache<K, V>) this.cache.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class HotRodSessionFactory<MC, AV, LC> extends CompositeSessionFactory<MC
private static final ThreadFactory THREAD_FACTORY = new DefaultThreadFactory(HotRodSessionFactory.class);

private final RemoteCache<SessionCreationMetaDataKey, SessionCreationMetaDataEntry<LC>> creationMetaDataCache;
private final Flag[] forceReturnFlags;
private final ImmutableSessionMetaDataFactory<SessionMetaDataEntry<LC>> metaDataFactory;
private final ImmutableSessionAttributesFactory<AV> attributesFactory;
private final Remover<String> attributesRemover;
Expand All @@ -75,6 +76,7 @@ public HotRodSessionFactory(HotRodSessionFactoryConfiguration config, SessionMet
this.attributesFactory = attributesFactory;
this.attributesRemover = attributesFactory;
this.creationMetaDataCache = config.getCache();
this.forceReturnFlags = config.getForceReturnFlags();
this.executor = Executors.newFixedThreadPool(config.getExpirationThreadPoolSize(), THREAD_FACTORY);
this.creationMetaDataCache.addClientListener(this);
}
Expand All @@ -93,6 +95,7 @@ public void close() {
@ClientCacheEntryExpired
public void expired(ClientCacheEntryExpiredEvent<SessionAccessMetaDataKey> event) {
RemoteCache<SessionCreationMetaDataKey, SessionCreationMetaDataEntry<LC>> creationMetaDataCache = this.creationMetaDataCache;
Flag[] forceReturnFlags = this.forceReturnFlags;
ImmutableSessionMetaDataFactory<SessionMetaDataEntry<LC>> metaDataFactory = this.metaDataFactory;
ImmutableSessionAttributesFactory<AV> attributesFactory = this.attributesFactory;
Remover<String> attributesRemover = this.attributesRemover;
Expand All @@ -101,7 +104,7 @@ public void expired(ClientCacheEntryExpiredEvent<SessionAccessMetaDataKey> event
Runnable task = new Runnable() {
@Override
public void run() {
SessionCreationMetaDataEntry<LC> creationMetaDataEntry = creationMetaDataCache.withFlags(Flag.FORCE_RETURN_VALUE).remove(new SessionCreationMetaDataKey(id));
SessionCreationMetaDataEntry<LC> creationMetaDataEntry = creationMetaDataCache.withFlags(forceReturnFlags).remove(new SessionCreationMetaDataKey(id));
if (creationMetaDataEntry != null) {
AV attributesValue = attributesFactory.findValue(id);
if (attributesValue != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCache;
import org.wildfly.clustering.ee.Immutability;
import org.wildfly.clustering.ee.Mutator;
Expand All @@ -35,6 +36,7 @@
public class CoarseSessionAttributesFactory<S, C, L, V> implements SessionAttributesFactory<C, Map<String, Object>> {

private final RemoteCache<SessionAttributesKey, V> cache;
private final Flag[] ignoreReturnFlags;
private final Marshaller<Map<String, Object>, V> marshaller;
private final Immutability immutability;
private final CacheProperties properties;
Expand All @@ -43,10 +45,11 @@ public class CoarseSessionAttributesFactory<S, C, L, V> implements SessionAttrib

public CoarseSessionAttributesFactory(HotRodSessionAttributesFactoryConfiguration<S, C, L, Map<String, Object>, V> configuration) {
this.cache = configuration.getCache();
this.ignoreReturnFlags = configuration.getIgnoreReturnFlags();
this.marshaller = configuration.getMarshaller();
this.immutability = configuration.getImmutability();
this.properties = configuration.getCacheProperties();
this.mutatorFactory = new RemoteCacheMutatorFactory<>(this.cache);
this.mutatorFactory = new RemoteCacheMutatorFactory<>(this.cache, this.ignoreReturnFlags);
this.provider = configuration.getHttpSessionActivationListenerProvider();
}

Expand All @@ -55,7 +58,7 @@ public Map<String, Object> createValue(String id, Void context) {
Map<String, Object> attributes = new ConcurrentHashMap<>();
try {
V value = this.marshaller.write(attributes);
this.cache.put(new SessionAttributesKey(id), value);
this.cache.withFlags(this.ignoreReturnFlags).put(new SessionAttributesKey(id), value);
return attributes;
} catch (IOException e) {
throw new IllegalStateException(e);
Expand Down Expand Up @@ -94,7 +97,7 @@ public ImmutableSessionAttributes createImmutableSessionAttributes(String id, Ma

@Override
public boolean remove(String id) {
this.cache.remove(new SessionAttributesKey(id));
this.cache.withFlags(this.ignoreReturnFlags).remove(new SessionAttributesKey(id));
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCache;
import org.wildfly.clustering.ee.Immutability;
import org.wildfly.clustering.ee.MutatorFactory;
Expand Down Expand Up @@ -38,6 +39,7 @@
public class FineSessionAttributesFactory<S, C, L, V> implements SessionAttributesFactory<C, Map<String, Object>> {

private final RemoteCache<SessionAttributesKey, Map<String, V>> cache;
private final Flag[] ignoreReturnFlags;
private final Marshaller<Object, V> marshaller;
private final Immutability immutability;
private final CacheProperties properties;
Expand All @@ -46,10 +48,11 @@ public class FineSessionAttributesFactory<S, C, L, V> implements SessionAttribut

public FineSessionAttributesFactory(HotRodSessionAttributesFactoryConfiguration<S, C, L, Object, V> configuration) {
this.cache = configuration.getCache();
this.ignoreReturnFlags = configuration.getIgnoreReturnFlags();
this.marshaller = configuration.getMarshaller();
this.immutability = configuration.getImmutability();
this.properties = configuration.getCacheProperties();
this.mutatorFactory = new RemoteCacheComputeMutatorFactory<>(this.cache, SessionAttributeMapComputeFunction::new);
this.mutatorFactory = new RemoteCacheComputeMutatorFactory<>(this.cache, this.ignoreReturnFlags, SessionAttributeMapComputeFunction::new);
this.provider = configuration.getHttpSessionActivationListenerProvider();
}

Expand Down Expand Up @@ -90,7 +93,7 @@ private Map<String, Object> getValue(String id, boolean purgeIfInvalid) {

@Override
public boolean remove(String id) {
this.cache.remove(new SessionAttributesKey(id));
this.cache.withFlags(this.ignoreReturnFlags).remove(new SessionAttributesKey(id));
return true;
}

Expand Down
Loading

0 comments on commit 2a7f565

Please sign in to comment.