Search in sources :

Example 11 with SequentialFile

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

the class PagingStoreImplTest method testConcurrentPaging.

protected void testConcurrentPaging(final SequentialFileFactory factory, final int numberOfThreads) throws Exception {
    PagingStoreFactory storeFactory = new FakeStoreFactory(factory);
    final int MAX_SIZE = 1024 * 10;
    final AtomicLong messageIdGenerator = new AtomicLong(0);
    final AtomicInteger aliveProducers = new AtomicInteger(numberOfThreads);
    final CountDownLatch latchStart = new CountDownLatch(numberOfThreads);
    final ConcurrentHashMap<Long, Message> buffers = new ConcurrentHashMap<>();
    final ArrayList<Page> readPages = new ArrayList<>();
    AddressSettings settings = new AddressSettings().setPageSizeBytes(MAX_SIZE).setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
    final PagingStore storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, new SimpleString("test"), settings, getExecutorFactory().getExecutor(), true);
    storeImpl.start();
    Assert.assertEquals(0, storeImpl.getNumberOfPages());
    // Marked the store to be paged
    storeImpl.startPaging();
    Assert.assertEquals(1, storeImpl.getNumberOfPages());
    final SimpleString destination = new SimpleString("test");
    class WriterThread extends Thread {

        Exception e;

        @Override
        public void run() {
            try {
                boolean firstTime = true;
                while (true) {
                    long id = messageIdGenerator.incrementAndGet();
                    // Each thread will Keep paging until all the messages are depaged.
                    // This is possible because the depage thread is not actually reading the pages.
                    // Just using the internal API to remove it from the page file system
                    Message msg = createMessage(id, storeImpl, destination, createRandomBuffer(id, 5));
                    final RoutingContextImpl ctx2 = new RoutingContextImpl(null);
                    if (storeImpl.page(msg, ctx2.getTransaction(), ctx2.getContextListing(storeImpl.getStoreName()), lock)) {
                        buffers.put(id, msg);
                    } else {
                        break;
                    }
                    if (firstTime) {
                        // We have at least one data paged. So, we can start depaging now
                        latchStart.countDown();
                        firstTime = false;
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
                this.e = e1;
            } finally {
                aliveProducers.decrementAndGet();
            }
        }
    }
    final class ReaderThread extends Thread {

        Exception e;

        @Override
        public void run() {
            try {
                // Wait every producer to produce at least one message
                ActiveMQTestBase.waitForLatch(latchStart);
                while (aliveProducers.get() > 0) {
                    Page page = storeImpl.depage();
                    if (page != null) {
                        readPages.add(page);
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
                this.e = e1;
            }
        }
    }
    WriterThread[] producerThread = new WriterThread[numberOfThreads];
    for (int i = 0; i < numberOfThreads; i++) {
        producerThread[i] = new WriterThread();
        producerThread[i].start();
    }
    ReaderThread consumer = new ReaderThread();
    consumer.start();
    for (int i = 0; i < numberOfThreads; i++) {
        producerThread[i].join();
        if (producerThread[i].e != null) {
            throw producerThread[i].e;
        }
    }
    consumer.join();
    if (consumer.e != null) {
        throw consumer.e;
    }
    final ConcurrentMap<Long, Message> buffers2 = new ConcurrentHashMap<>();
    for (Page page : readPages) {
        page.open();
        List<PagedMessage> msgs = page.read(new NullStorageManager());
        page.close();
        for (PagedMessage msg : msgs) {
            long id = msg.getMessage().toCore().getBodyBuffer().readLong();
            msg.getMessage().toCore().getBodyBuffer().resetReaderIndex();
            Message msgWritten = buffers.remove(id);
            buffers2.put(id, msg.getMessage());
            Assert.assertNotNull(msgWritten);
            Assert.assertEquals(msg.getMessage().getAddress(), msgWritten.getAddress());
            ActiveMQTestBase.assertEqualsBuffers(10, msgWritten.toCore().getBodyBuffer(), msg.getMessage().toCore().getBodyBuffer());
        }
    }
    Assert.assertEquals(0, buffers.size());
    List<String> files = factory.listFiles("page");
    Assert.assertTrue(files.size() != 0);
    for (String file : files) {
        SequentialFile fileTmp = factory.createSequentialFile(file);
        fileTmp.open();
        Assert.assertTrue("The page file size (" + fileTmp.size() + ") shouldn't be > " + MAX_SIZE, fileTmp.size() <= MAX_SIZE);
        fileTmp.close();
    }
    PagingStore storeImpl2 = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, new SimpleString("test"), settings, getExecutorFactory().getExecutor(), true);
    storeImpl2.start();
    int numberOfPages = storeImpl2.getNumberOfPages();
    Assert.assertTrue(numberOfPages != 0);
    storeImpl2.startPaging();
    storeImpl2.startPaging();
    Assert.assertEquals(numberOfPages, storeImpl2.getNumberOfPages());
    long lastMessageId = messageIdGenerator.incrementAndGet();
    Message lastMsg = createMessage(lastMessageId, storeImpl, destination, createRandomBuffer(lastMessageId, 5));
    storeImpl2.forceAnotherPage();
    final RoutingContextImpl ctx = new RoutingContextImpl(null);
    storeImpl2.page(lastMsg, ctx.getTransaction(), ctx.getContextListing(storeImpl2.getStoreName()), lock);
    buffers2.put(lastMessageId, lastMsg);
    Page lastPage = null;
    while (true) {
        Page page = storeImpl2.depage();
        if (page == null) {
            break;
        }
        lastPage = page;
        page.open();
        List<PagedMessage> msgs = page.read(new NullStorageManager());
        page.close();
        for (PagedMessage msg : msgs) {
            long id = msg.getMessage().toCore().getBodyBuffer().readLong();
            Message msgWritten = buffers2.remove(id);
            Assert.assertNotNull(msgWritten);
            Assert.assertEquals(msg.getMessage().getAddress(), msgWritten.getAddress());
            ActiveMQTestBase.assertEqualsByteArrays(msgWritten.toCore().getBodyBuffer().writerIndex(), msgWritten.toCore().getBodyBuffer().toByteBuffer().array(), msg.getMessage().toCore().getBodyBuffer().toByteBuffer().array());
        }
    }
    lastPage.open();
    List<PagedMessage> lastMessages = lastPage.read(new NullStorageManager());
    lastPage.close();
    Assert.assertEquals(1, lastMessages.size());
    lastMessages.get(0).getMessage().toCore().getBodyBuffer().resetReaderIndex();
    Assert.assertEquals(lastMessages.get(0).getMessage().toCore().getBodyBuffer().readLong(), lastMessageId);
    Assert.assertEquals(0, buffers2.size());
    Assert.assertEquals(0, storeImpl.getAddressSize());
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) Message(org.apache.activemq.artemis.api.core.Message) ArrayList(java.util.ArrayList) Page(org.apache.activemq.artemis.core.paging.impl.Page) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) RoutingContextImpl(org.apache.activemq.artemis.core.server.impl.RoutingContextImpl) PagingStoreImpl(org.apache.activemq.artemis.core.paging.impl.PagingStoreImpl) PagingStoreFactory(org.apache.activemq.artemis.core.paging.PagingStoreFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) NullStorageManager(org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) PagingStore(org.apache.activemq.artemis.core.paging.PagingStore)

Example 12 with SequentialFile

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

the class SequentialFileFactoryTestBase method testFill.

@Test
public void testFill() throws Exception {
    SequentialFile sf = factory.createSequentialFile("fill.amq");
    sf.open();
    try {
        checkFill(factory, sf, 2048);
        checkFill(factory, sf, 512);
        checkFill(factory, sf, 512 * 4);
    } finally {
        sf.close();
    }
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) Test(org.junit.Test)

Example 13 with SequentialFile

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

the class SequentialFileFactoryTestBase method testDelete.

@Test
public void testDelete() throws Exception {
    SequentialFile sf = factory.createSequentialFile("delete-me.amq");
    sf.open();
    SequentialFile sf2 = factory.createSequentialFile("delete-me2.amq");
    sf2.open();
    List<String> fileNames = factory.listFiles("amq");
    Assert.assertEquals(2, fileNames.size());
    Assert.assertTrue(fileNames.contains("delete-me.amq"));
    Assert.assertTrue(fileNames.contains("delete-me2.amq"));
    sf.delete();
    fileNames = factory.listFiles("amq");
    Assert.assertEquals(1, fileNames.size());
    Assert.assertTrue(fileNames.contains("delete-me2.amq"));
    sf2.close();
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) Test(org.junit.Test)

Example 14 with SequentialFile

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

the class SequentialFileFactoryTestBase method testPosition.

@Test
public void testPosition() throws Exception {
    SequentialFile sf = factory.createSequentialFile("position.amq");
    sf.open();
    try {
        sf.fill(3 * 512);
        String s1 = "orange";
        byte[] bytes1 = s1.getBytes(StandardCharsets.UTF_8);
        byte[] bytes2 = s1.getBytes(StandardCharsets.UTF_8);
        String s3 = "lemon";
        byte[] bytes3 = s3.getBytes(StandardCharsets.UTF_8);
        long initialPos = sf.position();
        sf.write(wrapBuffer(bytes1), true);
        long bytesWritten = sf.position() - initialPos;
        Assert.assertEquals(calculateRecordSize(bytes1.length, factory.getAlignment()), bytesWritten);
        initialPos = sf.position();
        sf.write(wrapBuffer(bytes2), true);
        bytesWritten = sf.position() - initialPos;
        Assert.assertEquals(calculateRecordSize(bytes2.length, factory.getAlignment()), bytesWritten);
        initialPos = sf.position();
        sf.write(wrapBuffer(bytes3), true);
        bytesWritten = sf.position() - initialPos;
        Assert.assertEquals(calculateRecordSize(bytes3.length, factory.getAlignment()), bytesWritten);
        byte[] rbytes1 = new byte[bytes1.length];
        byte[] rbytes2 = new byte[bytes2.length];
        byte[] rbytes3 = new byte[bytes3.length];
        ByteBuffer rb1 = factory.newBuffer(rbytes1.length);
        ByteBuffer rb2 = factory.newBuffer(rbytes2.length);
        ByteBuffer rb3 = factory.newBuffer(rbytes3.length);
        sf.position(calculateRecordSize(bytes1.length, factory.getAlignment()) + calculateRecordSize(bytes2.length, factory.getAlignment()));
        int bytesRead = sf.read(rb3);
        Assert.assertEquals(rb3.limit(), bytesRead);
        rb3.rewind();
        rb3.get(rbytes3);
        ActiveMQTestBase.assertEqualsByteArrays(bytes3, rbytes3);
        sf.position(rb1.limit());
        bytesRead = sf.read(rb2);
        Assert.assertEquals(rb2.limit(), bytesRead);
        rb2.get(rbytes2);
        ActiveMQTestBase.assertEqualsByteArrays(bytes2, rbytes2);
        sf.position(0);
        bytesRead = sf.read(rb1);
        Assert.assertEquals(rb1.limit(), bytesRead);
        rb1.get(rbytes1);
        ActiveMQTestBase.assertEqualsByteArrays(bytes1, rbytes1);
    } finally {
        try {
            sf.close();
        } catch (Exception ignored) {
        }
    }
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 15 with SequentialFile

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

the class SequentialFileFactoryTestBase method testRename.

@Test
public void testRename() throws Exception {
    SequentialFile sf = factory.createSequentialFile("test1.amq");
    sf.open();
    List<String> fileNames = factory.listFiles("amq");
    Assert.assertEquals(1, fileNames.size());
    Assert.assertTrue(fileNames.contains("test1.amq"));
    sf.renameTo("test1.cmp");
    fileNames = factory.listFiles("cmp");
    Assert.assertEquals(1, fileNames.size());
    Assert.assertTrue(fileNames.contains("test1.cmp"));
    sf.delete();
    fileNames = factory.listFiles("amq");
    Assert.assertEquals(0, fileNames.size());
    fileNames = factory.listFiles("cmp");
    Assert.assertEquals(0, fileNames.size());
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) 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