Search in sources :

Example 91 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class ActiveMQBufferInputStreamTest method testReadBytes.

@Test
public void testReadBytes() throws Exception {
    byte[] bytes = new byte[10 * 1024];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = getSamplebyte(i);
    }
    ActiveMQBuffer buffer = ActiveMQBuffers.wrappedBuffer(bytes);
    ActiveMQBufferInputStream is = new ActiveMQBufferInputStream(buffer);
    // First read byte per byte
    for (int i = 0; i < 1024; i++) {
        assertEquals(getSamplebyte(i), is.read());
    }
    // Second, read in chunks
    for (int i = 1; i < 10; i++) {
        bytes = new byte[1024];
        is.read(bytes);
        for (int j = 0; j < bytes.length; j++) {
            assertEquals(getSamplebyte(i * 1024 + j), bytes[j]);
        }
    }
    assertEquals(-1, is.read());
    bytes = new byte[1024];
    int sizeRead = is.read(bytes);
    assertEquals(-1, sizeRead);
    is.close();
}
Also used : ActiveMQBufferInputStream(org.apache.activemq.artemis.utils.ActiveMQBufferInputStream) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 92 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class LargeMessageBufferTest method testReadDataOverCached.

@Test
public void testReadDataOverCached() throws Exception {
    clearDataRecreateServerDirs();
    ActiveMQBuffer dynamic = ActiveMQBuffers.dynamicBuffer(1);
    String str1 = RandomUtil.randomString();
    String str2 = RandomUtil.randomString();
    double d1 = RandomUtil.randomDouble();
    float f1 = RandomUtil.randomFloat();
    dynamic.writeUTF(str1);
    dynamic.writeString(str2);
    dynamic.writeDouble(d1);
    dynamic.writeFloat(f1);
    LargeMessageControllerImpl readBuffer = splitBuffer(3, dynamic.toByteBuffer().array(), getTestFile());
    Assert.assertEquals(str1, readBuffer.readUTF());
    Assert.assertEquals(str2, readBuffer.readString());
    Assert.assertEquals(d1, readBuffer.readDouble(), 0.00000001);
    Assert.assertEquals(f1, readBuffer.readFloat(), 0.000001);
    readBuffer.readerIndex(0);
    Assert.assertEquals(str1, readBuffer.readUTF());
    Assert.assertEquals(str2, readBuffer.readString());
    Assert.assertEquals(d1, readBuffer.readDouble(), 0.00000001);
    Assert.assertEquals(f1, readBuffer.readFloat(), 0.000001);
    readBuffer.close();
}
Also used : LargeMessageControllerImpl(org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 93 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class TimedBufferTest method testTimeOnTimedBuffer.

@Test
public void testTimeOnTimedBuffer() throws Exception {
    final ReusableLatch latchFlushed = new ReusableLatch(0);
    final AtomicInteger flushes = new AtomicInteger(0);
    class TestObserver implements TimedBufferObserver {

        @Override
        public void flushBuffer(final ByteBuffer buffer, final boolean sync, final List<IOCallback> callbacks) {
            for (IOCallback callback : callbacks) {
                callback.done();
            }
        }

        /* (non-Javadoc)
          * @see org.apache.activemq.artemis.utils.timedbuffer.TimedBufferObserver#newBuffer(int, int)
          */
        @Override
        public ByteBuffer newBuffer(final int minSize, final int maxSize) {
            return ByteBuffer.allocate(maxSize);
        }

        @Override
        public int getRemainingBytes() {
            return 1024 * 1024;
        }
    }
    TimedBuffer timedBuffer = new TimedBuffer(null, 100, TimedBufferTest.ONE_SECOND_IN_NANOS / 2, false);
    timedBuffer.start();
    TestObserver observer = new TestObserver();
    timedBuffer.setObserver(observer);
    int x = 0;
    byte[] bytes = new byte[10];
    for (int j = 0; j < 10; j++) {
        bytes[j] = ActiveMQTestBase.getSamplebyte(x++);
    }
    ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(bytes);
    IOCallback callback = new IOCallback() {

        @Override
        public void done() {
            System.out.println("done");
            latchFlushed.countDown();
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
        }
    };
    try {
        latchFlushed.setCount(2);
        // simulating a low load period
        timedBuffer.addBytes(buff, true, callback);
        Thread.sleep(1000);
        timedBuffer.addBytes(buff, true, callback);
        Assert.assertTrue(latchFlushed.await(5, TimeUnit.SECONDS));
        latchFlushed.setCount(5);
        flushes.set(0);
        // Sending like crazy... still some wait (1 millisecond) between each send..
        long time = System.currentTimeMillis();
        for (int i = 0; i < 5; i++) {
            timedBuffer.addBytes(buff, true, callback);
            Thread.sleep(1);
        }
        Assert.assertTrue(latchFlushed.await(5, TimeUnit.SECONDS));
        // The purpose of the timed buffer is to batch writes up to a millisecond.. or up to the size of the buffer.
        Assert.assertTrue("Timed Buffer is not batching accordingly, it was expected to take at least 500 seconds batching multiple writes while it took " + (System.currentTimeMillis() - time) + " milliseconds", System.currentTimeMillis() - time >= 450);
        // ^^ there are some discounts that can happen inside the timed buffer that are still considered valid (like discounting the time it took to perform the operation itself
        // for that reason the test has been failing (before this commit) at 499 or 480 milliseconds. So, I'm using a reasonable number close to 500 milliseconds that would still be valid for the test
        // it should be in fact only writing once..
        // i will set for 3 just in case there's a GC or anything else happening on the test
        Assert.assertTrue("Too many writes were called", flushes.get() <= 3);
    } finally {
        timedBuffer.stop();
    }
}
Also used : TimedBuffer(org.apache.activemq.artemis.core.io.buffer.TimedBuffer) ByteBuffer(java.nio.ByteBuffer) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) TimedBufferObserver(org.apache.activemq.artemis.core.io.buffer.TimedBufferObserver) ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 94 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class TimedBufferTest method testTimingAndFlush.

@Test
public void testTimingAndFlush() throws Exception {
    final ArrayList<ByteBuffer> buffers = new ArrayList<>();
    final AtomicInteger flushTimes = new AtomicInteger(0);
    class TestObserver implements TimedBufferObserver {

        @Override
        public void flushBuffer(final ByteBuffer buffer, final boolean sync, final List<IOCallback> callbacks) {
            buffers.add(buffer);
            flushTimes.incrementAndGet();
        }

        /* (non-Javadoc)
          * @see org.apache.activemq.artemis.utils.timedbuffer.TimedBufferObserver#newBuffer(int, int)
          */
        @Override
        public ByteBuffer newBuffer(final int minSize, final int maxSize) {
            return ByteBuffer.allocate(maxSize);
        }

        @Override
        public int getRemainingBytes() {
            return 1024 * 1024;
        }
    }
    TimedBuffer timedBuffer = new TimedBuffer(null, 100, TimedBufferTest.ONE_SECOND_IN_NANOS / 10, false);
    timedBuffer.start();
    try {
        timedBuffer.setObserver(new TestObserver());
        int x = 0;
        byte[] bytes = new byte[10];
        for (int j = 0; j < 10; j++) {
            bytes[j] = ActiveMQTestBase.getSamplebyte(x++);
        }
        ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(bytes);
        timedBuffer.checkSize(10);
        timedBuffer.addBytes(buff, false, dummyCallback);
        Thread.sleep(200);
        Assert.assertEquals(0, flushTimes.get());
        bytes = new byte[10];
        for (int j = 0; j < 10; j++) {
            bytes[j] = ActiveMQTestBase.getSamplebyte(x++);
        }
        buff = ActiveMQBuffers.wrappedBuffer(bytes);
        timedBuffer.checkSize(10);
        timedBuffer.addBytes(buff, true, dummyCallback);
        Thread.sleep(500);
        Assert.assertEquals(1, flushTimes.get());
        ByteBuffer flushedBuffer = buffers.get(0);
        Assert.assertEquals(20, flushedBuffer.limit());
        Assert.assertEquals(20, flushedBuffer.capacity());
        flushedBuffer.rewind();
        for (int i = 0; i < 20; i++) {
            Assert.assertEquals(ActiveMQTestBase.getSamplebyte(i), flushedBuffer.get());
        }
    } finally {
        timedBuffer.stop();
    }
}
Also used : TimedBufferObserver(org.apache.activemq.artemis.core.io.buffer.TimedBufferObserver) TimedBuffer(org.apache.activemq.artemis.core.io.buffer.TimedBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ByteBuffer(java.nio.ByteBuffer) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 95 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class PagingStoreImplTest method testDepageOnCurrentPage.

@Test
public void testDepageOnCurrentPage() throws Exception {
    SequentialFileFactory factory = new FakeSequentialFileFactory();
    SimpleString destination = new SimpleString("test");
    PagingStoreFactory storeFactory = new FakeStoreFactory(factory);
    PagingStoreImpl storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE), getExecutorFactory().getExecutor(), true);
    storeImpl.start();
    Assert.assertEquals(0, storeImpl.getNumberOfPages());
    storeImpl.startPaging();
    List<ActiveMQBuffer> buffers = new ArrayList<>();
    int numMessages = 10;
    for (int i = 0; i < numMessages; i++) {
        ActiveMQBuffer buffer = createRandomBuffer(i + 1L, 10);
        buffers.add(buffer);
        Message msg = createMessage(i, storeImpl, destination, buffer);
        final RoutingContextImpl ctx = new RoutingContextImpl(null);
        Assert.assertTrue(storeImpl.page(msg, ctx.getTransaction(), ctx.getContextListing(storeImpl.getStoreName()), lock));
    }
    Assert.assertEquals(1, storeImpl.getNumberOfPages());
    storeImpl.sync();
    Page page = storeImpl.depage();
    page.open();
    List<PagedMessage> msg = page.read(new NullStorageManager());
    Assert.assertEquals(numMessages, msg.size());
    Assert.assertEquals(1, storeImpl.getNumberOfPages());
    page = storeImpl.depage();
    Assert.assertNull(page);
    Assert.assertEquals(0, storeImpl.getNumberOfPages());
    for (int i = 0; i < numMessages; i++) {
        ActiveMQBuffer horn1 = buffers.get(i);
        ActiveMQBuffer horn2 = msg.get(i).getMessage().toCore().getBodyBuffer();
        horn1.resetReaderIndex();
        horn2.resetReaderIndex();
        for (int j = 0; j < horn1.writerIndex(); j++) {
            Assert.assertEquals(horn1.readByte(), horn2.readByte());
        }
    }
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) 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) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Page(org.apache.activemq.artemis.core.paging.impl.Page) FakeSequentialFileFactory(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.FakeSequentialFileFactory) NIOSequentialFileFactory(org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory) SequentialFileFactory(org.apache.activemq.artemis.core.io.SequentialFileFactory) RoutingContextImpl(org.apache.activemq.artemis.core.server.impl.RoutingContextImpl) FakeSequentialFileFactory(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.FakeSequentialFileFactory) PagingStoreImpl(org.apache.activemq.artemis.core.paging.impl.PagingStoreImpl) NullStorageManager(org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager) PagingStoreFactory(org.apache.activemq.artemis.core.paging.PagingStoreFactory) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Aggregations

ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)150 Test (org.junit.Test)81 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)49 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)44 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)39 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)38 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)35 ByteBuffer (java.nio.ByteBuffer)34 Configuration (org.apache.activemq.artemis.core.config.Configuration)34 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)27 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)25 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)25 ArrayList (java.util.ArrayList)21 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)21 Queue (org.apache.activemq.artemis.core.server.Queue)18 HashMap (java.util.HashMap)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)15 RecordInfo (org.apache.activemq.artemis.core.journal.RecordInfo)15 CoreMessage (org.apache.activemq.artemis.core.message.impl.CoreMessage)14