Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import com.alipay.sofa.boot.autoconfigure.condition.OnVirtualThreadStartupDisableCondition;
import com.alipay.sofa.boot.constant.SofaBootConstants;
import com.alipay.sofa.boot.log.SofaBootLoggerFactory;
import com.alipay.sofa.boot.reflection.ReflectionCache;
import com.alipay.sofa.common.thread.NamedThreadFactory;
import com.alipay.sofa.common.thread.SofaThreadPoolExecutor;
import com.alipay.sofa.common.thread.virtual.SofaVirtualThreadFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand Down Expand Up @@ -97,9 +99,11 @@ public HealthCheckerProcessor healthCheckerProcessor(HealthProperties healthChec
@Bean
@ConditionalOnMissingBean
public HealthIndicatorProcessor healthIndicatorProcessor(HealthProperties healthCheckProperties,
ExecutorService readinessHealthCheckExecutor) {
ExecutorService readinessHealthCheckExecutor,
ObjectProvider<ReflectionCache> reflectionCacheProvider) {
HealthIndicatorProcessor healthIndicatorProcessor = new HealthIndicatorProcessor();
healthIndicatorProcessor.setHealthCheckExecutor(readinessHealthCheckExecutor);
reflectionCacheProvider.ifAvailable(healthIndicatorProcessor::setReflectionCache);
healthIndicatorProcessor.initExcludedIndicators(healthCheckProperties
.getExcludedIndicators());
healthIndicatorProcessor.setParallelCheck(healthCheckProperties.isParallelCheck());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.alipay.sofa.boot.actuator.autoconfigure.reflection;

import com.alipay.sofa.boot.actuator.reflection.ReflectionCacheEndpoint;
import com.alipay.sofa.boot.autoconfigure.reflection.ReflectionCacheAutoConfiguration;
import com.alipay.sofa.boot.reflection.ReflectionCache;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

/**
* Configures {@link ReflectionCacheEndpoint}.
*
* @author xiaosiyuan
*/
@AutoConfiguration(after = ReflectionCacheAutoConfiguration.class)
@ConditionalOnBean(ReflectionCache.class)
@ConditionalOnAvailableEndpoint(endpoint = ReflectionCacheEndpoint.class)
public class ReflectionCacheEndpointAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public ReflectionCacheEndpoint reflectionCacheEndpoint(ReflectionCache reflectionCache) {
return new ReflectionCacheEndpoint(reflectionCache);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
com.alipay.sofa.boot.actuator.autoconfigure.beans.IsleBeansEndpointAutoConfiguration
com.alipay.sofa.boot.actuator.autoconfigure.components.ComponentsEndpointAutoConfiguration
com.alipay.sofa.boot.actuator.autoconfigure.reflection.ReflectionCacheEndpointAutoConfiguration
com.alipay.sofa.boot.actuator.autoconfigure.health.ReadinessAutoConfiguration
com.alipay.sofa.boot.actuator.autoconfigure.health.ReadinessIsleAutoConfiguration
com.alipay.sofa.boot.actuator.autoconfigure.health.ReadinessEndpointAutoConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import com.alipay.sofa.boot.actuator.health.ModuleHealthChecker;
import com.alipay.sofa.boot.actuator.health.ReadinessCheckCallbackProcessor;
import com.alipay.sofa.boot.actuator.health.ReadinessCheckListener;
import com.alipay.sofa.boot.autoconfigure.reflection.ReflectionCacheAutoConfiguration;
import com.alipay.sofa.boot.autoconfigure.isle.SofaModuleAutoConfiguration;
import com.alipay.sofa.boot.autoconfigure.runtime.SofaRuntimeAutoConfiguration;
import com.alipay.sofa.boot.isle.ApplicationRuntimeModel;
import com.alipay.sofa.boot.reflection.ReflectionCache;
import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnJre;
Expand All @@ -49,7 +51,8 @@ public class ReadinessAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations
.of(ReadinessAutoConfiguration.class))
.of(ReflectionCacheAutoConfiguration.class,
ReadinessAutoConfiguration.class))
.withPropertyValues(
"management.endpoints.web.exposure.include=readiness");

Expand All @@ -64,6 +67,20 @@ void runShouldHaveReadinessBeans() {
.hasBean(ReadinessCheckListener.READINESS_HEALTH_CHECK_EXECUTOR_BEAN_NAME));
}

@Test
void runWithoutReflectionCacheAutoConfigurationShouldHaveReadinessBeans() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ReadinessAutoConfiguration.class))
.withPropertyValues("management.endpoints.web.exposure.include=readiness")
.run((context) -> assertThat(context)
.doesNotHaveBean(ReflectionCache.class)
.hasSingleBean(ReadinessCheckListener.class)
.hasSingleBean(HealthCheckerProcessor.class)
.hasSingleBean(HealthIndicatorProcessor.class)
.hasSingleBean(ReadinessCheckCallbackProcessor.class)
.hasBean(ReadinessCheckListener.READINESS_HEALTH_CHECK_EXECUTOR_BEAN_NAME));
}

@Test
void runWhenNotExposedShouldNotHaveReadinessBeans() {
this.contextRunner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alipay.sofa.boot.actuator.health.ReadinessEndpoint;
import com.alipay.sofa.boot.actuator.health.ReadinessEndpointWebExtension;
import com.alipay.sofa.boot.actuator.health.ReadinessHttpCodeStatusMapper;
import com.alipay.sofa.boot.autoconfigure.reflection.ReflectionCacheAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.health.HealthEndpointAutoConfiguration;
import org.springframework.boot.actuate.health.HttpCodeStatusMapper;
Expand All @@ -38,7 +39,8 @@ public class ReadinessEndpointAutoConfigurationTests {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations
.of(ReadinessEndpointAutoConfiguration.class,
.of(ReflectionCacheAutoConfiguration.class,
ReadinessEndpointAutoConfiguration.class,
ReadinessAutoConfiguration.class,
HealthEndpointAutoConfiguration.class));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.alipay.sofa.boot.actuator.autoconfigure.reflection;

import com.alipay.sofa.boot.actuator.reflection.ReflectionCacheEndpoint;
import com.alipay.sofa.boot.autoconfigure.reflection.ReflectionCacheAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

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

/**
* Tests for {@link ReflectionCacheEndpointAutoConfiguration}.
*
* @author xiaosiyuan
*/
public class ReflectionCacheEndpointAutoConfigurationTests {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations
.of(ReflectionCacheAutoConfiguration.class,
ReflectionCacheEndpointAutoConfiguration.class));

@Test
void runShouldHaveEndpointBean() {
this.contextRunner.withPropertyValues(
"management.endpoints.web.exposure.include=reflection-cache")
.run((context) -> assertThat(context)
.hasSingleBean(ReflectionCacheEndpoint.class));
}

@Test
void runWhenNotExposedShouldNotHaveEndpointBean() {
this.contextRunner.run(
(context) -> assertThat(context).doesNotHaveBean(ReflectionCacheEndpoint.class));
}

@Test
void runWhenReflectionCacheBeanMissingShouldNotHaveEndpointBean() {
new ApplicationContextRunner().withConfiguration(
AutoConfigurations.of(ReflectionCacheEndpointAutoConfiguration.class))
.withPropertyValues("management.endpoints.web.exposure.include=reflection-cache")
.run((context) -> assertThat(context)
.doesNotHaveBean(ReflectionCacheEndpoint.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alipay.sofa.boot.actuator.rpc.HealthCheckProviderConfigDelayRegisterChecker;
import com.alipay.sofa.boot.actuator.rpc.RpcAfterHealthCheckCallback;
import com.alipay.sofa.boot.actuator.rpc.SofaRpcEndpoint;
import com.alipay.sofa.boot.autoconfigure.reflection.ReflectionCacheAutoConfiguration;
import com.alipay.sofa.rpc.boot.context.RpcStartApplicationListener;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
Expand All @@ -46,7 +47,8 @@ public class RpcActuatorAutoConfigurationTests {
@Test
void runShouldHaveHealthCheckProviderConfigDelayRegisterChecker() {
this.contextRunner
.withUserConfiguration(ReadinessAutoConfiguration.class)
.withConfiguration(AutoConfigurations.of(ReflectionCacheAutoConfiguration.class,
ReadinessAutoConfiguration.class))
.withBean(RpcStartApplicationListener.class)
.run((context) -> assertThat(context)
.hasSingleBean(HealthCheckProviderConfigDelayRegisterChecker.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alipay.sofa.boot.log.ErrorCode;
import com.alipay.sofa.boot.log.SofaBootLoggerFactory;
import com.alipay.sofa.boot.reflection.ReflectionCache;
import com.alipay.sofa.boot.startup.BaseStat;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
Expand Down Expand Up @@ -98,6 +99,8 @@ public class HealthIndicatorProcessor implements ApplicationContextAware {

private long parallelCheckTimeout;

private ReflectionCache reflectionCache;

static {
REACTOR_CLASS_EXIST = ClassUtils.isPresent(REACTOR_CLASS, null);
}
Expand Down Expand Up @@ -133,7 +136,7 @@ public void initExcludedIndicators(List<String> excludes) {
excludedIndicators = new HashSet<>();
for (String exclude : excludes) {
try {
Class<?> c = Class.forName(exclude);
Class<?> c = loadClass(exclude);
excludedIndicators.add(c);
} catch (Throwable e) {
logger.warn("Unable to find excluded HealthIndicator class {}, just ignore it.",
Expand Down Expand Up @@ -295,6 +298,15 @@ public void setParallelCheckTimeout(long parallelCheckTimeout) {
this.parallelCheckTimeout = parallelCheckTimeout;
}

public void setReflectionCache(ReflectionCache reflectionCache) {
this.reflectionCache = reflectionCache;
}

private Class<?> loadClass(String className) throws ClassNotFoundException {
return reflectionCache != null ? reflectionCache.forName(className) : Class
.forName(className);
}

public Map<String, HealthCheckerConfig> getHealthIndicatorConfig() {
return healthIndicatorConfig;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.alipay.sofa.boot.actuator.reflection;

import com.alipay.sofa.boot.reflection.ReflectionCache;
import org.springframework.boot.actuate.endpoint.Access;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;

/**
* Endpoint for viewing and clearing the reflection cache.
*
* @author xiaosiyuan
*/
@Endpoint(id = "reflection-cache", defaultAccess = Access.READ_ONLY)
public class ReflectionCacheEndpoint {

private final ReflectionCache reflectionCache;

public ReflectionCacheEndpoint(ReflectionCache reflectionCache) {
this.reflectionCache = reflectionCache;
}

@ReadOperation
public ReflectionCache.CacheStats stats() {
return reflectionCache.getStats();
}

@WriteOperation
public ReflectionCache.CacheStats clear() {
reflectionCache.clear();
return reflectionCache.getStats();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.alipay.sofa.boot.actuator.reflection;

import com.alipay.sofa.boot.reflection.ReflectionCache;
import org.junit.jupiter.api.Test;

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

/**
* Tests for {@link ReflectionCacheEndpoint}.
*
* @author xiaosiyuan
*/
public class ReflectionCacheEndpointTests {

@Test
void statsShouldExposeReflectionCacheState() throws Exception {
ReflectionCache reflectionCache = new ReflectionCache();
ReflectionCacheEndpoint endpoint = new ReflectionCacheEndpoint(reflectionCache);

reflectionCache.forName(String.class.getName());
reflectionCache.forName(String.class.getName());

ReflectionCache.CacheStats stats = endpoint.stats();
assertThat(stats.isEnabled()).isTrue();
assertThat(stats.getClassHitCount()).isEqualTo(1);
assertThat(stats.getClassMissCount()).isEqualTo(1);
assertThat(stats.getClassCacheSize()).isEqualTo(1);
}

@Test
void clearShouldResetReflectionCacheState() throws Exception {
ReflectionCache reflectionCache = new ReflectionCache();
ReflectionCacheEndpoint endpoint = new ReflectionCacheEndpoint(reflectionCache);

reflectionCache.forName(String.class.getName());
reflectionCache.findMethod(String.class, "trim");
reflectionCache.getDeclaredConstructor(String.class, String.class);

ReflectionCache.CacheStats stats = endpoint.clear();
assertThat(stats.getTotalHitCount()).isZero();
assertThat(stats.getTotalMissCount()).isZero();
assertThat(stats.getClassCacheSize()).isZero();
assertThat(stats.getMethodCacheSize()).isZero();
assertThat(stats.getConstructorCacheSize()).isZero();
}
}
Loading
Loading