Skip to content

Commit

Permalink
ISPN-16586 Fix modification of default expiration at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
tristantarrant authored and wburns committed Oct 17, 2024
1 parent 56bf1bd commit bb75ddb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
12 changes: 10 additions & 2 deletions core/src/main/java/org/infinispan/cache/impl/CacheImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.infinispan.commons.util.Version;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.Configurations;
import org.infinispan.configuration.cache.ExpirationConfiguration;
import org.infinispan.configuration.format.PropertyFormatter;
import org.infinispan.configuration.global.GlobalConfiguration;
import org.infinispan.container.DataContainer;
Expand Down Expand Up @@ -177,7 +178,7 @@ public class CacheImpl<K, V> implements AdvancedCache<K, V>, InternalCache<K, V>
@Inject ComponentRef<AdvancedCache> encoderCache;
@Inject GroupManager groupManager;

protected Metadata defaultMetadata;
protected volatile Metadata defaultMetadata;
private final String name;
private volatile boolean stopping = false;
private boolean transactional;
Expand All @@ -196,11 +197,18 @@ public CacheImpl(String name) {
public void preStart() {
// We have to do this before start, since some components may start before the actual cache and they
// have to have access to the default metadata on some operations
defaultMetadata = Configurations.newDefaultMetadata(config);
updateDefaultMetadata();
// Listen for changes to the defaults
config.expiration().attributes().attribute(ExpirationConfiguration.LIFESPAN).addListener((attribute, oldValue) -> updateDefaultMetadata());
config.expiration().attributes().attribute(ExpirationConfiguration.MAX_IDLE).addListener((attribute, oldValue) -> updateDefaultMetadata());
transactional = config.transaction().transactionMode().isTransactional();
batchingEnabled = config.invocationBatching().enabled();
}

private void updateDefaultMetadata() {
defaultMetadata = Configurations.newDefaultMetadata(config);
}

@Override
public ComponentRegistry getComponentRegistry() {
return componentRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import org.infinispan.commons.time.ControlledTimeService;
import org.infinispan.commons.time.TimeService;
import org.infinispan.commons.util.TimeQuantity;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.configuration.cache.ExpirationConfiguration;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.metadata.EmbeddedMetadata;
import org.infinispan.test.SingleCacheManagerTest;
import org.infinispan.test.TestingUtil;
import org.infinispan.test.fwk.TestCacheManagerFactory;
import org.infinispan.commons.time.ControlledTimeService;
import org.infinispan.commons.time.TimeService;
import org.testng.annotations.Test;

@Test(groups = "functional", testName = "api.MetadataAPIDefaultExpiryTest")
Expand Down Expand Up @@ -80,6 +83,23 @@ public void testDefaultLifespanPutAsync() throws Exception {
expectCachedThenExpired(2, "v2");
}

public void updateExpiration() {
cache().put(1, "value");
CacheEntry<Object, Object> entry = cache().getAdvancedCache().getCacheEntry(1);
assertEquals(EXPIRATION_TIMEOUT, entry.getLifespan());
assertEquals(-1, entry.getMaxIdle());
cache().getCacheConfiguration().expiration().attributes().attribute(ExpirationConfiguration.LIFESPAN).set(TimeQuantity.valueOf(EXPIRATION_TIMEOUT * 2));
cache().put(2, "value");
entry = cache().getAdvancedCache().getCacheEntry(2);
assertEquals(EXPIRATION_TIMEOUT * 2, entry.getLifespan());
assertEquals(-1, entry.getMaxIdle());
cache().getCacheConfiguration().expiration().attributes().attribute(ExpirationConfiguration.MAX_IDLE).set(TimeQuantity.valueOf(EXPIRATION_TIMEOUT * 3));
cache().put(3, "value");
entry = cache().getAdvancedCache().getCacheEntry(3);
assertEquals(EXPIRATION_TIMEOUT * 2, entry.getLifespan());
assertEquals(EXPIRATION_TIMEOUT * 3, entry.getMaxIdle());
}

private void expectCachedThenExpired(Integer key, String value) {
// Check that it doesn't expire too early
controlledTimeService.advance(EXPIRATION_TIMEOUT - 1);
Expand Down
4 changes: 1 addition & 3 deletions core/src/test/java/org/infinispan/expiry/ExpiryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.infinispan.Cache;
import org.infinispan.commons.test.CommonsTestingUtil;
import org.infinispan.commons.time.ControlledTimeService;
import org.infinispan.commons.time.TimeService;
import org.infinispan.commons.util.Util;
import org.infinispan.configuration.cache.ConfigurationBuilder;
Expand All @@ -27,7 +28,6 @@
import org.infinispan.test.AbstractInfinispanTest;
import org.infinispan.test.TestingUtil;
import org.infinispan.test.fwk.TestCacheManagerFactory;
import org.infinispan.commons.time.ControlledTimeService;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand All @@ -37,7 +37,6 @@ public class ExpiryTest extends AbstractInfinispanTest {

public static final int EXPIRATION_TIMEOUT = 3000;
public static final int IDLE_TIMEOUT = 3000;
public static final int EXPIRATION_CHECK_TIMEOUT = 2000;
CacheContainer cm;

protected ControlledTimeService timeService;
Expand Down Expand Up @@ -374,7 +373,6 @@ private void doValuesAfterExpiryInTransaction(Method m, CacheContainer cc) throw
// Values come as a Collection, but comparison of HashMap#Values is done
// by reference equality, so wrap the collection around to set to make
// testing easier, given that we know that there are dup values.
Collection<String> values;
Map<Integer, String> dataIn = new HashMap<>();
dataIn.put(1, v(m, 1));
dataIn.put(2, v(m, 2));
Expand Down

0 comments on commit bb75ddb

Please sign in to comment.