use of org.apache.activemq.artemis.core.paging.PagedMessage in project activemq-artemis by apache.
the class Page method read.
private int read(StorageManager storage, ActiveMQBuffer fileBuffer, List<PagedMessage> messages) {
int readMessages = 0;
while (fileBuffer.readable()) {
final int position = fileBuffer.readerIndex();
byte byteRead = fileBuffer.readByte();
if (byteRead == Page.START_BYTE) {
if (fileBuffer.readerIndex() + DataConstants.SIZE_INT < fileBuffer.capacity()) {
int messageSize = fileBuffer.readInt();
int oldPos = fileBuffer.readerIndex();
if (fileBuffer.readerIndex() + messageSize < fileBuffer.capacity() && fileBuffer.getByte(oldPos + messageSize) == Page.END_BYTE) {
PagedMessage msg = new PagedMessageImpl(storageManager);
msg.decode(fileBuffer);
byte b = fileBuffer.readByte();
if (b != Page.END_BYTE) {
// constraint was already checked
throw new IllegalStateException("Internal error, it wasn't possible to locate END_BYTE " + b);
}
msg.initMessage(storage);
if (logger.isTraceEnabled()) {
logger.trace("Reading message " + msg + " on pageId=" + this.pageId + " for address=" + storeName);
}
readMessages++;
messages.add(msg);
} else {
markFileAsSuspect(file.getFileName(), position, messages.size());
break;
}
}
} else {
markFileAsSuspect(file.getFileName(), position, messages.size());
break;
}
}
return readMessages;
}
use of org.apache.activemq.artemis.core.paging.PagedMessage in project activemq-artemis by apache.
the class PageCursorProviderImpl method finishCleanup.
// Protected as a way to inject testing
protected void finishCleanup(ArrayList<Page> depagedPages) {
logger.tracef("this(%s) finishing cleanup on %s", this, depagedPages);
try {
for (Page depagedPage : depagedPages) {
PageCache cache;
PagedMessage[] pgdMessages;
synchronized (softCache) {
cache = softCache.get((long) depagedPage.getPageId());
}
if (logger.isTraceEnabled()) {
logger.trace("Removing pageNr=" + depagedPage.getPageId() + " from page-cache");
}
if (cache == null) {
// The page is not on cache any more
// We need to read the page-file before deleting it
// to make sure we remove any large-messages pending
storageManager.beforePageRead();
List<PagedMessage> pgdMessagesList = null;
try {
depagedPage.open();
pgdMessagesList = depagedPage.read(storageManager);
} finally {
try {
depagedPage.close(false);
} catch (Exception e) {
}
storageManager.afterPageRead();
}
depagedPage.close(false);
pgdMessages = pgdMessagesList.toArray(new PagedMessage[pgdMessagesList.size()]);
} else {
pgdMessages = cache.getMessages();
}
depagedPage.delete(pgdMessages);
onDeletePage(depagedPage);
synchronized (softCache) {
softCache.remove((long) depagedPage.getPageId());
}
}
} catch (Exception ex) {
ActiveMQServerLogger.LOGGER.problemCleaningPageAddress(ex, pagingStore.getAddress());
return;
}
}
use of org.apache.activemq.artemis.core.paging.PagedMessage 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());
}
}
}
use of org.apache.activemq.artemis.core.paging.PagedMessage in project activemq-artemis by apache.
the class PagingStoreImplTest method testDepageMultiplePages.
@Test
public void testDepageMultiplePages() throws Exception {
SequentialFileFactory factory = new FakeSequentialFileFactory();
SimpleString destination = new SimpleString("test");
PagingStoreFactory storeFactory = new FakeStoreFactory(factory);
PagingStoreImpl store = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE), getExecutorFactory().getExecutor(), true);
store.start();
Assert.assertEquals(0, store.getNumberOfPages());
store.startPaging();
Assert.assertEquals(1, store.getNumberOfPages());
List<ActiveMQBuffer> buffers = new ArrayList<>();
for (int i = 0; i < 10; i++) {
ActiveMQBuffer buffer = createRandomBuffer(i + 1L, 10);
buffers.add(buffer);
if (i == 5) {
store.forceAnotherPage();
}
Message msg = createMessage(i, store, destination, buffer);
final RoutingContextImpl ctx = new RoutingContextImpl(null);
Assert.assertTrue(store.page(msg, ctx.getTransaction(), ctx.getContextListing(store.getStoreName()), lock));
}
Assert.assertEquals(2, store.getNumberOfPages());
store.sync();
int sequence = 0;
for (int pageNr = 0; pageNr < 2; pageNr++) {
Page page = store.depage();
System.out.println("numberOfPages = " + store.getNumberOfPages());
page.open();
List<PagedMessage> msg = page.read(new NullStorageManager());
page.close();
Assert.assertEquals(5, msg.size());
for (int i = 0; i < 5; i++) {
Assert.assertEquals(sequence++, msg.get(i).getMessage().getMessageID());
ActiveMQTestBase.assertEqualsBuffers(18, buffers.get(pageNr * 5 + i), msg.get(i).getMessage().toCore().getBodyBuffer());
}
}
Assert.assertEquals(1, store.getNumberOfPages());
Assert.assertTrue(store.isPaging());
Message msg = createMessage(1, store, destination, buffers.get(0));
final RoutingContextImpl ctx = new RoutingContextImpl(null);
Assert.assertTrue(store.page(msg, ctx.getTransaction(), ctx.getContextListing(store.getStoreName()), lock));
Page newPage = store.depage();
newPage.open();
Assert.assertEquals(1, newPage.read(new NullStorageManager()).size());
newPage.delete(null);
Assert.assertEquals(1, store.getNumberOfPages());
Assert.assertTrue(store.isPaging());
Assert.assertNull(store.depage());
Assert.assertFalse(store.isPaging());
{
final RoutingContextImpl ctx2 = new RoutingContextImpl(null);
Assert.assertFalse(store.page(msg, ctx2.getTransaction(), ctx2.getContextListing(store.getStoreName()), lock));
}
store.startPaging();
{
final RoutingContextImpl ctx2 = new RoutingContextImpl(null);
Assert.assertTrue(store.page(msg, ctx2.getTransaction(), ctx2.getContextListing(store.getStoreName()), lock));
}
Page page = store.depage();
page.open();
List<PagedMessage> msgs = page.read(new NullStorageManager());
Assert.assertEquals(1, msgs.size());
Assert.assertEquals(1L, msgs.get(0).getMessage().getMessageID());
ActiveMQTestBase.assertEqualsBuffers(18, buffers.get(0), msgs.get(0).getMessage().toCore().getBodyBuffer());
Assert.assertEquals(1, store.getNumberOfPages());
Assert.assertTrue(store.isPaging());
Assert.assertNull(store.depage());
Assert.assertEquals(0, store.getNumberOfPages());
page.open();
}
use of org.apache.activemq.artemis.core.paging.PagedMessage in project activemq-artemis by apache.
the class PagingStoreImplTest method testOrderOnPaging.
@Test
public void testOrderOnPaging() throws Throwable {
clearDataRecreateServerDirs();
SequentialFileFactory factory = new NIOSequentialFileFactory(new File(getPageDir()), 1);
PagingStoreFactory storeFactory = new FakeStoreFactory(factory);
final int MAX_SIZE = 1024 * 10;
AddressSettings settings = new AddressSettings().setPageSizeBytes(MAX_SIZE).setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
final PagingStore store = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, new SimpleString("test"), settings, getExecutorFactory().getExecutor(), false);
store.start();
Assert.assertEquals(0, store.getNumberOfPages());
// Marked the store to be paged
store.startPaging();
final CountDownLatch producedLatch = new CountDownLatch(1);
Assert.assertEquals(1, store.getNumberOfPages());
final SimpleString destination = new SimpleString("test");
final long NUMBER_OF_MESSAGES = 100000;
final List<Throwable> errors = new ArrayList<>();
class WriterThread extends Thread {
WriterThread() {
super("PageWriter");
}
@Override
public void run() {
try {
for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
// 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(i, store, destination, createRandomBuffer(i, 1024));
msg.putLongProperty("count", i);
final RoutingContextImpl ctx2 = new RoutingContextImpl(null);
while (!store.page(msg, ctx2.getTransaction(), ctx2.getContextListing(store.getStoreName()), lock)) {
store.startPaging();
}
if (i == 0) {
producedLatch.countDown();
}
}
} catch (Throwable e) {
e.printStackTrace();
errors.add(e);
}
}
}
class ReaderThread extends Thread {
ReaderThread() {
super("PageReader");
}
@Override
public void run() {
try {
long msgsRead = 0;
while (msgsRead < NUMBER_OF_MESSAGES) {
Page page = store.depage();
if (page != null) {
page.open();
List<PagedMessage> messages = page.read(new NullStorageManager());
for (PagedMessage pgmsg : messages) {
Message msg = pgmsg.getMessage();
Assert.assertEquals(msgsRead++, msg.getMessageID());
Assert.assertEquals(msg.getMessageID(), msg.getLongProperty("count").longValue());
}
page.close();
page.delete(null);
} else {
System.out.println("Depaged!!!! numerOfMessages = " + msgsRead + " of " + NUMBER_OF_MESSAGES);
Thread.sleep(500);
}
}
} catch (Throwable e) {
e.printStackTrace();
errors.add(e);
}
}
}
WriterThread producerThread = new WriterThread();
producerThread.start();
ReaderThread consumer = new ReaderThread();
consumer.start();
producerThread.join();
consumer.join();
store.stop();
for (Throwable e : errors) {
throw e;
}
}
Aggregations