Search in sources :

Example 1 with OperationContextImpl

use of org.apache.activemq.artemis.core.persistence.impl.journal.OperationContextImpl in project activemq-artemis by apache.

the class OperationContextUnitTest method testCaptureExceptionOnExecutor.

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

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

        @Override
        public void run() {
            try {
                impl.waitCompletion(5000);
            } catch (Throwable e) {
                e.printStackTrace();
                numberOfFailures.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));
    impl.done();
    t.join();
    Assert.assertEquals(1, numberOfFailures.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) Test(org.junit.Test)

Example 2 with OperationContextImpl

use of org.apache.activemq.artemis.core.persistence.impl.journal.OperationContextImpl 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 3 with OperationContextImpl

use of org.apache.activemq.artemis.core.persistence.impl.journal.OperationContextImpl 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 4 with OperationContextImpl

use of org.apache.activemq.artemis.core.persistence.impl.journal.OperationContextImpl in project activemq-artemis by apache.

the class NIOJournalCompactTest method testStressDeletesNoSync.

@Test
public void testStressDeletesNoSync() throws Throwable {
    Configuration config = createBasicConfig().setJournalFileSize(100 * 1024).setJournalSyncNonTransactional(false).setJournalSyncTransactional(false).setJournalCompactMinFiles(0).setJournalCompactPercentage(0);
    final AtomicInteger errors = new AtomicInteger(0);
    final AtomicBoolean running = new AtomicBoolean(true);
    final AtomicLong seqGenerator = new AtomicLong(1);
    final ExecutorService executor = Executors.newCachedThreadPool(ActiveMQThreadFactory.defaultThreadFactory());
    final ExecutorService ioexecutor = Executors.newCachedThreadPool(ActiveMQThreadFactory.defaultThreadFactory());
    OrderedExecutorFactory factory = new OrderedExecutorFactory(executor);
    OrderedExecutorFactory iofactory = new OrderedExecutorFactory(ioexecutor);
    final ExecutorService deleteExecutor = Executors.newCachedThreadPool(ActiveMQThreadFactory.defaultThreadFactory());
    final JournalStorageManager storage = new JournalStorageManager(config, EmptyCriticalAnalyzer.getInstance(), factory, iofactory);
    storage.start();
    try {
        storage.loadInternalOnly();
        ((JournalImpl) storage.getMessageJournal()).setAutoReclaim(false);
        final LinkedList<Long> survivingMsgs = new LinkedList<>();
        Runnable producerRunnable = new Runnable() {

            @Override
            public void run() {
                try {
                    while (running.get()) {
                        final long[] values = new long[100];
                        long tx = seqGenerator.incrementAndGet();
                        OperationContextImpl ctx = new OperationContextImpl(executor);
                        storage.setContext(ctx);
                        for (int i = 0; i < 100; i++) {
                            long id = seqGenerator.incrementAndGet();
                            values[i] = id;
                            CoreMessage message = new CoreMessage(id, 100);
                            message.getBodyBuffer().writeBytes(new byte[1024]);
                            storage.storeMessageTransactional(tx, message);
                        }
                        CoreMessage message = new CoreMessage(seqGenerator.incrementAndGet(), 100);
                        survivingMsgs.add(message.getMessageID());
                        logger.info("Going to store " + message);
                        // This one will stay here forever
                        storage.storeMessage(message);
                        logger.info("message storeed " + message);
                        logger.info("Going to commit " + tx);
                        storage.commit(tx);
                        logger.info("Committed " + tx);
                        ctx.executeOnCompletion(new IOCallback() {

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

                            @Override
                            public void done() {
                                deleteExecutor.execute(new Runnable() {

                                    @Override
                                    public void run() {
                                        try {
                                            for (long messageID : values) {
                                                storage.deleteMessage(messageID);
                                            }
                                        } catch (Throwable e) {
                                            e.printStackTrace();
                                            errors.incrementAndGet();
                                        }
                                    }
                                });
                            }
                        });
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                    errors.incrementAndGet();
                }
            }
        };
        Runnable compressRunnable = new Runnable() {

            @Override
            public void run() {
                try {
                    while (running.get()) {
                        Thread.sleep(500);
                        System.out.println("Compacting");
                        ((JournalImpl) storage.getMessageJournal()).testCompact();
                        ((JournalImpl) storage.getMessageJournal()).checkReclaimStatus();
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                    errors.incrementAndGet();
                }
            }
        };
        Thread producerThread = new Thread(producerRunnable);
        producerThread.start();
        Thread compactorThread = new Thread(compressRunnable);
        compactorThread.start();
        Thread.sleep(1000);
        running.set(false);
        producerThread.join();
        compactorThread.join();
        deleteExecutor.shutdown();
        assertTrue("delete executor failted to terminate", deleteExecutor.awaitTermination(30, TimeUnit.SECONDS));
        storage.stop();
        executor.shutdown();
        assertTrue("executor failed to terminate", executor.awaitTermination(30, TimeUnit.SECONDS));
        ioexecutor.shutdown();
        assertTrue("ioexecutor failed to terminate", ioexecutor.awaitTermination(30, TimeUnit.SECONDS));
        Assert.assertEquals(0, errors.get());
    } catch (Throwable e) {
        e.printStackTrace();
        throw e;
    } finally {
        try {
            storage.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
        executor.shutdownNow();
        deleteExecutor.shutdownNow();
        ioexecutor.shutdownNow();
    }
}
Also used : OrderedExecutorFactory(org.apache.activemq.artemis.utils.actors.OrderedExecutorFactory) Configuration(org.apache.activemq.artemis.core.config.Configuration) OperationContextImpl(org.apache.activemq.artemis.core.persistence.impl.journal.OperationContextImpl) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) LinkedList(java.util.LinkedList) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) JournalStorageManager(org.apache.activemq.artemis.core.persistence.impl.journal.JournalStorageManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) AtomicLong(java.util.concurrent.atomic.AtomicLong) JournalImpl(org.apache.activemq.artemis.core.journal.impl.JournalImpl) Test(org.junit.Test)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)4 OperationContextImpl (org.apache.activemq.artemis.core.persistence.impl.journal.OperationContextImpl)4 Test (org.junit.Test)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IOCallback (org.apache.activemq.artemis.core.io.IOCallback)3 LinkedList (java.util.LinkedList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Configuration (org.apache.activemq.artemis.core.config.Configuration)1 JournalImpl (org.apache.activemq.artemis.core.journal.impl.JournalImpl)1 CoreMessage (org.apache.activemq.artemis.core.message.impl.CoreMessage)1 JournalStorageManager (org.apache.activemq.artemis.core.persistence.impl.journal.JournalStorageManager)1 OrderedExecutorFactory (org.apache.activemq.artemis.utils.actors.OrderedExecutorFactory)1