From 8b56c0429336c31b3615dcf8823fa15de6644f47 Mon Sep 17 00:00:00 2001 From: Justin Bertram Date: Mon, 29 Aug 2022 21:01:29 -0500 Subject: [PATCH] ARTEMIS-3962 porting changes from AMQ-5281 Incorrect handling of unknown values in selectors. There is a slight semantic change here due to an error in the way we were handling null identifiers. This may require a change in selector syntax to use "IS NULL" or "IS NOT NULL" when using identifiers which may be null in the message being selected. This was the case for an internal filter used by the cluster connection bridge to select which cluster notification messages to consume. See https://issues.apache.org/jira/browse/AMQ-5281 for more details. --- .../selector/filter/ComparisonExpression.java | 3 + .../artemis/selector/MockMessage.java | 115 ++++++++++++ .../artemis/selector/SelectorTest.java | 96 ---------- .../selector/UnknownHandlingSelectorTest.java | 175 ++++++++++++++++++ .../cluster/impl/ClusterConnectionBridge.java | 24 +-- .../artemis/core/filter/impl/FilterTest.java | 6 + 6 files changed, 312 insertions(+), 107 deletions(-) create mode 100644 artemis-selector/src/test/java/org/apache/activemq/artemis/selector/MockMessage.java create mode 100644 artemis-selector/src/test/java/org/apache/activemq/artemis/selector/UnknownHandlingSelectorTest.java diff --git a/artemis-selector/src/main/java/org/apache/activemq/artemis/selector/filter/ComparisonExpression.java b/artemis-selector/src/main/java/org/apache/activemq/artemis/selector/filter/ComparisonExpression.java index 13f2f5b336..46254e6c41 100755 --- a/artemis-selector/src/main/java/org/apache/activemq/artemis/selector/filter/ComparisonExpression.java +++ b/artemis-selector/src/main/java/org/apache/activemq/artemis/selector/filter/ComparisonExpression.java @@ -221,6 +221,9 @@ public abstract class ComparisonExpression extends BinaryExpression implements B // Iff one of the values is null if (lv == null ^ rv == null) { + if (lv == null) { + return null; + } return Boolean.FALSE; } if (lv == rv || lv.equals(rv)) { diff --git a/artemis-selector/src/test/java/org/apache/activemq/artemis/selector/MockMessage.java b/artemis-selector/src/test/java/org/apache/activemq/artemis/selector/MockMessage.java new file mode 100644 index 0000000000..c70b6ad859 --- /dev/null +++ b/artemis-selector/src/test/java/org/apache/activemq/artemis/selector/MockMessage.java @@ -0,0 +1,115 @@ +/* + * 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.selector; + +import java.util.HashMap; + +import org.apache.activemq.artemis.api.core.SimpleString; +import org.apache.activemq.artemis.selector.filter.FilterException; +import org.apache.activemq.artemis.selector.filter.Filterable; + +public class MockMessage implements Filterable { + + HashMap properties = new HashMap<>(); + private String text; + private Object destination; + private String messageId; + private String type; + private Object localConnectionId; + + public void setDestination(Object destination) { + this.destination = destination; + } + + public void setJMSMessageID(String messageId) { + this.messageId = messageId; + } + + public void setJMSType(String type) { + this.type = type; + } + + public void setText(String text) { + this.text = text; + } + + public void setBooleanProperty(String key, boolean value) { + properties.put(key, value); + } + + public void setStringProperty(String key, String value) { + properties.put(key, value); + } + + public void setByteProperty(String key, byte value) { + properties.put(key, value); + } + + public void setDoubleProperty(String key, double value) { + properties.put(key, value); + } + + public void setFloatProperty(String key, float value) { + properties.put(key, value); + } + + public void setLongProperty(String key, long value) { + properties.put(key, value); + } + + public void setIntProperty(String key, int value) { + properties.put(key, value); + } + + public void setShortProperty(String key, short value) { + properties.put(key, value); + } + + public void setObjectProperty(String key, Object value) { + properties.put(key, value); + } + + @Override + public T getBodyAs(Class type) throws FilterException { + if (type == String.class) { + return type.cast(text); + } + return null; + } + + @Override + public Object getProperty(SimpleString name) { + String stringName = name.toString(); + if ("JMSType".equals(stringName)) { + return type; + } + if ("JMSMessageID".equals(stringName)) { + return messageId; + } + return properties.get(stringName); + } + + public Object getDestination() { + return destination; + } + + @Override + public Object getLocalConnectionId() { + return localConnectionId; + } + +} \ No newline at end of file diff --git a/artemis-selector/src/test/java/org/apache/activemq/artemis/selector/SelectorTest.java b/artemis-selector/src/test/java/org/apache/activemq/artemis/selector/SelectorTest.java index a177c1f213..9e2f5303a3 100755 --- a/artemis-selector/src/test/java/org/apache/activemq/artemis/selector/SelectorTest.java +++ b/artemis-selector/src/test/java/org/apache/activemq/artemis/selector/SelectorTest.java @@ -16,110 +16,14 @@ */ package org.apache.activemq.artemis.selector; -import java.util.HashMap; - -import org.apache.activemq.artemis.api.core.SimpleString; import org.apache.activemq.artemis.selector.filter.BooleanExpression; import org.apache.activemq.artemis.selector.filter.FilterException; -import org.apache.activemq.artemis.selector.filter.Filterable; import org.apache.activemq.artemis.selector.impl.SelectorParser; import org.junit.Assert; import org.junit.Test; public class SelectorTest { - class MockMessage implements Filterable { - - HashMap properties = new HashMap<>(); - private String text; - private Object destination; - private String messageId; - private String type; - private Object localConnectionId; - - public void setDestination(Object destination) { - this.destination = destination; - } - - public void setJMSMessageID(String messageId) { - this.messageId = messageId; - } - - public void setJMSType(String type) { - this.type = type; - } - - public void setText(String text) { - this.text = text; - } - - public void setBooleanProperty(String key, boolean value) { - properties.put(key, value); - } - - public void setStringProperty(String key, String value) { - properties.put(key, value); - } - - public void setByteProperty(String key, byte value) { - properties.put(key, value); - } - - public void setDoubleProperty(String key, double value) { - properties.put(key, value); - } - - public void setFloatProperty(String key, float value) { - properties.put(key, value); - } - - public void setLongProperty(String key, long value) { - properties.put(key, value); - } - - public void setIntProperty(String key, int value) { - properties.put(key, value); - } - - public void setShortProperty(String key, short value) { - properties.put(key, value); - } - - public void setObjectProperty(String key, Object value) { - properties.put(key, value); - } - - @Override - public T getBodyAs(Class type) throws FilterException { - if (type == String.class) { - return type.cast(text); - } - return null; - } - - @Override - public Object getProperty(SimpleString name) { - String stringName = name.toString(); - if ("JMSType".equals(stringName)) { - return type; - } - if ("JMSMessageID".equals(stringName)) { - return messageId; - } - return properties.get(stringName); - } - - public Object getDestination() { - return destination; - } - - @Override - public Object getLocalConnectionId() { - return localConnectionId; - } - - } - @Test public void testBooleanSelector() throws Exception { MockMessage message = createMessage(); diff --git a/artemis-selector/src/test/java/org/apache/activemq/artemis/selector/UnknownHandlingSelectorTest.java b/artemis-selector/src/test/java/org/apache/activemq/artemis/selector/UnknownHandlingSelectorTest.java new file mode 100644 index 0000000000..4db3283019 --- /dev/null +++ b/artemis-selector/src/test/java/org/apache/activemq/artemis/selector/UnknownHandlingSelectorTest.java @@ -0,0 +1,175 @@ +/** + * 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.selector; + +import org.apache.activemq.artemis.selector.filter.BooleanExpression; +import org.apache.activemq.artemis.selector.filter.FilterException; +import org.apache.activemq.artemis.selector.impl.SelectorParser; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class UnknownHandlingSelectorTest { + + private MockMessage message; + + @Before + public void setUp() throws Exception { + message = new MockMessage(); + message.setDestination("FOO.BAR"); + message.setJMSType("selector-test"); + message.setJMSMessageID("connection:1:1:1:1"); + message.setBooleanProperty("trueProp", true); + message.setBooleanProperty("falseProp", false); + message.setObjectProperty("nullProp", null); + } + + /** + * | NOT + * +------+------ + * | T | F + * | F | T + * | U | U + * +------+------- + */ + @Test + public void notEvaluation() throws Exception { + assertSelector("not(trueProp)", false); + assertSelector("not(falseProp)", true); + assertSelector("not(unknownProp)", false); + } + + /** + * | AND | T | F | U + * +------+-------+-------+------- + * | T | T | F | U + * | F | F | F | F + * | U | U | F | U + * +------+-------+-------+------- + */ + @Test + public void andEvaluation() throws Exception { + assertSelectorEvaluatesToTrue("trueProp AND trueProp"); + assertSelectorEvaluatesToFalse("trueProp AND falseProp"); + assertSelectorEvaluatesToFalse("falseProp AND trueProp"); + assertSelectorEvaluatesToFalse("falseProp AND falseProp"); + assertSelectorEvaluatesToFalse("falseProp AND unknownProp"); + assertSelectorEvaluatesToFalse("unknownProp AND falseProp"); + assertSelectorEvaluatesToUnknown("trueProp AND unknownProp"); + assertSelectorEvaluatesToUnknown("unknownProp AND trueProp"); + assertSelectorEvaluatesToUnknown("unknownProp AND unknownProp"); + } + + /** + * | OR | T | F | U + * +------+-------+-------+-------- + * | T | T | T | T + * | F | T | F | U + * | U | T | U | U + * +------+-------+-------+------- + */ + @Test + public void orEvaluation() throws Exception { + assertSelectorEvaluatesToTrue("trueProp OR trueProp"); + assertSelectorEvaluatesToTrue("trueProp OR falseProp"); + assertSelectorEvaluatesToTrue("falseProp OR trueProp"); + assertSelectorEvaluatesToTrue("trueProp OR unknownProp"); + assertSelectorEvaluatesToTrue("unknownProp OR trueProp"); + assertSelectorEvaluatesToFalse("falseProp OR falseProp"); + assertSelectorEvaluatesToUnknown("falseProp OR unknownProp"); + assertSelectorEvaluatesToUnknown("unknownProp OR falseProp"); + assertSelectorEvaluatesToUnknown("unknownProp OR unknownProp"); + } + + @Test + public void comparisonWithUnknownShouldEvaluateToUnknown() throws Exception { + assertSelectorEvaluatesToUnknown("unknownProp = 0"); + assertSelectorEvaluatesToUnknown("unknownProp > 0"); + assertSelectorEvaluatesToUnknown("unknownProp >= 0"); + assertSelectorEvaluatesToUnknown("unknownProp < 0"); + assertSelectorEvaluatesToUnknown("unknownProp <= 0"); + assertSelectorEvaluatesToUnknown("unknownProp <> 0"); + assertSelectorEvaluatesToUnknown("unknownProp LIKE 'zero'"); + assertSelectorEvaluatesToUnknown("unknownProp NOT LIKE 'zero'"); + assertSelectorEvaluatesToUnknown("unknownProp IN ('zero')"); + assertSelectorEvaluatesToUnknown("unknownProp NOT IN ('zero')"); + assertSelectorEvaluatesToUnknown("unknownProp BETWEEN 1 AND 2"); + assertSelectorEvaluatesToUnknown("unknownProp NOT BETWEEN 1 AND 2"); + } + + @Test + public void comparisonWithNullPropShouldEvaluateToUnknown() throws Exception { + assertSelectorEvaluatesToUnknown("nullProp = 0"); + assertSelectorEvaluatesToUnknown("nullProp > 0"); + assertSelectorEvaluatesToUnknown("nullProp >= 0"); + assertSelectorEvaluatesToUnknown("nullProp < 0"); + assertSelectorEvaluatesToUnknown("nullProp <= 0"); + assertSelectorEvaluatesToUnknown("nullProp <> 0"); + assertSelectorEvaluatesToUnknown("nullProp LIKE 'zero'"); + assertSelectorEvaluatesToUnknown("nullProp NOT LIKE 'zero'"); + assertSelectorEvaluatesToUnknown("nullProp IN ('zero')"); + assertSelectorEvaluatesToUnknown("nullProp NOT IN ('zero')"); + assertSelectorEvaluatesToUnknown("nullProp BETWEEN 1 AND 2"); + assertSelectorEvaluatesToUnknown("nullProp NOT BETWEEN 1 AND 2"); + } + + @Test + public void isNullIsNotNull() throws Exception { + assertSelectorEvaluatesToTrue("unknownProp IS NULL"); + assertSelectorEvaluatesToTrue("nullProp IS NULL"); + assertSelectorEvaluatesToFalse("trueProp IS NULL"); + assertSelectorEvaluatesToFalse("unknownProp IS NOT NULL"); + assertSelectorEvaluatesToFalse("nullProp IS NOT NULL"); + assertSelectorEvaluatesToTrue("trueProp IS NOT NULL"); + } + + @Test + public void arithmeticWithNull() throws Exception { + assertSelectorEvaluatesToUnknown("-unknownProp = 0"); + assertSelectorEvaluatesToUnknown("+unknownProp = 0"); + assertSelectorEvaluatesToUnknown("unknownProp * 2 = 0"); + assertSelectorEvaluatesToUnknown("unknownProp / 2 = 0"); + assertSelectorEvaluatesToUnknown("unknownProp + 2 = 0"); + assertSelectorEvaluatesToUnknown("unknownProp - 2 = 0"); + } + + protected void assertSelectorEvaluatesToUnknown(String selector) throws FilterException { + assertSelector(selector, false); + assertSelector(not(selector), false); + } + + protected void assertSelectorEvaluatesToTrue(String selector) throws FilterException { + assertSelector(selector, true); + assertSelector(not(selector), false); + } + + protected void assertSelectorEvaluatesToFalse(String selector) throws FilterException { + assertSelector(selector, false); + assertSelector(not(selector), true); + } + + protected void assertSelector(String text, boolean expected) throws FilterException { + BooleanExpression selector = SelectorParser.parse(text); + Assert.assertTrue("Created a valid selector", selector != null); + boolean value = selector.matches(message); + Assert.assertEquals("Selector for: " + text, expected, value); + } + + private static String not(String selector) { + return "not(" + selector + ")"; + } +} \ No newline at end of file diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/cluster/impl/ClusterConnectionBridge.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/cluster/impl/ClusterConnectionBridge.java index 9df10c244b..96affe3278 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/cluster/impl/ClusterConnectionBridge.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/cluster/impl/ClusterConnectionBridge.java @@ -251,29 +251,31 @@ public class ClusterConnectionBridge extends BridgeImpl { SimpleString notifQueueName = new SimpleString(qName); - SimpleString filter = new SimpleString(ManagementHelper.HDR_BINDING_TYPE + "<>" + - BindingType.DIVERT.toInt() + + SimpleString filter = new SimpleString("(" + ManagementHelper.HDR_BINDING_TYPE + " <> " + BindingType.DIVERT.toInt() + + " OR " + + ManagementHelper.HDR_BINDING_TYPE + " IS NULL)" + " AND " + ManagementHelper.HDR_NOTIFICATION_TYPE + " IN ('" + CoreNotificationType.SESSION_CREATED + - "','" + + "', '" + CoreNotificationType.BINDING_ADDED + - "','" + + "', '" + CoreNotificationType.BINDING_REMOVED + - "','" + + "', '" + CoreNotificationType.CONSUMER_CREATED + - "','" + + "', '" + CoreNotificationType.CONSUMER_CLOSED + - "','" + + "', '" + CoreNotificationType.PROPOSAL + - "','" + + "', '" + CoreNotificationType.PROPOSAL_RESPONSE + - "','" + + "', '" + CoreNotificationType.UNPROPOSAL + - "') AND " + + "')" + + " AND " + ManagementHelper.HDR_DISTANCE + - "<" + + " < " + flowRecord.getMaxHops() + " AND (" + createSelectorFromAddress(appendIgnoresToFilter(flowRecord.getAddress())) + diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/filter/impl/FilterTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/filter/impl/FilterTest.java index 394aa7d983..6dbf2f1e68 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/core/filter/impl/FilterTest.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/filter/impl/FilterTest.java @@ -170,9 +170,15 @@ public class FilterTest extends SilentTestCase { @Test public void testDifferentNullString() throws Exception { filter = FilterImpl.createFilter(new SimpleString("prop <> 'foo'")); + Assert.assertFalse(filter.match(message)); + + filter = FilterImpl.createFilter(new SimpleString("prop <> 'foo' OR prop IS NULL")); Assert.assertTrue(filter.match(message)); filter = FilterImpl.createFilter(new SimpleString("NOT (prop = 'foo')")); + Assert.assertFalse(filter.match(message)); + + filter = FilterImpl.createFilter(new SimpleString("NOT (prop = 'foo') OR prop IS NULL")); Assert.assertTrue(filter.match(message)); filter = FilterImpl.createFilter(new SimpleString("prop <> 'foo'"));