Merge remote-tracking branch 'origin/master'

This commit is contained in:
Peter Lawrey
2015-04-28 14:25:50 +01:00
8 changed files with 381 additions and 68 deletions
@@ -1,11 +1,11 @@
package net.openhft.chronicle.map;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.function.Consumer;
import static org.junit.Assert.*;
@@ -13,17 +13,18 @@ import static org.junit.Assert.*;
* JUnit test class to support {@link FilePerKeyMapTest}
*/
public class FilePerKeyMapTest {
private static FilePerKeyMap map;
@BeforeClass
public static void createMap(){
map = new FilePerKeyMap("/tmp/filepermaptests");
//just in case it hasn't been cleared up last time
map.clear();
}
/**
* Testing all the methods of the map with simple tests.
*/
@Test
public void testMapMethods() {
//There are no entries in the map so null should be returned
FilePerKeyMap map = new FilePerKeyMap("/tmp/filepermaptests");
//just in case it hasn't been cleared up last time
map.clear();
String val = map.put("one", "test1");
assertEquals(val, null);
@@ -103,25 +104,11 @@ public class FilePerKeyMapTest {
}
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
map.clear();
map.close();
}
@Test
public void perfTest(){
//There are no entries in the map so null should be returned
FilePerKeyMap map = new FilePerKeyMap("/tmp/filepermaptests");
//just in case it hasn't been cleared up last time
map.clear();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 2_000_000; i++) {
sb.append('x');
@@ -139,32 +126,47 @@ public class FilePerKeyMapTest {
for (int i = 0; i < iterations; i++) {
map.put("big file", value);
}
System.out.println("Time to update "+ iterations + " iterations " + (System.currentTimeMillis()-time));
System.out.println("Time to update " + iterations + " iterations " + (System.currentTimeMillis() - time));
map.clear();
map.close();
}
@Ignore //This doesn't work on all OS
@Test
public void eventTest() {
//There are no entries in the map so null should be returned
FilePerKeyMap map = new FilePerKeyMap("/tmp/filepermaptests");
final List<FPMEvent> events = new ArrayList<>();
//just in case it hasn't been cleared up last time
map.clear();
map.registerForEvents(e -> System.out.println(e));
Consumer<FPMEvent> fpmEventConsumer = (FPMEvent e) -> {
System.out.println(e);
events.add(e);
};
map.registerForEvents(fpmEventConsumer);
map.put("one", "one");
map.put("one", "two");
map.remove("one");
map.put("one", "one");
try {
//Allow all events to be played through
Thread.sleep(3000);
//Check the events
assertEquals(4, events.size());
assertEquals(FPMEvent.EventType.NEW,events.get(0).getEventType());
assertEquals(FPMEvent.EventType.UPDATE, events.get(1).getEventType());
assertEquals(FPMEvent.EventType.DELETE, events.get(2).getEventType());
assertEquals(FPMEvent.EventType.NEW, events.get(3).getEventType());
assertEquals(true, events.get(0).isProgrammatic());
assertEquals(true, events.get(1).isProgrammatic());
assertEquals(true, events.get(2).isProgrammatic());
assertEquals(true, events.get(3).isProgrammatic());
} catch (InterruptedException e) {
e.printStackTrace();
}
map.unregisterForEvents(fpmEventConsumer);
map.clear();
map.close();
}
}
@@ -3,13 +3,18 @@ package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.replication.ReplicationChannel;
import net.openhft.chronicle.hash.replication.ReplicationHub;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.lang.model.constraints.MaxSize;
import net.openhft.lang.values.DoubleArray;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import static net.openhft.chronicle.map.ChronicleMapBuilder.of;
import static org.junit.Assert.assertEquals;
/**
@@ -121,4 +126,224 @@ public class TestEntriesSpanningSeveralChunksReplicationHub {
favoriteColourServer1.close();
}
public static final int NUMBER_OF_CHANNELS = 1000;
@Test(timeout = 100000)
public void test1000Channels()
throws IOException, InterruptedException {
final HashMap<Short, ChronicleMap> map1 = new HashMap<>();
final HashMap<Short, ChronicleMap> map2 = new HashMap<>();
// server 1 with identifier = 1
{
ChronicleMapBuilder<CharSequence, Short> builder =
of(CharSequence.class, Short.class)
.averageKeySize(10)
.averageValueSize(100)
.entries(1000);
byte identifier = (byte) 1;
TcpTransportAndNetworkConfig tcpConfig = TcpTransportAndNetworkConfig
.of(8053);
ReplicationHub hubOnServer1 = ReplicationHub.builder()
.tcpTransportAndNetwork(tcpConfig)
.maxNumberOfChannels(NUMBER_OF_CHANNELS)
.createWithId(identifier);
for (short channelId = 1; channelId < NUMBER_OF_CHANNELS; channelId++) {
ReplicationChannel channel = hubOnServer1.createChannel(channelId);
ChronicleMap map = builder.instance().replicatedViaChannel(channel).create();
map.put("key", channelId);
map1.put(channelId, map);
}
}
{
ChronicleMapBuilder<CharSequence, Short> builder =
of(CharSequence.class, Short.class)
.averageKeySize(10)
.averageValueSize(100)
.entries(1000);
byte identifier = (byte) 2;
TcpTransportAndNetworkConfig tcpConfig = TcpTransportAndNetworkConfig
.of(8054, new InetSocketAddress("localhost", 8053));
ReplicationHub hubOnServer1 = ReplicationHub.builder()
.tcpTransportAndNetwork(tcpConfig)
.maxNumberOfChannels(NUMBER_OF_CHANNELS)
.createWithId(identifier);
for (short channelId = 1; channelId < NUMBER_OF_CHANNELS; channelId++) {
ReplicationChannel channel = hubOnServer1.createChannel(channelId);
ChronicleMap map = builder.instance().replicatedViaChannel(channel).create();
map.put("key", channelId);
map2.put(channelId, map);
}
}
// check that the maps are the same, if the are not the same the test will timeout
// we have to give a few seconds for the replication to propagate
for (short channelId = 1; channelId < NUMBER_OF_CHANNELS; channelId++) {
// allow time for the recompilation to resolve
final ChronicleMap v1 = map1.get(channelId);
final ChronicleMap v2 = map2.get(channelId);
for (; ; ) {
if (v1 == null || v2 == null) {
Thread.sleep(10);
continue;
}
if (v1.isEmpty() || v2.isEmpty()) {
Thread.sleep(10);
continue;
}
if (!v1.equals(v2)) {
Thread.sleep(10);
continue;
}
Assert.assertEquals(channelId, v1.get("key"));
Assert.assertEquals(channelId, v2.get("key"));
break;
}
}
// close
for (short channelId = 1; channelId < NUMBER_OF_CHANNELS; channelId++) {
final ChronicleMap v1 = map1.get(channelId);
if (v1 != null)
v1.close();
final ChronicleMap v2 = map2.get(channelId);
if (v2 != null)
v2.close();
}
}
@Test
public void testChannelWithDifferentTypes() throws IOException, InterruptedException {
// server 1 - in this example only server 1 write any data
ChronicleMap<CharSequence, CharSequence> charMap1;
ChronicleMap<Integer, Integer> intMap1;
{
ChronicleMapBuilder<Integer, Integer> intMapBuilder =
of(Integer.class, Integer.class)
.entries(1000);
ChronicleMapBuilder<CharSequence, CharSequence> charMapBuilder =
of(CharSequence.class, CharSequence.class)
.averageKeySize(10)
.averageValueSize(100)
.entries(1000);
byte identifier = (byte) 1;
TcpTransportAndNetworkConfig tcpConfig = TcpTransportAndNetworkConfig.of(8025);
ReplicationHub hubOnServer1 = ReplicationHub.builder()
.tcpTransportAndNetwork(tcpConfig)
.maxNumberOfChannels(NUMBER_OF_CHANNELS)
.createWithId(identifier);
ReplicationChannel channel1 = hubOnServer1.createChannel(1);
intMap1 = intMapBuilder.instance()
.replicatedViaChannel(channel1)
.create();
ReplicationChannel channel2 = hubOnServer1.createChannel(2);
charMap1 = charMapBuilder.instance()
.replicatedViaChannel
(channel2)
.create();
intMap1.put(1, 1);
charMap1.put("Hello", "World");
}
// server 2
ChronicleMap<CharSequence, CharSequence> charMap2;
ChronicleMap<Integer, Integer> intMap2;
{
ChronicleMapBuilder<Integer, Integer> intMapBuilder =
of(Integer.class, Integer.class)
.entries(1000);
ChronicleMapBuilder<CharSequence, CharSequence> charMapBuilder =
of(CharSequence.class, CharSequence.class)
.averageKeySize(10)
.averageValueSize(100)
.entries(1000);
byte identifier = (byte) 2;
TcpTransportAndNetworkConfig tcpConfig = TcpTransportAndNetworkConfig.of(8026, new
InetSocketAddress("localhost", 8025)); // change the localhost to your server
ReplicationHub hubOnServer1 = ReplicationHub.builder()
.tcpTransportAndNetwork(tcpConfig)
.maxNumberOfChannels(NUMBER_OF_CHANNELS)
.createWithId(identifier);
ReplicationChannel channel1 = hubOnServer1.createChannel(1);
intMap2 = intMapBuilder.instance()
.replicatedViaChannel(channel1)
.create();
ReplicationChannel channel2 = hubOnServer1.createChannel(2);
charMap2 = charMapBuilder.instance().replicatedViaChannel
(channel2)
.create();
}
// allow time for the recompilation to resolve
for (int t = 0; t < 3500; t++) {
if (intMap1.equals(intMap2) &&
charMap1.equals(charMap2))
break;
Thread.sleep(1);
}
Assert.assertEquals(intMap1,intMap2);
Assert.assertEquals(charMap1,charMap2);
}
}
@@ -0,0 +1,57 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import org.junit.Assert;
import org.junit.Test;
import java.net.InetSocketAddress;
/**
* Created by Rob Austin
*/
public class TestReplication {
@Test
public void testAllDataGetsReplicated() throws InterruptedException {
TcpTransportAndNetworkConfig tcpConfigServer1 =
TcpTransportAndNetworkConfig.of(8082);
TcpTransportAndNetworkConfig tcpConfigServer2 =
TcpTransportAndNetworkConfig.of(8083, new InetSocketAddress("localhost",
8082));
final ChronicleMap<Integer, Integer> map2 = ChronicleMapBuilder.of(Integer.class,
Integer.class)
.replication((byte) 2, tcpConfigServer2)
.create();
final ChronicleMap<Integer, Integer> map1 = ChronicleMapBuilder.of(Integer.class,
Integer.class)
.replication((byte) 3, tcpConfigServer1)
.create();
for (int i = 0; i < 70000; i++) {
map1.put(i, i);
}
for (int i = 0; i < 10; i++) {
Thread.sleep(100);
System.out.println(map2.size());
}
Assert.assertEquals(map1.size(),map2.size());
}
}