Search in sources :

Example 16 with SequentialFile

use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.

the class AlignedJournalImplTest method testReloadIncompleteTransaction.

@Test
public void testReloadIncompleteTransaction() throws Exception {
    final int JOURNAL_SIZE = 2000;
    setupAndLoadJournal(JOURNAL_SIZE, 1);
    Assert.assertEquals(2, factory.listFiles("tt").size());
    Assert.assertEquals(0, records.size());
    Assert.assertEquals(0, transactions.size());
    for (int i = 0; i < 10; i++) {
        journalImpl.appendAddRecordTransactional(1L, i, (byte) 0, new SimpleEncoding(1, (byte) 15));
    }
    for (int i = 10; i < 20; i++) {
        journalImpl.appendAddRecordTransactional(1L, i, (byte) 0, new SimpleEncoding(1, (byte) 15));
    }
    journalImpl.appendCommitRecord(1L, false);
    journalImpl.debugWait();
    SequentialFile file = factory.createSequentialFile("tt-1.tt");
    file.open();
    ByteBuffer buffer = ByteBuffer.allocate(100);
    // Messing up with the first record (removing the position)
    file.position(100);
    file.read(buffer);
    buffer.position(1);
    buffer.putInt(-1);
    buffer.rewind();
    // Messing up with the first record (changing the fileID, so Journal
    // reload will think the record came from a different journal usage)
    file.position(100);
    buffer.rewind();
    file.writeDirect(buffer, true);
    file.close();
    setupAndLoadJournal(JOURNAL_SIZE, 100);
    Assert.assertEquals(0, records.size());
    journalImpl.checkReclaimStatus();
    Assert.assertEquals(0, journalImpl.getDataFilesCount());
    Assert.assertEquals(2, factory.listFiles("tt").size());
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) SimpleEncoding(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 17 with SequentialFile

use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.

the class AlignedJournalImplTest method testPartiallyBrokenFile.

@Test
public void testPartiallyBrokenFile() throws Exception {
    final int JOURNAL_SIZE = 20000;
    setupAndLoadJournal(JOURNAL_SIZE, 100);
    Assert.assertEquals(2, factory.listFiles("tt").size());
    Assert.assertEquals(0, records.size());
    Assert.assertEquals(0, transactions.size());
    for (int i = 0; i < 20; i++) {
        journalImpl.appendAddRecordTransactional(1L, i, (byte) 0, new SimpleEncoding(1, (byte) 15));
        journalImpl.appendAddRecordTransactional(2L, i + 20L, (byte) 0, new SimpleEncoding(1, (byte) 15));
    }
    journalImpl.appendCommitRecord(1L, false);
    journalImpl.appendCommitRecord(2L, false);
    journalImpl.debugWait();
    SequentialFile file = factory.createSequentialFile("tt-1.tt");
    file.open();
    ByteBuffer buffer = ByteBuffer.allocate(100);
    // Messing up with the first record (removing the position)
    file.position(100);
    file.read(buffer);
    // jumping RecordType, FileId, TransactionID, RecordID, VariableSize,
    // RecordType, RecordBody (that we know it is 1 )
    buffer.position(1 + 4 + 8 + 8 + 4 + 1 + 1 + 1);
    int posCheckSize = buffer.position();
    Assert.assertEquals(JournalImpl.SIZE_ADD_RECORD_TX + 2, buffer.getInt());
    buffer.position(posCheckSize);
    buffer.putInt(-1);
    buffer.rewind();
    // Changing the check bufferSize, so reload will ignore this record
    file.position(100);
    file.writeDirect(buffer, true);
    file.close();
    setupAndLoadJournal(JOURNAL_SIZE, 100);
    Assert.assertEquals(20, records.size());
    journalImpl.checkReclaimStatus();
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) SimpleEncoding(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 18 with SequentialFile

use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.

the class AlignedJournalImplTest method testBasicAlignment.

// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
// This test just validates basic alignment on the FakeSequentialFile itself
@Test
public void testBasicAlignment() throws Exception {
    FakeSequentialFileFactory factory = new FakeSequentialFileFactory(200, true);
    SequentialFile file = factory.createSequentialFile("test1");
    file.open();
    try {
        ByteBuffer buffer = ByteBuffer.allocateDirect(200);
        for (int i = 0; i < 200; i++) {
            buffer.put(i, (byte) 1);
        }
        file.writeDirect(buffer, true);
        buffer = ByteBuffer.allocate(400);
        for (int i = 0; i < 400; i++) {
            buffer.put(i, (byte) 2);
        }
        file.writeDirect(buffer, true);
        buffer = ByteBuffer.allocate(600);
        file.position(0);
        file.read(buffer);
        for (int i = 0; i < 200; i++) {
            Assert.assertEquals((byte) 1, buffer.get(i));
        }
        for (int i = 201; i < 600; i++) {
            Assert.assertEquals("Position " + i, (byte) 2, buffer.get(i));
        }
    } catch (Exception ignored) {
    }
}
Also used : FakeSequentialFileFactory(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.FakeSequentialFileFactory) SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 19 with SequentialFile

use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.

the class AlignedJournalImplTest method testReloadInvalidPrepared.

@Test
public void testReloadInvalidPrepared() throws Exception {
    final int JOURNAL_SIZE = 3000;
    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(50, (byte) 1));
    }
    journalImpl.appendPrepareRecord(1L, new SimpleEncoding(13, (byte) 0), false);
    setupAndLoadJournal(JOURNAL_SIZE, 100);
    Assert.assertEquals(0, records.size());
    Assert.assertEquals(1, transactions.size());
    SequentialFile file = factory.createSequentialFile("tt-1.tt");
    file.open();
    ByteBuffer buffer = ByteBuffer.allocate(100);
    // Messing up with the first record (removing the position)
    file.position(100);
    file.read(buffer);
    buffer.position(1);
    buffer.putInt(-1);
    buffer.rewind();
    // Messing up with the first record (changing the fileID, so Journal
    // reload will think the record came from a different journal usage)
    file.position(100);
    file.writeDirect(buffer, true);
    file.close();
    setupAndLoadJournal(JOURNAL_SIZE, 100);
    Assert.assertEquals(0, records.size());
    Assert.assertEquals(0, transactions.size());
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) SimpleEncoding(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 20 with SequentialFile

use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.

the class NIOSequentialFileFactoryTest method testInterrupts.

@Test
public void testInterrupts() throws Throwable {
    final EncodingSupport fakeEncoding = new EncodingSupport() {

        @Override
        public int getEncodeSize() {
            return 10;
        }

        @Override
        public void encode(ActiveMQBuffer buffer) {
            buffer.writeBytes(new byte[10]);
        }

        @Override
        public void decode(ActiveMQBuffer buffer) {
        }
    };
    final AtomicInteger calls = new AtomicInteger(0);
    final NIOSequentialFileFactory factory = new NIOSequentialFileFactory(new File(getTestDir()), new IOCriticalErrorListener() {

        @Override
        public void onIOException(Throwable code, String message, SequentialFile file) {
            new Exception("shutdown").printStackTrace();
            calls.incrementAndGet();
        }
    }, 1);
    Thread threadOpen = new Thread() {

        @Override
        public void run() {
            try {
                Thread.currentThread().interrupt();
                SequentialFile file = factory.createSequentialFile("file.txt");
                file.open();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    threadOpen.start();
    threadOpen.join();
    Thread threadClose = new Thread() {

        @Override
        public void run() {
            try {
                SequentialFile file = factory.createSequentialFile("file.txt");
                file.open();
                file.write(fakeEncoding, true);
                Thread.currentThread().interrupt();
                file.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    threadClose.start();
    threadClose.join();
    Thread threadWrite = new Thread() {

        @Override
        public void run() {
            try {
                SequentialFile file = factory.createSequentialFile("file.txt");
                file.open();
                Thread.currentThread().interrupt();
                file.write(fakeEncoding, true);
                file.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    threadWrite.start();
    threadWrite.join();
    Thread threadFill = new Thread() {

        @Override
        public void run() {
            try {
                SequentialFile file = factory.createSequentialFile("file.txt");
                file.open();
                Thread.currentThread().interrupt();
                file.fill(1024);
                file.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    threadFill.start();
    threadFill.join();
    Thread threadWriteDirect = new Thread() {

        @Override
        public void run() {
            try {
                SequentialFile file = factory.createSequentialFile("file.txt");
                file.open();
                ByteBuffer buffer = ByteBuffer.allocate(10);
                buffer.put(new byte[10]);
                Thread.currentThread().interrupt();
                file.writeDirect(buffer, true);
                file.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    threadWriteDirect.start();
    threadWriteDirect.join();
    Thread threadRead = new Thread() {

        @Override
        public void run() {
            try {
                SequentialFile file = factory.createSequentialFile("file.txt");
                file.open();
                file.write(fakeEncoding, true);
                file.position(0);
                ByteBuffer readBytes = ByteBuffer.allocate(fakeEncoding.getEncodeSize());
                Thread.currentThread().interrupt();
                file.read(readBytes);
                file.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    threadRead.start();
    threadRead.join();
    // An interrupt exception shouldn't issue a shutdown
    Assert.assertEquals(0, calls.get());
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) IOCriticalErrorListener(org.apache.activemq.artemis.core.io.IOCriticalErrorListener) ByteBuffer(java.nio.ByteBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) File(java.io.File) EncodingSupport(org.apache.activemq.artemis.core.journal.EncodingSupport) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) NIOSequentialFileFactory(org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory) Test(org.junit.Test)

Aggregations

SequentialFile (org.apache.activemq.artemis.core.io.SequentialFile)53 Test (org.junit.Test)21 ByteBuffer (java.nio.ByteBuffer)15 ArrayList (java.util.ArrayList)10 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)9 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)7 Pair (org.apache.activemq.artemis.api.core.Pair)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 SequentialFileFactory (org.apache.activemq.artemis.core.io.SequentialFileFactory)4 NIOSequentialFileFactory (org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory)4 Page (org.apache.activemq.artemis.core.paging.impl.Page)4 JDBCSequentialFile (org.apache.activemq.artemis.jdbc.store.file.JDBCSequentialFile)4 SimpleEncoding (org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding)4 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)3 IOCriticalErrorListener (org.apache.activemq.artemis.core.io.IOCriticalErrorListener)3 PagedMessage (org.apache.activemq.artemis.core.paging.PagedMessage)3 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2