Search in sources :

Example 11 with IOCallback

use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.

the class TimedBufferTest method testTimeOnTimedBuffer.

@Test
public void testTimeOnTimedBuffer() throws Exception {
    final ReusableLatch latchFlushed = new ReusableLatch(0);
    final AtomicInteger flushes = new AtomicInteger(0);
    class TestObserver implements TimedBufferObserver {

        @Override
        public void flushBuffer(final ByteBuffer buffer, final boolean sync, final List<IOCallback> callbacks) {
            for (IOCallback callback : callbacks) {
                callback.done();
            }
        }

        /* (non-Javadoc)
          * @see org.apache.activemq.artemis.utils.timedbuffer.TimedBufferObserver#newBuffer(int, int)
          */
        @Override
        public ByteBuffer newBuffer(final int minSize, final int maxSize) {
            return ByteBuffer.allocate(maxSize);
        }

        @Override
        public int getRemainingBytes() {
            return 1024 * 1024;
        }
    }
    TimedBuffer timedBuffer = new TimedBuffer(null, 100, TimedBufferTest.ONE_SECOND_IN_NANOS / 2, false);
    timedBuffer.start();
    TestObserver observer = new TestObserver();
    timedBuffer.setObserver(observer);
    int x = 0;
    byte[] bytes = new byte[10];
    for (int j = 0; j < 10; j++) {
        bytes[j] = ActiveMQTestBase.getSamplebyte(x++);
    }
    ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(bytes);
    IOCallback callback = new IOCallback() {

        @Override
        public void done() {
            System.out.println("done");
            latchFlushed.countDown();
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
        }
    };
    try {
        latchFlushed.setCount(2);
        // simulating a low load period
        timedBuffer.addBytes(buff, true, callback);
        Thread.sleep(1000);
        timedBuffer.addBytes(buff, true, callback);
        Assert.assertTrue(latchFlushed.await(5, TimeUnit.SECONDS));
        latchFlushed.setCount(5);
        flushes.set(0);
        // Sending like crazy... still some wait (1 millisecond) between each send..
        long time = System.currentTimeMillis();
        for (int i = 0; i < 5; i++) {
            timedBuffer.addBytes(buff, true, callback);
            Thread.sleep(1);
        }
        Assert.assertTrue(latchFlushed.await(5, TimeUnit.SECONDS));
        // The purpose of the timed buffer is to batch writes up to a millisecond.. or up to the size of the buffer.
        Assert.assertTrue("Timed Buffer is not batching accordingly, it was expected to take at least 500 seconds batching multiple writes while it took " + (System.currentTimeMillis() - time) + " milliseconds", System.currentTimeMillis() - time >= 450);
        // ^^ there are some discounts that can happen inside the timed buffer that are still considered valid (like discounting the time it took to perform the operation itself
        // for that reason the test has been failing (before this commit) at 499 or 480 milliseconds. So, I'm using a reasonable number close to 500 milliseconds that would still be valid for the test
        // it should be in fact only writing once..
        // i will set for 3 just in case there's a GC or anything else happening on the test
        Assert.assertTrue("Too many writes were called", flushes.get() <= 3);
    } finally {
        timedBuffer.stop();
    }
}
Also used : TimedBuffer(org.apache.activemq.artemis.core.io.buffer.TimedBuffer) ByteBuffer(java.nio.ByteBuffer) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) TimedBufferObserver(org.apache.activemq.artemis.core.io.buffer.TimedBufferObserver) ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 12 with IOCallback

use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.

the class OperationContextUnitTest method testCaptureExceptionOnFailure.

@Test
public void testCaptureExceptionOnFailure() throws Exception {
    ExecutorService executor = Executors.newSingleThreadExecutor(ActiveMQThreadFactory.defaultThreadFactory());
    final CountDownLatch latch = new CountDownLatch(1);
    final OperationContextImpl context = new OperationContextImpl(executor) {

        @Override
        public void complete() {
            super.complete();
            latch.countDown();
        }
    };
    context.storeLineUp();
    final AtomicInteger failures = new AtomicInteger(0);
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                context.waitCompletion(5000);
            } catch (Throwable e) {
                e.printStackTrace();
                failures.incrementAndGet();
            }
        }
    };
    t.start();
    // Need to wait complete to be called first or the test would be invalid.
    // We use a latch instead of forcing a sleep here
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    context.onError(ActiveMQExceptionType.UNSUPPORTED_PACKET.getCode(), "Poop happens!");
    t.join();
    Assert.assertEquals(1, failures.get());
    failures.set(0);
    final AtomicInteger operations = new AtomicInteger(0);
    // We should be up to date with lineUps and executions. this should now just finish processing
    context.executeOnCompletion(new IOCallback() {

        @Override
        public void done() {
            operations.incrementAndGet();
        }

        @Override
        public void onError(final int errorCode, final String errorMessage) {
            failures.incrementAndGet();
        }
    });
    Assert.assertEquals(1, failures.get());
    Assert.assertEquals(0, operations.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) OperationContextImpl(org.apache.activemq.artemis.core.persistence.impl.journal.OperationContextImpl) CountDownLatch(java.util.concurrent.CountDownLatch) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) Test(org.junit.Test)

Example 13 with IOCallback

use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.

the class OperationContextUnitTest method testCompleteTaskAfterPaging.

// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testCompleteTaskAfterPaging() throws Exception {
    ExecutorService executor = Executors.newSingleThreadExecutor(ActiveMQThreadFactory.defaultThreadFactory());
    try {
        OperationContextImpl impl = new OperationContextImpl(executor);
        final CountDownLatch latch1 = new CountDownLatch(1);
        final CountDownLatch latch2 = new CountDownLatch(1);
        impl.executeOnCompletion(new IOCallback() {

            @Override
            public void onError(int errorCode, String errorMessage) {
            }

            @Override
            public void done() {
                latch1.countDown();
            }
        });
        assertTrue(latch1.await(10, TimeUnit.SECONDS));
        for (int i = 0; i < 10; i++) impl.storeLineUp();
        for (int i = 0; i < 3; i++) impl.pageSyncLineUp();
        impl.executeOnCompletion(new IOCallback() {

            @Override
            public void onError(int errorCode, String errorMessage) {
            }

            @Override
            public void done() {
                latch2.countDown();
            }
        });
        assertFalse(latch2.await(1, TimeUnit.MILLISECONDS));
        for (int i = 0; i < 9; i++) impl.done();
        for (int i = 0; i < 2; i++) impl.pageSyncDone();
        assertFalse(latch2.await(1, TimeUnit.MILLISECONDS));
        impl.done();
        impl.pageSyncDone();
        assertTrue(latch2.await(10, TimeUnit.SECONDS));
    } finally {
        executor.shutdown();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) OperationContextImpl(org.apache.activemq.artemis.core.persistence.impl.journal.OperationContextImpl) CountDownLatch(java.util.concurrent.CountDownLatch) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) Test(org.junit.Test)

Example 14 with IOCallback

use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.

the class AMQSession method sendShouldBlockProducer.

private void sendShouldBlockProducer(final ProducerInfo producerInfo, final Message messageSend, final boolean sendProducerAck, final PagingStore store, final ActiveMQDestination dest, final AtomicInteger count, final org.apache.activemq.artemis.api.core.Message coreMsg, final SimpleString address) throws ResourceAllocationException {
    if (!store.checkMemory(() -> {
        Exception exceptionToSend = null;
        try {
            getCoreSession().send(coreMsg, false, dest.isTemporary());
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
            exceptionToSend = e;
        }
        connection.enableTtl();
        if (count == null || count.decrementAndGet() == 0) {
            if (exceptionToSend != null) {
                this.connection.getContext().setDontSendReponse(false);
                connection.sendException(exceptionToSend);
            } else {
                server.getStorageManager().afterCompleteOperations(new IOCallback() {

                    @Override
                    public void done() {
                        if (sendProducerAck) {
                            try {
                                ProducerAck ack = new ProducerAck(producerInfo.getProducerId(), messageSend.getSize());
                                connection.dispatchAsync(ack);
                            } catch (Exception e) {
                                connection.getContext().setDontSendReponse(false);
                                ActiveMQServerLogger.LOGGER.warn(e.getMessage(), e);
                                connection.sendException(e);
                            }
                        } else {
                            connection.getContext().setDontSendReponse(false);
                            try {
                                Response response = new Response();
                                response.setCorrelationId(messageSend.getCommandId());
                                connection.dispatchAsync(response);
                            } catch (Exception e) {
                                ActiveMQServerLogger.LOGGER.warn(e.getMessage(), e);
                                connection.sendException(e);
                            }
                        }
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                        try {
                            final IOException e = new IOException(errorMessage);
                            ActiveMQServerLogger.LOGGER.warn(errorMessage);
                            connection.serviceException(e);
                        } catch (Exception ex) {
                            ActiveMQServerLogger.LOGGER.debug(ex);
                        }
                    }
                });
            }
        }
    })) {
        this.connection.getContext().setDontSendReponse(false);
        connection.enableTtl();
        throw new ResourceAllocationException("Queue is full " + address);
    }
}
Also used : Response(org.apache.activemq.command.Response) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) IOException(java.io.IOException) ResourceAllocationException(javax.jms.ResourceAllocationException) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) ProducerAck(org.apache.activemq.command.ProducerAck) InvalidDestinationException(javax.jms.InvalidDestinationException) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) IOException(java.io.IOException) ResourceAllocationException(javax.jms.ResourceAllocationException)

Example 15 with IOCallback

use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.

the class ReplicationTest method testNoActions.

@Test
public void testNoActions() throws Exception {
    setupServer(true);
    StorageManager storage = getStorage();
    manager = liveServer.getReplicationManager();
    waitForComponent(manager);
    Journal replicatedJournal = new ReplicatedJournal((byte) 1, new FakeJournal(), manager);
    replicatedJournal.appendPrepareRecord(1, new FakeData(), false);
    final CountDownLatch latch = new CountDownLatch(1);
    storage.afterCompleteOperations(new IOCallback() {

        @Override
        public void onError(final int errorCode, final String errorMessage) {
        }

        @Override
        public void done() {
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
    Assert.assertEquals("should be empty " + manager.getActiveTokens(), 0, manager.getActiveTokens().size());
}
Also used : JournalStorageManager(org.apache.activemq.artemis.core.persistence.impl.journal.JournalStorageManager) StorageManager(org.apache.activemq.artemis.core.persistence.StorageManager) ReplicatedJournal(org.apache.activemq.artemis.core.replication.ReplicatedJournal) Journal(org.apache.activemq.artemis.core.journal.Journal) ReplicatedJournal(org.apache.activemq.artemis.core.replication.ReplicatedJournal) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CountDownLatch(java.util.concurrent.CountDownLatch) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) Test(org.junit.Test)

Aggregations

IOCallback (org.apache.activemq.artemis.core.io.IOCallback)27 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)12 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)7 ArrayList (java.util.ArrayList)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ByteBuffer (java.nio.ByteBuffer)4 ExecutorService (java.util.concurrent.ExecutorService)3 ActiveMQIllegalStateException (org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)3 OperationContext (org.apache.activemq.artemis.core.persistence.OperationContext)3 JournalStorageManager (org.apache.activemq.artemis.core.persistence.impl.journal.JournalStorageManager)3 OperationContextImpl (org.apache.activemq.artemis.core.persistence.impl.journal.OperationContextImpl)3 IOException (java.io.IOException)2 ActiveMQQueueExistsException (org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException)2 Message (org.apache.activemq.artemis.api.core.Message)2 Journal (org.apache.activemq.artemis.core.journal.Journal)2 SimpleWaitIOCallback (org.apache.activemq.artemis.core.journal.impl.SimpleWaitIOCallback)2 StorageManager (org.apache.activemq.artemis.core.persistence.StorageManager)2 ReplicatedJournal (org.apache.activemq.artemis.core.replication.ReplicatedJournal)2 MessageReference (org.apache.activemq.artemis.core.server.MessageReference)2