Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DUBBO-3137]: step3 - start to use ClusterConstants and MonitorConstants #4045

Merged
merged 1 commit into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.dubbo.rpc.cluster;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.CollectionUtils;
Expand All @@ -28,6 +27,7 @@
import java.util.Map;
import java.util.Optional;

import static org.apache.dubbo.common.constants.ClusterConstants.PRIORITY_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;

Expand Down Expand Up @@ -110,8 +110,8 @@ default int compareTo(Configurator o) {
int ipCompare = getUrl().getHost().compareTo(o.getUrl().getHost());
// host is the same, sort by priority
if (ipCompare == 0) {
int i = getUrl().getParameter(Constants.PRIORITY_KEY, 0);
int j = o.getUrl().getParameter(Constants.PRIORITY_KEY, 0);
int i = getUrl().getParameter(PRIORITY_KEY, 0);
int j = o.getUrl().getParameter(PRIORITY_KEY, 0);
return Integer.compare(i, j);
} else {
return ipCompare;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.dubbo.rpc.cluster.configurator;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.RemotingConstants;
import org.apache.dubbo.common.utils.NetUtils;
Expand All @@ -27,6 +26,8 @@
import java.util.Map;
import java.util.Set;

import static org.apache.dubbo.common.constants.ClusterConstants.CONFIG_VERSION_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.OVERRIDE_PROVIDERS_KEY;
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;
Expand Down Expand Up @@ -67,7 +68,7 @@ public URL configure(URL url) {
/**
* This if branch is created since 2.7.0.
*/
String apiVersion = configuratorUrl.getParameter(Constants.CONFIG_VERSION_KEY);
String apiVersion = configuratorUrl.getParameter(CONFIG_VERSION_KEY);
if (StringUtils.isNotEmpty(apiVersion)) {
String currentSide = url.getParameter(SIDE_KEY);
String configuratorSide = configuratorUrl.getParameter(SIDE_KEY);
Expand Down Expand Up @@ -108,7 +109,7 @@ private URL configureDeprecated(URL url) {
private URL configureIfMatch(String host, URL url) {
if (ANYHOST_VALUE.equals(configuratorUrl.getHost()) || host.equals(configuratorUrl.getHost())) {
// TODO, to support wildcards
String providers = configuratorUrl.getParameter(Constants.OVERRIDE_PROVIDERS_KEY);
String providers = configuratorUrl.getParameter(OVERRIDE_PROVIDERS_KEY);
if (StringUtils.isEmpty(providers) || providers.contains(url.getAddress()) || providers.contains(ANYHOST_VALUE)) {
String configApplication = configuratorUrl.getParameter(APPLICATION_KEY,
configuratorUrl.getUsername());
Expand All @@ -124,7 +125,7 @@ private URL configureIfMatch(String host, URL url) {
conditionKeys.add(VERSION_KEY);
conditionKeys.add(APPLICATION_KEY);
conditionKeys.add(SIDE_KEY);
conditionKeys.add(Constants.CONFIG_VERSION_KEY);
conditionKeys.add(CONFIG_VERSION_KEY);
for (Map.Entry<String, String> entry : configuratorUrl.getParameters().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.dubbo.rpc.cluster.configurator.parser;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.StringUtils;
Expand All @@ -31,6 +30,7 @@
import java.util.List;
import java.util.Map;

import static org.apache.dubbo.common.constants.ClusterConstants.OVERRIDE_PROVIDERS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
import static org.apache.dubbo.common.constants.RegistryConstants.APP_DYNAMIC_CONFIGURATORS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_CONFIGURATORS_CATEGORY;
Expand Down Expand Up @@ -148,7 +148,7 @@ private static String toParameterString(ConfigItem item) {

if (CollectionUtils.isNotEmpty(item.getProviderAddresses())) {
sb.append("&");
sb.append(Constants.OVERRIDE_PROVIDERS_KEY);
sb.append(OVERRIDE_PROVIDERS_KEY);
sb.append("=");
sb.append(CollectionUtils.join(item.getProviderAddresses(), ","));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Map;

import static org.apache.dubbo.common.constants.MonitorConstants.MONITOR_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PROTOCOL;

/**
Expand Down Expand Up @@ -66,7 +67,7 @@ public AbstractDirectory(URL url, URL consumerUrl, RouterChain<T> routerChain) {

if (url.getProtocol().equals(REGISTRY_PROTOCOL)) {
Map<String, String> queryMap = StringUtils.parseQueryString(url.getParameterAndDecoded(Constants.REFER_KEY));
this.url = url.addParameters(queryMap).removeParameter(Constants.MONITOR_KEY);
this.url = url.addParameters(queryMap).removeParameter(MONITOR_KEY);
} else {
this.url = url;
}
Expand Down
Original file line number Diff line number Diff line change
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.common.utils.CollectionUtils;
import org.apache.dubbo.rpc.Invocation;
Expand All @@ -25,6 +24,12 @@

import java.util.List;

import static org.apache.dubbo.common.constants.ClusterConstants.DEFAULT_WARMUP;
import static org.apache.dubbo.common.constants.ClusterConstants.DEFAULT_WEIGHT;
import static org.apache.dubbo.common.constants.ClusterConstants.REMOTE_TIMESTAMP_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.WARMUP_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.WEIGHT_KEY;

/**
* AbstractLoadBalance
*/
Expand Down Expand Up @@ -66,12 +71,12 @@ public <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invo
* @return weight
*/
protected int getWeight(Invoker<?> invoker, Invocation invocation) {
int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), WEIGHT_KEY, DEFAULT_WEIGHT);
if (weight > 0) {
long timestamp = invoker.getUrl().getParameter(Constants.REMOTE_TIMESTAMP_KEY, 0L);
long timestamp = invoker.getUrl().getParameter(REMOTE_TIMESTAMP_KEY, 0L);
if (timestamp > 0L) {
int uptime = (int) (System.currentTimeMillis() - timestamp);
int warmup = invoker.getUrl().getParameter(Constants.WARMUP_KEY, Constants.DEFAULT_WARMUP);
int warmup = invoker.getUrl().getParameter(WARMUP_KEY, DEFAULT_WARMUP);
if (uptime > 0 && uptime < warmup) {
weight = calculateWarmupWeight(uptime, warmup, weight);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@
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.ClusterConstants.ADDRESS_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.FORCE_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.PRIORITY_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.RULE_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.RUNTIME_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;
import static org.apache.dubbo.common.constants.CommonConstants.METHOD_KEY;

/**
* ConditionRouter
Expand All @@ -66,10 +71,10 @@ public ConditionRouter(String rule, boolean force, boolean enabled) {

public ConditionRouter(URL url) {
this.url = url;
this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
this.force = url.getParameter(Constants.FORCE_KEY, false);
this.priority = url.getParameter(PRIORITY_KEY, 0);
this.force = url.getParameter(FORCE_KEY, false);
this.enabled = url.getParameter(ENABLED_KEY, true);
init(url.getParameterAndDecoded(Constants.RULE_KEY));
init(url.getParameterAndDecoded(RULE_KEY));
}

public void init(String rule) {
Expand Down Expand Up @@ -188,7 +193,7 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
if (!result.isEmpty()) {
return result;
} else if (force) {
logger.warn("The route result is empty and force execute. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey() + ", router: " + url.getParameterAndDecoded(Constants.RULE_KEY));
logger.warn("The route result is empty and force execute. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey() + ", router: " + url.getParameterAndDecoded(RULE_KEY));
return result;
}
} catch (Throwable t) {
Expand All @@ -201,7 +206,7 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
public boolean isRuntime() {
// We always return true for previously defined Router, that is, old Router doesn't support cache anymore.
// return true;
return this.url.getParameter(Constants.RUNTIME_KEY, false);
return this.url.getParameter(RUNTIME_KEY, false);
}

@Override
Expand All @@ -226,7 +231,7 @@ private boolean matchCondition(Map<String, MatchPair> condition, URL url, URL pa
//get real invoked method name from invocation
if (invocation != null && (METHOD_KEY.equals(key) || METHODS_KEY.equals(key))) {
sampleValue = invocation.getMethodName();
} else if (Constants.ADDRESS_KEY.equals(key)) {
} else if (ADDRESS_KEY.equals(key)) {
sampleValue = url.getAddress();
} else if (Constants.HOST_KEY.equals(key)) {
sampleValue = url.getHost();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.dubbo.rpc.cluster.router.file;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.URLBuilder;
import org.apache.dubbo.common.utils.IOUtils;
Expand All @@ -28,6 +27,11 @@
import java.io.FileReader;
import java.io.IOException;

import static org.apache.dubbo.common.constants.ClusterConstants.ROUTER_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.RULE_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.RUNTIME_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.TYPE_KEY;

public class FileRouterFactory implements RouterFactory {

public static final String NAME = "file";
Expand All @@ -43,7 +47,7 @@ public Router getRouter(URL url) {
try {
// Transform File URL into Script Route URL, and Load
// file:///d:/path/to/route.js?router=script ==> script:///d:/path/to/route.js?type=js&rule=<file-content>
String protocol = url.getParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME); // Replace original protocol (maybe 'file') with 'script'
String protocol = url.getParameter(ROUTER_KEY, ScriptRouterFactory.NAME); // Replace original protocol (maybe 'file') with 'script'
String type = null; // Use file suffix to config script type, e.g., js, groovy ...
String path = url.getPath();
if (path != null) {
Expand All @@ -55,12 +59,12 @@ public Router getRouter(URL url) {
String rule = IOUtils.read(new FileReader(new File(url.getAbsolutePath())));

// FIXME: this code looks useless
boolean runtime = url.getParameter(Constants.RUNTIME_KEY, false);
boolean runtime = url.getParameter(RUNTIME_KEY, false);
URL script = URLBuilder.from(url)
.setProtocol(protocol)
.addParameter(Constants.TYPE_KEY, type)
.addParameter(Constants.RUNTIME_KEY, runtime)
.addParameterAndEncoded(Constants.RULE_KEY, rule)
.addParameter(TYPE_KEY, type)
.addParameter(RUNTIME_KEY, runtime)
.addParameterAndEncoded(RULE_KEY, rule)
.build();

return routerFactory.getRouter(script);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.dubbo.rpc.cluster.router.mock;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.rpc.Invocation;
Expand All @@ -27,6 +26,9 @@
import java.util.ArrayList;
import java.util.List;

import static org.apache.dubbo.common.constants.ClusterConstants.INVOCATION_NEED_MOCK;
import static org.apache.dubbo.common.constants.ClusterConstants.MOCK_PROTOCOL;

/**
* A specific Router designed to realize mock feature.
* If a request is configured to use mock, then this router guarantees that only the invokers with protocol MOCK appear in final the invoker list, all other invokers will be excluded.
Expand All @@ -50,7 +52,7 @@ public <T> List<Invoker<T>> route(final List<Invoker<T>> invokers,
if (invocation.getAttachments() == null) {
return getNormalInvokers(invokers);
} else {
String value = invocation.getAttachments().get(Constants.INVOCATION_NEED_MOCK);
String value = invocation.getAttachments().get(INVOCATION_NEED_MOCK);
if (value == null) {
return getNormalInvokers(invokers);
} else if (Boolean.TRUE.toString().equalsIgnoreCase(value)) {
Expand All @@ -66,7 +68,7 @@ private <T> List<Invoker<T>> getMockedInvokers(final List<Invoker<T>> invokers)
}
List<Invoker<T>> sInvokers = new ArrayList<Invoker<T>>(1);
for (Invoker<T> invoker : invokers) {
if (invoker.getUrl().getProtocol().equals(Constants.MOCK_PROTOCOL)) {
if (invoker.getUrl().getProtocol().equals(MOCK_PROTOCOL)) {
sInvokers.add(invoker);
}
}
Expand All @@ -79,7 +81,7 @@ private <T> List<Invoker<T>> getNormalInvokers(final List<Invoker<T>> invokers)
} else {
List<Invoker<T>> sInvokers = new ArrayList<Invoker<T>>(invokers.size());
for (Invoker<T> invoker : invokers) {
if (!invoker.getUrl().getProtocol().equals(Constants.MOCK_PROTOCOL)) {
if (!invoker.getUrl().getProtocol().equals(MOCK_PROTOCOL)) {
sInvokers.add(invoker);
}
}
Expand All @@ -90,7 +92,7 @@ private <T> List<Invoker<T>> getNormalInvokers(final List<Invoker<T>> invokers)
private <T> boolean hasMockProviders(final List<Invoker<T>> invokers) {
boolean hasMockProvider = false;
for (Invoker<T> invoker : invokers) {
if (invoker.getUrl().getProtocol().equals(Constants.MOCK_PROTOCOL)) {
if (invoker.getUrl().getProtocol().equals(MOCK_PROTOCOL)) {
hasMockProvider = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.dubbo.rpc.cluster.router.script;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
Expand All @@ -40,6 +39,13 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import static org.apache.dubbo.common.constants.ClusterConstants.DEFAULT_SCRIPT_TYPE_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.FORCE_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.PRIORITY_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.RULE_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.RUNTIME_KEY;
import static org.apache.dubbo.common.constants.ClusterConstants.TYPE_KEY;

/**
* ScriptRouter
*/
Expand All @@ -58,7 +64,7 @@ public class ScriptRouter extends AbstractRouter {

public ScriptRouter(URL url) {
this.url = url;
this.priority = url.getParameter(Constants.PRIORITY_KEY, SCRIPT_ROUTER_DEFAULT_PRIORITY);
this.priority = url.getParameter(PRIORITY_KEY, SCRIPT_ROUTER_DEFAULT_PRIORITY);

engine = getEngine(url);
rule = getRule(url);
Expand All @@ -77,7 +83,7 @@ public ScriptRouter(URL url) {
* get rule from url parameters.
*/
private String getRule(URL url) {
String vRule = url.getParameterAndDecoded(Constants.RULE_KEY);
String vRule = url.getParameterAndDecoded(RULE_KEY);
if (StringUtils.isEmpty(vRule)) {
throw new IllegalStateException("route rule can not be empty.");
}
Expand All @@ -88,7 +94,7 @@ private String getRule(URL url) {
* create ScriptEngine instance by type from url parameters, then cache it
*/
private ScriptEngine getEngine(URL url) {
String type = url.getParameter(Constants.TYPE_KEY, Constants.DEFAULT_SCRIPT_TYPE_KEY);
String type = url.getParameter(TYPE_KEY, DEFAULT_SCRIPT_TYPE_KEY);

return engines.computeIfAbsent(type, t -> {
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName(type);
Expand Down Expand Up @@ -142,12 +148,12 @@ private <T> Bindings createBindings(List<Invoker<T>> invokers, Invocation invoca

@Override
public boolean isRuntime() {
return this.url.getParameter(Constants.RUNTIME_KEY, false);
return this.url.getParameter(RUNTIME_KEY, false);
}

@Override
public boolean isForce() {
return url.getParameter(Constants.FORCE_KEY, false);
return url.getParameter(FORCE_KEY, false);
}

}
Loading