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

nacos-registry:serviceName split error #4974

Merged
merged 3 commits into from
Sep 5, 2019
Merged

Conversation

charish00
Copy link
Contributor

@charish00 charish00 commented Aug 30, 2019

What is the purpose of the change

bugfix: serviceName split return the wrong size of segments when group or version is empty

Brief changelog

bugfix: serviceName split return the wrong size of segments

Verifying this change

XXXXX

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a GITHUB_issue field for the change (usually before you start working on it). Trivial changes like typos do not require a GITHUB issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [Dubbo-XXX] Fix UnknownException when host config not exist #XXX. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add sample in dubbo samples project.
  • Run mvn clean install -DskipTests=false & mvn clean test-compile failsafe:integration-test to make sure unit-test and integration-test pass.
  • If this contribution is large, please follow the Software Donation Guide.

@charish00
Copy link
Contributor Author

dubbo admin service query doesn't work before I fix it

@codecov-io
Copy link

codecov-io commented Sep 2, 2019

Codecov Report

Merging #4974 into master will increase coverage by <.01%.
The diff coverage is 33.33%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #4974      +/-   ##
============================================
+ Coverage      63.9%   63.91%   +<.01%     
- Complexity      449      451       +2     
============================================
  Files           769      769              
  Lines         33163    33160       -3     
  Branches       5229     5227       -2     
============================================
+ Hits          21194    21195       +1     
+ Misses         9545     9544       -1     
+ Partials       2424     2421       -3
Impacted Files Coverage Δ Complexity Δ
...org/apache/dubbo/registry/nacos/NacosRegistry.java 0% <0%> (ø) 0 <0> (ø) ⬇️
.../apache/dubbo/registry/nacos/NacosServiceName.java 56.16% <70%> (-2.28%) 0 <0> (ø)
.../apache/dubbo/remoting/transport/AbstractPeer.java 58.69% <0%> (-4.35%) 0% <0%> (ø)
...he/dubbo/registry/multicast/MulticastRegistry.java 67.87% <0%> (-1.81%) 0% <0%> (ø)
.../dubbo/remoting/transport/netty4/NettyChannel.java 64.7% <0%> (-1.18%) 0% <0%> (ø)
.../java/org/apache/dubbo/config/ReferenceConfig.java 60.94% <0%> (+0.36%) 0% <0%> (ø) ⬇️
...c/main/java/org/apache/dubbo/rpc/RpcException.java 83.33% <0%> (+3.33%) 0% <0%> (ø) ⬇️
...ache/dubbo/rpc/protocol/ProtocolFilterWrapper.java 88.33% <0%> (+3.33%) 0% <0%> (ø) ⬇️
...e/dubbo/remoting/transport/netty/NettyChannel.java 57.64% <0%> (+3.52%) 20% <0%> (+1%) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3702887...2d3a9fe. Read the comment docs.

Copy link
Member

@beiwei30 beiwei30 left a comment

Choose a reason for hiding this comment

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

@charish00 would you mind to try if org.apache.dubbo.common.utils.StringUtils#split works? and it would be nice if you can explain why the original implementation doesn't work.

one more thing, it is not a good practice to depend on org.apache.commons.lang3, it would be very nice if you can throughly remove it from the current implementation.

@charish00
Copy link
Contributor Author

@charish00 would you mind to try if org.apache.dubbo.common.utils.StringUtils#split works? and it would be nice if you can explain why the original implementation doesn't work.

one more thing, it is not a good practice to depend on org.apache.commons.lang3, it would be very nice if you can throughly remove it from the current implementation.

StringUtils.split("category:serviceInterface::", ":").length return 2
"category:serviceInterface::".split(":", -1).length return 4

then length will be the only condition(length==4) to judge the serviceName format. "category:serviceInterface::" is a correct serviceName, we can't use the original implementation

Copy link

@Moriadry-zz Moriadry-zz left a comment

Choose a reason for hiding this comment

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

Some changes requested.

@@ -252,7 +251,7 @@ private void scheduleServiceNamesLookup(final URL url, final NotifyListener list
boolean accepted = false;
for (String category : ALL_SUPPORTED_CATEGORIES) {
String prefix = category + SERVICE_NAME_SEPARATOR;
if (StringUtils.startsWith(serviceName, prefix)) {
if (serviceName != null && serviceName.startsWith(prefix)) {

Choose a reason for hiding this comment

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

I think previous version is okay.

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

Choose a reason for hiding this comment

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

Why remove StringUtils usage? It somehow makes our code more elegant.

Copy link
Member

@beiwei30 beiwei30 Sep 5, 2019

Choose a reason for hiding this comment

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

I think we should not rely on common-lang3 directly. It is a traverse dependency from nacos. We've already had StringUtils in dubbo. Instead of use another StringUtils, I would prefer to enhance the existing one.

int length = segments.length;
if (length != 4) { // must present 4 segments
return false;
}

String category = segments[CATEGORY_INDEX];
if (!ArrayUtils.contains(categories, category)) { // no match category
if (!categories.contains(category)) { // no match category

Choose a reason for hiding this comment

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

This will cause NPE.

Copy link
Member

Choose a reason for hiding this comment

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

getCategories guarentees it cannot be null.

final List<String> categories = getCategories(url); 

return false;
}

String serviceInterface = segments[SERVICE_INTERFACE_INDEX];
if (!WILDCARD.equals(targetServiceInterface) &&
!StringUtils.equals(targetServiceInterface, serviceInterface)) { // no match service interface
!targetServiceInterface.equals(serviceInterface)) { // no match service interface

Choose a reason for hiding this comment

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

Same NPE here.

Copy link
Member

Choose a reason for hiding this comment

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

will fix.

return false;
}

String group = segments[SERVICE_GROUP_INDEX];
return group == null || WILDCARD.equals(targetGroup)
|| StringUtils.equals(targetGroup, group);
|| targetGroup.equals(group);

Choose a reason for hiding this comment

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

Same here.

Copy link
Member

Choose a reason for hiding this comment

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

targetGroup cannot be null, but I will use dubbo's StringUtils anyway.

return false;
}

String version = segments[SERVICE_VERSION_INDEX];
if (!WILDCARD.equals(targetVersion) &&
!StringUtils.equals(targetVersion, version)) { // no match service version
!targetVersion.equals(version)) { // no match service version

Choose a reason for hiding this comment

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

Same here.

Copy link
Member

Choose a reason for hiding this comment

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

targetVersion cannot be null, but I will use dubbo's StringUtils anyway.

@@ -113,12 +107,12 @@ public boolean isCompatible(NacosServiceName concreteServiceName) {
}

// Not match comparison
if (!StringUtils.equals(this.category, concreteServiceName.category) &&
!ArrayUtils.contains(splitPreserveAllTokens(this.category, VALUE_SEPARATOR), concreteServiceName.category)) {
if (!this.category.equals(concreteServiceName.category)

Choose a reason for hiding this comment

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

NPE concern.

Copy link
Member

Choose a reason for hiding this comment

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

will change to use dubbo's stringutils.

return false;
}

if (!StringUtils.equals(this.serviceInterface, concreteServiceName.serviceInterface)) {
if (!this.serviceInterface.equals(concreteServiceName.serviceInterface)) {

Choose a reason for hiding this comment

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

And here.

Copy link
Member

Choose a reason for hiding this comment

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

will change to use dubbo's stringutils.

@@ -132,13 +126,12 @@ public boolean isCompatible(NacosServiceName concreteServiceName) {
}

// range condition
if (!StringUtils.equals(this.version, concreteServiceName.version) &&
!matchRange(this.version, concreteServiceName.version)) {
if (!this.version.equals(concreteServiceName.version)

Choose a reason for hiding this comment

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

Same here.

Copy link
Member

Choose a reason for hiding this comment

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

will change to use dubbo's stringutils.

Copy link
Member

@beiwei30 beiwei30 left a comment

Choose a reason for hiding this comment

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

LGTM. I will follow up with import cleanup.

@beiwei30 beiwei30 merged commit a032767 into apache:master Sep 5, 2019
@Moriadry-zz
Copy link

@beiwei30
Without StringUtils.equals , won't it cause a nullpointexception? Could you please check this.

@beiwei30
Copy link
Member

beiwei30 commented Sep 5, 2019

since I will clean up imports, I will double check.

@Moriadry-zz
Copy link

Okay sir.

@beiwei30 beiwei30 mentioned this pull request Sep 5, 2019
6 tasks
@beiwei30
Copy link
Member

beiwei30 commented Sep 5, 2019

@moriadry would you mind to review #5012?

@charish00
Copy link
Contributor Author

sorry, I try to search the method “equals” in dubbo stringUtils, but miss the method “isEquals”.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants