Skip to content

Commit

Permalink
Merge pull request #1458 from hanbingleixue/develop
Browse files Browse the repository at this point in the history
Fix the issue with testing the write prohibition function in OpenGauss and Postgresql databases
  • Loading branch information
Sherlockhan authored Mar 12, 2024
2 parents e508742 + be8491c commit d270273
Show file tree
Hide file tree
Showing 25 changed files with 297 additions and 728 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2023-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;

/**
* Thread tool class, used for thread data transfer
*
* @author zhp
* @since 2024-02-06
*/
public class ThreadDatabaseUrlUtil {
/**
* Thread variable storage, mainly storing database url information
*/
private static final ThreadLocal<String> DATABASE_URL_THREAD_LOCAL = new InheritableThreadLocal<>();

/**
* Constructor
*/
private ThreadDatabaseUrlUtil() {
}

/**
* Set database url information
*
* @param url Database url information
*/
public static void setDatabaseUrl(String url) {
DATABASE_URL_THREAD_LOCAL.set(url);
}

/**
* Get database url information
*
* @return Database url information
*/
public static String getDatabaseUrl() {
return DATABASE_URL_THREAD_LOCAL.get();
}

/**
* Remove database url information
*/
public static void removeDatabaseUrl() {
DATABASE_URL_THREAD_LOCAL.remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.huaweicloud.sermant.opengaussv30.utils.QueryExecutorImplEnhancementHelper;

/**
* SQL执行器增强声明器
* QueryExecutorImpl declarer
*
* @author zhp
* @since 2024-02-04
Expand All @@ -35,6 +35,6 @@ public ClassMatcher getClassMatcher() {

@Override
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) {
return new InterceptDeclarer[]{QueryExecutorImplEnhancementHelper.getSendQueryInterceptDeclarer()};
return new InterceptDeclarer[]{QueryExecutorImplEnhancementHelper.getSendOneQueryInterceptDeclarer()};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
import com.huaweicloud.sermant.database.handler.DatabaseHandler;
import com.huaweicloud.sermant.database.interceptor.AbstractDatabaseInterceptor;

import org.opengauss.core.ParameterList;
import org.opengauss.core.Query;
import org.opengauss.core.v3.QueryExecutorImpl;
import org.opengauss.util.HostSpec;

import java.util.logging.Logger;

/**
* 执行SQL操作的拦截器
* Interceptor for QueryExecutorImpl sendOneQuery method
*
* @author zhp
* @since 2024-02-04
Expand All @@ -40,15 +41,15 @@ public class QueryExecutorImplInterceptor extends AbstractDatabaseInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger();

/**
* 无参构造方法
* Non-parametric construction method
*/
public QueryExecutorImplInterceptor() {
}

/**
* 有参构造方法
* Parameterized construction method
*
* @param handler 写操作处理器
* @param handler Database write operation handler
*/
public QueryExecutorImplInterceptor(DatabaseHandler handler) {
this.handler = handler;
Expand All @@ -59,7 +60,7 @@ public ExecuteContext doBefore(ExecuteContext context) {
DatabaseInfo databaseInfo = getDataBaseInfo(context);
String database = databaseInfo.getDatabaseName();
Query query = (Query) context.getArguments()[0];
String sql = query.getNativeSql();
String sql = query.toString((ParameterList) context.getArguments()[1]);
handleWriteOperationIfWriteDisabled(sql, database,
DatabaseWriteProhibitionManager.getOpenGaussProhibitionDatabases(), context);
return context;
Expand All @@ -73,7 +74,7 @@ protected void createAndCacheDatabaseInfo(ExecuteContext context) {
databaseInfo.setDatabaseName(queryExecutor.getDatabase());
HostSpec hostSpec = queryExecutor.getHostSpec();
if (hostSpec == null) {
LOGGER.info("Unable to obtain the link address of the database.");
LOGGER.warning("Unable to obtain the link address of the database.");
return;
}
databaseInfo.setPort(hostSpec.getPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,67 +23,61 @@
import com.huaweicloud.sermant.opengaussv30.interceptors.QueryExecutorImplInterceptor;

/**
* postgresql拦截点辅助类
* Helper class for openGauss3.0.x
*
* @author zhp
* @since 2024-02-04
**/
public class QueryExecutorImplEnhancementHelper {
private static final String EXECUTE_METHOD_NAME = "sendQuery";
private static final String EXECUTE_METHOD_NAME = "sendOneQuery";

private static final String ENHANCE_CLASS_NAME = "org.opengauss.core.v3.QueryExecutorImpl";

private static final String INT_CLASS_NAME = "int";

private static final String QUERY_CLASS_NAME = "org.opengauss.core.Query";
private static final String SIMPLE_QUERY_CLASS_NAME = "org.opengauss.core.v3.SimpleQuery";

private static final String V3_PARAMETER_LIST_CLASS_NAME = "org.opengauss.core.v3.V3ParameterList";

private static final String RESULT_HANDLER_CLASS_NAME = "org.opengauss.core.ResultHandler";

private static final String BATCH_RESULT_HANDLER_CLASS_NAME = "org.opengauss.jdbc.BatchResultHandler";
private static final String SIMPLE_PARAMETER_LIST_CLASS_NAME = "org.opengauss.core.v3.SimpleParameterList";

private static final String[] EXECUTE_INTERNAL_METHOD_PARAMS_TYPE = {
QUERY_CLASS_NAME,
V3_PARAMETER_LIST_CLASS_NAME,
INT_CLASS_NAME,
SIMPLE_QUERY_CLASS_NAME,
SIMPLE_PARAMETER_LIST_CLASS_NAME,
INT_CLASS_NAME,
INT_CLASS_NAME,
RESULT_HANDLER_CLASS_NAME,
BATCH_RESULT_HANDLER_CLASS_NAME
INT_CLASS_NAME
};

private QueryExecutorImplEnhancementHelper() {
}

private static MethodMatcher getSendQueryMethodMatcher() {
private static MethodMatcher getSendOneQueryMethodMatcher() {
return MethodMatcher.nameEquals(EXECUTE_METHOD_NAME)
.and(MethodMatcher.paramTypesEqual(EXECUTE_INTERNAL_METHOD_PARAMS_TYPE));
}

/**
* 获取QueryExecutorImpl sendQuery方法有参拦截声明器
* Get the parameterized interceptor declarer for the QueryExecutorImpl sendOneQuery method
*
* @param handler 数据库自定义处理器
* @return InterceptDeclarer QueryExecutorImpl sendQuery方法有参拦截声明器
* @param handler Database write operation handler
* @return InterceptDeclarer The parameterized interceptor declarer for the QueryExecutorImpl sendOneQuery method
*/
public static InterceptDeclarer getSendQueryInterceptDeclarer(DatabaseHandler handler) {
return InterceptDeclarer.build(getSendQueryMethodMatcher(), new QueryExecutorImplInterceptor(handler));
public static InterceptDeclarer getSendOneQueryInterceptDeclarer(DatabaseHandler handler) {
return InterceptDeclarer.build(getSendOneQueryMethodMatcher(), new QueryExecutorImplInterceptor(handler));
}

/**
* 获取QueryExecutorImpl sendQuery方法无参拦截声明器
* Get the non-parameter interceptor declarer for the QueryExecutorImpl sendOneQuery method
*
* @return InterceptDeclarer QueryExecutorImpl sendQuery方法无参拦截声明器
* @return InterceptDeclarer The non-parameter interceptor declarer for the QueryExecutorImpl sendOneQuery method
*/
public static InterceptDeclarer getSendQueryInterceptDeclarer() {
return InterceptDeclarer.build(getSendQueryMethodMatcher(), new QueryExecutorImplInterceptor());
public static InterceptDeclarer getSendOneQueryInterceptDeclarer() {
return InterceptDeclarer.build(getSendOneQueryMethodMatcher(), new QueryExecutorImplInterceptor());
}

/**
* 获取QueryExecutorImpl类的ClassMatcher
* Get the ClassMatcher of the QueryExecutorImpl class
*
* @return ClassMatcher 类匹配器
* @return ClassMatcher Class matcher
*/
public static ClassMatcher getQueryExecutorImplClassMatcher() {
return ClassMatcher.nameEquals(ENHANCE_CLASS_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,41 +78,33 @@ public void testDoBefore() throws Exception {
queryExecutorImplInterceptor.before(context);
Assert.assertNull(context.getThrowableOut());

/*
* The database write prohibition switch is turned off, and the write prohibition database set contains the
* intercepted database
*/
// The database write prohibition switch is turned off, and the write prohibition database set contains the
// intercepted database
Set<String> databases = new HashSet<>();
databases.add("database-test");
GLOBAL_CONFIG.setOpenGaussDatabases(databases);
queryExecutorImplInterceptor.before(context);
Assert.assertNull(context.getThrowableOut());

/*
* The database write prohibition switch is turned on, and the write prohibition database collection contains
* the intercepted databases
*/
// The database write prohibition switch is turned on, and the write prohibition database collection contains
// the intercepted databases
GLOBAL_CONFIG.setEnableOpenGaussWriteProhibition(true);
context = ExecuteContext.forMemberMethod(queryExecutor, methodMock, argument, null, null);
queryExecutorImplInterceptor.before(context);
Assert.assertEquals("Database prohibit to write, database: database-test",
context.getThrowableOut().getMessage());

/*
* The database write prohibition switch is turned on, and the write prohibition database collection contains
* the intercepted database. SQL does not perform write operations
*/
// The database write prohibition switch is turned on, and the write prohibition database collection contains
// the intercepted database. SQL does not perform write operations
Query readQuery = new BatchedQuery(new NativeQuery(READ_SQL, null),
null, 0, 0, false);
context = ExecuteContext.forMemberMethod(queryExecutor, methodMock,
new Object[]{readQuery, null, null, null, null, null, null}, null, null);
queryExecutorImplInterceptor.before(context);
Assert.assertNull(context.getThrowableOut());

/*
* The database write prohibition switch is turned on, and the write prohibition database collection does not
* contain the intercepted database
*/
// The database write prohibition switch is turned on, and the write prohibition database collection does not
// contain the intercepted database
GLOBAL_CONFIG.setOpenGaussDatabases(new HashSet<>());
context = ExecuteContext.forMemberMethod(queryExecutor, methodMock, argument, null, null);
queryExecutorImplInterceptor.before(context);
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit d270273

Please sign in to comment.