Skip to content

Commit

Permalink
[Dubbo-6034] Fix the RegistyUrl mismatching to ZoneAwareClusterInvoke…
Browse files Browse the repository at this point in the history
…r.PREFER_REGISTRY_KEY (apache#7788)

* fix: mismatch to ZoneAwareClusterInvoker.PREFER_REGISTRY_KEY

* fix: code style rule '.*' form

* feat: add unit test for PREFER_REGISTRY_KEY

Co-authored-by: plusman <liujianan@souche.com>
  • Loading branch information
plusmancn and plusman committed May 22, 2021
1 parent 8f7dc13 commit 7209ac3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.apache.dubbo.common.constants.CommonConstants.PREFERRED_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.LOADBALANCE_AMONG_REGISTRIES;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_ZONE;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_ZONE_FORCE;
import static org.apache.dubbo.common.constants.RegistryConstants.ZONE_KEY;
import static org.apache.dubbo.config.RegistryConfig.PREFER_REGISTRY_KEY;

/**
* When there're more than one registry for subscription.
Expand All @@ -58,9 +58,7 @@
public class ZoneAwareClusterInvoker<T> extends AbstractClusterInvoker<T> {

private static final Logger logger = LoggerFactory.getLogger(ZoneAwareClusterInvoker.class);

private static final String PREFER_REGISTRY_KEY = REGISTRY_KEY + "." + PREFERRED_KEY;


private static final String PREFER_REGISTRY_WITH_ZONE_KEY = REGISTRY_KEY + "." + ZONE_KEY;

private final LoadBalance loadBalanceAmongRegistries = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(LOADBALANCE_AMONG_REGISTRIES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import static org.apache.dubbo.common.constants.CommonConstants.EXTRA_KEYS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PREFERRED_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY;
import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY;
import static org.apache.dubbo.common.utils.PojoUtils.updatePropertyIfAbsent;
import static org.apache.dubbo.config.Constants.REGISTRIES_SUFFIX;
Expand All @@ -37,6 +39,7 @@
public class RegistryConfig extends AbstractConfig {

public static final String NO_AVAILABLE = "N/A";
public static final String PREFER_REGISTRY_KEY = REGISTRY_KEY + "." + PREFERRED_KEY;
private static final long serialVersionUID = 5508512956753757169L;

/**
Expand Down Expand Up @@ -486,6 +489,7 @@ public void setAccepts(String accepts) {
this.accepts = accepts;
}

@Parameter(key = PREFER_REGISTRY_KEY)
public Boolean getPreferred() {
return preferred;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package org.apache.dubbo.config;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.UrlUtils;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -26,6 +29,7 @@

import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_KEY;
import static org.apache.dubbo.config.Constants.SHUTDOWN_TIMEOUT_KEY;
import static org.apache.dubbo.config.RegistryConfig.PREFER_REGISTRY_KEY;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -186,5 +190,28 @@ public void testEquals() throws Exception {
registry2.setAddress("zookeeper://127.0.0.1:2183");
Assertions.assertNotEquals(registry1, registry2);
}

@Test
public void testPreferredWithTrueValue() {
RegistryConfig registry = new RegistryConfig();
registry.setPreferred(true);
Map<String, String> map = new HashMap<>();
// process Parameter annotation
AbstractConfig.appendParameters(map, registry);
// Simulate the check that ZoneAwareClusterInvoker#doInvoke do
URL url = UrlUtils.parseURL("zookeeper://127.0.0.1:2181", map);
Assertions.assertTrue(url.getParameter(PREFER_REGISTRY_KEY, false));
}

@Test
public void testPreferredWithFalseValue() {
RegistryConfig registry = new RegistryConfig();
registry.setPreferred(false);
Map<String, String> map = new HashMap<>();
// Process Parameter annotation
AbstractConfig.appendParameters(map, registry);
// Simulate the check that ZoneAwareClusterInvoker#doInvoke do
URL url = UrlUtils.parseURL("zookeeper://127.0.0.1:2181", map);
Assertions.assertFalse(url.getParameter(PREFER_REGISTRY_KEY, false));
}
}

0 comments on commit 7209ac3

Please sign in to comment.