Search in sources :

Example 1 with SenderFactory

use of com.nokia.dempsy.messagetransport.SenderFactory in project Dempsy by Dempsy.

the class PassthroughTransport method createOutbound.

@Override
public SenderFactory createOutbound(DempsyExecutor executor, final StatsCollector statsCollector) {
    return new SenderFactory() {

        private volatile boolean isStopped = false;

        private StatsCollector sc = statsCollector;

        @Override
        public Sender getSender(Destination destination) throws MessageTransportException {
            if (isStopped == true)
                throw new MessageTransportException("getSender called for the destination " + SafeString.valueOf(destination) + " on a stopped " + SafeString.valueOfClass(this));
            PassthroughSender ret = (PassthroughSender) (((PassthroughDestination) destination).sender);
            ret.statsCollector = sc;
            return ret;
        }

        @Override
        public void stop() {
            isStopped = true;
        }

        // There is no 'stop' on a PassthroughSender so there's nothing to do to reclaim it.
        @Override
        public void reclaim(Destination destination) {
        }
    };
}
Also used : Destination(com.nokia.dempsy.messagetransport.Destination) StatsCollector(com.nokia.dempsy.monitoring.StatsCollector) MessageTransportException(com.nokia.dempsy.messagetransport.MessageTransportException) SenderFactory(com.nokia.dempsy.messagetransport.SenderFactory)

Example 2 with SenderFactory

use of com.nokia.dempsy.messagetransport.SenderFactory in project Dempsy by Dempsy.

the class BlockingQueueTest method setUp2.

public void setUp2(String applicationContextFilename) throws Exception {
    ctx = new ClassPathXmlApplicationContext(applicationContextFilename, getClass());
    ctx.registerShutdownHook();
    SenderFactory lsender = (SenderFactory) ctx.getBean("senderFactory");
    senderFactory = lsender;
    destinationFactory = (BlockingQueueAdaptor) ctx.getBean("adaptor");
    pojo = (MyPojo) ctx.getBean("testPojo");
    overflowHandler = (MyOverflowHandler) ctx.getBean("testOverflowHandler");
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SenderFactory(com.nokia.dempsy.messagetransport.SenderFactory)

Example 3 with SenderFactory

use of com.nokia.dempsy.messagetransport.SenderFactory in project Dempsy by Dempsy.

the class TcpTransportTest method transportLargeMessage.

/**
    * Test the sending and recieving of a large message (10 meg).
    * @throws Throwable
    */
@Test
public void transportLargeMessage() throws Throwable {
    int port = -1;
    boolean localhost = false;
    SenderFactory factory = null;
    TcpReceiver adaptor = null;
    TcpSenderFactory senderFactory = null;
    try {
        //===========================================
        // setup the sender and receiver
        adaptor = new TcpReceiver(null, getFailFast());
        // distruptible sender factory
        factory = makeSenderFactory(false, null, 500);
        receiveLargeMessageLatch = new CountDownLatch(1);
        adaptor.setListener(new Listener() {

            @Override
            public boolean onMessage(byte[] messageBytes, boolean failfast) throws MessageTransportException {
                receivedByteArrayMessage = messageBytes;
                receiveLargeMessageLatch.countDown();
                return true;
            }

            @Override
            public void shuttingDown() {
            }
        });
        if (port <= 0)
            adaptor.setUseEphemeralPort(true);
        if (port > 0)
            adaptor.setPort(port);
        if (localhost)
            adaptor.setUseLocalhost(localhost);
        //===========================================
        // start the adaptor
        adaptor.start();
        // double start ... just want more coverage.
        adaptor.start();
        // get the destination
        Destination destination = adaptor.getDestination();
        if (port > 0)
            adaptor.setPort(port);
        if (localhost)
            adaptor.setUseLocalhost(localhost);
        senderFactory = makeSenderFactory(false, null, 500);
        int size = 1024 * 1024 * 10;
        byte[] tosend = new byte[size];
        for (int i = 0; i < size; i++) tosend[i] = (byte) i;
        TcpSender sender = (TcpSender) senderFactory.getSender(destination);
        // extend the timeout because of the larger messages
        sender.setTimeoutMillis(100000);
        sender.send(tosend);
        assertTrue(receiveLargeMessageLatch.await(1, TimeUnit.MINUTES));
        assertArrayEquals(tosend, receivedByteArrayMessage);
    } finally {
        if (factory != null)
            factory.stop();
        if (senderFactory != null)
            senderFactory.stop();
        if (adaptor != null)
            adaptor.stop();
        receivedByteArrayMessage = null;
    }
}
Also used : Destination(com.nokia.dempsy.messagetransport.Destination) Listener(com.nokia.dempsy.messagetransport.Listener) MessageTransportException(com.nokia.dempsy.messagetransport.MessageTransportException) SenderFactory(com.nokia.dempsy.messagetransport.SenderFactory) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with SenderFactory

use of com.nokia.dempsy.messagetransport.SenderFactory in project Dempsy by Dempsy.

the class TcpTransportTest method testTransportInstantiation.

/**
    * Just send a simple message and make sure it gets through.
    */
@Test
public void testTransportInstantiation() throws Throwable {
    final AtomicBoolean batchedAtLeastOnce = new AtomicBoolean(false);
    runAllCombinations(new Checker() {

        @Override
        public void check(int port, boolean localhost, long batchOutgoingMessagesDelayMillis) throws Throwable {
            final StatsCollector statsCollector = new StatsCollectorFactoryCoda().createStatsCollector(new ClusterId("test", "test-cluster"), new Destination() {
            });
            SenderFactory factory = null;
            TcpReceiver adaptor = null;
            try {
                boolean shouldBatch = batchOutgoingMessagesDelayMillis >= 0;
                if (shouldBatch)
                    batchedAtLeastOnce.set(true);
                TcpTransport transport = new TcpTransport();
                transport.setFailFast(getFailFast());
                // by default batching isn't disabled.
                assertFalse(transport.isBatchingDisabled());
                if (!shouldBatch)
                    transport.setDisableBatching(true);
                if (!shouldBatch)
                    assertTrue(transport.isBatchingDisabled());
                assertEquals(!shouldBatch, transport.isBatchingDisabled());
                //===========================================
                // setup the sender and receiver
                adaptor = (TcpReceiver) transport.createInbound(null);
                adaptor.setStatsCollector(statsCollector);
                StringListener receiver = new StringListener();
                adaptor.setListener(receiver);
                factory = transport.createOutbound(null, statsCollector);
                if (port > 0)
                    adaptor.setPort(port);
                if (localhost)
                    adaptor.setUseLocalhost(localhost);
                //===========================================
                // start the adaptor
                adaptor.start();
                // get the destination
                Destination destination = adaptor.getDestination();
                // send a message
                byte[] messageBytes = "Hello".getBytes();
                Sender sender = factory.getSender(destination);
                assertEquals((shouldBatch ? TcpTransport.defaultBatchingDelayMillis : -1), ((TcpSender) sender).getBatchOutgoingMessagesDelayMillis());
                sender.send(messageBytes);
                sender.send(messageBytes);
                // wait for it to be received.
                for (long endTime = System.currentTimeMillis() + baseTimeoutMillis; endTime > System.currentTimeMillis() && receiver.numMessages.get() < 2; ) Thread.sleep(1);
                assertEquals(2, receiver.numMessages.get());
                // verify everything came over ok.
                assertEquals(1, receiver.receivedStringMessages.size());
                assertEquals("Hello", receiver.receivedStringMessages.iterator().next());
                if (shouldBatch) {
                    // verify the histogram
                    Histogram histogram = ((TcpSender) sender).getBatchingHistogram();
                    assertEquals(calcMean(2), histogram.mean(), 0.0000001);
                    assertEquals(1, histogram.count());
                }
            } finally {
                if (factory != null)
                    factory.stop();
                if (adaptor != null)
                    adaptor.stop();
            }
        }

        @Override
        public String toString() {
            return "testTransportInstantiation";
        }
    });
    assertTrue(batchedAtLeastOnce.get());
}
Also used : Destination(com.nokia.dempsy.messagetransport.Destination) Histogram(com.yammer.metrics.core.Histogram) ClusterId(com.nokia.dempsy.config.ClusterId) StatsCollector(com.nokia.dempsy.monitoring.StatsCollector) BasicStatsCollector(com.nokia.dempsy.monitoring.basic.BasicStatsCollector) Sender(com.nokia.dempsy.messagetransport.Sender) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SenderFactory(com.nokia.dempsy.messagetransport.SenderFactory) StatsCollectorFactoryCoda(com.nokia.dempsy.monitoring.coda.StatsCollectorFactoryCoda) Test(org.junit.Test)

Example 5 with SenderFactory

use of com.nokia.dempsy.messagetransport.SenderFactory in project Dempsy by Dempsy.

the class TcpTransportTest method transportMultipleConnectionsFailedClient.

/**
    * This test checks the various behaviors of a tcp transport when the 
    * client is disrupted at the 5th byte. Multiple senders are started from
    * multiple threads and ONLY ONE fails at the 5th byte. All senders should
    * be able to continue on when that happens and so the message counts should
    * increase.
    */
@Test
public void transportMultipleConnectionsFailedClient() throws Throwable {
    runAllCombinations(new Checker() {

        @Override
        public void check(int port, boolean localhost, long batchOutgoingMessagesDelayMillis) throws Throwable {
            SenderFactory factory = null;
            TcpReceiver adaptor = null;
            BasicStatsCollector statsCollector = new BasicStatsCollector();
            try {
                //===========================================
                // setup the sender and receiver
                adaptor = new TcpReceiver(null, getFailFast());
                adaptor.setStatsCollector(statsCollector);
                // distruptible sender factory
                factory = makeSenderFactory(true, statsCollector, batchOutgoingMessagesDelayMillis);
                if (port > 0)
                    adaptor.setPort(port);
                if (localhost)
                    adaptor.setUseLocalhost(localhost);
                //===========================================
                // start the adaptor
                adaptor.start();
                // get the destination
                Destination destination = adaptor.getDestination();
                //===========================================
                // Start up sender threads and save the off
                ArrayList<Thread> threadsToJoinOn = new ArrayList<Thread>();
                SenderRunnable[] senders = new SenderRunnable[numThreads];
                for (int i = 0; i < numThreads; i++) threadsToJoinOn.add(i, new Thread(senders[i] = new SenderRunnable(destination, i, factory), "Test Sender for " + i));
                for (Thread thread : threadsToJoinOn) thread.start();
                //===========================================
                //===========================================
                // check that one sender has failed since this is disruptable.
                assertTrue(TestUtils.poll(baseTimeoutMillis, statsCollector, new TestUtils.Condition<BasicStatsCollector>() {

                    @Override
                    public boolean conditionMet(BasicStatsCollector o) throws Throwable {
                        return o.getMessagesNotSentCount() > 0;
                    }
                }));
                //===========================================
                //===========================================
                // check that ONLY one failed (the others didn't) when we're not batching. Otherwise
                //  more may fail.
                Thread.sleep(10);
                if (// if we're batching then we only expect a failure but we don't know how many
                batchOutgoingMessagesDelayMillis >= 0)
                    assertTrue(statsCollector.getMessagesNotSentCount() > 0);
                else
                    assertEquals(1, statsCollector.getMessagesNotSentCount());
                //===========================================
                // all of the counts should increase.
                long[] curCounts = new long[numThreads];
                int i = 0;
                for (SenderRunnable sender : senders) curCounts[i++] = sender.sentMessageCount.get();
                // ==========================================
                // go until they are all higher. The Senders should still be running
                // and sending successfully (only one failed once) so all counts should
                // be increasing.
                boolean allHigher = false;
                for (long endTime = System.currentTimeMillis() + numThreads * (baseTimeoutMillis); endTime > System.currentTimeMillis() && !allHigher; ) {
                    allHigher = true;
                    Thread.sleep(1);
                    i = 0;
                    for (SenderRunnable sender : senders) if (curCounts[i++] >= sender.sentMessageCount.get())
                        allHigher = false;
                }
                assertTrue(allHigher);
                // Stop the senders.
                for (SenderRunnable sender : senders) sender.keepGoing.set(false);
                // wait until all threads are stopped
                for (Thread t : threadsToJoinOn) t.join(5000);
                // make sure everything actually stopped.
                for (SenderRunnable sender : senders) assertTrue(sender.isStopped.get());
            // ==========================================
            } finally {
                if (factory != null)
                    factory.stop();
                if (adaptor != null)
                    adaptor.stop();
            }
        }

        @Override
        public String toString() {
            return "transportMultipleConnectionsFailedClient";
        }
    });
}
Also used : BasicStatsCollector(com.nokia.dempsy.monitoring.basic.BasicStatsCollector) Destination(com.nokia.dempsy.messagetransport.Destination) ArrayList(java.util.ArrayList) TestUtils(com.nokia.dempsy.TestUtils) SenderFactory(com.nokia.dempsy.messagetransport.SenderFactory) Test(org.junit.Test)

Aggregations

SenderFactory (com.nokia.dempsy.messagetransport.SenderFactory)5 Destination (com.nokia.dempsy.messagetransport.Destination)4 Test (org.junit.Test)3 MessageTransportException (com.nokia.dempsy.messagetransport.MessageTransportException)2 StatsCollector (com.nokia.dempsy.monitoring.StatsCollector)2 BasicStatsCollector (com.nokia.dempsy.monitoring.basic.BasicStatsCollector)2 TestUtils (com.nokia.dempsy.TestUtils)1 ClusterId (com.nokia.dempsy.config.ClusterId)1 Listener (com.nokia.dempsy.messagetransport.Listener)1 Sender (com.nokia.dempsy.messagetransport.Sender)1 StatsCollectorFactoryCoda (com.nokia.dempsy.monitoring.coda.StatsCollectorFactoryCoda)1 Histogram (com.yammer.metrics.core.Histogram)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1