Search in sources :

Example 1 with KeyedMessage

use of net.dempsy.messages.KeyedMessage in project Dempsy by Dempsy.

the class TestContainerLoadHandling method runSimpleProcessingWithoutQueuing.

/*
     * Actual Unit Tests
     */
public void runSimpleProcessingWithoutQueuing(final boolean block) throws Exception {
    // set up a gate for starting
    SendMessageThread.startLatch = new CountDownLatch(1);
    // produce the initial MPs
    final ArrayList<Thread> in = new ArrayList<Thread>();
    final KeyedMessage[] messages = new KeyedMessage[NUMMPS];
    for (int i = 0; i < NUMMPS; i++) messages[i] = km(new MockInputMessage("key" + i));
    for (int j = 0; j < NUMMPS; j++) in.add(startMessagePump(container, messages, block));
    // let the messages go.
    SendMessageThread.startLatch.countDown();
    assertTrue(poll(o -> container.getProcessorCount() == NUMMPS));
    stillRunning = false;
    assertTrue(poll(o -> in.stream().filter(t -> t.isAlive()).count() == 0));
    final int numMessagePumped = dispatcher.messages.size();
    final long initialDiscard = clusterStats.getMessageCollisionCount();
    dispatcher.messages.clear();
    // Send NTHREADS Messages and wait for them to come out, then repeat
    for (int i = 0; i < NUMRUNS; i++) {
        // set up a gate for starting
        SendMessageThread.startLatch = new CountDownLatch(1);
        SendMessageThread.finishedCount.set(0);
        SendMessageThread.processingCount.set(0);
        in.clear();
        for (int j = 0; j < NUMTHREADS; j++) in.add(sendMessages(container, messages, block, MSGPERTHREAD));
        assertTrue(poll(o -> SendMessageThread.processingCount.get() == NUMTHREADS));
        // let the messages go.
        SendMessageThread.startLatch.countDown();
        // after sends are allowed to proceed
        assertTrue("Timeout waiting on message to be sent", poll(o -> SendMessageThread.finishedCount.get() >= NUMTHREADS));
        assertEquals(NUMTHREADS, SendMessageThread.finishedCount.get());
        assertTrue(poll(o -> clusterStats.getInFlightMessageCount() == 0));
        assertTrue(poll(o -> in.stream().filter(t -> t.isAlive() == true).count() == 0));
        final long discarded = clusterStats.getMessageCollisionCount();
        if (block)
            assertEquals(0L, discarded);
        final int iter = i;
        assertEquals((NUMTHREADS * MSGPERTHREAD) * (iter + 1), dispatcher.messages.size() + discarded - initialDiscard);
        assertEquals((NUMTHREADS * MSGPERTHREAD) * (iter + 1) + numMessagePumped, clusterStats.getProcessedMessageCount() + discarded - initialDiscard);
    }
    checkStat(clusterStats);
}
Also used : MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) Arrays(java.util.Arrays) NonLockingContainer(net.dempsy.container.nonlocking.NonLockingContainer) Mp(net.dempsy.lifecycle.annotation.Mp) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) Dispatcher(net.dempsy.messages.Dispatcher) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) LoggerFactory(org.slf4j.LoggerFactory) MockOutputMessage(net.dempsy.container.mocks.MockOutputMessage) Random(java.util.Random) DummyInbound(net.dempsy.container.mocks.DummyInbound) MockInputMessage(net.dempsy.container.mocks.MockInputMessage) ArrayList(java.util.ArrayList) BasicClusterStatsCollector(net.dempsy.monitoring.basic.BasicClusterStatsCollector) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector) After(org.junit.After) Manager(net.dempsy.Manager) TestInfrastructure(net.dempsy.util.TestInfrastructure) ServiceTracker(net.dempsy.ServiceTracker) Parameterized(org.junit.runners.Parameterized) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) StatsCollector(net.dempsy.monitoring.StatsCollector) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) Collection(java.util.Collection) KeyedMessage(net.dempsy.messages.KeyedMessage) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) KeyExtractor(net.dempsy.lifecycle.annotation.utils.KeyExtractor) LockingContainer(net.dempsy.container.locking.LockingContainer) Output(net.dempsy.lifecycle.annotation.Output) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClusterStatsCollector(net.dempsy.monitoring.ClusterStatsCollector) NodeStatsCollector(net.dempsy.monitoring.NodeStatsCollector) List(java.util.List) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Functional.chain(net.dempsy.util.Functional.chain) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) MockInputMessage(net.dempsy.container.mocks.MockInputMessage) CountDownLatch(java.util.concurrent.CountDownLatch) KeyedMessage(net.dempsy.messages.KeyedMessage)

Example 2 with KeyedMessage

use of net.dempsy.messages.KeyedMessage in project Dempsy by Dempsy.

the class DeliverMessageJob method executeMessageOnContainers.

protected void executeMessageOnContainers(final RoutedMessage message, final boolean justArrived) {
    final KeyedMessage km = new KeyedMessage(message.key, message.message);
    Arrays.stream(deliveries).forEach(d -> d.container.dispatch(km, Operation.handle, d.containerSpecificData, justArrived));
}
Also used : KeyedMessage(net.dempsy.messages.KeyedMessage)

Example 3 with KeyedMessage

use of net.dempsy.messages.KeyedMessage in project Dempsy by Dempsy.

the class LockingContainer method dispatch.

// this is called directly from tests but shouldn't be accessed otherwise.
@Override
public void dispatch(final KeyedMessage keyedMessage, final Operation op, final boolean youOwnMessage) throws IllegalArgumentException, ContainerException {
    if (keyedMessage == null)
        // No. We didn't process the null message
        return;
    if (keyedMessage.message == null)
        throw new IllegalArgumentException("the container for " + clusterId + " attempted to dispatch a null message.");
    final boolean callDisposition = !(youOwnMessage || op == Operation.output);
    final Object actualMessage = callDisposition ? disposition.replicate(keyedMessage.message) : keyedMessage.message;
    final Object messageKey = keyedMessage.key;
    if (messageKey == null) {
        if (callDisposition)
            disposition.dispose(actualMessage);
        throw new ContainerException("Message " + objectDescription(actualMessage) + " contains no key.");
    }
    if (!inbound.doesMessageKeyBelongToNode(messageKey)) {
        if (callDisposition)
            disposition.dispose(actualMessage);
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Message with key " + SafeString.objectDescription(messageKey) + " sent to wrong container. ");
        if (op != Operation.output)
            statCollector.messageFailed(1);
        return;
    }
    boolean evictedAndBlocking;
    try {
        numBeingWorked.incrementAndGet();
        do {
            evictedAndBlocking = false;
            final InstanceWrapper wrapper = getInstanceForKey(messageKey, actualMessage);
            // wrapper will be null if the activate returns 'false'
            if (wrapper != null) {
                final Object instance = wrapper.getExclusive();
                if (instance != null) {
                    // null indicates we didn't get the lock
                    try (QuietCloseable qc = () -> wrapper.releaseLock()) {
                        if (wrapper.isEvicted()) {
                            // if we're not blocking then we need to just return a failure. Otherwise we want to try
                            // again because eventually the current Mp will be passivated and removed from the container
                            // and a subsequent call to getInstanceForDispatch will create a new one.
                            Thread.yield();
                            // we're going to try again.
                            evictedAndBlocking = true;
                        } else {
                            invokeOperationAndHandleDispose(wrapper.getInstance(), op, new KeyedMessage(messageKey, actualMessage));
                        }
                    }
                } else {
                    // ... we didn't get the lock
                    if (LOGGER.isTraceEnabled())
                        LOGGER.trace("the container for " + clusterId + " failed to obtain lock on " + SafeString.valueOf(prototype));
                    statCollector.messageCollision(actualMessage);
                    if (callDisposition)
                        disposition.dispose(actualMessage);
                }
            } else {
                // if we got here then the activate on the Mp explicitly returned 'false'
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("the container for " + clusterId + " failed to activate the Mp for " + SafeString.valueOf(prototype));
                if (callDisposition)
                    disposition.dispose(actualMessage);
                // leave the do/while loop
                break;
            }
        } while (evictedAndBlocking);
    } finally {
        numBeingWorked.decrementAndGet();
    }
}
Also used : ContainerException(net.dempsy.container.ContainerException) KeyedMessage(net.dempsy.messages.KeyedMessage) QuietCloseable(net.dempsy.util.QuietCloseable)

Example 4 with KeyedMessage

use of net.dempsy.messages.KeyedMessage in project Dempsy by Dempsy.

the class DeliverDelayedMessageJob method executeAllContainers.

@Override
public void executeAllContainers() {
    final KeyedMessage km = new KeyedMessage(message.key, message.message);
    Arrays.stream(message.containers).forEach(i -> containers[i].dispatch(km, Operation.handle, null, justArrived));
}
Also used : KeyedMessage(net.dempsy.messages.KeyedMessage)

Example 5 with KeyedMessage

use of net.dempsy.messages.KeyedMessage in project Dempsy by Dempsy.

the class TestContainerLoadHandling method runSimpleProcessingWithoutQueuing.

/*
     * Actual Unit Tests
     */
public void runSimpleProcessingWithoutQueuing() throws Exception {
    // set up a gate for starting
    SendMessageThread.startLatch = new CountDownLatch(1);
    // produce the initial MPs
    final ArrayList<Thread> in = new ArrayList<Thread>();
    final KeyedMessage[] messages = new KeyedMessage[NUMMPS];
    for (int i = 0; i < NUMMPS; i++) messages[i] = km(new MockInputMessage("key" + i));
    for (int j = 0; j < NUMMPS; j++) in.add(startMessagePump(container, messages));
    // let the messages go.
    SendMessageThread.startLatch.countDown();
    assertTrue(poll(o -> container.getProcessorCount() == NUMMPS));
    stillRunning = false;
    assertTrue(poll(o -> in.stream().filter(t -> t.isAlive()).count() == 0));
    final int numMessagePumped = dispatcher.messages.size();
    final long initialDiscard = clusterStats.getMessageCollisionCount();
    dispatcher.messages.clear();
    // Send NTHREADS Messages and wait for them to come out, then repeat
    for (int i = 0; i < NUMRUNS; i++) {
        // set up a gate for starting
        SendMessageThread.startLatch = new CountDownLatch(1);
        SendMessageThread.finishedCount.set(0);
        SendMessageThread.processingCount.set(0);
        in.clear();
        for (int j = 0; j < NUMTHREADS; j++) in.add(sendMessages(container, messages, MSGPERTHREAD));
        assertTrue(poll(o -> SendMessageThread.processingCount.get() == NUMTHREADS));
        // let the messages go.
        SendMessageThread.startLatch.countDown();
        // after sends are allowed to proceed
        assertTrue("Timeout waiting on message to be sent", poll(o -> SendMessageThread.finishedCount.get() >= NUMTHREADS));
        assertEquals(NUMTHREADS, SendMessageThread.finishedCount.get());
        assertTrue(poll(o -> clusterStats.getInFlightMessageCount() == 0));
        assertTrue(poll(o -> in.stream().filter(t -> t.isAlive() == true).count() == 0));
        final long discarded = clusterStats.getMessageCollisionCount();
        assertEquals(0L, discarded);
        final int iter = i;
        assertEquals((NUMTHREADS * MSGPERTHREAD) * (iter + 1), dispatcher.messages.size() + discarded - initialDiscard);
        assertEquals((NUMTHREADS * MSGPERTHREAD) * (iter + 1) + numMessagePumped, clusterStats.getProcessedMessageCount() + discarded - initialDiscard);
    }
    checkStat(clusterStats);
}
Also used : MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) Arrays(java.util.Arrays) Mp(net.dempsy.lifecycle.annotation.Mp) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) Dispatcher(net.dempsy.messages.Dispatcher) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) LoggerFactory(org.slf4j.LoggerFactory) MockOutputMessage(net.dempsy.container.mocks.MockOutputMessage) Random(java.util.Random) DummyInbound(net.dempsy.container.mocks.DummyInbound) MockInputMessage(net.dempsy.container.mocks.MockInputMessage) ArrayList(java.util.ArrayList) MessageResourceManager(net.dempsy.messages.MessageResourceManager) BasicClusterStatsCollector(net.dempsy.monitoring.basic.BasicClusterStatsCollector) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector) After(org.junit.After) Manager(net.dempsy.Manager) TestInfrastructure(net.dempsy.util.TestInfrastructure) ServiceTracker(net.dempsy.ServiceTracker) Parameterized(org.junit.runners.Parameterized) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) StatsCollector(net.dempsy.monitoring.StatsCollector) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) Collection(java.util.Collection) Operation(net.dempsy.container.Container.Operation) KeyedMessage(net.dempsy.messages.KeyedMessage) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) KeyExtractor(net.dempsy.lifecycle.annotation.utils.KeyExtractor) LockingContainer(net.dempsy.container.locking.LockingContainer) Output(net.dempsy.lifecycle.annotation.Output) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClusterStatsCollector(net.dempsy.monitoring.ClusterStatsCollector) NodeStatsCollector(net.dempsy.monitoring.NodeStatsCollector) List(java.util.List) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Functional.chain(net.dempsy.util.Functional.chain) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) MockInputMessage(net.dempsy.container.mocks.MockInputMessage) CountDownLatch(java.util.concurrent.CountDownLatch) KeyedMessage(net.dempsy.messages.KeyedMessage)

Aggregations

KeyedMessage (net.dempsy.messages.KeyedMessage)10 KeyedMessageWithType (net.dempsy.messages.KeyedMessageWithType)6 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 Dispatcher (net.dempsy.messages.Dispatcher)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 Collections (java.util.Collections)3 List (java.util.List)3 Random (java.util.Random)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Manager (net.dempsy.Manager)3 ServiceTracker (net.dempsy.ServiceTracker)3 ClusterId (net.dempsy.config.ClusterId)3 NonLockingAltContainer (net.dempsy.container.altnonlocking.NonLockingAltContainer)3 LockingContainer (net.dempsy.container.locking.LockingContainer)3 DummyInbound (net.dempsy.container.mocks.DummyInbound)3 MockInputMessage (net.dempsy.container.mocks.MockInputMessage)3