Skip to content

Commit 42be518

Browse files
committedOct 5, 2023
ARTEMIS-4450 Caching hasLocal boolean on bindings to avoid transversing the maps on every call
1 parent 4db3952 commit 42be518

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed
 

‎artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/Bindings.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,5 @@ public interface Bindings extends UnproposalListener {
6363

6464
int size();
6565

66-
default boolean contains(Class clazz) {
67-
return false;
68-
}
66+
boolean hasLocalBinding();
6967
}

‎artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/BindingsImpl.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.slf4j.Logger;
5353
import org.slf4j.LoggerFactory;
5454
import java.lang.invoke.MethodHandles;
55+
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
5556
import java.util.function.BiConsumer;
5657

5758
public final class BindingsImpl implements Bindings {
@@ -86,7 +87,12 @@ public final class BindingsImpl implements Bindings {
8687
/**
8788
* This has a version about adds and removes
8889
*/
89-
private final AtomicInteger version = new AtomicInteger(sequenceVersion.incrementAndGet());
90+
private volatile int version;
91+
92+
private static final AtomicIntegerFieldUpdater<BindingsImpl> VERSION_UPDATER = AtomicIntegerFieldUpdater
93+
.newUpdater(BindingsImpl.class, "version");
94+
95+
private volatile boolean hasLocal;
9096

9197
public BindingsImpl(final SimpleString name, final GroupingHandler groupingHandler, StorageManager storageManager) {
9298
this.groupingHandler = groupingHandler;
@@ -157,7 +163,19 @@ public void updated(QueueBinding binding) {
157163
}
158164

159165
private void updated() {
160-
version.set(sequenceVersion.incrementAndGet());
166+
VERSION_UPDATER.set(this, sequenceVersion.incrementAndGet());
167+
checkBindingsIdMap();
168+
}
169+
170+
private void checkBinding(Long bindingID, Binding binding) {
171+
if (binding instanceof LocalQueueBinding) {
172+
hasLocal = true;
173+
}
174+
}
175+
176+
private void checkBindingsIdMap() {
177+
hasLocal = false;
178+
bindingsIdMap.forEach(this::checkBinding);
161179
}
162180

163181
@Override
@@ -201,14 +219,8 @@ public int size() {
201219
}
202220

203221
@Override
204-
public boolean contains(Class clazz) {
205-
for (Binding binding : getBindings()) {
206-
if (clazz.isInstance(binding)) {
207-
return true;
208-
}
209-
}
210-
211-
return false;
222+
public boolean hasLocalBinding() {
223+
return hasLocal;
212224
}
213225

214226

@@ -296,7 +308,7 @@ public void route(final Message message, final RoutingContext context) throws Ex
296308
private void route(final Message message,
297309
final RoutingContext context,
298310
final boolean groupRouting) throws Exception {
299-
final int currentVersion = version.get();
311+
final int currentVersion = VERSION_UPDATER.get(this);
300312
final boolean reusableContext = context.isReusable(message, currentVersion);
301313

302314
if (!reusableContext) {

‎artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/PostOfficeImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ private AddressInfo checkAddress(RoutingContext context, SimpleString address) t
12691269

12701270
Bindings simpleRoute(SimpleString address, RoutingContext context, Message message, AddressInfo addressInfo) throws Exception {
12711271
Bindings bindings = addressManager.getBindingsForRoutingAddress(address);
1272-
if ((bindings == null || !bindings.contains(LocalQueueBinding.class)) && context.getServerSession() != null) {
1272+
if ((bindings == null || !bindings.hasLocalBinding()) && context.getServerSession() != null) {
12731273
AutoCreateResult autoCreateResult = context.getServerSession().checkAutoCreate(new QueueConfiguration(address).setRoutingType(context.getRoutingType()));
12741274
if (autoCreateResult == AutoCreateResult.NOT_FOUND) {
12751275
ActiveMQException e = ActiveMQMessageBundle.BUNDLE.addressDoesNotExist(address);

‎artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import org.apache.activemq.artemis.core.postoffice.PostOffice;
6565
import org.apache.activemq.artemis.core.postoffice.QueueBinding;
6666
import org.apache.activemq.artemis.core.postoffice.RoutingStatus;
67-
import org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding;
6867
import org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection;
6968
import org.apache.activemq.artemis.core.remoting.CertificateUtil;
7069
import org.apache.activemq.artemis.core.remoting.CloseListener;
@@ -1826,7 +1825,7 @@ public AutoCreateResult checkAutoCreate(final QueueConfiguration queueConfig) th
18261825
if (server.locateQueue(unPrefixedQueue) == null) {
18271826
// The queue doesn't exist.
18281827
Bindings bindings = server.getPostOffice().lookupBindingsForAddress(unPrefixedAddress);
1829-
if (bindings != null && bindings.contains(LocalQueueBinding.class) && !queueConfig.isFqqn()) {
1828+
if (bindings != null && bindings.hasLocalBinding() && !queueConfig.isFqqn()) {
18301829
// The address has another queue with a different name, which is fine. Just ignore it.
18311830
result = AutoCreateResult.EXISTED;
18321831
} else if (addressSettings.isAutoCreateQueues() || queueConfig.isTemporary()) {

‎tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/core/postoffice/impl/WildcardAddressManagerUnitTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,11 @@ static class BindingsFake implements Bindings {
493493
this.name = address;
494494
}
495495

496+
@Override
497+
public boolean hasLocalBinding() {
498+
return false;
499+
}
500+
496501
@Override
497502
public Collection<Binding> getBindings() {
498503
return bindings.values();

0 commit comments

Comments
 (0)
Please sign in to comment.