Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/3.0' into 3.0-improve-reference-…
Browse files Browse the repository at this point in the history
…bean-register
  • Loading branch information
kylixs committed Apr 30, 2021
2 parents 28b0098 + 175d85f commit 4e01a17
Show file tree
Hide file tree
Showing 107 changed files with 4,195 additions and 1,936 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ public interface AddressListener {
/**
* processing when receiving the address list
*
* @param addresses provider address list
* @param addresses provider address list
* @param consumerUrl
* @param registryDirectory
*/
List<URL> notify(List<URL> addresses, URL consumerUrl, Directory registryDirectory);

default void destroy(URL consumerUrl, Directory registryDirectory) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ default <T> void notify(List<Invoker<T>> invokers) {
*/
int getPriority();

default void stop() {
//do nothing by default
}

@Override
default int compareTo(Router o) {
if (o == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
Expand All @@ -31,6 +33,7 @@
* Router chain
*/
public class RouterChain<T> {
public static final Logger LOGGER = LoggerFactory.getLogger(RouterChain.class);

// full list of addresses from registry, classified by method name.
private List<Invoker<T>> invokers = Collections.emptyList();
Expand Down Expand Up @@ -113,4 +116,17 @@ public void setInvokers(List<Invoker<T>> invokers) {
this.invokers = (invokers == null ? Collections.emptyList() : invokers);
routers.forEach(router -> router.notify(this.invokers));
}

public void destroy() {
invokers = Collections.emptyList();
for (Router router : routers) {
try {
router.stop();
} catch (Exception e) {
LOGGER.error("Error trying to stop router " + router.getClass(), e);
}
}
routers = Collections.emptyList();
builtinRouters = Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public ListenableRouter(URL url, String ruleKey) {

@Override
public synchronized void process(ConfigChangedEvent event) {
if (logger.isInfoEnabled()) {
logger.info("Notification of condition rule, change type is: " + event.getChangeType() +
if (logger.isDebugEnabled()) {
logger.debug("Notification of condition rule, change type is: " + event.getChangeType() +
", raw rule is:\n " + event.getContent());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.dubbo.rpc.cluster.router.mesh.rule.destination.DestinationRule;
import org.apache.dubbo.rpc.cluster.router.mesh.rule.virtualservice.VirtualServiceRule;
import org.apache.dubbo.rpc.cluster.router.mesh.util.VsDestinationGroupRuleDispatcher;

import org.yaml.snakeyaml.Yaml;

import java.text.MessageFormat;
Expand All @@ -37,7 +38,7 @@ public class MeshAppRuleListener implements ConfigurationListener {

private final VsDestinationGroupRuleDispatcher vsDestinationGroupRuleDispatcher = new VsDestinationGroupRuleDispatcher();

private String appName;
private final String appName;

private VsDestinationGroup vsDestinationGroupHolder;

Expand All @@ -46,24 +47,26 @@ public MeshAppRuleListener(String appName) {
}

public void receiveConfigInfo(String configInfo) {
logger.info(MessageFormat.format("[MeshAppRule] Received rule for app [{0}]: {1}.",
appName, configInfo));
if(logger.isDebugEnabled()) {
logger.debug(MessageFormat.format("[MeshAppRule] Received rule for app [{0}]: {1}.",
appName, configInfo));
}
try {

VsDestinationGroup vsDestinationGroup = new VsDestinationGroup();
vsDestinationGroup.setAppName(appName);

Yaml yaml = new Yaml();
Yaml yaml2 = new Yaml();
Iterable objectIterable = yaml.loadAll(configInfo);
Iterable<Object> objectIterable = yaml.loadAll(configInfo);
for (Object result : objectIterable) {

Map resultMap = (Map) result;
if (resultMap.get("kind").equals("DestinationRule")) {
if ("DestinationRule".equals(resultMap.get("kind"))) {
DestinationRule destinationRule = yaml2.loadAs(yaml2.dump(result), DestinationRule.class);
vsDestinationGroup.getDestinationRuleList().add(destinationRule);

} else if (resultMap.get("kind").equals("VirtualService")) {
} else if ("VirtualService".equals(resultMap.get("kind"))) {
VirtualServiceRule virtualServiceRule = yaml2.loadAs(yaml2.dump(result), VirtualServiceRule.class);
vsDestinationGroup.getVirtualServiceRuleList().add(virtualServiceRule);
}
Expand All @@ -86,7 +89,7 @@ public void register(MeshRuleRouter subscriber) {
vsDestinationGroupRuleDispatcher.register(subscriber);
}

//

public void unregister(MeshRuleRouter sub) {
vsDestinationGroupRuleDispatcher.unregister(sub);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@
@Activate(order = 670)
public class MeshRuleAddressListenerInterceptor implements AddressListener {

private static final Object mark = new Object();
private static ConcurrentHashMap<String, Object> appMap = new ConcurrentHashMap<String, Object>();
private static final Object MARK = new Object();
private static final ConcurrentHashMap<String, Object> APP_MAP = new ConcurrentHashMap<>();

@Override
public List<URL> notify(List<URL> addresses, URL consumerUrl, Directory registryDirectory) {

if (addresses != null && !addresses.isEmpty()) {
for (URL serviceURL : addresses) {
for (URL url : addresses) {

String app = serviceURL.getRemoteApplication();
String app = url.getRemoteApplication();
if (app != null && !app.isEmpty()) {
if (appMap.putIfAbsent(app, mark) == null) {
if (APP_MAP.putIfAbsent(app, MARK) == null) {
MeshRuleManager.subscribeAppRule(app);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class MeshRuleManager {
private static final String MESH_RULE_DATA_ID_SUFFIX = ".MESHAPPRULE";
private static final String GROUP = "DEFAULT_GROUP";

private static ConcurrentHashMap<String, MeshAppRuleListener> appRuleListeners = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, MeshAppRuleListener> APP_RULE_LISTENERS = new ConcurrentHashMap<>();

public synchronized static void subscribeAppRule(String app) {

Expand All @@ -57,11 +57,11 @@ public synchronized static void subscribeAppRule(String app) {
}

configuration.addListener(appRuleDataId, GROUP, meshAppRuleListener);
appRuleListeners.put(app, meshAppRuleListener);
APP_RULE_LISTENERS.put(app, meshAppRuleListener);
}

public static void register(String app, MeshRuleRouter subscriber) {
MeshAppRuleListener meshAppRuleListener = appRuleListeners.get(app);
MeshAppRuleListener meshAppRuleListener = APP_RULE_LISTENERS.get(app);
if (meshAppRuleListener == null) {
logger.warn("appRuleListener can't find when Router register");
return;
Expand All @@ -70,7 +70,7 @@ public static void register(String app, MeshRuleRouter subscriber) {
}

public static void unregister(MeshRuleRouter subscriber) {
Collection<MeshAppRuleListener> listeners = appRuleListeners.values();
Collection<MeshAppRuleListener> listeners = APP_RULE_LISTENERS.values();
for (MeshAppRuleListener listener : listeners) {
listener.unregister(subscriber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class MeshRuleRouter implements Router, VsDestinationGroupRuleListener {

private VsDestinationGroup vsDestinationGroup;

private Map<String, String> sourcesLables = new HashMap<>();
private Map<String, String> sourcesLabels = new HashMap<>();

protected List<Invoker<?>> invokerList = new ArrayList<>();

Expand All @@ -63,7 +63,7 @@ public class MeshRuleRouter implements Router, VsDestinationGroupRuleListener {

public MeshRuleRouter(URL url) {
this.url = url;
sourcesLables.putAll(url.getParameters());
sourcesLabels.putAll(url.getParameters());
}

@Override
Expand Down Expand Up @@ -146,6 +146,7 @@ private void registerAppRule(List<Invoker<?>> invokers) {
}


@Override
public void onRuleChange(VsDestinationGroup vsDestinationGroup) {
this.vsDestinationGroup = vsDestinationGroup;
computeSubset();
Expand Down Expand Up @@ -236,7 +237,7 @@ protected DubboRouteDetail findMatchDubboRouteDetail(List<DubboRouteDetail> dubb
//FIXME to deal with headers
for (DubboMatchRequest dubboMatchRequest : matchRequestList) {
if (!DubboMatchRequest.isMatch(dubboMatchRequest, methodName, parameterTypeList, parameters,
sourcesLables,
sourcesLabels,
new HashMap<>(), invocation.getAttachments(),
new HashMap<>())) {
match = false;
Expand Down Expand Up @@ -320,22 +321,34 @@ protected boolean containMapKeyValue(Map<String, String> originMap, Map<String,
}


// just for test
/**
* just for test
* @param vsDestinationGroup
*/
protected void setVsDestinationGroup(VsDestinationGroup vsDestinationGroup) {
this.vsDestinationGroup = vsDestinationGroup;
}

// just for test
protected void setSourcesLables(Map<String, String> sourcesLables) {
this.sourcesLables = sourcesLables;
/**
* just for test
* @param sourcesLabels
*/
protected void setSourcesLabels(Map<String, String> sourcesLabels) {
this.sourcesLabels = sourcesLabels;
}

// just for test
/**
* just for test
* @param invokerList
*/
protected void setInvokerList(List<Invoker<?>> invokerList) {
this.invokerList = invokerList;
}

// just for test
/**
* just for test
* @param subsetMap
*/
protected void setSubsetMap(Map<String, List<Invoker<?>>> subsetMap) {
this.subsetMap = subsetMap;
}
Expand All @@ -345,8 +358,8 @@ public VsDestinationGroup getVsDestinationGroup() {
return vsDestinationGroup;
}

public Map<String, String> getSourcesLables() {
return sourcesLables;
public Map<String, String> getSourcesLabels() {
return sourcesLabels;
}

public List<Invoker<?>> getInvokerList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public class BaseServiceMetadata {
protected volatile String group;

public static String buildServiceKey(String path, String group, String version) {
StringBuilder buf = new StringBuilder();
int length = path == null ? 0 : path.length();
length += group == null ? 0 : group.length();
length += version == null ? 0 : version.length();
length += 3;
StringBuilder buf = new StringBuilder(length);
if (group != null && group.length() > 0) {
buf.append(group).append("/");
}
Expand Down
38 changes: 6 additions & 32 deletions dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public URL(String protocol,
}

this.urlAddress = new PathURLAddress(protocol, username, password, path, host, port);
this.urlParam = new URLParam(parameters);
this.urlParam = URLParam.parse(parameters);
}

protected URL(String protocol,
Expand All @@ -194,7 +194,7 @@ protected URL(String protocol,
}

this.urlAddress = new PathURLAddress(protocol, username, password, path, host, port);
this.urlParam = new URLParam(parameters);
this.urlParam = URLParam.parse(parameters);
}

public static URL cacheableValueOf(String url) {
Expand Down Expand Up @@ -469,10 +469,6 @@ public Map<String, String> getParameters(Predicate<String> nameToSelect) {
return Collections.unmodifiableMap(selectedParameters);
}

public Map<String, Map<String, String>> getMethodParameters() {
return urlParam.getMethodParameters();
}

public String getParameterAndDecoded(String key) {
return getParameterAndDecoded(key, null);
}
Expand Down Expand Up @@ -732,24 +728,11 @@ public String getMethodParameterAndDecoded(String method, String key, String def
}

public String getMethodParameter(String method, String key) {
Map<String, String> keyMap = getMethodParameters().get(method);
String value = null;
if (keyMap != null) {
value = keyMap.get(key);
}
if (StringUtils.isEmpty(value)) {
value = urlParam.getParameter(key);
}
return value;
return urlParam.getMethodParameter(method, key);
}

public String getMethodParameterStrict(String method, String key) {
Map<String, String> keyMap = getMethodParameters().get(method);
String value = null;
if (keyMap != null) {
value = keyMap.get(key);
}
return value;
return urlParam.getMethodParameterStrict(method, key);
}

public String getMethodParameter(String method, String key, String defaultValue) {
Expand Down Expand Up @@ -936,20 +919,11 @@ public boolean hasMethodParameter(String method, String key) {
}

public String getAnyMethodParameter(String key) {
String suffix = "." + key;
for (String fullKey : getParameters().keySet()) {
if (fullKey.endsWith(suffix)) {
return getParameter(fullKey);
}
}
return null;
return urlParam.getAnyMethodParameter(key);
}

public boolean hasMethodParameter(String method) {
if (method == null) {
return false;
}
return getMethodParameters().containsKey(method);
return urlParam.hasMethodParameter(method);
}

public boolean isLocalHost() {
Expand Down
Loading

0 comments on commit 4e01a17

Please sign in to comment.