Refactor control plugin (#11174)

* Refactor control plugin and add default implementation.

* Move auth default plugin into other modules.

* Fix control plugin config not effect problem.

* Fix UT error.
This commit is contained in:
杨翊 SionYang 2023-09-22 16:59:25 +08:00 committed by GitHub
parent a9d303cbe3
commit 51b6a443d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
122 changed files with 1030 additions and 661 deletions

View File

@ -41,7 +41,7 @@
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-plugin-default-impl</artifactId>
<artifactId>nacos-default-plugin-all</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>

View File

@ -144,19 +144,30 @@ nacos.core.auth.plugin.nacos.token.secret.key=
#nacos.core.auth.ldap.case.sensitive=true
#nacos.core.auth.ldap.ignore.partial.result.exception=false
#*************** Control Plugin Related Configurations ***************#
# plugin type
#nacos.plugin.control.manager.type=nacos
# local control rule storage dir, default ${nacos.home}/data/connection and ${nacos.home}/data/tps
#nacos.plugin.control.rule.local.basedir=${nacos.home}
# external control rule storage type, if exist
#nacos.plugin.control.rule.external.storage=
#*************** Config Change Plugin Related Configurations ***************#
# webhook
nacos.core.config.plugin.webhook.enabled=false
#nacos.core.config.plugin.webhook.enabled=false
# It is recommended to use EB https://help.aliyun.com/document_detail/413974.html
nacos.core.config.plugin.webhook.url=http://localhost:8080/webhook/send?token=***
#nacos.core.config.plugin.webhook.url=http://localhost:8080/webhook/send?token=***
# The content push max capacity ,byte
nacos.core.config.plugin.webhook.contentMaxCapacity=102400
#nacos.core.config.plugin.webhook.contentMaxCapacity=102400
# whitelist
nacos.core.config.plugin.whitelist.enabled=false
#nacos.core.config.plugin.whitelist.enabled=false
# The import file suffixs
nacos.core.config.plugin.whitelist.suffixs=xml,text,properties,yaml,html
#nacos.core.config.plugin.whitelist.suffixs=xml,text,properties,yaml,html
# fileformatcheck,which validate the import file of type and content
nacos.core.config.plugin.fileformatcheck.enabled=false
#nacos.core.config.plugin.fileformatcheck.enabled=false
#*************** Istio Related Configurations ***************#
### If turn on the MCP server:

View File

@ -20,57 +20,37 @@ import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.configs.ControlConfigs;
import com.alibaba.nacos.plugin.control.configs.ControlConfigsInitializer;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* spring value for control configs.
*
* @author shiyiyue
*/
@Component
public class SpringValueConfigsInitializer implements ControlConfigsInitializer {
@Value("${nacos.plugin.control.tps.barrier.creator:nacos}")
private String tpsBarrierCreator = "nacos";
private static final String PREFIX = "nacos.plugin.control.";
@Value("${nacos.plugin.control.tps.barrier.rule.creator:nacos}")
private String tpsRuleBarrierCreator = "nacos";
private static final String CONNECTION_RUNTIME_EJECTOR = PREFIX + "connection.runtime.ejector";
@Value("${nacos.plugin.control.connection.runtime.ejector:nacos}")
private String connectionRuntimeEjector = "nacos";
private static final String CONTROL_MANAGER_TYPE = PREFIX + "manager.type";
@Value("${nacos.plugin.control.connection.manager:nacos}")
private String connectionManager = "nacos";
private static final String RULE_EXTERNAL_STORAGE = PREFIX + "rule.external.storage";
@Value("${nacos.plugin.control.tps.manager:nacos}")
private String tpsManager = "nacos";
private static final String LOCAL_RULE_STORAGE_BASE_DIR = PREFIX + "rule.local.basedir";
@Value("${nacos.plugin.control.rule.external.storage:}")
private String ruleExternalStorage = "";
@Value("${nacos.plugin.control.rule.parser:nacos}")
private String ruleParser = "nacos";
@Value("${nacos.plugin.control.rule.local.basedir:}")
private String localRuleStorageBaseDir = "";
private static final String DEFAULT_CONNECTION_RUNTIME_EJECTOR = "nacos";
@Override
public void initialize(ControlConfigs controlConfigs) {
controlConfigs.setTpsManager(tpsManager);
controlConfigs.setTpsBarrierCreator(tpsBarrierCreator);
controlConfigs.setTpsRuleBarrierCreator(tpsRuleBarrierCreator);
controlConfigs.setConnectionRuntimeEjector(connectionRuntimeEjector);
controlConfigs.setConnectionManager(connectionManager);
controlConfigs.setRuleParser(ruleParser);
controlConfigs.setConnectionRuntimeEjector(
EnvUtil.getProperty(CONNECTION_RUNTIME_EJECTOR, DEFAULT_CONNECTION_RUNTIME_EJECTOR));
String localRuleStorageBaseDir = EnvUtil.getProperty(LOCAL_RULE_STORAGE_BASE_DIR);
if (StringUtils.isNotBlank(localRuleStorageBaseDir)) {
controlConfigs.setLocalRuleStorageBaseDir(localRuleStorageBaseDir);
} else {
controlConfigs.setLocalRuleStorageBaseDir(EnvUtil.getNacosHome());
}
controlConfigs.setRuleExternalStorage(ruleExternalStorage);
controlConfigs.setRuleExternalStorage(EnvUtil.getProperty(RULE_EXTERNAL_STORAGE));
controlConfigs.setControlManagerType(EnvUtil.getProperty(CONTROL_MANAGER_TYPE));
}
}

View File

@ -30,7 +30,7 @@ public class TpsControlConfig {
* tps control is enabled.
* @return true/false.
*/
public static final boolean isTpsControlEnabled() {
public static boolean isTpsControlEnabled() {
return true;
}
}

View File

@ -0,0 +1,17 @@
#
# Copyright 1999-2023 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.
#
com.alibaba.nacos.core.control.SpringValueConfigsInitializer

View File

@ -172,6 +172,30 @@ nacos.core.auth.plugin.nacos.token.secret.key=
#nacos.core.auth.ldap.case.sensitive=true
#nacos.core.auth.ldap.ignore.partial.result.exception=false
#*************** Control Plugin Related Configurations ***************#
# plugin type
#nacos.plugin.control.manager.type=nacos
# local control rule storage dir, default ${nacos.home}/data/connection and ${nacos.home}/data/tps
#nacos.plugin.control.rule.local.basedir=${nacos.home}
# external control rule storage type, if exist
#nacos.plugin.control.rule.external.storage=
#*************** Config Change Plugin Related Configurations ***************#
# webhook
#nacos.core.config.plugin.webhook.enabled=false
# It is recommended to use EB https://help.aliyun.com/document_detail/413974.html
#nacos.core.config.plugin.webhook.url=http://localhost:8080/webhook/send?token=***
# The content push max capacity ,byte
#nacos.core.config.plugin.webhook.contentMaxCapacity=102400
# whitelist
#nacos.core.config.plugin.whitelist.enabled=false
# The import file suffixs
#nacos.core.config.plugin.whitelist.suffixs=xml,text,properties,yaml,html
# fileformatcheck,which validate the import file of type and content
#nacos.core.config.plugin.fileformatcheck.enabled=false
#*************** Istio Related Configurations ***************#
### If turn on the MCP server:

View File

@ -21,6 +21,7 @@ import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.naming.monitor.MetricsMonitor;
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.Before;
import org.junit.Test;
@ -28,6 +29,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.mock.env.MockEnvironment;
import java.util.ArrayList;
@ -63,6 +65,7 @@ public class NacosMonitorPushResultHookTest {
@Before
public void setUp() {
EnvUtil.setEnvironment(new MockEnvironment());
MetricsMonitor.resetAll();
serviceInfo.setHosts(new ArrayList<>());
subscriber.setIp("0.0.0.0");

View File

@ -26,6 +26,7 @@ import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.monitor.MetricsMonitor;
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.naming.push.v2.NoRequiredRetryException;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.Before;
import org.junit.Test;
@ -33,6 +34,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.mock.env.MockEnvironment;
import java.util.Collections;
import java.util.Optional;
@ -79,6 +81,7 @@ public class PushExecuteTaskTest {
@Before
public void setUp() {
EnvUtil.setEnvironment(new MockEnvironment());
MetricsMonitor.resetAll();
when(indexesManager.getAllClientsSubscribeService(service)).thenReturn(Collections.singletonList(clientId));
when(clientManager.getClient(clientId)).thenReturn(client);

View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2023 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-plugin-default-impl</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>default-auth-plugin</artifactId>
<name>nacos-default-auth-plugin ${project.version}</name>
<dependencies>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-auth-plugin</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-common</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-sys</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-config</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2021 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.

View File

@ -77,7 +77,7 @@ public class NacosUserDetailsServiceImpl implements UserDetailsService {
}
if (user == null) {
throw new UsernameNotFoundException(username);
throw new UsernameNotFoundException(String.format("User %s not found", username));
}
return new NacosUserDetails(user);
}

View File

@ -1,5 +1,5 @@
#
# Copyright 1999-2021 Alibaba Group Holding Ltd.
# Copyright 1999-2023 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.

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2021 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2021 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2021 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2021 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.
@ -50,10 +50,10 @@ public class JwtTokenManagerTest {
when(authConfigs.isAuthEnabled()).thenReturn(true);
MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty(AuthConstants.TOKEN_SECRET_KEY, Base64.getEncoder().encodeToString(
"SecretKey0123$567890$234567890123456789012345678901234567890123456789".getBytes(
StandardCharsets.UTF_8)));
mockEnvironment.setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS,
AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString());
"SecretKey0123$567890$234567890123456789012345678901234567890123456789"
.getBytes(StandardCharsets.UTF_8)));
mockEnvironment
.setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS, AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString());
EnvUtil.setEnvironment(mockEnvironment);
jwtTokenManager = new JwtTokenManager(authConfigs);
@ -73,8 +73,8 @@ public class JwtTokenManagerTest {
MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty(AuthConstants.TOKEN_SECRET_KEY,
Base64.getEncoder().encodeToString(secretKey.getBytes(StandardCharsets.UTF_8)));
mockEnvironment.setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS,
AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString());
mockEnvironment
.setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS, AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString());
EnvUtil.setEnvironment(mockEnvironment);
@ -119,8 +119,8 @@ public class JwtTokenManagerTest {
MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty(AuthConstants.TOKEN_SECRET_KEY,
Base64.getEncoder().encodeToString(secretKey.getBytes(StandardCharsets.UTF_8)));
mockEnvironment.setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS,
AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString());
mockEnvironment
.setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS, AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString());
EnvUtil.setEnvironment(mockEnvironment);

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2021 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.
@ -26,6 +26,7 @@ import org.junit.Test;
* @date 2023/8/8
*/
public class Base64DecodeTest {
@Test
public void testStandardDecode() {
String origin = "aGVsbG8sbmFjb3MhdGVzdEJhc2U2NGVuY29kZQ==";
@ -33,7 +34,7 @@ public class Base64DecodeTest {
byte[] decodeOrigin = Base64Decode.decode(origin);
Assert.assertArrayEquals(decodeOrigin, expectDecodeOrigin.getBytes());
}
@Test
public void testNotStandardDecode() {
String notStandardOrigin = "SecretKey012345678901234567890123456789012345678901234567890123456789";

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2021 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2023 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-plugin-default-impl</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>default-control-plugin</artifactId>
<name>nacos-default-control-plugin ${project.version}</name>
<dependencies>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-control-plugin</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-common</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,72 @@
/*
* Copyright 1999-2023 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.plugin.control.impl;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.connection.ConnectionMetricsCollector;
import com.alibaba.nacos.plugin.control.connection.request.ConnectionCheckRequest;
import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckCode;
import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckResponse;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Nacos default control plugin implementation.
*
* @author shiyiyue
*/
public class NacosConnectionControlManager extends ConnectionControlManager {
@Override
public String getName() {
return "nacos";
}
public NacosConnectionControlManager() {
super();
}
@Override
public void applyConnectionLimitRule(ConnectionControlRule connectionControlRule) {
super.connectionControlRule = connectionControlRule;
Loggers.CONTROL.info("Connection control rule updated to ->" + (this.connectionControlRule == null ? null
: JacksonUtils.toJson(this.connectionControlRule)));
Loggers.CONTROL.warn("Connection control updated, But connection control manager is no limit implementation.");
}
@Override
public ConnectionCheckResponse check(ConnectionCheckRequest connectionCheckRequest) {
ConnectionCheckResponse connectionCheckResponse = new ConnectionCheckResponse();
connectionCheckResponse.setSuccess(true);
connectionCheckResponse.setCode(ConnectionCheckCode.PASS_BY_TOTAL);
int totalCountLimit = connectionControlRule.getCountLimit();
// Get total connection from metrics
Map<String, Integer> metricsTotalCount = metricsCollectorList.stream().collect(
Collectors.toMap(ConnectionMetricsCollector::getName, ConnectionMetricsCollector::getTotalCount));
int totalCount = metricsTotalCount.values().stream().mapToInt(Integer::intValue).sum();
if (totalCount > totalCountLimit) {
connectionCheckResponse.setSuccess(false);
connectionCheckResponse.setCode(ConnectionCheckCode.DENY_BY_TOTAL_OVER);
}
return connectionCheckResponse;
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 1999-2023 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.plugin.control.impl;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.spi.ControlManagerBuilder;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
/**
* Nacos default control plugin implementation.
*
* @author xiweng.yy
*/
public class NacosControlManagerBuilder implements ControlManagerBuilder {
@Override
public String getName() {
return "nacos";
}
@Override
public ConnectionControlManager buildConnectionControlManager() {
return new NacosConnectionControlManager();
}
@Override
public TpsControlManager buildTpsControlManager() {
return new NacosTpsControlManager();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.
@ -14,12 +14,13 @@
* limitations under the License.
*/
package com.alibaba.nacos.plugin.control.tps.nacos;
package com.alibaba.nacos.plugin.control.impl;
import com.alibaba.nacos.common.executor.ExecutorFactory;
import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.tps.TpsBarrier;
import com.alibaba.nacos.plugin.control.tps.TpsBarrierCreatorProxy;
import com.alibaba.nacos.plugin.control.tps.barrier.TpsBarrier;
import com.alibaba.nacos.plugin.control.tps.barrier.creator.DefaultNacosTpsBarrierCreator;
import com.alibaba.nacos.plugin.control.tps.barrier.creator.TpsBarrierCreator;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import com.alibaba.nacos.plugin.control.tps.TpsMetrics;
import com.alibaba.nacos.plugin.control.tps.request.TpsCheckRequest;
@ -36,7 +37,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* nacos tps control manager.
* Nacos default control plugin implementation.
*
* @author shiyiyue
*/
@ -55,15 +56,20 @@ public class NacosTpsControlManager extends TpsControlManager {
protected ScheduledExecutorService executorService;
public NacosTpsControlManager() {
super();
executorService = ExecutorFactory.newSingleScheduledExecutorService(r -> {
Thread thread = new Thread(r, "nacos.plugin.tps.control.reporter");
thread.setDaemon(true);
return thread;
});
startTpsReport();
}
@Override
protected TpsBarrierCreator buildTpsBarrierCreator() {
return new DefaultNacosTpsBarrierCreator();
}
protected void startTpsReport() {
executorService
.scheduleWithFixedDelay(new NacosTpsControlManager.TpsMetricsReporter(), 0, 900, TimeUnit.MILLISECONDS);
@ -76,7 +82,7 @@ public class NacosTpsControlManager extends TpsControlManager {
*/
public synchronized void registerTpsPoint(String pointName) {
if (!points.containsKey(pointName)) {
points.put(pointName, TpsBarrierCreatorProxy.getTpsBarrierCreator().createTpsBarrier(pointName));
points.put(pointName, tpsBarrierCreator.createTpsBarrier(pointName));
if (rules.containsKey(pointName)) {
points.get(pointName).applyRule(rules.get(pointName));
} else {

View File

@ -0,0 +1,17 @@
#
# Copyright 1999-2023 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.
#
com.alibaba.nacos.plugin.control.impl.NacosControlManagerBuilder

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2023 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-plugin-default-impl</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-default-plugin-all</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>default-auth-plugin</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>default-control-plugin</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>
</project>

View File

@ -26,35 +26,12 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-plugin-default-impl</artifactId>
<packaging>jar</packaging>
<packaging>pom</packaging>
<name>nacos-plugin-default-impl ${project.version}</name>
<url>https://nacos.io</url>
<dependencies>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-auth-plugin</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-common</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-sys</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-config</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
</dependency>
</dependencies>
<modules>
<module>nacos-default-control-plugin</module>
<module>nacos-default-auth-plugin</module>
<module>nacos-default-plugin-all</module>
</modules>
</project>

View File

@ -36,13 +36,7 @@
<artifactId>nacos-common</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-sys</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -18,18 +18,18 @@ package com.alibaba.nacos.plugin.control;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.configs.ControlConfigs;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.connection.nacos.NacosConnectionControlManager;
import com.alibaba.nacos.plugin.control.connection.DefaultConnectionControlManager;
import com.alibaba.nacos.plugin.control.event.ConnectionLimitRuleChangeEvent;
import com.alibaba.nacos.plugin.control.event.TpsControlRuleChangeEvent;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleParser;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleParserProxy;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleStorageProxy;
import com.alibaba.nacos.plugin.control.rule.storage.RuleStorageProxy;
import com.alibaba.nacos.plugin.control.spi.ControlManagerBuilder;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import com.alibaba.nacos.plugin.control.tps.nacos.NacosTpsControlManager;
import com.alibaba.nacos.plugin.control.tps.DefaultTpsControlManager;
import java.util.Collection;
import java.util.Optional;
/**
* control manager center.
@ -40,67 +40,70 @@ public class ControlManagerCenter {
static volatile ControlManagerCenter instance = null;
private final RuleStorageProxy ruleStorageProxy;
private TpsControlManager tpsControlManager;
private ConnectionControlManager connectionControlManager;
private RuleStorageProxy ruleStorageProxy;
private void initConnectionManager() {
Collection<ConnectionControlManager> connectionControlManagers = NacosServiceLoader
.load(ConnectionControlManager.class);
String connectionManagerName = ControlConfigs.getInstance().getConnectionManager();
for (ConnectionControlManager connectionControlManagerInternal : connectionControlManagers) {
if (connectionControlManagerInternal.getName().equalsIgnoreCase(connectionManagerName)) {
Loggers.CONTROL.info("Found connection control manager of name={},class={}", connectionManagerName,
connectionControlManagerInternal.getClass().getSimpleName());
connectionControlManager = connectionControlManagerInternal;
break;
}
}
if (connectionControlManager == null) {
Loggers.CONTROL.warn("Fail to connection control manager of name " + connectionManagerName);
connectionControlManager = new NacosConnectionControlManager();
}
}
private void initTpsControlManager() {
Collection<TpsControlManager> tpsControlManagers = NacosServiceLoader.load(TpsControlManager.class);
String tpsManagerName = ControlConfigs.getInstance().getTpsManager();
for (TpsControlManager tpsControlManagerInternal : tpsControlManagers) {
if (tpsControlManagerInternal.getName().equalsIgnoreCase(tpsManagerName)) {
Loggers.CONTROL.info("Found tps control manager of name={},class={}", tpsManagerName,
tpsControlManagerInternal.getClass().getSimpleName());
tpsControlManager = tpsControlManagerInternal;
break;
}
}
if (tpsControlManager == null) {
Loggers.CONTROL.warn("Fail to found tps control manager of name " + tpsManagerName);
tpsControlManager = new NacosTpsControlManager();
}
}
private ControlManagerCenter() {
initTpsControlManager();
initConnectionManager();
ruleStorageProxy = new RuleStorageProxy();
ruleStorageProxy = RuleStorageProxy.getInstance();
Optional<ControlManagerBuilder> controlManagerBuilder = findTargetControlManagerBuilder();
if (controlManagerBuilder.isPresent()) {
initConnectionManager(controlManagerBuilder.get());
initTpsControlManager(controlManagerBuilder.get());
} else {
buildNoLimitControlManagers();
}
}
private void initConnectionManager(ControlManagerBuilder controlManagerBuilder) {
try {
connectionControlManager = controlManagerBuilder.buildConnectionControlManager();
Loggers.CONTROL.info("Build connection control manager, class={}",
connectionControlManager.getClass().getCanonicalName());
} catch (Exception e) {
Loggers.CONTROL.warn("Build connection control manager failed, use no limit manager replaced.", e);
connectionControlManager = new DefaultConnectionControlManager();
}
}
private void initTpsControlManager(ControlManagerBuilder controlManagerBuilder) {
try {
tpsControlManager = controlManagerBuilder.buildTpsControlManager();
Loggers.CONTROL
.info("Build tps control manager, class={}", tpsControlManager.getClass().getCanonicalName());
} catch (Exception e) {
Loggers.CONTROL.warn("Build tps control manager failed, use no limit manager replaced.", e);
tpsControlManager = new DefaultTpsControlManager();
}
}
private Optional<ControlManagerBuilder> findTargetControlManagerBuilder() {
String controlManagerType = ControlConfigs.getInstance().getControlManagerType();
if (StringUtils.isEmpty(controlManagerType)) {
Loggers.CONTROL.info("Not configure type of control plugin, no limit control for current node.");
return Optional.empty();
}
for (ControlManagerBuilder each : NacosServiceLoader.load(ControlManagerBuilder.class)) {
Loggers.CONTROL.info("Found control manager plugin of name={}", each.getName());
if (controlManagerType.equalsIgnoreCase(each.getName())) {
return Optional.of(each);
}
}
Loggers.CONTROL.warn("Not found control manager plugin of name");
return Optional.empty();
}
private void buildNoLimitControlManagers() {
connectionControlManager = new DefaultConnectionControlManager();
tpsControlManager = new DefaultTpsControlManager();
}
public RuleStorageProxy getRuleStorageProxy() {
return ruleStorageProxy;
}
public RuleParser getRuleParser() {
return RuleParserProxy.getInstance();
}
public TpsControlManager getTpsControlManager() {
return tpsControlManager;
}

View File

@ -50,37 +50,13 @@ public class ControlConfigs {
ControlConfigs.instance = instance;
}
private String tpsBarrierCreator = "nacos";
private String tpsRuleBarrierCreator = "nacos";
private String connectionRuntimeEjector = "nacos";
private String connectionManager = "nacos";
private String tpsManager = "nacos";
private String ruleExternalStorage = "";
private String ruleParser = "nacos";
private String localRuleStorageBaseDir = "";
public String getTpsBarrierCreator() {
return tpsBarrierCreator;
}
public void setTpsBarrierCreator(String tpsBarrierCreator) {
this.tpsBarrierCreator = tpsBarrierCreator;
}
public String getTpsRuleBarrierCreator() {
return tpsRuleBarrierCreator;
}
public void setTpsRuleBarrierCreator(String tpsRuleBarrierCreator) {
this.tpsRuleBarrierCreator = tpsRuleBarrierCreator;
}
private String controlManagerType = "";
public String getRuleExternalStorage() {
return ruleExternalStorage;
@ -90,22 +66,6 @@ public class ControlConfigs {
this.ruleExternalStorage = ruleExternalStorage;
}
public String getRuleParser() {
return ruleParser;
}
public void setRuleParser(String ruleParser) {
this.ruleParser = ruleParser;
}
public String getConnectionManager() {
return connectionManager;
}
public void setConnectionManager(String connectionManager) {
this.connectionManager = connectionManager;
}
public String getConnectionRuntimeEjector() {
return connectionRuntimeEjector;
}
@ -114,14 +74,6 @@ public class ControlConfigs {
this.connectionRuntimeEjector = connectionRuntimeEjector;
}
public String getTpsManager() {
return tpsManager;
}
public void setTpsManager(String tpsManager) {
this.tpsManager = tpsManager;
}
public String getLocalRuleStorageBaseDir() {
return localRuleStorageBaseDir;
}
@ -129,4 +81,12 @@ public class ControlConfigs {
public void setLocalRuleStorageBaseDir(String localRuleStorageBaseDir) {
this.localRuleStorageBaseDir = localRuleStorageBaseDir;
}
public String getControlManagerType() {
return controlManagerType;
}
public void setControlManagerType(String controlManagerType) {
this.controlManagerType = controlManagerType;
}
}

View File

@ -23,8 +23,9 @@ import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.connection.request.ConnectionCheckRequest;
import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckResponse;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleParserProxy;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleStorageProxy;
import com.alibaba.nacos.plugin.control.rule.parser.ConnectionControlRuleParser;
import com.alibaba.nacos.plugin.control.rule.parser.NacosConnectionControlRuleParser;
import com.alibaba.nacos.plugin.control.rule.storage.RuleStorageProxy;
import java.util.Collection;
import java.util.Map;
@ -40,10 +41,26 @@ import java.util.stream.Collectors;
@SuppressWarnings("PMD.AbstractClassShouldStartWithAbstractNamingRule")
public abstract class ConnectionControlManager {
private final ConnectionControlRuleParser connectionControlRuleParser;
protected ConnectionControlRule connectionControlRule;
protected Collection<ConnectionMetricsCollector> metricsCollectorList;
private ScheduledExecutorService executorService;
public ConnectionControlManager() {
metricsCollectorList = NacosServiceLoader.load(ConnectionMetricsCollector.class);
Loggers.CONTROL.info("Load connection metrics collector,size={},{}", metricsCollectorList.size(),
metricsCollectorList);
this.connectionControlRuleParser = buildConnectionControlRuleParser();
initConnectionRule();
if (!metricsCollectorList.isEmpty()) {
initExecuteService();
startConnectionMetricsReport();
}
}
/**
* get manager name.
*
@ -51,18 +68,12 @@ public abstract class ConnectionControlManager {
*/
public abstract String getName();
private ScheduledExecutorService executorService;
protected ConnectionControlRuleParser buildConnectionControlRuleParser() {
return new NacosConnectionControlRuleParser();
}
public ConnectionControlManager() {
metricsCollectorList = NacosServiceLoader.load(ConnectionMetricsCollector.class);
Loggers.CONTROL.info("Load connection metrics collector,size={},{}", metricsCollectorList.size(),
metricsCollectorList);
initConnectionRule();
if (!metricsCollectorList.isEmpty()) {
initExecuteService();
startConnectionMetricsReport();
}
public ConnectionControlRuleParser getConnectionControlRuleParser() {
return connectionControlRuleParser;
}
private void initExecuteService() {
@ -73,10 +84,6 @@ public abstract class ConnectionControlManager {
});
}
private void startConnectionMetricsReport() {
executorService.scheduleWithFixedDelay(new ConnectionMetricsReporter(), 0, 3000, TimeUnit.MILLISECONDS);
}
private void initConnectionRule() {
RuleStorageProxy ruleStorageProxy = RuleStorageProxy.getInstance();
String localRuleContent = ruleStorageProxy.getLocalDiskStorage().getConnectionRule();
@ -92,7 +99,7 @@ public abstract class ConnectionControlManager {
}
if (StringUtils.isNotBlank(localRuleContent)) {
connectionControlRule = RuleParserProxy.getInstance().parseConnectionRule(localRuleContent);
connectionControlRule = connectionControlRuleParser.parseRule(localRuleContent);
Loggers.CONTROL.info("init connection rule end");
} else {
@ -101,6 +108,10 @@ public abstract class ConnectionControlManager {
}
}
private void startConnectionMetricsReport() {
executorService.scheduleWithFixedDelay(new ConnectionMetricsReporter(), 0, 3000, TimeUnit.MILLISECONDS);
}
public ConnectionControlRule getConnectionLimitRule() {
return connectionControlRule;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.
@ -14,29 +14,28 @@
* limitations under the License.
*/
package com.alibaba.nacos.plugin.control.connection.nacos;
package com.alibaba.nacos.plugin.control.connection;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.connection.request.ConnectionCheckRequest;
import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckCode;
import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckResponse;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
/**
* connection control manager.
* default connection control manager, no limit control.
*
* @author shiyiyue
*/
public class NacosConnectionControlManager extends ConnectionControlManager {
public class DefaultConnectionControlManager extends ConnectionControlManager {
@Override
public String getName() {
return "nacos";
return "noLimit";
}
public NacosConnectionControlManager() {
public DefaultConnectionControlManager() {
super();
}
@ -45,7 +44,7 @@ public class NacosConnectionControlManager extends ConnectionControlManager {
super.connectionControlRule = connectionControlRule;
Loggers.CONTROL.info("Connection control rule updated to ->" + (this.connectionControlRule == null ? null
: JacksonUtils.toJson(this.connectionControlRule)));
Loggers.CONTROL.warn("Connection control updated, But connection control manager is no limit implementation.");
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.
@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.alibaba.nacos.plugin.control.ruleactivator;
package com.alibaba.nacos.plugin.control.rule;
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter;
@ -22,9 +22,12 @@ import com.alibaba.nacos.common.notify.listener.Subscriber;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.ControlManagerCenter;
import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import com.alibaba.nacos.plugin.control.event.ConnectionLimitRuleChangeEvent;
import com.alibaba.nacos.plugin.control.event.TpsControlRuleChangeEvent;
import com.alibaba.nacos.plugin.control.rule.storage.RuleStorageProxy;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
import org.slf4j.Logger;
@ -46,14 +49,14 @@ public class ControlRuleChangeActivator {
NotifyCenter.registerSubscriber(connectionRuleChangeSubscriber);
}
class TpsRuleChangeSubscriber extends Subscriber<TpsControlRuleChangeEvent> {
static class TpsRuleChangeSubscriber extends Subscriber<TpsControlRuleChangeEvent> {
@Override
public void onEvent(TpsControlRuleChangeEvent event) {
String pointName = event.getPointName();
LOGGER.info("Tps control rule change event receive,pointName={}, external={} ", pointName,
event.isExternal());
if (event == null || event.getPointName() == null) {
if (event.getPointName() == null) {
return;
}
try {
@ -71,11 +74,11 @@ public class ControlRuleChangeActivator {
}
String tpsRuleContent = ruleStorageProxy.getLocalDiskStorage().getTpsRule(pointName);
TpsControlManager tpsControlManager = ControlManagerCenter.getInstance().getTpsControlManager();
TpsControlRule tpsControlRule = StringUtils.isBlank(tpsRuleContent) ? new TpsControlRule()
: ControlManagerCenter.getInstance().getRuleParser().parseTpsRule(tpsRuleContent);
: tpsControlManager.getTpsControlRuleParser().parseRule(tpsRuleContent);
ControlManagerCenter.getInstance().getTpsControlManager().applyTpsRule(pointName, tpsControlRule);
tpsControlManager.applyTpsRule(pointName, tpsControlRule);
} catch (Exception e) {
LOGGER.warn("Tps control rule apply error ,error= ", e);
@ -89,7 +92,7 @@ public class ControlRuleChangeActivator {
}
}
class ConnectionRuleChangeSubscriber extends Subscriber<ConnectionLimitRuleChangeEvent> {
static class ConnectionRuleChangeSubscriber extends Subscriber<ConnectionLimitRuleChangeEvent> {
@Override
public void onEvent(ConnectionLimitRuleChangeEvent event) {
@ -112,15 +115,15 @@ public class ControlRuleChangeActivator {
String limitRule = ruleStorageProxy.getLocalDiskStorage().getConnectionRule();
Loggers.CONTROL.info("start to apply connection rule content " + limitRule);
ConnectionControlManager controlManager = ControlManagerCenter.getInstance()
.getConnectionControlManager();
ConnectionControlRule connectionControlRule =
StringUtils.isBlank(limitRule) ? new ConnectionControlRule()
: ControlManagerCenter.getInstance().getRuleParser().parseConnectionRule(limitRule);
: controlManager.getConnectionControlRuleParser().parseRule(limitRule);
Loggers.CONTROL.info("end to apply connection rule content ");
if (connectionControlRule != null) {
ControlManagerCenter.getInstance().getConnectionControlManager()
.applyConnectionLimitRule(connectionControlRule);
controlManager.applyConnectionLimitRule(connectionControlRule);
} else {
LOGGER.info("Parse rule is null,Ignore illegal rule :{}", limitRule);
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 1999-2023 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.plugin.control.rule.parser;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
/**
* Connection control rule parser.
*
* @author xiweng.yy
*/
public interface ConnectionControlRuleParser extends RuleParser<ConnectionControlRule> {
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.
@ -14,36 +14,23 @@
* limitations under the License.
*/
package com.alibaba.nacos.plugin.control.ruleactivator;
package com.alibaba.nacos.plugin.control.rule.parser;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
/**
* nacos rule parser.
* Nacos default connection control rule parser with json.
*
* @author shiyiyue
* @author xiweng.yy
*/
public class NacosRuleParser implements RuleParser {
public class NacosConnectionControlRuleParser implements ConnectionControlRuleParser {
@Override
public TpsControlRule parseTpsRule(String ruleContent) {
return StringUtils.isBlank(ruleContent) ? new TpsControlRule()
: JacksonUtils.toObj(ruleContent, TpsControlRule.class);
}
@Override
public ConnectionControlRule parseConnectionRule(String ruleContent) {
public ConnectionControlRule parseRule(String ruleContent) {
return StringUtils.isBlank(ruleContent) ? new ConnectionControlRule()
: JacksonUtils.toObj(ruleContent, ConnectionControlRule.class);
}
@Override
public String getName() {
return "nacos";
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright 1999-2023 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.plugin.control.rule.parser;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
/**
* Nacos default tps control rule parser with json.
*
* @author xiweng.yy
*/
public class NacosTpsControlRuleParser implements TpsControlRuleParser {
@Override
public TpsControlRule parseRule(String ruleContent) {
return StringUtils.isBlank(ruleContent) ? new TpsControlRule()
: JacksonUtils.toObj(ruleContent, TpsControlRule.class);
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright 1999-2023 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.plugin.control.rule.parser;
/**
* parse rule content from raw string.
*
* @author shiyiyue
*/
public interface RuleParser<R> {
/**
* Parse control rule.
*
* @param ruleContent rule raw string
* @return target rule
*/
R parseRule(String ruleContent);
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 1999-2023 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.plugin.control.rule.parser;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
/**
* Tps control rule parser.
*
* @author xiweng.yy
*/
public interface TpsControlRuleParser extends RuleParser<TpsControlRule> {
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.
@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.alibaba.nacos.plugin.control.ruleactivator;
package com.alibaba.nacos.plugin.control.rule.storage;
/**
* external rule storage.

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.
@ -14,11 +14,12 @@
* limitations under the License.
*/
package com.alibaba.nacos.plugin.control.ruleactivator;
package com.alibaba.nacos.plugin.control.rule.storage;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.plugin.control.utils.DiskUtils;
import com.alibaba.nacos.plugin.control.utils.EnvUtils;
import org.slf4j.Logger;
import java.io.File;
@ -37,30 +38,26 @@ public class LocalDiskRuleStorage implements RuleStorage {
private static final Logger LOGGER = Loggers.CONTROL;
private String localRruleBaseDir = defaultBaseDir();
private String localRuleBaseDir = defaultBaseDir();
private File checkTpsBaseDir() {
File baseDir = new File(localRruleBaseDir, "data" + File.separator + "tps" + File.separator);
File baseDir = new File(localRuleBaseDir, "data" + File.separator + "tps" + File.separator);
if (!baseDir.exists()) {
baseDir.mkdirs();
}
return baseDir;
}
public String getLocalRruleBaseDir() {
return localRruleBaseDir;
}
public void setLocalRruleBaseDir(String localRruleBaseDir) {
this.localRruleBaseDir = localRruleBaseDir;
public void setLocalRuleBaseDir(String localRruleBaseDir) {
this.localRuleBaseDir = localRruleBaseDir;
}
private static String defaultBaseDir() {
return EnvUtil.getNacosHome();
return EnvUtils.getNacosHome();
}
private File getConnectionRuleFile() {
File baseDir = new File(localRruleBaseDir, "data" + File.separator + "connection" + File.separator);
File baseDir = new File(localRuleBaseDir, "data" + File.separator + "connection" + File.separator);
if (!baseDir.exists()) {
baseDir.mkdir();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.
@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.alibaba.nacos.plugin.control.ruleactivator;
package com.alibaba.nacos.plugin.control.rule.storage;
/**
* rule storage.

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
* Copyright 1999-2023 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.
@ -14,12 +14,14 @@
* limitations under the License.
*/
package com.alibaba.nacos.plugin.control.ruleactivator;
package com.alibaba.nacos.plugin.control.rule.storage;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.configs.ControlConfigs;
import com.alibaba.nacos.plugin.control.rule.ControlRuleChangeActivator;
import com.alibaba.nacos.plugin.control.spi.ExternalRuleStorageBuilder;
import org.slf4j.Logger;
import java.util.Collection;
@ -41,30 +43,40 @@ public class RuleStorageProxy {
ControlRuleChangeActivator controlRuleChangeActivator = null;
public RuleStorageProxy() {
Collection<ExternalRuleStorage> persistRuleActivators = NacosServiceLoader.load(ExternalRuleStorage.class);
String rulePersistActivator = ControlConfigs.getInstance().getRuleExternalStorage();
for (ExternalRuleStorage persistRuleActivator : persistRuleActivators) {
if (persistRuleActivator.getName().equalsIgnoreCase(rulePersistActivator)) {
LOGGER.info("Found persist rule storage of name " + rulePersistActivator);
externalRuleStorage = persistRuleActivator;
private RuleStorageProxy() {
String externalStorageType = ControlConfigs.getInstance().getRuleExternalStorage();
if (StringUtils.isNotEmpty(externalStorageType)) {
buildExternalStorage(externalStorageType);
}
initLocalStorage();
controlRuleChangeActivator = new ControlRuleChangeActivator();
}
private void buildExternalStorage(String externalStorageType) {
Collection<ExternalRuleStorageBuilder> externalRuleStorageBuilders = NacosServiceLoader
.load(ExternalRuleStorageBuilder.class);
for (ExternalRuleStorageBuilder each : externalRuleStorageBuilders) {
LOGGER.info("Found persist rule storage of name : {}", externalStorageType);
if (externalStorageType.equalsIgnoreCase(each.getName())) {
try {
externalRuleStorage = each.buildExternalRuleStorage();
} catch (Exception e) {
LOGGER.warn("Build external rule storage failed, the rules will not be persisted", e);
}
LOGGER.info("Build external rule storage of name {} finished", externalStorageType);
break;
}
}
if (externalRuleStorage == null && StringUtils.isNotBlank(rulePersistActivator)) {
LOGGER.error("Fail to found persist rule storage of name " + rulePersistActivator);
if (externalRuleStorage == null && StringUtils.isNotBlank(externalStorageType)) {
LOGGER.error("Fail to found persist rule storage of name : {}", externalStorageType);
}
//local disk storage.
}
private void initLocalStorage() {
localDiskRuleStorage = new LocalDiskRuleStorage();
if (StringUtils.isNotBlank(ControlConfigs.getInstance().getLocalRuleStorageBaseDir())) {
localDiskRuleStorage.setLocalRruleBaseDir(ControlConfigs.getInstance().getLocalRuleStorageBaseDir());
localDiskRuleStorage.setLocalRuleBaseDir(ControlConfigs.getInstance().getLocalRuleStorageBaseDir());
}
controlRuleChangeActivator = new ControlRuleChangeActivator();
}
public RuleStorage getLocalDiskStorage() {
@ -75,7 +87,7 @@ public class RuleStorageProxy {
return externalRuleStorage;
}
public static final RuleStorageProxy getInstance() {
public static RuleStorageProxy getInstance() {
return INSTANCE;
}
}

View File

@ -1,51 +0,0 @@
/*
* Copyright 1999-2020 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.plugin.control.ruleactivator;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
/**
* parse rule content from raw string.
*
* @author shiyiyue
*/
public interface RuleParser {
/**
* parse tps rule content.
*
* @param ruleContent ruleContent.
* @return
*/
TpsControlRule parseTpsRule(String ruleContent);
/**
* parse connection rule.
*
* @param ruleContent ruleContent.
* @return
*/
ConnectionControlRule parseConnectionRule(String ruleContent);
/**
* get name.
*
* @return
*/
String getName();
}

View File

@ -1,58 +0,0 @@
/*
* Copyright 1999-2020 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.plugin.control.ruleactivator;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.configs.ControlConfigs;
import org.slf4j.Logger;
import java.util.Collection;
/**
* rule parser proxy.
*
* @author shiyiyue
*/
public class RuleParserProxy {
private static final Logger LOGGER = Loggers.CONTROL;
private static RuleParser instance;
static {
Collection<RuleParser> ruleParsers = NacosServiceLoader.load(RuleParser.class);
String ruleParserName = ControlConfigs.getInstance().getRuleParser();
for (RuleParser ruleParser : ruleParsers) {
if (ruleParser.getName().equalsIgnoreCase(ruleParserName)) {
LOGGER.info("Found rule parser of name={},class={}", ruleParserName,
ruleParser.getClass().getSimpleName());
instance = ruleParser;
break;
}
}
if (instance == null) {
LOGGER.warn("Fail to rule parser of name " + ruleParserName);
instance = new NacosRuleParser();
}
}
public static RuleParser getInstance() {
return instance;
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright 1999-2023 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.plugin.control.spi;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
/**
* Nacos control plugin manager builder SPI.
*
* @author xiweng.yy
*/
public interface ControlManagerBuilder {
/**
* Get plugin name.
*
* @return name of plugin
*/
String getName();
/**
* Build {@link ConnectionControlManager} implementation for current plugin.
*
* @return ConnectionControlManager implementation
*/
ConnectionControlManager buildConnectionControlManager();
/**
* Build {@link TpsControlManager} implementation for current plugin.
*
* @return TpsControlManager implementation
*/
TpsControlManager buildTpsControlManager();
}

View File

@ -0,0 +1,41 @@
/*
* Copyright 1999-2023 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.plugin.control.spi;
import com.alibaba.nacos.plugin.control.rule.storage.ExternalRuleStorage;
/**
* Nacos control plugin external rule storage builder SPI.
*
* @author xiweng.yy
*/
public interface ExternalRuleStorageBuilder {
/**
* Get plugin name.
*
* @return name of plugin
*/
String getName();
/**
* Build {@link ExternalRuleStorage} implementation for current plugin if necessary.
*
* @return ExternalRuleStorage implementation
*/
ExternalRuleStorage buildExternalRuleStorage();
}

Some files were not shown because too many files have changed in this diff Show More