fix router match condition (#12491)

* fix router match condition

* fix ut compilation

* add null check
This commit is contained in:
Ken Liu 2023-06-12 20:48:03 +08:00 committed by GitHub
parent bde608ec3c
commit dd94c1561a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 28 deletions

View File

@ -128,7 +128,7 @@ public abstract class AbstractConfigurator implements Configurator {
if (apiVersion != null && apiVersion.startsWith(RULE_VERSION_V30)) {
ConditionMatch matcher = (ConditionMatch) configuratorUrl.getAttribute(MATCH_CONDITION);
if (matcher != null) {
if (matcher.isMatch(url)) {
if (matcher.isMatch(host, url)) {
return doConfigure(url, configuratorUrl.removeParameters(conditionKeys));
} else {
logger.debug("Cannot apply configurator rule, param mismatch, current params are " + url + ", params in rule is " + matcher);

View File

@ -26,6 +26,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
public class ConditionMatch {
private AddressMatch address;
private AddressMatch providerAddress;
private ListStringMatch service;
private ListStringMatch app;
private List<ParamMatch> param;
@ -38,6 +39,14 @@ public class ConditionMatch {
this.address = address;
}
public AddressMatch getProviderAddress() {
return providerAddress;
}
public void setProviderAddress(AddressMatch providerAddress) {
this.providerAddress = providerAddress;
}
public ListStringMatch getService() {
return service;
}
@ -62,8 +71,12 @@ public class ConditionMatch {
this.param = param;
}
public boolean isMatch(URL url) {
if (getAddress() != null && !getAddress().isMatch(url.getAddress())) {
public boolean isMatch(String host, URL url) {
if (getAddress() != null && !getAddress().isMatch(host)) {
return false;
}
if (getProviderAddress() != null && !getProviderAddress().isMatch(url.getAddress())) {
return false;
}
@ -90,6 +103,7 @@ public class ConditionMatch {
public String toString() {
return "ConditionMatch{" +
"address='" + address + '\'' +
"providerAddress='" + providerAddress + '\'' +
", service='" + service + '\'' +
", app='" + app + '\'' +
", param='" + param + '\'' +

View File

@ -40,12 +40,12 @@ public class ParamMatch {
}
public boolean isMatch(URL url) {
if (key == null) {
if (key == null || value == null) {
return false;
}
String input = url.getParameter(key);
return input != null && value.isMatch(input);
return value.isMatch(input);
}
@Override

View File

@ -39,7 +39,7 @@ public class ParamMatch {
}
public boolean isMatch(String input) {
if (getValue() != null && input != null) {
if (getValue() != null) {
return getValue().isMatch(input);
}
return false;

View File

@ -187,11 +187,11 @@ class ConfigParserTest {
URL notMatchURL3 = URL.valueOf("dubbo://10.0.0.1:20880/DemoService?match_key1=value_not_match");// key not match
ConditionMatch matcher = (ConditionMatch) url.getAttribute(MATCH_CONDITION);
Assertions.assertTrue(matcher.isMatch(matchURL1));
Assertions.assertTrue(matcher.isMatch(matchURL2));
Assertions.assertFalse(matcher.isMatch(notMatchURL1));
Assertions.assertFalse(matcher.isMatch(notMatchURL2));
Assertions.assertFalse(matcher.isMatch(notMatchURL3));
Assertions.assertTrue(matcher.isMatch(matchURL1.getAddress(), matchURL1));
Assertions.assertTrue(matcher.isMatch(matchURL2.getAddress(), matchURL2));
Assertions.assertFalse(matcher.isMatch(notMatchURL1.getAddress(), notMatchURL1));
Assertions.assertFalse(matcher.isMatch(notMatchURL2.getAddress(), notMatchURL2));
Assertions.assertFalse(matcher.isMatch(notMatchURL3.getAddress(), notMatchURL3));
}
}
@ -211,8 +211,8 @@ class ConfigParserTest {
URL notMatchURL = URL.valueOf("dubbo://10.0.0.1:20880/DemoService?match_key1=value_not_match");// key not match
ConditionMatch matcher = (ConditionMatch) url.getAttribute(MATCH_CONDITION);
Assertions.assertTrue(matcher.isMatch(matchURL));
Assertions.assertFalse(matcher.isMatch(notMatchURL));
Assertions.assertTrue(matcher.isMatch(matchURL.getAddress(), matchURL));
Assertions.assertFalse(matcher.isMatch(notMatchURL.getAddress(), notMatchURL));
}
}
@ -232,8 +232,8 @@ class ConfigParserTest {
URL notMatchURL = URL.valueOf("dubbo://10.0.0.1:20880/DemoService?match_key1=value_not_match");// key not match
ConditionMatch matcher = (ConditionMatch) url.getAttribute(MATCH_CONDITION);
Assertions.assertTrue(matcher.isMatch(matchURL));
Assertions.assertFalse(matcher.isMatch(notMatchURL));
Assertions.assertTrue(matcher.isMatch(matchURL.getAddress(), matchURL));
Assertions.assertFalse(matcher.isMatch(notMatchURL.getAddress(), notMatchURL));
}
}

View File

@ -124,18 +124,20 @@ public abstract class AbstractServiceNameMapping implements ServiceNameMapping {
@Override
public MappingListener stopListen(URL subscribeURL, MappingListener listener) {
synchronized (mappingListeners) {
String mappingKey = ServiceNameMapping.buildMappingKey(subscribeURL);
Set<MappingListener> listeners = mappingListeners.get(mappingKey);
//todo, remove listener from remote metadata center
if (CollectionUtils.isNotEmpty(listeners)) {
listeners.remove(listener);
listener.stop();
removeListener(subscribeURL, listener);
}
if (CollectionUtils.isEmpty(listeners)) {
mappingListeners.remove(mappingKey);
removeCachedMapping(mappingKey);
removeMappingLock(mappingKey);
if (listener != null) {
String mappingKey = ServiceNameMapping.buildMappingKey(subscribeURL);
Set<MappingListener> listeners = mappingListeners.get(mappingKey);
//todo, remove listener from remote metadata center
if (CollectionUtils.isNotEmpty(listeners)) {
listeners.remove(listener);
listener.stop();
removeListener(subscribeURL, listener);
}
if (CollectionUtils.isEmpty(listeners)) {
mappingListeners.remove(mappingKey);
removeCachedMapping(mappingKey);
removeMappingLock(mappingKey);
}
}
return listener;
}

View File

@ -248,7 +248,9 @@ public class ServiceDiscoveryRegistry extends FailbackRegistry {
serviceDiscovery.unsubscribe(url, listener);
String protocolServiceKey = url.getProtocolServiceKey();
Set<String> serviceNames = serviceNameMapping.getMapping(url);
serviceNameMapping.stopListen(url, mappingListeners.remove(protocolServiceKey));
if (mappingListeners.get(protocolServiceKey) != null) {
serviceNameMapping.stopListen(url, mappingListeners.remove(protocolServiceKey));
}
if (CollectionUtils.isNotEmpty(serviceNames)) {
String serviceNamesKey = toStringKeys(serviceNames);
Lock appSubscriptionLock = getAppSubscription(serviceNamesKey);