You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2023/09/19 12:49:59 UTC
[servicecomb-java-chassis] 03/12: [SCB-2008]change DynamicPropertyFactory to Environment: group executor
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit 0c73ca435860a57064517395ffeea8fd7fd66a24
Author: liubao <bi...@qq.com>
AuthorDate: Mon Sep 4 16:09:28 2023 +0800
[SCB-2008]change DynamicPropertyFactory to Environment: group executor
---
.../core/ServiceCombCoreConfiguration.java | 9 +++++
.../core/bootstrap/SCBEngineForTest.java | 5 ---
.../servicecomb/core/executor/GroupExecutor.java | 21 ++++++-----
.../main/resources/META-INF/spring/cse.bean.xml | 30 ----------------
.../core/executor/TestExecutorManager.java | 33 +++++++++++------
.../core/executor/TestGroupExecutor.java | 41 +++++++++++++++-------
6 files changed, 72 insertions(+), 67 deletions(-)
diff --git a/core/src/main/java/org/apache/servicecomb/core/ServiceCombCoreConfiguration.java b/core/src/main/java/org/apache/servicecomb/core/ServiceCombCoreConfiguration.java
index 284865458..0963963a0 100644
--- a/core/src/main/java/org/apache/servicecomb/core/ServiceCombCoreConfiguration.java
+++ b/core/src/main/java/org/apache/servicecomb/core/ServiceCombCoreConfiguration.java
@@ -20,11 +20,13 @@ import org.apache.servicecomb.config.MicroserviceProperties;
import org.apache.servicecomb.core.bootup.FilterChainCollector;
import org.apache.servicecomb.core.bootup.ServiceInformationCollector;
import org.apache.servicecomb.core.executor.ExecutorManager;
+import org.apache.servicecomb.core.executor.GroupExecutor;
import org.apache.servicecomb.core.provider.producer.ProducerBootListener;
import org.apache.servicecomb.core.registry.discovery.SwaggerLoader;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
@Configuration
@SuppressWarnings("unused")
@@ -71,4 +73,11 @@ public class ServiceCombCoreConfiguration {
public ExecutorManager executorManager() {
return new ExecutorManager();
}
+
+ @Bean(value = {"cse.executor.groupThreadPool", "cse.executor.default", "servicecomb.executor.groupThreadPool"})
+ public GroupExecutor groupExecutor(Environment environment) {
+ GroupExecutor groupExecutor = new GroupExecutor(environment);
+ groupExecutor.init();
+ return groupExecutor;
+ }
}
diff --git a/core/src/main/java/org/apache/servicecomb/core/bootstrap/SCBEngineForTest.java b/core/src/main/java/org/apache/servicecomb/core/bootstrap/SCBEngineForTest.java
index 71b5af2b3..36eb26dc7 100644
--- a/core/src/main/java/org/apache/servicecomb/core/bootstrap/SCBEngineForTest.java
+++ b/core/src/main/java/org/apache/servicecomb/core/bootstrap/SCBEngineForTest.java
@@ -17,8 +17,6 @@
package org.apache.servicecomb.core.bootstrap;
-import static org.apache.servicecomb.core.executor.ExecutorManager.EXECUTOR_GROUP_THREADPOOL;
-
import java.util.Arrays;
import java.util.List;
@@ -26,7 +24,6 @@ import org.apache.servicecomb.config.priority.ConfigObjectFactory;
import org.apache.servicecomb.config.priority.PriorityPropertyFactory;
import org.apache.servicecomb.config.priority.PriorityPropertyManager;
import org.apache.servicecomb.core.SCBEngine;
-import org.apache.servicecomb.core.executor.GroupExecutor;
import org.apache.servicecomb.core.filter.Filter;
import org.apache.servicecomb.core.filter.FilterChainsManager;
import org.apache.servicecomb.core.filter.impl.EmptyFilter;
@@ -39,8 +36,6 @@ import org.apache.servicecomb.foundation.common.utils.ReflectUtils;
*/
public class SCBEngineForTest extends SCBEngine {
public SCBEngineForTest() {
- getExecutorManager().registerExecutor(EXECUTOR_GROUP_THREADPOOL, new GroupExecutor().init());
-
List<Filter> filters = Arrays.asList(
new EmptyFilter()
);
diff --git a/core/src/main/java/org/apache/servicecomb/core/executor/GroupExecutor.java b/core/src/main/java/org/apache/servicecomb/core/executor/GroupExecutor.java
index a3fd1c8fc..24e41d407 100644
--- a/core/src/main/java/org/apache/servicecomb/core/executor/GroupExecutor.java
+++ b/core/src/main/java/org/apache/servicecomb/core/executor/GroupExecutor.java
@@ -30,8 +30,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
-import com.netflix.config.DynamicPropertyFactory;
+import org.springframework.core.env.Environment;
public class GroupExecutor implements Executor, Closeable {
private static final Logger LOGGER = LoggerFactory.getLogger(GroupExecutor.class);
@@ -51,6 +50,8 @@ public class GroupExecutor implements Executor, Closeable {
private static final AtomicBoolean LOG_PRINTED = new AtomicBoolean();
+ private final Environment environment;
+
protected String groupName;
protected int groupCount;
@@ -72,6 +73,10 @@ public class GroupExecutor implements Executor, Closeable {
private final Map<Long, Executor> threadExecutorMap = new ConcurrentHashMapEx<>();
+ public GroupExecutor(Environment environment) {
+ this.environment = environment;
+ }
+
public GroupExecutor init() {
return init("group");
}
@@ -104,12 +109,12 @@ public class GroupExecutor implements Executor, Closeable {
+ "4.if queue is full, and threads count is max, then reject the request.");
}
- groupCount = DynamicPropertyFactory.getInstance().getIntProperty(KEY_GROUP, 2).get();
- coreThreads = DynamicPropertyFactory.getInstance().getIntProperty(KEY_CORE_THREADS, 25).get();
+ groupCount = environment.getProperty(KEY_GROUP, int.class, 2);
+ coreThreads = environment.getProperty(KEY_CORE_THREADS, int.class, 25);
- maxThreads = DynamicPropertyFactory.getInstance().getIntProperty(KEY_MAX_THREADS, -1).get();
+ maxThreads = environment.getProperty(KEY_MAX_THREADS, int.class, -1);
if (maxThreads <= 0) {
- maxThreads = DynamicPropertyFactory.getInstance().getIntProperty(KEY_OLD_MAX_THREAD, -1).get();
+ maxThreads = environment.getProperty(KEY_OLD_MAX_THREAD, int.class, -1);
if (maxThreads > 0) {
LOGGER.warn("{} is deprecated, recommended to use {}.", KEY_OLD_MAX_THREAD, KEY_MAX_THREADS);
} else {
@@ -121,8 +126,8 @@ public class GroupExecutor implements Executor, Closeable {
coreThreads = maxThreads;
}
- maxIdleInSecond = DynamicPropertyFactory.getInstance().getIntProperty(KEY_MAX_IDLE_SECOND, 60).get();
- maxQueueSize = DynamicPropertyFactory.getInstance().getIntProperty(KEY_MAX_QUEUE_SIZE, Integer.MAX_VALUE).get();
+ maxIdleInSecond = environment.getProperty(KEY_MAX_IDLE_SECOND, int.class, 60);
+ maxQueueSize = environment.getProperty(KEY_MAX_QUEUE_SIZE, int.class, Integer.MAX_VALUE);
LOGGER.info(
"executor name={}, group={}. per group settings, coreThreads={}, maxThreads={}, maxIdleInSecond={}, maxQueueSize={}.",
diff --git a/core/src/main/resources/META-INF/spring/cse.bean.xml b/core/src/main/resources/META-INF/spring/cse.bean.xml
deleted file mode 100644
index 7b0757e16..000000000
--- a/core/src/main/resources/META-INF/spring/cse.bean.xml
+++ /dev/null
@@ -1,30 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- ~ 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.
- -->
-
-<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
-
- <bean id="cse.executor.groupThreadPool" class="org.apache.servicecomb.core.executor.GroupExecutor"
- init-method="init"/>
- <alias name="cse.executor.groupThreadPool" alias="cse.executor.default"/>
- <alias name="cse.executor.groupThreadPool" alias="servicecomb.executor.groupThreadPool"/>
-</beans>
diff --git a/core/src/test/java/org/apache/servicecomb/core/executor/TestExecutorManager.java b/core/src/test/java/org/apache/servicecomb/core/executor/TestExecutorManager.java
index cb1db7675..ea4d8662f 100644
--- a/core/src/test/java/org/apache/servicecomb/core/executor/TestExecutorManager.java
+++ b/core/src/test/java/org/apache/servicecomb/core/executor/TestExecutorManager.java
@@ -25,17 +25,24 @@ import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+import org.mockito.Mockito;
+import org.springframework.core.env.Environment;
import mockit.Expectations;
import mockit.Mocked;
-import org.junit.jupiter.api.Assertions;
public class TestExecutorManager {
@Mocked
Executor defaultExecutor;
+ ExecutorManager executorManager = new ExecutorManager();
+
+ Environment environment = Mockito.mock(Environment.class);
+
@Before
public void setup() {
+ executorManager.setEnvironment(environment);
ArchaiusUtils.resetConfig();
}
@@ -53,7 +60,7 @@ public class TestExecutorManager {
}
};
- Assertions.assertSame(executor, new ExecutorManager().findExecutor(operationMeta));
+ Assertions.assertSame(executor, executorManager.findExecutor(operationMeta));
}
@Test
@@ -62,7 +69,8 @@ public class TestExecutorManager {
// String schemaQualifiedName = "schemaId.opId";
String microserviceQualifiedName = "microserviceName.schemaId.opId";
String opBeanId = "opBeanId";
- ArchaiusUtils.setProperty(ExecutorManager.KEY_EXECUTORS_PREFIX + microserviceQualifiedName, opBeanId);
+ Mockito.when(environment.getProperty(
+ ExecutorManager.KEY_EXECUTORS_PREFIX + microserviceQualifiedName, String.class)).thenReturn(opBeanId);
new Expectations(BeanUtils.class) {
{
operationMeta.getMicroserviceQualifiedName();
@@ -72,7 +80,7 @@ public class TestExecutorManager {
}
};
- Assertions.assertSame(executor, new ExecutorManager().findExecutor(operationMeta, null));
+ Assertions.assertSame(executor, executorManager.findExecutor(operationMeta, null));
}
@Test
@@ -81,7 +89,8 @@ public class TestExecutorManager {
@Mocked OperationMeta operationMeta) {
String microserviceQualifiedName = "microserviceName.schemaId.opId";
String opBeanId = "opBeanId";
- ArchaiusUtils.setProperty(ExecutorManager.KEY_EXECUTORS_PREFIX + microserviceQualifiedName, opBeanId);
+ Mockito.when(environment.getProperty(
+ ExecutorManager.KEY_EXECUTORS_PREFIX + microserviceQualifiedName, String.class)).thenReturn(opBeanId);
new Expectations(BeanUtils.class) {
{
operationMeta.getMicroserviceQualifiedName();
@@ -91,13 +100,13 @@ public class TestExecutorManager {
}
};
- Assertions.assertSame(executor, new ExecutorManager().findExecutor(operationMeta, defExecutor));
+ Assertions.assertSame(executor, executorManager.findExecutor(operationMeta, defExecutor));
}
@Test
public void findExecutor_twoParam_schemaCfg_withOpDef(@Mocked OperationMeta operationMeta,
@Mocked Executor defExecutor) {
- Assertions.assertSame(defExecutor, new ExecutorManager().findExecutor(operationMeta, defExecutor));
+ Assertions.assertSame(defExecutor, executorManager.findExecutor(operationMeta, defExecutor));
}
@Test
@@ -106,7 +115,8 @@ public class TestExecutorManager {
String microserviceName = "serviceName";
String schemaName = "schemaId";
String opBeanId = "opBeanId";
- ArchaiusUtils.setProperty(ExecutorManager.KEY_EXECUTORS_PREFIX + microserviceName + "." + schemaName, opBeanId);
+ Mockito.when(environment.getProperty(ExecutorManager.KEY_EXECUTORS_PREFIX + microserviceName + "." + schemaName
+ , String.class)).thenReturn(opBeanId);
new Expectations(BeanUtils.class) {
{
operationMeta.getSchemaId();
@@ -117,7 +127,7 @@ public class TestExecutorManager {
result = executor;
}
};
- Assertions.assertSame(executor, new ExecutorManager().findExecutor(operationMeta, null));
+ Assertions.assertSame(executor, executorManager.findExecutor(operationMeta, null));
}
@Test
@@ -125,7 +135,8 @@ public class TestExecutorManager {
@Mocked SchemaMeta schemaMeta,
@Mocked OperationMeta operationMeta) {
String beanId = "beanId";
- ArchaiusUtils.setProperty(ExecutorManager.KEY_EXECUTORS_DEFAULT, beanId);
+ Mockito.when(environment.getProperty(ExecutorManager.KEY_EXECUTORS_DEFAULT
+ , String.class)).thenReturn(beanId);
new Expectations(BeanUtils.class) {
{
BeanUtils.getBean(beanId);
@@ -133,6 +144,6 @@ public class TestExecutorManager {
}
};
- Assertions.assertSame(executor, new ExecutorManager().findExecutor(operationMeta, null));
+ Assertions.assertSame(executor, executorManager.findExecutor(operationMeta, null));
}
}
diff --git a/core/src/test/java/org/apache/servicecomb/core/executor/TestGroupExecutor.java b/core/src/test/java/org/apache/servicecomb/core/executor/TestGroupExecutor.java
index aae5bdeb5..f44496d8a 100644
--- a/core/src/test/java/org/apache/servicecomb/core/executor/TestGroupExecutor.java
+++ b/core/src/test/java/org/apache/servicecomb/core/executor/TestGroupExecutor.java
@@ -17,25 +17,40 @@
package org.apache.servicecomb.core.executor;
+import static org.apache.servicecomb.core.executor.GroupExecutor.KEY_CORE_THREADS;
+import static org.apache.servicecomb.core.executor.GroupExecutor.KEY_GROUP;
+import static org.apache.servicecomb.core.executor.GroupExecutor.KEY_MAX_IDLE_SECOND;
+import static org.apache.servicecomb.core.executor.GroupExecutor.KEY_MAX_QUEUE_SIZE;
+import static org.apache.servicecomb.core.executor.GroupExecutor.KEY_MAX_THREADS;
+import static org.apache.servicecomb.core.executor.GroupExecutor.KEY_OLD_MAX_THREAD;
+
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
import org.apache.servicecomb.foundation.test.scaffolding.log.LogCollector;
-
-import mockit.Deencapsulation;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+import org.springframework.core.env.Environment;
+
+import mockit.Deencapsulation;
public class TestGroupExecutor {
String strThreadTest = "default";
@BeforeEach
public void setup() {
- ArchaiusUtils.resetConfig();
+ Mockito.when(environment.getProperty(KEY_GROUP, int.class, 2)).thenReturn(2);
+ Mockito.when(environment.getProperty(KEY_CORE_THREADS, int.class, 25)).thenReturn(25);
+ Mockito.when(environment.getProperty(KEY_MAX_IDLE_SECOND, int.class, 60)).thenReturn(60);
+ Mockito.when(environment.getProperty(KEY_MAX_THREADS, int.class, -1)).thenReturn(-1);
+ Mockito.when(environment.getProperty(KEY_OLD_MAX_THREAD, int.class, -1)).thenReturn(-1);
+ Mockito.when(environment.getProperty(KEY_MAX_QUEUE_SIZE, int.class, Integer.MAX_VALUE))
+ .thenReturn(Integer.MAX_VALUE);
}
@AfterAll
@@ -43,14 +58,16 @@ public class TestGroupExecutor {
ArchaiusUtils.resetConfig();
}
- GroupExecutor groupExecutor = new GroupExecutor();
+ Environment environment = Mockito.mock(Environment.class);
+
+ GroupExecutor groupExecutor = new GroupExecutor(environment);
@Test
public void groupCount() {
groupExecutor.initConfig();
Assertions.assertEquals(2, groupExecutor.groupCount);
- ArchaiusUtils.setProperty(GroupExecutor.KEY_GROUP, 4);
+ Mockito.when(environment.getProperty(KEY_GROUP, int.class, 2)).thenReturn(4);
groupExecutor.initConfig();
Assertions.assertEquals(4, groupExecutor.groupCount);
}
@@ -60,7 +77,7 @@ public class TestGroupExecutor {
groupExecutor.initConfig();
Assertions.assertEquals(25, groupExecutor.coreThreads);
- ArchaiusUtils.setProperty(GroupExecutor.KEY_CORE_THREADS, 100);
+ Mockito.when(environment.getProperty(KEY_CORE_THREADS, int.class, 25)).thenReturn(100);
groupExecutor.initConfig();
Assertions.assertEquals(100, groupExecutor.coreThreads);
}
@@ -70,7 +87,7 @@ public class TestGroupExecutor {
groupExecutor.initConfig();
Assertions.assertEquals(60, groupExecutor.maxIdleInSecond);
- ArchaiusUtils.setProperty(GroupExecutor.KEY_MAX_IDLE_SECOND, 100);
+ Mockito.when(environment.getProperty(KEY_MAX_IDLE_SECOND, int.class, 60)).thenReturn(100);
groupExecutor.initConfig();
Assertions.assertEquals(100, groupExecutor.maxIdleInSecond);
}
@@ -79,8 +96,7 @@ public class TestGroupExecutor {
public void maxQueueSize() {
groupExecutor.initConfig();
Assertions.assertEquals(Integer.MAX_VALUE, groupExecutor.maxQueueSize);
-
- ArchaiusUtils.setProperty(GroupExecutor.KEY_MAX_QUEUE_SIZE, 100);
+ Mockito.when(environment.getProperty(KEY_MAX_QUEUE_SIZE, int.class, Integer.MAX_VALUE)).thenReturn(100);
groupExecutor.initConfig();
Assertions.assertEquals(100, groupExecutor.maxQueueSize);
}
@@ -91,7 +107,7 @@ public class TestGroupExecutor {
Assertions.assertEquals(100, groupExecutor.maxThreads);
LogCollector collector = new LogCollector();
- ArchaiusUtils.setProperty(GroupExecutor.KEY_OLD_MAX_THREAD, 200);
+ Mockito.when(environment.getProperty(KEY_OLD_MAX_THREAD, int.class, -1)).thenReturn(200);
groupExecutor.initConfig();
Assertions.assertEquals(200, groupExecutor.maxThreads);
Assertions.assertEquals(
@@ -99,15 +115,14 @@ public class TestGroupExecutor {
collector.getEvents().get(collector.getEvents().size() - 2).getMessage().getFormattedMessage());
collector.teardown();
- ArchaiusUtils.setProperty(GroupExecutor.KEY_MAX_THREADS, 300);
+ Mockito.when(environment.getProperty(KEY_MAX_THREADS, int.class, -1)).thenReturn(300);
groupExecutor.initConfig();
Assertions.assertEquals(300, groupExecutor.maxThreads);
}
@Test
public void adjustCoreThreads() {
- ArchaiusUtils.setProperty(GroupExecutor.KEY_MAX_THREADS, 10);
-
+ Mockito.when(environment.getProperty(KEY_MAX_THREADS, int.class, -1)).thenReturn(10);
LogCollector collector = new LogCollector();
groupExecutor.initConfig();
Assertions.assertEquals(10, groupExecutor.maxThreads);