Change the format of ability table key to string, less time for format conversion when saving.

This commit is contained in:
Daydreamer-ia 2022-09-20 15:24:46 +08:00
parent 5cbfc524a9
commit 5ec1d1f8b5
14 changed files with 61 additions and 103 deletions

View File

@ -25,7 +25,7 @@ import java.util.stream.Collectors;
/**.
* @author Daydreamer
* @description Ability key constant.
* @description Ability key constant. It is used to constrain the ability key.
* @date 2022/8/31 12:27
**/
public enum AbilityKey {

View File

@ -38,13 +38,13 @@ public class RequestMeta {
private Map<String, String> labels = new HashMap<>();
private Map<AbilityKey, Boolean> abilityTable;
private Map<String, Boolean> abilityTable;
public AbilityStatus getConnectionAbility(AbilityKey abilityKey) {
if (abilityTable == null) {
if (abilityTable == null || !abilityTable.containsKey(abilityKey.getName())) {
return AbilityStatus.UNKNOWN;
}
return abilityTable.getOrDefault(abilityKey, false) ? AbilityStatus.SUPPORTED : AbilityStatus.NOT_SUPPORTED;
return abilityTable.get(abilityKey.getName()) ? AbilityStatus.SUPPORTED : AbilityStatus.NOT_SUPPORTED;
}
/**
@ -52,7 +52,7 @@ public class RequestMeta {
*
* @param abilityTable property value of clientVersion
*/
public void setAbilityTable(Map<AbilityKey, Boolean> abilityTable) {
public void setAbilityTable(Map<String, Boolean> abilityTable) {
this.abilityTable = abilityTable;
}

View File

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

View File

@ -61,8 +61,8 @@ public class AbilityTest {
{
super.abilityTable = new HashMap<>();
super.abilityTable.put(AbilityKey.TEST_1, true);
super.abilityTable.put(AbilityKey.TEST_2, false);
super.abilityTable.put(AbilityKey.TEST_1.getName(), true);
super.abilityTable.put(AbilityKey.TEST_2.getName(), false);
}
@Override

View File

@ -65,7 +65,7 @@ public abstract class AbstractAbilityControlManager {
/**.
* ability current node running
*/
protected final Map<AbilityKey, Boolean> currentRunningAbility = new ConcurrentHashMap<>();
protected final Map<String, Boolean> currentRunningAbility = new ConcurrentHashMap<>();
private final ReentrantLock lockForHandlerMappings = new ReentrantLock();
@ -75,18 +75,18 @@ public abstract class AbstractAbilityControlManager {
currentRunningAbility.putAll(initCurrentNodeAbilities());
}
/**
* . Turn on the ability whose key is <p>abilityKey</p>
/**.
* Turn on the ability whose key is <p>abilityKey</p>
*
* @param abilityKey ability key
* @param abilityKey ability key{@link AbilityKey}
* @return if turn success
*/
public boolean enableCurrentNodeAbility(AbilityKey abilityKey) {
return doTurn(true, abilityKey);
}
/**
* . Turn off the ability whose key is <p>abilityKey</p>
/**.
* Turn off the ability whose key is <p>abilityKey</p> {@link AbilityKey}
*
* @param abilityKey ability key
* @return if turn success
@ -95,8 +95,14 @@ public abstract class AbstractAbilityControlManager {
return doTurn(false, abilityKey);
}
/**.
* Whether current node support
*
* @param abilityKey ability key from {@link AbilityKey}
* @return whether support
*/
public boolean isCurrentNodeAbilityRunning(AbilityKey abilityKey) {
return currentRunningAbility.getOrDefault(abilityKey, false);
return currentRunningAbility.getOrDefault(abilityKey.getName(), false);
}
/**.
@ -104,14 +110,14 @@ public abstract class AbstractAbilityControlManager {
*
* @return current node abilities
*/
protected abstract Map<AbilityKey, Boolean> initCurrentNodeAbilities();
protected abstract Map<String, Boolean> initCurrentNodeAbilities();
/**.
* Return the abilities current node
*
* @return current abilities
*/
public Map<AbilityKey, Boolean> getCurrentNodeAbilities() {
public Map<String, Boolean> getCurrentNodeAbilities() {
return Collections.unmodifiableMap(currentRunningAbility);
}
@ -123,7 +129,7 @@ public abstract class AbstractAbilityControlManager {
* @return if turn success
*/
private boolean doTurn(boolean isOn, AbilityKey abilityKey) {
Boolean isEnabled = currentRunningAbility.get(abilityKey);
Boolean isEnabled = currentRunningAbility.get(abilityKey.getName());
// if not supporting this key
if (isEnabled == null) {
LOGGER.warn("[AbilityControlManager] Attempt to turn on/off a not existed ability!");
@ -133,7 +139,7 @@ public abstract class AbstractAbilityControlManager {
return true;
}
// turn on/off
currentRunningAbility.put(abilityKey, isOn);
currentRunningAbility.put(abilityKey.getName(), isOn);
// handler mappings
triggerHandlerMappingAsyn(abilityKey, isOn, this.handlerMappings);
// notify event
@ -192,20 +198,6 @@ public abstract class AbstractAbilityControlManager {
LOGGER.warn("[DefaultAbilityControlManager] - Destruction of the end");
}
/**.
* Combine with current node abilities, in order to get abilities current node provides
*
* @param abilities combined ability table
*/
public void combine(Map<AbilityKey, Boolean> abilities) {
currentRunningAbility.forEach((k, v) -> {
Boolean isCurrentSupport = currentRunningAbility.get(k);
if (isCurrentSupport != null) {
abilities.put(k, abilities.getOrDefault(k, false) && isCurrentSupport);
}
});
}
/**.
* hook for subclass
*/
@ -260,8 +252,8 @@ public abstract class AbstractAbilityControlManager {
*/
protected void doRegisterComponent(AbilityKey abilityKey, HandlerMapping handlerMapping,
Map<AbilityKey, List<HandlerWithPriority>> handlerMappings, Lock lockForHandlerMappings,
int priority, Map<AbilityKey, Boolean> abilityTable) {
if (!currentRunningAbility.containsKey(abilityKey)) {
int priority, Map<String, Boolean> abilityTable) {
if (!currentRunningAbility.containsKey(abilityKey.getName())) {
LOGGER.warn("[AbilityHandlePostProcessor] Failed to register processor: {}, because illegal key!",
handlerMapping.getClass().getSimpleName());
}
@ -275,7 +267,7 @@ public abstract class AbstractAbilityControlManager {
handlerMappings.put(abilityKey, handlers);
// choose behavior
// enable default
if (abilityTable.getOrDefault(abilityKey, false)) {
if (abilityTable.getOrDefault(abilityKey.getName(), false)) {
handlerMapping.enable();
} else {
handlerMapping.disable();
@ -384,15 +376,15 @@ public abstract class AbstractAbilityControlManager {
private boolean isOn;
private Map<AbilityKey, Boolean> table;
private Map<String, Boolean> table;
private AbilityUpdateEvent(){}
public Map<AbilityKey, Boolean> getAbilityTable() {
public Map<String, Boolean> getAbilityTable() {
return table;
}
public void setTable(Map<AbilityKey, Boolean> abilityTable) {
public void setTable(Map<String, Boolean> abilityTable) {
this.table = abilityTable;
}

View File

@ -37,7 +37,7 @@ public abstract class Connection implements Requester {
protected RpcClient.ServerInfo serverInfo;
protected Map<AbilityKey, Boolean> abilityTable;
protected Map<String, Boolean> abilityTable;
public Connection(RpcClient.ServerInfo serverInfo) {
this.serverInfo = serverInfo;
@ -52,13 +52,13 @@ public abstract class Connection implements Requester {
}
public AbilityStatus getConnectionAbility(AbilityKey abilityKey) {
if (abilityTable == null) {
if (abilityTable == null || !abilityTable.containsKey(abilityKey.getName())) {
return AbilityStatus.UNKNOWN;
}
return abilityTable.getOrDefault(abilityKey, false) ? AbilityStatus.SUPPORTED : AbilityStatus.NOT_SUPPORTED;
return abilityTable.get(abilityKey.getName()) ? AbilityStatus.SUPPORTED : AbilityStatus.NOT_SUPPORTED;
}
public void setAbilityTable(Map<AbilityKey, Boolean> abilityTable) {
public void setAbilityTable(Map<String, Boolean> abilityTable) {
this.abilityTable = abilityTable;
}

View File

@ -356,14 +356,10 @@ public abstract class GrpcClient extends RpcClient {
grpcConn.setConnectionId(connectionId);
// if not supported, it will be null
if (serverCheckResponse.getAbilities() != null) {
Map<AbilityKey, Boolean> abilityTable = mapAndFilter(serverCheckResponse.getAbilities());
// mark
markForSetup.put(serverCheckResponse.getConnectionId(), new CountDownLatch(1));
// combine with current node abilities
// in order to get abilities current node provides
NacosAbilityManagerHolder.getInstance().combine(abilityTable);
// set server abilities to connection
grpcConn.setAbilityTable(abilityTable);
grpcConn.setAbilityTable(serverCheckResponse.getAbilities());
}
//create stream request and bind connection event to this connection.
@ -378,7 +374,7 @@ public abstract class GrpcClient extends RpcClient {
conSetupRequest.setClientVersion(VersionUtils.getFullClientVersion());
conSetupRequest.setLabels(super.getLabels());
// set ability table
conSetupRequest.setAbilityTable(serverCheckResponse.getAbilities());
conSetupRequest.setAbilityTable(NacosAbilityManagerHolder.getInstance().getCurrentNodeAbilities());
conSetupRequest.setTenant(super.getTenant());
grpcConn.sendRequest(conSetupRequest);
// wait for response
@ -404,32 +400,6 @@ public abstract class GrpcClient extends RpcClient {
return null;
}
/**.
* filter the ability current node not support, map to enum
*
* @param originClientAbilities origin client abilities
* @return enum map
*/
private Map<AbilityKey, Boolean> mapAndFilter(Map<String, Boolean> originClientAbilities) {
Map<AbilityKey, Boolean> res = new HashMap<>(originClientAbilities.size());
Iterator<Map.Entry<String, Boolean>> iterator = originClientAbilities.entrySet().iterator();
// filter ability current node not support
AbstractAbilityControlManager instance = NacosAbilityManagerHolder.getInstance();
while (iterator.hasNext()) {
Map.Entry<String, Boolean> next = iterator.next();
AbilityKey anEnum = AbilityKey.getEnum(next.getKey());
if (anEnum != null) {
// whether support
boolean isRunning = instance.isCurrentNodeAbilityRunning(anEnum) && next.getValue();
res.put(anEnum, isRunning);
// if not support
originClientAbilities.replace(next.getKey(), isRunning);
}
}
return res;
}
@Override
protected void afterReset(ConnectResetRequest request) {
String connectionId = request.getConnectionId();

View File

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

View File

@ -17,7 +17,6 @@
package com.alibaba.nacos.core.remote;
import com.alibaba.nacos.api.ability.ClientAbilities;
import com.alibaba.nacos.api.ability.constant.AbilityKey;
import com.alibaba.nacos.api.remote.Requester;
import java.util.Map;
@ -33,7 +32,7 @@ public abstract class Connection implements Requester {
private boolean traced = false;
private Map<AbilityKey, Boolean> abilityTable;
private Map<String, Boolean> abilityTable;
private ClientAbilities abilities;
@ -55,11 +54,11 @@ public abstract class Connection implements Requester {
this.traced = traced;
}
public void setAbilityTable(Map<AbilityKey, Boolean> abilityTable) {
public void setAbilityTable(Map<String, Boolean> abilityTable) {
this.abilityTable = abilityTable;
}
public Map<AbilityKey, Boolean> getAbilityTable() {
public Map<String, Boolean> getAbilityTable() {
return this.abilityTable;
}

View File

@ -126,9 +126,7 @@ public class GrpcBiStreamRequestAcceptor extends BiRequestStreamGrpc.BiRequestSt
// null if supported
if (setUpRequest.getAbilityTable() != null) {
// map to table
Map<AbilityKey, Boolean> clientAbilities = AbilityKey
.mapEnum(setUpRequest.getAbilityTable());
connection.setAbilityTable(clientAbilities);
connection.setAbilityTable(setUpRequest.getAbilityTable());
}
boolean rejectSdkOnStarting = metaInfo.isSdkSource() && !ApplicationUtils.isStarted();

View File

@ -16,7 +16,6 @@
package com.alibaba.nacos.core.remote.grpc;
import com.alibaba.nacos.api.ability.constant.AbilityKey;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.grpc.auto.Payload;
import com.alibaba.nacos.api.grpc.auto.RequestGrpc;
@ -91,8 +90,8 @@ public class GrpcRequestAcceptor extends RequestGrpc.RequestImplBase {
// server check.
if (ServerCheckRequest.class.getSimpleName().equals(type)) {
Payload serverCheckResponseP = GrpcUtils.convert(new ServerCheckResponse(CONTEXT_KEY_CONN_ID.get(),
// to str map
AbilityKey.mapStr(NacosAbilityManagerHolder.getInstance().getCurrentNodeAbilities())));
// abilities current node supporting
NacosAbilityManagerHolder.getInstance().getCurrentNodeAbilities()));
traceIfNecessary(serverCheckResponseP, false);
responseObserver.onNext(serverCheckResponseP);
responseObserver.onCompleted();

View File

@ -39,8 +39,8 @@ public class AbilityControlManagerTest {
@Before
public void inject() {
Map<AbilityKey, Boolean> newTable = new HashMap<>();
newTable.put(AbilityKey.TEST_1, true);
Map<String, Boolean> newTable = new HashMap<>();
newTable.put(AbilityKey.TEST_1.getName(), true);
serverAbilityControlManager.setCurrentSupportingAbility(newTable);
}
@ -88,18 +88,18 @@ public class AbilityControlManagerTest {
@Test
public void testCurrentNodeAbility() {
Set<AbilityKey> keySet = serverAbilityControlManager.getCurrentNodeAbilities().keySet();
Set<String> keySet = serverAbilityControlManager.getCurrentNodeAbilities().keySet();
// diable all
keySet.forEach(key -> serverAbilityControlManager.disableCurrentNodeAbility(key));
keySet.forEach(key -> serverAbilityControlManager.disableCurrentNodeAbility(AbilityKey.getEnum(key)));
// get all
keySet.forEach(key -> {
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(key));
Assert.assertFalse(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.getEnum(key)));
});
// enable all
keySet.forEach(key -> serverAbilityControlManager.enableCurrentNodeAbility(key));
keySet.forEach(key -> serverAbilityControlManager.enableCurrentNodeAbility(AbilityKey.getEnum(key)));
// get all
keySet.forEach(key -> {
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(key));
Assert.assertTrue(serverAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.getEnum(key)));
});
}

View File

@ -26,7 +26,7 @@ import java.util.Map;
public class TestServerAbilityControlManager extends ServerAbilityControlManager {
@JustForTest
public void setCurrentSupportingAbility(Map<AbilityKey, Boolean> ability) {
public void setCurrentSupportingAbility(Map<String, Boolean> ability) {
currentRunningAbility.clear();
currentRunningAbility.putAll(ability);
}

View File

@ -60,9 +60,9 @@ public class AbilityConfigsTest {
void inject(AbilityConfigs abilityConfigs) {
TestServerAbilityControlManager serverAbilityControlManager = new TestServerAbilityControlManager();
Map<AbilityKey, Boolean> newTable = new HashMap<>();
newTable.put(AbilityKey.TEST_1, true);
newTable.put(AbilityKey.TEST_2, true);
Map<String, Boolean> newTable = new HashMap<>();
newTable.put(AbilityKey.TEST_1.getName(), true);
newTable.put(AbilityKey.TEST_2.getName(), true);
serverAbilityControlManager.setCurrentSupportingAbility(newTable);
abilityConfigs.setAbilityHandlerRegistry(serverAbilityControlManager);
this.serverAbilityControlManager = serverAbilityControlManager;