Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -832,12 +832,6 @@ public SimpleString getMatchingQueue(SimpleString address, RoutingType routingTy
return serverSession.getMatchingQueue(address, routingType);
}

public SimpleString getMatchingQueue(SimpleString address,
SimpleString queueName,
RoutingType routingType) throws Exception {
return serverSession.getMatchingQueue(address, queueName, routingType);
}

public AddressInfo getAddress(SimpleString address) {
return serverSession.getAddress(address);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ protected SimpleString getMatchingQueue(SimpleString queueName, SimpleString add
throw new ActiveMQIllegalStateException("Queue: " + queueName + " filter mismatch [" + filter + "] is different than existing filter [" + result.getFilterString() + "]");

}
return sessionSPI.getMatchingQueue(address, queueName, routingType);
return queueName;
}
}

Expand Down
6 changes: 6 additions & 0 deletions artemis-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@
<artifactId>mockserver-client-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public interface AddressManager {

SimpleString getMatchingQueue(SimpleString address, RoutingType routingType) throws Exception;

SimpleString getMatchingQueue(SimpleString address, SimpleString queueName, RoutingType routingType) throws Exception;

LocalQueueBinding findLocalBinding(long id);

void clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ default Queue findQueue(final long bindingID) {

SimpleString getMatchingQueue(SimpleString address, RoutingType routingType) throws Exception;

SimpleString getMatchingQueue(SimpleString address, SimpleString queueName, RoutingType routingType) throws Exception;

RoutingStatus route(Message message, boolean direct) throws Exception;

RoutingStatus route(Message message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1538,13 +1538,6 @@ public SimpleString getMatchingQueue(SimpleString address, RoutingType routingTy
return addressManager.getMatchingQueue(address, routingType);
}

@Override
public SimpleString getMatchingQueue(SimpleString address,
SimpleString queueName,
RoutingType routingType) throws Exception {
return addressManager.getMatchingQueue(address, queueName, routingType);
}

@Override
public void sendQueueInfoToQueue(final SimpleString queueName, final SimpleString address) throws Exception {
// We send direct to the queue so we can send it to the same queue that is bound to the notifications address -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,12 @@ public class SimpleAddressManager implements AddressManager {

protected final WildcardConfiguration wildcardConfiguration;

public SimpleAddressManager(final BindingsFactory bindingsFactory, final StorageManager storageManager,
final MetricsManager metricsManager) {
this(bindingsFactory, new WildcardConfiguration(), storageManager, metricsManager);
}

public SimpleAddressManager(final BindingsFactory bindingsFactory,
final WildcardConfiguration wildcardConfiguration,
final StorageManager storageManager,
final MetricsManager metricsManager) {
this.wildcardConfiguration = wildcardConfiguration;
this.bindingsFactory = bindingsFactory;
this.wildcardConfiguration = wildcardConfiguration;
this.storageManager = storageManager;
this.metricsManager = metricsManager;
}
Expand Down Expand Up @@ -190,37 +185,49 @@ public Collection<Binding> getDirectBindings(final SimpleString address) throws
return Collections.unmodifiableCollection(outputList);
}

/**
* This method looks at the local queue bindings of an address to find one with a matching routing type that also is
* not wild. A match with the same name as the input address is preferred.
* <p>
* If a match is found, the unique name of the corresponding binding is returned. If no match is found, the method
* returns {@code null}.
*
* @param address the address to match against
* @param routingType the routing type to use for matching the binding
* @return the unique name of the matching binding, or {@code null} if no match is found
* @throws Exception if an error occurs while attempting to find a matching binding
*/
@Override
public SimpleString getMatchingQueue(final SimpleString address, RoutingType routingType) throws Exception {
SimpleString realAddress = CompositeAddress.extractAddressName(address);
Binding binding = getBinding(realAddress);

if (binding == null || !(binding instanceof LocalQueueBinding) || !binding.getAddress().equals(realAddress)) {
Bindings bindings = mappings.get(realAddress);
if (bindings != null) {
for (Binding theBinding : bindings.getBindings()) {
if (theBinding instanceof LocalQueueBinding && !wildcardConfiguration.isWild(theBinding.getUniqueName())) {
binding = theBinding;
break;
}
Binding potentialMatch = getBinding(realAddress);
if (checkBindingForMatch(potentialMatch, realAddress, routingType)) {
// a local queue binding with the same name as the input address is preferred
return potentialMatch.getUniqueName();
}

Bindings bindings = mappings.get(realAddress);
if (bindings != null) {
for (Binding b : bindings.getBindings()) {
if (checkBindingForMatch(b, realAddress, routingType)) {
return b.getUniqueName();
}
}
}

return binding != null ? binding.getUniqueName() : null;
return null;
}

@Override
public SimpleString getMatchingQueue(final SimpleString address,
final SimpleString queueName,
RoutingType routingType) throws Exception {
SimpleString realAddress = CompositeAddress.extractAddressName(address);
Binding binding = getBinding(queueName);

if (binding != null && !binding.getAddress().equals(realAddress) && !realAddress.toString().isEmpty()) {
throw new IllegalStateException("queue belongs to address" + binding.getAddress());
private boolean checkBindingForMatch(Binding binding, SimpleString address, RoutingType routingType) {
if (binding != null &&
binding instanceof LocalQueueBinding lqb &&
lqb.getAddress().equals(address) &&
lqb.getQueue().getRoutingType() == routingType &&
!wildcardConfiguration.isWild(binding.getUniqueName())) {
return true;
}
return binding != null ? binding.getUniqueName() : null;
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ public WildcardAddressManager(final BindingsFactory bindingsFactory,
super(bindingsFactory, wildcardConfiguration, storageManager, metricsManager);
}

public WildcardAddressManager(final BindingsFactory bindingsFactory,
final StorageManager storageManager,
final MetricsManager metricsManager) {
super(bindingsFactory, storageManager, metricsManager);
}

// publish, may be a new address that needs wildcard bindings added
// won't contain a wildcard because we don't ever route to a wildcards at this time
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,6 @@ void createSharedQueue(SimpleString address,

SimpleString getMatchingQueue(SimpleString address, RoutingType routingType) throws Exception;

SimpleString getMatchingQueue(SimpleString address,
SimpleString queueName,
RoutingType routingType) throws Exception;

AddressInfo getAddress(SimpleString address);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,7 @@ public AutoCreateResult checkAutoCreate(final QueueConfiguration queueConfig) th
Queue q = server.locateQueue(unPrefixedQueue);
if (q == null) {
// The queue doesn't exist.
if (!queueConfig.isFqqn() && server.getPostOffice().getMatchingQueue(unPrefixedAddress, queueConfig.getRoutingType()) != null) {
if (!queueConfig.isFqqn() && getMatchingQueue(unPrefixedAddress, queueConfig.getRoutingType()) != null) {
// The address has a local, non-wildcard queue with a different name, which is fine. Just ignore it.
result = AutoCreateResult.EXISTED;
} else if (addressSettings.isAutoCreateQueues() || queueConfig.isTemporary()) {
Expand Down Expand Up @@ -2226,13 +2226,6 @@ public SimpleString getMatchingQueue(SimpleString address, RoutingType routingTy
return server.getPostOffice().getMatchingQueue(address, routingType);
}

@Override
public SimpleString getMatchingQueue(SimpleString address,
SimpleString queueName,
RoutingType routingType) throws Exception {
return server.getPostOffice().getMatchingQueue(address, queueName, routingType);
}

@Override
public AddressInfo getAddress(SimpleString address) {
return server.getPostOffice().getAddressInfo(removePrefix(address));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.activemq.artemis.core.postoffice.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.config.WildcardConfiguration;
import org.apache.activemq.artemis.core.postoffice.Binding;
import org.apache.activemq.artemis.core.postoffice.Bindings;
import org.apache.activemq.artemis.core.postoffice.BindingsFactory;
import org.apache.activemq.artemis.core.server.Queue;
import org.junit.jupiter.api.Test;

import static org.apache.activemq.artemis.utils.RandomUtil.randomUUIDSimpleString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class AddressManagerGetMatchingQueueTest {

@Test
public void testCorrectNameCorrectRoutingTypeSingleQueue() throws Exception {
SimpleString addressName = SimpleString.of("myAddress");
SimpleString queueName = randomUUIDSimpleString();
SimpleAddressManager am = getSimpleAddressManager();
am.addBinding(getLocalQueueBinding(addressName, queueName, RoutingType.ANYCAST));

assertEquals(queueName, am.getMatchingQueue(addressName, RoutingType.ANYCAST));
}

@Test
public void testCorrectNameCorrectRoutingTypeMultipleQueues() throws Exception {
SimpleString addressName = SimpleString.of("myAddress");
SimpleString queueName1 = randomUUIDSimpleString();
SimpleString queueName2 = randomUUIDSimpleString();
SimpleAddressManager am = getSimpleAddressManager();
am.addBinding(getLocalQueueBinding(addressName, queueName1, RoutingType.ANYCAST));
am.addBinding(getLocalQueueBinding(addressName, queueName2, RoutingType.ANYCAST));

assertThat(am.getMatchingQueue(addressName, RoutingType.ANYCAST), anyOf(is(queueName1), is(queueName2)));
}

@Test
public void testCorrectNameCorrectRoutingTypeMultipleQueuesMixedRoutingTypes() throws Exception {
SimpleString addressName = SimpleString.of("myAddress");
SimpleString queueName1 = randomUUIDSimpleString();
SimpleString queueName2 = randomUUIDSimpleString();
SimpleAddressManager am = getSimpleAddressManager();
am.addBinding(getLocalQueueBinding(addressName, queueName1, RoutingType.ANYCAST));
am.addBinding(getLocalQueueBinding(addressName, queueName2, RoutingType.MULTICAST));

assertEquals(queueName1, am.getMatchingQueue(addressName, RoutingType.ANYCAST));
}

@Test
public void testCorrectNameIncorrectRoutingTypeSingleQueue() throws Exception {
SimpleString addressName = SimpleString.of("myAddress");
SimpleAddressManager am = getSimpleAddressManager();
am.addBinding(getLocalQueueBinding(addressName, RoutingType.MULTICAST));

assertNull(am.getMatchingQueue(addressName, RoutingType.ANYCAST));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above, this returns nothing, but if we gave it good data would it return the thing we expect?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "good data" variation is tested by testCorrectNameCorrectRoutingTypeSingleQueue.

}

@Test
public void testCorrectNameIncorrectRoutingTypeMultipleQueues() throws Exception {
SimpleString addressName = SimpleString.of("myAddress");
SimpleAddressManager am = getSimpleAddressManager();
am.addBinding(getLocalQueueBinding(addressName, RoutingType.MULTICAST));
am.addBinding(getLocalQueueBinding(addressName, RoutingType.MULTICAST));

assertNull(am.getMatchingQueue(addressName, RoutingType.ANYCAST));
}

@Test
public void testIncorrectNameCorrectRoutingTypeSingleQueue() throws Exception {
SimpleAddressManager am = getSimpleAddressManager();
am.addBinding(getLocalQueueBinding(randomUUIDSimpleString(), RoutingType.ANYCAST));

assertNull(am.getMatchingQueue(randomUUIDSimpleString(), RoutingType.ANYCAST));
}

@Test
public void testIncorrectNameCorrectRoutingTypeMultipleQueues() throws Exception {
SimpleAddressManager am = getSimpleAddressManager();
am.addBinding(getLocalQueueBinding(randomUUIDSimpleString(), RoutingType.ANYCAST));
am.addBinding(getLocalQueueBinding(randomUUIDSimpleString(), RoutingType.ANYCAST));

assertNull(am.getMatchingQueue(randomUUIDSimpleString(), RoutingType.ANYCAST));
}

@Test
public void testIncorrectNameIncorrectRoutingTypeSingleQueue() throws Exception {
SimpleAddressManager am = getSimpleAddressManager();
am.addBinding(getLocalQueueBinding(randomUUIDSimpleString(), RoutingType.MULTICAST));

assertNull(am.getMatchingQueue(randomUUIDSimpleString(), RoutingType.ANYCAST));
}

@Test
public void testIncorrectNameIncorrectRoutingTypeMultipleQueues() throws Exception {
SimpleAddressManager am = getSimpleAddressManager();
am.addBinding(getLocalQueueBinding(randomUUIDSimpleString(), RoutingType.MULTICAST));
am.addBinding(getLocalQueueBinding(randomUUIDSimpleString(), RoutingType.MULTICAST));

assertNull(am.getMatchingQueue(randomUUIDSimpleString(), RoutingType.ANYCAST));
}

private static SimpleAddressManager getSimpleAddressManager() throws Exception {
return new SimpleAddressManager(getBindingsFactory(), new WildcardConfiguration(), null, null);
}

private static LocalQueueBinding getLocalQueueBinding(SimpleString addressName, RoutingType routingType) {
return getLocalQueueBinding(addressName, randomUUIDSimpleString(), routingType);
}

private static LocalQueueBinding getLocalQueueBinding(SimpleString addressName, SimpleString queueName, RoutingType routingType) {
return new LocalQueueBinding(addressName, getQueue(queueName, routingType), randomUUIDSimpleString());
}

private static BindingsFactory getBindingsFactory() throws Exception {
List<Binding> bindingsList = new ArrayList<>();
Bindings bindings = mock(Bindings.class);
doAnswer(invocation -> {
bindingsList.add(invocation.getArgument(0));
return null;
}).when(bindings).addBinding(any(Binding.class));
when(bindings.getBindings()).thenReturn(bindingsList);

BindingsFactory bindingsFactory = mock(BindingsFactory.class);
when(bindingsFactory.createBindings(any(SimpleString.class))).thenReturn(bindings);
return bindingsFactory;
}

private static Queue getQueue(SimpleString queueName, RoutingType anycast) {
Queue q = mock(Queue.class);
when(q.getName()).thenReturn(queueName);
when(q.getID()).thenReturn(1L);
when(q.getRoutingType()).thenReturn(anycast);
return q;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ public SimpleString getMatchingQueue(SimpleString address, RoutingType routingTy
return null;
}

@Override
public SimpleString getMatchingQueue(SimpleString address, SimpleString queueName, RoutingType routingType) {
return null;
}

@Override
public void start() throws Exception {

Expand Down
Loading