HCOLL-134 ( release draft cut of the code so that I can test it across my network )

This commit is contained in:
Rob Austin
2014-10-02 19:45:32 +01:00
parent 039a5fa75f
commit f996ba3177
4 changed files with 110 additions and 135 deletions
@@ -2,7 +2,6 @@ package net.openhft.chronicle.map;
import junit.framework.TestCase;
import net.openhft.lang.io.ByteBufferBytes;
import net.openhft.lang.io.serialization.BytesMarshallable;
import org.junit.Ignore;
import org.junit.Test;
import org.testng.Assert;
@@ -15,27 +14,7 @@ import static java.nio.ByteBuffer.allocate;
public class NodeDiscoveryBroadcasterTest extends TestCase {
public static final int SERVER2_IDENTIFER = 10;
public void test() {
// test to stop it erroring with no test
}
private ChronicleMap<Integer, CharSequence> map1;
private ChronicleMap<Integer, CharSequence> map2;
private volatile byte proposedIdentifier = 1;
private volatile boolean useAnotherIdentifier = false;
@Test
@Ignore
public void test2() throws IOException, InterruptedException {
DiscoveryCluster discoveryCluster = new DiscoveryCluster();
ChronicleMap<Integer, CharSequence> map = discoveryCluster.discoverMap(1025, 8087);
Thread.sleep(10000);
}
public static final int PROPOSED_IDENTIFIER = 5;
@Test
@@ -52,42 +31,41 @@ public class NodeDiscoveryBroadcasterTest extends TestCase {
AddressAndPort server2AddressAndPort = new AddressAndPort(server1Address, (short) 1234);
RemoteNodes server1RemoteNodes = new RemoteNodes();
BytesExternalizableImpl server1BytesExternalizable = new BytesExternalizableImpl(server1RemoteNodes, null);
KnownNodes server1KnownNodes = new KnownNodes();
BytesExternalizableImpl server1BytesExternalizable = new BytesExternalizableImpl(server1KnownNodes, null);
// for the unit test we are using a ByteBufferBytes, but iltimately this data would have been send
// and received via UDP
ByteBufferBytes udpData = new ByteBufferBytes(allocate(1024));
// SERER1
// SERER1 - is the new node join the cluster
// we will first send the boostrap along with our host and port
{
BytesExternalizableImpl.ProposedIdentifierWithHost proposedIdentifierWithHost = new BytesExternalizableImpl
.ProposedIdentifierWithHost(server1AddressAndPort, (byte) -1);
BytesExternalizableImpl.ProposedNodes proposedNodes = new BytesExternalizableImpl.ProposedNodes(server1AddressAndPort, (byte) -1);
server1BytesExternalizable.sendBootStrap(proposedIdentifierWithHost);
server1BytesExternalizable.sendBootStrap(proposedNodes);
server1BytesExternalizable.writeMarshallable(udpData);
}
RemoteNodes server2RemoteNodes = new RemoteNodes();
server2RemoteNodes.add(server2AddressAndPort, (byte) SERVER2_IDENTIFER);
BytesExternalizableImpl server2BytesExternalizable = new BytesExternalizableImpl(server2RemoteNodes, null);
KnownNodes server2KnownNodes = new KnownNodes();
server2KnownNodes.add(server2AddressAndPort, (byte) SERVER2_IDENTIFER);
BytesExternalizableImpl server2BytesExternalizable = new BytesExternalizableImpl(server2KnownNodes, null);
// add our identifier and host:port to the list of known identifiers
// SERVER 2
// SERVER 2 - is the node already in the cluster
// read the bootstrap along with the host and port it came from
{
udpData.flip();
server2BytesExternalizable.readMarshallable(udpData);
Assert.assertTrue(server2BytesExternalizable.proposedIdentifiersWithHost.values().contains
(new BytesExternalizableImpl.ProposedIdentifierWithHost(server1AddressAndPort, (byte) -1)));
(new BytesExternalizableImpl.ProposedNodes(server1AddressAndPort, (byte) -1)));
}
// SERVER 2
@@ -126,43 +104,76 @@ public class NodeDiscoveryBroadcasterTest extends TestCase {
}
// SERVER 1
// send/broadcast - propose an identifier to use, by choosing one that is not used already
// for the purpose of this test we wont choose one at random ( as the code does ) but just pick
// identifier=5
{
udpData.clear();
KnownNodes knownNodes = new KnownNodes();
RemoteNodes remoteNodes = new RemoteNodes();
BytesExternalizableImpl bytesExternalizable = new BytesExternalizableImpl(knownNodes, null);
BytesExternalizableImpl bytesExternalizable = new BytesExternalizableImpl(remoteNodes, null);
BytesExternalizableImpl.ProposedNodes proposedNodes = new BytesExternalizableImpl.ProposedNodes(server1AddressAndPort, (byte) PROPOSED_IDENTIFIER);
BytesExternalizableImpl.ProposedIdentifierWithHost proposedIdentifierWithHost = new BytesExternalizableImpl
.ProposedIdentifierWithHost(server1AddressAndPort, (byte) -1);
bytesExternalizable.sendBootStrap(proposedIdentifierWithHost);
// send the bootstrap along with this newly proposed identifier
bytesExternalizable.sendBootStrap(proposedNodes);
bytesExternalizable.writeMarshallable(udpData);
}
// SERVER 2 - is the node already in the cluster
// read the bootstrap along with the proposed identifier and host:port it came from
{
RemoteNodes remoteNodes = new RemoteNodes();
UDPEventListener udpEventListener = new UDPEventListener() {
@Override
public void onRemoteNodeEvent(RemoteNodes remoteNode, ConcurrentExpiryMap<AddressAndPort, BytesExternalizableImpl.ProposedIdentifierWithHost> proposedIdentifiersWithHost) {
int i = 1;
}
};
BytesMarshallable bytesExternalizable = new BytesExternalizableImpl(remoteNodes, udpEventListener);
udpData.flip();
bytesExternalizable.readMarshallable(udpData);
server2BytesExternalizable.readMarshallable(udpData);
Assert.assertTrue(server2BytesExternalizable.proposedIdentifiersWithHost.values().contains
(new BytesExternalizableImpl.ProposedNodes(server1AddressAndPort, (byte) PROPOSED_IDENTIFIER)));
}
// SERVER 2
// write/broadcast the the response to receiving a bootstrap message
{
//ByteBufferBytes bitSetBytes = new ByteBufferBytes(allocate(128 / 8));
// final DirectBitSet knownIdentifiers = new ATSDirectBitSet(bitSetBytes);
// RemoteNodes remoteNodes = new RemoteNodes(bitSetBytes);
udpData.clear();
// broadcasts in response to the bootstrap all the host:ports and identifiers that it knows about
// in this case given that there is only this node in the grid and one node joining,
// it will only be the node that has the bootstrap and this node, who's data will be sent
server2BytesExternalizable.writeMarshallable(udpData);
}
// SERVER 1
// read the bootstrap along with all the proposed identifiers ( this will include our
// PROPOSED_IDENTIFIER and any that come from other server
{
udpData.flip();
server1BytesExternalizable.readMarshallable(udpData);
Assert.assertTrue(server1BytesExternalizable.getRemoteNodes().addressAndPorts().contains
(server2AddressAndPort));
Assert.assertTrue(server1BytesExternalizable.getRemoteNodes().activeIdentifierBitSet().get
(SERVER2_IDENTIFER));
Assert.assertTrue(server1BytesExternalizable.proposedIdentifiersWithHost.values().contains
(new BytesExternalizableImpl.ProposedNodes(server1AddressAndPort, (byte) PROPOSED_IDENTIFIER)));
}
}
@@ -1,35 +0,0 @@
package net.openhft.chronicle.map.example;
public class WordCount {
static String[] words;
static int expectedSize;
static {
try {
// english version of war and peace -> ascii
String fullText =
new String(Files.readAllBytes(Paths.get("war_and_peace.txt")), UTF_8);
words = fullText.split("\\s+");
expectedSize = (int) Arrays.stream(words).distinct().count();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static interface ByteableIntValue extends IntValue, Byteable {}
@Benchmark
public int chronicleMap() throws IOException {
ChronicleMap<String, ByteableIntValue> map =
ChronicleMapBuilder.of(String.class, ByteableIntValue.class)
.entrySize(16)
.actualSegments(1)
.entries(expectedSize)
.create();
ByteableIntValue v = map.acquireUsing("a", null);
for (String word : words) {
map.acquireUsing(word, v).addValue(1);
}
return map.acquireUsing("a", v).getValue();
}
}