use of org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl in project activemq-artemis by apache.
the class XmlDataExporter method printPagedMessagesAsXML.
/**
* Reads from the page files and prints messages as it finds them (making sure to check acks and transactions
* from the journal).
*/
private void printPagedMessagesAsXML() {
try {
pagingmanager.start();
SimpleString[] stores = pagingmanager.getStoreNames();
for (SimpleString store : stores) {
PagingStore pageStore = pagingmanager.getPageStore(store);
if (pageStore != null) {
File folder = pageStore.getFolder();
ActiveMQServerLogger.LOGGER.debug("Reading page store " + store + " folder = " + folder);
int pageId = (int) pageStore.getFirstPage();
for (int i = 0; i < pageStore.getNumberOfPages(); i++) {
ActiveMQServerLogger.LOGGER.debug("Reading page " + pageId);
Page page = pageStore.createPage(pageId);
page.open();
List<PagedMessage> messages = page.read(storageManager);
page.close();
int messageId = 0;
for (PagedMessage message : messages) {
message.initMessage(storageManager);
long[] queueIDs = message.getQueueIDs();
List<String> queueNames = new ArrayList<>();
for (long queueID : queueIDs) {
PagePosition posCheck = new PagePositionImpl(pageId, messageId);
boolean acked = false;
Set<PagePosition> positions = cursorRecords.get(queueID);
if (positions != null) {
acked = positions.contains(posCheck);
}
if (!acked) {
PersistentQueueBindingEncoding queueBinding = queueBindings.get(queueID);
if (queueBinding != null) {
SimpleString queueName = queueBinding.getQueueName();
queueNames.add(queueName.toString());
}
}
}
if (queueNames.size() > 0 && (message.getTransactionID() == -1 || pgTXs.contains(message.getTransactionID()))) {
printSingleMessageAsXML(message.getMessage().toCore(), queueNames);
}
messageId++;
}
pageId++;
}
} else {
ActiveMQServerLogger.LOGGER.debug("Page store was null");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl in project activemq-artemis by apache.
the class PagingLeakTest method testValidateLeak.
@Test
@BMRules(rules = { @BMRule(name = "newPosition", targetClass = "org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl", targetMethod = "<init>()", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.PagingLeakTest.newPosition()"), @BMRule(name = "finalPosition", targetClass = "org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl", targetMethod = "finalize", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.PagingLeakTest.deletePosition()") })
public void testValidateLeak() throws Throwable {
System.out.println("location::" + getBindingsDir());
List<PagePositionImpl> positions = new ArrayList<>();
for (int i = 0; i < 300; i++) {
positions.add(new PagePositionImpl(3, 3));
}
long timeout = System.currentTimeMillis() + 5000;
while (pagePosInstances.get() != 300 && timeout > System.currentTimeMillis()) {
forceGC();
}
// This is just to validate the rules are correctly applied on byteman
assertEquals("You have changed something on PagePositionImpl in such way that these byteman rules are no longer working", 300, pagePosInstances.get());
positions.clear();
Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisfied() throws Exception {
forceGC();
return pagePosInstances.get() == 0;
}
}, 5000, 100);
// This is just to validate the rules are correctly applied on byteman
assertEquals("You have changed something on PagePositionImpl in such way that these byteman rules are no longer working", 0, pagePosInstances.get());
final ArrayList<Exception> errors = new ArrayList<>();
// A backup that will be waiting to be activated
Configuration config = createDefaultNettyConfig();
config.setJournalBufferTimeout_AIO(10).setJournalBufferTimeout_NIO(10);
final ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, true));
server.start();
AddressSettings settings = new AddressSettings().setPageSizeBytes(2 * 1024).setMaxSizeBytes(10 * 1024).setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
server.getAddressSettingsRepository().addMatch("#", settings);
final SimpleString address = new SimpleString("pgdAddress");
class Consumer extends Thread {
final ServerLocator locator;
final ClientSessionFactory sf;
final ClientSession session;
final ClientConsumer consumer;
final int sleepTime;
final int maxConsumed;
Consumer(int sleepTime, String suffix, int maxConsumed) throws Exception {
server.createQueue(address, RoutingType.MULTICAST, address.concat(suffix), null, true, false);
this.sleepTime = sleepTime;
locator = createInVMLocator(0);
sf = locator.createSessionFactory();
session = sf.createSession(true, true);
consumer = session.createConsumer(address.concat(suffix));
this.maxConsumed = maxConsumed;
}
@Override
public void run() {
try {
session.start();
long lastTime = System.currentTimeMillis();
for (long i = 0; i < maxConsumed; i++) {
ClientMessage msg = consumer.receive(5000);
if (msg == null) {
errors.add(new Exception("didn't receive a message"));
return;
}
msg.acknowledge();
if (sleepTime > 0) {
Thread.sleep(sleepTime);
}
if (i % 1000 == 0) {
System.out.println("Consumed " + i + " events in " + (System.currentTimeMillis() - lastTime));
lastTime = System.currentTimeMillis();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
int numberOfMessages = 500;
Consumer consumer1 = new Consumer(10, "-1", 150);
Consumer consumer2 = new Consumer(0, "-2", numberOfMessages);
final ServerLocator locator = createInVMLocator(0);
final ClientSessionFactory sf = locator.createSessionFactory();
final ClientSession session = sf.createSession(true, true);
final ClientProducer producer = session.createProducer(address);
byte[] b = new byte[1024];
for (long i = 0; i < numberOfMessages; i++) {
ClientMessage msg = session.createMessage(true);
msg.getBodyBuffer().writeBytes(b);
producer.send(msg);
if (i == 100) {
System.out.println("Starting consumers!!!");
consumer1.start();
consumer2.start();
}
if (i % 250 == 0) {
validateInstances();
}
}
System.out.println("Sent " + numberOfMessages);
consumer1.join();
consumer2.join();
validateInstances();
Throwable elast = null;
for (Throwable e : errors) {
e.printStackTrace();
elast = e;
}
if (elast != null) {
throw elast;
}
}
use of org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl in project activemq-artemis by apache.
the class PagingTest method testRestartWithComplete.
// The pages are complete, and this is simulating a scenario where the server crashed before deleting the pages.
@Test
public void testRestartWithComplete() throws Exception {
clearDataRecreateServerDirs();
Configuration config = createDefaultInVMConfig();
final AtomicBoolean mainCleanup = new AtomicBoolean(true);
class InterruptedCursorProvider extends PageCursorProviderImpl {
InterruptedCursorProvider(PagingStore pagingStore, StorageManager storageManager, ArtemisExecutor executor, int maxCacheSize) {
super(pagingStore, storageManager, executor, maxCacheSize);
}
@Override
public void cleanup() {
if (mainCleanup.get()) {
super.cleanup();
} else {
try {
pagingStore.unlock();
} catch (Throwable ignored) {
}
}
}
}
server = new ActiveMQServerImpl(config, ManagementFactory.getPlatformMBeanServer(), new ActiveMQSecurityManagerImpl()) {
@Override
protected PagingStoreFactoryNIO getPagingStoreFactory() {
return new PagingStoreFactoryNIO(this.getStorageManager(), this.getConfiguration().getPagingLocation(), this.getConfiguration().getJournalBufferTimeout_NIO(), this.getScheduledPool(), this.getExecutorFactory(), this.getConfiguration().isJournalSyncNonTransactional(), null) {
@Override
public PageCursorProvider newCursorProvider(PagingStore store, StorageManager storageManager, AddressSettings addressSettings, ArtemisExecutor executor) {
return new InterruptedCursorProvider(store, storageManager, executor, addressSettings.getPageCacheMaxSize());
}
};
}
};
addServer(server);
AddressSettings defaultSetting = new AddressSettings().setPageSizeBytes(PagingTest.PAGE_SIZE).setMaxSizeBytes(PagingTest.PAGE_MAX).setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
server.getAddressSettingsRepository().addMatch("#", defaultSetting);
server.start();
locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true);
sf = createSessionFactory(locator);
ClientSession session = sf.createSession(true, true, 0);
session.createQueue(PagingTest.ADDRESS, PagingTest.ADDRESS, null, true);
Queue queue = server.locateQueue(ADDRESS);
queue.getPageSubscription().getPagingStore().startPaging();
ClientProducer producer = session.createProducer(PagingTest.ADDRESS);
ClientMessage message;
for (int i = 0; i < 20; i++) {
message = session.createMessage(true);
ActiveMQBuffer bodyLocal = message.getBodyBuffer();
bodyLocal.writeBytes(new byte[100 * 4]);
message.putIntProperty(new SimpleString("idi"), i);
producer.send(message);
session.commit();
if (i < 19) {
queue.getPageSubscription().getPagingStore().forceAnotherPage();
}
}
Assert.assertEquals(20, queue.getPageSubscription().getPagingStore().getCurrentWritingPage());
// This will force a scenario where the pages are cleaned up. When restarting we need to check if the current page is complete
// if it is complete we must move to another page avoiding races on cleanup
// which could happen during a crash / restart
long tx = server.getStorageManager().generateID();
for (int i = 1; i <= 20; i++) {
server.getStorageManager().storePageCompleteTransactional(tx, queue.getID(), new PagePositionImpl(i, 1));
}
server.getStorageManager().commit(tx);
session.close();
sf.close();
server.stop();
mainCleanup.set(false);
logger.trace("Server restart");
server.start();
queue = server.locateQueue(ADDRESS);
locator = createInVMNonHALocator();
sf = createSessionFactory(locator);
session = sf.createSession(null, null, false, false, true, false, 0);
producer = session.createProducer(PagingTest.ADDRESS);
for (int i = 0; i < 10; i++) {
message = session.createMessage(true);
ActiveMQBuffer bodyLocal = message.getBodyBuffer();
bodyLocal.writeBytes(new byte[100 * 4]);
message.putIntProperty(new SimpleString("newid"), i);
producer.send(message);
session.commit();
if (i == 5) {
queue.getPageSubscription().getPagingStore().forceAnotherPage();
}
}
mainCleanup.set(true);
queue = server.locateQueue(ADDRESS);
queue.getPageSubscription().cleanupEntries(false);
queue.getPageSubscription().getPagingStore().getCursorProvider().cleanup();
ClientConsumer consumer = session.createConsumer(ADDRESS);
session.start();
for (int i = 0; i < 10; i++) {
message = consumer.receive(5000);
Assert.assertNotNull(message);
Assert.assertEquals(i, message.getIntProperty("newid").intValue());
message.acknowledge();
}
server.stop();
// Thread.sleep(5000);
}
use of org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl in project activemq-artemis by apache.
the class CursorAckRecordEncoding method decode.
@Override
public void decode(ActiveMQBuffer buffer) {
queueID = buffer.readLong();
long pageNR = buffer.readLong();
int messageNR = buffer.readInt();
this.position = new PagePositionImpl(pageNR, messageNR);
}
use of org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl in project activemq-artemis by apache.
the class PrintData method printPages.
private static void printPages(DescribeJournal describeJournal, StorageManager sm, PagingManager manager, PrintStream out, boolean safe) throws Exception {
PageCursorsInfo cursorACKs = calculateCursorsInfo(describeJournal.getRecords());
Set<Long> pgTXs = cursorACKs.getPgTXs();
manager.start();
SimpleString[] stores = manager.getStoreNames();
for (SimpleString store : stores) {
PagingStore pgStore = manager.getPageStore(store);
File folder = null;
if (pgStore != null) {
folder = pgStore.getFolder();
}
out.println("####################################################################################################");
out.println("Exploring store " + store + " folder = " + folder);
int pgid = (int) pgStore.getFirstPage();
for (int pg = 0; pg < pgStore.getNumberOfPages(); pg++) {
out.println("******* Page " + pgid);
Page page = pgStore.createPage(pgid);
page.open();
List<PagedMessage> msgs = page.read(sm);
page.close();
int msgID = 0;
for (PagedMessage msg : msgs) {
msg.initMessage(sm);
if (safe) {
try {
out.print("pg=" + pgid + ", msg=" + msgID + ",pgTX=" + msg.getTransactionID() + ", msg=" + msg.getMessage().getClass().getSimpleName() + "(safe data, size=" + msg.getMessage().getPersistentSize() + ")");
} catch (Exception e) {
out.print("pg=" + pgid + ", msg=" + msgID + ",pgTX=" + msg.getTransactionID() + ", msg=" + msg.getMessage().getClass().getSimpleName() + "(safe data)");
}
} else {
out.print("pg=" + pgid + ", msg=" + msgID + ",pgTX=" + msg.getTransactionID() + ",userMessageID=" + (msg.getMessage().getUserID() != null ? msg.getMessage().getUserID() : "") + ", msg=" + msg.getMessage());
}
out.print(",Queues = ");
long[] q = msg.getQueueIDs();
for (int i = 0; i < q.length; i++) {
out.print(q[i]);
PagePosition posCheck = new PagePositionImpl(pgid, msgID);
boolean acked = false;
Set<PagePosition> positions = cursorACKs.getCursorRecords().get(q[i]);
if (positions != null) {
acked = positions.contains(posCheck);
}
if (acked) {
out.print(" (ACK)");
}
if (cursorACKs.getCompletePages(q[i]).contains(Long.valueOf(pgid))) {
out.println(" (PG-COMPLETE)");
}
if (i + 1 < q.length) {
out.print(",");
}
}
if (msg.getTransactionID() >= 0 && !pgTXs.contains(msg.getTransactionID())) {
out.print(", **PG_TX_NOT_FOUND**");
}
out.println();
msgID++;
}
pgid++;
}
}
}
Aggregations