[infinispan-commits] Infinispan SVN: r1999 - in trunk/client/hotrod-client/src: main/java/org/infinispan/client/hotrod/impl and 2 other directories.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Wed Jul 7 08:33:09 EDT 2010
Author: mircea.markus
Date: 2010-07-07 08:33:08 -0400 (Wed, 07 Jul 2010)
New Revision: 1999
Modified:
trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/HotRodMarshaller.java
trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java
trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/RemoteCacheImpl.java
trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/SerializationMarshaller.java
trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/HotRodIntegrationTest.java
trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/retry/DistributionRetryTest.java
Log:
[ISPN-513] = hotrod client:optimize serialization of key/values
Modified: trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/HotRodMarshaller.java
===================================================================
--- trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/HotRodMarshaller.java 2010-07-06 16:52:56 UTC (rev 1998)
+++ trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/HotRodMarshaller.java 2010-07-07 12:33:08 UTC (rev 1999)
@@ -2,6 +2,8 @@
import net.jcip.annotations.ThreadSafe;
+import java.util.Properties;
+
/**
* Used for un/marshalling objects sent between hotrod client and server (hotrod is a binary protocol).
* A single instance of this class is shared by all threads, so this class needs to be thread safe.
@@ -12,7 +14,13 @@
@ThreadSafe
public interface HotRodMarshaller {
- byte[] marshallObject(Object toMarshall);
+ void init(Properties config);
+ /**
+ * @param isKeyHint if true the object passed to the marshaller is a key. This info can be used to optimize the
+ * size of the allocated byte[].
+ */
+ byte[] marshallObject(Object toMarshall, boolean isKeyHint);
+
Object readObject(byte[] bytes);
}
Modified: trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java
===================================================================
--- trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java 2010-07-06 16:52:56 UTC (rev 1998)
+++ trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java 2010-07-07 12:33:08 UTC (rev 1999)
@@ -154,7 +154,7 @@
*/
public RemoteCacheManager(HotRodMarshaller hotRodMarshaller, Properties props, boolean start) {
this(props);
- this.hotRodMarshaller = hotRodMarshaller;
+ setHotRodMarshaller(hotRodMarshaller);
if (log.isTraceEnabled()) {
log.trace("Using explicitly set marshaller: " + hotRodMarshaller);
}
@@ -305,7 +305,7 @@
hotrodMarshallerClass = SerializationMarshaller.class.getName();
log.info("'marshaller' not specified, using " + hotrodMarshallerClass);
}
- hotRodMarshaller = (HotRodMarshaller) VHelper.newInstance(hotrodMarshallerClass);
+ setHotRodMarshaller((HotRodMarshaller)VHelper.newInstance(hotrodMarshallerClass));
}
String asyncExecutorClass = DefaultAsyncExecutorFactory.class.getName();
@@ -396,4 +396,8 @@
return new String[]{t.nextToken(), t.nextToken()};
}
+ private void setHotRodMarshaller(HotRodMarshaller hotRodMarshaller) {
+ this.hotRodMarshaller = hotRodMarshaller;
+ hotRodMarshaller.init(props);
+ }
}
Modified: trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/RemoteCacheImpl.java
===================================================================
--- trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/RemoteCacheImpl.java 2010-07-06 16:52:56 UTC (rev 1998)
+++ trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/RemoteCacheImpl.java 2010-07-07 12:33:08 UTC (rev 1999)
@@ -60,7 +60,7 @@
@Override
public boolean removeWithVersion(K key, long version) {
assertRemoteCacheManagerIsStarted();
- VersionedOperationResponse response = operations.removeIfUnmodified(obj2bytes(key), version, flags());
+ VersionedOperationResponse response = operations.removeIfUnmodified(obj2bytes(key, true), version, flags());
return response.getCode().isUpdated();
}
@@ -83,7 +83,7 @@
@Override
public boolean replaceWithVersion(K key, V newValue, long version, int lifespanSeconds, int maxIdleTimeSeconds) {
assertRemoteCacheManagerIsStarted();
- VersionedOperationResponse response = operations.replaceIfUnmodified(obj2bytes(key), obj2bytes(newValue), lifespanSeconds, maxIdleTimeSeconds, version, flags());
+ VersionedOperationResponse response = operations.replaceIfUnmodified(obj2bytes(key, true), obj2bytes(newValue, false), lifespanSeconds, maxIdleTimeSeconds, version, flags());
return response.getCode().isUpdated();
}
@@ -106,7 +106,7 @@
@Override
public VersionedValue<V> getVersioned(K key) {
assertRemoteCacheManagerIsStarted();
- BinaryVersionedValue value = operations.getWithVersion(obj2bytes(key), flags());
+ BinaryVersionedValue value = operations.getWithVersion(obj2bytes(key, true), flags());
return binary2VersionedValue(value);
}
@@ -164,7 +164,7 @@
if (log.isTraceEnabled()) {
log.trace("About to add (K,V): (" + key + ", " + value + ") lifespanSecs:" + lifespanSecs + ", maxIdleSecs:" + maxIdleSecs);
}
- byte[] result = operations.put(obj2bytes(key), obj2bytes(value), lifespanSecs, maxIdleSecs, flags());
+ byte[] result = operations.put(obj2bytes(key, true), obj2bytes(value, false), lifespanSecs, maxIdleSecs, flags());
return (V) bytes2obj(result);
}
@@ -174,7 +174,7 @@
assertRemoteCacheManagerIsStarted();
int lifespanSecs = toSeconds(lifespan, lifespanUnit);
int maxIdleSecs = toSeconds(maxIdleTime, maxIdleTimeUnit);
- byte[] bytes = operations.putIfAbsent(obj2bytes(key), obj2bytes(value), lifespanSecs, maxIdleSecs, flags());
+ byte[] bytes = operations.putIfAbsent(obj2bytes(key, true), obj2bytes(value, false), lifespanSecs, maxIdleSecs, flags());
return (V) bytes2obj(bytes);
}
@@ -183,7 +183,7 @@
assertRemoteCacheManagerIsStarted();
int lifespanSecs = toSeconds(lifespan, lifespanUnit);
int maxIdleSecs = toSeconds(maxIdleTime, maxIdleTimeUnit);
- byte[] bytes = operations.replace(obj2bytes(key), obj2bytes(value), lifespanSecs, maxIdleSecs, flags());
+ byte[] bytes = operations.replace(obj2bytes(key, true), obj2bytes(value, false), lifespanSecs, maxIdleSecs, flags());
return (V) bytes2obj(bytes);
}
@@ -270,20 +270,20 @@
@Override
public boolean containsKey(Object key) {
assertRemoteCacheManagerIsStarted();
- return operations.containsKey(obj2bytes(key), flags());
+ return operations.containsKey(obj2bytes(key, true), flags());
}
@Override
public V get(Object key) {
assertRemoteCacheManagerIsStarted();
- byte[] bytes = operations.get(obj2bytes(key), flags());
+ byte[] bytes = operations.get(obj2bytes(key, true), flags());
return (V) bytes2obj(bytes);
}
@Override
public V remove(Object key) {
assertRemoteCacheManagerIsStarted();
- byte[] existingValue = operations.remove(obj2bytes(key), flags());
+ byte[] existingValue = operations.remove(obj2bytes(key, true), flags());
return (V) bytes2obj(existingValue);
}
@@ -323,8 +323,8 @@
return flags;
}
- private byte[] obj2bytes(Object obj) {
- return this.marshaller.marshallObject(obj);
+ private byte[] obj2bytes(Object obj, boolean isKey) {
+ return this.marshaller.marshallObject(obj, isKey);
}
private Object bytes2obj(byte[] bytes) {
Modified: trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/SerializationMarshaller.java
===================================================================
--- trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/SerializationMarshaller.java 2010-07-06 16:52:56 UTC (rev 1998)
+++ trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/SerializationMarshaller.java 2010-07-07 12:33:08 UTC (rev 1999)
@@ -2,19 +2,27 @@
import org.infinispan.client.hotrod.HotRodMarshaller;
import org.infinispan.client.hotrod.exceptions.HotRodClientException;
+import org.infinispan.io.ExposedByteArrayOutputStream;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
+import java.util.Properties;
/**
* Default marshaller implementation based on object serialization.
- * todo - the marshaller should only be when writing to the actual transport
+ * Supports two configuration elements:
+ * <ul>
+ * <li>marshaller.default-array-size.key - the size of the {@link ExposedByteArrayOutputStream} that will be
+ * created for marshalling keys</li>
+ * <li> marshaller.default-array-size.value - the size of the {@link ExposedByteArrayOutputStream} that will be
+ * created for marshalling values
+ * </li>
+ * </ul>
*
* @author Mircea.Markus at jboss.com
* @since 4.1
@@ -23,9 +31,22 @@
private static Log log = LogFactory.getLog(SerializationMarshaller.class);
+ private volatile int defaultArraySizeForKey = 128;
+ private volatile int defaultArraySizeForValue = 256;
+
@Override
- public byte[] marshallObject(Object toMarshall) {
- ByteArrayOutputStream result = new ByteArrayOutputStream(1000);
+ public void init(Properties config) {
+ if (config.contains("marshaller.default-array-size.key")) {
+ defaultArraySizeForKey = Integer.parseInt(config.getProperty("marshaller.default-array-size.key"));
+ }
+ if (config.contains("marshaller.default-array-size.value")) {
+ defaultArraySizeForValue = Integer.parseInt(config.getProperty("marshaller.default-array-size.value"));
+ }
+ }
+
+ @Override
+ public byte[] marshallObject(Object toMarshall, boolean isKeyHint) {
+ ExposedByteArrayOutputStream result = getByteArray(isKeyHint);
try {
ObjectOutputStream oos = new ObjectOutputStream(result);
oos.writeObject(toMarshall);
@@ -35,6 +56,14 @@
}
}
+ private ExposedByteArrayOutputStream getByteArray(boolean keyHint) {
+ if (keyHint) {
+ return new ExposedByteArrayOutputStream(defaultArraySizeForKey);
+ } else {
+ return new ExposedByteArrayOutputStream(defaultArraySizeForValue);
+ }
+ }
+
@Override
public Object readObject(byte[] bytes) {
try {
Modified: trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/HotRodIntegrationTest.java
===================================================================
--- trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/HotRodIntegrationTest.java 2010-07-06 16:52:56 UTC (rev 1998)
+++ trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/HotRodIntegrationTest.java 2010-07-07 12:33:08 UTC (rev 1999)
@@ -187,8 +187,8 @@
private void assertCacheContains(Cache cache, String key, String value) {
SerializationMarshaller marshaller = new SerializationMarshaller();
- byte[] keyBytes = marshaller.marshallObject(key);
- byte[] valueBytes = marshaller.marshallObject(value);
+ byte[] keyBytes = marshaller.marshallObject(key, true);
+ byte[] valueBytes = marshaller.marshallObject(value, false);
ByteArrayKey cacheKey = new ByteArrayKey(keyBytes);
CacheValue cacheValue = (CacheValue) cache.get(cacheKey);
if (value == null) {
Modified: trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/retry/DistributionRetryTest.java
===================================================================
--- trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/retry/DistributionRetryTest.java 2010-07-06 16:52:56 UTC (rev 1998)
+++ trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/retry/DistributionRetryTest.java 2010-07-07 12:33:08 UTC (rev 1999)
@@ -115,7 +115,7 @@
TcpTransportFactory tcpTp = (TcpTransportFactory) TestingUtil.extractField(remoteCacheManager, "transportFactory");
SerializationMarshaller sm = new SerializationMarshaller();
- TcpTransport transport = (TcpTransport) tcpTp.getTransport(sm.marshallObject(key));
+ TcpTransport transport = (TcpTransport) tcpTp.getTransport(sm.marshallObject(key, true));
try {
assertEquals(transport.getServerAddress(), new InetSocketAddress("localhost", hotRodServer2.getPort()));
} finally {
@@ -136,7 +136,7 @@
public Object getKey() {
String result = String.valueOf(r.nextLong());
SerializationMarshaller sm = new SerializationMarshaller();
- return sm.marshallObject(result);
+ return sm.marshallObject(result, true);
}
static String getStringObject(byte[] bytes) {
More information about the infinispan-commits
mailing list