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);
}
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));
}
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();
}
}
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));
}
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);
}
Aggregations