* [ISSUE #11655]fix invalid usage of ProporityQueue in ConfigChangePluginManager * add UT for config change plugin * for checkstyle * for checkstyle
This commit is contained in:
parent
9fcc4c0dbe
commit
2bb93982df
@ -45,11 +45,10 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Properties;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Config change pointcut aspect,which config change plugin services will pointcut.
|
||||
@ -62,9 +61,9 @@ public class ConfigChangeAspect {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigChangeAspect.class);
|
||||
|
||||
private static final Integer DEFAULT_BEFORE_QUEUE_CAPACITY = 2;
|
||||
private static final Integer DEFAULT_BEFORE_LIST_CAPACITY = 2;
|
||||
|
||||
private static final Integer DEFAULT_AFTER_QUEUE_CAPACITY = 1;
|
||||
private static final Integer DEFAULT_AFTER_LIST_CAPACITY = 1;
|
||||
|
||||
private static final String ENABLED = "enabled";
|
||||
|
||||
@ -132,10 +131,10 @@ public class ConfigChangeAspect {
|
||||
String appName, String srcUser, String configTags, String desc, String use, String effect, String type)
|
||||
throws Throwable {
|
||||
final ConfigChangePointCutTypes configChangePointCutType = ConfigChangePointCutTypes.PUBLISH_BY_HTTP;
|
||||
final PriorityQueue<ConfigChangePluginService> pluginServicePriorityQueue = getPluginServicePriorityQueue(
|
||||
final List<ConfigChangePluginService> pluginServices = getPluginServices(
|
||||
configChangePointCutType);
|
||||
// didn't enabled or add relative plugin
|
||||
if (pluginServicePriorityQueue.isEmpty()) {
|
||||
if (pluginServices.isEmpty()) {
|
||||
return pjp.proceed();
|
||||
}
|
||||
ConfigChangeRequest configChangeRequest = new ConfigChangeRequest(configChangePointCutType);
|
||||
@ -151,7 +150,7 @@ public class ConfigChangeAspect {
|
||||
configChangeRequest.setArg("use", use);
|
||||
configChangeRequest.setArg("effect", effect);
|
||||
configChangeRequest.setArg("type", type);
|
||||
return configChangeServiceHandle(pjp, pluginServicePriorityQueue, configChangeRequest);
|
||||
return configChangeServiceHandle(pjp, pluginServices, configChangeRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -161,10 +160,10 @@ public class ConfigChangeAspect {
|
||||
Object removeConfigByIdAround(ProceedingJoinPoint pjp, HttpServletRequest request, HttpServletResponse response,
|
||||
String dataId, String group, String tenant) throws Throwable {
|
||||
final ConfigChangePointCutTypes configChangePointCutType = ConfigChangePointCutTypes.REMOVE_BY_HTTP;
|
||||
final PriorityQueue<ConfigChangePluginService> pluginServicePriorityQueue = getPluginServicePriorityQueue(
|
||||
final List<ConfigChangePluginService> pluginServices = getPluginServices(
|
||||
configChangePointCutType);
|
||||
// didn't enabled or add relative plugin
|
||||
if (pluginServicePriorityQueue.isEmpty()) {
|
||||
if (pluginServices.isEmpty()) {
|
||||
return pjp.proceed();
|
||||
}
|
||||
ConfigChangeRequest configChangeRequest = new ConfigChangeRequest(configChangePointCutType);
|
||||
@ -174,7 +173,7 @@ public class ConfigChangeAspect {
|
||||
configChangeRequest.setArg("srcIp", RequestUtil.getRemoteIp(request));
|
||||
configChangeRequest.setArg("requestIpApp", RequestUtil.getAppName(request));
|
||||
configChangeRequest.setArg("use", RequestUtil.getSrcUserName(request));
|
||||
return configChangeServiceHandle(pjp, pluginServicePriorityQueue, configChangeRequest);
|
||||
return configChangeServiceHandle(pjp, pluginServices, configChangeRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -184,10 +183,10 @@ public class ConfigChangeAspect {
|
||||
public Object removeConfigByIdsAround(ProceedingJoinPoint pjp, HttpServletRequest request, List<Long> ids)
|
||||
throws Throwable {
|
||||
final ConfigChangePointCutTypes configChangePointCutType = ConfigChangePointCutTypes.REMOVE_BATCH_HTTP;
|
||||
final PriorityQueue<ConfigChangePluginService> pluginServicePriorityQueue = getPluginServicePriorityQueue(
|
||||
final List<ConfigChangePluginService> pluginServices = getPluginServices(
|
||||
configChangePointCutType);
|
||||
// didn't enabled or add relative plugin
|
||||
if (pluginServicePriorityQueue.isEmpty()) {
|
||||
if (pluginServices.isEmpty()) {
|
||||
return pjp.proceed();
|
||||
}
|
||||
ConfigChangeRequest configChangeRequest = new ConfigChangeRequest(configChangePointCutType);
|
||||
@ -195,7 +194,7 @@ public class ConfigChangeAspect {
|
||||
configChangeRequest.setArg("srcIp", RequestUtil.getRemoteIp(request));
|
||||
configChangeRequest.setArg("requestIpApp", RequestUtil.getAppName(request));
|
||||
configChangeRequest.setArg("use", RequestUtil.getSrcUserName(request));
|
||||
return configChangeServiceHandle(pjp, pluginServicePriorityQueue, configChangeRequest);
|
||||
return configChangeServiceHandle(pjp, pluginServices, configChangeRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -205,10 +204,10 @@ public class ConfigChangeAspect {
|
||||
public Object importConfigAround(ProceedingJoinPoint pjp, HttpServletRequest request, String srcUser,
|
||||
String namespace, SameConfigPolicy policy, MultipartFile file) throws Throwable {
|
||||
final ConfigChangePointCutTypes configChangePointCutType = ConfigChangePointCutTypes.IMPORT_BY_HTTP;
|
||||
final PriorityQueue<ConfigChangePluginService> pluginServicePriorityQueue = getPluginServicePriorityQueue(
|
||||
final List<ConfigChangePluginService> pluginServices = getPluginServices(
|
||||
configChangePointCutType);
|
||||
// didn't enabled or add relative plugin
|
||||
if (pluginServicePriorityQueue.isEmpty()) {
|
||||
if (pluginServices.isEmpty()) {
|
||||
return pjp.proceed();
|
||||
}
|
||||
ConfigChangeRequest configChangeRequest = new ConfigChangeRequest(configChangePointCutType);
|
||||
@ -219,7 +218,7 @@ public class ConfigChangeAspect {
|
||||
configChangeRequest.setArg("srcIp", RequestUtil.getRemoteIp(request));
|
||||
configChangeRequest.setArg("requestIpApp", RequestUtil.getAppName(request));
|
||||
configChangeRequest.setArg("use", RequestUtil.getSrcUserName(request));
|
||||
return configChangeServiceHandle(pjp, pluginServicePriorityQueue, configChangeRequest);
|
||||
return configChangeServiceHandle(pjp, pluginServices, configChangeRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -229,10 +228,10 @@ public class ConfigChangeAspect {
|
||||
Object publishConfigAroundRpc(ProceedingJoinPoint pjp, ConfigPublishRequest request, RequestMeta meta)
|
||||
throws Throwable {
|
||||
final ConfigChangePointCutTypes configChangePointCutType = ConfigChangePointCutTypes.PUBLISH_BY_RPC;
|
||||
final PriorityQueue<ConfigChangePluginService> pluginServicePriorityQueue = getPluginServicePriorityQueue(
|
||||
final List<ConfigChangePluginService> pluginServices = getPluginServices(
|
||||
configChangePointCutType);
|
||||
// didn't enabled or add relative plugin
|
||||
if (pluginServicePriorityQueue.isEmpty()) {
|
||||
if (pluginServices.isEmpty()) {
|
||||
return pjp.proceed();
|
||||
}
|
||||
ConfigChangeRequest configChangeRequest = new ConfigChangeRequest(configChangePointCutType);
|
||||
@ -250,7 +249,7 @@ public class ConfigChangeAspect {
|
||||
configChangeRequest.setArg("requestIpApp", request.getAdditionParam("requestIpApp"));
|
||||
configChangeRequest.setArg("srcUser", request.getAdditionParam("src_user"));
|
||||
configChangeRequest.setArg("use", request.getAdditionParam("use"));
|
||||
return configChangeServiceHandle(pjp, pluginServicePriorityQueue, configChangeRequest);
|
||||
return configChangeServiceHandle(pjp, pluginServices, configChangeRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -260,10 +259,10 @@ public class ConfigChangeAspect {
|
||||
Object removeConfigAroundRpc(ProceedingJoinPoint pjp, ConfigRemoveRequest request, RequestMeta meta)
|
||||
throws Throwable {
|
||||
final ConfigChangePointCutTypes configChangePointCutType = ConfigChangePointCutTypes.REMOVE_BY_RPC;
|
||||
final PriorityQueue<ConfigChangePluginService> pluginServicePriorityQueue = getPluginServicePriorityQueue(
|
||||
final List<ConfigChangePluginService> pluginServices = getPluginServices(
|
||||
configChangePointCutType);
|
||||
// didn't enabled or add relative plugin
|
||||
if (pluginServicePriorityQueue.isEmpty()) {
|
||||
if (pluginServices.isEmpty()) {
|
||||
return pjp.proceed();
|
||||
}
|
||||
ConfigChangeRequest configChangeRequest = new ConfigChangeRequest(configChangePointCutType);
|
||||
@ -275,42 +274,41 @@ public class ConfigChangeAspect {
|
||||
configChangeRequest.setArg("requestIpApp", request.getHeader("requestIpApp"));
|
||||
configChangeRequest.setArg("srcUser", request.getHeader("src_user"));
|
||||
configChangeRequest.setArg("use", request.getHeader("use"));
|
||||
return configChangeServiceHandle(pjp, pluginServicePriorityQueue, configChangeRequest);
|
||||
return configChangeServiceHandle(pjp, pluginServices, configChangeRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute relevant config change plugin services.
|
||||
*/
|
||||
private Object configChangeServiceHandle(ProceedingJoinPoint pjp,
|
||||
PriorityQueue<ConfigChangePluginService> configChangePluginServicePriorityQueue,
|
||||
List<ConfigChangePluginService> configChangePluginServiceList,
|
||||
ConfigChangeRequest configChangeRequest) {
|
||||
configChangeRequest.setArg("modifyTime", TimeUtils.getCurrentTimeStr());
|
||||
ConfigChangePointCutTypes handleType = configChangeRequest.getRequestType();
|
||||
ConfigChangeResponse configChangeResponse = new ConfigChangeResponse(handleType);
|
||||
// default success,when before plugin service verify failed , set false
|
||||
configChangeResponse.setSuccess(true);
|
||||
PriorityQueue<ConfigChangePluginService> beforeExecutePriorityQueue = new PriorityQueue<>(
|
||||
DEFAULT_BEFORE_QUEUE_CAPACITY, Comparator.comparingInt(ConfigChangePluginService::getOrder));
|
||||
PriorityQueue<ConfigChangePluginService> afterExecutePriorityQueue = new PriorityQueue<>(
|
||||
DEFAULT_AFTER_QUEUE_CAPACITY, Comparator.comparingInt(ConfigChangePluginService::getOrder));
|
||||
|
||||
List<ConfigChangePluginService> beforeExecutePluginServices = new ArrayList<>(DEFAULT_BEFORE_LIST_CAPACITY);
|
||||
List<ConfigChangePluginService> afterExecutePluginServices = new ArrayList<>(DEFAULT_AFTER_LIST_CAPACITY);
|
||||
|
||||
Object retVal = null;
|
||||
Object[] args = pjp.getArgs();
|
||||
configChangeRequest.setArg(ConfigChangeConstants.ORIGINAL_ARGS, args);
|
||||
|
||||
for (ConfigChangePluginService ccs : configChangePluginServicePriorityQueue) {
|
||||
for (ConfigChangePluginService ccs : configChangePluginServiceList) {
|
||||
if (!isEnabled(ccs)) {
|
||||
continue;
|
||||
}
|
||||
if (ConfigChangeExecuteTypes.EXECUTE_BEFORE_TYPE.equals(ccs.executeType())) {
|
||||
beforeExecutePriorityQueue.add(ccs);
|
||||
beforeExecutePluginServices.add(ccs);
|
||||
} else {
|
||||
afterExecutePriorityQueue.add(ccs);
|
||||
afterExecutePluginServices.add(ccs);
|
||||
}
|
||||
}
|
||||
|
||||
// before plugin service execute
|
||||
for (ConfigChangePluginService ccs : beforeExecutePriorityQueue) {
|
||||
for (ConfigChangePluginService ccs : beforeExecutePluginServices) {
|
||||
final String serviceType = ccs.getServiceType().toLowerCase(Locale.ROOT);
|
||||
final Properties properties = configChangeConfigs.getPluginProperties(serviceType);
|
||||
configChangeRequest.setArg("pluginProperties", properties);
|
||||
@ -339,7 +337,7 @@ public class ConfigChangeAspect {
|
||||
|
||||
// after plugin service execute
|
||||
ConfigExecutor.executeAsyncConfigChangePluginTask(() -> {
|
||||
for (ConfigChangePluginService ccs : afterExecutePriorityQueue) {
|
||||
for (ConfigChangePluginService ccs : afterExecutePluginServices) {
|
||||
try {
|
||||
final String serviceType = ccs.getServiceType().toLowerCase(Locale.ROOT);
|
||||
final Properties properties = configChangeConfigs.getPluginProperties(serviceType);
|
||||
@ -354,19 +352,19 @@ public class ConfigChangeAspect {
|
||||
return retVal;
|
||||
}
|
||||
|
||||
private PriorityQueue<ConfigChangePluginService> getPluginServicePriorityQueue(
|
||||
private List<ConfigChangePluginService> getPluginServices(
|
||||
ConfigChangePointCutTypes configChangePointCutType) {
|
||||
PriorityQueue<ConfigChangePluginService> pluginServicePriorityQueue = ConfigChangePluginManager
|
||||
.findPluginServiceQueueByPointcut(configChangePointCutType);
|
||||
if (pluginServicePriorityQueue == null) {
|
||||
return new PriorityQueue<>();
|
||||
List<ConfigChangePluginService> pluginServicePriorityList = ConfigChangePluginManager
|
||||
.findPluginServicesByPointcut(configChangePointCutType);
|
||||
if (pluginServicePriorityList == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
for (ConfigChangePluginService each : pluginServicePriorityQueue) {
|
||||
for (ConfigChangePluginService each : pluginServicePriorityList) {
|
||||
if (isEnabled(each)) {
|
||||
return pluginServicePriorityQueue;
|
||||
return pluginServicePriorityList;
|
||||
}
|
||||
}
|
||||
return new PriorityQueue<>();
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
private boolean isEnabled(ConfigChangePluginService configChangePluginService) {
|
||||
|
@ -19,8 +19,10 @@ package com.alibaba.nacos.config.server.aspect;
|
||||
import com.alibaba.nacos.api.config.remote.request.ConfigPublishRequest;
|
||||
import com.alibaba.nacos.api.config.remote.request.ConfigRemoveRequest;
|
||||
import com.alibaba.nacos.api.config.remote.response.ConfigPublishResponse;
|
||||
import com.alibaba.nacos.api.config.remote.response.ConfigRemoveResponse;
|
||||
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
|
||||
import com.alibaba.nacos.api.remote.request.RequestMeta;
|
||||
import com.alibaba.nacos.common.event.ServerConfigChangeEvent;
|
||||
import com.alibaba.nacos.config.server.configuration.ConfigChangeConfigs;
|
||||
import com.alibaba.nacos.config.server.model.SameConfigPolicy;
|
||||
import com.alibaba.nacos.config.server.utils.RequestUtil;
|
||||
@ -224,4 +226,46 @@ public class ConfigChangeAspectTest {
|
||||
Assert.assertEquals(configPublishResponse, o);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveConfigAroundRpcException() throws Throwable {
|
||||
Mockito.when(configChangePluginService.executeType()).thenReturn(ConfigChangeExecuteTypes.EXECUTE_BEFORE_TYPE);
|
||||
ProceedingJoinPoint proceedingJoinPoint = Mockito.mock(ProceedingJoinPoint.class);
|
||||
ConfigRemoveRequest request = new ConfigRemoveRequest();
|
||||
RequestMeta requestMeta = new RequestMeta();
|
||||
|
||||
Mockito.when(proceedingJoinPoint.proceed(any())).thenThrow(new NacosRuntimeException(503));
|
||||
//execute
|
||||
Object o = configChangeAspect.removeConfigAroundRpc(proceedingJoinPoint, request, requestMeta);
|
||||
//expect
|
||||
Mockito.verify(configChangePluginService, Mockito.times(1))
|
||||
.execute(any(ConfigChangeRequest.class), any(ConfigChangeResponse.class));
|
||||
|
||||
Assert.assertTrue(((ConfigRemoveResponse) o).getMessage().contains("config change join point fail"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisEnablePluginService() throws Throwable {
|
||||
Properties properties = new Properties();
|
||||
properties.put("mockedConfigChangeService.enabled", "false");
|
||||
propertiesStatic.when(() -> PropertiesUtil.getPropertiesWithPrefix(any(),
|
||||
eq(ConfigChangeConstants.NACOS_CORE_CONFIG_PLUGIN_PREFIX))).thenReturn(properties);
|
||||
configChangeConfigs.onEvent(ServerConfigChangeEvent.newEvent());
|
||||
Assert.assertFalse(Boolean.parseBoolean(configChangeConfigs
|
||||
.getPluginProperties("mockedConfigChangeService").getProperty("enabled")));
|
||||
|
||||
Mockito.when(configChangePluginService.executeType()).thenReturn(ConfigChangeExecuteTypes.EXECUTE_BEFORE_TYPE);
|
||||
Mockito.when(configChangePluginService.getServiceType()).thenReturn("mockedConfigChangeService");
|
||||
ProceedingJoinPoint proceedingJoinPoint = Mockito.mock(ProceedingJoinPoint.class);
|
||||
ConfigRemoveRequest request = new ConfigRemoveRequest();
|
||||
RequestMeta requestMeta = new RequestMeta();
|
||||
ConfigPublishResponse configPublishResponse = ConfigPublishResponse.buildSuccessResponse();
|
||||
Mockito.when(proceedingJoinPoint.proceed()).thenReturn(configPublishResponse);
|
||||
//execute
|
||||
Object o = configChangeAspect.removeConfigAroundRpc(proceedingJoinPoint, request, requestMeta);
|
||||
//expect
|
||||
Mockito.verify(configChangePluginService, Mockito.times(0))
|
||||
.execute(any(ConfigChangeRequest.class), any(ConfigChangeResponse.class));
|
||||
Assert.assertEquals(configPublishResponse, o);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 1999-2020 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed 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.alibaba.nacos.config.server.configuration;
|
||||
|
||||
import com.alibaba.nacos.common.event.ServerConfigChangeEvent;
|
||||
import com.alibaba.nacos.sys.env.EnvUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
/**
|
||||
* Nacos config change configs test.
|
||||
*
|
||||
* @author liyunfei
|
||||
**/
|
||||
public class ConfigChangeConfigsTest {
|
||||
|
||||
private ConfigChangeConfigs configChangeConfigs;
|
||||
|
||||
private MockEnvironment environment;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
environment = new MockEnvironment();
|
||||
environment.setProperty("nacos.core.config.plugin.mockPlugin.enabled", "true");
|
||||
EnvUtil.setEnvironment(environment);
|
||||
configChangeConfigs = new ConfigChangeConfigs();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnable() {
|
||||
Assert.assertTrue(Boolean.parseBoolean(configChangeConfigs
|
||||
.getPluginProperties("mockPlugin").getProperty("enabled")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpgradeEnable() {
|
||||
environment.setProperty("nacos.core.config.plugin.mockPlugin.enabled", "false");
|
||||
configChangeConfigs.onEvent(ServerConfigChangeEvent.newEvent());
|
||||
Assert.assertFalse(Boolean.parseBoolean(configChangeConfigs
|
||||
.getPluginProperties("mockPlugin").getProperty("enabled")));
|
||||
}
|
||||
|
||||
}
|
@ -23,12 +23,13 @@ import com.alibaba.nacos.plugin.config.spi.ConfigChangePluginService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* All config change plugin manager.
|
||||
@ -39,7 +40,7 @@ public class ConfigChangePluginManager {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigChangePluginManager.class);
|
||||
|
||||
private static final Integer PLUGIN_SERVICE_COUNT = 2;
|
||||
private static final Integer PLUGIN_SERVICE_COUNT = 4;
|
||||
|
||||
private static final Integer POINT_CUT_TYPE_COUNT = ConfigChangePointCutTypes.values().length;
|
||||
|
||||
@ -51,10 +52,10 @@ public class ConfigChangePluginManager {
|
||||
PLUGIN_SERVICE_COUNT);
|
||||
|
||||
/**
|
||||
* The relationship of config change pointcut type and the queue of {@link ConfigChangePluginService} will pointcut
|
||||
* The relationship of config change pointcut type and the list of {@link ConfigChangePluginService} will pointcut
|
||||
* it, default capacity is the count of pointcutTypes.
|
||||
*/
|
||||
private static Map<ConfigChangePointCutTypes, PriorityQueue<ConfigChangePluginService>> priorityQueueMap = new ConcurrentHashMap<>(
|
||||
private static final Map<ConfigChangePointCutTypes, List<ConfigChangePluginService>> CONFIG_CHANGE_PLUGIN_SERVICES_MAP = new ConcurrentHashMap<>(
|
||||
POINT_CUT_TYPE_COUNT);
|
||||
|
||||
private static final ConfigChangePluginManager INSTANCE = new ConfigChangePluginManager();
|
||||
@ -83,8 +84,10 @@ public class ConfigChangePluginManager {
|
||||
// map the relationship of pointcut and plugin service
|
||||
addPluginServiceByPointCut(each);
|
||||
}
|
||||
// sort plugin service
|
||||
sortPluginServiceByPointCut();
|
||||
}
|
||||
|
||||
|
||||
public static ConfigChangePluginManager getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
@ -118,23 +121,29 @@ public class ConfigChangePluginManager {
|
||||
* @param pointcutName pointcut method name,detail see {@link ConfigChangePointCutTypes}.
|
||||
* @return
|
||||
*/
|
||||
public static PriorityQueue<ConfigChangePluginService> findPluginServiceQueueByPointcut(
|
||||
public static List<ConfigChangePluginService> findPluginServicesByPointcut(
|
||||
ConfigChangePointCutTypes pointcutName) {
|
||||
return priorityQueueMap.getOrDefault(pointcutName, new PriorityQueue<>());
|
||||
return CONFIG_CHANGE_PLUGIN_SERVICES_MAP.getOrDefault(pointcutName, new ArrayList<>());
|
||||
}
|
||||
|
||||
private static boolean addPluginServiceByPointCut(ConfigChangePluginService configChangePluginService) {
|
||||
private static void addPluginServiceByPointCut(ConfigChangePluginService configChangePluginService) {
|
||||
ConfigChangePointCutTypes[] pointcutNames = configChangePluginService.pointcutMethodNames();
|
||||
for (ConfigChangePointCutTypes name : pointcutNames) {
|
||||
PriorityQueue<ConfigChangePluginService> configChangePluginServicePriorityQueue = priorityQueueMap
|
||||
List<ConfigChangePluginService> configChangePluginServiceList = CONFIG_CHANGE_PLUGIN_SERVICES_MAP
|
||||
.get(name);
|
||||
if (configChangePluginServicePriorityQueue == null) {
|
||||
configChangePluginServicePriorityQueue = new PriorityQueue<>(PLUGIN_SERVICE_COUNT,
|
||||
Comparator.comparingInt(ConfigChangePluginService::getOrder));
|
||||
if (configChangePluginServiceList == null) {
|
||||
configChangePluginServiceList = new ArrayList<>(PLUGIN_SERVICE_COUNT);
|
||||
}
|
||||
configChangePluginServicePriorityQueue.add(configChangePluginService);
|
||||
priorityQueueMap.put(name, configChangePluginServicePriorityQueue);
|
||||
configChangePluginServiceList.add(configChangePluginService);
|
||||
CONFIG_CHANGE_PLUGIN_SERVICES_MAP.put(name, configChangePluginServiceList);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void sortPluginServiceByPointCut() {
|
||||
CONFIG_CHANGE_PLUGIN_SERVICES_MAP.forEach((type, pluginServices) -> {
|
||||
List<ConfigChangePluginService> sortedList = new ArrayList<>(pluginServices);
|
||||
sortedList.sort(Comparator.comparingInt(ConfigChangePluginService::getOrder));
|
||||
CONFIG_CHANGE_PLUGIN_SERVICES_MAP.put(type, sortedList);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -25,8 +25,9 @@ import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* ConfigChangePluginManagerTests.
|
||||
@ -94,28 +95,91 @@ public class ConfigChangePluginManagerTests {
|
||||
return new ConfigChangePointCutTypes[]{ConfigChangePointCutTypes.IMPORT_BY_HTTP, ConfigChangePointCutTypes.PUBLISH_BY_RPC};
|
||||
}
|
||||
});
|
||||
ConfigChangePluginManager.join(new ConfigChangePluginService() {
|
||||
@Override
|
||||
public void execute(ConfigChangeRequest configChangeRequest, ConfigChangeResponse configChangeResponse) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigChangeExecuteTypes executeType() {
|
||||
return ConfigChangeExecuteTypes.EXECUTE_AFTER_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return "test3";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 400;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigChangePointCutTypes[] pointcutMethodNames() {
|
||||
return new ConfigChangePointCutTypes[]{ConfigChangePointCutTypes.IMPORT_BY_HTTP, ConfigChangePointCutTypes.PUBLISH_BY_RPC,
|
||||
ConfigChangePointCutTypes.REMOVE_BATCH_HTTP, ConfigChangePointCutTypes.REMOVE_BY_RPC,
|
||||
ConfigChangePointCutTypes.REMOVE_BY_HTTP};
|
||||
}
|
||||
});
|
||||
|
||||
ConfigChangePluginManager.join(new ConfigChangePluginService() {
|
||||
@Override
|
||||
public void execute(ConfigChangeRequest configChangeRequest, ConfigChangeResponse configChangeResponse) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigChangeExecuteTypes executeType() {
|
||||
return ConfigChangeExecuteTypes.EXECUTE_AFTER_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return "test4";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 600;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigChangePointCutTypes[] pointcutMethodNames() {
|
||||
return new ConfigChangePointCutTypes[]{ConfigChangePointCutTypes.PUBLISH_BY_HTTP, ConfigChangePointCutTypes.REMOVE_BATCH_HTTP,
|
||||
ConfigChangePointCutTypes.REMOVE_BY_RPC, ConfigChangePointCutTypes.REMOVE_BY_HTTP};
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindPluginServiceQueueByPointcut() {
|
||||
PriorityQueue<ConfigChangePluginService> configChangePluginServicePriorityQueue = ConfigChangePluginManager
|
||||
.findPluginServiceQueueByPointcut(ConfigChangePointCutTypes.PUBLISH_BY_HTTP);
|
||||
Assert.assertEquals(1, configChangePluginServicePriorityQueue.size());
|
||||
configChangePluginServicePriorityQueue = ConfigChangePluginManager
|
||||
.findPluginServiceQueueByPointcut(ConfigChangePointCutTypes.PUBLISH_BY_RPC);
|
||||
Assert.assertEquals(2, configChangePluginServicePriorityQueue.size());
|
||||
configChangePluginServicePriorityQueue = ConfigChangePluginManager
|
||||
.findPluginServiceQueueByPointcut(ConfigChangePointCutTypes.IMPORT_BY_HTTP);
|
||||
Assert.assertEquals(1, configChangePluginServicePriorityQueue.size());
|
||||
configChangePluginServicePriorityQueue = ConfigChangePluginManager
|
||||
.findPluginServiceQueueByPointcut(ConfigChangePointCutTypes.REMOVE_BATCH_HTTP);
|
||||
Assert.assertEquals(0, configChangePluginServicePriorityQueue.size());
|
||||
configChangePluginServicePriorityQueue = ConfigChangePluginManager
|
||||
.findPluginServiceQueueByPointcut(ConfigChangePointCutTypes.REMOVE_BY_RPC);
|
||||
Assert.assertEquals(0, configChangePluginServicePriorityQueue.size());
|
||||
configChangePluginServicePriorityQueue = ConfigChangePluginManager
|
||||
.findPluginServiceQueueByPointcut(ConfigChangePointCutTypes.REMOVE_BY_HTTP);
|
||||
Assert.assertEquals(0, configChangePluginServicePriorityQueue.size());
|
||||
List<ConfigChangePluginService> configChangePluginServices = ConfigChangePluginManager
|
||||
.findPluginServicesByPointcut(ConfigChangePointCutTypes.PUBLISH_BY_HTTP);
|
||||
Assert.assertEquals(2, configChangePluginServices.size());
|
||||
Assert.assertTrue(isSorted(configChangePluginServices));
|
||||
configChangePluginServices = ConfigChangePluginManager
|
||||
.findPluginServicesByPointcut(ConfigChangePointCutTypes.PUBLISH_BY_RPC);
|
||||
Assert.assertEquals(3, configChangePluginServices.size());
|
||||
Assert.assertTrue(isSorted(configChangePluginServices));
|
||||
configChangePluginServices = ConfigChangePluginManager
|
||||
.findPluginServicesByPointcut(ConfigChangePointCutTypes.IMPORT_BY_HTTP);
|
||||
Assert.assertEquals(2, configChangePluginServices.size());
|
||||
Assert.assertTrue(isSorted(configChangePluginServices));
|
||||
configChangePluginServices = ConfigChangePluginManager
|
||||
.findPluginServicesByPointcut(ConfigChangePointCutTypes.REMOVE_BATCH_HTTP);
|
||||
Assert.assertEquals(2, configChangePluginServices.size());
|
||||
Assert.assertTrue(isSorted(configChangePluginServices));
|
||||
configChangePluginServices = ConfigChangePluginManager
|
||||
.findPluginServicesByPointcut(ConfigChangePointCutTypes.REMOVE_BY_RPC);
|
||||
Assert.assertEquals(2, configChangePluginServices.size());
|
||||
Assert.assertTrue(isSorted(configChangePluginServices));
|
||||
configChangePluginServices = ConfigChangePluginManager
|
||||
.findPluginServicesByPointcut(ConfigChangePointCutTypes.REMOVE_BY_HTTP);
|
||||
Assert.assertEquals(2, configChangePluginServices.size());
|
||||
Assert.assertTrue(isSorted(configChangePluginServices));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -126,6 +190,15 @@ public class ConfigChangePluginManagerTests {
|
||||
configChangePluginServiceOptional = ConfigChangePluginManager.getInstance().findPluginServiceImpl("test2");
|
||||
Assert.assertTrue(configChangePluginServiceOptional.isPresent());
|
||||
configChangePluginServiceOptional = ConfigChangePluginManager.getInstance().findPluginServiceImpl("test3");
|
||||
Assert.assertTrue(configChangePluginServiceOptional.isPresent());
|
||||
configChangePluginServiceOptional = ConfigChangePluginManager.getInstance().findPluginServiceImpl("test4");
|
||||
Assert.assertTrue(configChangePluginServiceOptional.isPresent());
|
||||
configChangePluginServiceOptional = ConfigChangePluginManager.getInstance().findPluginServiceImpl("test5");
|
||||
Assert.assertFalse(configChangePluginServiceOptional.isPresent());
|
||||
}
|
||||
|
||||
private boolean isSorted(List<ConfigChangePluginService> list) {
|
||||
return IntStream.range(0, list.size() - 1)
|
||||
.allMatch(i -> list.get(i).getOrder() <= list.get(i + 1).getOrder());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user