Skip to content

feat:support TSF router. #534

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 2 commits into from
Aug 8, 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 @@ -33,29 +33,21 @@
import com.tencent.polaris.api.plugin.route.RouteInfo;
import com.tencent.polaris.api.plugin.route.RouteResult;
import com.tencent.polaris.api.plugin.route.ServiceRouter;
import com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceEventKey;
import com.tencent.polaris.api.pojo.*;
import com.tencent.polaris.api.pojo.ServiceEventKey.EventType;
import com.tencent.polaris.api.pojo.ServiceEventKeysProvider;
import com.tencent.polaris.api.pojo.ServiceInfo;
import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.api.pojo.ServiceInstancesWrap;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.api.pojo.ServiceRule;
import com.tencent.polaris.api.pojo.Services;
import com.tencent.polaris.api.rpc.Criteria;
import com.tencent.polaris.api.rpc.RequestBaseEntity;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.client.util.Utils;
import com.tencent.polaris.logging.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;

import static com.tencent.polaris.api.plugin.route.RouterConstants.ROUTER_FAULT_TOLERANCE_ENABLE;

/**
* 同步调用流程
Expand All @@ -71,16 +63,16 @@ public class BaseFlow {
/**
* 通用获取单个服务实例的方法,用于SDK内部调用
*
* @param extensions 插件上下文
* @param serviceKey 服务信息
* @param extensions 插件上下文
* @param serviceKey 服务信息
* @param coreRouterNames 核心路由插件链
* @param lbPolicy 负载均衡策略
* @param protocol 协议信息
* @param hashKey 一致性hash的key
* @param lbPolicy 负载均衡策略
* @param protocol 协议信息
* @param hashKey 一致性hash的key
* @return 过滤后的实例
*/
public static Instance commonGetOneInstance(Extensions extensions, ServiceKey serviceKey,
List<String> coreRouterNames, String lbPolicy, String protocol, String hashKey) {
List<String> coreRouterNames, String lbPolicy, String protocol, String hashKey) {
ServiceEventKey svcEventKey = new ServiceEventKey(serviceKey, EventType.INSTANCE);
LOG.debug("[ConnectionManager]start to discover service {}", svcEventKey);
DefaultServiceEventKeysProvider provider = new DefaultServiceEventKeysProvider();
Expand Down Expand Up @@ -123,14 +115,14 @@ public static Instance commonGetOneInstance(Extensions extensions, ServiceKey se
/**
* 处理服务路由
*
* @param routeInfo 路由信息
* @param dstInstances 目标实例列表
* @param routeInfo 路由信息
* @param dstInstances 目标实例列表
* @param routerChainGroup 插件链
* @return 过滤后的实例
* @throws PolarisException 异常
*/
public static ServiceInstances processServiceRouters(RouteInfo routeInfo, ServiceInstances dstInstances,
RouterChainGroup routerChainGroup) throws PolarisException {
RouterChainGroup routerChainGroup) throws PolarisException {
if (null == dstInstances || CollectionUtils.isEmpty(dstInstances.getInstances())) {
return dstInstances;
}
Expand All @@ -141,10 +133,19 @@ public static ServiceInstances processServiceRouters(RouteInfo routeInfo, Servic
if (processRouterChain(routerChainGroup.getBeforeRouters(), routeInfo, serviceInstancesWrap)) {
processed = true;
}
Map<String, String> destSvcMetadata = Optional.ofNullable(serviceInstancesWrap.getMetadata()).orElse(Collections.emptyMap());
List<Instance> faultToleranceServiceInstances = new ArrayList<>();
if (Boolean.parseBoolean(destSvcMetadata.get(ROUTER_FAULT_TOLERANCE_ENABLE))) {
faultToleranceServiceInstances = new ArrayList<>(dstInstances.getInstances());
}
//再走业务路由
if (processRouterChain(routerChainGroup.getCoreRouters(), routeInfo, serviceInstancesWrap)) {
processed = true;
}
if (CollectionUtils.isEmpty(serviceInstancesWrap.getInstances())
&& Boolean.parseBoolean(destSvcMetadata.get(ROUTER_FAULT_TOLERANCE_ENABLE))) {
serviceInstancesWrap.setInstances(faultToleranceServiceInstances);
}
//最后走后置路由
if (processRouterChain(routerChainGroup.getAfterRouters(), routeInfo, serviceInstancesWrap)) {
processed = true;
Expand All @@ -156,7 +157,7 @@ public static ServiceInstances processServiceRouters(RouteInfo routeInfo, Servic
}

private static boolean processRouterChain(List<ServiceRouter> routers,
RouteInfo routeInfo, ServiceInstancesWrap serviceInstances) throws PolarisException {
RouteInfo routeInfo, ServiceInstancesWrap serviceInstances) throws PolarisException {
if (CollectionUtils.isEmpty(routers)) {
return false;
}
Expand Down Expand Up @@ -190,15 +191,15 @@ private static boolean processRouterChain(List<ServiceRouter> routers,
/**
* 同步拉取资源数据
*
* @param extensions 插件集合
* @param extensions 插件集合
* @param internalRequest 是否内部请求
* @param paramProvider 参数提供器
* @param controlParam 控制参数
* @param paramProvider 参数提供器
* @param controlParam 控制参数
* @return 多资源应答
* @throws PolarisException 获取异常
*/
public static ResourcesResponse syncGetResources(Extensions extensions, boolean internalRequest,
ServiceEventKeysProvider paramProvider, FlowControlParam controlParam)
ServiceEventKeysProvider paramProvider, FlowControlParam controlParam)
throws PolarisException {

if (CollectionUtils.isEmpty(paramProvider.getSvcEventKeys()) && null == paramProvider.getSvcEventKey()) {
Expand Down Expand Up @@ -245,7 +246,7 @@ public static ResourcesResponse syncGetResources(Extensions extensions, boolean
}

private static boolean readResourcesFromLocalCache(ServiceEventKeysProvider paramProvider,
Extensions extensions, ResourcesResponse resourcesResponse) {
Extensions extensions, ResourcesResponse resourcesResponse) {
LocalRegistry localRegistry = extensions.getLocalRegistry();
if (null != paramProvider.getSvcEventKey()) {
if (loadLocalResources(paramProvider.getSvcEventKey(), resourcesResponse, localRegistry)) {
Expand All @@ -263,7 +264,7 @@ private static boolean readResourcesFromLocalCache(ServiceEventKeysProvider para
}

private static boolean loadLocalResources(ServiceEventKey svcEventKey, ResourcesResponse resourcesResponse,
LocalRegistry localRegistry) {
LocalRegistry localRegistry) {
ResourceFilter filter = new ResourceFilter(svcEventKey, false, true);
if (svcEventKey.getEventType() == EventType.INSTANCE) {
ServiceInstances instances = localRegistry.getInstances(filter);
Expand Down Expand Up @@ -293,7 +294,7 @@ private static boolean loadLocalResources(ServiceEventKey svcEventKey, Resources
}

public static Instance processLoadBalance(LoadBalancer loadBalancer, Criteria criteria,
ServiceInstances dstInstances) throws PolarisException {
ServiceInstances dstInstances) throws PolarisException {
Instance instance = loadBalancer.chooseInstance(criteria, dstInstances);
if (null == instance) {
throw new PolarisException(ErrorCode.INSTANCE_NOT_FOUND,
Expand All @@ -306,12 +307,12 @@ public static Instance processLoadBalance(LoadBalancer loadBalancer, Criteria cr
/**
* 构建流程控制参数
*
* @param entity 请求对象
* @param config 配置对象
* @param entity 请求对象
* @param config 配置对象
* @param controlParam 控制参数
*/
public static void buildFlowControlParam(RequestBaseEntity entity, Configuration config,
FlowControlParam controlParam) {
FlowControlParam controlParam) {
long timeoutMs = entity.getTimeoutMs();
if (timeoutMs == 0) {
timeoutMs = config.getGlobal().getAPI().getTimeout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ consumer:
#描述: 缓存插件名
type: inmemory
#描述: 是否启用服务数据缓存
serviceExpireEnable: true
serviceExpireEnable: false
#描述: 服务过期淘汰时间
serviceExpireTime: 24h
#描述: 服务定期同步刷新周期
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.metadata.core.constant;

/**
* Metadata constant for Polaris.
*
* @author Haotian Zhang
*/
public class MetadataConstants {

/**
* local service namespace.
*/
public static final String LOCAL_NAMESPACE = "LOCAL_NAMESPACE";

/**
* local service name.
*/
public static final String LOCAL_SERVICE = "LOCAL_SERVICE";

/**
* local host ip.
*/
public static final String LOCAL_IP = "LOCAL_IP";

/**
* local host port.
*/
public static final String LOCAL_PORT = "LOCAL_PORT";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.metadata.core.constant;

/**
* Metadata constant for TSF.
*
* @author Haotian Zhang
*/
public final class TsfMetadataConstants {
/**
* tsf application id.
*/
public static String TSF_APPLICATION_ID = "TSF_APPLICATION_ID";

/**
* tsf program version.
*/
public static String TSF_PROG_VERSION = "TSF_PROG_VERSION";

/**
* tsf group id.
*/
public static String TSF_GROUP_ID = "TSF_GROUP_ID";

/**
* tsf namespace id.
*/
public static String TSF_NAMESPACE_ID = "TSF_NAMESPACE_ID";

/**
* tsf instance id.
*/
public static String TSF_INSTNACE_ID = "TSF_INSTNACE_ID";

/**
* tsf region.
*/
public static String TSF_REGION = "TSF_REGION";

/**
* tsf zone.
*/
public static String TSF_ZONE = "TSF_ZONE";

/**
* tsf SDK version.
*/
public static String TSF_SDK_VERSION = "TSF_SDK_VERSION";

/**
* tsf tags.
*/
public static String TSF_TAGS = "TSF_TAGS";

private TsfMetadataConstants() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.metadata.core.manager;

import com.tencent.polaris.metadata.core.MetadataContainer;
import com.tencent.polaris.metadata.core.impl.MessageMetadataContainerImpl;
import com.tencent.polaris.metadata.core.impl.MetadataContainerImpl;

import static com.tencent.polaris.metadata.core.manager.MetadataContext.DEFAULT_TRANSITIVE_PREFIX;

public class CalleeMetadataContainerGroup implements MetadataContainerGroup {

private final MetadataContainer messageMetadataContainer;

private final static MetadataContainer applicationMetadataContainer = new MetadataContainerImpl(DEFAULT_TRANSITIVE_PREFIX);

private final MetadataContainer customMetadataContainer;

public CalleeMetadataContainerGroup(String transitivePrefix) {
assert null != transitivePrefix;
this.messageMetadataContainer = new MessageMetadataContainerImpl(transitivePrefix);
this.customMetadataContainer = new MetadataContainerImpl(transitivePrefix);
}

@Override
public MetadataContainer getMessageMetadataContainer() {
return messageMetadataContainer;
}

@Override
public MetadataContainer getApplicationMetadataContainer() {
return applicationMetadataContainer;
}

public static MetadataContainer getStaticApplicationMetadataContainer() {
return applicationMetadataContainer;
}

@Override
public MetadataContainer getCustomMetadataContainer() {
return customMetadataContainer;
}

}
Loading
Loading