Search in sources :

Example 11 with JournalImpl

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

the class ValidateTransactionHealthTest method reload.

private void reload(final String type, final String journalDir, final long numberOfRecords, final int numberOfThreads) throws Exception {
    JournalImpl journal = ValidateTransactionHealthTest.createJournal(type, journalDir);
    journal.start();
    try {
        Loader loadTest = new Loader(numberOfRecords);
        journal.load(loadTest);
        Assert.assertEquals(numberOfRecords * numberOfThreads, loadTest.numberOfAdds);
        Assert.assertEquals(0, loadTest.numberOfPreparedTransactions);
        Assert.assertEquals(0, loadTest.numberOfUpdates);
        Assert.assertEquals(0, loadTest.numberOfDeletes);
        if (loadTest.ex != null) {
            throw loadTest.ex;
        }
    } finally {
        journal.stop();
    }
}
Also used : JournalImpl(org.apache.activemq.artemis.core.journal.impl.JournalImpl)

Example 12 with JournalImpl

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

the class NIOJournalCompactTest method internalCompactTest.

private void internalCompactTest(final boolean preXA, // prepare before compact
final boolean postXA, // prepare after compact
final boolean regularAdd, final boolean performAppend, final boolean performUpdate, boolean performDelete, boolean performNonTransactionalDelete, final boolean pendingTransactions, final boolean deleteTransactRecords, final boolean delayCommit, final boolean createControlFile, final boolean deleteControlFile, final boolean renameFilesAfterCompacting) throws Exception {
    if (performNonTransactionalDelete) {
        performDelete = false;
    }
    if (performDelete) {
        performNonTransactionalDelete = false;
    }
    setup(2, 60 * 4096, false);
    ArrayList<Long> liveIDs = new ArrayList<>();
    ArrayList<Pair<Long, Long>> transactedRecords = new ArrayList<>();
    final CountDownLatch latchDone = new CountDownLatch(1);
    final CountDownLatch latchWait = new CountDownLatch(1);
    journal = new JournalImpl(fileSize, minFiles, minFiles, 0, 0, fileFactory, filePrefix, fileExtension, maxAIO) {

        @Override
        protected SequentialFile createControlFile(final List<JournalFile> files, final List<JournalFile> newFiles, final Pair<String, String> pair) throws Exception {
            if (createControlFile) {
                return super.createControlFile(files, newFiles, pair);
            } else {
                throw new IllegalStateException("Simulating a crash during compact creation");
            }
        }

        @Override
        protected void deleteControlFile(final SequentialFile controlFile) throws Exception {
            if (deleteControlFile) {
                super.deleteControlFile(controlFile);
            }
        }

        @Override
        protected void renameFiles(final List<JournalFile> oldFiles, final List<JournalFile> newFiles) throws Exception {
            if (renameFilesAfterCompacting) {
                super.renameFiles(oldFiles, newFiles);
            }
        }

        @Override
        public void onCompactDone() {
            latchDone.countDown();
            System.out.println("Waiting on Compact");
            try {
                ActiveMQTestBase.waitForLatch(latchWait);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Done");
        }
    };
    journal.setAutoReclaim(false);
    startJournal();
    load();
    long transactionID = 0;
    if (regularAdd) {
        for (int i = 0; i < NIOJournalCompactTest.NUMBER_OF_RECORDS / 2; i++) {
            add(i);
            if (i % 10 == 0 && i > 0) {
                journal.forceMoveNextFile();
            }
            update(i);
        }
        for (int i = NIOJournalCompactTest.NUMBER_OF_RECORDS / 2; i < NIOJournalCompactTest.NUMBER_OF_RECORDS; i++) {
            addTx(transactionID, i);
            updateTx(transactionID, i);
            if (i % 10 == 0) {
                journal.forceMoveNextFile();
            }
            commit(transactionID++);
            update(i);
        }
    }
    if (pendingTransactions) {
        for (long i = 0; i < 100; i++) {
            long recordID = idGenerator.generateID();
            addTx(transactionID, recordID);
            updateTx(transactionID, recordID);
            if (preXA) {
                prepare(transactionID, new SimpleEncoding(10, (byte) 0));
            }
            transactedRecords.add(new Pair<>(transactionID++, recordID));
        }
    }
    if (regularAdd) {
        for (int i = 0; i < NIOJournalCompactTest.NUMBER_OF_RECORDS; i++) {
            if (!(i % 10 == 0)) {
                delete(i);
            } else {
                liveIDs.add((long) i);
            }
        }
    }
    journal.forceMoveNextFile();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                journal.testCompact();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    t.start();
    ActiveMQTestBase.waitForLatch(latchDone);
    int nextID = NIOJournalCompactTest.NUMBER_OF_RECORDS;
    if (performAppend) {
        for (int i = 0; i < 50; i++) {
            add(nextID++);
            if (i % 10 == 0) {
                journal.forceMoveNextFile();
            }
        }
        for (int i = 0; i < 50; i++) {
            // A Total new transaction (that was created after the compact started) to add new record while compacting
            // is still working
            addTx(transactionID, nextID++);
            commit(transactionID++);
            if (i % 10 == 0) {
                journal.forceMoveNextFile();
            }
        }
    }
    if (performUpdate) {
        int count = 0;
        for (Long liveID : liveIDs) {
            if (count++ % 2 == 0) {
                update(liveID);
            } else {
                // A Total new transaction (that was created after the compact started) to update a record that is being
                // compacted
                updateTx(transactionID, liveID);
                commit(transactionID++);
            }
        }
    }
    if (performDelete) {
        int count = 0;
        for (long liveID : liveIDs) {
            if (count++ % 2 == 0) {
                System.out.println("Deleting no trans " + liveID);
                delete(liveID);
            } else {
                System.out.println("Deleting TX " + liveID);
                // A Total new transaction (that was created after the compact started) to delete a record that is being
                // compacted
                deleteTx(transactionID, liveID);
                commit(transactionID++);
            }
            System.out.println("Deletes are going into " + ((JournalImpl) journal).getCurrentFile());
        }
    }
    if (performNonTransactionalDelete) {
        for (long liveID : liveIDs) {
            delete(liveID);
        }
    }
    if (pendingTransactions && !delayCommit) {
        for (Pair<Long, Long> tx : transactedRecords) {
            if (postXA) {
                prepare(tx.getA(), new SimpleEncoding(10, (byte) 0));
            }
            if (tx.getA() % 2 == 0) {
                commit(tx.getA());
                if (deleteTransactRecords) {
                    delete(tx.getB());
                }
            } else {
                rollback(tx.getA());
            }
        }
    }
    /**
     * Some independent adds and updates
     */
    for (int i = 0; i < 1000; i++) {
        long id = idGenerator.generateID();
        add(id);
        delete(id);
        if (i % 100 == 0) {
            journal.forceMoveNextFile();
        }
    }
    journal.forceMoveNextFile();
    latchWait.countDown();
    t.join();
    if (pendingTransactions && delayCommit) {
        for (Pair<Long, Long> tx : transactedRecords) {
            if (postXA) {
                prepare(tx.getA(), new SimpleEncoding(10, (byte) 0));
            }
            if (tx.getA() % 2 == 0) {
                commit(tx.getA());
                if (deleteTransactRecords) {
                    delete(tx.getB());
                }
            } else {
                rollback(tx.getA());
            }
        }
    }
    long lastId = idGenerator.generateID();
    add(lastId);
    if (createControlFile && deleteControlFile && renameFilesAfterCompacting) {
        journal.testCompact();
    }
    journal.flush();
    stopJournal();
    createJournal();
    startJournal();
    loadAndCheck();
    journal.forceMoveNextFile();
    update(lastId);
    stopJournal();
    createJournal();
    startJournal();
    loadAndCheck();
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) ArrayList(java.util.ArrayList) SimpleEncoding(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding) CountDownLatch(java.util.concurrent.CountDownLatch) JournalFile(org.apache.activemq.artemis.core.journal.impl.JournalFile) AtomicLong(java.util.concurrent.atomic.AtomicLong) Pair(org.apache.activemq.artemis.api.core.Pair) JournalImpl(org.apache.activemq.artemis.core.journal.impl.JournalImpl)

Example 13 with JournalImpl

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

the class CrashOnCompactTest method addJournal.

private static void addJournal(File folder, boolean crash) throws Exception {
    JournalImpl journal = createJournal(folder, crash);
    journal.loadInternalOnly();
    for (int i = 0; i < 1000; i++) {
        journal.appendAddRecord(i, (byte) 1, new byte[5], true);
    }
    for (int i = 0; i < 100; i++) {
        journal.appendDeleteRecord(i, true);
    }
    journal.compact();
    journal.stop();
}
Also used : JournalImpl(org.apache.activemq.artemis.core.journal.impl.JournalImpl)

Example 14 with JournalImpl

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

the class CrashOnCompactTest method checkJournalSize.

private void checkJournalSize() throws Exception {
    JournalImpl journal = createJournal(getTestDirfile(), false);
    ArrayList<RecordInfo> info = new ArrayList<>();
    ArrayList<PreparedTransactionInfo> txInfo = new ArrayList<>();
    journal.load(info, txInfo, new TransactionFailureCallback() {

        @Override
        public void failedTransaction(long transactionID, List<RecordInfo> records, List<RecordInfo> recordsToDelete) {
        }
    });
    Assert.assertEquals(900, info.size());
}
Also used : PreparedTransactionInfo(org.apache.activemq.artemis.core.journal.PreparedTransactionInfo) RecordInfo(org.apache.activemq.artemis.core.journal.RecordInfo) ArrayList(java.util.ArrayList) TransactionFailureCallback(org.apache.activemq.artemis.core.journal.TransactionFailureCallback) JournalImpl(org.apache.activemq.artemis.core.journal.impl.JournalImpl)

Example 15 with JournalImpl

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

the class RedeliveryConsumerTest method internaltestInfiniteDedeliveryMessageOnPersistent.

private void internaltestInfiniteDedeliveryMessageOnPersistent(final boolean strict) throws Exception {
    setUp(strict);
    ClientSession session = factory.createSession(false, false, false);
    RedeliveryConsumerTest.log.info("created");
    ClientProducer prod = session.createProducer(ADDRESS);
    prod.send(createTextMessage(session, "Hello"));
    session.commit();
    session.close();
    int expectedCount = 1;
    for (int i = 0; i < 700; i++) {
        session = factory.createSession(false, false, false);
        session.start();
        ClientConsumer consumer = session.createConsumer(ADDRESS);
        ClientMessage msg = consumer.receive(5000);
        assertNotNull(msg);
        assertEquals(expectedCount, msg.getDeliveryCount());
        if (i % 100 == 0) {
            expectedCount++;
            msg.acknowledge();
            session.rollback();
        }
        session.close();
    }
    factory.close();
    server.stop();
    setUp(false);
    for (int i = 0; i < 700; i++) {
        session = factory.createSession(false, false, false);
        session.start();
        ClientConsumer consumer = session.createConsumer(ADDRESS);
        ClientMessage msg = consumer.receive(5000);
        assertNotNull(msg);
        assertEquals(expectedCount, msg.getDeliveryCount());
        session.close();
    }
    server.stop();
    JournalImpl journal = new JournalImpl(server.getConfiguration().getJournalFileSize(), 2, 2, 0, 0, new NIOSequentialFileFactory(server.getConfiguration().getJournalLocation(), 1), "activemq-data", "amq", 1);
    final AtomicInteger updates = new AtomicInteger();
    journal.start();
    journal.load(new LoaderCallback() {

        @Override
        public void failedTransaction(long transactionID, List<RecordInfo> records, List<RecordInfo> recordsToDelete) {
        }

        @Override
        public void updateRecord(RecordInfo info) {
            if (info.userRecordType == JournalRecordIds.UPDATE_DELIVERY_COUNT) {
                updates.incrementAndGet();
            }
        }

        @Override
        public void deleteRecord(long id) {
        }

        @Override
        public void addRecord(RecordInfo info) {
        }

        @Override
        public void addPreparedTransaction(PreparedTransactionInfo preparedTransaction) {
        }
    });
    journal.stop();
    assertEquals(7, updates.get());
}
Also used : PreparedTransactionInfo(org.apache.activemq.artemis.core.journal.PreparedTransactionInfo) RecordInfo(org.apache.activemq.artemis.core.journal.RecordInfo) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) LoaderCallback(org.apache.activemq.artemis.core.journal.LoaderCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) JournalImpl(org.apache.activemq.artemis.core.journal.impl.JournalImpl) NIOSequentialFileFactory(org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory)

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