This is an automated email from the ASF dual-hosted git repository. lixiaojie pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push: new 717c15e [Enhancement]: refactor categorizing with Collectors.groupingBy (#3490) 717c15e is described below commit 717c15e6581dc818552ef9bdf4cb6961d9b234da Author: kezhenxu94 <kezhenx...@163.com> AuthorDate: Wed Feb 20 00:04:00 2019 +0800 [Enhancement]: refactor categorizing with Collectors.groupingBy (#3490) --- .../registry/integration/RegistryDirectory.java | 47 +++++++++++++--------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java index 449bb28..f843871 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java @@ -52,6 +52,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -64,7 +65,6 @@ import static org.apache.dubbo.common.Constants.DYNAMIC_CONFIGURATORS_CATEGORY; import static org.apache.dubbo.common.Constants.PROVIDERS_CATEGORY; import static org.apache.dubbo.common.Constants.ROUTERS_CATEGORY; import static org.apache.dubbo.common.Constants.ROUTE_PROTOCOL; -import static org.apache.dubbo.common.utils.UrlUtils.classifyUrls; /** @@ -124,7 +124,7 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify this.queryMap = StringUtils.parseQueryString(url.getParameterAndDecoded(Constants.REFER_KEY)); this.overrideDirectoryUrl = this.directoryUrl = turnRegistryUrlToConsumerUrl(url); String group = directoryUrl.getParameter(Constants.GROUP_KEY, ""); - this.multiGroup = group != null && ("*".equals(group) || group.contains(",")); + this.multiGroup = group != null && (Constants.ANY_VALUE.equals(group) || group.contains(",")); } private URL turnRegistryUrlToConsumerUrl(URL url) { @@ -189,21 +189,30 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify @Override public synchronized void notify(List<URL> urls) { - List<URL> categoryUrls = urls.stream() + Map<String, List<URL>> categoryUrls = urls.stream() + .filter(Objects::nonNull) .filter(this::isValidCategory) .filter(this::isNotCompatibleFor26x) - .collect(Collectors.toList()); + .collect(Collectors.groupingBy(url -> { + if (UrlUtils.isConfigurator(url)) { + return CONFIGURATORS_CATEGORY; + } else if (UrlUtils.isRoute(url)) { + return ROUTERS_CATEGORY; + } else if (UrlUtils.isProvider(url)) { + return PROVIDERS_CATEGORY; + } + return ""; + })); - /** - * TODO Try to refactor the processing of these three type of urls using Collectors.groupBy()? - */ - this.configurators = Configurator.toConfigurators(classifyUrls(categoryUrls, UrlUtils::isConfigurator)) - .orElse(configurators); + List<URL> configuratorURLs = categoryUrls.getOrDefault(CONFIGURATORS_CATEGORY, Collections.emptyList()); + this.configurators = Configurator.toConfigurators(configuratorURLs).orElse(this.configurators); - toRouters(classifyUrls(categoryUrls, UrlUtils::isRoute)).ifPresent(this::addRouters); + List<URL> routerURLs = categoryUrls.getOrDefault(ROUTERS_CATEGORY, Collections.emptyList()); + toRouters(routerURLs).ifPresent(this::addRouters); // providers - refreshOverrideAndInvoker(classifyUrls(categoryUrls, UrlUtils::isProvider)); + List<URL> providerURLs = categoryUrls.getOrDefault(PROVIDERS_CATEGORY, Collections.emptyList()); + refreshOverrideAndInvoker(providerURLs); } private void refreshOverrideAndInvoker(List<URL> urls) { @@ -283,7 +292,7 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify private List<Invoker<T>> toMergeInvokerList(List<Invoker<T>> invokers) { List<Invoker<T>> mergedInvokers = new ArrayList<>(); - Map<String, List<Invoker<T>>> groupMap = new HashMap<String, List<Invoker<T>>>(); + Map<String, List<Invoker<T>>> groupMap = new HashMap<>(); for (Invoker<T> invoker : invokers) { String group = invoker.getUrl().getParameter(Constants.GROUP_KEY, ""); groupMap.computeIfAbsent(group, k -> new ArrayList<>()); @@ -343,11 +352,11 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify * @return invokers */ private Map<String, Invoker<T>> toInvokers(List<URL> urls) { - Map<String, Invoker<T>> newUrlInvokerMap = new HashMap<String, Invoker<T>>(); + Map<String, Invoker<T>> newUrlInvokerMap = new HashMap<>(); if (urls == null || urls.isEmpty()) { return newUrlInvokerMap; } - Set<String> keys = new HashSet<String>(); + Set<String> keys = new HashSet<>(); String queryProtocols = this.queryMap.get(Constants.PROTOCOL_KEY); for (URL providerUrl : urls) { // If protocol is configured at the reference side, only the matching protocol is selected @@ -393,7 +402,7 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify enabled = url.getParameter(Constants.ENABLED_KEY, true); } if (enabled) { - invoker = new InvokerDelegate<T>(protocol.refer(serviceType, url), url, providerUrl); + invoker = new InvokerDelegate<>(protocol.refer(serviceType, url), url, providerUrl); } } catch (Throwable t) { logger.error("Failed to refer invoker for interface:" + serviceType + ",url:(" + url + ")" + t.getMessage(), t); @@ -426,7 +435,7 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify this.overrideDirectoryUrl = this.overrideDirectoryUrl.addParametersIfAbsent(providerUrl.getParameters()); // Merge the provider side parameters if ((providerUrl.getPath() == null || providerUrl.getPath() - .length() == 0) && "dubbo".equals(providerUrl.getProtocol())) { // Compatible version 1.0 + .length() == 0) && Constants.DUBBO_PROTOCOL.equals(providerUrl.getProtocol())) { // Compatible version 1.0 //fix by tony.chenl DUBBO-44 String path = directoryUrl.getParameter(Constants.INTERFACE_KEY); if (path != null) { @@ -474,7 +483,7 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify private void destroyAllInvokers() { Map<String, Invoker<T>> localUrlInvokerMap = this.urlInvokerMap; // local reference if (localUrlInvokerMap != null) { - for (Invoker<T> invoker : new ArrayList<Invoker<T>>(localUrlInvokerMap.values())) { + for (Invoker<T> invoker : new ArrayList<>(localUrlInvokerMap.values())) { try { invoker.destroy(); } catch (Throwable t) { @@ -505,7 +514,7 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify for (Map.Entry<String, Invoker<T>> entry : oldUrlInvokerMap.entrySet()) { if (!newInvokers.contains(entry.getValue())) { if (deleted == null) { - deleted = new ArrayList<String>(); + deleted = new ArrayList<>(); } deleted.add(entry.getKey()); } @@ -597,7 +606,7 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify } Map<String, Invoker<T>> localUrlInvokerMap = urlInvokerMap; if (localUrlInvokerMap != null && localUrlInvokerMap.size() > 0) { - for (Invoker<T> invoker : new ArrayList<Invoker<T>>(localUrlInvokerMap.values())) { + for (Invoker<T> invoker : new ArrayList<>(localUrlInvokerMap.values())) { if (invoker.isAvailable()) { return true; }