Skip to content

Commit

Permalink
feat: support lossless config from console & support warmup. (#1446)
Browse files Browse the repository at this point in the history
Co-authored-by: shedfreewu <[email protected]>
  • Loading branch information
SkyeBeFreeman and shedfreewu authored Oct 28, 2024
1 parent 56ad635 commit 5aaf5b6
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 113 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@
- [fix: fix PolarisCircuitBreakerConfiguration not clear when gateway invoke by wildcard apis](https://github.com/Tencent/spring-cloud-tencent/pull/1423)
- [fix:fix actuator name warning from #1428 .](https://github.com/Tencent/spring-cloud-tencent/pull/1430)
- [feat:upgrade api circuit breaker.](https://github.com/Tencent/spring-cloud-tencent/pull/1440)
- [feat: support lossless config from console & support warmup.](https://github.com/Tencent/spring-cloud-tencent/pull/1446)
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class PolarisLoadBalancerClientConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.strategy", havingValue = "roundRobin", matchIfMissing = true)
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.strategy", havingValue = "roundRobin")
public ReactorLoadBalancer<ServiceInstance> roundRobinLoadBalancer(Environment environment,
LoadBalancerClientFactory loadBalancerClientFactory) {
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
Expand Down Expand Up @@ -97,7 +97,7 @@ public ReactorLoadBalancer<ServiceInstance> polarisRingHashLoadBalancer(Environm

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.strategy", havingValue = "polarisWeightedRoundRobin")
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.strategy", havingValue = "polarisWeightedRoundRobin", matchIfMissing = true)
public ReactorLoadBalancer<ServiceInstance> polarisWeightedRoundRobinLoadBalancer(Environment environment,
LoadBalancerClientFactory loadBalancerClientFactory, PolarisSDKContextManager polarisSDKContextManager) {
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.polaris.api.config.provider.LosslessConfig;
import com.tencent.polaris.specification.api.v1.traffic.manage.LosslessProto;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -35,14 +36,23 @@
*/
public class LosslessConfigModifierTest {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
private final ApplicationContextRunner delayRegisterContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TestApplication.class))
.withPropertyValues("spring.cloud.nacos.discovery.enabled=false")
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.lossless.enabled=true")
.withPropertyValues("spring.cloud.polaris.lossless.port=20000")
.withPropertyValues("spring.cloud.polaris.lossless.healthCheckPath=/xxx")
.withPropertyValues("spring.cloud.polaris.lossless.delayRegisterInterval=10")
.withPropertyValues("spring.application.name=test")
.withPropertyValues("spring.cloud.gateway.enabled=false");

private final ApplicationContextRunner healthCheckContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TestApplication.class))
.withPropertyValues("spring.cloud.nacos.discovery.enabled=false")
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.lossless.enabled=true")
.withPropertyValues("spring.cloud.polaris.lossless.port=20000")
.withPropertyValues("spring.cloud.polaris.lossless.healthCheckPath=/xxx")
.withPropertyValues("spring.cloud.polaris.lossless.healthCheckInterval=5")
.withPropertyValues("spring.application.name=test")
.withPropertyValues("spring.cloud.gateway.enabled=false");
Expand All @@ -60,15 +70,29 @@ void setUp() {
}

@Test
void testModify() {
contextRunner.run(context -> {
void testDelayRegister() {
delayRegisterContextRunner.run(context -> {
PolarisSDKContextManager polarisSDKContextManager = context.getBean(PolarisSDKContextManager.class);
LosslessConfig losslessConfig = polarisSDKContextManager.getSDKContext().
getConfig().getProvider().getLossless();
assertThat(losslessConfig.getHost()).isEqualTo("0.0.0.0");
assertThat(losslessConfig.getPort()).isEqualTo(20000);
assertThat(losslessConfig.getDelayRegisterInterval()).isEqualTo(10);
assertThat(losslessConfig.getStrategy()).isEqualTo(LosslessProto.DelayRegister.DelayStrategy.DELAY_BY_TIME);
});
}

@Test
void testHealthCheck() {
healthCheckContextRunner.run(context -> {
PolarisSDKContextManager polarisSDKContextManager = context.getBean(PolarisSDKContextManager.class);
LosslessConfig losslessConfig = polarisSDKContextManager.getSDKContext().
getConfig().getProvider().getLossless();
assertThat(losslessConfig.getHost()).isEqualTo("0.0.0.0");
assertThat(losslessConfig.getPort()).isEqualTo(20000);
assertThat(losslessConfig.getHealthCheckPath()).isEqualTo("/xxx");
assertThat(losslessConfig.getHealthCheckInterval()).isEqualTo(5);
assertThat(losslessConfig.getStrategy()).isEqualTo(LosslessProto.DelayRegister.DelayStrategy.DELAY_BY_HEALTH_CHECK);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.plugin.lossless.config;

import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.polaris.api.config.consumer.WeightAdjustConfig;
import com.tencent.polaris.plugin.lossless.warmup.WarmupWeightAdjuster;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Test for {@link WarmupConfigModifier}.
*
* @author Shedfree Wu
*/
public class WarmupConfigModifierTest {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TestApplication.class))
.withPropertyValues("spring.cloud.nacos.discovery.enabled=false")
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.warmup.enabled=true")
.withPropertyValues("spring.application.name=test")
.withPropertyValues("spring.cloud.gateway.enabled=false");
private final ApplicationContextRunner disabledContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TestApplication.class))
.withPropertyValues("spring.cloud.nacos.discovery.enabled=false")
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.warmup.enabled=false")
.withPropertyValues("spring.application.name=test")
.withPropertyValues("spring.cloud.gateway.enabled=false");

@BeforeEach
void setUp() {
PolarisSDKContextManager.innerDestroy();
}

@Test
void testModify() {
contextRunner.run(context -> {
PolarisSDKContextManager polarisSDKContextManager = context.getBean(PolarisSDKContextManager.class);
WeightAdjustConfig weightAdjustConfig = polarisSDKContextManager.getSDKContext().
getConfig().getConsumer().getWeightAdjust();
assertThat(weightAdjustConfig.isEnable()).isTrue();
assertThat(weightAdjustConfig.getChain().contains(WarmupWeightAdjuster.WARMUP_WEIGHT_ADJUSTER_NAME)).isTrue();
});
}


@Test
void testDisabled() {
disabledContextRunner.run(context -> {
PolarisSDKContextManager polarisSDKContextManager = context.getBean(PolarisSDKContextManager.class);
WeightAdjustConfig weightAdjustConfig = polarisSDKContextManager.getSDKContext().
getConfig().getConsumer().getWeightAdjust();
assertThat(weightAdjustConfig.isEnable()).isTrue();
assertThat(weightAdjustConfig.getChain().contains(WarmupWeightAdjuster.WARMUP_WEIGHT_ADJUSTER_NAME)).isFalse();
});
}

@SpringBootApplication
protected static class TestApplication {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

package org.springframework.cloud.client.serviceregistry;

public class AutoServiceRegistrationUtils {
public final class AutoServiceRegistrationUtils {

private AutoServiceRegistrationUtils() {

}

public static void register(AbstractAutoServiceRegistration autoServiceRegistration) {
autoServiceRegistration.register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<artifactId>lossless-deregister</artifactId>
</dependency>

<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>lossless-warmup</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.cloud.rpc.enhancement.transformer.RegistrationTransformer;
import com.tencent.polaris.api.pojo.BaseInstance;
import com.tencent.polaris.plugin.lossless.common.HttpLosslessActionProvider;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
Expand Down Expand Up @@ -78,12 +79,13 @@ public Object invokeRegister(ProceedingJoinPoint joinPoint) throws Throwable {
}

// web started, get port from registration
BaseInstance instance = SpringCloudLosslessActionProvider.getBaseInstance(registration, registrationTransformer);
BaseInstance instance = getBaseInstance(registration, registrationTransformer);

Runnable registerAction = () -> executeJoinPoint(joinPoint);

SpringCloudLosslessActionProvider losslessActionProvider =
new SpringCloudLosslessActionProvider(serviceRegistry, registration, losslessProperties, registerAction);
Runnable deregisterAction = () -> serviceRegistry.deregister(registration);
HttpLosslessActionProvider losslessActionProvider =
new HttpLosslessActionProvider(registerAction, deregisterAction, registration.getPort(),
instance, polarisSDKContextManager.getSDKContext().getExtensions());

polarisSDKContextManager.getLosslessAPI().setLosslessActionProvider(instance, losslessActionProvider);
polarisSDKContextManager.getLosslessAPI().losslessRegister(instance);
Expand All @@ -104,4 +106,8 @@ public void executeJoinPoint(ProceedingJoinPoint joinPoint) {
throw new RuntimeException(e);
}
}

public static BaseInstance getBaseInstance(Registration registration, RegistrationTransformer registrationTransformer) {
return registrationTransformer.transform(registration);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

import com.tencent.cloud.common.constant.OrderConstant.Modifier;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.factory.config.provider.LosslessConfigImpl;
import com.tencent.polaris.specification.api.v1.traffic.manage.LosslessProto;

/**
* Config modifier for lossless.
Expand All @@ -39,17 +41,28 @@ public LosslessConfigModifier(LosslessProperties losslessProperties) {

@Override
public void modify(ConfigurationImpl configuration) {
LosslessConfigImpl losslessConfig = (LosslessConfigImpl) configuration.getProvider().getLossless();
if (losslessProperties.isEnabled()) {
LosslessConfigImpl losslessConfig = (LosslessConfigImpl) configuration.getProvider().getLossless();
losslessConfig.setEnable(true);
losslessConfig.setPort(losslessProperties.getPort());
if (Objects.nonNull(losslessProperties.getDelayRegisterInterval())) {
losslessConfig.setDelayRegisterInterval(losslessProperties.getDelayRegisterInterval());
}
if (Objects.nonNull(losslessProperties.getHealthCheckInterval())) {
losslessConfig.setHealthCheckInterval(losslessProperties.getHealthCheckInterval());
if (StringUtils.isNotEmpty(losslessProperties.getHealthCheckPath())) {
losslessConfig.setHealthCheckPath(losslessProperties.getHealthCheckPath());
losslessConfig.setStrategy(LosslessProto.DelayRegister.DelayStrategy.DELAY_BY_HEALTH_CHECK);

if (Objects.nonNull(losslessProperties.getHealthCheckInterval())) {
losslessConfig.setHealthCheckInterval(losslessProperties.getHealthCheckInterval());
}
}
else {
losslessConfig.setStrategy(LosslessProto.DelayRegister.DelayStrategy.DELAY_BY_TIME);
}
}
else {
losslessConfig.setEnable(false);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisEnabled
@EnableConfigurationProperties(LosslessProperties.class)
@EnableConfigurationProperties({LosslessProperties.class, WarmupProperties.class})
public class LosslessPropertiesAutoConfiguration {

@Bean
Expand All @@ -41,4 +41,9 @@ public LosslessConfigModifier losslessConfigModifier(LosslessProperties lossless
return new LosslessConfigModifier(losslessProperties);
}

@Bean
@ConditionalOnMissingBean
public WarmupConfigModifier warmupConfigModifier(WarmupProperties warmupProperties) {
return new WarmupConfigModifier(warmupProperties);
}
}
Loading

0 comments on commit 5aaf5b6

Please sign in to comment.