From 92add6e833d38bac1b0453b081b8f90230911866 Mon Sep 17 00:00:00 2001 From: Daydreamer-ia <2296032269@qq.com> Date: Wed, 31 Aug 2022 16:59:43 +0800 Subject: [PATCH 1/6] Separate client capability and server capability: ClientAbilities and ServerAbilities. --- .../api/ability/constant/AbilityKey.java | 94 ++---------------- .../api/ability/entity/AbilityTable.java | 11 ++- .../ability/register/AbilityBitOperate.java | 61 ++++++++++++ .../register/impl/ClientAbilities.java | 72 ++++++++++++++ .../register/impl/ServerAbilities.java | 73 ++++++++++++++ .../request/ConnectionSetupRequest.java | 13 +++ .../nacos/api/utils/AbilityTableUtils.java | 13 +-- .../api/utils/AbilityTableUtilsTest.java | 56 ----------- .../ability/ClientAbilityControlManager.java | 11 ++- .../client/config/impl/ClientWorker.java | 7 +- .../AbstractAbilityControlManager.java | 18 +++- .../ability/DefaultAbilityControlManager.java | 51 +++++----- .../ability/inter/AbilityControlManager.java | 9 +- .../ability/inter/AbilityHandlerRegistry.java | 22 +++-- .../nacos/common/remote/client/RpcClient.java | 12 --- .../remote/client/RpcClientFactory.java | 3 +- .../common/remote/client/grpc/GrpcClient.java | 24 ++++- .../remote/client/grpc/GrpcClusterClient.java | 11 +++ .../remote/client/grpc/GrpcSdkClient.java | 11 +++ .../remote/client/grpc/GrpcClientTest.java | 10 ++ .../control/ServerAbilityControlManager.java | 42 ++++---- .../inte/ClusterAbilityControlSupport.java | 18 ++-- .../alibaba/nacos/core/remote/Connection.java | 7 +- .../grpc/GrpcBiStreamRequestAcceptor.java | 14 ++- .../core/remote/grpc/GrpcRequestAcceptor.java | 4 +- .../ability/AbilityControlManagerTest.java | 97 ++++++++++--------- .../TestClientAbilityControlManager.java | 3 +- .../TestServerAbilityControlManager.java | 15 +-- 28 files changed, 472 insertions(+), 310 deletions(-) create mode 100644 api/src/main/java/com/alibaba/nacos/api/ability/register/AbilityBitOperate.java create mode 100644 api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java create mode 100644 api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java delete mode 100644 api/src/test/java/com/alibaba/nacos/api/utils/AbilityTableUtilsTest.java 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 cf194ccca..71e1e1435 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 @@ -16,96 +16,16 @@ package com.alibaba.nacos.api.ability.constant; -import com.alibaba.nacos.api.utils.AbilityTableUtils; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - /**. * @author Daydreamer - * @description Ability table key. It can be a replacement of {@link com.alibaba.nacos.api.ability.ServerAbilities} - * and {@link com.alibaba.nacos.api.ability.ClientAbilities}. - * @date 2022/7/12 19:23 + * @description Ability key constant. + * @date 2022/8/31 12:27 **/ -@SuppressWarnings("unchecked") -public class AbilityKey { - - private static final HashMap CURRENT_SERVER_SUPPORT_ABILITY = new HashMap<>(); - - private static final HashMap CURRENT_SERVER_ABILITY_OFFSET = new HashMap<>(); - - private static final byte[] ABILITY_BIT_FLAGS; - - private AbilityKey() { - } +public enum AbilityKey { - static { - /* - * example: - * There is a function named "compression". - * The key is "compression", the value is the offset of the flag bit of this ability in the ability table. The value should be unique. - * - * You can add a new public static field like: - * public static final String COMPRESSION = "compression"; - * This field can be used outside. - * - * And then you need to declare the offset of the flag bit of this ability in the ability table, you can: - * CURRENT_SERVER_ABILITY_OFFSET.put("compression", 1); means that is the first bit from left to right in the table. - * - */ - - // put ability here, which you want current server supports - - } + /** + * just for junit test + */ + TEST_1, TEST_2, - /**. - * Return ability table of current node - * But this ability is static which means that this ability table is all function this node supports if no one to ask it to close some functions. - * If you want to get what function current node is supporting, you should call AbilityControlManager#getCurrentAbility - * By the way, AbilityControlManager is singleton, you can get it by static method - * - * @return ability table - */ - public static Map getCurrentNodeSupportAbility() { - return Collections.unmodifiableMap(CURRENT_SERVER_SUPPORT_ABILITY); - } - - /**. - * Return the static ability bit table - * - * @return ability bit table - */ - public static byte[] getAbilityBitFlags() { - return ABILITY_BIT_FLAGS.clone(); - } - - /**. - * Is it a legal key - * - * @param key input - * @return whether a legal key - */ - public static boolean isLegal(String key) { - return CURRENT_SERVER_SUPPORT_ABILITY.containsKey(key); - } - - static { - // init the bits table - ABILITY_BIT_FLAGS = AbilityTableUtils.getAbilityBitBy(CURRENT_SERVER_ABILITY_OFFSET.values()); - // init the ability table, default all true - CURRENT_SERVER_ABILITY_OFFSET.forEach((k, v) -> { - CURRENT_SERVER_SUPPORT_ABILITY.put(k, Boolean.TRUE); - }); - } - - /**. - * Return the ability bit offsets - * - * @return bit offset - */ - public static Map offset() { - return CURRENT_SERVER_ABILITY_OFFSET; - } - } diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/entity/AbilityTable.java b/api/src/main/java/com/alibaba/nacos/api/ability/entity/AbilityTable.java index 921a092a7..67eb663dd 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/entity/AbilityTable.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/entity/AbilityTable.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.api.ability.entity; import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import java.util.Map; @@ -34,10 +35,10 @@ public class AbilityTable implements Cloneable { /**. * ability table - * key: name from {@link AbilityKey} + * key: name from {@link com.alibaba.nacos.api.ability.constant.AbilityKey} * value: whether to turn on */ - private Map ability; + private Map ability; /**. * whether it from a server node @@ -70,7 +71,7 @@ public class AbilityTable implements Cloneable { return this; } - public AbilityTable(String connectionId, Map ability, boolean isServer, String version) { + public AbilityTable(String connectionId, Map ability, boolean isServer, String version) { this.connectionId = connectionId; this.ability = ability; this.isServer = isServer; @@ -86,11 +87,11 @@ public class AbilityTable implements Cloneable { return this; } - public Map getAbility() { + public Map getAbility() { return ability; } - public AbilityTable setAbility(Map ability) { + public AbilityTable setAbility(Map ability) { this.ability = ability; return this; } diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/register/AbilityBitOperate.java b/api/src/main/java/com/alibaba/nacos/api/ability/register/AbilityBitOperate.java new file mode 100644 index 000000000..e16165e74 --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/ability/register/AbilityBitOperate.java @@ -0,0 +1,61 @@ +/* + * Copyright 1999-2022 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.api.ability.register; + +import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.utils.AbilityTableUtils; + +import java.util.HashMap; +import java.util.Map; + +/**. + * @author Daydreamer + * @description Operation for bit table. + * @date 2022/7/12 19:23 + **/ +public abstract class AbilityBitOperate { + + protected final HashMap abilityOffset = new HashMap<>(); + + private byte[] abilityBitFlag; + + /**. + * Return the static ability bit table + * + * @return ability bit table + */ + public byte[] getAbilityBitFlags() { + return abilityBitFlag.clone(); + } + + /**. + * Return the ability bit offsets + * + * @return bit offset + */ + public Map offset() { + return abilityOffset; + } + + /** + * put the bit offset to {@link AbilityBitOperate#abilityBitFlag} + */ + protected void init() { + // init the bits table + abilityBitFlag = AbilityTableUtils.getAbilityBitBy(abilityOffset.values()); + } +} diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java new file mode 100644 index 000000000..ba1a7496d --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java @@ -0,0 +1,72 @@ +/* + * Copyright 1999-2022 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.api.ability.register.impl; + +import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.AbilityBitOperate; + +import java.util.Map; + +/**. + * @author Daydreamer + * @description It is used to register client abilities. + * @date 2022/8/31 12:32 + **/ +public class ClientAbilities extends AbilityBitOperate { + + private static final ClientAbilities INSTANCE = new ClientAbilities(); + + { + /* + * example: + * There is a function named "compression". + * The key is "compression", the value is the offset of the flag bit of this ability in the ability table. The value should be unique. + * + * You can add a new public static field in

AbilityKeyConstant

like: + * public static final String COMPRESSION = "compression"; + * This field can be used outside. + * + * And then you need to declare the offset of the flag bit of this ability in the ability table, you can: + * abilityOffset.put("compression", 1); means that is the first bit from left to right in the table. + * + */ + // put ability here, which you want current server supports + } + + private ClientAbilities() { + // put key to bit offset + init(); + } + + /** + * get the ability offset for server + * + * @return ability offset + */ + public static byte[] getBitFlags() { + return INSTANCE.getAbilityBitFlags(); + } + + /** + * get the ability offset for server + * + * @return ability offset + */ + public static Map getOffset() { + return INSTANCE.offset(); + } +} diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java new file mode 100644 index 000000000..842591587 --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java @@ -0,0 +1,73 @@ +/* + * Copyright 1999-2022 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.api.ability.register.impl; + +import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.AbilityBitOperate; + +import java.util.Map; + +/**. + * @author Daydreamer + * @description It is used to register server abilities. + * @date 2022/8/31 12:32 + **/ +public class ServerAbilities extends AbilityBitOperate { + + private static final ServerAbilities INSTANCE = new ServerAbilities(); + + { + /* + * example: + * There is a function named "compression". + * The key is "compression", the value is the offset of the flag bit of this ability in the ability table. The value should be unique. + * + * You can add a new public static field like: + * public static final String COMPRESSION = "compression"; + * This field can be used outside. + * + * And then you need to declare the offset of the flag bit of this ability in the ability table, you can: + * CURRENT_SERVER_ABILITY_OFFSET.put("compression", 1); means that is the first bit from left to right in the table. + * + */ + // put ability here, which you want current server supports + } + + private ServerAbilities() { + // put key to bit offset + init(); + } + + /** + * get bit table + * + * @return ability offset + */ + public static byte[] getBitFlags() { + return INSTANCE.getAbilityBitFlags(); + } + + /** + * get the ability offset for server + * + * @return ability offset + */ + public static Map getOffset() { + return INSTANCE.offset(); + } + +} diff --git a/api/src/main/java/com/alibaba/nacos/api/remote/request/ConnectionSetupRequest.java b/api/src/main/java/com/alibaba/nacos/api/remote/request/ConnectionSetupRequest.java index 046a897a4..d2fbd704a 100644 --- a/api/src/main/java/com/alibaba/nacos/api/remote/request/ConnectionSetupRequest.java +++ b/api/src/main/java/com/alibaba/nacos/api/remote/request/ConnectionSetupRequest.java @@ -35,6 +35,19 @@ public class ConnectionSetupRequest extends InternalRequest { private byte[] abilityTable; + /** + * server will resolve {@link ConnectionSetupRequest#abilityTable} to server abilities, or to client abilities + */ + private boolean isServer; + + public boolean isServer() { + return isServer; + } + + public void setServer(boolean server) { + isServer = server; + } + public ConnectionSetupRequest() { } diff --git a/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java b/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java index 11ad28cc0..aa74be70d 100644 --- a/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java +++ b/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.api.utils; import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import java.util.Collection; import java.util.Collections; @@ -67,18 +68,18 @@ public class AbilityTableUtils { * get ability table by bits * * @param bits bit flag - * @param offsetMap offset from {@link AbilityKey} + * @param offsetMap offset from {@link AbilityBitOperate} * @return Return the Map containing AbilityTableKey and isRunning. */ - public static Map getAbilityTableBy(byte[] bits, Map offsetMap) { + public static Map getAbilityTableBy(byte[] bits, Map offsetMap) { if (bits == null || offsetMap.size() == 0) { return Collections.emptyMap(); } int length = bits.length; - Set> entries = offsetMap.entrySet(); - Map res = new HashMap<>(offsetMap.size()); - for (Map.Entry entry : entries) { - String abilityKey = entry.getKey(); + Set> entries = offsetMap.entrySet(); + Map res = new HashMap<>(offsetMap.size()); + for (Map.Entry entry : entries) { + AbilityKey abilityKey = entry.getKey(); Integer offset = entry.getValue(); // if not exists int index = offset / 8 + (offset % 8 == 0 ? -1 : 0); diff --git a/api/src/test/java/com/alibaba/nacos/api/utils/AbilityTableUtilsTest.java b/api/src/test/java/com/alibaba/nacos/api/utils/AbilityTableUtilsTest.java deleted file mode 100644 index c2fea2c40..000000000 --- a/api/src/test/java/com/alibaba/nacos/api/utils/AbilityTableUtilsTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 1999-2022 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.api.utils; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.HashMap; -import java.util.Map; - -public class AbilityTableUtilsTest { - - @Test - public void testGetByteArray() { - Map offset = new HashMap<>(); - offset.put("a", 1); - offset.put("b", 2); - offset.put("c", 10); - offset.put("d", 127); - byte[] abilityBitBy = AbilityTableUtils.getAbilityBitBy(offset.values()); - Assert.assertEquals(16, abilityBitBy.length); - Assert.assertEquals((byte) (3 << 6), abilityBitBy[0]); - Assert.assertEquals((byte) (1 << 6), abilityBitBy[1]); - } - - @Test - public void testGetAbilityTable() { - Map offset = new HashMap<>(); - offset.put("a", 1); - offset.put("b", 2); - offset.put("c", 10); - offset.put("d", 127); - byte[] abilityBitBy = AbilityTableUtils.getAbilityBitBy(offset.values()); - Map abilityTableBy = AbilityTableUtils.getAbilityTableBy(abilityBitBy, offset); - Assert.assertEquals(4, abilityTableBy.size()); - Assert.assertEquals(Boolean.TRUE, abilityTableBy.get("a")); - Assert.assertEquals(Boolean.TRUE, abilityTableBy.get("b")); - Assert.assertEquals(Boolean.TRUE, abilityTableBy.get("c")); - Assert.assertEquals(Boolean.TRUE, abilityTableBy.get("d")); - Assert.assertEquals(Boolean.FALSE, abilityTableBy.getOrDefault("asdasd", false)); - } -} 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 9d39574f8..9e859b3b2 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 @@ -16,11 +16,15 @@ package com.alibaba.nacos.client.ability; +import com.alibaba.nacos.api.ability.constant.AbilityKey; import com.alibaba.nacos.api.ability.entity.AbilityTable; +import com.alibaba.nacos.api.ability.register.impl.ClientAbilities; +import com.alibaba.nacos.api.utils.AbilityTableUtils; import com.alibaba.nacos.common.ability.AbstractAbilityControlManager; import com.alibaba.nacos.common.ability.DefaultAbilityControlManager; import java.util.Collections; +import java.util.Map; import java.util.Optional; /**. @@ -34,7 +38,12 @@ public class ClientAbilityControlManager extends DefaultAbilityControlManager { } @Override - public boolean isSupport(String connectionId, String abilityKey) { + protected Map getCurrentNodeSupportAbility() { + return AbilityTableUtils.getAbilityTableBy(ClientAbilities.getBitFlags(), ClientAbilities.getOffset()); + } + + @Override + public boolean isSupport(String connectionId, AbilityKey abilityKey) { Boolean isRunning = currentRunningAbility.getOrDefault(abilityKey, false); if (!isRunning) { return false; diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index fc450b990..d56b23a7c 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.client.config.impl; import com.alibaba.nacos.api.PropertyKeyConst; -import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.config.ConfigType; import com.alibaba.nacos.api.config.listener.Listener; @@ -885,7 +885,6 @@ public class ClientWorker implements Closeable { if (rpcClient.isWaitInitiated()) { initRpcClientHandler(rpcClient); rpcClient.setTenant(getTenant()); - rpcClient.clientAbilities(initAbilities()); rpcClient.start(); } @@ -893,10 +892,6 @@ public class ClientWorker implements Closeable { } } - - private byte[] initAbilities() { - return AbilityKey.getAbilityBitFlags(); - } /** * build config string. 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 e1723a9e2..c72e6a9e2 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 @@ -17,6 +17,7 @@ package com.alibaba.nacos.common.ability; import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.common.ability.inter.AbilityControlManager; import com.alibaba.nacos.common.notify.Event; @@ -43,10 +44,10 @@ public abstract class AbstractAbilityControlManager implements AbilityControlMan /** * Abilities current supporting *

- * key: ability key from {@link AbilityKey} + * key: ability key from {@link AbilityBitOperate} * value: whether to turn on */ - protected final Map currentRunningAbility = new ConcurrentHashMap<>(); + protected final Map currentRunningAbility = new ConcurrentHashMap<>(); /** * Ability table collections @@ -64,10 +65,17 @@ public abstract class AbstractAbilityControlManager implements AbilityControlMan // register events registerAbilityEvent(); // put abilities - currentRunningAbility.putAll(AbilityKey.getCurrentNodeSupportAbility()); + currentRunningAbility.putAll(getCurrentNodeSupportAbility()); // initialize init(); } + + /** + * This is a hook for subclass to init current node ability + * + * @return current node ability + */ + protected abstract Map getCurrentNodeSupportAbility(); private void registerAbilityEvent(){ @@ -83,7 +91,7 @@ public abstract class AbstractAbilityControlManager implements AbilityControlMan * @return is running */ @Override - public boolean isCurrentNodeAbilityRunning(String abilityKey) { + public boolean isCurrentNodeAbilityRunning(AbilityKey abilityKey) { return currentRunningAbility.getOrDefault(abilityKey, false); } @@ -204,7 +212,7 @@ public abstract class AbstractAbilityControlManager implements AbilityControlMan * @return ability table */ @Override - public Map getCurrentRunningAbility() { + public Map getCurrentRunningAbility() { return new HashMap<>(this.currentRunningAbility); } diff --git a/common/src/main/java/com/alibaba/nacos/common/ability/DefaultAbilityControlManager.java b/common/src/main/java/com/alibaba/nacos/common/ability/DefaultAbilityControlManager.java index b3012392e..9d7dbfe36 100644 --- a/common/src/main/java/com/alibaba/nacos/common/ability/DefaultAbilityControlManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/ability/DefaultAbilityControlManager.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.common.ability; import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.common.JustForTest; import com.alibaba.nacos.common.ability.handler.HandlerMapping; @@ -52,10 +53,10 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro private static final Logger LOGGER = LoggerFactory.getLogger(DefaultAbilityControlManager.class); /** - * . These handlers will be invoked when the flag of ability change key: ability key from {@link AbilityKey} value: - * componments who want to be invoked if its interested ability turn on/off + * These handlers will be invoked when the flag of ability change key: ability key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey} value: + * components who want to be invoked if its interested ability turn on/off */ - private final Map> handlerMappings = new ConcurrentHashMap<>(); + private final Map> handlerMappings = new ConcurrentHashMap<>(); /**. * run for HandlerMapping @@ -76,7 +77,7 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro * @return if turn success */ @Override - public boolean enableCurrentNodeAbility(String abilityKey) { + public boolean enableCurrentNodeAbility(AbilityKey abilityKey) { return doTurn(true, abilityKey); } @@ -87,7 +88,7 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro * @return if turn success */ @Override - public boolean disableCurrentNodeAbility(String abilityKey) { + public boolean disableCurrentNodeAbility(AbilityKey abilityKey) { return doTurn(false, abilityKey); } @@ -98,7 +99,7 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro * @param abilityKey ability key from {@link AbilityKey} * @return if turn success */ - private boolean doTurn(boolean isOn, String abilityKey) { + private boolean doTurn(boolean isOn, AbilityKey abilityKey) { Boolean isEnabled = currentRunningAbility.get(abilityKey); // if not supporting this key if (isEnabled == null) { @@ -130,12 +131,12 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro * @param handlerMapping component instance. */ @Override - public void registerComponent(String abilityKey, HandlerMapping handlerMapping, int priority) { + public void registerComponent(AbilityKey abilityKey, HandlerMapping handlerMapping, int priority) { doRegisterComponent(abilityKey, handlerMapping, this.handlerMappings, lockForHandlerMappings, priority, currentRunningAbility); } @Override - public int removeComponent(String abilityKey, Class handlerMappingClazz) { + public int removeComponent(AbilityKey abilityKey, Class handlerMappingClazz) { return doRemove(abilityKey, handlerMappingClazz, lockForHandlerMappings, handlerMappings); } @@ -161,14 +162,14 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro /** * Remove the component instance of

handlerMappingClazz

. * - * @param abilityKey ability key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey} + * @param abilityKey ability key from {@link AbilityBitOperate} * @param handlerMappingClazz implement of {@link HandlerMapping} * @param lock lock for operation * @param handlerMappingsMap handler collection map * @return the count of components have removed */ - protected int doRemove(String abilityKey, Class handlerMappingClazz, Lock lock, - Map> handlerMappingsMap) { + protected int doRemove(AbilityKey abilityKey, Class handlerMappingClazz, Lock lock, + Map> handlerMappingsMap) { List handlerMappings = handlerMappingsMap.get(abilityKey); if (CollectionUtils.isEmpty(handlerMappings)) { return 0; @@ -190,7 +191,7 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro } @Override - public int removeAll(String abilityKey) { + public int removeAll(AbilityKey abilityKey) { List remove = this.handlerMappings.remove(abilityKey); return Optional.ofNullable(remove).orElse(Collections.emptyList()).size(); } @@ -204,9 +205,9 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro * @param lockForHandlerMappings lock to ensure concurrency * @param abilityTable behavioral basis of handler */ - protected void doRegisterComponent(String abilityKey, HandlerMapping handlerMapping, - Map> handlerMappings, Lock lockForHandlerMappings, - int priority, Map abilityTable) { + protected void doRegisterComponent(AbilityKey abilityKey, HandlerMapping handlerMapping, + Map> handlerMappings, Lock lockForHandlerMappings, + int priority, Map abilityTable) { if (!currentRunningAbility.containsKey(abilityKey)) { LOGGER.warn("[AbilityHandlePostProcessor] Failed to register processor: {}, because illegal key!", handlerMapping.getClass().getSimpleName()); @@ -239,24 +240,24 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro /** * Invoke componments which linked to ability key asyn. * - * @param key ability key from {@link AbilityKey} + * @param key ability key from {@link AbilityBitOperate} * @param isEnabled turn on/off * @param handlerMappingsMap handler collection */ - protected void triggerHandlerMappingAsyn(String key, boolean isEnabled, - Map> handlerMappingsMap) { + protected void triggerHandlerMappingAsyn(AbilityKey key, boolean isEnabled, + Map> handlerMappingsMap) { simpleThreadPool.execute(() -> doTriggerSyn(key, isEnabled, handlerMappingsMap)); } /** * Invoke componments which linked to ability key syn. * - * @param key ability key from {@link AbilityKey} + * @param key ability key from {@link AbilityBitOperate} * @param isEnabled turn on/off * @param handlerMappingsMap handler collection */ - protected void doTriggerSyn(String key, boolean isEnabled, - Map> handlerMappingsMap) { + protected void doTriggerSyn(AbilityKey key, boolean isEnabled, + Map> handlerMappingsMap) { List handlerWithPriorities = handlerMappingsMap.get(key); // return if empty if (CollectionUtils.isEmpty(handlerWithPriorities)) { @@ -281,7 +282,7 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro } @JustForTest - protected Map> handlerMapping() { + protected Map> handlerMapping() { return this.handlerMappings; } @@ -318,17 +319,17 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro private static final long serialVersionUID = -1232411212311111L; - private String abilityKey; + private AbilityKey abilityKey; private boolean isOn; private AbilityUpdateEvent(){} - public String getAbilityKey() { + public AbilityKey getAbilityKey() { return abilityKey; } - public void setAbilityKey(String abilityKey) { + public void setAbilityKey(AbilityKey abilityKey) { this.abilityKey = abilityKey; } diff --git a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java index 75aac8893..22bbfd85a 100644 --- a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.common.ability.inter; import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import com.alibaba.nacos.api.ability.entity.AbilityTable; import java.util.Map; @@ -32,10 +33,10 @@ public interface AbilityControlManager { * Whether the ability is supported for Connection. If the ability of current node is closed, it will return false. * * @param connectionId the connection range of ability table. - * @param abilityKey key name which comes from {@link AbilityKey}. + * @param abilityKey key name which comes from {@link AbilityBitOperate}. * @return whether the ability is supported in certain connection. */ - boolean isSupport(String connectionId, String abilityKey); + boolean isSupport(String connectionId, AbilityKey abilityKey); /** * Whether the ability current node supporting is running. Return false if current node doesn't support. @@ -43,7 +44,7 @@ public interface AbilityControlManager { * @param abilityKey ability key * @return is running */ - boolean isCurrentNodeAbilityRunning(String abilityKey); + boolean isCurrentNodeAbilityRunning(AbilityKey abilityKey); /** * Register a new ability table. @@ -72,7 +73,7 @@ public interface AbilityControlManager { * * @return ability table */ - Map getCurrentRunningAbility(); + Map getCurrentRunningAbility(); /**. * Initialize the manager diff --git a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityHandlerRegistry.java b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityHandlerRegistry.java index c27254784..77d2e847c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityHandlerRegistry.java +++ b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityHandlerRegistry.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.common.ability.inter; +import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import com.alibaba.nacos.common.ability.AbstractAbilityControlManager; import com.alibaba.nacos.common.ability.handler.HandlerMapping; @@ -32,7 +34,7 @@ public interface AbilityHandlerRegistry { * @param abilityKey ability key * @return if turn success */ - boolean enableCurrentNodeAbility(String abilityKey); + boolean enableCurrentNodeAbility(AbilityKey abilityKey); /**. * Turn off the ability whose key is

abilityKey

@@ -40,42 +42,42 @@ public interface AbilityHandlerRegistry { * @param abilityKey ability key * @return if turn success */ - boolean disableCurrentNodeAbility(String abilityKey); + boolean disableCurrentNodeAbility(AbilityKey abilityKey); /**. * Register the component which is managed by {@link AbstractAbilityControlManager}. * if you are hoping that a component will be invoked when turn on/off the ability whose key is

abilityKey

. * - * @param abilityKey component key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey} + * @param abilityKey component key from {@link AbilityBitOperate} * @param priority a positive number, the higher the priority is, the faster it will be called. `1` is the lowest priority. * @param handlerMapping component instance. */ - void registerComponent(String abilityKey, HandlerMapping handlerMapping, int priority); + void registerComponent(AbilityKey abilityKey, HandlerMapping handlerMapping, int priority); /**. * Default method to register component with the lowest priority. * - * @param abilityKey component key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey} + * @param abilityKey component key from {@link AbilityBitOperate} * @param handlerMapping component instance. */ - default void registerComponent(String abilityKey, HandlerMapping handlerMapping) { + default void registerComponent(AbilityKey abilityKey, HandlerMapping handlerMapping) { registerComponent(abilityKey, handlerMapping, 1); } /** * Remove the component instance of

handlerMappingClazz

. * - * @param abilityKey ability key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey} + * @param abilityKey ability key from {@link AbilityBitOperate} * @param handlerMappingClazz implement of {@link HandlerMapping} * @return the count of components have removed */ - int removeComponent(String abilityKey, Class handlerMappingClazz); + int removeComponent(AbilityKey abilityKey, Class handlerMappingClazz); /** * Remove all {@link HandlerMapping} interested in the special ability. - * @param abilityKey abnility key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey} + * @param abilityKey abnility key from {@link AbilityBitOperate} * @return the count of components have removed */ - int removeAll(String abilityKey); + int removeAll(AbilityKey abilityKey); } diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClient.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClient.java index 9db2e1e84..59d8134ec 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClient.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClient.java @@ -90,8 +90,6 @@ public abstract class RpcClient implements Closeable { private static final long DEFAULT_TIMEOUT_MILLS = 3000L; - protected byte[] clientAbilities; - /** * default keep alive time 5s. */ @@ -133,16 +131,6 @@ public abstract class RpcClient implements Closeable { } } - /** - * init client abilities. - * - * @param clientAbilities clientAbilities. - */ - public RpcClient clientAbilities(byte[] clientAbilities) { - this.clientAbilities = clientAbilities; - return this; - } - /** * init server list factory. only can init once. * diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClientFactory.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClientFactory.java index 8b40c9cfb..8293f0192 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClientFactory.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClientFactory.java @@ -16,7 +16,7 @@ package com.alibaba.nacos.common.remote.client; -import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.impl.ServerAbilities; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.common.remote.ConnectionType; import com.alibaba.nacos.common.remote.client.grpc.GrpcClient; @@ -140,7 +140,6 @@ public class RpcClientFactory { client.setThreadPoolCoreSize(threadPoolCoreSize); client.setThreadPoolMaxSize(threadPoolMaxSize); client.labels(labels); - client.clientAbilities(AbilityKey.getAbilityBitFlags()); return client; }); } 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 a68c81e58..d4c61452e 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 @@ -17,7 +17,9 @@ package com.alibaba.nacos.common.remote.client.grpc; import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.AbilityBitOperate; 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; @@ -317,8 +319,8 @@ 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 abilityTable = AbilityTableUtils - .getAbilityTableBy(serverCheckResponse.getAbilities(), AbilityKey.offset()); + Map abilityTable = AbilityTableUtils + .getAbilityTableBy(serverCheckResponse.getAbilities(), ServerAbilities.getOffset()); AbilityTable table = new AbilityTable(); table.setServer(true) .setConnectionId(serverCheckResponse.getConnectionId()) @@ -341,7 +343,8 @@ public abstract class GrpcClient extends RpcClient { ConnectionSetupRequest conSetupRequest = new ConnectionSetupRequest(); conSetupRequest.setClientVersion(VersionUtils.getFullClientVersion()); conSetupRequest.setLabels(super.getLabels()); - conSetupRequest.setAbilityTable(super.clientAbilities == null ? AbilityKey.getAbilityBitFlags() : clientAbilities); + conSetupRequest.setAbilityTable(getAbilityBit()); + conSetupRequest.setServer(isServer()); conSetupRequest.setTenant(super.getTenant()); grpcConn.sendRequest(conSetupRequest); //wait to register connection setup @@ -355,6 +358,21 @@ public abstract class GrpcClient extends RpcClient { return null; } + /** + * get ability, server or client + * + * @return bit table + */ + protected abstract byte[] getAbilityBit(); + + /** + * Return whether server environment + * The same offset may refer to different functions in the client capability table and the server capability table. + * + * @return whether server environment + */ + protected abstract boolean isServer(); + } diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClusterClient.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClusterClient.java index 7b63653b6..67cd0cd7c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClusterClient.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClusterClient.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.common.remote.client.grpc; +import com.alibaba.nacos.api.ability.register.impl.ServerAbilities; import com.alibaba.nacos.api.common.Constants; /** @@ -35,6 +36,16 @@ public class GrpcClusterClient extends GrpcClient { super(name); } + @Override + public byte[] getAbilityBit() { + return ServerAbilities.getBitFlags(); + } + + @Override + protected boolean isServer() { + return true; + } + @Override public int rpcPortOffset() { return Integer.parseInt(System.getProperty(NACOS_SERVER_GRPC_PORT_OFFSET_KEY, diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcSdkClient.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcSdkClient.java index 34cf40824..8cbc9fcc3 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcSdkClient.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcSdkClient.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.common.remote.client.grpc; +import com.alibaba.nacos.api.ability.register.impl.ClientAbilities; import com.alibaba.nacos.api.common.Constants; /** @@ -35,6 +36,16 @@ public class GrpcSdkClient extends GrpcClient { super(name); } + @Override + public byte[] getAbilityBit() { + return ClientAbilities.getBitFlags(); + } + + @Override + protected boolean isServer() { + return false; + } + @Override public int rpcPortOffset() { return Integer.parseInt(System.getProperty(NACOS_SERVER_GRPC_PORT_OFFSET_KEY, diff --git a/common/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClientTest.java b/common/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClientTest.java index 5a81ab3ed..dd6f8f965 100644 --- a/common/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClientTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClientTest.java @@ -47,6 +47,16 @@ public class GrpcClientTest { @Before public void setUp() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { grpcClient = spy(new GrpcClient("testClient") { + @Override + public byte[] getAbilityBit() { + return new byte[1]; + } + + @Override + protected boolean isServer() { + return false; + } + @Override public int rpcPortOffset() { return 1000; 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 44a01258f..6a6d5effa 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 @@ -16,7 +16,10 @@ package com.alibaba.nacos.core.ability.control; +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.utils.AbilityTableUtils; import com.alibaba.nacos.common.JustForTest; import com.alibaba.nacos.common.ability.AbstractAbilityControlManager; import com.alibaba.nacos.common.ability.DefaultAbilityControlManager; @@ -48,7 +51,7 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im /**. * ability for cluster */ - private final Map clusterAbilityTable = new ConcurrentHashMap<>(); + private final Map clusterAbilityTable = new ConcurrentHashMap<>(); /**. * ability for server @@ -58,7 +61,7 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im /** * components for cluster. these will be invoked if cluster ability table changes. */ - private final Map> clusterHandlerMapping = new ConcurrentHashMap<>(); + private final Map> clusterHandlerMapping = new ConcurrentHashMap<>(); private Lock lockForClusterComponents = new ReentrantLock(); @@ -73,7 +76,12 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im } @Override - public boolean isSupport(String connectionId, String abilityKey) { + protected Map getCurrentNodeSupportAbility() { + return AbilityTableUtils.getAbilityTableBy(ServerAbilities.getBitFlags(), ServerAbilities.getOffset()); + } + + @Override + public boolean isSupport(String connectionId, AbilityKey abilityKey) { Boolean isRunning = currentRunningAbility.getOrDefault(abilityKey, false); if (!isRunning) { return false; @@ -93,12 +101,12 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im * @return whether it is turn on */ @Override - public boolean isClusterEnableAbility(String abilityKey) { + public boolean isClusterEnableAbility(AbilityKey abilityKey) { return clusterAbilityTable.getOrDefault(abilityKey, Boolean.FALSE); } @Override - public Map getClusterAbility() { + public Map getClusterAbility() { return Collections.unmodifiableMap(clusterAbilityTable); } @@ -110,17 +118,17 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im * @param handlerMapping component */ @Override - public void registerComponentForCluster(String abilityKey, HandlerMapping handlerMapping, int priority) { + public void registerComponentForCluster(AbilityKey abilityKey, HandlerMapping handlerMapping, int priority) { doRegisterComponent(abilityKey, handlerMapping, this.clusterHandlerMapping, lockForClusterComponents, priority, clusterAbilityTable); } @Override - public int removeClusterComponent(String abilityKey, Class handlerMappingClazz) { + public int removeClusterComponent(AbilityKey abilityKey, Class handlerMappingClazz) { return doRemove(abilityKey, handlerMappingClazz, lockForClusterComponents, clusterHandlerMapping); } @Override - public int removeAllForCluster(String abilityKey) { + public int removeAllForCluster(AbilityKey abilityKey) { List remove = this.clusterHandlerMapping.remove(abilityKey); return Optional.ofNullable(remove).orElse(Collections.emptyList()).size(); } @@ -134,8 +142,8 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im if (isServer) { serversAbilityTable.put(table.getConnectionId(), table); // enter cluster - Map nodeAbility = table.getAbility(); - Set keySet = clusterAbilityTable.keySet(); + Map nodeAbility = table.getAbility(); + Set keySet = clusterAbilityTable.keySet(); keySet.forEach(abilityKey -> { Boolean isEnabled = clusterAbilityTable.get(abilityKey); Boolean val = nodeAbility.getOrDefault(abilityKey, Boolean.FALSE); @@ -153,7 +161,7 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im } } - private ClusterAbilityUpdateEvent buildClusterEvent(String abilityKey, boolean isOn) { + private ClusterAbilityUpdateEvent buildClusterEvent(AbilityKey abilityKey, boolean isOn) { // notify ClusterAbilityUpdateEvent event = new ClusterAbilityUpdateEvent(); event.setAbilityKey(abilityKey); @@ -176,7 +184,7 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im serversAbilityTable.remove(connectionId); // remove from cluster if (MapUtil.isNotEmpty(serversAbilityTable)) { - Set keySet = clusterAbilityTable.keySet(); + Set keySet = clusterAbilityTable.keySet(); keySet.forEach(abilityKey -> { Boolean isEnabled = clusterAbilityTable.getOrDefault(abilityKey, Boolean.FALSE); // nothing to do if enabled @@ -219,17 +227,17 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im private static final long serialVersionUID = -122222411212200111L; - private String abilityKey; + private AbilityKey abilityKey; private boolean isOn; private ClusterAbilityUpdateEvent(){} - public String getAbilityKey() { + public AbilityKey getAbilityKey() { return abilityKey; } - public void setAbilityKey(String abilityKey) { + public void setAbilityKey(AbilityKey abilityKey) { this.abilityKey = abilityKey; } @@ -244,12 +252,12 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im } @JustForTest - protected void setClusterAbilityTable(Map map) { + protected void setClusterAbilityTable(Map map) { clusterAbilityTable.putAll(map); } @JustForTest - protected Map> clusterHandlerMapping() { + protected Map> clusterHandlerMapping() { return this.clusterHandlerMapping; } diff --git a/core/src/main/java/com/alibaba/nacos/core/ability/inte/ClusterAbilityControlSupport.java b/core/src/main/java/com/alibaba/nacos/core/ability/inte/ClusterAbilityControlSupport.java index 1150b2005..5b79e6b6b 100644 --- a/core/src/main/java/com/alibaba/nacos/core/ability/inte/ClusterAbilityControlSupport.java +++ b/core/src/main/java/com/alibaba/nacos/core/ability/inte/ClusterAbilityControlSupport.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.core.ability.inte; +import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import com.alibaba.nacos.common.ability.handler.HandlerMapping; import com.alibaba.nacos.common.ability.inter.AbilityControlManager; @@ -33,7 +35,7 @@ public interface ClusterAbilityControlSupport { * * @return the cluster abilities. */ - Map getClusterAbility(); + Map getClusterAbility(); /**. * Register components for cluster. These will be trigger when its interested ability changes @@ -42,7 +44,7 @@ public interface ClusterAbilityControlSupport { * @param priority a positive number, the higher the priority, the faster it will be called * @param handlerMapping component */ - void registerComponentForCluster(String abilityKey, HandlerMapping handlerMapping, int priority); + void registerComponentForCluster(AbilityKey abilityKey, HandlerMapping handlerMapping, int priority); /**. * Default method to register component @@ -50,25 +52,25 @@ public interface ClusterAbilityControlSupport { * @param abilityKey component key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey}. * @param handlerMapping component instance. */ - default void registerComponentForCluster(String abilityKey, HandlerMapping handlerMapping) { + default void registerComponentForCluster(AbilityKey abilityKey, HandlerMapping handlerMapping) { registerComponentForCluster(abilityKey, handlerMapping, 1); } /** * Remove the component instance of

handlerMappingClazz

. * - * @param abilityKey ability key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey} + * @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(String abilityKey, Class handlerMappingClazz); + int removeClusterComponent(AbilityKey abilityKey, Class handlerMappingClazz); /** * Remove all {@link HandlerMapping} interested in the special ability. - * @param abilityKey abnility key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey} + * @param abilityKey abnility key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey}. * @return the count of components have removed */ - int removeAllForCluster(String abilityKey); + int removeAllForCluster(AbilityKey abilityKey); /**. * Whether current cluster supports ability @@ -76,5 +78,5 @@ public interface ClusterAbilityControlSupport { * @param abilityKey ability key * @return whether it is turn on */ - boolean isClusterEnableAbility(String abilityKey); + boolean isClusterEnableAbility(AbilityKey abilityKey); } 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 2553d24a7..2e159bce1 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,6 +17,7 @@ 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; @@ -32,7 +33,7 @@ public abstract class Connection implements Requester { private boolean traced = false; - private Map abilityTable; + private Map abilityTable; private ClientAbilities abilities; @@ -54,11 +55,11 @@ public abstract class Connection implements Requester { this.traced = traced; } - public Map getAbilityTable() { + public Map getAbilityTable() { return abilityTable; } - public void setAbilityTable(Map abilityTable) { + public void setAbilityTable(Map abilityTable) { this.abilityTable = 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 6f2c32b7f..39edc93c3 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 @@ -16,7 +16,8 @@ package com.alibaba.nacos.core.remote.grpc; -import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.impl.ClientAbilities; +import com.alibaba.nacos.api.ability.register.impl.ServerAbilities; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.grpc.auto.BiRequestStreamGrpc; import com.alibaba.nacos.api.grpc.auto.Payload; @@ -122,7 +123,16 @@ 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())); + if (setUpRequest.isServer()) { + // if from server + connection.setAbilityTable(AbilityTableUtils.getAbilityTableBy(setUpRequest.getAbilityTable(), + ServerAbilities.getOffset())); + } else { + // if from client + connection.setAbilityTable(AbilityTableUtils.getAbilityTableBy(setUpRequest.getAbilityTable(), + ClientAbilities.getOffset())); + } + System.out.println(connection.getAbilityTable()); boolean rejectSdkOnStarting = metaInfo.isSdkSource() && !ApplicationUtils.isStarted(); if (rejectSdkOnStarting || !connectionManager.register(connectionId, connection)) { 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 d16ce16ab..a2869d6cd 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,7 @@ package com.alibaba.nacos.core.remote.grpc; -import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.register.impl.ServerAbilities; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.grpc.auto.Payload; import com.alibaba.nacos.api.grpc.auto.RequestGrpc; @@ -90,7 +90,7 @@ 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(), - AbilityKey.getAbilityBitFlags())); + ServerAbilities.getBitFlags())); traceIfNecessary(serverCheckResponseP, false); responseObserver.onNext(serverCheckResponseP); responseObserver.onCompleted(); diff --git a/test/core-test/src/test/java/com/alibaba/nacos/test/ability/AbilityControlManagerTest.java b/test/core-test/src/test/java/com/alibaba/nacos/test/ability/AbilityControlManagerTest.java index 99d7cced7..16d753d4f 100644 --- a/test/core-test/src/test/java/com/alibaba/nacos/test/ability/AbilityControlManagerTest.java +++ b/test/core-test/src/test/java/com/alibaba/nacos/test/ability/AbilityControlManagerTest.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.test.ability; +import com.alibaba.nacos.api.ability.constant.AbilityKey; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.common.ability.handler.HandlerMapping; import org.junit.Assert; @@ -43,74 +44,74 @@ public class AbilityControlManagerTest { @Before public void inject() { - Map newTable = new HashMap<>(); - newTable.put("stop-raft", true); + Map newTable = new HashMap<>(); + newTable.put(AbilityKey.TEST_1, true); clientAbilityControlManager.setCurrentSupportingAbility(newTable); - Map table = new HashMap<>(); - table.put("stop-raft", true); + Map table = new HashMap<>(); + table.put(AbilityKey.TEST_1, true); serverAbilityControlManager.setCurrentSupportingAbility(table); - Map cluster = new HashMap<>(); - cluster.put("stop-raft", true); + Map cluster = new HashMap<>(); + cluster.put(AbilityKey.TEST_1, true); serverAbilityControlManager.setClusterAbility(cluster); serverAbilityControlManager.setCurrentSupportingAbility(newTable); } @Test public void testClientAdd() { - Map newTable = new HashMap<>(); - newTable.put("test-no-existed", true); - newTable.put("stop-raft", true); + Map newTable = new HashMap<>(); + newTable.put(AbilityKey.TEST_2, true); + newTable.put(AbilityKey.TEST_1, true); AbilityTable table = new AbilityTable(); table.setConnectionId("test-00001"); table.setAbility(newTable); table.setServer(true); clientAbilityControlManager.addNewTable(table); - Assert.assertFalse(clientAbilityControlManager.isSupport("test-00001", "test-no-existed")); - Assert.assertTrue(clientAbilityControlManager.isSupport("test-00001", "stop-raft")); + Assert.assertFalse(clientAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_2)); + Assert.assertTrue(clientAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_1)); } @Test public void testServerAdd() { - Map newTable = new HashMap<>(); - newTable.put("test-no-existed", true); - newTable.put("stop-raft", true); + Map newTable = new HashMap<>(); + newTable.put(AbilityKey.TEST_2, true); + newTable.put(AbilityKey.TEST_1, true); AbilityTable table = new AbilityTable(); table.setConnectionId("test-00001"); table.setAbility(newTable); table.setServer(true); serverAbilityControlManager.addNewTable(table); - Assert.assertFalse(serverAbilityControlManager.isSupport("test-00001", "test-no-existed")); - Assert.assertTrue(serverAbilityControlManager.isSupport("test-00001", "stop-raft")); - Assert.assertTrue(serverAbilityControlManager.isClusterEnableAbility("stop-raft")); + Assert.assertFalse(serverAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_2)); + Assert.assertTrue(serverAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_1)); + Assert.assertTrue(serverAbilityControlManager.isClusterEnableAbility(AbilityKey.TEST_1)); - Map otherServer = new HashMap<>(); - otherServer.put("test-no-existed", true); - otherServer.put("stop-raft", false); + Map otherServer = new HashMap<>(); + otherServer.put(AbilityKey.TEST_2, true); + otherServer.put(AbilityKey.TEST_1, false); AbilityTable otherServerTable = new AbilityTable(); otherServerTable.setConnectionId("test-00000"); otherServerTable.setAbility(otherServer); otherServerTable.setServer(true); serverAbilityControlManager.addNewTable(otherServerTable); - Assert.assertFalse(serverAbilityControlManager.isClusterEnableAbility("stop-raft")); + Assert.assertFalse(serverAbilityControlManager.isClusterEnableAbility(AbilityKey.TEST_1)); - Map clientTa = new HashMap<>(); - clientTa.put("test-no-existed", true); - clientTa.put("stop-raft", false); + Map clientTa = new HashMap<>(); + clientTa.put(AbilityKey.TEST_2, true); + clientTa.put(AbilityKey.TEST_1, false); AbilityTable clientTable = new AbilityTable(); clientTable.setConnectionId("test-00002"); clientTable.setAbility(clientTa); clientTable.setServer(false); serverAbilityControlManager.addNewTable(clientTable); - Assert.assertFalse(serverAbilityControlManager.isClusterEnableAbility("stop-raft")); + Assert.assertFalse(serverAbilityControlManager.isClusterEnableAbility(AbilityKey.TEST_1)); } @Test public void testClientRemove() { - Map clientTa = new HashMap<>(); - clientTa.put("test-no-existed", true); - clientTa.put("stop-raft", false); + Map clientTa = new HashMap<>(); + clientTa.put(AbilityKey.TEST_2, true); + clientTa.put(AbilityKey.TEST_1, false); AbilityTable clientTable = new AbilityTable(); clientTable.setConnectionId("test-01111"); clientTable.setAbility(clientTa); @@ -125,56 +126,56 @@ public class AbilityControlManagerTest { public void testComponent() throws InterruptedException { enabled = 0; // invoke enable() or disable() when registering - serverAbilityControlManager.registerComponent("stop-raft", new TestHandlerMapping(), -1); + serverAbilityControlManager.registerComponent(AbilityKey.TEST_1, new TestHandlerMapping(), -1); Assert.assertEquals(1, serverAbilityControlManager.handlerMappingCount()); - serverAbilityControlManager.enableCurrentNodeAbility("stop-raft"); + serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.TEST_1); // wait for invoking handler asyn Thread.sleep(200L); // nothing happens if it has enabled Assert.assertEquals(enabled, 1); - Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning("stop-raft")); + Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1)); // invoke disable() - serverAbilityControlManager.disableCurrentNodeAbility("stop-raft"); + serverAbilityControlManager.disableCurrentNodeAbility(AbilityKey.TEST_1); // wait for invoking handler asyn Thread.sleep(200L); // disable will invoke handler Assert.assertEquals(enabled, 0); - Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning("stop-raft")); + Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1)); - serverAbilityControlManager.disableCurrentNodeAbility("stop-raft"); + serverAbilityControlManager.disableCurrentNodeAbility(AbilityKey.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("stop-raft")); + Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1)); - serverAbilityControlManager.enableCurrentNodeAbility("stop-raft"); + serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.TEST_1); // wait for invoking handler asyn Thread.sleep(200L); Assert.assertEquals(enabled, 1); - Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning("stop-raft")); + Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1)); - serverAbilityControlManager.enableCurrentNodeAbility("stop-raft"); + serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.TEST_1); // wait for invoking handler asyn Thread.sleep(200L); Assert.assertEquals(enabled, 1); - Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning("stop-raft")); + Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1)); } @Test public void testClusterComponent() throws InterruptedException { clusterEnabled = 0; // invoke enable() because it turn on - serverAbilityControlManager.registerComponentForCluster("stop-raft", new ClusterHandlerMapping(), -1); + serverAbilityControlManager.registerComponentForCluster(AbilityKey.TEST_1, new ClusterHandlerMapping(), -1); Assert.assertEquals(1, serverAbilityControlManager.clusterHandlerMappingCount()); - Assert.assertTrue(serverAbilityControlManager.isClusterEnableAbility("stop-raft")); + Assert.assertTrue(serverAbilityControlManager.isClusterEnableAbility(AbilityKey.TEST_1)); Assert.assertEquals(clusterEnabled, 1); - Map serverAbility = new HashMap<>(); - serverAbility.put("test-no-existed", true); - serverAbility.put("stop-raft", false); + Map 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); @@ -184,20 +185,20 @@ public class AbilityControlManagerTest { Thread.sleep(200L); // disabled - Assert.assertFalse(serverAbilityControlManager.isClusterEnableAbility("stop-raft")); + 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("stop-raft")); + Assert.assertTrue(serverAbilityControlManager.isClusterEnableAbility(AbilityKey.TEST_1)); Assert.assertEquals(clusterEnabled, 1); } @Test public void testCurrentNodeAbility() { - Set keySet = serverAbilityControlManager.getCurrentRunningAbility().keySet(); + Set keySet = serverAbilityControlManager.getCurrentRunningAbility().keySet(); // diable all keySet.forEach(key -> serverAbilityControlManager.disableCurrentNodeAbility(key)); // get all @@ -215,7 +216,7 @@ public class AbilityControlManagerTest { @Test public void testPriority() throws InterruptedException { TestServerAbilityControlManager testServerAbilityControlManager = new TestServerAbilityControlManager(); - String key = "key"; + AbilityKey key = AbilityKey.TEST_1; TestPriority clusterHandlerMapping1 = new TestPriority("1"); TestPriority clusterHandlerMapping2 = new TestPriority("2"); TestPriority clusterHandlerMapping3 = new TestPriority("3"); diff --git a/test/core-test/src/test/java/com/alibaba/nacos/test/ability/TestClientAbilityControlManager.java b/test/core-test/src/test/java/com/alibaba/nacos/test/ability/TestClientAbilityControlManager.java index 5e9fc0899..e506e2952 100644 --- a/test/core-test/src/test/java/com/alibaba/nacos/test/ability/TestClientAbilityControlManager.java +++ b/test/core-test/src/test/java/com/alibaba/nacos/test/ability/TestClientAbilityControlManager.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.test.ability; +import com.alibaba.nacos.api.ability.constant.AbilityKey; import com.alibaba.nacos.client.ability.ClientAbilityControlManager; import com.alibaba.nacos.common.JustForTest; @@ -24,7 +25,7 @@ import java.util.Map; public class TestClientAbilityControlManager extends ClientAbilityControlManager { @JustForTest - public void setCurrentSupportingAbility(Map ability) { + public void setCurrentSupportingAbility(Map ability) { currentRunningAbility.putAll(ability); } } diff --git a/test/core-test/src/test/java/com/alibaba/nacos/test/ability/TestServerAbilityControlManager.java b/test/core-test/src/test/java/com/alibaba/nacos/test/ability/TestServerAbilityControlManager.java index 2c48bc821..3571c6060 100644 --- a/test/core-test/src/test/java/com/alibaba/nacos/test/ability/TestServerAbilityControlManager.java +++ b/test/core-test/src/test/java/com/alibaba/nacos/test/ability/TestServerAbilityControlManager.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.test.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; @@ -27,12 +28,12 @@ import java.util.Map; public class TestServerAbilityControlManager extends ServerAbilityControlManager { @JustForTest - public void setCurrentSupportingAbility(Map ability) { + public void setCurrentSupportingAbility(Map ability) { currentRunningAbility.putAll(ability); } @JustForTest - public void setClusterAbility(Map ability) { + public void setClusterAbility(Map ability) { super.setClusterAbilityTable(ability); } @@ -42,7 +43,7 @@ public class TestServerAbilityControlManager extends ServerAbilityControlManager } @JustForTest - public List getHandlerMapping(String abilityKey) { + public List getHandlerMapping(AbilityKey abilityKey) { return super.handlerMapping().get(abilityKey); } @@ -52,7 +53,7 @@ public class TestServerAbilityControlManager extends ServerAbilityControlManager } @JustForTest - public List getClusterHandlerMapping(String abilityKey) { + public List getClusterHandlerMapping(AbilityKey abilityKey) { return super.clusterHandlerMapping().get(abilityKey); } @@ -60,19 +61,19 @@ public class TestServerAbilityControlManager extends ServerAbilityControlManager * Just a test method. */ @JustForTest - public void registerClusterHandlerMapping(String key, HandlerMapping handlerMapping, int priority) { + public void registerClusterHandlerMapping(AbilityKey key, HandlerMapping handlerMapping, int priority) { List orDefault = super.clusterHandlerMapping().getOrDefault(key, new ArrayList<>()); orDefault.add(new HandlerWithPriority(handlerMapping, priority)); clusterHandlerMapping().put(key, orDefault); } @JustForTest - public void triggerCluster(String abilityKey) { + public void triggerCluster(AbilityKey abilityKey) { triggerHandlerMappingAsyn(abilityKey, true, clusterHandlerMapping()); } @JustForTest - public void trigger(String abilityKey) { + public void trigger(AbilityKey abilityKey) { triggerHandlerMappingAsyn(abilityKey, true, handlerMapping()); } } From 4560d88327aabe7d39859fa8a47c0d801184da00 Mon Sep 17 00:00:00 2001 From: Daydreamer-ia <2296032269@qq.com> Date: Wed, 31 Aug 2022 19:05:00 +0800 Subject: [PATCH 2/6] Tell unknown if it cannot find AbilityTable. --- .../api/ability/constant/AbilityStatus.java | 40 +++++++++++++++++++ .../ability/ClientAbilityControlManager.java | 17 ++++---- .../ability/inter/AbilityControlManager.java | 3 +- .../control/ServerAbilityControlManager.java | 16 ++++---- 4 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityStatus.java diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityStatus.java b/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityStatus.java new file mode 100644 index 000000000..bf788bc8a --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityStatus.java @@ -0,0 +1,40 @@ +/* + * Copyright 1999-2022 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.api.ability.constant; + +/**. + * @author Daydreamer + * @description It is used to know a certain ability whether supporting. + * @date 2022/8/31 12:27 + **/ +public enum AbilityStatus { + + /** + * Support a certain ability + */ + SUPPORTED, + + /** + * Not support a certain ability + */ + NOT_SUPPORTED, + + /** + * Cannot find ability table, unknown + */ + UNKNOWN +} 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 9e859b3b2..1ecdb41df 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 @@ -17,6 +17,7 @@ package com.alibaba.nacos.client.ability; import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.constant.AbilityStatus; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.api.ability.register.impl.ClientAbilities; import com.alibaba.nacos.api.utils.AbilityTableUtils; @@ -43,17 +44,19 @@ public class ClientAbilityControlManager extends DefaultAbilityControlManager { } @Override - public boolean isSupport(String connectionId, AbilityKey abilityKey) { + public AbilityStatus isSupport(String connectionId, AbilityKey abilityKey) { Boolean isRunning = currentRunningAbility.getOrDefault(abilityKey, false); if (!isRunning) { - return false; + return AbilityStatus.NOT_SUPPORTED; } AbilityTable abilityTable = nodeAbilityTable.get(connectionId); - // false if null - return abilityTable != null - && Optional.ofNullable(abilityTable.getAbility()) - .orElse(Collections.emptyMap()) - .getOrDefault(abilityKey, false); + if(abilityTable == null) { + return AbilityStatus.UNKNOWN; + } + Boolean isSupport = Optional.ofNullable(abilityTable.getAbility()) + .orElse(Collections.emptyMap()) + .getOrDefault(abilityKey, false); + return isSupport ? AbilityStatus.SUPPORTED : AbilityStatus.NOT_SUPPORTED; } @Override diff --git a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java index 22bbfd85a..a40cfca63 100644 --- a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.common.ability.inter; import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.constant.AbilityStatus; import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import com.alibaba.nacos.api.ability.entity.AbilityTable; @@ -36,7 +37,7 @@ public interface AbilityControlManager { * @param abilityKey key name which comes from {@link AbilityBitOperate}. * @return whether the ability is supported in certain connection. */ - boolean isSupport(String connectionId, AbilityKey abilityKey); + AbilityStatus isSupport(String connectionId, AbilityKey abilityKey); /** * Whether the ability current node supporting is running. Return false if current node doesn't support. 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 6a6d5effa..16da67df9 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 @@ -17,6 +17,7 @@ package com.alibaba.nacos.core.ability.control; import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.constant.AbilityStatus; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.api.ability.register.impl.ServerAbilities; import com.alibaba.nacos.api.utils.AbilityTableUtils; @@ -81,17 +82,18 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im } @Override - public boolean isSupport(String connectionId, AbilityKey abilityKey) { + public AbilityStatus isSupport(String connectionId, AbilityKey abilityKey) { Boolean isRunning = currentRunningAbility.getOrDefault(abilityKey, false); if (!isRunning) { - return false; + return AbilityStatus.NOT_SUPPORTED; } AbilityTable abilityTable = nodeAbilityTable.get(connectionId); - // false if null - return abilityTable != null - && Optional.ofNullable(abilityTable.getAbility()) - .orElse(Collections.emptyMap()) - .getOrDefault(abilityKey, false); + if(abilityTable == null) { + return AbilityStatus.UNKNOWN; + } + Boolean isSupport = Optional.ofNullable(abilityTable.getAbility()).orElse(Collections.emptyMap()) + .getOrDefault(abilityKey, false); + return isSupport ? AbilityStatus.SUPPORTED : AbilityStatus.NOT_SUPPORTED; } /**. From 22ace653fcdc29c7b28ed76855ab112fb90467fd Mon Sep 17 00:00:00 2001 From: Daydreamer-ia <2296032269@qq.com> Date: Wed, 31 Aug 2022 21:22:24 +0800 Subject: [PATCH 3/6] The abilities of stock connection abilities remains unchanged and the abilities of incremental connection changes. --- .../nacos/api/ability/entity/AbilityTable.java | 7 ++++++- .../nacos/api/utils/AbilityTableUtils.java | 15 +++++++++++++++ .../ability/ClientAbilityControlManager.java | 4 ---- .../ability/AbstractAbilityControlManager.java | 9 +++++++++ .../remote/client/grpc/GrpcClusterClient.java | 5 ++++- .../common/remote/client/grpc/GrpcSdkClient.java | 5 ++++- .../control/ServerAbilityControlManager.java | 4 ---- 7 files changed, 38 insertions(+), 11 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/entity/AbilityTable.java b/api/src/main/java/com/alibaba/nacos/api/ability/entity/AbilityTable.java index 67eb663dd..f35f30826 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/entity/AbilityTable.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/entity/AbilityTable.java @@ -95,5 +95,10 @@ public class AbilityTable implements Cloneable { this.ability = ability; return this; } - + + @Override + public String toString() { + return "AbilityTable{" + "connectionId='" + connectionId + '\'' + ", ability=" + ability + ", isServer=" + + isServer + ", version='" + version + '\'' + '}'; + } } diff --git a/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java b/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java index aa74be70d..e6d4c9d01 100644 --- a/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java +++ b/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java @@ -24,6 +24,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; /**. * @author Daydreamer @@ -98,4 +99,18 @@ public class AbilityTableUtils { } return res; } + + /**. + * get ability bit table by existed ability table and offset map + * + * @param offsetMap offset from {@link AbilityBitOperate} + * @return Return the Map containing AbilityTableKey and isRunning. + */ + public static byte[] getAbilityBiTableBy(Map offsetMap, Map abilityTable) { + // filter the element which abilityTable don't have or value is false + Map res = offsetMap.entrySet().stream() + .filter(item -> abilityTable.getOrDefault(item.getKey(), false)) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + return getAbilityBitBy(res.values()); + } } 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 1ecdb41df..da37ab638 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 @@ -45,10 +45,6 @@ public class ClientAbilityControlManager extends DefaultAbilityControlManager { @Override public AbilityStatus isSupport(String connectionId, AbilityKey abilityKey) { - Boolean isRunning = currentRunningAbility.getOrDefault(abilityKey, false); - if (!isRunning) { - return AbilityStatus.NOT_SUPPORTED; - } AbilityTable abilityTable = nodeAbilityTable.get(connectionId); if(abilityTable == null) { return AbilityStatus.UNKNOWN; 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 c72e6a9e2..28efd20dc 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 @@ -27,6 +27,7 @@ import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.ReentrantLock; @@ -117,6 +118,14 @@ public abstract class AbstractAbilityControlManager implements AbilityControlMan // hook method add(table); // add to node + Set abilityKeys = table.getAbility().keySet(); + Map clientAbilities = table.getAbility(); + abilityKeys.forEach(abilityKey -> { + Boolean res = currentRunningAbility.getOrDefault(abilityKey, false); + Boolean coming = clientAbilities.getOrDefault(abilityKey, false); + clientAbilities.put(abilityKey, res && coming); + }); + System.out.println(table); nodeAbilityTable.put(connectionId, table); } finally { lockForAbilityTable.unlock(); diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClusterClient.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClusterClient.java index 67cd0cd7c..fdae2aa2f 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClusterClient.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClusterClient.java @@ -18,6 +18,8 @@ package com.alibaba.nacos.common.remote.client.grpc; import com.alibaba.nacos.api.ability.register.impl.ServerAbilities; import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.utils.AbilityTableUtils; +import com.alibaba.nacos.common.ability.discover.NacosAbilityManagerHolder; /** * gRPC client for cluster. @@ -38,7 +40,8 @@ public class GrpcClusterClient extends GrpcClient { @Override public byte[] getAbilityBit() { - return ServerAbilities.getBitFlags(); + // calculate the ability bit table based on ability of current client + return AbilityTableUtils.getAbilityBiTableBy(ServerAbilities.getOffset(), NacosAbilityManagerHolder.getInstance().getCurrentRunningAbility()); } @Override diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcSdkClient.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcSdkClient.java index 8cbc9fcc3..9c85b2b3c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcSdkClient.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcSdkClient.java @@ -18,6 +18,8 @@ package com.alibaba.nacos.common.remote.client.grpc; import com.alibaba.nacos.api.ability.register.impl.ClientAbilities; import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.utils.AbilityTableUtils; +import com.alibaba.nacos.common.ability.discover.NacosAbilityManagerHolder; /** * gRPC client for sdk. @@ -38,7 +40,8 @@ public class GrpcSdkClient extends GrpcClient { @Override public byte[] getAbilityBit() { - return ClientAbilities.getBitFlags(); + // calculate the ability bit table based on ability of current client + return AbilityTableUtils.getAbilityBiTableBy(ClientAbilities.getOffset(), NacosAbilityManagerHolder.getInstance().getCurrentRunningAbility()); } @Override 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 16da67df9..1ee0577e3 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 @@ -83,10 +83,6 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im @Override public AbilityStatus isSupport(String connectionId, AbilityKey abilityKey) { - Boolean isRunning = currentRunningAbility.getOrDefault(abilityKey, false); - if (!isRunning) { - return AbilityStatus.NOT_SUPPORTED; - } AbilityTable abilityTable = nodeAbilityTable.get(connectionId); if(abilityTable == null) { return AbilityStatus.UNKNOWN; From dd6c565e65e8692cfd5cd063c7128d1f76d960f2 Mon Sep 17 00:00:00 2001 From: Daydreamer-ia <2296032269@qq.com> Date: Wed, 31 Aug 2022 21:26:13 +0800 Subject: [PATCH 4/6] Fix note --- .../nacos/api/ability/register/impl/ClientAbilities.java | 8 ++++---- .../nacos/api/ability/register/impl/ServerAbilities.java | 6 +++--- .../core/remote/grpc/GrpcBiStreamRequestAcceptor.java | 1 - 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java index ba1a7496d..df065f653 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java @@ -36,15 +36,15 @@ public class ClientAbilities extends AbilityBitOperate { * There is a function named "compression". * The key is "compression", the value is the offset of the flag bit of this ability in the ability table. The value should be unique. * - * You can add a new public static field in

AbilityKeyConstant

like: - * public static final String COMPRESSION = "compression"; + * You can add a new public field in

AbilityKey

like: + * DATA_COMPRESSION * This field can be used outside. * * And then you need to declare the offset of the flag bit of this ability in the ability table, you can: - * abilityOffset.put("compression", 1); means that is the first bit from left to right in the table. + * abilityOffset.put(AbilityKey.DATA_COMPRESSION, 1); means that is the first bit from left to right in the table. * */ - // put ability here, which you want current server supports + // put ability here, which you want current client supports } private ClientAbilities() { diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java index 842591587..facf2525c 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java @@ -36,12 +36,12 @@ public class ServerAbilities extends AbilityBitOperate { * There is a function named "compression". * The key is "compression", the value is the offset of the flag bit of this ability in the ability table. The value should be unique. * - * You can add a new public static field like: - * public static final String COMPRESSION = "compression"; + * You can add a new public field in

AbilityKey

like: + * DATA_COMPRESSION * This field can be used outside. * * And then you need to declare the offset of the flag bit of this ability in the ability table, you can: - * CURRENT_SERVER_ABILITY_OFFSET.put("compression", 1); means that is the first bit from left to right in the table. + * abilityOffset.put(AbilityKey.DATA_COMPRESSION, 1); means that is the first bit from left to right in the table. * */ // put ability here, which you want current server supports 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 39edc93c3..b143834ba 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 @@ -132,7 +132,6 @@ public class GrpcBiStreamRequestAcceptor extends BiRequestStreamGrpc.BiRequestSt connection.setAbilityTable(AbilityTableUtils.getAbilityTableBy(setUpRequest.getAbilityTable(), ClientAbilities.getOffset())); } - System.out.println(connection.getAbilityTable()); boolean rejectSdkOnStarting = metaInfo.isSdkSource() && !ApplicationUtils.isStarted(); if (rejectSdkOnStarting || !connectionManager.register(connectionId, connection)) { From 37cf6c47d8b2efdeb2f162d8ecf844e3ffc021d5 Mon Sep 17 00:00:00 2001 From: Daydreamer-ia <2296032269@qq.com> Date: Thu, 1 Sep 2022 12:33:34 +0800 Subject: [PATCH 5/6] Fix checkstyle. --- .../api/ability/constant/AbilityKey.java | 21 +++++++++++++++++-- .../api/ability/constant/AbilityStatus.java | 6 +++--- .../api/ability/entity/AbilityTable.java | 1 - ...te.java => AbstractAbilityBitOperate.java} | 9 ++++---- .../register/impl/ClientAbilities.java | 8 +++---- .../register/impl/ServerAbilities.java | 8 +++---- .../request/ConnectionSetupRequest.java | 2 +- .../nacos/api/utils/AbilityTableUtils.java | 6 +++--- .../ability/ClientAbilityControlManager.java | 2 +- .../client/config/impl/ClientWorker.java | 1 - .../client/naming/remote/TestConnection.java | 16 ++++++++++++++ .../AbstractAbilityControlManager.java | 5 ++--- .../ability/DefaultAbilityControlManager.java | 14 +++++++------ .../ability/inter/AbilityControlManager.java | 4 ++-- .../ability/inter/AbilityHandlerRegistry.java | 10 ++++----- .../listener/ClientAbilityEventListener.java | 17 ++++++++++++++- .../remote/client/RpcClientFactory.java | 1 - .../common/remote/client/grpc/GrpcClient.java | 3 +-- .../control/ServerAbilityControlManager.java | 2 +- .../inte/ClusterAbilityControlSupport.java | 1 - 20 files changed, 91 insertions(+), 46 deletions(-) rename api/src/main/java/com/alibaba/nacos/api/ability/register/{AbilityBitOperate.java => AbstractAbilityBitOperate.java} (87%) 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 71e1e1435..8a9b8f8c1 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 @@ -23,9 +23,26 @@ package com.alibaba.nacos.api.ability.constant; **/ public enum AbilityKey { - /** + /**. * just for junit test */ - TEST_1, TEST_2, + TEST_1("test_1"), + /**. + * just for junit test + */ + TEST_2("test_2"); + + + + + + + + + private final String name; + + AbilityKey(String name) { + this.name = name; + } } diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityStatus.java b/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityStatus.java index bf788bc8a..a762c0373 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityStatus.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityStatus.java @@ -23,17 +23,17 @@ package com.alibaba.nacos.api.ability.constant; **/ public enum AbilityStatus { - /** + /**. * Support a certain ability */ SUPPORTED, - /** + /**. * Not support a certain ability */ NOT_SUPPORTED, - /** + /**. * Cannot find ability table, unknown */ UNKNOWN diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/entity/AbilityTable.java b/api/src/main/java/com/alibaba/nacos/api/ability/entity/AbilityTable.java index f35f30826..63fda78da 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/entity/AbilityTable.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/entity/AbilityTable.java @@ -17,7 +17,6 @@ package com.alibaba.nacos.api.ability.entity; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import java.util.Map; diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/register/AbilityBitOperate.java b/api/src/main/java/com/alibaba/nacos/api/ability/register/AbstractAbilityBitOperate.java similarity index 87% rename from api/src/main/java/com/alibaba/nacos/api/ability/register/AbilityBitOperate.java rename to api/src/main/java/com/alibaba/nacos/api/ability/register/AbstractAbilityBitOperate.java index e16165e74..321842672 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/register/AbilityBitOperate.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/register/AbstractAbilityBitOperate.java @@ -19,6 +19,7 @@ package com.alibaba.nacos.api.ability.register; import com.alibaba.nacos.api.ability.constant.AbilityKey; import com.alibaba.nacos.api.utils.AbilityTableUtils; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -27,7 +28,7 @@ import java.util.Map; * @description Operation for bit table. * @date 2022/7/12 19:23 **/ -public abstract class AbilityBitOperate { +public abstract class AbstractAbilityBitOperate { protected final HashMap abilityOffset = new HashMap<>(); @@ -48,11 +49,11 @@ public abstract class AbilityBitOperate { * @return bit offset */ public Map offset() { - return abilityOffset; + return Collections.unmodifiableMap(abilityOffset); } - /** - * put the bit offset to {@link AbilityBitOperate#abilityBitFlag} + /**. + * put the bit offset to {@link AbstractAbilityBitOperate#abilityBitFlag} */ protected void init() { // init the bits table diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java index df065f653..8df959560 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.api.ability.register.impl; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; import java.util.Map; @@ -26,7 +26,7 @@ import java.util.Map; * @description It is used to register client abilities. * @date 2022/8/31 12:32 **/ -public class ClientAbilities extends AbilityBitOperate { +public class ClientAbilities extends AbstractAbilityBitOperate { private static final ClientAbilities INSTANCE = new ClientAbilities(); @@ -52,7 +52,7 @@ public class ClientAbilities extends AbilityBitOperate { init(); } - /** + /**. * get the ability offset for server * * @return ability offset @@ -61,7 +61,7 @@ public class ClientAbilities extends AbilityBitOperate { return INSTANCE.getAbilityBitFlags(); } - /** + /**. * get the ability offset for server * * @return ability offset diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java index facf2525c..2f959087d 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.api.ability.register.impl; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; import java.util.Map; @@ -26,7 +26,7 @@ import java.util.Map; * @description It is used to register server abilities. * @date 2022/8/31 12:32 **/ -public class ServerAbilities extends AbilityBitOperate { +public class ServerAbilities extends AbstractAbilityBitOperate { private static final ServerAbilities INSTANCE = new ServerAbilities(); @@ -52,7 +52,7 @@ public class ServerAbilities extends AbilityBitOperate { init(); } - /** + /**. * get bit table * * @return ability offset @@ -61,7 +61,7 @@ public class ServerAbilities extends AbilityBitOperate { return INSTANCE.getAbilityBitFlags(); } - /** + /**. * get the ability offset for server * * @return ability offset diff --git a/api/src/main/java/com/alibaba/nacos/api/remote/request/ConnectionSetupRequest.java b/api/src/main/java/com/alibaba/nacos/api/remote/request/ConnectionSetupRequest.java index d2fbd704a..15b3c35d5 100644 --- a/api/src/main/java/com/alibaba/nacos/api/remote/request/ConnectionSetupRequest.java +++ b/api/src/main/java/com/alibaba/nacos/api/remote/request/ConnectionSetupRequest.java @@ -35,7 +35,7 @@ public class ConnectionSetupRequest extends InternalRequest { private byte[] abilityTable; - /** + /**. * server will resolve {@link ConnectionSetupRequest#abilityTable} to server abilities, or to client abilities */ private boolean isServer; diff --git a/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java b/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java index e6d4c9d01..a97d2a2c2 100644 --- a/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java +++ b/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.api.utils; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; import java.util.Collection; import java.util.Collections; @@ -69,7 +69,7 @@ public class AbilityTableUtils { * get ability table by bits * * @param bits bit flag - * @param offsetMap offset from {@link AbilityBitOperate} + * @param offsetMap offset from {@link AbstractAbilityBitOperate} * @return Return the Map containing AbilityTableKey and isRunning. */ public static Map getAbilityTableBy(byte[] bits, Map offsetMap) { @@ -103,7 +103,7 @@ public class AbilityTableUtils { /**. * get ability bit table by existed ability table and offset map * - * @param offsetMap offset from {@link AbilityBitOperate} + * @param offsetMap offset from {@link AbstractAbilityBitOperate} * @return Return the Map containing AbilityTableKey and isRunning. */ public static byte[] getAbilityBiTableBy(Map offsetMap, Map 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 da37ab638..534b3b202 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 @@ -46,7 +46,7 @@ public class ClientAbilityControlManager extends DefaultAbilityControlManager { @Override public AbilityStatus isSupport(String connectionId, AbilityKey abilityKey) { AbilityTable abilityTable = nodeAbilityTable.get(connectionId); - if(abilityTable == null) { + if (abilityTable == null) { return AbilityStatus.UNKNOWN; } Boolean isSupport = Optional.ofNullable(abilityTable.getAbility()) diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index d56b23a7c..a227d537e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -17,7 +17,6 @@ package com.alibaba.nacos.client.config.impl; import com.alibaba.nacos.api.PropertyKeyConst; -import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.config.ConfigType; import com.alibaba.nacos.api.config.listener.Listener; diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/remote/TestConnection.java b/client/src/test/java/com/alibaba/nacos/client/naming/remote/TestConnection.java index 6d02c962d..43fc088fd 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/remote/TestConnection.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/remote/TestConnection.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2022 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.client.naming.remote; import com.alibaba.nacos.api.exception.NacosException; 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 28efd20dc..51e77b837 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 @@ -17,7 +17,7 @@ package com.alibaba.nacos.common.ability; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.common.ability.inter.AbilityControlManager; import com.alibaba.nacos.common.notify.Event; @@ -45,7 +45,7 @@ public abstract class AbstractAbilityControlManager implements AbilityControlMan /** * Abilities current supporting *

- * key: ability key from {@link AbilityBitOperate} + * key: ability key from {@link AbstractAbilityBitOperate} * value: whether to turn on */ protected final Map currentRunningAbility = new ConcurrentHashMap<>(); @@ -125,7 +125,6 @@ public abstract class AbstractAbilityControlManager implements AbilityControlMan Boolean coming = clientAbilities.getOrDefault(abilityKey, false); clientAbilities.put(abilityKey, res && coming); }); - System.out.println(table); nodeAbilityTable.put(connectionId, table); } finally { lockForAbilityTable.unlock(); diff --git a/common/src/main/java/com/alibaba/nacos/common/ability/DefaultAbilityControlManager.java b/common/src/main/java/com/alibaba/nacos/common/ability/DefaultAbilityControlManager.java index 9d7dbfe36..46268198f 100644 --- a/common/src/main/java/com/alibaba/nacos/common/ability/DefaultAbilityControlManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/ability/DefaultAbilityControlManager.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.common.ability; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.common.JustForTest; import com.alibaba.nacos.common.ability.handler.HandlerMapping; @@ -47,13 +47,15 @@ import java.util.concurrent.locks.ReentrantLock; * @description It is a relatively complete capability control center implementation. * @date 2022/7/12 19:18 **/ +@SuppressWarnings("PMD.AbstractClassShouldStartWithAbstractNamingRule") public abstract class DefaultAbilityControlManager extends AbstractAbilityControlManager implements AbilityHandlerRegistry { private static final Logger LOGGER = LoggerFactory.getLogger(DefaultAbilityControlManager.class); - /** - * These handlers will be invoked when the flag of ability change key: ability key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey} value: + /**. + * These handlers will be invoked when the flag of ability change key: + * ability key from {@link com.alibaba.nacos.api.ability.constant.AbilityKey} value: * components who want to be invoked if its interested ability turn on/off */ private final Map> handlerMappings = new ConcurrentHashMap<>(); @@ -162,7 +164,7 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro /** * Remove the component instance of

handlerMappingClazz

. * - * @param abilityKey ability key from {@link AbilityBitOperate} + * @param abilityKey ability key from {@link AbstractAbilityBitOperate} * @param handlerMappingClazz implement of {@link HandlerMapping} * @param lock lock for operation * @param handlerMappingsMap handler collection map @@ -240,7 +242,7 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro /** * Invoke componments which linked to ability key asyn. * - * @param key ability key from {@link AbilityBitOperate} + * @param key ability key from {@link AbstractAbilityBitOperate} * @param isEnabled turn on/off * @param handlerMappingsMap handler collection */ @@ -252,7 +254,7 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro /** * Invoke componments which linked to ability key syn. * - * @param key ability key from {@link AbilityBitOperate} + * @param key ability key from {@link AbstractAbilityBitOperate} * @param isEnabled turn on/off * @param handlerMappingsMap handler collection */ diff --git a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java index a40cfca63..0972ff980 100644 --- a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java @@ -18,7 +18,7 @@ package com.alibaba.nacos.common.ability.inter; import com.alibaba.nacos.api.ability.constant.AbilityKey; import com.alibaba.nacos.api.ability.constant.AbilityStatus; -import com.alibaba.nacos.api.ability.register.AbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; import com.alibaba.nacos.api.ability.entity.AbilityTable; import java.util.Map; @@ -34,7 +34,7 @@ public interface AbilityControlManager { * Whether the ability is supported for Connection. If the ability of current node is closed, it will return false. * * @param connectionId the connection range of ability table. - * @param abilityKey key name which comes from {@link AbilityBitOperate}. + * @param abilityKey key name which comes from {@link AbstractAbilityBitOperate}. * @return whether the ability is supported in certain connection. */ AbilityStatus isSupport(String connectionId, AbilityKey abilityKey); diff --git a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityHandlerRegistry.java b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityHandlerRegistry.java index 77d2e847c..52d1b432f 100644 --- a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityHandlerRegistry.java +++ b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityHandlerRegistry.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.common.ability.inter; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; import com.alibaba.nacos.common.ability.AbstractAbilityControlManager; import com.alibaba.nacos.common.ability.handler.HandlerMapping; @@ -48,7 +48,7 @@ public interface AbilityHandlerRegistry { * Register the component which is managed by {@link AbstractAbilityControlManager}. * if you are hoping that a component will be invoked when turn on/off the ability whose key is

abilityKey

. * - * @param abilityKey component key from {@link AbilityBitOperate} + * @param abilityKey component key from {@link AbstractAbilityBitOperate} * @param priority a positive number, the higher the priority is, the faster it will be called. `1` is the lowest priority. * @param handlerMapping component instance. */ @@ -57,7 +57,7 @@ public interface AbilityHandlerRegistry { /**. * Default method to register component with the lowest priority. * - * @param abilityKey component key from {@link AbilityBitOperate} + * @param abilityKey component key from {@link AbstractAbilityBitOperate} * @param handlerMapping component instance. */ default void registerComponent(AbilityKey abilityKey, HandlerMapping handlerMapping) { @@ -67,7 +67,7 @@ public interface AbilityHandlerRegistry { /** * Remove the component instance of

handlerMappingClazz

. * - * @param abilityKey ability key from {@link AbilityBitOperate} + * @param abilityKey ability key from {@link AbstractAbilityBitOperate} * @param handlerMappingClazz implement of {@link HandlerMapping} * @return the count of components have removed */ @@ -75,7 +75,7 @@ public interface AbilityHandlerRegistry { /** * Remove all {@link HandlerMapping} interested in the special ability. - * @param abilityKey abnility key from {@link AbilityBitOperate} + * @param abilityKey abnility key from {@link AbstractAbilityBitOperate} * @return the count of components have removed */ int removeAll(AbilityKey abilityKey); diff --git a/common/src/main/java/com/alibaba/nacos/common/ability/listener/ClientAbilityEventListener.java b/common/src/main/java/com/alibaba/nacos/common/ability/listener/ClientAbilityEventListener.java index 0c2004365..0093709c5 100644 --- a/common/src/main/java/com/alibaba/nacos/common/ability/listener/ClientAbilityEventListener.java +++ b/common/src/main/java/com/alibaba/nacos/common/ability/listener/ClientAbilityEventListener.java @@ -1,5 +1,20 @@ -package com.alibaba.nacos.common.ability.listener; +/* + * Copyright 1999-2022 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.common.ability.listener; import com.alibaba.nacos.common.ability.discover.NacosAbilityManagerHolder; import com.alibaba.nacos.common.remote.client.Connection; diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClientFactory.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClientFactory.java index 8293f0192..2d71b9e46 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClientFactory.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClientFactory.java @@ -16,7 +16,6 @@ package com.alibaba.nacos.common.remote.client; -import com.alibaba.nacos.api.ability.register.impl.ServerAbilities; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.common.remote.ConnectionType; import com.alibaba.nacos.common.remote.client.grpc.GrpcClient; 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 d4c61452e..c6771ab76 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 @@ -17,7 +17,6 @@ package com.alibaba.nacos.common.remote.client.grpc; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.api.ability.register.impl.ServerAbilities; import com.alibaba.nacos.api.exception.NacosException; @@ -358,7 +357,7 @@ public abstract class GrpcClient extends RpcClient { return null; } - /** + /**. * get ability, server or client * * @return bit table 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 1ee0577e3..e61d39553 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 @@ -84,7 +84,7 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im @Override public AbilityStatus isSupport(String connectionId, AbilityKey abilityKey) { AbilityTable abilityTable = nodeAbilityTable.get(connectionId); - if(abilityTable == null) { + if (abilityTable == null) { return AbilityStatus.UNKNOWN; } Boolean isSupport = Optional.ofNullable(abilityTable.getAbility()).orElse(Collections.emptyMap()) diff --git a/core/src/main/java/com/alibaba/nacos/core/ability/inte/ClusterAbilityControlSupport.java b/core/src/main/java/com/alibaba/nacos/core/ability/inte/ClusterAbilityControlSupport.java index 5b79e6b6b..9b989a078 100644 --- a/core/src/main/java/com/alibaba/nacos/core/ability/inte/ClusterAbilityControlSupport.java +++ b/core/src/main/java/com/alibaba/nacos/core/ability/inte/ClusterAbilityControlSupport.java @@ -17,7 +17,6 @@ package com.alibaba.nacos.core.ability.inte; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbilityBitOperate; import com.alibaba.nacos.common.ability.handler.HandlerMapping; import com.alibaba.nacos.common.ability.inter.AbilityControlManager; From 6273cce1df1b3177903f09ab563f8621f04952d7 Mon Sep 17 00:00:00 2001 From: Daydreamer-ia <2296032269@qq.com> Date: Thu, 1 Sep 2022 17:42:01 +0800 Subject: [PATCH 6/6] Separate client capability and server capability: ClientAbilities and ServerAbilities. --- .../api/ability/constant/AbilityKey.java | 46 +++++++++++++++---- ...rate.java => AbstractAbilityRegistry.java} | 26 +++++------ .../register/impl/ClientAbilities.java | 22 ++++----- .../register/impl/ServerAbilities.java | 22 ++++----- .../nacos/api/utils/AbilityTableUtils.java | 22 +++++++-- .../ability/ClientAbilityControlManager.java | 3 +- .../AbstractAbilityControlManager.java | 4 +- .../ability/DefaultAbilityControlManager.java | 8 ++-- .../ability/inter/AbilityControlManager.java | 4 +- .../ability/inter/AbilityHandlerRegistry.java | 10 ++-- .../common/remote/client/grpc/GrpcClient.java | 15 +++--- .../remote/client/grpc/GrpcClusterClient.java | 9 ---- .../remote/client/grpc/GrpcSdkClient.java | 9 ---- .../remote/client/grpc/GrpcClientTest.java | 4 -- .../control/ServerAbilityControlManager.java | 3 +- .../grpc/GrpcBiStreamRequestAcceptor.java | 14 ++---- .../ability/AbilityControlManagerTest.java | 9 ++-- 17 files changed, 119 insertions(+), 111 deletions(-) rename api/src/main/java/com/alibaba/nacos/api/ability/register/{AbstractAbilityBitOperate.java => AbstractAbilityRegistry.java} (70%) 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 8a9b8f8c1..11a26e54a 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 @@ -16,6 +16,11 @@ package com.alibaba.nacos.api.ability.constant; +import java.util.Arrays; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + /**. * @author Daydreamer * @description Ability key constant. @@ -26,23 +31,44 @@ public enum AbilityKey { /**. * just for junit test */ - TEST_1("test_1"), + TEST_1("test_1", 1), /**. * just for junit test */ - TEST_2("test_2"); - - - - - - - + TEST_2("test_2", 2); + /**. + * the name of a certain ability + */ private final String name; - AbilityKey(String name) { + /**. + * the offset in ability table + */ + private final int offset; + + AbilityKey(String name, int offset) { this.name = name; + this.offset = offset; + } + + public String getName() { + return name; + } + + public int getOffset() { + return offset; + } + + private static final Map OFFSET_MAP; + + public static Map offset() { + return OFFSET_MAP; + } + + static { + OFFSET_MAP = Arrays.stream(AbilityKey.values()) + .collect(Collectors.toMap(Function.identity(), AbilityKey::getOffset)); } } diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/register/AbstractAbilityBitOperate.java b/api/src/main/java/com/alibaba/nacos/api/ability/register/AbstractAbilityRegistry.java similarity index 70% rename from api/src/main/java/com/alibaba/nacos/api/ability/register/AbstractAbilityBitOperate.java rename to api/src/main/java/com/alibaba/nacos/api/ability/register/AbstractAbilityRegistry.java index 321842672..4ad0df43d 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/register/AbstractAbilityBitOperate.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/register/AbstractAbilityRegistry.java @@ -28,9 +28,9 @@ import java.util.Map; * @description Operation for bit table. * @date 2022/7/12 19:23 **/ -public abstract class AbstractAbilityBitOperate { +public abstract class AbstractAbilityRegistry { - protected final HashMap abilityOffset = new HashMap<>(); + protected final Map supportedAbilities = new HashMap<>(); private byte[] abilityBitFlag; @@ -42,21 +42,21 @@ public abstract class AbstractAbilityBitOperate { public byte[] getAbilityBitFlags() { return abilityBitFlag.clone(); } - - /**. - * Return the ability bit offsets - * - * @return bit offset - */ - public Map offset() { - return Collections.unmodifiableMap(abilityOffset); - } /**. - * put the bit offset to {@link AbstractAbilityBitOperate#abilityBitFlag} + * put the bit offset to {@link AbstractAbilityRegistry#abilityBitFlag} */ protected void init() { // init the bits table - abilityBitFlag = AbilityTableUtils.getAbilityBitBy(abilityOffset.values()); + abilityBitFlag = AbilityTableUtils.getAbilityBiTableBy(AbilityKey.values(), supportedAbilities); + } + + /** + * get static ability current server supports + * + * @return static ability + */ + public Map getSupportedAbilities() { + return Collections.unmodifiableMap(supportedAbilities); } } diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java index 8df959560..c4ec32be5 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ClientAbilities.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.api.ability.register.impl; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityRegistry; import java.util.Map; @@ -26,7 +26,7 @@ import java.util.Map; * @description It is used to register client abilities. * @date 2022/8/31 12:32 **/ -public class ClientAbilities extends AbstractAbilityBitOperate { +public class ClientAbilities extends AbstractAbilityRegistry { private static final ClientAbilities INSTANCE = new ClientAbilities(); @@ -34,14 +34,14 @@ public class ClientAbilities extends AbstractAbilityBitOperate { /* * example: * There is a function named "compression". - * The key is "compression", the value is the offset of the flag bit of this ability in the ability table. The value should be unique. + * The key is from

AbilityKey

, the value is whether turn on. * * You can add a new public field in

AbilityKey

like: - * DATA_COMPRESSION - * This field can be used outside. + * DATA_COMPRESSION("compression", 1) + * This field can be used outside, and the offset should be unique. * * And then you need to declare the offset of the flag bit of this ability in the ability table, you can: - * abilityOffset.put(AbilityKey.DATA_COMPRESSION, 1); means that is the first bit from left to right in the table. + * supportedAbilities.put(AbilityKey.DATA_COMPRESSION, true); means that is the first bit from left to right in the table. * */ // put ability here, which you want current client supports @@ -61,12 +61,12 @@ public class ClientAbilities extends AbstractAbilityBitOperate { return INSTANCE.getAbilityBitFlags(); } - /**. - * get the ability offset for server + /** + * get static ability current server supports * - * @return ability offset + * @return static ability */ - public static Map getOffset() { - return INSTANCE.offset(); + public static Map getStaticAbilities(){ + return INSTANCE.getSupportedAbilities(); } } diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java index 2f959087d..bd01a53b8 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilities.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.api.ability.register.impl; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityRegistry; import java.util.Map; @@ -26,7 +26,7 @@ import java.util.Map; * @description It is used to register server abilities. * @date 2022/8/31 12:32 **/ -public class ServerAbilities extends AbstractAbilityBitOperate { +public class ServerAbilities extends AbstractAbilityRegistry { private static final ServerAbilities INSTANCE = new ServerAbilities(); @@ -34,14 +34,14 @@ public class ServerAbilities extends AbstractAbilityBitOperate { /* * example: * There is a function named "compression". - * The key is "compression", the value is the offset of the flag bit of this ability in the ability table. The value should be unique. + * The key is from

AbilityKey

, the value is whether turn on. * * You can add a new public field in

AbilityKey

like: - * DATA_COMPRESSION - * This field can be used outside. + * DATA_COMPRESSION("compression", 1) + * This field can be used outside, and the offset should be unique. * * And then you need to declare the offset of the flag bit of this ability in the ability table, you can: - * abilityOffset.put(AbilityKey.DATA_COMPRESSION, 1); means that is the first bit from left to right in the table. + * supportedAbilities.put(AbilityKey.DATA_COMPRESSION, true); means that is the first bit from left to right in the table. * */ // put ability here, which you want current server supports @@ -61,13 +61,13 @@ public class ServerAbilities extends AbstractAbilityBitOperate { return INSTANCE.getAbilityBitFlags(); } - /**. - * get the ability offset for server + /** + * get static ability current server supports * - * @return ability offset + * @return static ability */ - public static Map getOffset() { - return INSTANCE.offset(); + public static Map getStaticAbilities(){ + return INSTANCE.getSupportedAbilities(); } } diff --git a/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java b/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java index a97d2a2c2..fe75aab2c 100644 --- a/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java +++ b/api/src/main/java/com/alibaba/nacos/api/utils/AbilityTableUtils.java @@ -17,11 +17,13 @@ package com.alibaba.nacos.api.utils; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityRegistry; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -69,7 +71,7 @@ public class AbilityTableUtils { * get ability table by bits * * @param bits bit flag - * @param offsetMap offset from {@link AbstractAbilityBitOperate} + * @param offsetMap offset from {@link AbstractAbilityRegistry} * @return Return the Map containing AbilityTableKey and isRunning. */ public static Map getAbilityTableBy(byte[] bits, Map offsetMap) { @@ -103,7 +105,7 @@ public class AbilityTableUtils { /**. * get ability bit table by existed ability table and offset map * - * @param offsetMap offset from {@link AbstractAbilityBitOperate} + * @param offsetMap offset from {@link AbstractAbilityRegistry} * @return Return the Map containing AbilityTableKey and isRunning. */ public static byte[] getAbilityBiTableBy(Map offsetMap, Map abilityTable) { @@ -113,4 +115,18 @@ public class AbilityTableUtils { .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); 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 abilityTable + */ + public static byte[] getAbilityBiTableBy(AbilityKey[] abilityKeys, Map abilityTable) { + // filter the element which abilityTable don't have or value is false + List keyList = Arrays.stream(abilityKeys).collect(Collectors.toList()); + keyList.removeIf(key -> !abilityTable.getOrDefault(key, false)); + return getAbilityBitBy(keyList.stream().map(AbilityKey::getOffset).collect(Collectors.toList())); + } } 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 534b3b202..eef2427a5 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 @@ -20,7 +20,6 @@ import com.alibaba.nacos.api.ability.constant.AbilityKey; import com.alibaba.nacos.api.ability.constant.AbilityStatus; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.api.ability.register.impl.ClientAbilities; -import com.alibaba.nacos.api.utils.AbilityTableUtils; import com.alibaba.nacos.common.ability.AbstractAbilityControlManager; import com.alibaba.nacos.common.ability.DefaultAbilityControlManager; @@ -40,7 +39,7 @@ public class ClientAbilityControlManager extends DefaultAbilityControlManager { @Override protected Map getCurrentNodeSupportAbility() { - return AbilityTableUtils.getAbilityTableBy(ClientAbilities.getBitFlags(), ClientAbilities.getOffset()); + return ClientAbilities.getStaticAbilities(); } @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 51e77b837..96c5e1c45 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 @@ -17,7 +17,7 @@ package com.alibaba.nacos.common.ability; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityRegistry; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.common.ability.inter.AbilityControlManager; import com.alibaba.nacos.common.notify.Event; @@ -45,7 +45,7 @@ public abstract class AbstractAbilityControlManager implements AbilityControlMan /** * Abilities current supporting *

- * key: ability key from {@link AbstractAbilityBitOperate} + * key: ability key from {@link AbstractAbilityRegistry} * value: whether to turn on */ protected final Map currentRunningAbility = new ConcurrentHashMap<>(); diff --git a/common/src/main/java/com/alibaba/nacos/common/ability/DefaultAbilityControlManager.java b/common/src/main/java/com/alibaba/nacos/common/ability/DefaultAbilityControlManager.java index 46268198f..01fc7a60b 100644 --- a/common/src/main/java/com/alibaba/nacos/common/ability/DefaultAbilityControlManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/ability/DefaultAbilityControlManager.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.common.ability; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityRegistry; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.common.JustForTest; import com.alibaba.nacos.common.ability.handler.HandlerMapping; @@ -164,7 +164,7 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro /** * Remove the component instance of

handlerMappingClazz

. * - * @param abilityKey ability key from {@link AbstractAbilityBitOperate} + * @param abilityKey ability key from {@link AbstractAbilityRegistry} * @param handlerMappingClazz implement of {@link HandlerMapping} * @param lock lock for operation * @param handlerMappingsMap handler collection map @@ -242,7 +242,7 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro /** * Invoke componments which linked to ability key asyn. * - * @param key ability key from {@link AbstractAbilityBitOperate} + * @param key ability key from {@link AbstractAbilityRegistry} * @param isEnabled turn on/off * @param handlerMappingsMap handler collection */ @@ -254,7 +254,7 @@ public abstract class DefaultAbilityControlManager extends AbstractAbilityContro /** * Invoke componments which linked to ability key syn. * - * @param key ability key from {@link AbstractAbilityBitOperate} + * @param key ability key from {@link AbstractAbilityRegistry} * @param isEnabled turn on/off * @param handlerMappingsMap handler collection */ diff --git a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java index 0972ff980..304d673d4 100644 --- a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityControlManager.java @@ -18,7 +18,7 @@ package com.alibaba.nacos.common.ability.inter; import com.alibaba.nacos.api.ability.constant.AbilityKey; import com.alibaba.nacos.api.ability.constant.AbilityStatus; -import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityRegistry; import com.alibaba.nacos.api.ability.entity.AbilityTable; import java.util.Map; @@ -34,7 +34,7 @@ public interface AbilityControlManager { * Whether the ability is supported for Connection. If the ability of current node is closed, it will return false. * * @param connectionId the connection range of ability table. - * @param abilityKey key name which comes from {@link AbstractAbilityBitOperate}. + * @param abilityKey key name which comes from {@link AbstractAbilityRegistry}. * @return whether the ability is supported in certain connection. */ AbilityStatus isSupport(String connectionId, AbilityKey abilityKey); diff --git a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityHandlerRegistry.java b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityHandlerRegistry.java index 52d1b432f..5cbc2a8a5 100644 --- a/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityHandlerRegistry.java +++ b/common/src/main/java/com/alibaba/nacos/common/ability/inter/AbilityHandlerRegistry.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.common.ability.inter; import com.alibaba.nacos.api.ability.constant.AbilityKey; -import com.alibaba.nacos.api.ability.register.AbstractAbilityBitOperate; +import com.alibaba.nacos.api.ability.register.AbstractAbilityRegistry; import com.alibaba.nacos.common.ability.AbstractAbilityControlManager; import com.alibaba.nacos.common.ability.handler.HandlerMapping; @@ -48,7 +48,7 @@ public interface AbilityHandlerRegistry { * Register the component which is managed by {@link AbstractAbilityControlManager}. * if you are hoping that a component will be invoked when turn on/off the ability whose key is

abilityKey

. * - * @param abilityKey component key from {@link AbstractAbilityBitOperate} + * @param abilityKey component key from {@link AbstractAbilityRegistry} * @param priority a positive number, the higher the priority is, the faster it will be called. `1` is the lowest priority. * @param handlerMapping component instance. */ @@ -57,7 +57,7 @@ public interface AbilityHandlerRegistry { /**. * Default method to register component with the lowest priority. * - * @param abilityKey component key from {@link AbstractAbilityBitOperate} + * @param abilityKey component key from {@link AbstractAbilityRegistry} * @param handlerMapping component instance. */ default void registerComponent(AbilityKey abilityKey, HandlerMapping handlerMapping) { @@ -67,7 +67,7 @@ public interface AbilityHandlerRegistry { /** * Remove the component instance of

handlerMappingClazz

. * - * @param abilityKey ability key from {@link AbstractAbilityBitOperate} + * @param abilityKey ability key from {@link AbstractAbilityRegistry} * @param handlerMappingClazz implement of {@link HandlerMapping} * @return the count of components have removed */ @@ -75,7 +75,7 @@ public interface AbilityHandlerRegistry { /** * Remove all {@link HandlerMapping} interested in the special ability. - * @param abilityKey abnility key from {@link AbstractAbilityBitOperate} + * @param abilityKey abnility key from {@link AbstractAbilityRegistry} * @return the count of components have removed */ int removeAll(AbilityKey abilityKey); 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 c6771ab76..7519de40f 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 @@ -48,6 +48,7 @@ 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,7 +320,7 @@ public abstract class GrpcClient extends RpcClient { // ability table will be null if server doesn't support ability table ServerCheckResponse serverCheckResponse = (ServerCheckResponse) response; Map abilityTable = AbilityTableUtils - .getAbilityTableBy(serverCheckResponse.getAbilities(), ServerAbilities.getOffset()); + .getAbilityTableBy(serverCheckResponse.getAbilities(), AbilityKey.offset()); AbilityTable table = new AbilityTable(); table.setServer(true) .setConnectionId(serverCheckResponse.getConnectionId()) @@ -342,7 +343,10 @@ public abstract class GrpcClient extends RpcClient { ConnectionSetupRequest conSetupRequest = new ConnectionSetupRequest(); conSetupRequest.setClientVersion(VersionUtils.getFullClientVersion()); conSetupRequest.setLabels(super.getLabels()); - conSetupRequest.setAbilityTable(getAbilityBit()); + // set ability table + byte[] bitTable = AbilityTableUtils.getAbilityBiTableBy(AbilityKey.values(), + NacosAbilityManagerHolder.getInstance().getCurrentRunningAbility()); + conSetupRequest.setAbilityTable(bitTable); conSetupRequest.setServer(isServer()); conSetupRequest.setTenant(super.getTenant()); grpcConn.sendRequest(conSetupRequest); @@ -357,13 +361,6 @@ public abstract class GrpcClient extends RpcClient { return null; } - /**. - * get ability, server or client - * - * @return bit table - */ - protected abstract byte[] getAbilityBit(); - /** * Return whether server environment * The same offset may refer to different functions in the client capability table and the server capability table. diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClusterClient.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClusterClient.java index fdae2aa2f..74919ba07 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClusterClient.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClusterClient.java @@ -16,10 +16,7 @@ package com.alibaba.nacos.common.remote.client.grpc; -import com.alibaba.nacos.api.ability.register.impl.ServerAbilities; import com.alibaba.nacos.api.common.Constants; -import com.alibaba.nacos.api.utils.AbilityTableUtils; -import com.alibaba.nacos.common.ability.discover.NacosAbilityManagerHolder; /** * gRPC client for cluster. @@ -38,12 +35,6 @@ public class GrpcClusterClient extends GrpcClient { super(name); } - @Override - public byte[] getAbilityBit() { - // calculate the ability bit table based on ability of current client - return AbilityTableUtils.getAbilityBiTableBy(ServerAbilities.getOffset(), NacosAbilityManagerHolder.getInstance().getCurrentRunningAbility()); - } - @Override protected boolean isServer() { return true; diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcSdkClient.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcSdkClient.java index 9c85b2b3c..46cd691f6 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcSdkClient.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcSdkClient.java @@ -16,10 +16,7 @@ package com.alibaba.nacos.common.remote.client.grpc; -import com.alibaba.nacos.api.ability.register.impl.ClientAbilities; import com.alibaba.nacos.api.common.Constants; -import com.alibaba.nacos.api.utils.AbilityTableUtils; -import com.alibaba.nacos.common.ability.discover.NacosAbilityManagerHolder; /** * gRPC client for sdk. @@ -38,12 +35,6 @@ public class GrpcSdkClient extends GrpcClient { super(name); } - @Override - public byte[] getAbilityBit() { - // calculate the ability bit table based on ability of current client - return AbilityTableUtils.getAbilityBiTableBy(ClientAbilities.getOffset(), NacosAbilityManagerHolder.getInstance().getCurrentRunningAbility()); - } - @Override protected boolean isServer() { return false; diff --git a/common/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClientTest.java b/common/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClientTest.java index dd6f8f965..af7104d13 100644 --- a/common/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClientTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcClientTest.java @@ -47,10 +47,6 @@ public class GrpcClientTest { @Before public void setUp() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { grpcClient = spy(new GrpcClient("testClient") { - @Override - public byte[] getAbilityBit() { - return new byte[1]; - } @Override protected boolean isServer() { 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 e61d39553..825faa807 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 @@ -20,7 +20,6 @@ import com.alibaba.nacos.api.ability.constant.AbilityKey; import com.alibaba.nacos.api.ability.constant.AbilityStatus; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.api.ability.register.impl.ServerAbilities; -import com.alibaba.nacos.api.utils.AbilityTableUtils; import com.alibaba.nacos.common.JustForTest; import com.alibaba.nacos.common.ability.AbstractAbilityControlManager; import com.alibaba.nacos.common.ability.DefaultAbilityControlManager; @@ -78,7 +77,7 @@ public class ServerAbilityControlManager extends DefaultAbilityControlManager im @Override protected Map getCurrentNodeSupportAbility() { - return AbilityTableUtils.getAbilityTableBy(ServerAbilities.getBitFlags(), ServerAbilities.getOffset()); + return ServerAbilities.getStaticAbilities(); } @Override 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 b143834ba..b8cf7508b 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 @@ -16,8 +16,7 @@ package com.alibaba.nacos.core.remote.grpc; -import com.alibaba.nacos.api.ability.register.impl.ClientAbilities; -import com.alibaba.nacos.api.ability.register.impl.ServerAbilities; +import com.alibaba.nacos.api.ability.constant.AbilityKey; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.grpc.auto.BiRequestStreamGrpc; import com.alibaba.nacos.api.grpc.auto.Payload; @@ -123,15 +122,8 @@ 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()); - if (setUpRequest.isServer()) { - // if from server - connection.setAbilityTable(AbilityTableUtils.getAbilityTableBy(setUpRequest.getAbilityTable(), - ServerAbilities.getOffset())); - } else { - // if from client - connection.setAbilityTable(AbilityTableUtils.getAbilityTableBy(setUpRequest.getAbilityTable(), - ClientAbilities.getOffset())); - } + connection.setAbilityTable(AbilityTableUtils.getAbilityTableBy(setUpRequest.getAbilityTable(), + AbilityKey.offset())); boolean rejectSdkOnStarting = metaInfo.isSdkSource() && !ApplicationUtils.isStarted(); if (rejectSdkOnStarting || !connectionManager.register(connectionId, connection)) { diff --git a/test/core-test/src/test/java/com/alibaba/nacos/test/ability/AbilityControlManagerTest.java b/test/core-test/src/test/java/com/alibaba/nacos/test/ability/AbilityControlManagerTest.java index 16d753d4f..038cd0f82 100644 --- a/test/core-test/src/test/java/com/alibaba/nacos/test/ability/AbilityControlManagerTest.java +++ b/test/core-test/src/test/java/com/alibaba/nacos/test/ability/AbilityControlManagerTest.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.test.ability; import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.constant.AbilityStatus; import com.alibaba.nacos.api.ability.entity.AbilityTable; import com.alibaba.nacos.common.ability.handler.HandlerMapping; import org.junit.Assert; @@ -68,8 +69,8 @@ public class AbilityControlManagerTest { table.setAbility(newTable); table.setServer(true); clientAbilityControlManager.addNewTable(table); - Assert.assertFalse(clientAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_2)); - Assert.assertTrue(clientAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_1)); + Assert.assertEquals(AbilityStatus.NOT_SUPPORTED, clientAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_2)); + Assert.assertEquals(AbilityStatus.SUPPORTED, clientAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_1)); } @Test @@ -82,8 +83,8 @@ public class AbilityControlManagerTest { table.setAbility(newTable); table.setServer(true); serverAbilityControlManager.addNewTable(table); - Assert.assertFalse(serverAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_2)); - Assert.assertTrue(serverAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_1)); + 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 otherServer = new HashMap<>();