Search in sources :

Example 36 with JournalImpl

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

the class ValidateTransactionHealthTest method appendData.

public static JournalImpl appendData(final String journalType, final String journalDir, final long numberOfElements, final int transactionSize, final int numberOfThreads) throws Exception {
    final JournalImpl journal = ValidateTransactionHealthTest.createJournal(journalType, journalDir);
    journal.start();
    journal.load(new LoaderCallback() {

        @Override
        public void addPreparedTransaction(final PreparedTransactionInfo preparedTransaction) {
        }

        @Override
        public void addRecord(final RecordInfo info) {
        }

        @Override
        public void deleteRecord(final long id) {
        }

        @Override
        public void updateRecord(final RecordInfo info) {
        }

        @Override
        public void failedTransaction(final long transactionID, final List<RecordInfo> records, final List<RecordInfo> recordsToDelete) {
        }
    });
    LocalThread[] threads = new LocalThread[numberOfThreads];
    final AtomicLong sequenceTransaction = new AtomicLong();
    for (int i = 0; i < numberOfThreads; i++) {
        threads[i] = new LocalThread(journal, numberOfElements, transactionSize, sequenceTransaction);
        threads[i].start();
    }
    Exception e = null;
    for (LocalThread t : threads) {
        t.join();
        if (t.e != null) {
            e = t.e;
        }
    }
    if (e != null) {
        throw e;
    }
    journal.flush();
    return journal;
}
Also used : PreparedTransactionInfo(org.apache.activemq.artemis.core.journal.PreparedTransactionInfo) AtomicLong(java.util.concurrent.atomic.AtomicLong) RecordInfo(org.apache.activemq.artemis.core.journal.RecordInfo) LoaderCallback(org.apache.activemq.artemis.core.journal.LoaderCallback) JournalImpl(org.apache.activemq.artemis.core.journal.impl.JournalImpl)

Example 37 with JournalImpl

use of org.apache.activemq.artemis.core.journal.impl.JournalImpl 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)

Example 38 with JournalImpl

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

the class NIOJournalCompactTest method testCompactFirstFileReclaimed.

@Test
public void testCompactFirstFileReclaimed() throws Exception {
    setup(2, 60 * 1024, false);
    final byte recordType = (byte) 0;
    journal = new JournalImpl(fileSize, minFiles, minFiles, 0, 0, fileFactory, filePrefix, fileExtension, maxAIO);
    journal.start();
    journal.loadInternalOnly();
    journal.appendAddRecord(1, recordType, "test".getBytes(), true);
    journal.forceMoveNextFile();
    journal.appendUpdateRecord(1, recordType, "update".getBytes(), true);
    journal.appendDeleteRecord(1, true);
    journal.appendAddRecord(2, recordType, "finalRecord".getBytes(), true);
    for (int i = 10; i < 100; i++) {
        journal.appendAddRecord(i, recordType, ("tst" + i).getBytes(), true);
        journal.forceMoveNextFile();
        journal.appendUpdateRecord(i, recordType, ("uptst" + i).getBytes(), true);
        journal.appendDeleteRecord(i, true);
    }
    journal.testCompact();
    journal.stop();
    List<RecordInfo> records1 = new ArrayList<>();
    List<PreparedTransactionInfo> preparedRecords = new ArrayList<>();
    journal.start();
    journal.load(records1, preparedRecords, null);
    assertEquals(1, records1.size());
}
Also used : PreparedTransactionInfo(org.apache.activemq.artemis.core.journal.PreparedTransactionInfo) RecordInfo(org.apache.activemq.artemis.core.journal.RecordInfo) ArrayList(java.util.ArrayList) JournalImpl(org.apache.activemq.artemis.core.journal.impl.JournalImpl) Test(org.junit.Test)

Example 39 with JournalImpl

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

the class CrashOnCompactTest method createJournal.

private static JournalImpl createJournal(File folder, boolean crash) throws Exception {
    NIOSequentialFileFactory factory = new NIOSequentialFileFactory(folder, 10);
    JournalImpl journal = new JournalImpl(100 * 1024, 2, 2, 0, 0, factory, "jrntest", "jrn", 512) {

        @Override
        protected SequentialFile writeControlFile(final SequentialFileFactory fileFactory, final List<JournalFile> files, final List<JournalFile> newFiles, final List<Pair<String, String>> renames) throws Exception {
            if (crash) {
                SequentialFile controlFile = fileFactory.createSequentialFile(AbstractJournalUpdateTask.FILE_COMPACT_CONTROL);
                controlFile.open();
                controlFile.close();
                System.err.println("crashing after creation of control file");
                System.exit(OK);
            }
            return JournalCompactor.writeControlFile(fileFactory, files, newFiles, renames);
        }
    };
    journal.setAutoReclaim(false);
    journal.start();
    return journal;
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) ArrayList(java.util.ArrayList) List(java.util.List) NIOSequentialFileFactory(org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory) SequentialFileFactory(org.apache.activemq.artemis.core.io.SequentialFileFactory) NIOSequentialFileFactory(org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory) JournalImpl(org.apache.activemq.artemis.core.journal.impl.JournalImpl)

Example 40 with JournalImpl

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

the class ActiveMQTestBase method countJournal.

/**
 * Reads a journal system and returns a Map<Integer,AtomicInteger> of recordTypes and the number of records per type,
 * independent of being deleted or not
 *
 * @param config
 * @return
 * @throws Exception
 */
protected HashMap<Integer, AtomicInteger> countJournal(Configuration config) throws Exception {
    final HashMap<Integer, AtomicInteger> recordsType = new HashMap<>();
    SequentialFileFactory messagesFF = new NIOSequentialFileFactory(config.getJournalLocation(), null, 1);
    JournalImpl messagesJournal = new JournalImpl(config.getJournalFileSize(), config.getJournalMinFiles(), config.getJournalPoolFiles(), 0, 0, messagesFF, "activemq-data", "amq", 1);
    List<JournalFile> filesToRead = messagesJournal.orderFiles();
    for (JournalFile file : filesToRead) {
        JournalImpl.readJournalFile(messagesFF, file, new RecordTypeCounter(recordsType));
    }
    return recordsType;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JournalFile(org.apache.activemq.artemis.core.journal.impl.JournalFile) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) NIOSequentialFileFactory(org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory) SequentialFileFactory(org.apache.activemq.artemis.core.io.SequentialFileFactory) NIOSequentialFileFactory(org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory) JournalImpl(org.apache.activemq.artemis.core.journal.impl.JournalImpl)

Aggregations

JournalImpl (org.apache.activemq.artemis.core.journal.impl.JournalImpl)41 RecordInfo (org.apache.activemq.artemis.core.journal.RecordInfo)20 NIOSequentialFileFactory (org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory)19 SequentialFileFactory (org.apache.activemq.artemis.core.io.SequentialFileFactory)13 PreparedTransactionInfo (org.apache.activemq.artemis.core.journal.PreparedTransactionInfo)13 Test (org.junit.Test)12 File (java.io.File)10 ArrayList (java.util.ArrayList)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 JournalFile (org.apache.activemq.artemis.core.journal.impl.JournalFile)6 SimpleEncoding (org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding)6 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 AIOSequentialFileFactory (org.apache.activemq.artemis.core.io.aio.AIOSequentialFileFactory)5 Pair (org.apache.activemq.artemis.api.core.Pair)4 Journal (org.apache.activemq.artemis.core.journal.Journal)4 FakeSequentialFileFactory (org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.FakeSequentialFileFactory)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Configuration (org.apache.activemq.artemis.core.config.Configuration)3 MappedSequentialFileFactory (org.apache.activemq.artemis.core.io.mapped.MappedSequentialFileFactory)3