Skip to content

Commit da7ac3b

Browse files
committed
Use SentinelOperations in Druid filter
Refactor SentinelDruidFilter to use SentinelOperations/SentinelTemplate and SentinelContext instead of custom SQL parsing and SentinelUtils. Introduces CONTEXT_NAME and ORIGIN_NAME constants, begins context in beforeExecute, and removes context/set result/failure and ends via sentinelOperations in afterExecute. Adds a unit test (SentinelDruidFilterTest) that wires the filter into a DruidDataSource and adds test dependencies (microsphere-alibaba-druid-test and H2) to the module pom.
1 parent e236e3b commit da7ac3b

3 files changed

Lines changed: 70 additions & 87 deletions

File tree

microsphere-sentinel-plugins/microsphere-sentinel-alibaba-druid/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@
6161
<scope>test</scope>
6262
</dependency>
6363

64+
<!-- Microsphere Alibaba Druid Test -->
65+
<dependency>
66+
<groupId>io.github.microsphere-projects</groupId>
67+
<artifactId>microsphere-alibaba-druid-test</artifactId>
68+
<scope>test</scope>
69+
</dependency>
70+
71+
<!-- H2 -->
72+
<dependency>
73+
<groupId>com.h2database</groupId>
74+
<artifactId>h2</artifactId>
75+
<scope>test</scope>
76+
</dependency>
77+
6478
</dependencies>
6579

6680
</project>

microsphere-sentinel-plugins/microsphere-sentinel-alibaba-druid/src/main/java/io/microsphere/sentinel/alibaba/druid/SentinelDruidFilter.java

Lines changed: 15 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,12 @@
1919
import com.alibaba.druid.filter.Filter;
2020
import com.alibaba.druid.filter.FilterAdapter;
2121
import com.alibaba.druid.proxy.jdbc.StatementProxy;
22-
import com.alibaba.druid.sql.ast.SQLStatement;
23-
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
24-
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
25-
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
26-
import com.alibaba.druid.sql.ast.statement.SQLSelect;
27-
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
28-
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
29-
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
30-
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
3122
import io.microsphere.alibaba.druid.filter.AbstractStatementFilter;
32-
import io.microsphere.logging.Logger;
33-
import io.microsphere.sentinel.util.SentinelUtils;
23+
import io.microsphere.sentinel.common.SentinelContext;
24+
import io.microsphere.sentinel.common.SentinelOperations;
25+
import io.microsphere.sentinel.common.SentinelTemplate;
3426

35-
import java.sql.SQLException;
36-
import java.util.List;
37-
import java.util.Objects;
38-
import java.util.concurrent.Callable;
39-
40-
import static com.alibaba.druid.sql.SQLUtils.parseStatements;
41-
import static io.microsphere.logging.LoggerFactory.getLogger;
27+
import static io.microsphere.sentinel.common.SentinelContext.removeContext;
4228

4329
/**
4430
* Sentinel x Druid {@link Filter}
@@ -50,81 +36,23 @@
5036
*/
5137
public class SentinelDruidFilter extends AbstractStatementFilter {
5238

53-
private static final Logger logger = getLogger(SentinelDruidFilter.class);
39+
static final String CONTEXT_NAME = "microsphere_sentinel_jdbc_context";
40+
41+
static final String ORIGIN_NAME = "Statement";
42+
43+
private final SentinelOperations sentinelOperations = new SentinelTemplate();
5444

5545
@Override
5646
protected void beforeExecute(StatementProxy statement, String resourceName) throws Throwable {
57-
47+
SentinelContext context = this.sentinelOperations.begin(resourceName, CONTEXT_NAME, ORIGIN_NAME);
48+
context.setContext();
5849
}
5950

6051
@Override
6152
protected void afterExecute(StatementProxy statement, String resourceName, Object result, Throwable failure) {
62-
63-
}
64-
65-
protected <T> T doInSentinel(StatementProxy statement, Callable<T> callable) throws SQLException {
66-
String resourceName = getSentinelResourceName(statement);
67-
return SentinelUtils.doInSentinel(resourceName, "sentinel_microsphere_jdbc_context", "Statement", callable, SQLException.class);
68-
}
69-
70-
private String getSentinelResourceName(StatementProxy statement) {
71-
String sql = statement.getLastExecuteSql();
72-
if (Objects.equals(sql, validationSQL)) {
73-
return sql;
74-
}
75-
String dbType = dataSource.getDbType();
76-
List<SQLStatement> statementList = parseStatements(sql, dbType);
77-
String resourceName = null;
78-
if (statementList.size() > 0) {
79-
SQLStatement sqlStatement = statementList.get(0);
80-
resourceName = getSentinelResourceName(sqlStatement);
81-
}
82-
if (resourceName == null) {
83-
logger.debug("The JDBC statement can't be recognized, sql : '{}' , dbType : '{}'", sql, dbType);
84-
resourceName = "UNRECOGNIZED";
85-
}
86-
return resourceName;
87-
}
88-
89-
private String getSentinelResourceName(SQLStatement sqlStatement) {
90-
try {
91-
if (sqlStatement instanceof SQLSelectStatement) {
92-
return getSentinelResourceName((SQLSelectStatement) sqlStatement);
93-
} else if (sqlStatement instanceof SQLUpdateStatement) {
94-
return getSentinelResourceName((SQLUpdateStatement) sqlStatement);
95-
} else if (sqlStatement instanceof SQLInsertStatement) {
96-
return getSentinelResourceName((SQLInsertStatement) sqlStatement);
97-
} else if (sqlStatement instanceof SQLDeleteStatement) {
98-
return getSentinelResourceName((SQLDeleteStatement) sqlStatement);
99-
}
100-
} catch (Throwable e) {
101-
logger.debug("The JDBC statement can't be parsed, sql : '{}'", sqlStatement, e);
102-
}
103-
return null;
104-
}
105-
106-
private String getSentinelResourceName(SQLSelectStatement selectStatement) {
107-
SQLSelect sqlSelect = selectStatement.getSelect();
108-
SQLSelectQueryBlock sqlSelectQueryBlock = sqlSelect.getFirstQueryBlock();
109-
if (sqlSelectQueryBlock == null) {
110-
return null;
111-
}
112-
SQLTableSource sqlTableSource = sqlSelectQueryBlock.getFrom();
113-
return "SELECT " + sqlTableSource.computeAlias();
114-
}
115-
116-
private String getSentinelResourceName(SQLUpdateStatement updateStatement) {
117-
SQLTableSource sqlTableSource = updateStatement.getFrom();
118-
return "UPDATE " + sqlTableSource.computeAlias();
119-
}
120-
121-
private String getSentinelResourceName(SQLInsertStatement insertStatement) {
122-
SQLExprTableSource sqlTableSource = insertStatement.getTableSource();
123-
return "INSERT " + sqlTableSource.computeAlias();
124-
}
125-
126-
private String getSentinelResourceName(SQLDeleteStatement deleteStatement) {
127-
SQLTableSource sqlTableSource = deleteStatement.getTableSource();
128-
return "DELETE " + sqlTableSource.computeAlias();
53+
SentinelContext context = removeContext();
54+
context.setResult(result);
55+
context.setFailure(failure);
56+
this.sentinelOperations.end(context);
12957
}
13058
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.sentinel.alibaba.druid;
19+
20+
21+
import com.alibaba.druid.pool.DruidDataSource;
22+
import io.microsphere.alibaba.druid.test.AbstractAlibabaDruidTest;
23+
24+
import static java.util.Arrays.asList;
25+
26+
/**
27+
* {@link SentinelDruidFilter} Testt
28+
*
29+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
30+
* @see SentinelDruidFilter
31+
* @see AbstractAlibabaDruidTest
32+
* @since 1.0.0
33+
*/
34+
class SentinelDruidFilterTest extends AbstractAlibabaDruidTest {
35+
36+
@Override
37+
protected void customize(DruidDataSource dataSource) {
38+
SentinelDruidFilter sentinelDruidFilter = new SentinelDruidFilter();
39+
dataSource.setProxyFilters(asList(sentinelDruidFilter));
40+
}
41+
}

0 commit comments

Comments
 (0)