-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1434 from daizhenyu/develop-mysql-write-prohibition
数据库禁写动态配置
- Loading branch information
Showing
16 changed files
with
629 additions
and
100 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
sermant-plugins/sermant-database-write-prohibition/database-config-service/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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>sermant-database-write-prohibition</artifactId> | ||
<groupId>com.huaweicloud.sermant</groupId> | ||
<version>1.0.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>database-config-service</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<config.skip.flag>false</config.skip.flag> | ||
<package.plugin.type>service</package.plugin.type> | ||
<snakeyaml.version>2.0</snakeyaml.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.huaweicloud.sermant</groupId> | ||
<artifactId>sermant-agentcore-core</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.yaml</groupId> | ||
<artifactId>snakeyaml</artifactId> | ||
<version>${snakeyaml.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.huaweicloud.sermant</groupId> | ||
<artifactId>database-controller</artifactId> | ||
<version>${project.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
119 changes: 119 additions & 0 deletions
119
.../src/main/java/com/huaweicloud/sermant/database/dynamicconfig/DatabaseConfigListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* 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.huaweicloud.sermant.database.dynamicconfig; | ||
|
||
import com.huaweicloud.sermant.core.common.LoggerFactory; | ||
import com.huaweicloud.sermant.core.config.ConfigManager; | ||
import com.huaweicloud.sermant.core.plugin.config.ServiceMeta; | ||
import com.huaweicloud.sermant.core.service.dynamicconfig.common.DynamicConfigEvent; | ||
import com.huaweicloud.sermant.core.service.dynamicconfig.common.DynamicConfigEventType; | ||
import com.huaweicloud.sermant.core.service.dynamicconfig.common.DynamicConfigListener; | ||
import com.huaweicloud.sermant.database.config.DatabaseWriteProhibitionConfig; | ||
import com.huaweicloud.sermant.database.config.DatabaseWriteProhibitionManager; | ||
|
||
import org.yaml.snakeyaml.DumperOptions; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.yaml.snakeyaml.error.YAMLException; | ||
import org.yaml.snakeyaml.representer.Representer; | ||
|
||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* 数据层中间件禁写插件的动态配置监听器 | ||
* | ||
* @author daizhenyu | ||
* @since 2024-01-26 | ||
*/ | ||
public class DatabaseConfigListener implements DynamicConfigListener { | ||
/** | ||
* 全局配置的Key | ||
*/ | ||
public static final String GLOBAL_CONFIG_KEY = "sermant.database.write.globalConfig"; | ||
|
||
/** | ||
* 局部配置的key的前缀 | ||
*/ | ||
public static final String LOCAL_CONFIG_KEY_PREFIX = "sermant.database.write."; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(); | ||
|
||
private final Yaml yaml; | ||
|
||
/** | ||
* 监听器构造方法 | ||
*/ | ||
public DatabaseConfigListener() { | ||
Representer representer = new Representer(new DumperOptions()); | ||
representer.getPropertyUtils().setSkipMissingProperties(true); | ||
this.yaml = new Yaml(representer); | ||
} | ||
|
||
@Override | ||
public void process(DynamicConfigEvent event) { | ||
try { | ||
if (event.getEventType() == DynamicConfigEventType.DELETE) { | ||
processDeleteEvent(event); | ||
return; | ||
} | ||
processInitOrCreateOrUpdateEvent(event); | ||
} catch (YAMLException e) { | ||
LOGGER.log(Level.SEVERE, "Fail to convert dynamic database-write-prohibition config, {0}", e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* 处理初始化、创建或者更新配置的事件 | ||
* | ||
* @param event 事件 | ||
*/ | ||
private void processInitOrCreateOrUpdateEvent(DynamicConfigEvent event) { | ||
if (GLOBAL_CONFIG_KEY.equals(event.getKey())) { | ||
DatabaseWriteProhibitionManager | ||
.updateGlobalConfig(yaml.loadAs(event.getContent(), DatabaseWriteProhibitionConfig.class)); | ||
} | ||
if ((LOCAL_CONFIG_KEY_PREFIX + ConfigManager.getConfig(ServiceMeta.class).getService()).equals( | ||
event.getKey())) { | ||
DatabaseWriteProhibitionManager | ||
.updateLocalConfig(yaml.loadAs(event.getContent(), DatabaseWriteProhibitionConfig.class)); | ||
} | ||
if (event.getEventType() == DynamicConfigEventType.INIT) { | ||
LOGGER.log(Level.INFO, "Init database-write-prohibition config, current config: {0}", | ||
DatabaseWriteProhibitionManager.printConfig()); | ||
return; | ||
} | ||
LOGGER.log(Level.INFO, "Update database-write-prohibition config, current config: {0}", | ||
DatabaseWriteProhibitionManager.printConfig()); | ||
} | ||
|
||
/** | ||
* 处理删除配置的事件 | ||
* | ||
* @param event 事件 | ||
*/ | ||
private void processDeleteEvent(DynamicConfigEvent event) { | ||
if (GLOBAL_CONFIG_KEY.equals(event.getKey())) { | ||
DatabaseWriteProhibitionManager.updateGlobalConfig(new DatabaseWriteProhibitionConfig()); | ||
} | ||
if ((LOCAL_CONFIG_KEY_PREFIX + ConfigManager.getConfig(ServiceMeta.class).getService()).equals( | ||
event.getKey())) { | ||
DatabaseWriteProhibitionManager.updateLocalConfig(new DatabaseWriteProhibitionConfig()); | ||
} | ||
LOGGER.log(Level.INFO, "Delete database-write-prohibition config, current config: {0}", | ||
DatabaseWriteProhibitionManager.printConfig()); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...e/src/main/java/com/huaweicloud/sermant/database/dynamicconfig/DatabaseConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* 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.huaweicloud.sermant.database.dynamicconfig; | ||
|
||
import com.huaweicloud.sermant.core.common.LoggerFactory; | ||
import com.huaweicloud.sermant.core.config.ConfigManager; | ||
import com.huaweicloud.sermant.core.plugin.config.ServiceMeta; | ||
import com.huaweicloud.sermant.core.plugin.service.PluginService; | ||
import com.huaweicloud.sermant.core.plugin.subscribe.CommonGroupConfigSubscriber; | ||
import com.huaweicloud.sermant.core.plugin.subscribe.ConfigSubscriber; | ||
|
||
import java.util.logging.Logger; | ||
|
||
/** | ||
* 数据层中间件禁写的动态配置服务 | ||
* | ||
* @author daizhenyu | ||
* @since 2024-01-27 | ||
*/ | ||
public class DatabaseConfigService implements PluginService { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(); | ||
|
||
@Override | ||
public void start() { | ||
ConfigSubscriber subscriber = new CommonGroupConfigSubscriber( | ||
ConfigManager.getConfig(ServiceMeta.class).getService(), | ||
new DatabaseConfigListener(), "database-write-prohibition"); | ||
subscriber.subscribe(); | ||
LOGGER.info("Success to subscribe database-write-prohibition config"); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ain/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.service.PluginService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# | ||
# Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
# | ||
# 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.huaweicloud.sermant.database.dynamicconfig.DatabaseConfigService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.