use of org.goldenorb.Message in project goldenorb by jzachr.
the class MessageThread method testSingleVertex.
/**
* Tests mapping 2 messages manually to a single Vertex.
*
* @throws Exception
*/
@Test
public void testSingleVertex() throws Exception {
InboundMessageQueue imqTest = new InboundMessageQueue();
Message<Text> msg1 = new Message<Text>(Text.class);
msg1.setDestinationVertex("Test Vertex");
msg1.setMessageValue(new Text("Test Message"));
imqTest.addMessage(msg1);
Message<Text> msg2 = new Message<Text>(Text.class);
msg2.setDestinationVertex("Test Vertex");
msg2.setMessageValue(new Text("testtesttest"));
imqTest.addMessage(msg2);
List<Message<? extends Writable>> list = imqTest.getMessage("Test Vertex");
assertTrue(list.get(0) == msg1);
assertTrue(list.get(1) == msg2);
}
use of org.goldenorb.Message in project goldenorb by jzachr.
the class MessageThread method testInboundMessageQueue.
/**
* Tests mapping many messages to many Vertices using threads.
*
* @throws Exception
*/
@Test
public void testInboundMessageQueue() throws Exception {
int numOfThreads = 100;
int numOfMessages = 10000;
InboundMessageQueue imq = new InboundMessageQueue();
CountDownLatch startLatch = new CountDownLatch(1);
CountDownLatch everyoneDoneLatch = new CountDownLatch(numOfThreads);
// create new MessageThreads that add the passed message to the inbound message queue
for (int i = 0; i < numOfThreads; i++) {
Messages msgs = new Messages(TextMessage.class);
for (int p = 0; p < numOfMessages; p++) {
TextMessage txtmsg = new TextMessage(Integer.toString(i), new Text("test message " + Integer.toString(p)));
msgs.add(txtmsg);
}
MessageThread mThread = new MessageThread(msgs, imq, startLatch, everyoneDoneLatch);
mThread.start();
}
// start the threads simultaneously
startLatch.countDown();
// wait until all threads are done
everyoneDoneLatch.await();
Iterator<String> iter = imq.getVerticesWithMessages().iterator();
int count = 0;
while (iter.hasNext()) {
iter.next();
count++;
}
// check a random Vertex
int randomVertex = (int) (Math.random() * (numOfThreads));
Iterator<Message<? extends Writable>> iter2 = imq.getMessage(Integer.toString(randomVertex)).iterator();
int count2 = 0;
while (iter2.hasNext()) {
iter2.next();
count2++;
}
assertTrue(count == numOfThreads);
assertTrue(count2 == numOfMessages);
assertThat(imq.getMessage(Integer.toString(randomVertex)), notNullValue());
}
use of org.goldenorb.Message in project goldenorb by jzachr.
the class OutboundMessageThread method run.
/**
* Adds messages to the OutboundMessageQueue.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public void run() {
try {
startLatch.await();
for (Message msg : msgs.getList()) {
omq.sendMessage(msg);
}
everyoneDoneLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
use of org.goldenorb.Message in project goldenorb by jzachr.
the class OutboundMessageQueue method sendMessage.
/**
* This method queues up a Message to be sent. Once the Message count reaches the maximum number, it sends
* the vertices via Hadoop RPC.
*
* @param m
* - a Message to be sent
*/
public void sendMessage(Message<? extends Writable> m) {
synchronized (pmo) {
// get the HashMap bin that is unique to the DestinationVertex
int messageHash = Math.abs(m.getDestinationVertex().hashCode()) % numberOfPartitions;
Map<String, List<Message<? extends Writable>>> currentPartition = pmo.partitionMessageMapsList.get(messageHash);
Integer messageCounter;
synchronized (pmo.partitionMessageCounter) {
synchronized (currentPartition) {
messageCounter = pmo.partitionMessageCounter.get(messageHash);
// increment the message counter
messageCounter++;
pmo.partitionMessageCounter.set(messageHash, messageCounter);
}
// else create a new synchronized list for the key on demand, then put the list in the map
if (currentPartition.containsKey(m.getDestinationVertex())) {
currentPartition.get(m.getDestinationVertex()).add(m);
} else {
List<Message<? extends Writable>> messageList = Collections.synchronizedList(new ArrayList<Message<? extends Writable>>());
messageList.add(m);
currentPartition.put(m.getDestinationVertex(), messageList);
}
// once the expected number of messages is met, begins the message sending operation
if (messageCounter >= maxMessages) {
Messages messagesToBeSent = new Messages(messageClass);
// collects the messages associated to each key and adds them to a Messages object to be sent
for (Collection<Message<? extends Writable>> ms : currentPartition.values()) {
for (Message<? extends Writable> message : ms) {
messagesToBeSent.add(message);
}
}
// logger stuff
omqLogger.info(this.toString() + " Partition: " + Integer.toString(partitionId) + "Sending bulk messages. Count: " + messageCounter + ", " + messagesToBeSent.size());
omqLogger.info(messageClass.getName());
// sends the Messages to the partition as specified over RPC, then creates a fresh, empty Map in its
// place
orbClients.get(messageHash).sendMessages(messagesToBeSent);
pmo.partitionMessageMapsList.set(messageHash, Collections.synchronizedMap(new HashMap<String, List<Message<? extends Writable>>>()));
// reset counter to 0
pmo.partitionMessageCounter.set(messageHash, new Integer(0));
}
}
}
}
use of org.goldenorb.Message in project goldenorb by jzachr.
the class OutboundMessageQueue method sendRemainingMessages.
/**
* Sends any remaining messages if the maximum number of messages is not met.
*/
public void sendRemainingMessages() {
for (int partitionID = 0; partitionID < numberOfPartitions; partitionID++) {
Messages messagesToBeSent = new Messages(messageClass);
for (Collection<Message<? extends Writable>> ms : pmo.partitionMessageMapsList.get(partitionID).values()) {
for (Message<? extends Writable> message : ms) {
messagesToBeSent.add(message);
}
}
if (messagesToBeSent.size() > 0) {
omqLogger.info("Partition {} sending bulk messages. Count: " + pmo.partitionMessageCounter.get(partitionID) + ", " + messagesToBeSent.size(), partitionID);
orbClients.get(partitionID).sendMessages(messagesToBeSent);
} else {
omqLogger.debug("No messages to be sent from Partition {}", partitionID);
}
}
}
Aggregations