Skip to content

Commit

Permalink
数据库禁写动态配置
Browse files Browse the repository at this point in the history
Signed-off-by: daizhenyu <[email protected]>
  • Loading branch information
daizhenyu committed Jan 29, 2024
1 parent 5f69c99 commit 8249587
Show file tree
Hide file tree
Showing 16 changed files with 649 additions and 100 deletions.
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* 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.Locale;
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.INIT) {
processInitEvent(event);
return;
}

if (event.getEventType() == DynamicConfigEventType.DELETE) {
processDeleteEvent(event);
return;
}
processCreateOrUpdateEvent(event);
} catch (YAMLException e) {
LOGGER.severe(String.format(Locale.ROOT, "Fail to convert dynamic database-write-prohibition config, %s",
e.getMessage()));
}
}

/**
* 处理创建或者更新配置的事件
*
* @param event 事件
*/
private void processCreateOrUpdateEvent(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));
}
LOGGER.info(String.format(Locale.ROOT, "Update database-write-prohibition config, current config: %s",
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.info(String.format(Locale.ROOT, "Delete database-write-prohibition config, current config: %s",
DatabaseWriteProhibitionManager.printConfig()));
}

/**
* 处理启动时初始化配置的事件
*
* @param event 事件
*/
private void processInitEvent(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));
}
LOGGER.info(String.format(Locale.ROOT, "Init database-write-prohibition config, current config: %s",
DatabaseWriteProhibitionManager.printConfig()));
}
}
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");
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,118 @@
**/
public class DatabaseWriteProhibitionConfig {
/**
* 是否开启禁写
* MongoDb是否开启禁写
*/
private boolean enableDatabaseWriteProhibition = false;
private boolean enableMongoDbWriteProhibition = false;

/**
* 需要禁写的数据库
* MongoDb需要禁写的数据库
*/
private Set<String> databases = new HashSet<>();
private Set<String> mongoDbDatabases = new HashSet<>();

public boolean isEnableDatabaseWriteProhibition() {
return enableDatabaseWriteProhibition;
}
/**
* Mysql是否开启禁写
*/
private boolean enableMysqlWriteProhibition = false;

public void setEnableDatabaseWriteProhibition(boolean enableDatabaseWriteProhibition) {
this.enableDatabaseWriteProhibition = enableDatabaseWriteProhibition;
}
/**
* Mysql需要禁写的数据库
*/
private Set<String> mysqlDatabases = new HashSet<>();

/**
* PostgreSQL是否开启禁写
*/
private boolean enablePostgreSqlWriteProhibition = false;

/**
* PostgreSQL需要禁写的数据库
*/
private Set<String> postgreSqlDatabases = new HashSet<>();

/**
* OpenGauss是否开启禁写
*/
private boolean enableOpenGaussWriteProhibition = false;

/**
* 获取禁消费的数据库列表
*
* @return 数据库列表
* OpenGauss需要禁写的数据库
*/
public Set<String> getDatabases() {
return databases;
private Set<String> openGaussDatabases = new HashSet<>();

public boolean isEnableMongoDbWriteProhibition() {
return enableMongoDbWriteProhibition;
}

public void setEnableMongoDbWriteProhibition(boolean enableMongoDbWriteProhibition) {
this.enableMongoDbWriteProhibition = enableMongoDbWriteProhibition;
}

public Set<String> getMongoDbDatabases() {
return mongoDbDatabases;
}

public void setMongoDbDatabases(Set<String> mongoDbDatabases) {
this.mongoDbDatabases = mongoDbDatabases;
}

public boolean isEnableMysqlWriteProhibition() {
return enableMysqlWriteProhibition;
}

public void setEnableMysqlWriteProhibition(boolean enableMysqlWriteProhibition) {
this.enableMysqlWriteProhibition = enableMysqlWriteProhibition;
}

public Set<String> getMysqlDatabases() {
return mysqlDatabases;
}

public void setMysqlDatabases(Set<String> mysqlDatabases) {
this.mysqlDatabases = mysqlDatabases;
}

public boolean isEnablePostgreSqlWriteProhibition() {
return enablePostgreSqlWriteProhibition;
}

public void setEnablePostgreSqlWriteProhibition(boolean enablePostgreSqlWriteProhibition) {
this.enablePostgreSqlWriteProhibition = enablePostgreSqlWriteProhibition;
}

public Set<String> getPostgreSqlDatabases() {
return postgreSqlDatabases;
}

public void setPostgreSqlDatabases(Set<String> postgreSqlDatabases) {
this.postgreSqlDatabases = postgreSqlDatabases;
}

public boolean isEnableOpenGaussWriteProhibition() {
return enableOpenGaussWriteProhibition;
}

public void setEnableOpenGaussWriteProhibition(boolean enableOpenGaussWriteProhibition) {
this.enableOpenGaussWriteProhibition = enableOpenGaussWriteProhibition;
}

public Set<String> getOpenGaussDatabases() {
return openGaussDatabases;
}

public void setDatabases(Set<String> databases) {
this.databases = databases;
public void setOpenGaussDatabases(Set<String> openGaussDatabases) {
this.openGaussDatabases = openGaussDatabases;
}

@Override
public String toString() {
return "enableDatabaseWriteProhibition=" + enableDatabaseWriteProhibition + ", databases=" + databases;
return "enableMongoDbWriteProhibition=" + enableMongoDbWriteProhibition
+ ", mongoDbDatabases=" + mongoDbDatabases + "; "
+ "enableMysqlWriteProhibition=" + enableMysqlWriteProhibition
+ ", mysqlDatabases=" + mysqlDatabases + "; "
+ "enablePostgreSqlWriteProhibition=" + enablePostgreSqlWriteProhibition
+ ", postgreSqlDatabases=" + postgreSqlDatabases + ";"
+ " enableOpenGaussWriteProhibition=" + enableOpenGaussWriteProhibition
+ ", openGaussDatabases=" + openGaussDatabases;
}
}
Loading

0 comments on commit 8249587

Please sign in to comment.