use of org.apache.activemq.artemis.core.persistence.StorageManager in project activemq-artemis by apache.
the class ActiveMQServerImpl method createQueue.
public Queue createQueue(final AddressInfo addrInfo, final SimpleString queueName, final SimpleString filterString, final SimpleString user, final boolean durable, final boolean temporary, final boolean ignoreIfExists, final boolean transientQueue, final boolean autoCreated, final int maxConsumers, final boolean purgeOnNoConsumers, final boolean exclusive, final boolean lastValue, final boolean autoCreateAddress) throws Exception {
final QueueBinding binding = (QueueBinding) postOffice.getBinding(queueName);
if (binding != null) {
if (ignoreIfExists) {
return binding.getQueue();
} else {
throw ActiveMQMessageBundle.BUNDLE.queueAlreadyExists(queueName, binding.getAddress());
}
}
final Filter filter = FilterImpl.createFilter(filterString);
final long txID = storageManager.generateID();
final long queueID = storageManager.generateID();
final QueueConfig.Builder queueConfigBuilder;
final SimpleString addressToUse = addrInfo == null ? queueName : addrInfo.getName();
queueConfigBuilder = QueueConfig.builderWith(queueID, queueName, addressToUse);
AddressInfo info = postOffice.getAddressInfo(addressToUse);
RoutingType routingType = addrInfo == null ? null : addrInfo.getRoutingType();
RoutingType rt = (routingType == null ? ActiveMQDefaultConfiguration.getDefaultRoutingType() : routingType);
if (autoCreateAddress) {
if (info == null) {
final AddressInfo addressInfo = new AddressInfo(addressToUse, rt);
addressInfo.setAutoCreated(true);
addressInfo.setInternal(addrInfo == null ? false : addrInfo.isInternal());
addAddressInfo(addressInfo);
} else if (!info.getRoutingTypes().contains(rt)) {
EnumSet<RoutingType> routingTypes = EnumSet.copyOf(info.getRoutingTypes());
routingTypes.add(rt);
updateAddressInfo(info.getName(), routingTypes);
}
} else if (info == null) {
throw ActiveMQMessageBundle.BUNDLE.addressDoesNotExist(addressToUse);
} else if (!info.getRoutingTypes().contains(rt)) {
throw ActiveMQMessageBundle.BUNDLE.invalidRoutingTypeForAddress(rt, info.getName().toString(), info.getRoutingTypes());
}
final QueueConfig queueConfig = queueConfigBuilder.filter(filter).pagingManager(pagingManager).user(user).durable(durable).temporary(temporary).autoCreated(autoCreated).routingType(addrInfo.getRoutingType()).maxConsumers(maxConsumers).purgeOnNoConsumers(purgeOnNoConsumers).exclusive(exclusive).lastValue(lastValue).build();
callBrokerPlugins(hasBrokerPlugins() ? plugin -> plugin.beforeCreateQueue(queueConfig) : null);
final Queue queue = queueFactory.createQueueWith(queueConfig);
if (transientQueue) {
queue.setConsumersRefCount(new TransientQueueManagerImpl(this, queue.getName()));
} else {
queue.setConsumersRefCount(new QueueManagerImpl(this, queue.getName()));
}
final QueueBinding localQueueBinding = new LocalQueueBinding(queue.getAddress(), queue, nodeManager.getNodeId());
if (queue.isDurable()) {
storageManager.addQueueBinding(txID, localQueueBinding);
}
try {
postOffice.addBinding(localQueueBinding);
if (queue.isDurable()) {
storageManager.commitBindings(txID);
}
} catch (Exception e) {
try {
if (durable) {
storageManager.rollbackBindings(txID);
}
final PageSubscription pageSubscription = queue.getPageSubscription();
try {
queue.close();
} finally {
if (pageSubscription != null) {
pageSubscription.destroy();
}
}
} catch (Throwable ignored) {
logger.debug(ignored.getMessage(), ignored);
}
throw e;
}
if (addrInfo == null || !addrInfo.isInternal()) {
managementService.registerQueue(queue, queue.getAddress(), storageManager);
}
callBrokerPlugins(hasBrokerPlugins() ? plugin -> plugin.afterCreateQueue(queue) : null);
callPostQueueCreationCallbacks(queue.getName());
return queue;
}
use of org.apache.activemq.artemis.core.persistence.StorageManager in project activemq-artemis by apache.
the class ActiveMQServerImpl method createQueue.
@Override
public Queue createQueue(final SimpleString address, final RoutingType routingType, final SimpleString queueName, final SimpleString filterString, final SimpleString user, final boolean durable, final boolean temporary, final boolean ignoreIfExists, final boolean transientQueue, final boolean autoCreated, final int maxConsumers, final boolean purgeOnNoConsumers, final boolean exclusive, final boolean lastValue, final boolean autoCreateAddress) throws Exception {
final QueueBinding binding = (QueueBinding) postOffice.getBinding(queueName);
if (binding != null) {
if (ignoreIfExists) {
return binding.getQueue();
} else {
throw ActiveMQMessageBundle.BUNDLE.queueAlreadyExists(queueName, binding.getAddress());
}
}
final Filter filter = FilterImpl.createFilter(filterString);
final long txID = storageManager.generateID();
final long queueID = storageManager.generateID();
final QueueConfig.Builder queueConfigBuilder;
final SimpleString addressToUse = address == null ? queueName : address;
queueConfigBuilder = QueueConfig.builderWith(queueID, queueName, addressToUse);
AddressInfo info = postOffice.getAddressInfo(addressToUse);
if (autoCreateAddress) {
RoutingType rt = (routingType == null ? ActiveMQDefaultConfiguration.getDefaultRoutingType() : routingType);
if (info == null) {
final AddressInfo addressInfo = new AddressInfo(addressToUse, rt);
addressInfo.setAutoCreated(true);
addAddressInfo(addressInfo);
} else if (!info.getRoutingTypes().contains(routingType)) {
EnumSet<RoutingType> routingTypes = EnumSet.copyOf(info.getRoutingTypes());
routingTypes.add(routingType);
updateAddressInfo(info.getName(), routingTypes);
}
} else if (info == null) {
throw ActiveMQMessageBundle.BUNDLE.addressDoesNotExist(addressToUse);
} else if (!info.getRoutingTypes().contains(routingType)) {
throw ActiveMQMessageBundle.BUNDLE.invalidRoutingTypeForAddress(routingType, info.getName().toString(), info.getRoutingTypes());
}
final QueueConfig queueConfig = queueConfigBuilder.filter(filter).pagingManager(pagingManager).user(user).durable(durable).temporary(temporary).autoCreated(autoCreated).routingType(routingType).maxConsumers(maxConsumers).purgeOnNoConsumers(purgeOnNoConsumers).exclusive(exclusive).lastValue(lastValue).build();
callBrokerPlugins(hasBrokerPlugins() ? plugin -> plugin.beforeCreateQueue(queueConfig) : null);
final Queue queue = queueFactory.createQueueWith(queueConfig);
if (transientQueue) {
queue.setConsumersRefCount(new TransientQueueManagerImpl(this, queue.getName()));
} else {
queue.setConsumersRefCount(new QueueManagerImpl(this, queue.getName()));
}
final QueueBinding localQueueBinding = new LocalQueueBinding(queue.getAddress(), queue, nodeManager.getNodeId());
if (queue.isDurable()) {
storageManager.addQueueBinding(txID, localQueueBinding);
}
try {
postOffice.addBinding(localQueueBinding);
if (queue.isDurable()) {
storageManager.commitBindings(txID);
}
} catch (Exception e) {
try {
if (durable) {
storageManager.rollbackBindings(txID);
}
final PageSubscription pageSubscription = queue.getPageSubscription();
try {
queue.close();
} finally {
if (pageSubscription != null) {
pageSubscription.destroy();
}
}
} catch (Throwable ignored) {
logger.debug(ignored.getMessage(), ignored);
}
throw e;
}
managementService.registerQueue(queue, queue.getAddress(), storageManager);
callBrokerPlugins(hasBrokerPlugins() ? plugin -> plugin.afterCreateQueue(queue) : null);
callPostQueueCreationCallbacks(queue.getName());
return queue;
}
use of org.apache.activemq.artemis.core.persistence.StorageManager in project activemq-artemis by apache.
the class HangConsumerTest method testHangDuplicateQueues.
/**
* This would recreate the scenario where a queue was duplicated
*
* @throws Exception
*/
@Test
public void testHangDuplicateQueues() throws Exception {
final Semaphore blocked = new Semaphore(1);
final CountDownLatch latchDelete = new CountDownLatch(1);
class MyQueueWithBlocking extends QueueImpl {
/**
* @param id
* @param address
* @param name
* @param filter
* @param pageSubscription
* @param durable
* @param temporary
* @param scheduledExecutor
* @param postOffice
* @param storageManager
* @param addressSettingsRepository
* @param executor
*/
MyQueueWithBlocking(final long id, final SimpleString address, final SimpleString name, final Filter filter, final SimpleString user, final PageSubscription pageSubscription, final boolean durable, final boolean temporary, final boolean autoCreated, final RoutingType deliveryMode, final Integer maxConsumers, final Boolean purgeOnNoConsumers, final ScheduledExecutorService scheduledExecutor, final PostOffice postOffice, final StorageManager storageManager, final HierarchicalRepository<AddressSettings> addressSettingsRepository, final ArtemisExecutor executor, final ActiveMQServer server) {
super(id, address, name, filter, pageSubscription, user, durable, temporary, autoCreated, deliveryMode, maxConsumers, purgeOnNoConsumers, scheduledExecutor, postOffice, storageManager, addressSettingsRepository, executor, server, null);
}
@Override
public synchronized int deleteMatchingReferences(final int flushLimit, final Filter filter) throws Exception {
latchDelete.countDown();
blocked.acquire();
blocked.release();
return super.deleteMatchingReferences(flushLimit, filter);
}
@Override
public void deliverScheduledMessages() {
}
}
class LocalFactory extends QueueFactoryImpl {
LocalFactory(final ExecutorFactory executorFactory, final ScheduledExecutorService scheduledExecutor, final HierarchicalRepository<AddressSettings> addressSettingsRepository, final StorageManager storageManager, final ActiveMQServer server) {
super(executorFactory, scheduledExecutor, addressSettingsRepository, storageManager, server);
}
@Override
public Queue createQueueWith(final QueueConfig config) {
queue = new MyQueueWithBlocking(config.id(), config.address(), config.name(), config.filter(), config.user(), config.pageSubscription(), config.isDurable(), config.isTemporary(), config.isAutoCreated(), config.deliveryMode(), config.maxConsumers(), config.isPurgeOnNoConsumers(), scheduledExecutor, postOffice, storageManager, addressSettingsRepository, executorFactory.getExecutor(), server);
return queue;
}
@Deprecated
@Override
public Queue createQueue(final long persistenceID, final SimpleString address, final SimpleString name, final Filter filter, final PageSubscription pageSubscription, final SimpleString user, final boolean durable, final boolean temporary, final boolean autoCreated) {
queue = new MyQueueWithBlocking(persistenceID, address, name, filter, user, pageSubscription, durable, temporary, autoCreated, RoutingType.MULTICAST, null, null, scheduledExecutor, postOffice, storageManager, addressSettingsRepository, executorFactory.getExecutor(), server);
return queue;
}
}
LocalFactory queueFactory = new LocalFactory(server.getExecutorFactory(), server.getScheduledPool(), server.getAddressSettingsRepository(), server.getStorageManager(), server);
queueFactory.setPostOffice(server.getPostOffice());
((ActiveMQServerImpl) server).replaceQueueFactory(queueFactory);
queue = server.createQueue(QUEUE, RoutingType.ANYCAST, QUEUE, null, true, false);
blocked.acquire();
ClientSessionFactory factory = locator.createSessionFactory();
ClientSession session = factory.createSession(false, false, false);
ClientProducer producer = session.createProducer(QUEUE);
producer.send(session.createMessage(true));
session.commit();
Thread tDelete = new Thread() {
@Override
public void run() {
try {
server.destroyQueue(QUEUE);
} catch (Exception e) {
e.printStackTrace();
}
}
};
tDelete.start();
Assert.assertTrue(latchDelete.await(10, TimeUnit.SECONDS));
try {
server.createQueue(QUEUE, RoutingType.ANYCAST, QUEUE, null, true, false);
} catch (Exception expected) {
}
blocked.release();
server.stop();
tDelete.join();
session.close();
// a duplicate binding would impede the server from starting
server.start();
waitForServerToStart(server);
server.stop();
}
use of org.apache.activemq.artemis.core.persistence.StorageManager in project activemq-artemis by apache.
the class InterruptedLargeMessageTest method testRestartBeforeDelete.
@Test
public void testRestartBeforeDelete() throws Exception {
class NoPostACKQueue extends QueueImpl {
NoPostACKQueue(long id, SimpleString address, SimpleString name, Filter filter, SimpleString user, PageSubscription pageSubscription, boolean durable, boolean temporary, boolean autoCreated, ScheduledExecutorService scheduledExecutor, PostOffice postOffice, StorageManager storageManager, HierarchicalRepository<AddressSettings> addressSettingsRepository, ArtemisExecutor executor) {
super(id, address, name, filter, pageSubscription, user, durable, temporary, autoCreated, scheduledExecutor, postOffice, storageManager, addressSettingsRepository, executor, null, null);
}
@Override
public void postAcknowledge(final MessageReference ref) {
System.out.println("Ignoring postACK on message " + ref);
}
@Override
public void deliverScheduledMessages() {
}
}
final class NoPostACKQueueFactory implements QueueFactory {
final StorageManager storageManager;
final PostOffice postOffice;
final ScheduledExecutorService scheduledExecutor;
final HierarchicalRepository<AddressSettings> addressSettingsRepository;
final ExecutorFactory execFactory;
NoPostACKQueueFactory(StorageManager storageManager, PostOffice postOffice, ScheduledExecutorService scheduledExecutor, HierarchicalRepository<AddressSettings> addressSettingsRepository, final ExecutorFactory execFactory) {
this.storageManager = storageManager;
this.postOffice = postOffice;
this.scheduledExecutor = scheduledExecutor;
this.addressSettingsRepository = addressSettingsRepository;
this.execFactory = execFactory;
}
@Override
public Queue createQueueWith(final QueueConfig config) {
return new NoPostACKQueue(config.id(), config.address(), config.name(), config.filter(), config.user(), config.pageSubscription(), config.isDurable(), config.isTemporary(), config.isAutoCreated(), scheduledExecutor, postOffice, storageManager, addressSettingsRepository, execFactory.getExecutor());
}
@Deprecated
@Override
public Queue createQueue(long persistenceID, SimpleString address, SimpleString name, Filter filter, PageSubscription pageSubscription, SimpleString user, boolean durable, boolean temporary, boolean autoCreated) {
return new NoPostACKQueue(persistenceID, address, name, filter, user, pageSubscription, durable, temporary, autoCreated, scheduledExecutor, postOffice, storageManager, addressSettingsRepository, execFactory.getExecutor());
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.server.QueueFactory#setPostOffice(org.apache.activemq.artemis.core.postoffice.PostOffice)
*/
@Override
public void setPostOffice(PostOffice postOffice) {
}
}
ClientSession session = null;
LargeMessageTestInterceptorIgnoreLastPacket.disableInterrupt();
ActiveMQServer server = createServer(true, isNetty());
server.start();
QueueFactory original = server.getQueueFactory();
((ActiveMQServerImpl) server).replaceQueueFactory(new NoPostACKQueueFactory(server.getStorageManager(), server.getPostOffice(), server.getScheduledPool(), server.getAddressSettingsRepository(), server.getExecutorFactory()));
locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true);
ClientSessionFactory sf = createSessionFactory(locator);
session = sf.createSession(false, true, true);
session.createQueue(ADDRESS, ADDRESS, true);
ClientProducer producer = session.createProducer(ADDRESS);
for (int i = 0; i < 10; i++) {
Message clientFile = createLargeClientMessageStreaming(session, LARGE_MESSAGE_SIZE, true);
producer.send(clientFile);
}
session.commit();
session.close();
session = sf.createSession(false, false);
ClientConsumer cons = session.createConsumer(ADDRESS);
session.start();
for (int i = 0; i < 10; i++) {
ClientMessage msg = cons.receive(5000);
Assert.assertNotNull(msg);
msg.saveToOutputStream(new java.io.OutputStream() {
@Override
public void write(int b) throws IOException {
}
});
msg.acknowledge();
session.commit();
}
((ActiveMQServerImpl) server).replaceQueueFactory(original);
server.fail(false);
server.start();
server.stop();
validateNoFilesOnLargeDir();
}
use of org.apache.activemq.artemis.core.persistence.StorageManager in project activemq-artemis by apache.
the class PageCursorStressTest method testLazyCommit.
@Test
public void testLazyCommit() throws Exception {
PagingStoreImpl pageStore = lookupPageStore(ADDRESS);
pageStore.startPaging();
final int NUM_MESSAGES = 100;
final int messageSize = 100 * 1024;
PageCursorProvider cursorProvider = this.server.getPagingManager().getPageStore(ADDRESS).getCursorProvider();
System.out.println("cursorProvider = " + cursorProvider);
PageSubscription cursor = this.server.getPagingManager().getPageStore(ADDRESS).getCursorProvider().getSubscription(queue.getID());
LinkedListIterator<PagedReference> iterator = cursor.iterator();
System.out.println("Cursor: " + cursor);
StorageManager storage = this.server.getStorageManager();
long pgtxLazy = storage.generateID();
Transaction txLazy = pgMessages(storage, pageStore, pgtxLazy, 0, NUM_MESSAGES, messageSize);
addMessages(100, NUM_MESSAGES, messageSize);
System.out.println("Number of pages - " + pageStore.getNumberOfPages());
// First consume what's already there without any tx as nothing was committed
for (int i = 100; i < 200; i++) {
PagedReference pos = iterator.next();
assertNotNull("Null at position " + i, pos);
assertEquals(i, pos.getMessage().getIntProperty("key").intValue());
cursor.ack(pos);
}
assertNull(iterator.next());
txLazy.commit();
storage.waitOnOperations();
for (int i = 0; i < 100; i++) {
PagedReference pos = iterator.next();
assertNotNull("Null at position " + i, pos);
assertEquals(i, pos.getMessage().getIntProperty("key").intValue());
cursor.ack(pos);
}
assertNull(iterator.next());
waitCleanup();
server.stop();
createServer();
waitCleanup();
assertEquals(1, lookupPageStore(ADDRESS).getNumberOfPages());
}
Aggregations