Skip to content

Commit

Permalink
feat: support lossless config from console & support warmup (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
shedfreewu authored Oct 17, 2024
1 parent 1ee5edc commit 59c38e2
Show file tree
Hide file tree
Showing 50 changed files with 1,950 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public Instance getOneInstance(GetOneInstanceRequest request) {
ServiceInstances routerInstances =
BaseFlow.processServiceRouters(routeInfo, dstInstances, extensions.getConfigRouterChainGroup());
LoadBalancer loadBalancer = extensions.getLoadBalancer();
return BaseFlow.processLoadBalance(loadBalancer, request.getCriteria(), routerInstances);
return BaseFlow.processLoadBalance(loadBalancer, request.getCriteria(), routerInstances, extensions.getWeightAdjusters());
}

private static CommonInstancesRequest buildCommonInstancesRequest(GetOneInstanceRequest request, Configuration configuration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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.plugin.weight.WeightAdjuster;
import com.tencent.polaris.api.pojo.*;
import com.tencent.polaris.api.pojo.ServiceEventKey.EventType;
import com.tencent.polaris.api.rpc.Criteria;
Expand Down Expand Up @@ -111,7 +112,7 @@ public static Instance commonGetOneInstance(Extensions extensions, ServiceKey se
.getPlugin(PluginTypes.LOAD_BALANCER.getBaseType(), lbPolicy);
Criteria criteria = new Criteria();
criteria.setHashKey(hashKey);
return BaseFlow.processLoadBalance(loadBalancer, criteria, instancesAfterRoute);
return BaseFlow.processLoadBalance(loadBalancer, criteria, instancesAfterRoute, extensions.getWeightAdjusters());
}

/**
Expand Down Expand Up @@ -296,8 +297,27 @@ private static boolean loadLocalResources(ServiceEventKey svcEventKey, Resources
}

public static Instance processLoadBalance(LoadBalancer loadBalancer, Criteria criteria,
ServiceInstances dstInstances) throws PolarisException {
ServiceInstances dstInstances, List<WeightAdjuster> weightAdjusters) throws PolarisException {
if (criteria == null) {
criteria = new Criteria();
}
Map<String, InstanceWeight> dynamicWeight = new HashMap<>();
if (CollectionUtils.isNotEmpty(weightAdjusters)) {
for (WeightAdjuster weightAdjuster : weightAdjusters) {
dynamicWeight = weightAdjuster.timingAdjustDynamicWeight(dynamicWeight, dstInstances);
}
if (CollectionUtils.isNotEmpty(criteria.getDynamicWeight())) {
// rebuild dstInstances with new total weight
int totalWeight = 0;
for (Map.Entry<String, InstanceWeight> weightEntry : criteria.getDynamicWeight().entrySet()) {
totalWeight += weightEntry.getValue().getDynamicWeight();
}
dstInstances = new ServiceInstancesWrap(dstInstances, dstInstances.getInstances(), totalWeight);
}
}
criteria.setDynamicWeight(dynamicWeight);
Instance instance = loadBalancer.chooseInstance(criteria, dstInstances);
LOG.debug("[processLoadBalance] choose instance: {}", instance);
if (null == instance) {
throw new PolarisException(ErrorCode.INSTANCE_NOT_FOUND,
String.format("no suitable instance for service %s after loadbanlancer %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ consumer:
subscribe:
#服务订阅执行回调Listener的线程数量
callbackConcurrency: 1
weightAdjust:
enable: true
chain:
# 开启了服务预热插件,可以支持多个动态权重调整插件同时生效
# - warmup
# 被调方配置
provider:
# Switch of registration
Expand All @@ -300,6 +305,7 @@ provider:
host: 0.0.0.0
# 优雅上下线功能需要对外提供接口,接口的监听端口
port: 28080
strategy: DELAY_BY_TIME
# 优雅上线时,如果没有启动探测,则兜底延迟多久会进行上线
delayRegisterInterval: 30s
# 优雅上线的探测周期
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ public interface ConsumerConfig extends Verifier {
* @return Map of {@link DiscoveryConfig}
*/
Map<String, ? extends DiscoveryConfig> getDiscoveryConfigMap();

WeightAdjustConfig getWeightAdjust();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.tencent.polaris.api.config.consumer;

import java.util.List;

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

public interface WeightAdjustConfig extends PluginConfig, Verifier {

boolean isEnable();

List<String> getChain();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.tencent.polaris.api.config.provider;

import com.tencent.polaris.api.config.verify.Verifier;
import com.tencent.polaris.specification.api.v1.traffic.manage.LosslessProto;

public interface LosslessConfig extends Verifier {

Expand Down Expand Up @@ -51,5 +52,14 @@ public interface LosslessConfig extends Verifier {
*/
long getHealthCheckInterval();

String getType();

LosslessProto.DelayRegister.DelayStrategy getStrategy();

String getHealthCheckProtocol();

String getHealthCheckPath();

String getHealthCheckMethod();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.polaris.api.config.consumer.ConsumerConfig;
import com.tencent.polaris.api.config.consumer.WeightAdjustConfig;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.factory.util.ConfigUtils;

Expand Down Expand Up @@ -63,6 +64,9 @@ public class ConsumerConfigImpl implements ConsumerConfig {
@JsonIgnore
private final Map<String, DiscoveryConfigImpl> discoveryConfigMap = new ConcurrentHashMap<>();

@JsonProperty
private WeightAdjustConfigImpl weightAdjust;

@Override
public LocalCacheConfigImpl getLocalCache() {
return localCache;
Expand Down Expand Up @@ -123,13 +127,19 @@ public Map<String, DiscoveryConfigImpl> getDiscoveryConfigMap() {
return discoveryConfigMap;
}

@Override
public WeightAdjustConfig getWeightAdjust() {
return weightAdjust;
}

@Override
public void verify() {
ConfigUtils.validateNull(localCache, "localCache");
ConfigUtils.validateNull(serviceRouter, "serviceRouter");
ConfigUtils.validateNull(loadbalancer, "loadbalancer");
ConfigUtils.validateNull(circuitBreaker, "circuitBreaker");
ConfigUtils.validateNull(outlierDetection, "outlierDetection");
ConfigUtils.validateNull(weightAdjust, "weightAdjust");

localCache.verify();
serviceRouter.verify();
Expand All @@ -138,6 +148,7 @@ public void verify() {
outlierDetection.verify();
subscribe.verify();
zeroProtection.verify();
weightAdjust.verify();
if (CollectionUtils.isNotEmpty(discoveries)) {
for (DiscoveryConfigImpl discoveryConfig : discoveries) {
discoveryConfig.verify();
Expand Down Expand Up @@ -169,6 +180,9 @@ public void setDefault(Object defaultObject) {
if (null == zeroProtection) {
zeroProtection = new ZeroProtectionConfigImpl();
}
if (null == weightAdjust) {
weightAdjust = new WeightAdjustConfigImpl();
}
if (null != defaultObject) {
ConsumerConfig consumerConfig = (ConsumerConfig) defaultObject;
localCache.setDefault(consumerConfig.getLocalCache());
Expand All @@ -178,6 +192,7 @@ public void setDefault(Object defaultObject) {
outlierDetection.setDefault(consumerConfig.getOutlierDetection());
subscribe.setDefault(consumerConfig.getSubscribe());
zeroProtection.setDefault(consumerConfig.getZeroProtection());
weightAdjust.setDefault(consumerConfig.getWeightAdjust());
if (CollectionUtils.isNotEmpty(discoveries)) {
for (DiscoveryConfigImpl discoveryConfig : discoveries) {
discoveryConfig.setDefault(consumerConfig.getDiscoveries().get(0));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.factory.config.consumer;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.polaris.api.config.consumer.WeightAdjustConfig;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.factory.config.plugin.PluginConfigImpl;
import com.tencent.polaris.factory.util.ConfigUtils;

public class WeightAdjustConfigImpl extends PluginConfigImpl implements WeightAdjustConfig {

@JsonProperty
private Boolean enable;

@JsonProperty
private List<String> chain;

@Override
public boolean isEnable() {
if (null == enable) {
return false;
}
return enable;
}

public void setEnable(boolean enable) {
this.enable = enable;
}

@Override
public List<String> getChain() {
return chain;
}

public void setChain(List<String> chain) {
this.chain = chain;
}

@Override
public void verify() {
ConfigUtils.validateNull(enable, "weightAdjust.enable");
if (!enable) {
return;
}
verifyPluginConfig();
}

@Override
public void setDefault(Object defaultObject) {
if (null != defaultObject) {
WeightAdjustConfig defaultConfig = (WeightAdjustConfig) defaultObject;
if (null == enable) {
enable = defaultConfig.isEnable();
}
if (null == chain) {
chain = defaultConfig.getChain();
}
if (enable) {
setDefaultPluginConfig(defaultConfig);
}
}
}

@Override
public String toString() {
return "WeightAdjustConfigImpl{" +
"enable=" + enable +
", chain=" + chain +
"} ";
}
}
Loading

0 comments on commit 59c38e2

Please sign in to comment.