Skip to content

feat:upgrade nearby router. #540

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

Merged
merged 1 commit into from
Aug 10, 2024
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 @@ -167,6 +167,8 @@ consumer:
- isolatedRouter
#描述: 服务路由链
chain:
# 命名空间就近路由
- namespaceRouter
# 泳道路由
- laneRouter
# 元数据路由
Expand All @@ -191,10 +193,10 @@ consumer:
#描述:规则路由降级策略。all(降级返回所有的节点),none(不降级)
failoverType: all
nearbyBasedRouter:
#描述: 就近路由的最小匹配级别。region(大区)、zone(区域)、campus(园区)
matchLevel: zone
#描述: 就近路由的最小匹配级别。ALL(全部)、REGION(大区)、ZONE(区域)、CAMPUS(园区)
matchLevel: ZONE
#描述: 最大匹配级别
maxMatchLevel: all
maxMatchLevel: ALL
#描述: 强制就近
strictNearby: false
#描述: 全部实例不健康时是否降级其他地域
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.tencent.polaris.api.config.plugin.PluginConfig;
import com.tencent.polaris.api.config.verify.Verifier;

import java.util.List;

/**
Expand All @@ -44,6 +45,8 @@ public interface ServiceRouterConfig extends PluginConfig, Verifier {
String DEFAULT_ROUTER_CANARY = "canaryRouter";

String DEFAULT_ROUTER_LANE = "laneRouter";

String DEFAULT_ROUTER_NAMESPACE = "namespaceRouter";


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ public Map<String, String> getServiceMetadata() {
}

public void setServiceMetadata(Map<String, String> serviceMetadata) {
this.serviceMetadata = serviceMetadata;
if (serviceMetadata != null) {
this.serviceMetadata = serviceMetadata;
} else {
this.serviceMetadata = new HashMap<>();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.tencent.polaris.api.utils.StringUtils;

import java.util.Map;
import java.util.Objects;

/**
Expand Down Expand Up @@ -33,6 +32,7 @@ public enum EventType {
FAULT_DETECTING,
SERVICE_CONTRACT,
LANE_RULE,
NEARBY_ROUTE_RULE,
}

private final ServiceKey serviceKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package com.tencent.polaris.api.rpc;

/**
* 命名空间就近路由的降级类型
*
* @author Haotian Zhang
*/
public enum NamespaceRouterFailoverType {

/**
* 默认返回全量地址。优先保证服务调用正常
*/
all,
/**
* 不降级,返回空地址列表
*/
none
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package com.tencent.polaris.api.utils;

/**
* Utils for comparing something.
*
* @author Haotian Zhang
*/
public class CompareUtils {

public static boolean isWildcardMatcherSingle(String name) {
return StringUtils.equalsIgnoreCase(name, RuleUtils.MATCH_ALL) || StringUtils.isBlank(name);
}

public static int compareSingleValue(String value1, String value2) {
boolean serviceWildcard1 = isWildcardMatcherSingle(value1);
boolean serviceWildcard2 = isWildcardMatcherSingle(value2);
if (serviceWildcard1 && serviceWildcard2) {
return 0;
}
if (serviceWildcard1) {
// 1 before 2
return 1;
}
if (serviceWildcard2) {
// 1 before 2
return -1;
}
return value1.compareTo(value2);
}

public static int compareService(String namespace1, String service1, String namespace2, String service2) {
int nsResult = CompareUtils.compareSingleValue(namespace1, namespace2);
if (nsResult != 0) {
return nsResult;
}
return CompareUtils.compareSingleValue(service1, service2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.tencent.polaris.api.utils;


import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.api.pojo.TrieNode;
import com.tencent.polaris.logging.LoggerFactory;
import com.tencent.polaris.metadata.core.manager.MetadataContainerGroup;
Expand Down Expand Up @@ -319,4 +320,21 @@ private static boolean matchValueByValueType(boolean isMatchSource, String ruleM
return allMetaMatched;
}

public static boolean matchService(ServiceKey serviceKey, String namespace, String service) {
String inputNamespace = "";
String inputService = "";
if (null != serviceKey) {
inputNamespace = serviceKey.getNamespace();
inputService = serviceKey.getService();
}
if (StringUtils.isNotBlank(namespace) && !StringUtils.equals(namespace, RuleUtils.MATCH_ALL) && !StringUtils
.equals(inputNamespace, namespace)) {
return false;
}
if (StringUtils.isNotBlank(service) && !StringUtils.equals(service, RuleUtils.MATCH_ALL) && !StringUtils
.equals(inputService, service)) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package com.tencent.polaris.api.utils;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Test for {@link CompareUtils}
*
* @author Haotian Zhang
*/
public class CompareUtilsTest {

@Test
public void testIsWildcardMatcherSingle() {
assertThat(CompareUtils.isWildcardMatcherSingle("*")).isTrue();
assertThat(CompareUtils.isWildcardMatcherSingle("")).isTrue();
assertThat(CompareUtils.isWildcardMatcherSingle("abc")).isFalse();
}

@Test
public void testCompareSingleValue() {
assertThat(CompareUtils.compareSingleValue("*", "abc") > 0).isTrue();
assertThat(CompareUtils.compareSingleValue("*", "*") == 0).isTrue();
assertThat(CompareUtils.compareSingleValue("abc", "*") < 0).isTrue();
assertThat(CompareUtils.compareSingleValue("abc", "def") < 0).isTrue();
}

@Test
public void testCompareService() {
assertThat(CompareUtils.compareService("*", "*", "*", "*") == 0).isTrue();
assertThat(CompareUtils.compareService("*", "abc", "*", "*") < 0).isTrue();
assertThat(CompareUtils.compareService("*", "abc", "abc", "*") > 0).isTrue();
assertThat(CompareUtils.compareService("abc", "abc", "abc", "*") < 0).isTrue();
assertThat(CompareUtils.compareService("abc", "abc", "abc", "def") < 0).isTrue();
assertThat(CompareUtils.compareService("abc", "*", "abc", "def") > 0).isTrue();
assertThat(CompareUtils.compareService("abc", "*", "*", "def") < 0).isTrue();
}
}
5 changes: 5 additions & 0 deletions polaris-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@
<artifactId>router-lane</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-namespace</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-metadata</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,18 @@
import com.tencent.polaris.api.flow.DiscoveryFlow;
import com.tencent.polaris.api.plugin.lossless.InstanceProperties;
import com.tencent.polaris.api.plugin.lossless.LosslessPolicy;
import com.tencent.polaris.api.plugin.route.LocationLevel;
import com.tencent.polaris.api.plugin.server.CommonProviderRequest;
import com.tencent.polaris.api.plugin.server.CommonProviderResponse;
import com.tencent.polaris.api.plugin.server.CommonServiceContractRequest;
import com.tencent.polaris.api.plugin.server.ReportServiceContractRequest;
import com.tencent.polaris.api.plugin.server.ReportServiceContractResponse;
import com.tencent.polaris.api.plugin.server.ServerConnector;
import com.tencent.polaris.api.plugin.server.TargetServer;
import com.tencent.polaris.api.plugin.server.*;
import com.tencent.polaris.api.pojo.BaseInstance;
import com.tencent.polaris.api.pojo.RetStatus;
import com.tencent.polaris.api.rpc.GetAllInstancesRequest;
import com.tencent.polaris.api.rpc.GetHealthyInstancesRequest;
import com.tencent.polaris.api.rpc.GetServiceContractRequest;
import com.tencent.polaris.api.rpc.GetServiceRuleRequest;
import com.tencent.polaris.api.rpc.GetServicesRequest;
import com.tencent.polaris.api.rpc.InstanceDeregisterRequest;
import com.tencent.polaris.api.rpc.InstanceHeartbeatRequest;
import com.tencent.polaris.api.rpc.InstanceRegisterRequest;
import com.tencent.polaris.api.rpc.InstanceRegisterResponse;
import com.tencent.polaris.api.rpc.InstancesFuture;
import com.tencent.polaris.api.rpc.InstancesResponse;
import com.tencent.polaris.api.rpc.RequestBaseEntity;
import com.tencent.polaris.api.rpc.ServiceCallResult;
import com.tencent.polaris.api.rpc.ServiceRuleResponse;
import com.tencent.polaris.api.rpc.ServicesResponse;
import com.tencent.polaris.api.rpc.UnWatchInstancesRequest;
import com.tencent.polaris.api.rpc.WatchInstancesRequest;
import com.tencent.polaris.api.rpc.WatchServiceResponse;
import com.tencent.polaris.api.rpc.*;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.client.api.ServiceCallResultListener;
import com.tencent.polaris.client.pojo.ServiceRuleByProto;
import com.tencent.polaris.client.util.Utils;
import com.tencent.polaris.logging.LoggerFactory;
import com.tencent.polaris.specification.api.v1.traffic.manage.RoutingProto;
import org.slf4j.Logger;

import java.util.List;
Expand Down Expand Up @@ -338,9 +315,9 @@ private void enrichInstanceLocation(InstanceRegisterRequest request) {
return;
}

request.setRegion(sdkContext.getValueContext().getValue(LocationLevel.region.name()));
request.setZone(sdkContext.getValueContext().getValue(LocationLevel.zone.name()));
request.setCampus(sdkContext.getValueContext().getValue(LocationLevel.campus.name()));
request.setRegion(sdkContext.getValueContext().getValue(RoutingProto.NearbyRoutingConfig.LocationLevel.REGION.name()));
request.setZone(sdkContext.getValueContext().getValue(RoutingProto.NearbyRoutingConfig.LocationLevel.ZONE.name()));
request.setCampus(sdkContext.getValueContext().getValue(RoutingProto.NearbyRoutingConfig.LocationLevel.CAMPUS.name()));
}

/**
Expand Down
5 changes: 5 additions & 0 deletions polaris-discovery/polaris-discovery-factory/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
<artifactId>router-lane</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-namespace</artifactId>
<version>${project.version}</version>
</dependency>
<!--依赖负载均衡插件-->
<dependency>
<groupId>com.tencent.polaris</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import com.sun.net.httpserver.HttpServer;
import com.tencent.polaris.api.config.Configuration;
import com.tencent.polaris.api.core.ProviderAPI;
import com.tencent.polaris.api.plugin.route.LocationLevel;
import com.tencent.polaris.api.rpc.InstanceDeregisterRequest;
import com.tencent.polaris.api.rpc.InstanceRegisterRequest;
import com.tencent.polaris.api.rpc.InstanceRegisterResponse;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
import com.tencent.polaris.quickstart.example.utils.ProviderExampleUtils;
import com.tencent.polaris.specification.api.v1.traffic.manage.RoutingProto;

import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -85,7 +85,7 @@ private static String providedInstanceId(String namespace, String service, Strin

// do the instance register
private static void register(String namespace, String service, String host, int port,
ProviderAPI providerAPI) {
ProviderAPI providerAPI) {
InstanceRegisterRequest registerRequest = new InstanceRegisterRequest();
registerRequest.setNamespace(namespace);
registerRequest.setService(service);
Expand All @@ -103,7 +103,7 @@ private static void register(String namespace, String service, String host, int

// do the instance deregister
private static void deregister(String namespace, String service, String host, int port,
ProviderAPI providerAPI) {
ProviderAPI providerAPI) {
InstanceDeregisterRequest deregisterRequest = new InstanceDeregisterRequest();
deregisterRequest.setNamespace(namespace);
deregisterRequest.setService(service);
Expand Down Expand Up @@ -155,9 +155,9 @@ public EchoServerHandler(SDKContext context, int localPort) {
public void handle(HttpExchange exchange) throws IOException {

Map<String, String> location = new HashMap<>();
location.put(LocationLevel.region.name(), context.getValueContext().getValue(LocationLevel.region.name()));
location.put(LocationLevel.zone.name(), context.getValueContext().getValue(LocationLevel.zone.name()));
location.put(LocationLevel.campus.name(), context.getValueContext().getValue(LocationLevel.campus.name()));
location.put(RoutingProto.NearbyRoutingConfig.LocationLevel.REGION.name(), context.getValueContext().getValue(RoutingProto.NearbyRoutingConfig.LocationLevel.REGION.name()));
location.put(RoutingProto.NearbyRoutingConfig.LocationLevel.ZONE.name(), context.getValueContext().getValue(RoutingProto.NearbyRoutingConfig.LocationLevel.ZONE.name()));
location.put(RoutingProto.NearbyRoutingConfig.LocationLevel.CAMPUS.name(), context.getValueContext().getValue(RoutingProto.NearbyRoutingConfig.LocationLevel.CAMPUS.name()));

Map<String, String> parameters = splitQuery(exchange.getRequestURI());
String echoValue = parameters.get("value");
Expand Down
Loading
Loading