Skip to content

Commit

Permalink
fix:修复ProcessRoutersRequest丢失RouteArgument的问题 (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun authored Nov 17, 2022
1 parent bd8e932 commit c98e92d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ public void appendArguments(RouteArgument argument) {
}

public void setArguments(Set<RouteArgument> arguments) {
if (CollectionUtils.isEmpty(arguments)) {
this.arguments = Collections.emptySet();
}
else {
this.arguments = arguments;
if (!CollectionUtils.isEmpty(arguments)) {
this.arguments.addAll(arguments);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ public void setSourceService(ServiceInfo serviceInfo) {
Optional.ofNullable(serviceInfo.getMetadata()).orElse(new HashMap<>())
.forEach((key, value) -> sourceService.appendArguments(RouteArgument.fromLabel(key, value)));

if (Objects.isNull(routerArgument)) {
this.routerArgument = new HashMap<>();
}
Set<RouteArgument> arguments = routerArgument.computeIfAbsent("ruleRouter", k -> new HashSet<>());
arguments.addAll(sourceService.getArguments());
buildRouterArgumentsBySourceService();
}

public String getMethod() {
Expand Down Expand Up @@ -108,9 +104,7 @@ public void putRouterArgument(String routerType, Set<RouteArgument> arguments) {
}

public Set<RouteArgument> getRouterArguments(String routerType) {
if (routerArgument == null) {
return Collections.emptySet();
}
buildRouterArgumentsBySourceService();
Set<RouteArgument> arguments = routerArgument.get(routerType);
if (CollectionUtils.isEmpty(arguments)) {
return Collections.emptySet();
Expand All @@ -120,9 +114,7 @@ public Set<RouteArgument> getRouterArguments(String routerType) {
}

public Map<String, Set<RouteArgument>> getRouterArguments() {
if (routerArgument == null) {
return Collections.emptyMap();
}
buildRouterArgumentsBySourceService();
Map<String, Set<RouteArgument>> routerArgument = new HashMap<>(this.routerArgument);
return Collections.unmodifiableMap(routerArgument);
}
Expand All @@ -135,6 +127,17 @@ public void setMetadataFailoverType(MetadataFailoverType metadataFailoverType) {
this.metadataFailoverType = metadataFailoverType;
}

private void buildRouterArgumentsBySourceService() {
if (CollectionUtils.isEmpty(routerArgument)) {
routerArgument = new HashMap<>();
}
if (Objects.isNull(sourceService)) {
return;
}
Set<RouteArgument> arguments = routerArgument.computeIfAbsent("ruleRouter", k -> new HashSet<>());
arguments.addAll(sourceService.getArguments());
}

@Deprecated
public void putRouterMetadata(String routerType, Map<String, String> metadata) {
if (MapUtils.isEmpty(metadata)) {
Expand Down Expand Up @@ -166,9 +169,7 @@ public void addRouterMetadata(String routerType, Map<String, String> metadata) {

@Deprecated
public Map<String, String> getRouterMetadata(String routerType) {
if (routerArgument == null) {
return Collections.emptyMap();
}
buildRouterArgumentsBySourceService();
Set<RouteArgument> arguments = routerArgument.get(routerType);
if (CollectionUtils.isEmpty(arguments)) {
return Collections.emptyMap();
Expand All @@ -182,9 +183,7 @@ public Map<String, String> getRouterMetadata(String routerType) {

@Deprecated
public Map<String, Map<String, String>> getRouterMetadata() {
if (Objects.isNull(routerArgument)) {
return Collections.emptyMap();
}
buildRouterArgumentsBySourceService();

Map<String, Map<String, String>> ret = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

package com.tencent.polaris.router.api.rpc;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import com.tencent.polaris.api.pojo.RouteArgument;
import com.tencent.polaris.api.pojo.ServiceInfo;
import com.tencent.polaris.api.pojo.SourceService;
import org.junit.Assert;
Expand All @@ -31,8 +34,9 @@ public class ProcessRoutersRequestTests {
public void testGetRouterArguments() {
ProcessRoutersRequest request = new ProcessRoutersRequest();
request.getRouterArguments();

request.getRouterArguments("ruleRouter");
request.getRouterMetadata();
request.getRouterMetadata("ruleRouter");
}

@Test
Expand All @@ -49,5 +53,49 @@ public void testSetSourceService() {
SourceService sourceService = (SourceService) request.getSourceService();
Assert.assertEquals(2, sourceService.getArguments().size());
Assert.assertEquals(2, request.getRouterArguments("ruleRouter").size());
Assert.assertEquals(1, request.getRouterMetadata().size());
Assert.assertEquals(2, request.getRouterMetadata("ruleRouter").size());
}

@Test
public void testSetSourceService2() {
ProcessRoutersRequest request = new ProcessRoutersRequest();
SourceService serviceInfo = new SourceService();
serviceInfo.appendArguments(RouteArgument.fromLabel("$header.uid", "123"));
serviceInfo.setArguments(new HashSet<>(Arrays.asList(
RouteArgument.fromLabel("uid", "123")
)));

request.setSourceService(serviceInfo);

SourceService sourceService = (SourceService) request.getSourceService();
Assert.assertEquals(2, sourceService.getArguments().size());
Assert.assertEquals(2, request.getRouterArguments("ruleRouter").size());
Assert.assertEquals(1, request.getRouterMetadata().size());
Assert.assertEquals(2, request.getRouterMetadata("ruleRouter").size());
}

@Test
public void testSetSourceService3() {
ProcessRoutersRequest request = new ProcessRoutersRequest();
SourceService serviceInfo = new SourceService();
serviceInfo.appendArguments(RouteArgument.fromLabel("$header.uid", "123"));
serviceInfo.setArguments(new HashSet<>(Arrays.asList(
RouteArgument.fromLabel("uid", "123")
)));

request.setSourceService(serviceInfo);

SourceService sourceService = (SourceService) request.getSourceService();
Assert.assertEquals(2, sourceService.getArguments().size());
Assert.assertEquals(2, request.getRouterArguments("ruleRouter").size());
Assert.assertEquals(1, request.getRouterMetadata().size());
Assert.assertEquals(2, request.getRouterMetadata("ruleRouter").size());

((SourceService) request.getSourceService()).appendArguments(RouteArgument.fromLabel("$header.env", "prod"));
Assert.assertEquals(3, sourceService.getArguments().size());
Assert.assertEquals(3, request.getRouterArguments("ruleRouter").size());
Assert.assertEquals(1, request.getRouterMetadata().size());
Assert.assertEquals(3, request.getRouterMetadata("ruleRouter").size());
}
}

0 comments on commit c98e92d

Please sign in to comment.