From 5ec1d1f8b59727f350e0fa069220a190cd07d43e Mon Sep 17 00:00:00 2001 From: Daydreamer-ia <2296032269@qq.com> Date: Tue, 20 Sep 2022 15:24:46 +0800 Subject: [PATCH] Change the format of ability table key to string, less time for format conversion when saving. --- .../api/ability/constant/AbilityKey.java | 2 +- .../nacos/api/remote/request/RequestMeta.java | 8 +-- .../ability/ClientAbilityControlManager.java | 4 +- .../nacos/client/ability/AbilityTest.java | 4 +- .../AbstractAbilityControlManager.java | 54 ++++++++----------- .../common/remote/client/Connection.java | 8 +-- .../common/remote/client/grpc/GrpcClient.java | 34 +----------- .../control/ServerAbilityControlManager.java | 12 ++--- .../alibaba/nacos/core/remote/Connection.java | 7 ++- .../grpc/GrpcBiStreamRequestAcceptor.java | 4 +- .../core/remote/grpc/GrpcRequestAcceptor.java | 5 +- .../ability/AbilityControlManagerTest.java | 14 ++--- .../TestServerAbilityControlManager.java | 2 +- .../ability/config/AbilityConfigsTest.java | 6 +-- 14 files changed, 61 insertions(+), 103 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityKey.java b/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityKey.java index d2e71e126..d78de35d3 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityKey.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityKey.java @@ -25,7 +25,7 @@ import java.util.stream.Collectors; /**. * @author Daydreamer - * @description Ability key constant. + * @description Ability key constant. It is used to constrain the ability key. * @date 2022/8/31 12:27 **/ public enum AbilityKey { diff --git a/api/src/main/java/com/alibaba/nacos/api/remote/request/RequestMeta.java b/api/src/main/java/com/alibaba/nacos/api/remote/request/RequestMeta.java index c212ebe33..c633f8934 100644 --- a/api/src/main/java/com/alibaba/nacos/api/remote/request/RequestMeta.java +++ b/api/src/main/java/com/alibaba/nacos/api/remote/request/RequestMeta.java @@ -38,13 +38,13 @@ public class RequestMeta { private Map labels = new HashMap<>(); - private Map abilityTable; + private Map abilityTable; public AbilityStatus getConnectionAbility(AbilityKey abilityKey) { - if (abilityTable == null) { + if (abilityTable == null || !abilityTable.containsKey(abilityKey.getName())) { return AbilityStatus.UNKNOWN; } - return abilityTable.getOrDefault(abilityKey, false) ? AbilityStatus.SUPPORTED : AbilityStatus.NOT_SUPPORTED; + return abilityTable.get(abilityKey.getName()) ? AbilityStatus.SUPPORTED : AbilityStatus.NOT_SUPPORTED; } /** @@ -52,7 +52,7 @@ public class RequestMeta { * * @param abilityTable property value of clientVersion */ - public void setAbilityTable(Map abilityTable) { + public void setAbilityTable(Map abilityTable) { this.abilityTable = abilityTable; } diff --git a/client/src/main/java/com/alibaba/nacos/client/ability/ClientAbilityControlManager.java b/client/src/main/java/com/alibaba/nacos/client/ability/ClientAbilityControlManager.java index f1cb6cf7d..af537a60b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/ability/ClientAbilityControlManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/ability/ClientAbilityControlManager.java @@ -33,8 +33,8 @@ public class ClientAbilityControlManager extends AbstractAbilityControlManager { } @Override - protected Map initCurrentNodeAbilities() { - return ClientAbilities.getStaticAbilities(); + protected Map initCurrentNodeAbilities() { + return AbilityKey.mapStr(ClientAbilities.getStaticAbilities()); } @Override diff --git a/client/src/test/java/com/alibaba/nacos/client/ability/AbilityTest.java b/client/src/test/java/com/alibaba/nacos/client/ability/AbilityTest.java index f5f966f6e..a11850a27 100644 --- a/client/src/test/java/com/alibaba/nacos/client/ability/AbilityTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/ability/AbilityTest.java @@ -61,8 +61,8 @@ public class AbilityTest { { super.abilityTable = new HashMap<>(); - super.abilityTable.put(AbilityKey.TEST_1, true); - super.abilityTable.put(AbilityKey.TEST_2, false); + super.abilityTable.put(AbilityKey.TEST_1.getName(), true); + super.abilityTable.put(AbilityKey.TEST_2.getName(), false); } @Override diff --git a/common/src/main/java/com/alibaba/nacos/common/ability/AbstractAbilityControlManager.java b/common/src/main/java/com/alibaba/nacos/common/ability/AbstractAbilityControlManager.java index ca8c028d2..a501145cb 100644 --- a/common/src/main/java/com/alibaba/nacos/common/ability/AbstractAbilityControlManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/ability/AbstractAbilityControlManager.java @@ -65,7 +65,7 @@ public abstract class AbstractAbilityControlManager { /**. * ability current node running */ - protected final Map currentRunningAbility = new ConcurrentHashMap<>(); + protected final Map currentRunningAbility = new ConcurrentHashMap<>(); private final ReentrantLock lockForHandlerMappings = new ReentrantLock(); @@ -75,18 +75,18 @@ public abstract class AbstractAbilityControlManager { currentRunningAbility.putAll(initCurrentNodeAbilities()); } - /** - * . Turn on the ability whose key is

abilityKey

+ /**. + * Turn on the ability whose key is

abilityKey

* - * @param abilityKey ability key + * @param abilityKey ability key{@link AbilityKey} * @return if turn success */ public boolean enableCurrentNodeAbility(AbilityKey abilityKey) { return doTurn(true, abilityKey); } - /** - * . Turn off the ability whose key is

abilityKey

+ /**. + * Turn off the ability whose key is

abilityKey

{@link AbilityKey} * * @param abilityKey ability key * @return if turn success @@ -95,8 +95,14 @@ public abstract class AbstractAbilityControlManager { return doTurn(false, abilityKey); } + /**. + * Whether current node support + * + * @param abilityKey ability key from {@link AbilityKey} + * @return whether support + */ public boolean isCurrentNodeAbilityRunning(AbilityKey abilityKey) { - return currentRunningAbility.getOrDefault(abilityKey, false); + return currentRunningAbility.getOrDefault(abilityKey.getName(), false); } /**. @@ -104,14 +110,14 @@ public abstract class AbstractAbilityControlManager { * * @return current node abilities */ - protected abstract Map initCurrentNodeAbilities(); + protected abstract Map initCurrentNodeAbilities(); /**. * Return the abilities current node * * @return current abilities */ - public Map getCurrentNodeAbilities() { + public Map getCurrentNodeAbilities() { return Collections.unmodifiableMap(currentRunningAbility); } @@ -123,7 +129,7 @@ public abstract class AbstractAbilityControlManager { * @return if turn success */ private boolean doTurn(boolean isOn, AbilityKey abilityKey) { - Boolean isEnabled = currentRunningAbility.get(abilityKey); + Boolean isEnabled = currentRunningAbility.get(abilityKey.getName()); // if not supporting this key if (isEnabled == null) { LOGGER.warn("[AbilityControlManager] Attempt to turn on/off a not existed ability!"); @@ -133,7 +139,7 @@ public abstract class AbstractAbilityControlManager { return true; } // turn on/off - currentRunningAbility.put(abilityKey, isOn); + currentRunningAbility.put(abilityKey.getName(), isOn); // handler mappings triggerHandlerMappingAsyn(abilityKey, isOn, this.handlerMappings); // notify event @@ -192,20 +198,6 @@ public abstract class AbstractAbilityControlManager { LOGGER.warn("[DefaultAbilityControlManager] - Destruction of the end"); } - /**. - * Combine with current node abilities, in order to get abilities current node provides - * - * @param abilities combined ability table - */ - public void combine(Map abilities) { - currentRunningAbility.forEach((k, v) -> { - Boolean isCurrentSupport = currentRunningAbility.get(k); - if (isCurrentSupport != null) { - abilities.put(k, abilities.getOrDefault(k, false) && isCurrentSupport); - } - }); - } - /**. * hook for subclass */ @@ -260,8 +252,8 @@ public abstract class AbstractAbilityControlManager { */ protected void doRegisterComponent(AbilityKey abilityKey, HandlerMapping handlerMapping, Map> handlerMappings, Lock lockForHandlerMappings, - int priority, Map abilityTable) { - if (!currentRunningAbility.containsKey(abilityKey)) { + int priority, Map abilityTable) { + if (!currentRunningAbility.containsKey(abilityKey.getName())) { LOGGER.warn("[AbilityHandlePostProcessor] Failed to register processor: {}, because illegal key!", handlerMapping.getClass().getSimpleName()); } @@ -275,7 +267,7 @@ public abstract class AbstractAbilityControlManager { handlerMappings.put(abilityKey, handlers); // choose behavior // enable default - if (abilityTable.getOrDefault(abilityKey, false)) { + if (abilityTable.getOrDefault(abilityKey.getName(), false)) { handlerMapping.enable(); } else { handlerMapping.disable(); @@ -384,15 +376,15 @@ public abstract class AbstractAbilityControlManager { private boolean isOn; - private Map table; + private Map table; private AbilityUpdateEvent(){} - public Map getAbilityTable() { + public Map getAbilityTable() { return table; } - public void setTable(Map abilityTable) { + public void setTable(Map abilityTable) { this.table = abilityTable; } diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/Connection.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/Connection.java index a7e37199c..b8ea5a836 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/Connection.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/Connection.java @@ -37,7 +37,7 @@ public abstract class Connection implements Requester { protected RpcClient.ServerInfo serverInfo; - protected Map abilityTable; + protected Map abilityTable; public Connection(RpcClient.ServerInfo serverInfo) { this.serverInfo = serverInfo; @@ -52,13 +52,13 @@ public abstract class Connection implements Requester { } public AbilityStatus getConnectionAbility(AbilityKey abilityKey) { - if (abilityTable == null) { + if (abilityTable == null || !abilityTable.containsKey(abilityKey.getName())) { return AbilityStatus.UNKNOWN; } - return abilityTable.getOrDefault(abilityKey, false) ? AbilityStatus.SUPPORTED : AbilityStatus.NOT_SUPPORTED; + return abilityTable.get(abilityKey.getName()) ? AbilityStatus.SUPPORTED : AbilityStatus.NOT_SUPPORTED; } - public void setAbilityTable(Map abilityTable) { + public void setAbilityTable(Map abilityTable) { this.abilityTable = abilityTable; } diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClient.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClient.java index 5026d858d..4c8f94e3e 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClient.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClient.java @@ -356,14 +356,10 @@ public abstract class GrpcClient extends RpcClient { grpcConn.setConnectionId(connectionId); // if not supported, it will be null if (serverCheckResponse.getAbilities() != null) { - Map abilityTable = mapAndFilter(serverCheckResponse.getAbilities()); // mark markForSetup.put(serverCheckResponse.getConnectionId(), new CountDownLatch(1)); - // combine with current node abilities - // in order to get abilities current node provides - NacosAbilityManagerHolder.getInstance().combine(abilityTable); // set server abilities to connection - grpcConn.setAbilityTable(abilityTable); + grpcConn.setAbilityTable(serverCheckResponse.getAbilities()); } //create stream request and bind connection event to this connection. @@ -378,7 +374,7 @@ public abstract class GrpcClient extends RpcClient { conSetupRequest.setClientVersion(VersionUtils.getFullClientVersion()); conSetupRequest.setLabels(super.getLabels()); // set ability table - conSetupRequest.setAbilityTable(serverCheckResponse.getAbilities()); + conSetupRequest.setAbilityTable(NacosAbilityManagerHolder.getInstance().getCurrentNodeAbilities()); conSetupRequest.setTenant(super.getTenant()); grpcConn.sendRequest(conSetupRequest); // wait for response @@ -404,32 +400,6 @@ public abstract class GrpcClient extends RpcClient { return null; } - /**. - * filter the ability current node not support, map to enum - * - * @param originClientAbilities origin client abilities - * @return enum map - */ - private Map mapAndFilter(Map originClientAbilities) { - Map res = new HashMap<>(originClientAbilities.size()); - Iterator> iterator = originClientAbilities.entrySet().iterator(); - - // filter ability current node not support - AbstractAbilityControlManager instance = NacosAbilityManagerHolder.getInstance(); - while (iterator.hasNext()) { - Map.Entry next = iterator.next(); - AbilityKey anEnum = AbilityKey.getEnum(next.getKey()); - if (anEnum != null) { - // whether support - boolean isRunning = instance.isCurrentNodeAbilityRunning(anEnum) && next.getValue(); - res.put(anEnum, isRunning); - // if not support - originClientAbilities.replace(next.getKey(), isRunning); - } - } - return res; - } - @Override protected void afterReset(ConnectResetRequest request) { String connectionId = request.getConnectionId(); diff --git a/core/src/main/java/com/alibaba/nacos/core/ability/control/ServerAbilityControlManager.java b/core/src/main/java/com/alibaba/nacos/core/ability/control/ServerAbilityControlManager.java index 67845d821..b42eaf9c8 100644 --- a/core/src/main/java/com/alibaba/nacos/core/ability/control/ServerAbilityControlManager.java +++ b/core/src/main/java/com/alibaba/nacos/core/ability/control/ServerAbilityControlManager.java @@ -38,16 +38,16 @@ public class ServerAbilityControlManager extends AbstractAbilityControlManager { } @Override - protected Map initCurrentNodeAbilities() { + protected Map initCurrentNodeAbilities() { // static abilities - Map staticAbilities = ServerAbilities.getStaticAbilities(); + Map staticAbilities = AbilityKey.mapStr(ServerAbilities.getStaticAbilities()); // all function server can support - Set abilityKeys = staticAbilities.keySet(); - Map abilityTable = new HashMap<>(abilityKeys.size()); + Set abilityKeys = staticAbilities.keySet(); + Map abilityTable = new HashMap<>(abilityKeys.size()); // if not define in config, then load from ServerAbilities - Set unIncludedInConfig = new HashSet<>(); + Set unIncludedInConfig = new HashSet<>(); abilityKeys.forEach(abilityKey -> { - String key = AbilityConfigs.PREFIX + abilityKey.getName(); + String key = AbilityConfigs.PREFIX + abilityKey; try { Boolean property = EnvUtil.getProperty(key, Boolean.class); // if not null diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/Connection.java b/core/src/main/java/com/alibaba/nacos/core/remote/Connection.java index 6f49a3ca7..ad1e4254e 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/Connection.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/Connection.java @@ -17,7 +17,6 @@ package com.alibaba.nacos.core.remote; import com.alibaba.nacos.api.ability.ClientAbilities; -import com.alibaba.nacos.api.ability.constant.AbilityKey; import com.alibaba.nacos.api.remote.Requester; import java.util.Map; @@ -33,7 +32,7 @@ public abstract class Connection implements Requester { private boolean traced = false; - private Map abilityTable; + private Map abilityTable; private ClientAbilities abilities; @@ -55,11 +54,11 @@ public abstract class Connection implements Requester { this.traced = traced; } - public void setAbilityTable(Map abilityTable) { + public void setAbilityTable(Map abilityTable) { this.abilityTable = abilityTable; } - public Map getAbilityTable() { + public Map getAbilityTable() { return this.abilityTable; } diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcBiStreamRequestAcceptor.java b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcBiStreamRequestAcceptor.java index dcb01113d..12440975d 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcBiStreamRequestAcceptor.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcBiStreamRequestAcceptor.java @@ -126,9 +126,7 @@ public class GrpcBiStreamRequestAcceptor extends BiRequestStreamGrpc.BiRequestSt // null if supported if (setUpRequest.getAbilityTable() != null) { // map to table - Map clientAbilities = AbilityKey - .mapEnum(setUpRequest.getAbilityTable()); - connection.setAbilityTable(clientAbilities); + connection.setAbilityTable(setUpRequest.getAbilityTable()); } boolean rejectSdkOnStarting = metaInfo.isSdkSource() && !ApplicationUtils.isStarted(); diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcRequestAcceptor.java b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcRequestAcceptor.java index eb69ab86f..8fd7e1b58 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcRequestAcceptor.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcRequestAcceptor.java @@ -16,7 +16,6 @@ package com.alibaba.nacos.core.remote.grpc; -import com.alibaba.nacos.api.ability.constant.AbilityKey; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.grpc.auto.Payload; import com.alibaba.nacos.api.grpc.auto.RequestGrpc; @@ -91,8 +90,8 @@ public class GrpcRequestAcceptor extends RequestGrpc.RequestImplBase { // server check. if (ServerCheckRequest.class.getSimpleName().equals(type)) { Payload serverCheckResponseP = GrpcUtils.convert(new ServerCheckResponse(CONTEXT_KEY_CONN_ID.get(), - // to str map - AbilityKey.mapStr(NacosAbilityManagerHolder.getInstance().getCurrentNodeAbilities()))); + // abilities current node supporting + NacosAbilityManagerHolder.getInstance().getCurrentNodeAbilities())); traceIfNecessary(serverCheckResponseP, false); responseObserver.onNext(serverCheckResponseP); responseObserver.onCompleted(); diff --git a/core/src/test/java/com/alibaba/nacos/core/ability/AbilityControlManagerTest.java b/core/src/test/java/com/alibaba/nacos/core/ability/AbilityControlManagerTest.java index 26483920b..44180a7aa 100644 --- a/core/src/test/java/com/alibaba/nacos/core/ability/AbilityControlManagerTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/ability/AbilityControlManagerTest.java @@ -39,8 +39,8 @@ public class AbilityControlManagerTest { @Before public void inject() { - Map newTable = new HashMap<>(); - newTable.put(AbilityKey.TEST_1, true); + Map newTable = new HashMap<>(); + newTable.put(AbilityKey.TEST_1.getName(), true); serverAbilityControlManager.setCurrentSupportingAbility(newTable); } @@ -88,18 +88,18 @@ public class AbilityControlManagerTest { @Test public void testCurrentNodeAbility() { - Set keySet = serverAbilityControlManager.getCurrentNodeAbilities().keySet(); + Set keySet = serverAbilityControlManager.getCurrentNodeAbilities().keySet(); // diable all - keySet.forEach(key -> serverAbilityControlManager.disableCurrentNodeAbility(key)); + keySet.forEach(key -> serverAbilityControlManager.disableCurrentNodeAbility(AbilityKey.getEnum(key))); // get all keySet.forEach(key -> { - Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(key)); + Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.getEnum(key))); }); // enable all - keySet.forEach(key -> serverAbilityControlManager.enableCurrentNodeAbility(key)); + keySet.forEach(key -> serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.getEnum(key))); // get all keySet.forEach(key -> { - Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(key)); + Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.getEnum(key))); }); } diff --git a/core/src/test/java/com/alibaba/nacos/core/ability/TestServerAbilityControlManager.java b/core/src/test/java/com/alibaba/nacos/core/ability/TestServerAbilityControlManager.java index e9bb30ff8..a3685bd79 100644 --- a/core/src/test/java/com/alibaba/nacos/core/ability/TestServerAbilityControlManager.java +++ b/core/src/test/java/com/alibaba/nacos/core/ability/TestServerAbilityControlManager.java @@ -26,7 +26,7 @@ import java.util.Map; public class TestServerAbilityControlManager extends ServerAbilityControlManager { @JustForTest - public void setCurrentSupportingAbility(Map ability) { + public void setCurrentSupportingAbility(Map ability) { currentRunningAbility.clear(); currentRunningAbility.putAll(ability); } diff --git a/core/src/test/java/com/alibaba/nacos/core/ability/config/AbilityConfigsTest.java b/core/src/test/java/com/alibaba/nacos/core/ability/config/AbilityConfigsTest.java index 3a5ba7d6c..86e081f44 100644 --- a/core/src/test/java/com/alibaba/nacos/core/ability/config/AbilityConfigsTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/ability/config/AbilityConfigsTest.java @@ -60,9 +60,9 @@ public class AbilityConfigsTest { void inject(AbilityConfigs abilityConfigs) { TestServerAbilityControlManager serverAbilityControlManager = new TestServerAbilityControlManager(); - Map newTable = new HashMap<>(); - newTable.put(AbilityKey.TEST_1, true); - newTable.put(AbilityKey.TEST_2, true); + Map newTable = new HashMap<>(); + newTable.put(AbilityKey.TEST_1.getName(), true); + newTable.put(AbilityKey.TEST_2.getName(), true); serverAbilityControlManager.setCurrentSupportingAbility(newTable); abilityConfigs.setAbilityHandlerRegistry(serverAbilityControlManager); this.serverAbilityControlManager = serverAbilityControlManager;