Search in sources :

Example 31 with SimpleEncoding

use of org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding in project activemq-artemis by apache.

the class AlignedJournalImplTest method testEmptyPrepare.

@Test
public void testEmptyPrepare() throws Exception {
    final int JOURNAL_SIZE = 512 * 4;
    setupAndLoadJournal(JOURNAL_SIZE, 1);
    journalImpl.appendPrepareRecord(2L, new SimpleEncoding(10, (byte) 'j'), false);
    journalImpl.forceMoveNextFile();
    journalImpl.appendAddRecord(1L, (byte) 0, new SimpleEncoding(10, (byte) 'k'), false);
    setupAndLoadJournal(JOURNAL_SIZE, 1);
    Assert.assertEquals(1, journalImpl.getDataFilesCount());
    Assert.assertEquals(1, transactions.size());
    journalImpl.forceMoveNextFile();
    setupAndLoadJournal(JOURNAL_SIZE, 1);
    Assert.assertEquals(1, journalImpl.getDataFilesCount());
    Assert.assertEquals(1, transactions.size());
    journalImpl.appendCommitRecord(2L, false);
    journalImpl.appendDeleteRecord(1L, false);
    journalImpl.forceMoveNextFile();
    setupAndLoadJournal(JOURNAL_SIZE, 0);
    journalImpl.forceMoveNextFile();
    journalImpl.debugWait();
    journalImpl.checkReclaimStatus();
    Assert.assertEquals(0, transactions.size());
    Assert.assertEquals(0, journalImpl.getDataFilesCount());
}
Also used : SimpleEncoding(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding) Test(org.junit.Test)

Example 32 with SimpleEncoding

use of org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding in project activemq-artemis by apache.

the class AlignedJournalImplTest method testReduceFreeFiles.

@Test
public void testReduceFreeFiles() throws Exception {
    final int JOURNAL_SIZE = 2000;
    setupAndLoadJournal(JOURNAL_SIZE, 100, 10);
    Assert.assertEquals(10, factory.listFiles("tt").size());
    setupAndLoadJournal(JOURNAL_SIZE, 100, 2);
    Assert.assertEquals(10, factory.listFiles("tt").size());
    for (int i = 0; i < 10; i++) {
        journalImpl.appendAddRecord(i, (byte) 0, new SimpleEncoding(1, (byte) 0), false);
        journalImpl.forceMoveNextFile();
    }
    setupAndLoadJournal(JOURNAL_SIZE, 100, 2);
    Assert.assertEquals(10, records.size());
    Assert.assertEquals(12, factory.listFiles("tt").size());
    for (int i = 0; i < 10; i++) {
        journalImpl.appendDeleteRecord(i, false);
    }
    journalImpl.forceMoveNextFile();
    journalImpl.checkReclaimStatus();
    setupAndLoadJournal(JOURNAL_SIZE, 100, 2);
    Assert.assertEquals(0, records.size());
    Assert.assertEquals(2, factory.listFiles("tt").size());
}
Also used : SimpleEncoding(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding) Test(org.junit.Test)

Example 33 with SimpleEncoding

use of org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding in project activemq-artemis by apache.

the class AlignedJournalImplTest method testReloadWithCompletedTransaction.

@Test
public void testReloadWithCompletedTransaction() throws Exception {
    final int JOURNAL_SIZE = 2000;
    setupAndLoadJournal(JOURNAL_SIZE, 100);
    Assert.assertEquals(0, records.size());
    Assert.assertEquals(0, transactions.size());
    for (int i = 0; i < 10; i++) {
        journalImpl.appendAddRecordTransactional(1, i, (byte) 1, new SimpleEncoding(1, (byte) 1));
        journalImpl.forceMoveNextFile();
    }
    journalImpl.appendCommitRecord(1L, false);
    journalImpl.debugWait();
    Assert.assertEquals(12, factory.listFiles("tt").size());
    setupAndLoadJournal(JOURNAL_SIZE, 100);
    Assert.assertEquals(10, records.size());
    Assert.assertEquals(0, transactions.size());
    journalImpl.checkReclaimStatus();
    Assert.assertEquals(10, journalImpl.getDataFilesCount());
    Assert.assertEquals(12, factory.listFiles("tt").size());
    for (int i = 0; i < 10; i++) {
        journalImpl.appendDeleteRecordTransactional(2L, i);
        journalImpl.forceMoveNextFile();
    }
    journalImpl.appendCommitRecord(2L, false);
    journalImpl.appendAddRecord(100, (byte) 1, new SimpleEncoding(5, (byte) 1), false);
    journalImpl.forceMoveNextFile();
    journalImpl.appendAddRecord(101, (byte) 1, new SimpleEncoding(5, (byte) 1), false);
    journalImpl.checkReclaimStatus();
    Assert.assertEquals(1, journalImpl.getDataFilesCount());
    setupAndLoadJournal(JOURNAL_SIZE, 100);
    Assert.assertEquals(1, journalImpl.getDataFilesCount());
    Assert.assertEquals(3, factory.listFiles("tt").size());
}
Also used : SimpleEncoding(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding) Test(org.junit.Test)

Example 34 with SimpleEncoding

use of org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding in project activemq-artemis by apache.

the class AlignedJournalImplTest method testReclaimingAfterConcurrentAddsAndDeletes.

public void testReclaimingAfterConcurrentAddsAndDeletes(final boolean transactional) throws Exception {
    final int JOURNAL_SIZE = 10 * 1024;
    setupAndLoadJournal(JOURNAL_SIZE, 1);
    Assert.assertEquals(0, records.size());
    Assert.assertEquals(0, transactions.size());
    final CountDownLatch latchReady = new CountDownLatch(2);
    final CountDownLatch latchStart = new CountDownLatch(1);
    final AtomicInteger finishedOK = new AtomicInteger(0);
    final BlockingQueue<Integer> queueDelete = new LinkedBlockingQueue<>();
    final int NUMBER_OF_ELEMENTS = 500;
    Thread t1 = new Thread() {

        @Override
        public void run() {
            try {
                latchReady.countDown();
                ActiveMQTestBase.waitForLatch(latchStart);
                for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
                    if (transactional) {
                        journalImpl.appendAddRecordTransactional(i, i, (byte) 1, new SimpleEncoding(50, (byte) 1));
                        journalImpl.appendCommitRecord(i, false);
                    } else {
                        journalImpl.appendAddRecord(i, (byte) 1, new SimpleEncoding(50, (byte) 1), false);
                    }
                    queueDelete.offer(i);
                }
                finishedOK.incrementAndGet();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    Thread t2 = new Thread() {

        @Override
        public void run() {
            try {
                latchReady.countDown();
                ActiveMQTestBase.waitForLatch(latchStart);
                for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
                    Integer toDelete = queueDelete.poll(10, TimeUnit.SECONDS);
                    if (toDelete == null) {
                        break;
                    }
                    if (transactional) {
                        journalImpl.appendDeleteRecordTransactional(toDelete, toDelete, new SimpleEncoding(50, (byte) 1));
                        journalImpl.appendCommitRecord(i, false);
                    } else {
                        journalImpl.appendDeleteRecord(toDelete, false);
                    }
                }
                finishedOK.incrementAndGet();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    t1.start();
    t2.start();
    ActiveMQTestBase.waitForLatch(latchReady);
    latchStart.countDown();
    t1.join();
    t2.join();
    Assert.assertEquals(2, finishedOK.intValue());
    journalImpl.debugWait();
    journalImpl.forceMoveNextFile();
    journalImpl.debugWait();
    journalImpl.checkReclaimStatus();
    Assert.assertEquals(0, journalImpl.getDataFilesCount());
    Assert.assertEquals(2, factory.listFiles("tt").size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SimpleEncoding(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 35 with SimpleEncoding

use of org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding in project activemq-artemis by apache.

the class AlignedJournalImplTest method testReloadWithPreparedTransaction.

@Test
public void testReloadWithPreparedTransaction() throws Exception {
    final int JOURNAL_SIZE = 3 * 1024;
    setupAndLoadJournal(JOURNAL_SIZE, 1);
    Assert.assertEquals(0, records.size());
    Assert.assertEquals(0, transactions.size());
    for (int i = 0; i < 10; i++) {
        journalImpl.appendAddRecordTransactional(1, i, (byte) 1, new SimpleEncoding(50, (byte) 1));
        journalImpl.forceMoveNextFile();
    }
    journalImpl.debugWait();
    SimpleEncoding xid1 = new SimpleEncoding(10, (byte) 1);
    journalImpl.appendPrepareRecord(1L, xid1, false);
    Assert.assertEquals(12, factory.listFiles("tt").size());
    setupAndLoadJournal(JOURNAL_SIZE, 1024);
    Assert.assertEquals(0, records.size());
    Assert.assertEquals(1, transactions.size());
    Assert.assertEquals(10, transactions.get(0).getExtraData().length);
    for (int i = 0; i < 10; i++) {
        Assert.assertEquals((byte) 1, transactions.get(0).getExtraData()[i]);
    }
    journalImpl.checkReclaimStatus();
    Assert.assertEquals(10, journalImpl.getDataFilesCount());
    Assert.assertEquals(12, factory.listFiles("tt").size());
    journalImpl.appendCommitRecord(1L, false);
    setupAndLoadJournal(JOURNAL_SIZE, 1024);
    Assert.assertEquals(10, records.size());
    journalImpl.checkReclaimStatus();
    for (int i = 0; i < 10; i++) {
        journalImpl.appendDeleteRecordTransactional(2L, i);
    }
    SimpleEncoding xid2 = new SimpleEncoding(15, (byte) 2);
    journalImpl.appendPrepareRecord(2L, xid2, false);
    setupAndLoadJournal(JOURNAL_SIZE, 1);
    Assert.assertEquals(1, transactions.size());
    Assert.assertEquals(15, transactions.get(0).getExtraData().length);
    for (byte element : transactions.get(0).getExtraData()) {
        Assert.assertEquals(2, element);
    }
    Assert.assertEquals(10, journalImpl.getDataFilesCount());
    Assert.assertEquals(12, factory.listFiles("tt").size());
    journalImpl.appendCommitRecord(2L, false);
    setupAndLoadJournal(JOURNAL_SIZE, 1);
    Assert.assertEquals(0, records.size());
    Assert.assertEquals(0, transactions.size());
    journalImpl.forceMoveNextFile();
    // Reclaiming should still be able to reclaim a file if a transaction was ignored
    journalImpl.checkReclaimStatus();
    journalImpl.flush();
}
Also used : SimpleEncoding(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding) Test(org.junit.Test)

Aggregations

SimpleEncoding (org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding)47 Test (org.junit.Test)43 EncodingSupport (org.apache.activemq.artemis.core.journal.EncodingSupport)13 RecordInfo (org.apache.activemq.artemis.core.journal.RecordInfo)7 JournalImpl (org.apache.activemq.artemis.core.journal.impl.JournalImpl)6 SequentialFile (org.apache.activemq.artemis.core.io.SequentialFile)5 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 PreparedTransactionInfo (org.apache.activemq.artemis.core.journal.PreparedTransactionInfo)3 SequentialFileFactory (org.apache.activemq.artemis.core.io.SequentialFileFactory)2 AIOSequentialFileFactory (org.apache.activemq.artemis.core.io.aio.AIOSequentialFileFactory)2 Journal (org.apache.activemq.artemis.core.journal.Journal)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Pair (org.apache.activemq.artemis.api.core.Pair)1 JournalFile (org.apache.activemq.artemis.core.journal.impl.JournalFile)1 FakeSequentialFileFactory (org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.FakeSequentialFileFactory)1