Remove the component listening to cluster ability, disable the cluster capability table when the connected server does not support capability negotiation.
This commit is contained in:
parent
41d278d6f1
commit
182b821938
@ -51,7 +51,7 @@ public abstract class AbstractAbilityRegistry {
|
||||
abilityBitFlag = AbilityTableUtils.getAbilityBiTableBy(AbilityKey.values(), supportedAbilities);
|
||||
}
|
||||
|
||||
/**
|
||||
/**.
|
||||
* get static ability current server supports
|
||||
*
|
||||
* @return static ability
|
||||
|
@ -61,12 +61,12 @@ public class ClientAbilities extends AbstractAbilityRegistry {
|
||||
return INSTANCE.getAbilityBitFlags();
|
||||
}
|
||||
|
||||
/**
|
||||
/**.
|
||||
* get static ability current server supports
|
||||
*
|
||||
* @return static ability
|
||||
*/
|
||||
public static Map<AbilityKey, Boolean> getStaticAbilities(){
|
||||
public static Map<AbilityKey, Boolean> getStaticAbilities() {
|
||||
return INSTANCE.getSupportedAbilities();
|
||||
}
|
||||
}
|
||||
|
@ -61,12 +61,12 @@ public class ServerAbilities extends AbstractAbilityRegistry {
|
||||
return INSTANCE.getAbilityBitFlags();
|
||||
}
|
||||
|
||||
/**
|
||||
/**.
|
||||
* get static ability current server supports
|
||||
*
|
||||
* @return static ability
|
||||
*/
|
||||
public static Map<AbilityKey, Boolean> getStaticAbilities(){
|
||||
public static Map<AbilityKey, Boolean> getStaticAbilities() {
|
||||
return INSTANCE.getSupportedAbilities();
|
||||
}
|
||||
|
||||
|
@ -116,12 +116,12 @@ public class AbilityTableUtils {
|
||||
return getAbilityBitBy(res.values());
|
||||
}
|
||||
|
||||
/**
|
||||
/**.
|
||||
* get ability bit table by existed ability table and abilityKeys array
|
||||
*
|
||||
* @param abilityKeys abilityKeys array
|
||||
* @param abilityTable existed ability table
|
||||
* @return filter ability which value is false in <code>abilityTable<code/>
|
||||
* @return filter ability which value is false in <code>abilityTable</code>
|
||||
*/
|
||||
public static byte[] getAbilityBiTableBy(AbilityKey[] abilityKeys, Map<AbilityKey, Boolean> abilityTable) {
|
||||
// filter the element which <code>abilityTable</code> don't have or value is false
|
||||
|
@ -35,8 +35,8 @@ public class AbilityTableUtilsTest {
|
||||
Assert.assertEquals(abilityBitBy[2], -128);
|
||||
// clear
|
||||
byte[] abilityBits = AbilityTableUtils.getAbilityBitBy(Collections.emptyList());
|
||||
Assert.assertEquals(abilityBits.length , 1);
|
||||
Assert.assertEquals(abilityBits[0] , 0);
|
||||
Assert.assertEquals(abilityBits.length, 1);
|
||||
Assert.assertEquals(abilityBits[0], 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -169,7 +169,6 @@ public class AbilityControlManagerTest {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class TestHandlerMapping implements HandlerMapping {
|
||||
|
||||
@Override
|
||||
|
@ -117,15 +117,18 @@ public abstract class AbstractAbilityControlManager implements AbilityControlMan
|
||||
}
|
||||
// hook method
|
||||
add(table);
|
||||
// add to node
|
||||
Set<AbilityKey> abilityKeys = table.getAbility().keySet();
|
||||
// null if not support ability table
|
||||
Map<AbilityKey, Boolean> clientAbilities = table.getAbility();
|
||||
abilityKeys.forEach(abilityKey -> {
|
||||
Boolean res = currentRunningAbility.getOrDefault(abilityKey, false);
|
||||
Boolean coming = clientAbilities.getOrDefault(abilityKey, false);
|
||||
clientAbilities.put(abilityKey, res && coming);
|
||||
});
|
||||
nodeAbilityTable.put(connectionId, table);
|
||||
if (clientAbilities != null) {
|
||||
// add to nod
|
||||
Set<AbilityKey> abilityKeys = table.getAbility().keySet();
|
||||
abilityKeys.forEach(abilityKey -> {
|
||||
Boolean res = currentRunningAbility.getOrDefault(abilityKey, false);
|
||||
Boolean coming = clientAbilities.getOrDefault(abilityKey, false);
|
||||
clientAbilities.put(abilityKey, res && coming);
|
||||
});
|
||||
nodeAbilityTable.put(connectionId, table);
|
||||
}
|
||||
} finally {
|
||||
lockForAbilityTable.unlock();
|
||||
}
|
||||
@ -143,17 +146,9 @@ public abstract class AbstractAbilityControlManager implements AbilityControlMan
|
||||
*/
|
||||
@Override
|
||||
public final void removeTable(String connectionId) {
|
||||
// if not exists
|
||||
if(connectionId == null || !nodeAbilityTable.containsKey(connectionId)){
|
||||
return;
|
||||
}
|
||||
AbilityTable removingTable = null;
|
||||
lockForAbilityTable.lock();
|
||||
try {
|
||||
// check
|
||||
if (!nodeAbilityTable.containsKey(connectionId)) {
|
||||
return;
|
||||
}
|
||||
// hook method
|
||||
remove(connectionId);
|
||||
// remove
|
||||
|
@ -18,7 +18,6 @@ package com.alibaba.nacos.common.remote.client.grpc;
|
||||
|
||||
import com.alibaba.nacos.api.ability.constant.AbilityKey;
|
||||
import com.alibaba.nacos.api.ability.entity.AbilityTable;
|
||||
import com.alibaba.nacos.api.ability.register.impl.ServerAbilities;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.grpc.auto.BiRequestStreamGrpc;
|
||||
import com.alibaba.nacos.api.grpc.auto.Payload;
|
||||
@ -48,7 +47,6 @@ import io.grpc.stub.StreamObserver;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
@ -319,12 +317,15 @@ public abstract class GrpcClient extends RpcClient {
|
||||
// submit ability table as soon as possible
|
||||
// ability table will be null if server doesn't support ability table
|
||||
ServerCheckResponse serverCheckResponse = (ServerCheckResponse) response;
|
||||
Map<AbilityKey, Boolean> abilityTable = AbilityTableUtils
|
||||
.getAbilityTableBy(serverCheckResponse.getAbilities(), AbilityKey.offset());
|
||||
AbilityTable table = new AbilityTable();
|
||||
table.setServer(true)
|
||||
.setConnectionId(serverCheckResponse.getConnectionId())
|
||||
.setAbility(abilityTable);
|
||||
.setConnectionId(serverCheckResponse.getConnectionId());
|
||||
// if not supported, it will be null
|
||||
if (serverCheckResponse.getAbilities() != null) {
|
||||
Map<AbilityKey, Boolean> abilityTable = AbilityTableUtils
|
||||
.getAbilityTableBy(serverCheckResponse.getAbilities(), AbilityKey.offset());
|
||||
table.setAbility(abilityTable);
|
||||
}
|
||||
NacosAbilityManagerHolder.getInstance().addNewTable(table);
|
||||
|
||||
BiRequestStreamGrpc.BiRequestStreamStub biRequestStreamStub = BiRequestStreamGrpc
|
||||
|
@ -23,21 +23,16 @@ import com.alibaba.nacos.api.ability.register.impl.ServerAbilities;
|
||||
import com.alibaba.nacos.common.JustForTest;
|
||||
import com.alibaba.nacos.common.ability.AbstractAbilityControlManager;
|
||||
import com.alibaba.nacos.common.ability.DefaultAbilityControlManager;
|
||||
import com.alibaba.nacos.common.ability.handler.HandlerMapping;
|
||||
import com.alibaba.nacos.common.notify.NotifyCenter;
|
||||
import com.alibaba.nacos.common.utils.ConcurrentHashSet;
|
||||
import com.alibaba.nacos.common.utils.MapUtil;
|
||||
import com.alibaba.nacos.core.ability.inte.ClusterAbilityControlSupport;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**.
|
||||
* @author Daydreamer
|
||||
@ -46,8 +41,6 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
**/
|
||||
public class ServerAbilityControlManager extends DefaultAbilityControlManager implements ClusterAbilityControlSupport {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ServerAbilityControlManager.class);
|
||||
|
||||
/**.
|
||||
* ability for cluster
|
||||
*/
|
||||
@ -57,13 +50,11 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im
|
||||
* ability for server
|
||||
*/
|
||||
private final Map<String, AbilityTable> serversAbilityTable = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* components for cluster. these will be invoked if cluster ability table changes.
|
||||
*/
|
||||
private final Map<AbilityKey, List<HandlerWithPriority>> clusterHandlerMapping = new ConcurrentHashMap<>();
|
||||
|
||||
private Lock lockForClusterComponents = new ReentrantLock();
|
||||
/**.
|
||||
* Number of servers that do not support capability negotiation
|
||||
*/
|
||||
private final ConcurrentHashSet<String> serverNoAbilityNegotiation = new ConcurrentHashSet<>();
|
||||
|
||||
public ServerAbilityControlManager() {
|
||||
// add current node into
|
||||
@ -92,14 +83,17 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im
|
||||
}
|
||||
|
||||
/**.
|
||||
* Whether current cluster supports ability
|
||||
* Whether all the servers currently connected support a certain capability
|
||||
*
|
||||
* @param abilityKey ability key
|
||||
* @return whether it is turn on
|
||||
*/
|
||||
@Override
|
||||
public boolean isClusterEnableAbility(AbilityKey abilityKey) {
|
||||
return clusterAbilityTable.getOrDefault(abilityKey, Boolean.FALSE);
|
||||
public AbilityStatus isClusterEnableAbilityNow(AbilityKey abilityKey) {
|
||||
if (serverNoAbilityNegotiation.size() > 0) {
|
||||
return AbilityStatus.UNKNOWN;
|
||||
}
|
||||
return clusterAbilityTable.getOrDefault(abilityKey, Boolean.FALSE) ? AbilityStatus.SUPPORTED : AbilityStatus.NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -107,29 +101,6 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im
|
||||
return Collections.unmodifiableMap(clusterAbilityTable);
|
||||
}
|
||||
|
||||
/**.
|
||||
* Register components for cluster. These will be trigger when its interested ability changes
|
||||
*
|
||||
* @param abilityKey ability key
|
||||
* @param priority the higher the priority, the faster it will be called
|
||||
* @param handlerMapping component
|
||||
*/
|
||||
@Override
|
||||
public void registerComponentForCluster(AbilityKey abilityKey, HandlerMapping handlerMapping, int priority) {
|
||||
doRegisterComponent(abilityKey, handlerMapping, this.clusterHandlerMapping, lockForClusterComponents, priority, clusterAbilityTable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int removeClusterComponent(AbilityKey abilityKey, Class<? extends HandlerMapping> handlerMappingClazz) {
|
||||
return doRemove(abilityKey, handlerMappingClazz, lockForClusterComponents, clusterHandlerMapping);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int removeAllForCluster(AbilityKey abilityKey) {
|
||||
List<HandlerWithPriority> remove = this.clusterHandlerMapping.remove(abilityKey);
|
||||
return Optional.ofNullable(remove).orElse(Collections.emptyList()).size();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void add(AbilityTable table) {
|
||||
// from which env
|
||||
@ -148,13 +119,15 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im
|
||||
Boolean newRes = val && isEnabled;
|
||||
// if ability changes
|
||||
if (!newRes.equals(isEnabled)) {
|
||||
triggerHandlerMappingAsyn(abilityKey, false, this.clusterHandlerMapping);
|
||||
clusterAbilityTable.replace(abilityKey, false);
|
||||
// notify
|
||||
NotifyCenter.publishEvent(buildClusterEvent(abilityKey, false));
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (isServer && table.getAbility() == null) {
|
||||
// add mark if server doesn't support ability table
|
||||
serverNoAbilityNegotiation.add(table.getConnectionId());
|
||||
}
|
||||
}
|
||||
|
||||
@ -171,13 +144,15 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im
|
||||
protected void remove(String connectionId) {
|
||||
// from which
|
||||
AbilityTable abilityTable = nodeAbilityTable.get(connectionId);
|
||||
// if not support
|
||||
serverNoAbilityNegotiation.remove(connectionId);
|
||||
// return if null
|
||||
if (abilityTable == null) {
|
||||
return;
|
||||
}
|
||||
// from which env
|
||||
if (abilityTable.isServer()) {
|
||||
// remove from server ability collection
|
||||
// remove from server ability collection if support
|
||||
serversAbilityTable.remove(connectionId);
|
||||
// remove from cluster
|
||||
if (MapUtil.isNotEmpty(serversAbilityTable)) {
|
||||
@ -198,7 +173,6 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im
|
||||
clusterAbilityTable.replace(abilityKey, newVal);
|
||||
// if change
|
||||
if (!isEnabled.equals(newVal)) {
|
||||
triggerHandlerMappingAsyn(abilityKey, newVal, this.clusterHandlerMapping);
|
||||
// notify
|
||||
NotifyCenter.publishEvent(buildClusterEvent(abilityKey, newVal));
|
||||
}
|
||||
@ -207,16 +181,6 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doDestroy() {
|
||||
if (MapUtil.isNotEmpty(clusterHandlerMapping)) {
|
||||
if (MapUtil.isNotEmpty(clusterHandlerMapping)) {
|
||||
clusterHandlerMapping.keySet().forEach(key -> doTriggerSyn(key, false, clusterHandlerMapping));
|
||||
}
|
||||
}
|
||||
LOGGER.warn("[ServerAbilityControlManager] - Destruction of the end");
|
||||
}
|
||||
|
||||
/**.
|
||||
* notify when current node ability changing
|
||||
*/
|
||||
@ -252,10 +216,10 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im
|
||||
protected void setClusterAbilityTable(Map<AbilityKey, Boolean> map) {
|
||||
clusterAbilityTable.putAll(map);
|
||||
}
|
||||
|
||||
|
||||
@JustForTest
|
||||
protected Map<AbilityKey, List<HandlerWithPriority>> clusterHandlerMapping() {
|
||||
return this.clusterHandlerMapping;
|
||||
protected Set<String> serverNotSupport() {
|
||||
return serverNoAbilityNegotiation;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -17,7 +17,7 @@
|
||||
package com.alibaba.nacos.core.ability.inte;
|
||||
|
||||
import com.alibaba.nacos.api.ability.constant.AbilityKey;
|
||||
import com.alibaba.nacos.common.ability.handler.HandlerMapping;
|
||||
import com.alibaba.nacos.api.ability.constant.AbilityStatus;
|
||||
import com.alibaba.nacos.common.ability.inter.AbilityControlManager;
|
||||
|
||||
import java.util.Map;
|
||||
@ -37,45 +37,10 @@ public interface ClusterAbilityControlSupport {
|
||||
Map<AbilityKey, Boolean> getClusterAbility();
|
||||
|
||||
/**.
|
||||
* Register components for cluster. These will be trigger when its interested ability changes
|
||||
*
|
||||
* @param abilityKey ability key
|
||||
* @param priority a positive number, the higher the priority, the faster it will be called
|
||||
* @param handlerMapping component
|
||||
*/
|
||||
void registerComponentForCluster(AbilityKey abilityKey, HandlerMapping handlerMapping, int priority);
|
||||
|
||||
/**.
|
||||
* Default method to register component
|
||||
*
|
||||
* @param abilityKey component key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey}.
|
||||
* @param handlerMapping component instance.
|
||||
*/
|
||||
default void registerComponentForCluster(AbilityKey abilityKey, HandlerMapping handlerMapping) {
|
||||
registerComponentForCluster(abilityKey, handlerMapping, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the component instance of <p>handlerMappingClazz</p>.
|
||||
*
|
||||
* @param abilityKey ability key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey}.
|
||||
* @param handlerMappingClazz implement of {@link HandlerMapping}
|
||||
* @return the count of components have removed
|
||||
*/
|
||||
int removeClusterComponent(AbilityKey abilityKey, Class<? extends HandlerMapping> handlerMappingClazz);
|
||||
|
||||
/**
|
||||
* Remove all {@link HandlerMapping} interested in the special ability.
|
||||
* @param abilityKey abnility key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey}.
|
||||
* @return the count of components have removed
|
||||
*/
|
||||
int removeAllForCluster(AbilityKey abilityKey);
|
||||
|
||||
/**.
|
||||
* Whether current cluster supports ability
|
||||
* Whether all the servers currently connected support a certain capability
|
||||
*
|
||||
* @param abilityKey ability key
|
||||
* @return whether it is turn on
|
||||
*/
|
||||
boolean isClusterEnableAbility(AbilityKey abilityKey);
|
||||
AbilityStatus isClusterEnableAbilityNow(AbilityKey abilityKey);
|
||||
}
|
||||
|
@ -122,8 +122,11 @@ public class GrpcBiStreamRequestAcceptor extends BiRequestStreamGrpc.BiRequestSt
|
||||
setUpRequest.getClientVersion(), appName, setUpRequest.getLabels());
|
||||
metaInfo.setTenant(setUpRequest.getTenant());
|
||||
Connection connection = new GrpcConnection(metaInfo, responseObserver, CONTEXT_KEY_CHANNEL.get());
|
||||
connection.setAbilityTable(AbilityTableUtils.getAbilityTableBy(setUpRequest.getAbilityTable(),
|
||||
AbilityKey.offset()));
|
||||
// null if supported
|
||||
if (setUpRequest.getAbilityTable() != null) {
|
||||
connection.setAbilityTable(AbilityTableUtils.getAbilityTableBy(setUpRequest.getAbilityTable(),
|
||||
AbilityKey.offset()));
|
||||
}
|
||||
boolean rejectSdkOnStarting = metaInfo.isSdkSource() && !ApplicationUtils.isStarted();
|
||||
|
||||
if (rejectSdkOnStarting || !connectionManager.register(connectionId, connection)) {
|
||||
|
@ -26,7 +26,6 @@ import org.junit.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ -35,11 +34,7 @@ public class AbilityControlManagerTest {
|
||||
|
||||
private TestServerAbilityControlManager serverAbilityControlManager = new TestServerAbilityControlManager();
|
||||
|
||||
private volatile int clusterEnabled = 0;
|
||||
|
||||
private volatile int enabled = 0;
|
||||
|
||||
private volatile LinkedList<String> testPriority = new LinkedList<>();
|
||||
|
||||
@Before
|
||||
public void inject() {
|
||||
@ -83,7 +78,6 @@ public class AbilityControlManagerTest {
|
||||
serverAbilityControlManager.addNewTable(table);
|
||||
Assert.assertEquals(AbilityStatus.NOT_SUPPORTED, serverAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_2));
|
||||
Assert.assertEquals(AbilityStatus.SUPPORTED, serverAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_1));
|
||||
Assert.assertTrue(serverAbilityControlManager.isClusterEnableAbility(AbilityKey.TEST_1));
|
||||
|
||||
Map<AbilityKey, Boolean> otherServer = new HashMap<>();
|
||||
otherServer.put(AbilityKey.TEST_2, true);
|
||||
@ -93,7 +87,6 @@ public class AbilityControlManagerTest {
|
||||
otherServerTable.setAbility(otherServer);
|
||||
otherServerTable.setServer(true);
|
||||
serverAbilityControlManager.addNewTable(otherServerTable);
|
||||
Assert.assertFalse(serverAbilityControlManager.isClusterEnableAbility(AbilityKey.TEST_1));
|
||||
|
||||
Map<AbilityKey, Boolean> clientTa = new HashMap<>();
|
||||
clientTa.put(AbilityKey.TEST_2, true);
|
||||
@ -103,7 +96,31 @@ public class AbilityControlManagerTest {
|
||||
clientTable.setAbility(clientTa);
|
||||
clientTable.setServer(false);
|
||||
serverAbilityControlManager.addNewTable(clientTable);
|
||||
Assert.assertFalse(serverAbilityControlManager.isClusterEnableAbility(AbilityKey.TEST_1));
|
||||
|
||||
// if not support
|
||||
AbilityTable serverTable = new AbilityTable();
|
||||
serverTable.setConnectionId("test-001231");
|
||||
serverTable.setServer(true);
|
||||
serverAbilityControlManager.addNewTable(serverTable);
|
||||
// unknown because not support
|
||||
Assert.assertEquals(serverAbilityControlManager.isClusterEnableAbilityNow(AbilityKey.TEST_1), AbilityStatus.UNKNOWN);
|
||||
Assert.assertEquals(serverAbilityControlManager.getServerNotSupportAbility().size(), 1);
|
||||
Assert.assertTrue(serverAbilityControlManager.getServerNotSupportAbility().contains("test-001231"));
|
||||
|
||||
AbilityTable serverTable1 = new AbilityTable();
|
||||
serverTable1.setConnectionId("test-001231231");
|
||||
serverTable1.setServer(true);
|
||||
serverAbilityControlManager.addNewTable(serverTable1);
|
||||
// unknown because not support
|
||||
Assert.assertEquals(serverAbilityControlManager.isClusterEnableAbilityNow(AbilityKey.TEST_1), AbilityStatus.UNKNOWN);
|
||||
Assert.assertEquals(serverAbilityControlManager.getServerNotSupportAbility().size(), 2);
|
||||
Assert.assertTrue(serverAbilityControlManager.getServerNotSupportAbility().contains("test-001231231"));
|
||||
|
||||
// remove then support
|
||||
serverAbilityControlManager.removeTable("test-001231");
|
||||
Assert.assertEquals(serverAbilityControlManager.isClusterEnableAbilityNow(AbilityKey.TEST_1), AbilityStatus.UNKNOWN);
|
||||
serverAbilityControlManager.removeTable("test-001231231");
|
||||
Assert.assertEquals(serverAbilityControlManager.isClusterEnableAbilityNow(AbilityKey.TEST_1), AbilityStatus.NOT_SUPPORTED);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -162,38 +179,6 @@ public class AbilityControlManagerTest {
|
||||
Assert.assertEquals(enabled, 1);
|
||||
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClusterComponent() throws InterruptedException {
|
||||
clusterEnabled = 0;
|
||||
// invoke enable() because it turn on
|
||||
serverAbilityControlManager.registerComponentForCluster(AbilityKey.TEST_1, new ClusterHandlerMapping(), -1);
|
||||
Assert.assertEquals(1, serverAbilityControlManager.clusterHandlerMappingCount());
|
||||
Assert.assertTrue(serverAbilityControlManager.isClusterEnableAbility(AbilityKey.TEST_1));
|
||||
Assert.assertEquals(clusterEnabled, 1);
|
||||
|
||||
Map<AbilityKey, Boolean> serverAbility = new HashMap<>();
|
||||
serverAbility.put(AbilityKey.TEST_2, true);
|
||||
serverAbility.put(AbilityKey.TEST_1, false);
|
||||
AbilityTable serverTable = new AbilityTable();
|
||||
serverTable.setConnectionId("test-01111");
|
||||
serverTable.setAbility(serverAbility);
|
||||
serverTable.setServer(true);
|
||||
serverAbilityControlManager.addNewTable(serverTable);
|
||||
// wait for invoking handler asyn
|
||||
Thread.sleep(200L);
|
||||
|
||||
// disabled
|
||||
Assert.assertFalse(serverAbilityControlManager.isClusterEnableAbility(AbilityKey.TEST_1));
|
||||
Assert.assertEquals(clusterEnabled, 0);
|
||||
|
||||
// remove this table to enabled
|
||||
serverAbilityControlManager.removeTable("test-01111");
|
||||
// wait for invoking handler asyn
|
||||
Thread.sleep(200L);
|
||||
Assert.assertTrue(serverAbilityControlManager.isClusterEnableAbility(AbilityKey.TEST_1));
|
||||
Assert.assertEquals(clusterEnabled, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCurrentNodeAbility() {
|
||||
@ -212,101 +197,6 @@ public class AbilityControlManagerTest {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPriority() throws InterruptedException {
|
||||
TestServerAbilityControlManager testServerAbilityControlManager = new TestServerAbilityControlManager();
|
||||
AbilityKey key = AbilityKey.TEST_1;
|
||||
TestPriority clusterHandlerMapping1 = new TestPriority("1");
|
||||
TestPriority clusterHandlerMapping2 = new TestPriority("2");
|
||||
TestPriority clusterHandlerMapping3 = new TestPriority("3");
|
||||
// first one, invoke enable()
|
||||
testServerAbilityControlManager.registerComponentForCluster(key, clusterHandlerMapping2, 128);
|
||||
// last one, invoke enable()
|
||||
testServerAbilityControlManager.registerComponentForCluster(key, clusterHandlerMapping3);
|
||||
// second one, invoke enable()
|
||||
testServerAbilityControlManager.registerComponentForCluster(key, clusterHandlerMapping1, 12);
|
||||
// trigger cluster
|
||||
testServerAbilityControlManager.triggerCluster(key);
|
||||
Assert.assertEquals(3, testServerAbilityControlManager.getClusterHandlerMapping(key).size());
|
||||
// wait for invoking
|
||||
Thread.sleep(200L);
|
||||
Assert.assertEquals("2", testPriority.poll());
|
||||
Assert.assertEquals("3", testPriority.poll());
|
||||
Assert.assertEquals("1", testPriority.poll());
|
||||
// here are priority
|
||||
Assert.assertEquals("2", testPriority.poll());
|
||||
Assert.assertEquals("1", testPriority.poll());
|
||||
Assert.assertEquals("3", testPriority.poll());
|
||||
// remove
|
||||
testServerAbilityControlManager.registerClusterHandlerMapping(key, new ClusterHandlerMapping(), -1);
|
||||
Assert.assertEquals(4, testServerAbilityControlManager.getClusterHandlerMapping(key).size());
|
||||
Assert.assertEquals(1, testServerAbilityControlManager.removeClusterComponent(key, ClusterHandlerMapping.class));
|
||||
Assert.assertEquals(3, testServerAbilityControlManager.getClusterHandlerMapping(key).size());
|
||||
testServerAbilityControlManager.removeAllForCluster(key);
|
||||
Assert.assertNull(testServerAbilityControlManager.getClusterHandlerMapping(key));
|
||||
|
||||
// first one
|
||||
testServerAbilityControlManager.registerComponent(key, clusterHandlerMapping2, 128);
|
||||
// last one
|
||||
testServerAbilityControlManager.registerComponent(key, clusterHandlerMapping3);
|
||||
// second one
|
||||
testServerAbilityControlManager.registerComponent(key, clusterHandlerMapping1, 12);
|
||||
Assert.assertEquals(3, testServerAbilityControlManager.getHandlerMapping(key).size());
|
||||
// wait for invoking
|
||||
Thread.sleep(200L);
|
||||
// trigger
|
||||
testServerAbilityControlManager.trigger(key);
|
||||
// wait for invoking
|
||||
Thread.sleep(200L);
|
||||
Assert.assertEquals("2", testPriority.poll());
|
||||
Assert.assertEquals("3", testPriority.poll());
|
||||
Assert.assertEquals("1", testPriority.poll());
|
||||
// here are priority
|
||||
Assert.assertEquals("2", testPriority.poll());
|
||||
Assert.assertEquals("1", testPriority.poll());
|
||||
Assert.assertEquals("3", testPriority.poll());
|
||||
// remove
|
||||
testServerAbilityControlManager.registerComponent(key, new ClusterHandlerMapping(), -1);
|
||||
Assert.assertEquals(4, testServerAbilityControlManager.getHandlerMapping(key).size());
|
||||
Assert.assertEquals(1, testServerAbilityControlManager.removeComponent(key, ClusterHandlerMapping.class));
|
||||
Assert.assertEquals(3, testServerAbilityControlManager.getHandlerMapping(key).size());
|
||||
testServerAbilityControlManager.removeAll(key);
|
||||
Assert.assertNull(testServerAbilityControlManager.getClusterHandlerMapping(key));
|
||||
}
|
||||
|
||||
class TestPriority implements HandlerMapping {
|
||||
|
||||
String mark;
|
||||
|
||||
public TestPriority(String mark) {
|
||||
// unique one
|
||||
this.mark = mark.intern();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enable() {
|
||||
testPriority.offer(mark);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disable() {
|
||||
testPriority.offer(mark);
|
||||
}
|
||||
}
|
||||
|
||||
class ClusterHandlerMapping implements HandlerMapping {
|
||||
|
||||
@Override
|
||||
public void enable() {
|
||||
clusterEnabled++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disable() {
|
||||
clusterEnabled--;
|
||||
}
|
||||
}
|
||||
|
||||
class TestHandlerMapping implements HandlerMapping {
|
||||
|
||||
@Override
|
||||
|
@ -18,12 +18,10 @@ package com.alibaba.nacos.core.ability;
|
||||
|
||||
import com.alibaba.nacos.api.ability.constant.AbilityKey;
|
||||
import com.alibaba.nacos.common.JustForTest;
|
||||
import com.alibaba.nacos.common.ability.handler.HandlerMapping;
|
||||
import com.alibaba.nacos.core.ability.control.ServerAbilityControlManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class TestServerAbilityControlManager extends ServerAbilityControlManager {
|
||||
|
||||
@ -43,37 +41,8 @@ public class TestServerAbilityControlManager extends ServerAbilityControlManager
|
||||
}
|
||||
|
||||
@JustForTest
|
||||
public List<HandlerWithPriority> getHandlerMapping(AbilityKey abilityKey) {
|
||||
return super.handlerMapping().get(abilityKey);
|
||||
}
|
||||
|
||||
@JustForTest
|
||||
public int clusterHandlerMappingCount() {
|
||||
return super.clusterHandlerMapping().size();
|
||||
public Set<String> getServerNotSupportAbility() {
|
||||
return super.serverNotSupport();
|
||||
}
|
||||
|
||||
@JustForTest
|
||||
public List<HandlerWithPriority> getClusterHandlerMapping(AbilityKey abilityKey) {
|
||||
return super.clusterHandlerMapping().get(abilityKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Just a test method.
|
||||
*/
|
||||
@JustForTest
|
||||
public void registerClusterHandlerMapping(AbilityKey key, HandlerMapping handlerMapping, int priority) {
|
||||
List<HandlerWithPriority> orDefault = super.clusterHandlerMapping().getOrDefault(key, new ArrayList<>());
|
||||
orDefault.add(new HandlerWithPriority(handlerMapping, priority));
|
||||
clusterHandlerMapping().put(key, orDefault);
|
||||
}
|
||||
|
||||
@JustForTest
|
||||
public void triggerCluster(AbilityKey abilityKey) {
|
||||
triggerHandlerMappingAsyn(abilityKey, true, clusterHandlerMapping());
|
||||
}
|
||||
|
||||
@JustForTest
|
||||
public void trigger(AbilityKey abilityKey) {
|
||||
triggerHandlerMappingAsyn(abilityKey, true, handlerMapping());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user