extern abilities spi processor, refactor AbilityKey.

This commit is contained in:
Daydreamer-ia 2023-09-25 14:17:18 +08:00
parent e5f54badb3
commit c565a2bdd5
14 changed files with 229 additions and 132 deletions

View File

@ -16,26 +16,28 @@
package com.alibaba.nacos.api.ability.constant;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import java.util.*;
import java.util.stream.Collectors;
/**.
/**
* Ability key constant. It is used to constrain the ability key.<br/>
* <strong>Ensure that return value of {@link AbilityKey#getName()} is unique under one specify {@link AbilityMode}<strong/>.
*
* @author Daydreamer
* @description Ability key constant. It is used to constrain the ability key.
* @date 2022/8/31 12:27
**/
public enum AbilityKey {
TEST_1("test_1", "just for junit test"),
SERVER_TEST_1("test_1", "just for junit test", AbilityMode.SERVER),
TEST_2("test_2", "just for junit test");
SERVER_TEST_2("test_2", "just for junit test", AbilityMode.SERVER),
SDK_CLIENT_TEST_1("test_1", "just for junit test", AbilityMode.SDK_CLIENT),
CLUSTER_CLIENT_TEST_1("test_1", "just for junit test", AbilityMode.CLUSTER_CLIENT);
/**.
* the name of a certain ability
/**
* the name of a certain ability.
*/
private final String keyName;
@ -44,11 +46,17 @@ public enum AbilityKey {
*/
private final String description;
AbilityKey(String name, String description) {
this.keyName = name;
/**
* ability mode, which endpoint hold this ability.
*/
private final AbilityMode mode;
AbilityKey(String keyName, String description, AbilityMode mode) {
this.keyName = keyName;
this.description = description;
this.mode = mode;
}
public String getName() {
return keyName;
}
@ -56,54 +64,58 @@ public enum AbilityKey {
public String getDescription() {
return description;
}
/**.
* All key set
public AbilityMode getMode() {
return mode;
}
/**
* All key set.
*/
private static final Map<String, AbilityKey> ALL_ABILITIES;
private static final Map<AbilityMode, Map<String, AbilityKey>> ALL_ABILITIES = new HashMap<>();
/**.
* Get all keys
/**
* Get all keys.
*
* @return all keys
*/
public static Collection<AbilityKey> getAllValues() {
return Collections.unmodifiableCollection(ALL_ABILITIES.values());
public static Collection<AbilityKey> getAllValues(AbilityMode mode) {
return Collections.unmodifiableCollection(ALL_ABILITIES.get(mode).values());
}
/**.
* Get all names
/**
* Get all names.
*
* @return all names
*/
public static Collection<String> getAllNames() {
return Collections.unmodifiableCollection(ALL_ABILITIES.keySet());
public static Collection<String> getAllNames(AbilityMode mode) {
return Collections.unmodifiableCollection(ALL_ABILITIES.get(mode).keySet());
}
/**.
* Whether contains this name
/**
* Whether contains this name.
*
* @param name key name
* @return whether contains
*/
public static boolean isLegalKey(String name) {
return ALL_ABILITIES.containsKey(name);
public static boolean isLegalKey(AbilityMode mode, String name) {
return ALL_ABILITIES.get(mode).containsKey(name);
}
/**.
* Map the string key to enum
/**
* Map the string key to enum.
*
* @param abilities map
* @return enum map
*/
public static Map<AbilityKey, Boolean> mapEnum(Map<String, Boolean> abilities) {
public static Map<AbilityKey, Boolean> mapEnum(AbilityMode mode, Map<String, Boolean> abilities) {
if (abilities == null || abilities.isEmpty()) {
return Collections.emptyMap();
}
return abilities.entrySet()
.stream()
.filter(entry -> isLegalKey(entry.getKey()))
.collect(Collectors.toMap((entry) -> getEnum(entry.getKey()), Map.Entry::getValue));
.filter(entry -> isLegalKey(mode, entry.getKey()))
.collect(Collectors.toMap((entry) -> getEnum(mode, entry.getKey()), Map.Entry::getValue));
}
/**.
@ -127,11 +139,26 @@ public enum AbilityKey {
* @param key string key
* @return enum
*/
public static AbilityKey getEnum(String key) {
return ALL_ABILITIES.get(key);
public static AbilityKey getEnum(AbilityMode mode, String key) {
return ALL_ABILITIES.get(mode).get(key);
}
static {
ALL_ABILITIES = Arrays.stream(AbilityKey.values()).collect(Collectors.toMap(AbilityKey::getName, Function.identity()));
// check for developer
// ensure that name filed is unique under a AbilityMode
try {
for (AbilityKey value : AbilityKey.values()) {
AbilityMode mode = value.mode;
Map<String, AbilityKey> map = ALL_ABILITIES.getOrDefault(mode, new HashMap<>());
AbilityKey previous = map.putIfAbsent(value.getName(), value);
if (previous != null) {
throw new IllegalStateException("Duplicate key name field " + value + " and " + previous + " under mode: " + mode);
}
ALL_ABILITIES.put(mode, map);
}
} catch (Throwable t) {
// for developer checking
t.printStackTrace();
}
}
}

View File

@ -0,0 +1,25 @@
package com.alibaba.nacos.api.ability.constant;
/**
* Ability mode.
*
* @author Daydreamer
* @date 2023/9/25 12:32
**/
public enum AbilityMode {
/**
* for server ability.
*/
SERVER,
/**
* for sdk client.
*/
SDK_CLIENT,
/**
* for cluster client.
*/
CLUSTER_CLIENT;
}

View File

@ -21,6 +21,7 @@ package com.alibaba.nacos.api.ability.initializer;
*
* @author xiweng.yy
*/
@Deprecated
public interface AbilityInitializer<A> {
/**

View File

@ -0,0 +1,22 @@
package com.alibaba.nacos.api.ability.initializer;
import com.alibaba.nacos.api.ability.constant.AbilityKey;
import java.util.Map;
/**
* Nacos ability post processor, load by spi.
*
* @author Daydreamer-ia
*/
public interface AbilityPostProcessor {
/**
* process before loading by <code>Ability Controller </code>.
*
* @param abilities abilities
*/
void process(Map<AbilityKey, Boolean> abilities);
}

View File

@ -17,6 +17,7 @@
package com.alibaba.nacos.api.utils;
import com.alibaba.nacos.api.ability.constant.AbilityKey;
import com.alibaba.nacos.api.ability.constant.AbilityMode;
import org.junit.Assert;
import org.junit.Test;
@ -36,39 +37,39 @@ public class AbilityKeyTest {
Map<String, Boolean> stringBooleanMap = AbilityKey.mapStr(enumMap);
Assert.assertEquals(0, stringBooleanMap.size());
enumMap.put(AbilityKey.TEST_1, true);
enumMap.put(AbilityKey.TEST_2, false);
enumMap.put(AbilityKey.SERVER_TEST_1, true);
enumMap.put(AbilityKey.SERVER_TEST_2, false);
stringBooleanMap = AbilityKey.mapStr(enumMap);
Assert.assertEquals(2, stringBooleanMap.size());
Assert.assertTrue(stringBooleanMap.get(AbilityKey.TEST_1.getName()));
Assert.assertFalse(stringBooleanMap.get(AbilityKey.TEST_2.getName()));
Assert.assertTrue(stringBooleanMap.get(AbilityKey.SERVER_TEST_1.getName()));
Assert.assertFalse(stringBooleanMap.get(AbilityKey.SERVER_TEST_2.getName()));
enumMap.put(AbilityKey.TEST_2, true);
enumMap.put(AbilityKey.SERVER_TEST_2, true);
stringBooleanMap = AbilityKey.mapStr(enumMap);
Assert.assertEquals(2, stringBooleanMap.size());
Assert.assertTrue(stringBooleanMap.get(AbilityKey.TEST_1.getName()));
Assert.assertTrue(stringBooleanMap.get(AbilityKey.TEST_2.getName()));
Assert.assertTrue(stringBooleanMap.get(AbilityKey.SERVER_TEST_1.getName()));
Assert.assertTrue(stringBooleanMap.get(AbilityKey.SERVER_TEST_2.getName()));
}
@Test
public void testMapEnum() {
Map<String, Boolean> mapStr = new HashMap<>();
mapStr.put("test-no-existed", true);
Map<AbilityKey, Boolean> enumMap = AbilityKey.mapEnum(mapStr);
Map<AbilityKey, Boolean> enumMap = AbilityKey.mapEnum(AbilityMode.SERVER, mapStr);
Assert.assertEquals(0, enumMap.size());
mapStr.put(AbilityKey.TEST_2.getName(), false);
mapStr.put(AbilityKey.TEST_1.getName(), true);
enumMap = AbilityKey.mapEnum(mapStr);
Assert.assertFalse(enumMap.get(AbilityKey.TEST_2));
Assert.assertTrue(enumMap.get(AbilityKey.TEST_1));
mapStr.put(AbilityKey.SERVER_TEST_2.getName(), false);
mapStr.put(AbilityKey.SERVER_TEST_1.getName(), true);
enumMap = AbilityKey.mapEnum(AbilityMode.SERVER, mapStr);
Assert.assertFalse(enumMap.get(AbilityKey.SERVER_TEST_2));
Assert.assertTrue(enumMap.get(AbilityKey.SERVER_TEST_1));
mapStr.clear();
mapStr.put(AbilityKey.TEST_2.getName(), true);
mapStr.put(AbilityKey.TEST_1.getName(), true);
enumMap = AbilityKey.mapEnum(mapStr);
Assert.assertTrue(enumMap.get(AbilityKey.TEST_2));
Assert.assertTrue(enumMap.get(AbilityKey.TEST_1));
mapStr.put(AbilityKey.SERVER_TEST_2.getName(), true);
mapStr.put(AbilityKey.SERVER_TEST_1.getName(), true);
enumMap = AbilityKey.mapEnum(AbilityMode.SERVER, mapStr);
Assert.assertTrue(enumMap.get(AbilityKey.SERVER_TEST_2));
Assert.assertTrue(enumMap.get(AbilityKey.SERVER_TEST_1));
}

View File

@ -33,8 +33,8 @@ public class ClientAbilityControlManager extends AbstractAbilityControlManager {
}
@Override
protected Map<String, Boolean> initCurrentNodeAbilities() {
return AbilityKey.mapStr(ClientAbilities.getStaticAbilities());
protected Map<AbilityKey, Boolean> initCurrentNodeAbilities() {
return ClientAbilities.getStaticAbilities();
}
@Override

View File

@ -98,8 +98,8 @@ public class AbilityTest {
{
super.abilityTable = new HashMap<>();
super.abilityTable.put(AbilityKey.TEST_1.getName(), true);
super.abilityTable.put(AbilityKey.TEST_2.getName(), false);
super.abilityTable.put(AbilityKey.SERVER_TEST_1.getName(), true);
super.abilityTable.put(AbilityKey.SERVER_TEST_2.getName(), false);
}
@Override
@ -127,7 +127,7 @@ public class AbilityTest {
};
rpcClient.start();
// test not ready
Assert.assertNull(rpcClient.getConnectionAbility(AbilityKey.TEST_1));
Assert.assertNull(rpcClient.getConnectionAbility(AbilityKey.SERVER_TEST_1));
// test ready
rpcClient.serverListFactory(new ServerListFactory() {
@ -149,23 +149,23 @@ public class AbilityTest {
});
rpcClient.start();
// if connect successfully
Assert.assertEquals(rpcClient.getConnectionAbility(AbilityKey.TEST_1), AbilityStatus.SUPPORTED);
Assert.assertEquals(rpcClient.getConnectionAbility(AbilityKey.TEST_2), AbilityStatus.NOT_SUPPORTED);
Assert.assertEquals(rpcClient.getConnectionAbility(AbilityKey.SERVER_TEST_1), AbilityStatus.SUPPORTED);
Assert.assertEquals(rpcClient.getConnectionAbility(AbilityKey.SERVER_TEST_2), AbilityStatus.NOT_SUPPORTED);
}
@After
public void testServerRequestAbility() {
//test support
ServerRequestHandler serverRequestHandler = (request, connection) -> {
Assert.assertEquals(connection.getConnectionAbility(AbilityKey.TEST_1), AbilityStatus.SUPPORTED);
Assert.assertEquals(connection.getConnectionAbility(AbilityKey.TEST_2), AbilityStatus.NOT_SUPPORTED);
Assert.assertEquals(connection.getConnectionAbility(AbilityKey.SERVER_TEST_1), AbilityStatus.SUPPORTED);
Assert.assertEquals(connection.getConnectionAbility(AbilityKey.SERVER_TEST_2), AbilityStatus.NOT_SUPPORTED);
return new Response() { };
};
serverRequestHandler.requestReply(null, connection);
// test no ability table
serverRequestHandler = (request, connection) -> {
Assert.assertEquals(connection.getConnectionAbility(AbilityKey.TEST_1), AbilityStatus.UNKNOWN);
Assert.assertEquals(connection.getConnectionAbility(AbilityKey.SERVER_TEST_1), AbilityStatus.UNKNOWN);
return new Response() { };
};
serverRequestHandler.requestReply(null, new TestConnection(new RpcClient.ServerInfo()));

View File

@ -17,12 +17,14 @@
package com.alibaba.nacos.common.ability;
import com.alibaba.nacos.api.ability.constant.AbilityKey;
import com.alibaba.nacos.api.ability.initializer.AbilityPostProcessor;
import com.alibaba.nacos.api.ability.register.AbstractAbilityRegistry;
import com.alibaba.nacos.common.JustForTest;
import com.alibaba.nacos.common.ability.handler.HandlerMapping;
import com.alibaba.nacos.common.executor.ExecutorFactory;
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.MapUtil;
import com.alibaba.nacos.common.utils.ThreadUtils;
@ -32,6 +34,7 @@ import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
@ -41,9 +44,10 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**.
/**
* It is a capability control center, manager current node abilities or other control.
*
* @author Daydreamer
* @description It is a capability control center, manager current node abilities or other control.
* @date 2022/7/12 19:18
**/
public abstract class AbstractAbilityControlManager {
@ -72,7 +76,23 @@ public abstract class AbstractAbilityControlManager {
protected AbstractAbilityControlManager() {
ThreadUtils.addShutdownHook(this::destroy);
NotifyCenter.registerToPublisher(AbilityUpdateEvent.class, 16384);
currentRunningAbility.putAll(initCurrentNodeAbilities());
currentRunningAbility.putAll(getAbilityTable());
}
/**
* initialize abilities.
*
* @return abilities
*/
private Map<String, Boolean> getAbilityTable() {
// get processors
Collection<AbilityPostProcessor> processors = NacosServiceLoader.load(AbilityPostProcessor.class);
Map<AbilityKey, Boolean> abilities = initCurrentNodeAbilities();
// get abilities
for (AbilityPostProcessor processor : processors) {
processor.process(abilities);
}
return AbilityKey.mapStr(abilities);
}
/**.
@ -110,7 +130,7 @@ public abstract class AbstractAbilityControlManager {
*
* @return current node abilities
*/
protected abstract Map<String, Boolean> initCurrentNodeAbilities();
protected abstract Map<AbilityKey, Boolean> initCurrentNodeAbilities();
/**.
* Return the abilities current node

View File

@ -38,16 +38,16 @@ public class ServerAbilityControlManager extends AbstractAbilityControlManager {
}
@Override
protected Map<String, Boolean> initCurrentNodeAbilities() {
protected Map<AbilityKey, Boolean> initCurrentNodeAbilities() {
// static abilities
Map<String, Boolean> staticAbilities = AbilityKey.mapStr(ServerAbilities.getStaticAbilities());
Map<AbilityKey, Boolean> staticAbilities = ServerAbilities.getStaticAbilities();
// all function server can support
Set<String> abilityKeys = staticAbilities.keySet();
Map<String, Boolean> abilityTable = new HashMap<>(abilityKeys.size());
Set<AbilityKey> abilityKeys = staticAbilities.keySet();
Map<AbilityKey, Boolean> abilityTable = new HashMap<>(abilityKeys.size());
// if not define in config, then load from ServerAbilities
Set<String> unIncludedInConfig = new HashSet<>();
Set<AbilityKey> unIncludedInConfig = new HashSet<>();
abilityKeys.forEach(abilityKey -> {
String key = AbilityConfigs.PREFIX + abilityKey;
String key = AbilityConfigs.PREFIX + abilityKey.getName();
try {
Boolean property = EnvUtil.getProperty(key, Boolean.class);
// if not null

View File

@ -17,6 +17,7 @@
package com.alibaba.nacos.core.ability;
import com.alibaba.nacos.api.ability.constant.AbilityKey;
import com.alibaba.nacos.api.ability.constant.AbilityMode;
import com.alibaba.nacos.common.ability.handler.HandlerMapping;
import org.junit.Assert;
import org.junit.Before;
@ -40,7 +41,7 @@ public class AbilityControlManagerTest {
@Before
public void inject() {
Map<String, Boolean> newTable = new HashMap<>();
newTable.put(AbilityKey.TEST_1.getName(), true);
newTable.put(AbilityKey.SERVER_TEST_1.getName(), true);
serverAbilityControlManager.setCurrentSupportingAbility(newTable);
}
@ -48,65 +49,65 @@ public class AbilityControlManagerTest {
public void testComponent() throws InterruptedException {
enabled = 0;
// invoke enable() or disable() when registering
serverAbilityControlManager.registerComponent(AbilityKey.TEST_1, new TestHandlerMapping(), -1);
serverAbilityControlManager.registerComponent(AbilityKey.SERVER_TEST_1, new TestHandlerMapping(), -1);
Assert.assertEquals(1, serverAbilityControlManager.handlerMappingCount());
serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.TEST_1);
serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.SERVER_TEST_1);
// wait for invoking handler asyn
Thread.sleep(200L);
// nothing happens if it has enabled
Assert.assertEquals(enabled, 1);
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_1));
// invoke disable()
serverAbilityControlManager.disableCurrentNodeAbility(AbilityKey.TEST_1);
serverAbilityControlManager.disableCurrentNodeAbility(AbilityKey.SERVER_TEST_1);
// wait for invoking handler asyn
Thread.sleep(200L);
// disable will invoke handler
Assert.assertEquals(enabled, 0);
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_1));
serverAbilityControlManager.disableCurrentNodeAbility(AbilityKey.TEST_1);
serverAbilityControlManager.disableCurrentNodeAbility(AbilityKey.SERVER_TEST_1);
// wait for invoking handler asyn
Thread.sleep(200L);
// nothing to do because it has disable
Assert.assertEquals(enabled, 0);
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_1));
serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.TEST_1);
serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.SERVER_TEST_1);
// wait for invoking handler asyn
Thread.sleep(200L);
Assert.assertEquals(enabled, 1);
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_1));
serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.TEST_1);
serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.SERVER_TEST_1);
// wait for invoking handler asyn
Thread.sleep(200L);
Assert.assertEquals(enabled, 1);
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_1));
}
@Test
public void testCurrentNodeAbility() {
Set<String> keySet = serverAbilityControlManager.getCurrentNodeAbilities().keySet();
// diable all
keySet.forEach(key -> serverAbilityControlManager.disableCurrentNodeAbility(AbilityKey.getEnum(key)));
keySet.forEach(key -> serverAbilityControlManager.disableCurrentNodeAbility(AbilityKey.getEnum(AbilityMode.SERVER, key)));
// get all
keySet.forEach(key -> {
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.getEnum(key)));
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.getEnum(AbilityMode.SERVER, key)));
});
// enable all
keySet.forEach(key -> serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.getEnum(key)));
keySet.forEach(key -> serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.getEnum(AbilityMode.SERVER, key)));
// get all
keySet.forEach(key -> {
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.getEnum(key)));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.getEnum(AbilityMode.SERVER, key)));
});
}
@Test
public void testPriority() throws InterruptedException {
TestServerAbilityControlManager testClientAbilityControlManager = new TestServerAbilityControlManager();
AbilityKey key = AbilityKey.TEST_1;
AbilityKey key = AbilityKey.SERVER_TEST_1;
TestPriority handlerMapping1 = new TestPriority("1");
TestPriority handlerMapping2 = new TestPriority("2");
TestPriority handlerMapping3 = new TestPriority("3");

View File

@ -57,18 +57,18 @@ public class AbilityConfigsTest {
EnvUtil.setEnvironment(environment);
abilityConfigs = new TestAbilityConfig();
inject(abilityConfigs);
serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.TEST_1);
serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.TEST_2);
serverAbilityControlManager.registerComponent(AbilityKey.TEST_1, new TestHandler());
serverAbilityControlManager.registerComponent(AbilityKey.TEST_2, new TestHandler());
serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.SERVER_TEST_1);
serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.SERVER_TEST_2);
serverAbilityControlManager.registerComponent(AbilityKey.SERVER_TEST_1, new TestHandler());
serverAbilityControlManager.registerComponent(AbilityKey.SERVER_TEST_2, new TestHandler());
// tmp is 2 now
}
void inject(AbilityConfigs abilityConfigs) {
TestServerAbilityControlManager serverAbilityControlManager = new TestServerAbilityControlManager();
Map<String, Boolean> newTable = new HashMap<>();
newTable.put(AbilityKey.TEST_1.getName(), true);
newTable.put(AbilityKey.TEST_2.getName(), true);
newTable.put(AbilityKey.SERVER_TEST_1.getName(), true);
newTable.put(AbilityKey.SERVER_TEST_2.getName(), true);
serverAbilityControlManager.setCurrentSupportingAbility(newTable);
abilityConfigs.setAbilityHandlerRegistry(serverAbilityControlManager);
this.serverAbilityControlManager = serverAbilityControlManager;
@ -86,63 +86,63 @@ public class AbilityConfigsTest {
instanceField.setAccessible(true);
ServerAbilities serverAbilities = (ServerAbilities) instanceField.get(ServerAbilities.class);
currentAbilities = (Map<AbilityKey, Boolean>) abilitiesField.get(serverAbilities);
currentAbilities.put(AbilityKey.TEST_1, true);
currentAbilities.put(AbilityKey.TEST_2, true);
currentAbilities.put(AbilityKey.SERVER_TEST_1, true);
currentAbilities.put(AbilityKey.SERVER_TEST_2, true);
}
@Test
public void testLoadAbilities() throws Exception {
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.TEST_1.getName(), Boolean.TRUE.toString());
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.TEST_2.getName(), Boolean.FALSE.toString());
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.SERVER_TEST_1.getName(), Boolean.TRUE.toString());
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.SERVER_TEST_2.getName(), Boolean.FALSE.toString());
// test load
fill();
ServerAbilityControlManager manager = new ServerAbilityControlManager();
// config has higher priority
Assert.assertTrue(manager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
Assert.assertFalse(manager.isCurrentNodeAbilityRunning(AbilityKey.TEST_2));
Assert.assertTrue(manager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_1));
Assert.assertFalse(manager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_2));
// clear
currentAbilities.clear();
}
@Test
public void testInit() {
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_2));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_1));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_2));
}
@Test
public void testConfigChange() throws InterruptedException {
// test no change
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.TEST_1.getName(), Boolean.TRUE.toString());
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.TEST_2.getName(), Boolean.TRUE.toString());
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.SERVER_TEST_1.getName(), Boolean.TRUE.toString());
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.SERVER_TEST_2.getName(), Boolean.TRUE.toString());
abilityConfigs.onEvent(new ServerConfigChangeEvent());
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_2));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_1));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_2));
//wait for invoke
Thread.sleep(100);
Assert.assertEquals(tmp, 2);
// test change
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.TEST_1.getName(), Boolean.FALSE.toString());
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.SERVER_TEST_1.getName(), Boolean.FALSE.toString());
abilityConfigs.onEvent(new ServerConfigChangeEvent());
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_2));
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_1));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_2));
//wait for invoke
Thread.sleep(100);
Assert.assertEquals(tmp, 1);
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.TEST_1.getName(), Boolean.TRUE.toString());
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.SERVER_TEST_1.getName(), Boolean.TRUE.toString());
abilityConfigs.onEvent(new ServerConfigChangeEvent());
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_1));
//wait for invoke
Thread.sleep(100);
Assert.assertEquals(tmp, 2);
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.TEST_1.getName(), Boolean.FALSE.toString());
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.TEST_2.getName(), Boolean.FALSE.toString());
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.SERVER_TEST_1.getName(), Boolean.FALSE.toString());
environment.setProperty(AbilityConfigs.PREFIX + AbilityKey.SERVER_TEST_2.getName(), Boolean.FALSE.toString());
abilityConfigs.onEvent(new ServerConfigChangeEvent());
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_2));
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_1));
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.SERVER_TEST_2));
//wait for invoke
Thread.sleep(100);
Assert.assertEquals(tmp, 0);

View File

@ -29,7 +29,7 @@ public class TestAbilityConfig extends AbilityConfigs {
public TestAbilityConfig() {
Set<AbilityKey> serverAbilityKeys = super.getServerAbilityKeys();
serverAbilityKeys.add(AbilityKey.TEST_1);
serverAbilityKeys.add(AbilityKey.TEST_2);
serverAbilityKeys.add(AbilityKey.SERVER_TEST_1);
serverAbilityKeys.add(AbilityKey.SERVER_TEST_2);
}
}

View File

@ -130,8 +130,8 @@ public class AbilityDiscovery {
@Test
public void testClientDiscovery() throws NacosException {
// client judge ability
Assert.assertEquals(client.getConnectionAbility(AbilityKey.TEST_1), AbilityStatus.SUPPORTED);
Assert.assertEquals(client.getConnectionAbility(AbilityKey.TEST_2), AbilityStatus.NOT_SUPPORTED);
Assert.assertEquals(client.getConnectionAbility(AbilityKey.SERVER_TEST_1), AbilityStatus.SUPPORTED);
Assert.assertEquals(client.getConnectionAbility(AbilityKey.SERVER_TEST_2), AbilityStatus.NOT_SUPPORTED);
}
@Test
@ -157,8 +157,8 @@ public class AbilityDiscovery {
client.registerServerRequestHandler(new ServerRequestHandler() {
@Override
public Response requestReply(Request request, Connection connection) {
if (connection.getConnectionAbility(AbilityKey.TEST_1).equals(AbilityStatus.SUPPORTED) && connection
.getConnectionAbility(AbilityKey.TEST_2).equals(AbilityStatus.NOT_SUPPORTED)) {
if (connection.getConnectionAbility(AbilityKey.SERVER_TEST_1).equals(AbilityStatus.SUPPORTED) && connection
.getConnectionAbility(AbilityKey.SERVER_TEST_2).equals(AbilityStatus.NOT_SUPPORTED)) {
clientSuccess = true;
}
return new Response(){};
@ -199,8 +199,8 @@ public class AbilityDiscovery {
@Override
public ConfigQueryResponse handle(ConfigQueryRequest request, RequestMeta meta) throws NacosException {
if (meta.getConnectionAbility(AbilityKey.TEST_1).equals(AbilityStatus.SUPPORTED) && meta
.getConnectionAbility(AbilityKey.TEST_2).equals(AbilityStatus.NOT_SUPPORTED)) {
if (meta.getConnectionAbility(AbilityKey.SERVER_TEST_1).equals(AbilityStatus.SUPPORTED) && meta
.getConnectionAbility(AbilityKey.SERVER_TEST_2).equals(AbilityStatus.NOT_SUPPORTED)) {
serverSuccess = true;
}
return new ConfigQueryResponse();

View File

@ -9,10 +9,10 @@ import java.util.Map;
public class TestServerAbilityControlManager extends ServerAbilityControlManager {
@Override
protected Map<String, Boolean> initCurrentNodeAbilities() {
Map<String, Boolean> map = new HashMap<>();
map.put(AbilityKey.TEST_1.getName(), true);
map.put(AbilityKey.TEST_2.getName(), false);
protected Map<AbilityKey, Boolean> initCurrentNodeAbilities() {
Map<AbilityKey, Boolean> map = new HashMap<>();
map.put(AbilityKey.SERVER_TEST_1, true);
map.put(AbilityKey.SERVER_TEST_2, false);
return map;
}
}