feature: add polaris ring hash load balancer (#970)
parent
d6794c31ff
commit
2161f5505b
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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.cloud.common.metadata.MetadataContextHolder;
|
||||
|
||||
/**
|
||||
* PolarisLoadBalancerRingHashKeyProvider.
|
||||
*
|
||||
* @author seanthefish
|
||||
*/
|
||||
public final class PolarisLoadBalancerRingHashKeyProvider {
|
||||
|
||||
private static final String LOAD_BALANCER_HASH_KEY = "LOAD_BALANCER_HASH_KEY";
|
||||
|
||||
private PolarisLoadBalancerRingHashKeyProvider() {
|
||||
}
|
||||
|
||||
public static void hashKey(String key) {
|
||||
MetadataContextHolder.get().setLoadbalancer(LOAD_BALANCER_HASH_KEY, key);
|
||||
}
|
||||
|
||||
static String getHashKey() {
|
||||
return MetadataContextHolder.get().getLoadbalancerMetadata().get(LOAD_BALANCER_HASH_KEY);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* 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.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
|
||||
import com.tencent.polaris.api.config.consumer.LoadBalanceConfig;
|
||||
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.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.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;
|
||||
|
||||
/**
|
||||
* PolarisRingHashLoadBalancer.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class PolarisRingHashLoadBalancer implements ReactorServiceInstanceLoadBalancer {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(PolarisWeightedRandomLoadBalancer.class);
|
||||
|
||||
private final String serviceId;
|
||||
|
||||
private final RouterAPI routerAPI;
|
||||
|
||||
private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider;
|
||||
|
||||
public PolarisRingHashLoadBalancer(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);
|
||||
String hashKey = Optional.ofNullable(PolarisLoadBalancerRingHashKeyProvider.getHashKey()).orElse("");
|
||||
return supplier.get(request).next().map(serviceInstances -> getInstanceResponse(serviceInstances, hashKey));
|
||||
}
|
||||
|
||||
private Response<ServiceInstance> getInstanceResponse(List<ServiceInstance> serviceInstances, String hashKey) {
|
||||
if (serviceInstances.isEmpty()) {
|
||||
log.warn("No servers available for service: " + this.serviceId);
|
||||
return new EmptyResponse();
|
||||
}
|
||||
|
||||
ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
|
||||
request.setDstInstances(convertToPolarisServiceInstances(serviceInstances));
|
||||
request.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_RING_HASH);
|
||||
Criteria criteria = new Criteria();
|
||||
criteria.setHashKey(hashKey);
|
||||
request.setCriteria(criteria);
|
||||
|
||||
try {
|
||||
ProcessLoadBalanceResponse response = routerAPI.processLoadBalance(request);
|
||||
return new DefaultResponse(new PolarisServiceInstance(response.getTargetInstance()));
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.warn("PolarisRoutingLoadbalancer error", e);
|
||||
return new EmptyResponse();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
/*
|
||||
* 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.metadata.StaticMetadataManager;
|
||||
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||
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 org.springframework.context.ApplicationContext;
|
||||
|
||||
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.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* PolarisRingHashLoadBalancerTest.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class PolarisRingHashLoadBalancerTest {
|
||||
|
||||
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");
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
MetadataLocalProperties metadataLocalProperties = mock(MetadataLocalProperties.class);
|
||||
StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class);
|
||||
doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class);
|
||||
doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class);
|
||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext)
|
||||
.thenReturn(applicationContext);
|
||||
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
|
||||
PolarisRingHashLoadBalancer polarisRingHashLoadBalancer = new PolarisRingHashLoadBalancer(LOCAL_SERVICE, supplierObjectProvider, routerAPI);
|
||||
Mono<Response<ServiceInstance>> responseMono = polarisRingHashLoadBalancer.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);
|
||||
}
|
||||
}
|
40
spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/loadbalancer/PolarisWeightedRandomLoadBalancerAutoConfigurationTest.java
40
spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/loadbalancer/PolarisWeightedRandomLoadBalancerAutoConfigurationTest.java
Loading…
Reference in new issue