Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support lossless config from console & support warmup #1435

Merged
merged 7 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
- [fix: fix PolarisCircuitBreakerConfiguration not clear when gateway invoke by wildcard apis](https://github.com/Tencent/spring-cloud-tencent/pull/1418)
- [fix:fix actuator name warning from #1428 .](https://github.com/Tencent/spring-cloud-tencent/pull/1429)
- [feat:upgrade api circuit breaker.](https://github.com/Tencent/spring-cloud-tencent/pull/1438)
- [feat: support lossless config from console & support warmup.](https://github.com/Tencent/spring-cloud-tencent/pull/1435)
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@

<properties>
<!-- Project revision -->
<revision>2.0.0.0-2022.0.5-RC2</revision>
<revision>2.0.0.0-2022.0.5-SNAPSHOT</revision>

<!-- Spring Framework -->
<spring.framework.version>6.0.22</spring.framework.version>
Expand Down
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
4 changes: 2 additions & 2 deletions spring-cloud-tencent-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@

<properties>
<!-- Project revision -->
<revision>2.0.0.0-2022.0.5-RC2</revision>
<revision>2.0.0.0-2022.0.5-SNAPSHOT</revision>

<!-- Polaris SDK version -->
<polaris.version>2.0.0.0-RC2</polaris.version>
<polaris.version>2.0.0.0-SNAPSHOT</polaris.version>

<!-- Dependencies -->
<guava.version>32.0.1-jre</guava.version>
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
Loading
Loading