-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
support sofa thread pool actuator #1301
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* 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.threadpool; | ||
|
||
import com.alipay.sofa.boot.actuator.threadpool.ThreadPoolEndpoint; | ||
import com.alipay.sofa.common.thread.ThreadPoolGovernor; | ||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for {@link ThreadPoolEndpoint}. | ||
* | ||
* @author huzijie | ||
* @version ThreadPoolEndpointAutoConfiguration.java, v 0.1 2024年03月22日 11:58 huzijie Exp $ | ||
*/ | ||
@ConditionalOnAvailableEndpoint(endpoint = ThreadPoolEndpoint.class) | ||
public class ThreadPoolEndpointAutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public ThreadPoolEndpoint threadPoolEndpoint() { | ||
return new ThreadPoolEndpoint(ThreadPoolGovernor.getInstance()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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.threadpool; | ||
|
||
import com.alipay.sofa.boot.actuator.threadpool.ThreadPoolEndpoint; | ||
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 ThreadPoolEndpointAutoConfiguration}. | ||
* | ||
* @author huzijie | ||
* @version ThreadPoolEndpointAutoConfigurationTests.java, v 0.1 2024年03月22日 11:59 huzijie Exp $ | ||
*/ | ||
public class ThreadPoolEndpointAutoConfigurationTests { | ||
|
||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations | ||
.of(ThreadPoolEndpointAutoConfiguration.class)); | ||
|
||
@Test | ||
void runShouldHaveEndpointBean() { | ||
this.contextRunner.withPropertyValues("management.endpoints.web.exposure.include=threadpool") | ||
.run((context) -> assertThat(context).hasSingleBean(ThreadPoolEndpoint.class)); | ||
} | ||
|
||
@Test | ||
void runWhenNotExposedShouldNotHaveEndpointBean() { | ||
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(ThreadPoolEndpoint.class)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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.threadpool; | ||
|
||
import com.alipay.sofa.common.thread.ThreadPoolConfig; | ||
import com.alipay.sofa.common.thread.ThreadPoolGovernor; | ||
import com.alipay.sofa.common.thread.ThreadPoolMonitorWrapper; | ||
import org.springframework.boot.actuate.endpoint.OperationResponseBody; | ||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; | ||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
|
||
/** | ||
* {@link Endpoint @Endpoint} to expose details of sofa thread pools registered in {@link ThreadPoolGovernor}. | ||
* | ||
* @author huzijie | ||
* @version ThreadPoolEndpoint.java, v 0.1 2024年03月22日 11:41 huzijie Exp $ | ||
*/ | ||
@Endpoint(id = "threadpool") | ||
public class ThreadPoolEndpoint { | ||
|
||
private final ThreadPoolGovernor threadPoolGovernor; | ||
|
||
public ThreadPoolEndpoint(ThreadPoolGovernor threadPoolGovernor) { | ||
this.threadPoolGovernor = threadPoolGovernor; | ||
} | ||
|
||
@ReadOperation | ||
public ThreadPoolsDescriptor threadPools () { | ||
Collection<ThreadPoolMonitorWrapper> threadPoolWrappers = threadPoolGovernor.getAllThreadPoolWrappers(); | ||
|
||
List<ThreadPoolInfo> threadPoolInfoList = threadPoolWrappers.stream().map(this::convertToThreadPoolInfo).toList(); | ||
return new ThreadPoolsDescriptor(threadPoolInfoList); | ||
} | ||
|
||
private ThreadPoolInfo convertToThreadPoolInfo(ThreadPoolMonitorWrapper wrapper) { | ||
ThreadPoolConfig threadPoolConfig = wrapper.getThreadPoolConfig(); | ||
String threadPoolName = threadPoolConfig.getThreadPoolName(); | ||
String spaceName = threadPoolConfig.getSpaceName(); | ||
long period = threadPoolConfig.getPeriod(); | ||
long taskTimeout = threadPoolConfig.getTaskTimeoutMilli(); | ||
|
||
ThreadPoolExecutor threadPoolExecutor = wrapper.getThreadPoolExecutor(); | ||
String threadPoolClassName = threadPoolExecutor.getClass().getName(); | ||
int coreSize = threadPoolExecutor.getCorePoolSize(); | ||
int maxSize = threadPoolExecutor.getMaximumPoolSize(); | ||
int queueSize = threadPoolExecutor.getQueue().size(); | ||
int queueRemainingCapacity = threadPoolExecutor.getQueue().remainingCapacity(); | ||
String queueClassName = threadPoolExecutor.getQueue().getClass().getName(); | ||
|
||
return new ThreadPoolInfo(threadPoolName, spaceName, threadPoolClassName, coreSize, maxSize, queueClassName, queueSize, queueRemainingCapacity, period, taskTimeout); | ||
} | ||
|
||
public record ThreadPoolsDescriptor(List<ThreadPoolInfo> threadPoolInfoList) implements OperationResponseBody {} | ||
|
||
public record ThreadPoolInfo(String threadPoolName, | ||
String spaceName, | ||
|
||
String threadPoolClassName, | ||
int coreSize, | ||
int maxSize, | ||
String queueClassName, | ||
int queueSize, | ||
int queueRemainingCapacity, | ||
long monitorPeriod, | ||
long taskTimeout) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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.threadpool; | ||
|
||
import com.alipay.sofa.common.thread.ThreadPoolConfig; | ||
import com.alipay.sofa.common.thread.ThreadPoolGovernor; | ||
import com.alipay.sofa.common.thread.ThreadPoolMonitorWrapper; | ||
import com.alipay.sofa.common.thread.ThreadPoolStatistics; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* @author huzijie | ||
* @version ThreadPoolEndpointTests.java, v 0.1 2024年03月22日 12:01 huzijie Exp $ | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
public class ThreadPoolEndpointTests { | ||
|
||
@Mock | ||
private ThreadPoolGovernor threadPoolGovernor; | ||
|
||
@InjectMocks | ||
private ThreadPoolEndpoint threadPoolEndpoint; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 6, 10, TimeUnit.SECONDS, | ||
new LinkedBlockingQueue<>(8)); | ||
|
||
ThreadPoolConfig threadPoolConfig = Mockito.mock(ThreadPoolConfig.class); | ||
when(threadPoolConfig.getThreadPoolName()).thenReturn("mockThreadPoolName"); | ||
when(threadPoolConfig.getSpaceName()).thenReturn("mockSpaceName"); | ||
when(threadPoolConfig.getTaskTimeoutMilli()).thenReturn(10L); | ||
when(threadPoolConfig.getPeriod()).thenReturn(9L); | ||
|
||
ThreadPoolStatistics threadPoolStatistics = new ThreadPoolStatistics(threadPoolExecutor); | ||
ThreadPoolMonitorWrapper threadPoolMonitorWrapper = new ThreadPoolMonitorWrapper( | ||
threadPoolExecutor, threadPoolConfig, threadPoolStatistics); | ||
when(threadPoolGovernor.getAllThreadPoolWrappers()).thenReturn( | ||
List.of(threadPoolMonitorWrapper)); | ||
} | ||
|
||
@Test | ||
public void threadPools() { | ||
List<ThreadPoolEndpoint.ThreadPoolInfo> descriptor = threadPoolEndpoint.threadPools() | ||
.threadPoolInfoList(); | ||
assertThat(descriptor).hasSize(1); | ||
ThreadPoolEndpoint.ThreadPoolInfo threadPoolInfo = descriptor.get(0); | ||
assertThat(threadPoolInfo.threadPoolName()).isEqualTo("mockThreadPoolName"); | ||
assertThat(threadPoolInfo.spaceName()).isEqualTo("mockSpaceName"); | ||
assertThat(threadPoolInfo.threadPoolClassName()).isEqualTo( | ||
"java.util.concurrent.ThreadPoolExecutor"); | ||
assertThat(threadPoolInfo.coreSize()).isEqualTo(5); | ||
assertThat(threadPoolInfo.maxSize()).isEqualTo(6); | ||
assertThat(threadPoolInfo.queueSize()).isEqualTo(0); | ||
assertThat(threadPoolInfo.queueRemainingCapacity()).isEqualTo(8); | ||
assertThat(threadPoolInfo.queueClassName()).isEqualTo( | ||
"java.util.concurrent.LinkedBlockingQueue"); | ||
assertThat(threadPoolInfo.monitorPeriod()).isEqualTo(9); | ||
assertThat(threadPoolInfo.taskTimeout()).isEqualTo(10); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,67 @@ | ||||||||||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||||||||||
* 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.smoke.tests.actuator.threadpool; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
import com.alipay.sofa.common.thread.SofaThreadPoolExecutor; | ||||||||||||||||||||||||||||||||||||||||||||
import com.alipay.sofa.smoke.tests.actuator.ActuatorSofaBootApplication; | ||||||||||||||||||||||||||||||||||||||||||||
import org.junit.jupiter.api.Test; | ||||||||||||||||||||||||||||||||||||||||||||
import org.springframework.beans.factory.annotation.Autowired; | ||||||||||||||||||||||||||||||||||||||||||||
import org.springframework.boot.test.context.SpringBootTest; | ||||||||||||||||||||||||||||||||||||||||||||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||||||||||||||||||||||||||||||||||||||||||||
import org.springframework.context.annotation.Bean; | ||||||||||||||||||||||||||||||||||||||||||||
import org.springframework.context.annotation.Configuration; | ||||||||||||||||||||||||||||||||||||||||||||
import org.springframework.context.annotation.Import; | ||||||||||||||||||||||||||||||||||||||||||||
import org.springframework.http.HttpStatus; | ||||||||||||||||||||||||||||||||||||||||||||
import org.springframework.http.ResponseEntity; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
import java.util.concurrent.LinkedBlockingQueue; | ||||||||||||||||||||||||||||||||||||||||||||
import java.util.concurrent.TimeUnit; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
import static org.assertj.core.api.Assertions.assertThat; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||
* Integration tests for {@link com.alipay.sofa.boot.actuator.threadpool.ThreadPoolEndpoint} web response. | ||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||
* @author huzijie | ||||||||||||||||||||||||||||||||||||||||||||
* @version ThreadPoolEndpointWebTests.java, v 0.1 2024年03月22日 14:06 huzijie Exp $ | ||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||
@SpringBootTest(classes = ActuatorSofaBootApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { "management.endpoints.web.exposure.include=threadpool" }) | ||||||||||||||||||||||||||||||||||||||||||||
@Import(ThreadPoolEndpointWebTests.Config.class) | ||||||||||||||||||||||||||||||||||||||||||||
public class ThreadPoolEndpointWebTests { | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
@Autowired | ||||||||||||||||||||||||||||||||||||||||||||
private TestRestTemplate restTemplate; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||||||||||
public void threadPoolActuator() { | ||||||||||||||||||||||||||||||||||||||||||||
ResponseEntity<String> response = restTemplate.getForEntity("/actuator/threadpool", String.class); | ||||||||||||||||||||||||||||||||||||||||||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); | ||||||||||||||||||||||||||||||||||||||||||||
assertThat(response.getBody()) | ||||||||||||||||||||||||||||||||||||||||||||
.contains(""" | ||||||||||||||||||||||||||||||||||||||||||||
{"threadPoolName":"demoThreadPool","threadPoolClassName":"com.alipay.sofa.common.thread.SofaThreadPoolExecutor","coreSize":20,"maxSize":20,"queueClassName":"java.util.concurrent.LinkedBlockingQueue","queueSize":0,"queueRemainingCapacity":10,"monitorPeriod":5000,"taskTimeout":30000}"""); | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+49
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The integration test for the + /**
+ * Defines a custom thread pool bean for testing the ThreadPoolEndpoint.
+ * @return SofaThreadPoolExecutor instance
+ */
@Bean Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
@Configuration | ||||||||||||||||||||||||||||||||||||||||||||
static class Config { | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
@Bean | ||||||||||||||||||||||||||||||||||||||||||||
public SofaThreadPoolExecutor sofaThreadPoolExecutor() { | ||||||||||||||||||||||||||||||||||||||||||||
return new SofaThreadPoolExecutor(20, 20, 30, TimeUnit.SECONDS, | ||||||||||||||||||||||||||||||||||||||||||||
new LinkedBlockingQueue<>(10), "demoThreadPool"); | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The auto-configuration class for
ThreadPoolEndpoint
is correctly implemented with appropriate conditions to ensure it's only configured when available and not already defined. However, adding documentation for thethreadPoolEndpoint
method could improve code maintainability and clarity for future developers.Committable suggestion