Skip to content

Commit

Permalink
OF-2782: Unsure SerializingCache retains functionality after cluster …
Browse files Browse the repository at this point in the history
…state change

When a server joins or leaves a cluster, all clustered caches are recreated. SerializingCaches should be recreated as such.
  • Loading branch information
guusdk committed Jan 22, 2024
1 parent e57845e commit 73e0a95
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,11 @@ public static synchronized void joinedCluster() {
.filter(CacheFactory::isClusterableCache)
.forEach(cache -> {
final CacheWrapper cacheWrapper = ((CacheWrapper) cache);
final Cache clusteredCache = cacheFactoryStrategy.createCache(cacheWrapper.getName());
Cache clusteredCache = cacheFactoryStrategy.createCache(cacheWrapper.getName());
if (cacheWrapper.getWrappedCache() instanceof SerializingCache) {
final SerializingCache serializingCache = (SerializingCache) cacheWrapper.getWrappedCache();
clusteredCache = new SerializingCache(clusteredCache, serializingCache.getKeyClass(), serializingCache.getValueClass());
}
cacheWrapper.setWrappedCache(clusteredCache);
});
clusteringStarting = false;
Expand All @@ -964,7 +968,11 @@ public static synchronized void leftCluster() {
.filter(CacheFactory::isClusterableCache)
.forEach(cache -> {
final CacheWrapper cacheWrapper = ((CacheWrapper) cache);
final Cache standaloneCache = cacheFactoryStrategy.createCache(cacheWrapper.getName());
Cache standaloneCache = cacheFactoryStrategy.createCache(cacheWrapper.getName());
if (cacheWrapper.getWrappedCache() instanceof SerializingCache) {
final SerializingCache serializingCache = (SerializingCache) cacheWrapper.getWrappedCache();
standaloneCache = new SerializingCache(standaloneCache, serializingCache.getKeyClass(), serializingCache.getValueClass());
}
cacheWrapper.setWrappedCache(standaloneCache);
});
log.info("Clustering stopped; cache migration complete");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021-2022 Ignite Realtime Foundation. All rights reserved.
* Copyright (C) 2021-2024 Ignite Realtime Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,6 +59,8 @@ public class SerializingCache<K extends Serializable, V extends Serializable> im
private final Cache<String, String> delegate;
private final Marshaller marshaller;
private final Unmarshaller unmarshaller;
private final Class<K> keyClass;
private final Class<V> valueClass;

/**
* Creates a new serializing cache backed by the provided delegate.
Expand All @@ -78,6 +80,8 @@ public class SerializingCache<K extends Serializable, V extends Serializable> im
final JAXBContext jaxbContext = JAXBContext.newInstance(keyClass, valueClass);
marshaller = jaxbContext.createMarshaller();
unmarshaller = jaxbContext.createUnmarshaller();
this.keyClass = keyClass;
this.valueClass = valueClass;
} catch (JAXBException e) {
throw new IllegalArgumentException("Unable to create a cache using classes " + keyClass + " and " + valueClass, e);
}
Expand Down Expand Up @@ -113,6 +117,18 @@ private Object unmarshall(@Nullable final String object)
}
}

@Nonnull
public Class<K> getKeyClass()
{
return keyClass;
}

@Nonnull
public Class<V> getValueClass()
{
return valueClass;
}

@Override
@Nullable
public synchronized V put(@Nullable final K key, @Nullable final V value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,24 @@ public void testSerializingCacheRecreation() throws Exception
assertSame(resultA, resultB);
assertInstanceOf(SerializingCache.class, ((CacheWrapper) resultB).getWrappedCache());
}

/**
* Verifies that after the JVM left the cluster (at which point the cache implementations are swapped) a Serializing
* Cache remains a Serializing Cache.
*
* @see <a href="https://igniterealtime.atlassian.net/browse/OF-2782">OF-2782</a>
*/
@Test
public void testSerializingCacheRetainsTypeAfterLeftCluster() throws Exception
{
// Setup test fixture.
final String name = "unittest-serializingcache-leftcluster";
final CacheWrapper cache = CacheFactory.createSerializingCache(name, String.class, String.class);

// Execute system under test.
CacheFactory.leftCluster();

// Verify results.
assertInstanceOf(SerializingCache.class, cache.getWrappedCache());
}
}

0 comments on commit 73e0a95

Please sign in to comment.