feat:added polaris weighted round robin load balancer. (#1072)
parent
cc8d72cf14
commit
98a5454eeb
@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
* 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 java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
|
||||||
|
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
|
||||||
|
import com.tencent.polaris.api.pojo.Instance;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceInstances;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||||
|
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.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.DefaultResponse;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.Request;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.Response;
|
||||||
|
import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
|
||||||
|
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
|
||||||
|
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract Loadbalancer of Polaris.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:veteranchen@tencent.com">veteranchen</a>
|
||||||
|
*/
|
||||||
|
public abstract class AbstractPolarisLoadBalancer implements ReactorServiceInstanceLoadBalancer {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(AbstractPolarisLoadBalancer.class);
|
||||||
|
|
||||||
|
private final String serviceId;
|
||||||
|
|
||||||
|
private final RouterAPI routerAPI;
|
||||||
|
|
||||||
|
private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider;
|
||||||
|
|
||||||
|
public AbstractPolarisLoadBalancer(String serviceId, ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider, RouterAPI routerAPI) {
|
||||||
|
this.serviceId = serviceId;
|
||||||
|
this.supplierObjectProvider = supplierObjectProvider;
|
||||||
|
this.routerAPI = routerAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ServiceInstances convertToPolarisServiceInstances(List<ServiceInstance> serviceInstances) {
|
||||||
|
ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceInstances.get(0).getServiceId());
|
||||||
|
List<Instance> polarisInstances = serviceInstances.stream()
|
||||||
|
.map(serviceInstance -> ((PolarisServiceInstance) serviceInstance).getPolarisInstance())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
return new DefaultServiceInstances(serviceKey, polarisInstances);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Response<ServiceInstance>> choose(Request request) {
|
||||||
|
ServiceInstanceListSupplier supplier = supplierObjectProvider
|
||||||
|
.getIfAvailable(NoopServiceInstanceListSupplier::new);
|
||||||
|
return supplier.get(request).next().map(serviceInstances -> {
|
||||||
|
if (serviceInstances.isEmpty()) {
|
||||||
|
log.warn("No servers available for service: " + this.serviceId);
|
||||||
|
return new EmptyResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
ProcessLoadBalanceRequest req = new ProcessLoadBalanceRequest();
|
||||||
|
req.setDstInstances(convertToPolarisServiceInstances(serviceInstances));
|
||||||
|
req = setProcessLoadBalanceRequest(req);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ProcessLoadBalanceResponse response = routerAPI.processLoadBalance(req);
|
||||||
|
return new DefaultResponse(new PolarisServiceInstance(response.getTargetInstance()));
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
log.warn("PolarisRoutingLoadbalancer error", e);
|
||||||
|
return new EmptyResponse();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract ProcessLoadBalanceRequest setProcessLoadBalanceRequest(ProcessLoadBalanceRequest req);
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* 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.api.rpc.Criteria;
|
||||||
|
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||||
|
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WeightedRoundRobin Loadbalancer of Polaris.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:veteranchen@tencent.com">veteranchen</a>
|
||||||
|
*/
|
||||||
|
public class PolarisWeightedRoundRobinLoadBalancer extends AbstractPolarisLoadBalancer {
|
||||||
|
|
||||||
|
public PolarisWeightedRoundRobinLoadBalancer(String serviceId, ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider, RouterAPI routerAPI) {
|
||||||
|
super(serviceId, supplierObjectProvider, routerAPI);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ProcessLoadBalanceRequest setProcessLoadBalanceRequest(ProcessLoadBalanceRequest req) {
|
||||||
|
req.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_ROUND_ROBIN);
|
||||||
|
req.setCriteria(new Criteria());
|
||||||
|
return req;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
* 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.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
|
||||||
|
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||||
|
import com.tencent.polaris.api.pojo.Instance;
|
||||||
|
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||||
|
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.Request;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.Response;
|
||||||
|
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.common.metadata.MetadataContext.LOCAL_NAMESPACE;
|
||||||
|
import static com.tencent.cloud.common.metadata.MetadataContext.LOCAL_SERVICE;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link PolarisWeightedRandomLoadBalancer}.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:veteranchen@tencent.com">veteranchen</a>
|
||||||
|
*/
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class PolarisWeightedRoundRobinLoadBalancerTest {
|
||||||
|
|
||||||
|
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||||
|
private static Instance testInstance;
|
||||||
|
@Mock
|
||||||
|
private RouterAPI routerAPI;
|
||||||
|
@Mock
|
||||||
|
private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void beforeAll() {
|
||||||
|
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||||
|
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||||
|
.thenReturn("unit-test");
|
||||||
|
|
||||||
|
testInstance = Instance.createDefaultInstance("instance-id", LOCAL_NAMESPACE,
|
||||||
|
LOCAL_SERVICE, "host", 8090);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void afterAll() {
|
||||||
|
mockedApplicationContextAwareUtils.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void chooseNormalLogicTest_thenReturnAvailablePolarisInstance() {
|
||||||
|
|
||||||
|
Request request = Mockito.mock(Request.class);
|
||||||
|
List<ServiceInstance> mockInstanceList = new ArrayList<>();
|
||||||
|
mockInstanceList.add(new PolarisServiceInstance(testInstance));
|
||||||
|
|
||||||
|
ServiceInstanceListSupplier serviceInstanceListSupplier = Mockito.mock(ServiceInstanceListSupplier.class);
|
||||||
|
when(serviceInstanceListSupplier.get(request)).thenReturn(Flux.just(mockInstanceList));
|
||||||
|
|
||||||
|
when(supplierObjectProvider.getIfAvailable(any())).thenReturn(serviceInstanceListSupplier);
|
||||||
|
|
||||||
|
ProcessLoadBalanceResponse mockLbRes = new ProcessLoadBalanceResponse(testInstance);
|
||||||
|
when(routerAPI.processLoadBalance(any())).thenReturn(mockLbRes);
|
||||||
|
|
||||||
|
// request construct and execute invoke
|
||||||
|
PolarisWeightedRoundRobinLoadBalancer polarisWeightedRoundRobinLoadBalancer = new PolarisWeightedRoundRobinLoadBalancer(LOCAL_SERVICE, supplierObjectProvider, routerAPI);
|
||||||
|
Mono<Response<ServiceInstance>> responseMono = polarisWeightedRoundRobinLoadBalancer.choose(request);
|
||||||
|
ServiceInstance serviceInstance = responseMono.block().getServer();
|
||||||
|
|
||||||
|
// verify method has invoked
|
||||||
|
verify(supplierObjectProvider).getIfAvailable(any());
|
||||||
|
|
||||||
|
//result assert
|
||||||
|
Assertions.assertThat(serviceInstance).isNotNull();
|
||||||
|
Assertions.assertThat(serviceInstance instanceof PolarisServiceInstance).isTrue();
|
||||||
|
|
||||||
|
PolarisServiceInstance polarisServiceInstance = (PolarisServiceInstance) serviceInstance;
|
||||||
|
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getId()).isEqualTo("instance-id");
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getNamespace()).isEqualTo(LOCAL_NAMESPACE);
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getService()).isEqualTo(LOCAL_SERVICE);
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getHost()).isEqualTo("host");
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getPort()).isEqualTo(8090);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue