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-4793]make RegistryDirectory can refresh the invokers when providers number become 0 when using nacos registry #4802

Merged
merged 3 commits into from
Sep 9, 2019
Merged
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 @@ -17,6 +17,7 @@
package org.apache.dubbo.registry.nacos;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.URLBuilder;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.UrlUtils;
Expand All @@ -35,7 +36,6 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
Expand All @@ -59,6 +59,7 @@
import static org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.CONSUMERS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY;
import static org.apache.dubbo.registry.Constants.ADMIN_PROTOCOL;
Expand Down Expand Up @@ -369,15 +370,26 @@ private List<String> doGetServiceNames(URL url) {
return serviceNames;
}

private List<URL> buildURLs(URL consumerURL, Collection<Instance> instances) {
if (instances.isEmpty()) {
return Collections.emptyList();
private List<URL> toUrlWithEmpty(URL consumerURL, Collection<Instance> instances) {
Copy link

@Moriadry-zz Moriadry-zz Aug 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last nitpick, what about changing toUrlWithEmpty to toUrlsWhenEmpty? (just
one suggestion, there should be another better method name to make it clear)

I know we use toUrlWithEmpty in zookeeperRegistry.class, but I found the name may bring us a little confusion :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name tells that the protocol will be set "empty"

List<URL> urls = buildURLs(consumerURL, instances);
if (urls.size() == 0) {
URL empty = URLBuilder.from(consumerURL)
.setProtocol(EMPTY_PROTOCOL)
.addParameter(CATEGORY_KEY, DEFAULT_CATEGORY)
.build();
urls.add(empty);
}
return urls;
}

private List<URL> buildURLs(URL consumerURL, Collection<Instance> instances) {
List<URL> urls = new LinkedList<>();
for (Instance instance : instances) {
URL url = buildURL(instance);
if (UrlUtils.isMatch(consumerURL, url)) {
urls.add(url);
if (instances != null && !instances.isEmpty()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we don't need to check whether instances is empty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For String or Collection, I think not only null, but also size check are needed because the for each will create useless iterator object.

for (Instance instance : instances) {
URL url = buildURL(instance);
if (UrlUtils.isMatch(consumerURL, url)) {
urls.add(url);
}
}
}
return urls;
Expand Down Expand Up @@ -406,9 +418,11 @@ private void subscribeEventListener(String serviceName, final URL url, final Not
*/
private void notifySubscriber(URL url, NotifyListener listener, Collection<Instance> instances) {
List<Instance> healthyInstances = new LinkedList<>(instances);
// Healthy Instances
filterHealthyInstances(healthyInstances);
List<URL> urls = buildURLs(url, healthyInstances);
if (healthyInstances.size() > 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If healthyInstances is empty, this still works? Not sure, could u pls check it.
I believe we should make our code as clean as possible :)

Copy link
Contributor Author

@tswstarplanet tswstarplanet Aug 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. I think it's a fail-fast term.

// Healthy Instances
filterHealthyInstances(healthyInstances);
}
List<URL> urls = toUrlWithEmpty(url, healthyInstances);
NacosRegistry.this.notify(url, listener, urls);
}

Expand Down