Skip to content

Commit

Permalink
feat:added ribbon lb polaris weighted round robin rule (#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
veterancj authored Aug 4, 2023
1 parent 8f60cbd commit abaf028
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 101 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
- fix:fix router label feign interceptor order.
- fix:fix web server wrong open bug.
- fix:add deregister judgment.
- feat:added ribbon lb polaris weighted round robin rule.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.tencent.cloud.polaris.loadbalancer.PolarisLoadBalancer;
import com.tencent.cloud.polaris.loadbalancer.PolarisRingHashRule;
import com.tencent.cloud.polaris.loadbalancer.PolarisWeightedRandomRule;
import com.tencent.cloud.polaris.loadbalancer.PolarisWeightedRoundRobinRule;
import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerProperties;
import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor;
import com.tencent.cloud.polaris.router.spi.RouterResponseInterceptor;
Expand Down Expand Up @@ -74,6 +75,7 @@ public class PolarisLoadBalancerCompositeRule extends AbstractLoadBalancerRule {
final static String STRATEGY_RANDOM = "random";
final static String STRATEGY_ROUND_ROBIN = "roundRobin";
final static String STRATEGY_WEIGHT = "polarisWeightedRandom";
final static String STRATEGY_WEIGHT_ROUND_ROBIN = "polarisWeightedRoundRobin";
final static String STRATEGY_HASH_RING = "polarisRingHash";
final static String STRATEGY_RETRY = "retry";
final static String STRATEGY_RESPONSE_TIME_WEIGHTED = "responseTimeWeighted";
Expand Down Expand Up @@ -231,6 +233,8 @@ public AbstractLoadBalancerRule getRule() {
return new RandomRule();
case STRATEGY_WEIGHT:
return new PolarisWeightedRandomRule(routerAPI);
case STRATEGY_WEIGHT_ROUND_ROBIN:
return new PolarisWeightedRoundRobinRule(routerAPI);
case STRATEGY_HASH_RING:
return new PolarisRingHashRule(routerAPI);
case STRATEGY_RETRY:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.tencent.cloud.common.pojo.PolarisServer;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.polaris.loadbalancer.PolarisWeightedRandomRule;
import com.tencent.cloud.polaris.loadbalancer.PolarisWeightedRoundRobinRule;
import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;
Expand Down Expand Up @@ -143,6 +144,17 @@ public void testWeightLB() {
assertThat(lbRule).isInstanceOf(PolarisWeightedRandomRule.class);
}

@Test
public void testWeightRRLB() {
when(polarisLoadBalancerProperties.getStrategy()).thenReturn(PolarisLoadBalancerCompositeRule.STRATEGY_WEIGHT_ROUND_ROBIN);
PolarisLoadBalancerCompositeRule compositeRule = new PolarisLoadBalancerCompositeRule(routerAPI,
polarisLoadBalancerProperties, config, requestInterceptors, null, null);

AbstractLoadBalancerRule lbRule = compositeRule.getRule();

assertThat(lbRule).isInstanceOf(PolarisWeightedRoundRobinRule.class);
}

@Test
public void testRetryLB() {
when(polarisLoadBalancerProperties.getStrategy()).thenReturn(PolarisLoadBalancerCompositeRule.STRATEGY_RETRY);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent 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.cloud.polaris.loadbalancer;

import java.util.List;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractServerPredicate;
import com.netflix.loadbalancer.AvailabilityPredicate;
import com.netflix.loadbalancer.CompositePredicate;
import com.netflix.loadbalancer.PredicateBasedRule;
import com.netflix.loadbalancer.Server;
import com.tencent.cloud.common.pojo.PolarisServer;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;

import org.springframework.util.CollectionUtils;

/**
* Abstract polaris load balancer rule.
*
* @author <a href="mailto:[email protected]">veteranchen</a>
*/
public abstract class AbstractPolarisRule extends PredicateBasedRule {
private final RouterAPI routerAPI;
private CompositePredicate compositePredicate;

public AbstractPolarisRule(RouterAPI routerAPI) {
this.routerAPI = routerAPI;
}

@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
AvailabilityPredicate availabilityPredicate = new AvailabilityPredicate(this, clientConfig);
compositePredicate = CompositePredicate.withPredicates(availabilityPredicate).build();
}

@Override
public AbstractServerPredicate getPredicate() {
return compositePredicate;
}

@Override
public Server choose(Object key) {
List<Server> servers = getLoadBalancer().getReachableServers();
if (CollectionUtils.isEmpty(servers)) {
return null;
}

// filter circuit breaker servers by ribbon
if (compositePredicate != null) {
servers = compositePredicate.getEligibleServers(servers);
}

ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(servers);
ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
request.setDstInstances(serviceInstances);
request = setProcessLoadBalanceRequest(request);
ProcessLoadBalanceResponse processLoadBalanceResponse = routerAPI.processLoadBalance(request);
Instance targetInstance = processLoadBalanceResponse.getTargetInstance();
return new PolarisServer(serviceInstances, targetInstance);
}

protected abstract ProcessLoadBalanceRequest setProcessLoadBalanceRequest(ProcessLoadBalanceRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,77 +18,31 @@

package com.tencent.cloud.polaris.loadbalancer;

import java.util.List;
import java.util.Optional;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractServerPredicate;
import com.netflix.loadbalancer.AvailabilityPredicate;
import com.netflix.loadbalancer.CompositePredicate;
import com.netflix.loadbalancer.PredicateBasedRule;
import com.netflix.loadbalancer.Server;
import com.tencent.cloud.common.pojo.PolarisServer;
import com.tencent.polaris.api.config.consumer.LoadBalanceConfig;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.api.rpc.Criteria;
import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;

import org.springframework.util.CollectionUtils;

/**
* Polaris weighted load balancer.
*
* @author lepdou 2022-05-17
*/
public class PolarisRingHashRule extends PredicateBasedRule {

private final RouterAPI routerAPI;
private CompositePredicate compositePredicate;
public class PolarisRingHashRule extends AbstractPolarisRule {

public PolarisRingHashRule(RouterAPI routerAPI) {
this.routerAPI = routerAPI;
}

@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
AvailabilityPredicate availabilityPredicate = new AvailabilityPredicate(this, clientConfig);
compositePredicate = CompositePredicate.withPredicates(availabilityPredicate).build();
}

@Override
public AbstractServerPredicate getPredicate() {
return compositePredicate;
super(routerAPI);
}

@Override
public Server choose(Object key) {
List<Server> servers = getLoadBalancer().getReachableServers();
if (CollectionUtils.isEmpty(servers)) {
return null;
}

// filter circuit breaker servers by ribbon
if (compositePredicate != null) {
servers = compositePredicate.getEligibleServers(servers);
}

ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(servers);

ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
request.setDstInstances(serviceInstances);
protected ProcessLoadBalanceRequest setProcessLoadBalanceRequest(ProcessLoadBalanceRequest request) {
request.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_RING_HASH);
Criteria criteria = new Criteria();
String hashKey = Optional.ofNullable(PolarisLoadBalancerRingHashKeyProvider.getHashKey()).orElse("");
criteria.setHashKey(hashKey);
request.setCriteria(criteria);

ProcessLoadBalanceResponse processLoadBalanceResponse = routerAPI.processLoadBalance(request);

Instance targetInstance = processLoadBalanceResponse.getTargetInstance();

return new PolarisServer(serviceInstances, targetInstance);
return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,71 +18,24 @@

package com.tencent.cloud.polaris.loadbalancer;

import java.util.List;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractServerPredicate;
import com.netflix.loadbalancer.AvailabilityPredicate;
import com.netflix.loadbalancer.CompositePredicate;
import com.netflix.loadbalancer.PredicateBasedRule;
import com.netflix.loadbalancer.Server;
import com.tencent.cloud.common.pojo.PolarisServer;
import com.tencent.polaris.api.config.consumer.LoadBalanceConfig;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;

import org.springframework.util.CollectionUtils;

/**
* Polaris weighted load balancer.
*
* @author lepdou 2022-05-17
*/
public class PolarisWeightedRandomRule extends PredicateBasedRule {

private final RouterAPI routerAPI;
private CompositePredicate compositePredicate;
public class PolarisWeightedRandomRule extends AbstractPolarisRule {

public PolarisWeightedRandomRule(RouterAPI routerAPI) {
this.routerAPI = routerAPI;
}

@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
AvailabilityPredicate availabilityPredicate = new AvailabilityPredicate(this, clientConfig);
compositePredicate = CompositePredicate.withPredicates(availabilityPredicate).build();
super(routerAPI);
}

@Override
public AbstractServerPredicate getPredicate() {
return compositePredicate;
}

@Override
public Server choose(Object key) {
List<Server> servers = getLoadBalancer().getReachableServers();
if (CollectionUtils.isEmpty(servers)) {
return null;
}

// filter circuit breaker servers by ribbon
if (compositePredicate != null) {
servers = compositePredicate.getEligibleServers(servers);
}

ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(servers);

ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
request.setDstInstances(serviceInstances);
protected ProcessLoadBalanceRequest setProcessLoadBalanceRequest(ProcessLoadBalanceRequest request) {
request.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);

ProcessLoadBalanceResponse processLoadBalanceResponse = routerAPI.processLoadBalance(request);

Instance targetInstance = processLoadBalanceResponse.getTargetInstance();

return new PolarisServer(serviceInstances, targetInstance);
return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent 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.cloud.polaris.loadbalancer;

import com.tencent.polaris.api.config.consumer.LoadBalanceConfig;
import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;

/**
* Polaris weighted load balancer.
*
* @author <a href="mailto:[email protected]">veteranchen</a>
*/
public class PolarisWeightedRoundRobinRule extends AbstractPolarisRule {

public PolarisWeightedRoundRobinRule(RouterAPI routerAPI) {
super(routerAPI);
}

@Override
protected ProcessLoadBalanceRequest setProcessLoadBalanceRequest(ProcessLoadBalanceRequest request) {
request.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_ROUND_ROBIN);
return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.tencent.cloud.polaris.loadbalancer.PolarisLoadBalancer;
import com.tencent.cloud.polaris.loadbalancer.PolarisRingHashRule;
import com.tencent.cloud.polaris.loadbalancer.PolarisWeightedRandomRule;
import com.tencent.cloud.polaris.loadbalancer.PolarisWeightedRoundRobinRule;
import com.tencent.cloud.polaris.loadbalancer.transformer.InstanceTransformer;

import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -74,6 +75,13 @@ public IRule polarisWeightedRandomRule(PolarisSDKContextManager polarisSDKContex
return new PolarisWeightedRandomRule(polarisSDKContextManager.getRouterAPI());
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.strategy", havingValue = "polarisWeightedRoundRobin")
public IRule polarisWeightedRoundRobinRule(PolarisSDKContextManager polarisSDKContextManager) {
return new PolarisWeightedRoundRobinRule(polarisSDKContextManager.getRouterAPI());
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.strategy", havingValue = "polarisRingHash")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
"value": "polarisWeightedRandom",
"description": "polaris weighted random load balancer."
},
{
"value": "polarisWeightedRoundRobin",
"description": "polaris weighted round robin load balancer."
},
{
"value": "polarisRingHash",
"description": "polaris ring hash load balancer."
Expand Down
Loading

0 comments on commit abaf028

Please sign in to comment.