Search in sources :

Example 11 with OrderedExecutorFactory

use of org.apache.activemq.artemis.utils.actors.OrderedExecutorFactory in project activemq-artemis by apache.

the class JournalImpl method start.

@Override
public synchronized void start() {
    if (state != JournalState.STOPPED) {
        throw new IllegalStateException("Journal " + this + " is not stopped, state is " + state);
    }
    if (providedIOThreadPool == null) {
        ThreadFactory factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {

            @Override
            public ThreadFactory run() {
                return new ActiveMQThreadFactory("ArtemisIOThread", true, JournalImpl.class.getClassLoader());
            }
        });
        threadPool = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), factory);
        ioExecutorFactory = new OrderedExecutorFactory(threadPool);
    } else {
        ioExecutorFactory = providedIOThreadPool;
    }
    filesExecutor = ioExecutorFactory.getExecutor();
    compactorExecutor = ioExecutorFactory.getExecutor();
    appendExecutor = ioExecutorFactory.getExecutor();
    filesRepository.setExecutor(filesExecutor);
    fileFactory.start();
    setJournalState(JournalState.STARTED);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ActiveMQThreadFactory(org.apache.activemq.artemis.utils.ActiveMQThreadFactory) OrderedExecutorFactory(org.apache.activemq.artemis.utils.actors.OrderedExecutorFactory) SynchronousQueue(java.util.concurrent.SynchronousQueue) ActiveMQThreadFactory(org.apache.activemq.artemis.utils.ActiveMQThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 12 with OrderedExecutorFactory

use of org.apache.activemq.artemis.utils.actors.OrderedExecutorFactory in project activemq-artemis by apache.

the class ActiveMQServerImpl method initializeExecutorServices.

/**
 * Sets up ActiveMQ Artemis Executor Services.
 */
private void initializeExecutorServices() {
    /* We check to see if a Thread Pool is supplied in the InjectedObjectRegistry.  If so we created a new Ordered
       * Executor based on the provided Thread pool.  Otherwise we create a new ThreadPool.
       */
    if (serviceRegistry.getExecutorService() == null) {
        ThreadFactory tFactory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {

            @Override
            public ThreadFactory run() {
                return new ActiveMQThreadFactory("ActiveMQ-server-" + this.toString(), false, ClientSessionFactoryImpl.class.getClassLoader());
            }
        });
        if (configuration.getThreadPoolMaxSize() == -1) {
            threadPool = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), tFactory);
        } else {
            threadPool = new ActiveMQThreadPoolExecutor(0, configuration.getThreadPoolMaxSize(), 60L, TimeUnit.SECONDS, tFactory);
        }
    } else {
        threadPool = serviceRegistry.getExecutorService();
        this.threadPoolSupplied = true;
    }
    this.executorFactory = new OrderedExecutorFactory(threadPool);
    if (serviceRegistry.getIOExecutorService() != null) {
        this.ioExecutorFactory = new OrderedExecutorFactory(serviceRegistry.getIOExecutorService());
    } else {
        ThreadFactory tFactory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {

            @Override
            public ThreadFactory run() {
                return new ActiveMQThreadFactory("ActiveMQ-IO-server-" + this.toString(), false, ClientSessionFactoryImpl.class.getClassLoader());
            }
        });
        this.ioExecutorPool = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), tFactory);
        this.ioExecutorFactory = new OrderedExecutorFactory(ioExecutorPool);
    }
    /* We check to see if a Scheduled Executor Service is provided in the InjectedObjectRegistry.  If so we use this
       * Scheduled ExecutorService otherwise we create a new one.
       */
    if (serviceRegistry.getScheduledExecutorService() == null) {
        ThreadFactory tFactory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {

            @Override
            public ThreadFactory run() {
                return new ActiveMQThreadFactory("ActiveMQ-scheduled-threads", false, ClientSessionFactoryImpl.class.getClassLoader());
            }
        });
        scheduledPool = new ScheduledThreadPoolExecutor(configuration.getScheduledThreadPoolMaxSize(), tFactory);
    } else {
        this.scheduledPoolSupplied = true;
        this.scheduledPool = serviceRegistry.getScheduledExecutorService();
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ActiveMQThreadFactory(org.apache.activemq.artemis.utils.ActiveMQThreadFactory) OrderedExecutorFactory(org.apache.activemq.artemis.utils.actors.OrderedExecutorFactory) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) SynchronousQueue(java.util.concurrent.SynchronousQueue) ActiveMQThreadFactory(org.apache.activemq.artemis.utils.ActiveMQThreadFactory) ActiveMQThreadPoolExecutor(org.apache.activemq.artemis.utils.ActiveMQThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ActiveMQThreadPoolExecutor(org.apache.activemq.artemis.utils.ActiveMQThreadPoolExecutor)

Example 13 with OrderedExecutorFactory

use of org.apache.activemq.artemis.utils.actors.OrderedExecutorFactory 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

OrderedExecutorFactory (org.apache.activemq.artemis.utils.actors.OrderedExecutorFactory)13 Test (org.junit.Test)6 JournalStorageManager (org.apache.activemq.artemis.core.persistence.impl.journal.JournalStorageManager)5 ExecutorService (java.util.concurrent.ExecutorService)4 ThreadFactory (java.util.concurrent.ThreadFactory)4 ActiveMQThreadFactory (org.apache.activemq.artemis.utils.ActiveMQThreadFactory)4 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)3 ConfigurationImpl (org.apache.activemq.artemis.core.config.impl.ConfigurationImpl)3 SynchronousQueue (java.util.concurrent.SynchronousQueue)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 Configuration (org.apache.activemq.artemis.core.config.Configuration)2 JournalImpl (org.apache.activemq.artemis.core.journal.impl.JournalImpl)2 PagingManagerImpl (org.apache.activemq.artemis.core.paging.impl.PagingManagerImpl)2 PagingStoreFactoryNIO (org.apache.activemq.artemis.core.paging.impl.PagingStoreFactoryNIO)2 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)2 HierarchicalObjectRepository (org.apache.activemq.artemis.core.settings.impl.HierarchicalObjectRepository)2 Before (org.junit.Before)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1