Merge remote-tracking branch 'origin/master'

This commit is contained in:
Peter Lawrey
2014-10-17 09:15:37 +01:00
39 changed files with 2259 additions and 507 deletions
@@ -21,9 +21,7 @@ import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import static java.util.concurrent.TimeUnit.SECONDS;
import static net.openhft.chronicle.map.Replicators.tcp;
import java.util.concurrent.TimeUnit;
/**
* @author Rob Austin.
@@ -70,10 +68,10 @@ public class Builder {
final int serverPort,
final InetSocketAddress... endpoints) throws IOException {
TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(serverPort, endpoints)
.heartBeatInterval(1L, SECONDS);
.heartBeatInterval(1L, TimeUnit.SECONDS);
return ChronicleMapBuilder.of(kClass, vClass)
.entries(20000L)
.replicators(identifier, tcp( tcpConfig));
.replicators(identifier, tcpConfig);
}
@@ -17,7 +17,10 @@
package net.openhft.chronicle.map;
import org.junit.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.Closeable;
import java.io.IOException;
@@ -32,7 +35,7 @@ import static org.junit.Assert.assertTrue;
*
* @author Rob Austin.
*/
public class ClusterReplicationTest {
public class ChannelReplicationTest {
private ChronicleMap<Integer, CharSequence> map1a;
private ChronicleMap<Integer, CharSequence> map2a;
@@ -40,8 +43,8 @@ public class ClusterReplicationTest {
private ChronicleMap<Integer, CharSequence> map1b;
private ChronicleMap<Integer, CharSequence> map2b;
private ReplicatingChannel clusterB;
private ReplicatingChannel clusterA;
private ChannelProvider channelProviderA;
private ChannelProvider channelProviderB;
@Before
@@ -52,12 +55,13 @@ public class ClusterReplicationTest {
.of(8086, new InetSocketAddress("localhost", 8087))
.heartBeatInterval(1, SECONDS);
clusterA = new ReplicatingChannelBuilder((byte) 1, 1024).tcpReplication(tcpConfig)
channelProviderA = new ChannelProviderBuilder()
.replicators((byte) 1, tcpConfig)
.create();
map1a = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(1000)
.channel(clusterA.createChannel((short) 1))
.channel(channelProviderA.createChannel((short) 1))
.create(getPersistenceFile());
}
@@ -65,12 +69,12 @@ public class ClusterReplicationTest {
TcpReplicationConfig tcpConfig =
TcpReplicationConfig.of(8087).heartBeatInterval(1, SECONDS);
clusterB = new ReplicatingChannelBuilder((byte) 2, 1024).tcpReplication(tcpConfig)
channelProviderB = new ChannelProviderBuilder().replicators((byte) 2, tcpConfig)
.create();
map1b = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(1000)
.channel(clusterB.createChannel((short) 1))
.channel(channelProviderB.createChannel((short) 1))
.create(getPersistenceFile());
}
}
@@ -78,7 +82,7 @@ public class ClusterReplicationTest {
@After
public void tearDown() throws InterruptedException {
for (final Closeable closeable : new Closeable[]{clusterA, clusterB}) {
for (final Closeable closeable : new Closeable[]{channelProviderA, channelProviderB}) {
try {
closeable.close();
} catch (IOException e) {
@@ -93,13 +97,13 @@ public class ClusterReplicationTest {
map2b = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(1000)
.channel(clusterB.createChannel((short) 2))
.channel(channelProviderB.createChannel((short) 2))
.create(getPersistenceFile());
map2a = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(1000)
.channel(clusterA.createChannel((short) 2))
.channel(channelProviderA.createChannel((short) 2))
.create(getPersistenceFile());
map2a.put(1, "EXAMPLE-2");
@@ -0,0 +1,28 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.set.ChronicleSet;
import net.openhft.chronicle.set.ChronicleSetBuilder;
import org.junit.Test;
import java.io.IOException;
import java.util.Random;
import java.util.Set;
public class Issue3 {
@Test
public void test() throws IOException {
Set<Long> set = ChronicleSetBuilder.of(Long.class)
.actualSegments(1)
.actualEntriesPerSegment(1000)
.create();
Random r = new Random();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 300; j++) {
set.add(r.nextLong());
}
set.clear();
}
}
}
@@ -35,7 +35,7 @@ import static org.junit.Assert.assertTrue;
*
* @author Rob Austin.
*/
public class PostClusterMapCreationTest {
public class PostChannelCreationTest {
private ChronicleMap<Integer, CharSequence> map1a;
private ChronicleMap<Integer, CharSequence> map2a;
@@ -43,11 +43,8 @@ public class PostClusterMapCreationTest {
private ChronicleMap<Integer, CharSequence> map1b;
private ChronicleMap<Integer, CharSequence> map2b;
private ReplicatingChannel clusterB;
private ReplicatingChannel clusterA;
private ReplicatingChannelBuilder replicatingClusterBuilder1;
private ChannelProvider channelProviderA;
private ChannelProvider channelProviderB;
public static File getPersistenceFile() {
String TMP = System.getProperty("java.io.tmpdir");
@@ -62,43 +59,43 @@ public class PostClusterMapCreationTest {
public void setup() throws IOException {
{
final TcpReplicationConfig tcpReplicationConfig = TcpReplicationConfig
final TcpReplicationConfig tcpConfig = TcpReplicationConfig
.of(8086, new InetSocketAddress("localhost", 8087))
.heartBeatInterval(1, SECONDS);
clusterA = new ReplicatingChannelBuilder((byte) 1, 1024)
.tcpReplication(tcpReplicationConfig).create();
channelProviderA = new ChannelProviderBuilder()
.replicators((byte) 1, tcpConfig).create();
// this is how you add maps after the custer is created
map1a = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(1000)
.channel(clusterA.createChannel((short) 1))
.channel(channelProviderA.createChannel((short) 1))
.create(getPersistenceFile());
map2a = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(1000)
.channel(clusterA.createChannel((short) 2))
.channel(channelProviderA.createChannel((short) 2))
.create(getPersistenceFile());
}
{
final TcpReplicationConfig tcpReplicationConfig = TcpReplicationConfig
final TcpReplicationConfig tcpConfig = TcpReplicationConfig
.of(8087, new InetSocketAddress("localhost", 8086))
.heartBeatInterval(1, SECONDS);
clusterB = new ReplicatingChannelBuilder((byte) 2, 1024)
.tcpReplication(tcpReplicationConfig).create();
channelProviderB = new ChannelProviderBuilder()
.replicators((byte) 2, tcpConfig).create();
// this is how you add maps after the custer is created
map1b = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(1000)
.channel(clusterB.createChannel((short) 1))
.channel(channelProviderB.createChannel((short) 1))
.create(getPersistenceFile());
map2b = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(1000)
.channel(clusterB.createChannel((short) 2))
.channel(channelProviderB.createChannel((short) 2))
.create(getPersistenceFile());
}
@@ -108,7 +105,7 @@ public class PostClusterMapCreationTest {
@After
public void tearDown() throws InterruptedException {
for (final Closeable closeable : new Closeable[]{clusterA, clusterB}) {
for (final Closeable closeable : new Closeable[]{channelProviderA, channelProviderB}) {
try {
closeable.close();
} catch (IOException e) {
@@ -0,0 +1,240 @@
package net.openhft.chronicle.map;
import net.openhft.lang.io.ByteBufferBytes;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.serialization.BytesMarshallable;
import net.openhft.lang.model.constraints.NotNull;
import org.junit.Assert;
import org.junit.Test;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.Collections;
public class SerializerTest {
@Test
public void testValueMarshallable() throws Exception {
testReadWriteValue("Test");
testReadWriteValue(1);
testReadWriteValue(1L);
testReadWriteValue(1.0);
testReadWriteValue(1.0f);
testReadWriteValue(Collections.singleton("Test"));
testReadWriteValue(Collections.EMPTY_MAP);
testReadWriteValue(new MyTestClass(3));
testReadWriteValue(new MyTestClassMarshallable(3));
testReadWriteValue(new MyTestClassExternalizable(3));
testReadWriteValue(new MyTestClassObjectGraph(3));
}
@Test
public void testKeyMarshallable() throws Exception {
testReadWriteKey("Test");
testReadWriteKey(1);
testReadWriteKey(1L);
testReadWriteKey(1.0);
testReadWriteKey(1.0f);
testReadWriteKey(Collections.singleton("Test"));
testReadWriteKey(Collections.EMPTY_MAP);
testReadWriteKey(new MyTestClass(3));
testReadWriteKey(new MyTestClassMarshallable(3));
testReadWriteKey(new MyTestClassExternalizable(3));
testReadWriteKey(new MyTestClassObjectGraph(3));
}
public void testReadWriteValue(Object value) throws Exception {
Class valueClass = value.getClass();
final ByteBufferBytes out = new ByteBufferBytes(ByteBuffer.allocateDirect(1024));
ByteBufferBytes in = out.slice();
ChronicleMapBuilder builder = ChronicleMapBuilder.of(Integer.class, valueClass);
builder.preMapConstruction();
Serializer v = new Serializer(builder.valueBuilder);
v.writeMarshallable(value, out);
long position = out.position();
in.limit(position);
Object actual = v.readMarshallable(in);
Assert.assertEquals(actual, value);
}
public void testReadWriteKey(Object key) throws Exception {
Class clazz = key.getClass();
final ByteBufferBytes out = new ByteBufferBytes(ByteBuffer.allocateDirect(1024));
ByteBufferBytes in = out.slice();
ChronicleMapBuilder builder = ChronicleMapBuilder.of(clazz, Integer.class);
builder.preMapConstruction();
{
Serializer v = new Serializer(builder.keyBuilder);
v.writeMarshallable(key, out);
long position = out.position();
in.limit(position);
Object actual = v.readMarshallable(in);
Assert.assertEquals(actual, key);
}
}
public static class MyTestClassExternalizable implements Externalizable {
int a;
public MyTestClassExternalizable() {
}
MyTestClassExternalizable(int a) {
this.a = a;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyTestClassExternalizable that = (MyTestClassExternalizable) o;
if (a != that.a) return false;
return true;
}
@Override
public int hashCode() {
return a;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.write(a);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
a = in.readInt();
}
}
public static class MyTestClass implements Serializable {
int a;
MyTestClass(int a) {
this.a = a;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyTestClass myTestClass = (MyTestClass) o;
if (a != myTestClass.a) return false;
return true;
}
@Override
public int hashCode() {
return a;
}
}
public static class MyTestClassMarshallable implements BytesMarshallable {
int a;
public MyTestClassMarshallable() {
}
public MyTestClassMarshallable(int a) {
this.a = a;
}
public void setA(int a) {
this.a = a;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyTestClassMarshallable that = (MyTestClassMarshallable) o;
if (a != that.a) return false;
return true;
}
@Override
public int hashCode() {
return a;
}
public void readMarshallable(@NotNull Bytes in) throws IllegalStateException {
a = in.readInt();
}
/**
* write an object to bytes
*
* @param out to write to
*/
public void writeMarshallable(@NotNull Bytes out) {
out.writeInt(a);
}
}
public static class MyTestClassObjectGraph implements Serializable {
MyTestClass delegate;
public MyTestClassObjectGraph() {
}
MyTestClassObjectGraph(int a) {
delegate = new MyTestClass(a);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyTestClassObjectGraph that = (MyTestClassObjectGraph) o;
if (delegate != null ? !delegate.equals(that.delegate) : that.delegate != null)
return false;
return true;
}
@Override
public int hashCode() {
return delegate != null ? delegate.hashCode() : 0;
}
}
}
@@ -41,9 +41,8 @@ public class SingleMapClusterTest {
private ChronicleMap<Integer, CharSequence> map1b;
private ReplicatingChannel clusterB;
private ReplicatingChannel clusterA;
private ChannelProvider channelProviderA;
private ChannelProvider channelProviderB;
public static File getPersistenceFile() {
String TMP = System.getProperty("java.io.tmpdir");
@@ -60,13 +59,15 @@ public class SingleMapClusterTest {
final TcpReplicationConfig tcpReplicationConfig = TcpReplicationConfig
.of(8086, new InetSocketAddress("localhost", 8087))
.heartBeatInterval(1, SECONDS);
clusterA = new ReplicatingChannelBuilder((byte) 1, 1024)
.tcpReplication(tcpReplicationConfig).create();
int maxEntrySize = 1024;
byte identifier= 1;
channelProviderA = new ChannelProviderBuilder()
.maxEntrySize(maxEntrySize)
.replicators(identifier, tcpReplicationConfig).create();
// this is how you add maps after the custer is created
map1a = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(1000)
.channel(clusterA.createChannel((short) 1))
.channel(channelProviderA.createChannel((short) 1))
.create(getPersistenceFile());
}
@@ -76,11 +77,12 @@ public class SingleMapClusterTest {
.of(8087, new InetSocketAddress("localhost", 8086))
.heartBeatInterval(1, SECONDS);
clusterB = new ReplicatingChannelBuilder((byte) 2, 1024)
.tcpReplication(tcpReplicationConfig).create();
channelProviderB = new ChannelProviderBuilder()
.replicators((byte) 2, tcpReplicationConfig).create();
// this is how you add maps after the custer is created
map1b = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.channel(clusterB.createChannel((short) 1))
.channel(channelProviderB.createChannel((short) 1))
.entries(1000)
.create(getPersistenceFile());
}
@@ -89,7 +91,7 @@ public class SingleMapClusterTest {
@After
public void tearDown() throws InterruptedException {
for (final Closeable closeable : new Closeable[]{clusterA, clusterB}) {
for (final Closeable closeable : new Closeable[]{channelProviderA, channelProviderB}) {
try {
closeable.close();
} catch (IOException e) {
@@ -0,0 +1,49 @@
package net.openhft.chronicle.map;
import net.openhft.lang.io.ByteBufferBytes;
import org.junit.Test;
import org.testng.Assert;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import static java.util.concurrent.TimeUnit.SECONDS;
import static net.openhft.chronicle.map.Builder.getPersistenceFile;
import static net.openhft.chronicle.map.TcpReplicationConfig.of;
/**
* @author Rob Austin.
*/
public class StatelessClientTest {
@Test
public void test() throws IOException, InterruptedException {
ChronicleMapBuilder<Integer, CharSequence> builder =
ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(20000L)
.replicators((byte) 2, of(8076).heartBeatInterval(1L, SECONDS));
{
final ChronicleMap<Integer, CharSequence> map2a =
builder.create(getPersistenceFile());
map2a.put(10, "EXAMPLE-10"); // this will be the last time that map1 go an update from map2
}
KeyValueSerializer keyValueSerializer = new KeyValueSerializer(builder.keyBuilder, builder.valueBuilder);
final ByteBufferBytes out = new ByteBufferBytes(ByteBuffer.allocateDirect(1024));
// ByteBufferBytes in = out.slice();
InetSocketAddress remote = new InetSocketAddress("localhost", 8076);
StatelessMapClient<Integer, String> map = new StatelessMapClient<Integer, String>(keyValueSerializer, remote);
Assert.assertEquals("EXAMPLE-10", map.get(10));
Assert.assertEquals("EXAMPLE-10", map.get(10));
Assert.assertEquals(1, map.size());
}
}
@@ -49,7 +49,7 @@ public class TCPSocketReplicationTest3wayPutReturnsNull {
.entries(1000)
.putReturnsNull(true)
.replicators(identifier, Replicators.tcp(tcpConfig))
.replicators(identifier, tcpConfig)
.create(Builder.getPersistenceFile());
}
@@ -27,7 +27,7 @@ import java.net.InetSocketAddress;
import static java.util.concurrent.TimeUnit.SECONDS;
import static net.openhft.chronicle.map.Builder.getPersistenceFile;
import static net.openhft.chronicle.map.Replicators.tcp;
import static net.openhft.chronicle.map.TcpReplicationConfig.of;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -43,21 +43,20 @@ public class TwoMapOnDifferentServers {
@Before
public void setup() throws IOException {
final TcpReplicationConfig tcpReplicationConfig = TcpReplicationConfig
.of(8076, new InetSocketAddress("localhost", 8077))
.heartBeatInterval(1, SECONDS);
final TcpReplicationConfig tcpConfig =
of(8076, new InetSocketAddress("localhost", 8077))
.heartBeatInterval(1, SECONDS);
File persistenceFile = getPersistenceFile();
map1 = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(20000)
.replicators((byte) 1, tcp(tcpReplicationConfig))
.replicators((byte) 1, tcpConfig)
.create(persistenceFile);
map2 = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(20000)
.replicators((byte) 2, tcp(
TcpReplicationConfig.of(8077).heartBeatInterval(1, SECONDS)))
.replicators((byte) 2, of(8077).heartBeatInterval(1, SECONDS))
.create(persistenceFile);
}
@@ -23,8 +23,6 @@ import java.io.Closeable;
import java.io.IOException;
import java.net.Inet4Address;
import static net.openhft.chronicle.map.Replicators.udp;
/**
* Test ReplicatedChronicleMap where the Replicated is over a TCP Socket
*
@@ -44,7 +42,7 @@ public class UDPSocketReplicationTest {
return ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(1000)
.replicators((byte) identifier, udp(udpConfig))
.replicators((byte) identifier, udpConfig)
.create(Builder.getPersistenceFile());
}
@@ -16,7 +16,6 @@
package net.openhft.chronicle.set;
import net.openhft.chronicle.map.ChronicleSet;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -27,7 +26,6 @@ import java.net.InetSocketAddress;
import static java.util.concurrent.TimeUnit.SECONDS;
import static net.openhft.chronicle.map.Builder.getPersistenceFile;
import static net.openhft.chronicle.map.Replicators.tcp;
import static net.openhft.chronicle.map.TcpReplicationConfig.of;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -51,8 +49,8 @@ public class SetForTCPSocketReplicationTest3way {
final int serverPort,
final InetSocketAddress... endpoints) throws IOException {
return (T) ChronicleSetBuilder.of(Integer.class)
.replicators(identifier, tcp(of(serverPort, endpoints).
heartBeatInterval(1L, SECONDS))).create(getPersistenceFile());
.replicators(identifier, of(serverPort, endpoints).
heartBeatInterval(1L, SECONDS)).create(getPersistenceFile());
}