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.
This commit is contained in:
Justin Bertram 2022-08-29 21:01:29 -05:00
parent f11b96e7ed
commit 8b56c04293
No known key found for this signature in database
GPG Key ID: F41830B875BB8633
6 changed files with 312 additions and 107 deletions

View File

@ -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)) {

View File

@ -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<String, Object> 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> T getBodyAs(Class<T> 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;
}
}

View File

@ -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<String, Object> 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> T getBodyAs(Class<T> 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();

View File

@ -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 + ")";
}
}

View File

@ -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())) +

View File

@ -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'"));