Search in sources :

Example 1 with Messages

use of org.goldenorb.Messages 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());
}
Also used : Messages(org.goldenorb.Messages) Message(org.goldenorb.Message) TextMessage(org.goldenorb.types.message.TextMessage) Writable(org.apache.hadoop.io.Writable) Text(org.apache.hadoop.io.Text) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(org.goldenorb.types.message.TextMessage) Test(org.junit.Test)

Example 2 with Messages

use of org.goldenorb.Messages 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));
            }
        }
    }
}
Also used : Messages(org.goldenorb.Messages) Message(org.goldenorb.Message) HashMap(java.util.HashMap) Writable(org.apache.hadoop.io.Writable) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with Messages

use of org.goldenorb.Messages 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);
        }
    }
}
Also used : Messages(org.goldenorb.Messages) Message(org.goldenorb.Message) Writable(org.apache.hadoop.io.Writable)

Example 4 with Messages

use of org.goldenorb.Messages in project goldenorb by jzachr.

the class OutboundMessageThread method testOutBoundMessageQueue.

@Test
public void testOutBoundMessageQueue() throws Exception {
    // OutboundMessageQueue settings
    int numberOfPartitions = 100;
    // max number of Messages to be sent by a Thread
    int numOfMessagesToSendPerThread = 10500;
    // max number of Messages to trigger a send operation by the queue
    int numOfMessagesPerBlock = 1000;
    int partitionId = 1;
    Class<? extends Message<? extends Writable>> messageClass = TextMessage.class;
    Map<Integer, OrbPartitionCommunicationProtocol> orbClients = new HashMap<Integer, OrbPartitionCommunicationProtocol>();
    for (int i = 0; i < numberOfPartitions; i++) {
        orbClients.put(new Integer(i), infoCollector);
    }
    OutboundMessageQueue omq = new OutboundMessageQueue(numberOfPartitions, numOfMessagesPerBlock, orbClients, messageClass, partitionId);
    // initialize the Threads and pass them their test Messages
    CountDownLatch startLatch = new CountDownLatch(1);
    CountDownLatch everyoneDoneLatch = new CountDownLatch(numberOfPartitions);
    for (int i = 0; i < numberOfPartitions; i++) {
        Messages msgs = new Messages(TextMessage.class);
        for (int p = 0; p < numOfMessagesToSendPerThread; p++) {
            TextMessage txtmsg = new TextMessage(Integer.toString(i), new Text("test message " + Integer.toString(p)));
            msgs.add(txtmsg);
        }
        OutboundMessageThread obmThread = new OutboundMessageThread(msgs, omq, startLatch, everyoneDoneLatch);
        // initialize a Thread
        obmThread.start();
    }
    // start all Threads simultaneously
    startLatch.countDown();
    // wait until all Threads are done
    everyoneDoneLatch.await();
    omq.sendRemainingMessages();
    assertThat(omq, notNullValue());
    assertTrue(infoCollector.mList.size() == (numberOfPartitions * numOfMessagesToSendPerThread));
}
Also used : OrbPartitionCommunicationProtocol(org.goldenorb.OrbPartitionCommunicationProtocol) Messages(org.goldenorb.Messages) HashMap(java.util.HashMap) Text(org.apache.hadoop.io.Text) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(org.goldenorb.types.message.TextMessage) Test(org.junit.Test)

Aggregations

Messages (org.goldenorb.Messages)4 Writable (org.apache.hadoop.io.Writable)3 Message (org.goldenorb.Message)3 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Text (org.apache.hadoop.io.Text)2 TextMessage (org.goldenorb.types.message.TextMessage)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 OrbPartitionCommunicationProtocol (org.goldenorb.OrbPartitionCommunicationProtocol)1