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-4218] Fix NPE when the TagRouterRule addresses config is null (#4218) #4236

Merged
merged 3 commits into from
Jun 3, 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,6 +16,7 @@
*/
package org.apache.dubbo.rpc.cluster.router.tag.model;

import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.rpc.cluster.router.AbstractRouterRule;

import java.util.ArrayList;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void init() {
return;
}

tags.forEach(tag -> {
tags.stream().filter(tag -> CollectionUtils.isNotEmpty(tag.getAddresses())).forEach(tag -> {
tagnameToAddresses.put(tag.getName(), tag.getAddresses());
tag.getAddresses().forEach(addr -> {
List<String> tagNames = addressToTagnames.computeIfAbsent(addr, k -> new ArrayList<>());
Expand All @@ -60,7 +61,10 @@ public void init() {
}

public List<String> getAddresses() {
return tags.stream().flatMap(tag -> tag.getAddresses().stream()).collect(Collectors.toList());
return tags.stream()
.filter(tag -> CollectionUtils.isNotEmpty(tag.getAddresses()))
.flatMap(tag -> tag.getAddresses().stream())
.collect(Collectors.toList());
}

public List<String> getTagNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.dubbo.rpc.cluster.router.tag.model.TagRouterRule;
import org.apache.dubbo.rpc.cluster.router.tag.model.TagRuleParser;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -63,4 +65,47 @@ public void normalTagRuleTest() {
private void setData(String path, String data) throws Exception {
client.setData().forPath(path, data.getBytes());
}

/**
* TagRouterRule parse test when the tags addresses is null
*
* <pre>
* ~ -> null
* null -> null
* </pre>
*/
@Test
public void tagRouterRuleParseTest(){
String tagRouterRuleConfig = "---\n" +
"force: false\n" +
"runtime: true\n" +
"enabled: false\n" +
"priority: 1\n" +
"key: demo-provider\n" +
"tags:\n" +
" - name: tag1\n" +
" addresses: null\n" +
" - name: tag2\n" +
" addresses: [\"30.5.120.37:20880\"]\n" +
" - name: tag3\n" +
" addresses: []\n" +
" - name: tag4\n" +
" addresses: ~\n" +
"...";

TagRouterRule tagRouterRule = TagRuleParser.parse(tagRouterRuleConfig);

// assert tags
assert tagRouterRule.getTagNames().contains("tag1");
assert tagRouterRule.getTagNames().contains("tag2");
assert tagRouterRule.getTagNames().contains("tag3");
assert tagRouterRule.getTagNames().contains("tag4");
// assert addresses
assert tagRouterRule.getAddresses().contains("30.5.120.37:20880");
assert tagRouterRule.getTagnameToAddresses().get("tag1")==null;
Copy link
Member

Choose a reason for hiding this comment

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

assert tagRouterRule.getAddresses().size==1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I add it

assert tagRouterRule.getTagnameToAddresses().get("tag2").size()==1;
assert tagRouterRule.getTagnameToAddresses().get("tag3")==null;
assert tagRouterRule.getTagnameToAddresses().get("tag4")==null;
assert tagRouterRule.getAddresses().size()==1;
}
}