Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
ISPN-5251 Near cache size must be provided
  • Loading branch information
galderz authored and wburns committed Mar 25, 2015
1 parent e229a0a commit 45cef19
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
@@ -1,11 +1,15 @@
package org.infinispan.client.hotrod.configuration;

import org.infinispan.client.hotrod.logging.Log;
import org.infinispan.client.hotrod.logging.LogFactory;
import org.infinispan.commons.configuration.Builder;

public class NearCacheConfigurationBuilder extends AbstractConfigurationChildBuilder
implements Builder<NearCacheConfiguration> {
private static final Log log = LogFactory.getLog(NearCacheConfigurationBuilder.class);

private NearCacheMode mode = NearCacheMode.DISABLED;
private int maxEntries = -1;
private Integer maxEntries = null; // undefined

protected NearCacheConfigurationBuilder(ConfigurationBuilder builder) {
super(builder);
Expand All @@ -23,12 +27,13 @@ public NearCacheConfigurationBuilder mode(NearCacheMode mode) {

@Override
public void validate() {
// No-op
if (mode.enabled() && maxEntries == null)
throw log.nearCacheMaxEntriesUndefined();
}

@Override
public NearCacheConfiguration create() {
return new NearCacheConfiguration(mode, maxEntries);
return new NearCacheConfiguration(mode, maxEntries == null ? -1 : maxEntries.intValue());
}

@Override
Expand Down
Expand Up @@ -183,4 +183,7 @@ public interface Log extends BasicLogger {
@LogMessage(level = ERROR)
@Message(value = "Unable to read %s bytes %s", id = 4044)
void unableToUnmarshallBytesError(String element, String bytes, @Cause Exception e);

@Message(value = "When enabling near caching, number of max entries must be configured", id = 4045)
CacheConfigurationException nearCacheMaxEntriesUndefined();
}
Expand Up @@ -43,7 +43,7 @@ private <K, V> AssertsNearCache<K, V> createAssertClient() {
for (HotRodServer server : servers)
clientBuilder.addServer().host("127.0.0.1").port(server.getPort());

clientBuilder.nearCache().mode(getNearCacheMode());
clientBuilder.nearCache().mode(getNearCacheMode()).maxEntries(-1);
return AssertsNearCache.create(this.<byte[], Object>cache(0), clientBuilder);
}

Expand Down
Expand Up @@ -44,7 +44,7 @@ protected <K, V> AssertsNearCache<K, V> createStickyAssertClient() {
for (HotRodServer server : servers)
clientBuilder.addServer().host("127.0.0.1").port(server.getPort());
clientBuilder.balancingStrategy(StickyServerLoadBalancingStrategy.class);
clientBuilder.nearCache().mode(getNearCacheMode());
clientBuilder.nearCache().mode(getNearCacheMode()).maxEntries(-1);
return AssertsNearCache.create(this.<byte[], Object>cache(0), clientBuilder);
}

Expand Down
Expand Up @@ -9,6 +9,7 @@
import org.infinispan.client.hotrod.configuration.NearCacheMode;
import org.infinispan.client.hotrod.test.RemoteCacheManagerCallable;
import org.infinispan.client.hotrod.test.SingleHotRodServerTest;
import org.infinispan.commons.CacheConfigurationException;
import org.testng.annotations.Test;

@Test(groups = "functional", testName = "client.hotrod.near.EagerNearCacheTest")
Expand All @@ -25,7 +26,7 @@ protected RemoteCacheManager getRemoteCacheManager() {
protected <K, V> AssertsNearCache<K, V> createClient() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.addServer().host("127.0.0.1").port(hotrodServer.getPort());
builder.nearCache().mode(getNearCacheMode());
builder.nearCache().mode(getNearCacheMode()).maxEntries(-1);
return AssertsNearCache.create(this.<byte[], Object>cache(), builder);
}

Expand All @@ -41,7 +42,7 @@ public void testGetNearCacheAfterConnect() {
cache.put(1, "one");
cache.put(2, "two");

builder.nearCache().mode(getNearCacheMode());
builder.nearCache().mode(getNearCacheMode()).maxEntries(-1);
assertClient = AssertsNearCache.create(this.<byte[], Object>cache(), builder);

assertEquals(2, assertClient.remote.size());
Expand Down Expand Up @@ -91,4 +92,13 @@ public void call() {
});
}

@Test(expectedExceptions = CacheConfigurationException.class,
expectedExceptionsMessageRegExp = ".*When enabling near caching, number of max entries must be configured.*")
public void testConfigurationWithoutMaxEntries() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.addServer().host("127.0.0.1").port(hotrodServer.getPort());
builder.nearCache().mode(getNearCacheMode());
new RemoteCacheManager(builder.build());
}

}
Expand Up @@ -571,7 +571,7 @@ operations `get` and `getVersioned` since data can potentially be located
locally within the Hot Rod client instead of having to go remote.

To enable near caching, the user must decide how to configure the near cache
to work, either `lazy` or `eager`.
to work, either `lazy` or `eager`, and define its size:

* Lazy near caching: In lazy mode, near cache is only populated upon retrievals
from the server via calls to `get` or `getVersioned` operations. When near
Expand All @@ -590,6 +590,16 @@ executed, the value is directly fetched from the near cache. The downside of
eager mode is that it's a more heavyweight mode and hence takes up more
resources in terms of bandwith.

* Size: When near cache is enabled, its size must be configured by defining
the maximum number of entries to keep in the near cache. When the maximum is
reached, near cached entries are evicted using a least-recently-used (LRU)
algorithm. If providing 0 or a negative value, it is assumed that the near
cache is unbounded.

WARNING: Users should be careful when configuring near cache to be
unbounded since it shifts the responsibility to keep the near cache's size
within the boundaries of the client JVM to the user.

The Hot Rod client's near cache mode is configured using the `NearCacheMode`
enumeration and calling:

Expand All @@ -600,27 +610,12 @@ import org.infinispan.client.hotrod.configuration.NearCacheMode;
...
// For lazy near caches
ConfigurationBuilder lazy = new ConfigurationBuilder();
lazy.nearCache().mode(NearCacheMode.LAZY);
ConfigurationBuilder lazyUnbounded = new ConfigurationBuilder();
lazy.nearCache().mode(NearCacheMode.LAZY).maxEntries(-1);
// For eager near caches
ConfigurationBuilder eager = new ConfigurationBuilder();
eager.nearCache().mode(NearCacheMode.EAGER);
----

By default near cache size is unbounded but this can be changed to set a
maximum size in terms of number of entries for the near cache. When the
maximum is reached, near cached entries are evicted using a least-recently-used
(LRU) algorithm. Near cache's maximum size can be configured like this:

[source,java]
----
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
...
// Sets 100 as the maximum number of entries to keep in the near cache
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.nearCache().maxEntries(100);
ConfigurationBuilder eagerBounded = new ConfigurationBuilder();
eager.nearCache().mode(NearCacheMode.EAGER).maxEntries(100);
----

NOTE: Near caches work the same way for local caches as they do for clustered
Expand Down

0 comments on commit 45cef19

Please sign in to comment.