Skip to content

Commit

Permalink
[DUBBO-3137]: step3 - start using CommonConstants (#4030)
Browse files Browse the repository at this point in the history
* [DUBBO-3137]: start to use org.apache.dubbo.common.constants.CommonConstants

* remove useless imports

* remove useless imports

* remove unused imports

* remove unused imports

* remove unused imports

* remove unused imports

* remove unused imports

* use static import
  • Loading branch information
beiwei30 authored and CrazyHZM committed May 12, 2019
1 parent 6a3f4c7 commit fe3d345
Show file tree
Hide file tree
Showing 199 changed files with 1,500 additions and 1,089 deletions.
Expand Up @@ -28,6 +28,8 @@
import java.util.Map;
import java.util.Optional;

import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY;

/**
* Configurator. (SPI, Prototype, ThreadSafe)
*
Expand Down Expand Up @@ -82,7 +84,7 @@ static Optional<List<Configurator>> toConfigurators(List<URL> urls) {
}
Map<String, String> override = new HashMap<>(url.getParameters());
//The anyhost parameter of override may be added automatically, it can't change the judgement of changing url
override.remove(Constants.ANYHOST_KEY);
override.remove(ANYHOST_KEY);
if (override.size() == 0) {
configurators.clear();
continue;
Expand Down
Expand Up @@ -27,6 +27,16 @@
import java.util.Map;
import java.util.Set;

import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER;
import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;

/**
* AbstractOverrideConfigurator
*/
Expand All @@ -49,19 +59,19 @@ public URL getUrl() {
@Override
public URL configure(URL url) {
// If override url is not enabled or is invalid, just return.
if (!configuratorUrl.getParameter(Constants.ENABLED_KEY, true) || configuratorUrl.getHost() == null || url == null || url.getHost() == null) {
if (!configuratorUrl.getParameter(ENABLED_KEY, true) || configuratorUrl.getHost() == null || url == null || url.getHost() == null) {
return url;
}
/**
* This if branch is created since 2.7.0.
*/
String apiVersion = configuratorUrl.getParameter(Constants.CONFIG_VERSION_KEY);
if (StringUtils.isNotEmpty(apiVersion)) {
String currentSide = url.getParameter(Constants.SIDE_KEY);
String configuratorSide = configuratorUrl.getParameter(Constants.SIDE_KEY);
if (currentSide.equals(configuratorSide) && Constants.CONSUMER.equals(configuratorSide) && 0 == configuratorUrl.getPort()) {
String currentSide = url.getParameter(SIDE_KEY);
String configuratorSide = configuratorUrl.getParameter(SIDE_KEY);
if (currentSide.equals(configuratorSide) && CONSUMER.equals(configuratorSide) && 0 == configuratorUrl.getPort()) {
url = configureIfMatch(NetUtils.getLocalHost(), url);
} else if (currentSide.equals(configuratorSide) && Constants.PROVIDER.equals(configuratorSide) && url.getPort() == configuratorUrl.getPort()) {
} else if (currentSide.equals(configuratorSide) && PROVIDER.equals(configuratorSide) && url.getPort() == configuratorUrl.getPort()) {
url = configureIfMatch(url.getHost(), url);
}
}
Expand All @@ -84,41 +94,41 @@ private URL configureDeprecated(URL url) {
} else {// override url don't have a port, means the ip override url specify is a consumer address or 0.0.0.0
// 1.If it is a consumer ip address, the intention is to control a specific consumer instance, it must takes effect at the consumer side, any provider received this override url should ignore;
// 2.If the ip is 0.0.0.0, this override url can be used on consumer, and also can be used on provider
if (url.getParameter(Constants.SIDE_KEY, Constants.PROVIDER).equals(Constants.CONSUMER)) {
if (url.getParameter(SIDE_KEY, PROVIDER).equals(CONSUMER)) {
return configureIfMatch(NetUtils.getLocalHost(), url);// NetUtils.getLocalHost is the ip address consumer registered to registry.
} else if (url.getParameter(Constants.SIDE_KEY, Constants.CONSUMER).equals(Constants.PROVIDER)) {
return configureIfMatch(Constants.ANYHOST_VALUE, url);// take effect on all providers, so address must be 0.0.0.0, otherwise it won't flow to this if branch
} else if (url.getParameter(SIDE_KEY, CONSUMER).equals(PROVIDER)) {
return configureIfMatch(ANYHOST_VALUE, url);// take effect on all providers, so address must be 0.0.0.0, otherwise it won't flow to this if branch
}
}
return url;
}

private URL configureIfMatch(String host, URL url) {
if (Constants.ANYHOST_VALUE.equals(configuratorUrl.getHost()) || host.equals(configuratorUrl.getHost())) {
if (ANYHOST_VALUE.equals(configuratorUrl.getHost()) || host.equals(configuratorUrl.getHost())) {
// TODO, to support wildcards
String providers = configuratorUrl.getParameter(Constants.OVERRIDE_PROVIDERS_KEY);
if (StringUtils.isEmpty(providers) || providers.contains(url.getAddress()) || providers.contains(Constants.ANYHOST_VALUE)) {
String configApplication = configuratorUrl.getParameter(Constants.APPLICATION_KEY,
if (StringUtils.isEmpty(providers) || providers.contains(url.getAddress()) || providers.contains(ANYHOST_VALUE)) {
String configApplication = configuratorUrl.getParameter(APPLICATION_KEY,
configuratorUrl.getUsername());
String currentApplication = url.getParameter(Constants.APPLICATION_KEY, url.getUsername());
if (configApplication == null || Constants.ANY_VALUE.equals(configApplication)
String currentApplication = url.getParameter(APPLICATION_KEY, url.getUsername());
if (configApplication == null || ANY_VALUE.equals(configApplication)
|| configApplication.equals(currentApplication)) {
Set<String> conditionKeys = new HashSet<String>();
conditionKeys.add(Constants.CATEGORY_KEY);
conditionKeys.add(RemotingConstants.CHECK_KEY);
conditionKeys.add(Constants.DYNAMIC_KEY);
conditionKeys.add(Constants.ENABLED_KEY);
conditionKeys.add(Constants.GROUP_KEY);
conditionKeys.add(Constants.VERSION_KEY);
conditionKeys.add(Constants.APPLICATION_KEY);
conditionKeys.add(Constants.SIDE_KEY);
conditionKeys.add(ENABLED_KEY);
conditionKeys.add(GROUP_KEY);
conditionKeys.add(VERSION_KEY);
conditionKeys.add(APPLICATION_KEY);
conditionKeys.add(SIDE_KEY);
conditionKeys.add(Constants.CONFIG_VERSION_KEY);
for (Map.Entry<String, String> entry : configuratorUrl.getParameters().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key.startsWith("~") || Constants.APPLICATION_KEY.equals(key) || Constants.SIDE_KEY.equals(key)) {
if (key.startsWith("~") || APPLICATION_KEY.equals(key) || SIDE_KEY.equals(key)) {
conditionKeys.add(key);
if (value != null && !Constants.ANY_VALUE.equals(value)
if (value != null && !ANY_VALUE.equals(value)
&& !value.equals(url.getParameter(key.startsWith("~") ? key.substring(1) : key))) {
return url;
}
Expand Down
Expand Up @@ -31,6 +31,8 @@
import java.util.List;
import java.util.Map;

import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;

/**
* Config parser
*/
Expand Down Expand Up @@ -194,7 +196,7 @@ private static List<String> parseAddresses(ConfigItem item) {
addresses = new ArrayList<>();
}
if (addresses.size() == 0) {
addresses.add(Constants.ANYHOST_VALUE);
addresses.add(ANYHOST_VALUE);
}
return addresses;
}
Expand Down
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.dubbo.rpc.cluster.loadbalance;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
Expand All @@ -31,6 +30,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN;

/**
* ConsistentHashLoadBalance
*/
Expand Down Expand Up @@ -78,7 +79,7 @@ private static final class ConsistentHashSelector<T> {
this.identityHashCode = identityHashCode;
URL url = invokers.get(0).getUrl();
this.replicaNumber = url.getMethodParameter(methodName, HASH_NODES, 160);
String[] index = Constants.COMMA_SPLIT_PATTERN.split(url.getMethodParameter(methodName, HASH_ARGUMENTS, "0"));
String[] index = COMMA_SPLIT_PATTERN.split(url.getMethodParameter(methodName, HASH_ARGUMENTS, "0"));
argumentIndex = new int[index.length];
for (int i = 0; i < index.length; i++) {
argumentIndex[i] = Integer.parseInt(index[i]);
Expand Down
Expand Up @@ -39,6 +39,11 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.dubbo.common.constants.CommonConstants.METHOD_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY_PREFIX;
import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY;

/**
* ConditionRouter
*
Expand All @@ -63,7 +68,7 @@ public ConditionRouter(URL url) {
this.url = url;
this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
this.force = url.getParameter(Constants.FORCE_KEY, false);
this.enabled = url.getParameter(Constants.ENABLED_KEY, true);
this.enabled = url.getParameter(ENABLED_KEY, true);
init(url.getParameterAndDecoded(Constants.RULE_KEY));
}

Expand Down Expand Up @@ -219,7 +224,7 @@ private boolean matchCondition(Map<String, MatchPair> condition, URL url, URL pa
String key = matchPair.getKey();
String sampleValue;
//get real invoked method name from invocation
if (invocation != null && (Constants.METHOD_KEY.equals(key) || Constants.METHODS_KEY.equals(key))) {
if (invocation != null && (METHOD_KEY.equals(key) || METHODS_KEY.equals(key))) {
sampleValue = invocation.getMethodName();
} else if (Constants.ADDRESS_KEY.equals(key)) {
sampleValue = url.getAddress();
Expand All @@ -228,7 +233,7 @@ private boolean matchCondition(Map<String, MatchPair> condition, URL url, URL pa
} else {
sampleValue = sample.get(key);
if (sampleValue == null) {
sampleValue = sample.get(Constants.DEFAULT_KEY_PREFIX + key);
sampleValue = sample.get(DEFAULT_KEY_PREFIX + key);
}
}
if (sampleValue != null) {
Expand Down
Expand Up @@ -16,8 +16,8 @@
*/
package org.apache.dubbo.rpc.cluster.router.condition.config;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.configcenter.DynamicConfiguration;

/**
Expand All @@ -31,7 +31,7 @@ public class AppRouter extends ListenableRouter {
private static final int APP_ROUTER_DEFAULT_PRIORITY = 150;

public AppRouter(DynamicConfiguration configuration, URL url) {
super(configuration, url, url.getParameter(Constants.APPLICATION_KEY));
super(configuration, url, url.getParameter(CommonConstants.APPLICATION_KEY));
this.priority = APP_ROUTER_DEFAULT_PRIORITY;
}
}
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.CollectionUtils;
Expand Down Expand Up @@ -232,7 +233,7 @@ public <T> void notify(List<Invoker<T>> invokers) {

Invoker<T> invoker = invokers.get(0);
URL url = invoker.getUrl();
String providerApplication = url.getParameter(Constants.REMOTE_APPLICATION_KEY);
String providerApplication = url.getParameter(CommonConstants.REMOTE_APPLICATION_KEY);

if (StringUtils.isEmpty(providerApplication)) {
logger.error("TagRouter must getConfig from or subscribe to a specific application, but the application " +
Expand Down
Expand Up @@ -26,6 +26,21 @@
import java.util.Map;
import java.util.Set;

import static org.apache.dubbo.common.constants.CommonConstants.ALIVE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.CORE_THREADS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY_PREFIX;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.QUEUES_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.RELEASE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.THREADPOOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.THREADS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.THREAD_NAME_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;

/**
* ClusterUtils
*/
Expand All @@ -42,29 +57,29 @@ public static URL mergeUrl(URL remoteUrl, Map<String, String> localMap) {
map.putAll(remoteMap);

// Remove configurations from provider, some items should be affected by provider.
map.remove(Constants.THREAD_NAME_KEY);
map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY);
map.remove(THREAD_NAME_KEY);
map.remove(DEFAULT_KEY_PREFIX + THREAD_NAME_KEY);

map.remove(Constants.THREADPOOL_KEY);
map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.THREADPOOL_KEY);
map.remove(THREADPOOL_KEY);
map.remove(DEFAULT_KEY_PREFIX + THREADPOOL_KEY);

map.remove(Constants.CORE_THREADS_KEY);
map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.CORE_THREADS_KEY);
map.remove(CORE_THREADS_KEY);
map.remove(DEFAULT_KEY_PREFIX + CORE_THREADS_KEY);

map.remove(Constants.THREADS_KEY);
map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.THREADS_KEY);
map.remove(THREADS_KEY);
map.remove(DEFAULT_KEY_PREFIX + THREADS_KEY);

map.remove(Constants.QUEUES_KEY);
map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.QUEUES_KEY);
map.remove(QUEUES_KEY);
map.remove(DEFAULT_KEY_PREFIX + QUEUES_KEY);

map.remove(Constants.ALIVE_KEY);
map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.ALIVE_KEY);
map.remove(ALIVE_KEY);
map.remove(DEFAULT_KEY_PREFIX + ALIVE_KEY);

map.remove(RemotingConstants.TRANSPORTER_KEY);
map.remove(Constants.DEFAULT_KEY_PREFIX + RemotingConstants.TRANSPORTER_KEY);
map.remove(DEFAULT_KEY_PREFIX + RemotingConstants.TRANSPORTER_KEY);

map.remove(Constants.ASYNC_KEY);
map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.ASYNC_KEY);
map.remove(DEFAULT_KEY_PREFIX + Constants.ASYNC_KEY);

// remove method async entry.
Set<String> methodAsyncKey = new HashSet<>();
Expand All @@ -82,28 +97,28 @@ public static URL mergeUrl(URL remoteUrl, Map<String, String> localMap) {
// All providers come to here have been filtered by group, which means only those providers that have the exact same group value with the consumer could come to here.
// So, generally, we don't need to care about the group value here.
// But when comes to group merger, there is an exception, the consumer group may be '*' while the provider group can be empty or any other values.
String remoteGroup = map.get(Constants.GROUP_KEY);
String remoteRelease = map.get(Constants.RELEASE_KEY);
String remoteGroup = map.get(GROUP_KEY);
String remoteRelease = map.get(RELEASE_KEY);
map.putAll(localMap);
if (StringUtils.isNotEmpty(remoteGroup)) {
map.put(Constants.GROUP_KEY, remoteGroup);
map.put(GROUP_KEY, remoteGroup);
}
// we should always keep the Provider RELEASE_KEY not overrode by the the value on Consumer side.
map.remove(Constants.RELEASE_KEY);
map.remove(RELEASE_KEY);
if (StringUtils.isNotEmpty(remoteRelease)) {
map.put(Constants.RELEASE_KEY, remoteRelease);
map.put(RELEASE_KEY, remoteRelease);
}
}
if (remoteMap != null && remoteMap.size() > 0) {
// Use version passed from provider side
reserveRemoteValue(Constants.DUBBO_VERSION_KEY, map, remoteMap);
reserveRemoteValue(Constants.VERSION_KEY, map, remoteMap);
reserveRemoteValue(Constants.METHODS_KEY, map, remoteMap);
reserveRemoteValue(Constants.TIMESTAMP_KEY, map, remoteMap);
reserveRemoteValue(VERSION_KEY, map, remoteMap);
reserveRemoteValue(METHODS_KEY, map, remoteMap);
reserveRemoteValue(TIMESTAMP_KEY, map, remoteMap);
reserveRemoteValue(Constants.TAG_KEY, map, remoteMap);
// TODO, for compatibility consideration, we cannot simply change the value behind APPLICATION_KEY from Consumer to Provider. So just add an extra key here.
// Reserve application name from provider.
map.put(Constants.REMOTE_APPLICATION_KEY, remoteMap.get(Constants.APPLICATION_KEY));
map.put(REMOTE_APPLICATION_KEY, remoteMap.get(APPLICATION_KEY));

// Combine filters and listeners on Provider and Consumer
String remoteFilter = remoteMap.get(Constants.REFERENCE_FILTER_KEY);
Expand All @@ -130,4 +145,4 @@ private static void reserveRemoteValue(String key, Map<String, String> map, Map<
}
}

}
}
Expand Up @@ -35,6 +35,9 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT;
import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY;

/**
* Invoke a specific number of invokers concurrently, usually used for demanding real-time operations, but need to waste more service resources.
*
Expand All @@ -60,7 +63,7 @@ public Result doInvoke(final Invocation invocation, List<Invoker<T>> invokers, L
checkInvokers(invokers, invocation);
final List<Invoker<T>> selected;
final int forks = getUrl().getParameter(Constants.FORKS_KEY, Constants.DEFAULT_FORKS);
final int timeout = getUrl().getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
final int timeout = getUrl().getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
if (forks <= 0 || forks >= invokers.size()) {
selected = invokers;
} else {
Expand Down

0 comments on commit fe3d345

Please sign in to comment.