Skip to content

Commit

Permalink
mysql数据库禁写和数据库实例信息获取
Browse files Browse the repository at this point in the history
Signed-off-by: daizhenyu <[email protected]>
  • Loading branch information
daizhenyu committed Feb 5, 2024
1 parent bfdb51a commit 882d952
Show file tree
Hide file tree
Showing 31 changed files with 1,501 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package com.huaweicloud.sermant.database.controller;

import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* 数据库控制器
Expand All @@ -27,6 +30,8 @@
* @since 2024-01-15
**/
public class DatabaseController {
private static final Logger LOGGER = LoggerFactory.getLogger();

private static Object result = new Object();

private DatabaseController() {
Expand All @@ -42,5 +47,6 @@ private DatabaseController() {
public static void disableDatabaseWriteOperation(String database, ExecuteContext context) {
context.setThrowableOut(new SQLException("Database prohibit to write, database: " + database));
context.skip(result);
LOGGER.log(Level.FINE, "Database prohibit to write, database: {0}", database);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.entity;

/**
* 数据库示例信息
*
* @author daizhenyu
* @since 2024-02-01
**/
public class DatabaseInfo {
private String databaseName;

private String hostAddress;

private int port;

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getDatabaseName() {
return databaseName;
}

public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}

public String getHostAddress() {
return hostAddress;
}

public void setHostAddress(String hostAddress) {
this.hostAddress = hostAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,25 @@
import com.huaweicloud.sermant.database.handler.DatabaseHandler;

/**
* mongodb抽象interceptor
* 数据库抽象interceptor
*
* @author daizhenyu
* @since 2024-01-22
**/
public abstract class AbstractDatabaseInterceptor extends AbstractInterceptor {
/**
* 数据库信息存储的key
*/
protected static final String DATABASE_INFO = "databaseInfo";

/**
* 自定义数据库处理handle
*/
protected DatabaseHandler handler;

@Override
public ExecuteContext before(ExecuteContext context) throws Exception {
createAndCacheDatabaseInfo(context);
if (handler != null) {
handler.doBefore(context);
return context;
Expand Down Expand Up @@ -63,4 +72,11 @@ public ExecuteContext onThrow(ExecuteContext context) throws Exception {
* @return ExecuteContext 上下文
*/
protected abstract ExecuteContext doBefore(ExecuteContext context);

/**
* 创建数据库实例对象并缓存在上下文的本地局部属性集中
*
* @param context 上下文
*/
protected abstract void createAndCacheDatabaseInfo(ExecuteContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.utils;

import com.huaweicloud.sermant.core.utils.StringUtils;

import java.util.regex.Pattern;

/**
* sql解析工具类
*
* @author daizhenyu
* @since 2024-01-25
**/
public class SqlParserUtils {
private static Pattern writePattern =
Pattern.compile("INSERT\\b|UPDATE\\b|DELETE\\b|CREATE\\b|ALTER\\b|DROP\\b|TRUNCATE\\b",
Pattern.CASE_INSENSITIVE);

private SqlParserUtils() {
}

/**
* 解析sql并判断是否为写操作
*
* @param sql sql语句
* @return 是否为写操作
*/
public static boolean isWriteOperation(String sql) {
if (StringUtils.isEmpty(sql)) {
return false;
}
return writePattern.matcher(sql).find();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.mongodb.interceptors;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.database.config.DatabaseWriteProhibitionManager;
import com.huaweicloud.sermant.database.controller.DatabaseController;
import com.huaweicloud.sermant.database.entity.DatabaseInfo;
import com.huaweicloud.sermant.database.interceptor.AbstractDatabaseInterceptor;

/**
* mongodb抽象interceptor
*
* @author daizhenyu
* @since 2024-02-02
**/
public abstract class AbstractMongoDbInterceptor extends AbstractDatabaseInterceptor {
@Override
public ExecuteContext doBefore(ExecuteContext context) {
String database = ((DatabaseInfo) context.getLocalFieldValue(DATABASE_INFO)).getDatabaseName();
if (DatabaseWriteProhibitionManager.getMongoDbProhibitionDatabases().contains(database)) {
DatabaseController.disableDatabaseWriteOperation(database, context);
}
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@
package com.huaweicloud.sermant.mongodb.interceptors;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.database.config.DatabaseWriteProhibitionManager;
import com.huaweicloud.sermant.database.controller.DatabaseController;
import com.huaweicloud.sermant.database.entity.DatabaseInfo;
import com.huaweicloud.sermant.database.handler.DatabaseHandler;
import com.huaweicloud.sermant.database.interceptor.AbstractDatabaseInterceptor;

import com.mongodb.ServerAddress;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.internal.connection.Connection;

/**
* 执行同步和异步write操作的拦截器
* 执行executeCommand方法的拦截器
*
* @author daizhenyu
* @since 2024-01-18
**/
public class ExecuteCommandInterceptor extends AbstractDatabaseInterceptor {
public class ExecuteCommandInterceptor extends AbstractMongoDbInterceptor {
private static final int PARAM_INDEX = 5;

/**
* 无参构造方法
*/
Expand All @@ -45,11 +49,16 @@ public ExecuteCommandInterceptor(DatabaseHandler handler) {
}

@Override
public ExecuteContext doBefore(ExecuteContext context) {
String database = (String) context.getArguments()[0];
if (DatabaseWriteProhibitionManager.getMongoDbProhibitionDatabases().contains(database)) {
DatabaseController.disableDatabaseWriteOperation(database, context);
protected void createAndCacheDatabaseInfo(ExecuteContext context) {
DatabaseInfo info = new DatabaseInfo();
context.setLocalFieldValue(DATABASE_INFO, info);
info.setDatabaseName((String) context.getArguments()[0]);
Connection connection = (Connection) context.getArguments()[PARAM_INDEX];
ConnectionDescription description = connection.getDescription();
if (description != null) {
ServerAddress serverAddress = description.getServerAddress();
info.setHostAddress(serverAddress.getHost());
info.setPort(serverAddress.getPort());
}
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@
package com.huaweicloud.sermant.mongodb.interceptors;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.database.config.DatabaseWriteProhibitionManager;
import com.huaweicloud.sermant.database.controller.DatabaseController;
import com.huaweicloud.sermant.database.entity.DatabaseInfo;
import com.huaweicloud.sermant.database.handler.DatabaseHandler;
import com.huaweicloud.sermant.database.interceptor.AbstractDatabaseInterceptor;

import com.mongodb.ServerAddress;
import com.mongodb.internal.binding.WriteBinding;

/**
* 执行重试write操作的拦截器
*
* @author daizhenyu
* @since 2024-01-18
**/
public class ExecuteRetryableCommandInterceptor extends AbstractDatabaseInterceptor {
public class ExecuteRetryableCommandInterceptor extends AbstractMongoDbInterceptor {
/**
* 无参构造方法
*/
Expand All @@ -45,11 +46,15 @@ public ExecuteRetryableCommandInterceptor(DatabaseHandler handler) {
}

@Override
public ExecuteContext doBefore(ExecuteContext context) {
String database = (String) context.getArguments()[1];
if (DatabaseWriteProhibitionManager.getMongoDbProhibitionDatabases().contains(database)) {
DatabaseController.disableDatabaseWriteOperation(database, context);
}
return context;
protected void createAndCacheDatabaseInfo(ExecuteContext context) {
DatabaseInfo info = new DatabaseInfo();
context.setLocalFieldValue(DATABASE_INFO, info);
ServerAddress serverAddress = ((WriteBinding) context.getArguments()[0])
.getWriteConnectionSource()
.getServerDescription()
.getAddress();
info.setDatabaseName((String) context.getArguments()[1]);
info.setHostAddress(serverAddress.getHost());
info.setPort(serverAddress.getPort());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.mongodb.interceptors;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.database.entity.DatabaseInfo;
import com.huaweicloud.sermant.database.handler.DatabaseHandler;

import com.mongodb.ServerAddress;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.internal.connection.Connection;

/**
* 执行executeWriteCommand方法的拦截器
*
* @author daizhenyu
* @since 2024-01-18
**/
public class ExecuteWriteCommandInterceptor extends AbstractMongoDbInterceptor {
private static final int PARAM_INDEX = 4;

/**
* 无参构造方法
*/
public ExecuteWriteCommandInterceptor() {
}

/**
* 有参构造方法
*
* @param handler 写操作处理器
*/
public ExecuteWriteCommandInterceptor(DatabaseHandler handler) {
this.handler = handler;
}

@Override
protected void createAndCacheDatabaseInfo(ExecuteContext context) {
DatabaseInfo info = new DatabaseInfo();
context.setLocalFieldValue(DATABASE_INFO, info);
info.setDatabaseName((String) context.getArguments()[0]);
Connection connection = (Connection) context.getArguments()[PARAM_INDEX];
ConnectionDescription description = connection.getDescription();
if (description != null) {
ServerAddress serverAddress = description.getServerAddress();
info.setHostAddress(serverAddress.getHost());
info.setPort(serverAddress.getPort());
}
}
}
Loading

0 comments on commit 882d952

Please sign in to comment.