This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 8b1f85efa06 Remove route to the specified database with Hint (#18620)
(#18725)
8b1f85efa06 is described below
commit 8b1f85efa061c3d70c326f02cb4ef5cee8dd4f27
Author: Cheng Zhang <[email protected]>
AuthorDate: Thu Jun 30 16:16:32 2022 +0800
Remove route to the specified database with Hint (#18620) (#18725)
---
.../special-api/sharding/hint.cn.md | 42 ---------
.../special-api/sharding/hint.en.md | 41 ---------
.../statement/CommonSQLStatementContext.java | 10 --
.../shardingsphere/infra/hint/HintManager.java | 14 ---
.../route/engine/impl/PartialSQLRouteExecutor.java | 23 -----
.../route/engine/PartialSQLRouteExecutorTest.java | 101 ---------------------
6 files changed, 231 deletions(-)
diff --git
a/docs/document/content/user-manual/shardingsphere-jdbc/special-api/sharding/hint.cn.md
b/docs/document/content/user-manual/shardingsphere-jdbc/special-api/sharding/hint.cn.md
index c208e1b752d..07e898a0fcb 100644
---
a/docs/document/content/user-manual/shardingsphere-jdbc/special-api/sharding/hint.cn.md
+++
b/docs/document/content/user-manual/shardingsphere-jdbc/special-api/sharding/hint.cn.md
@@ -142,45 +142,3 @@ SQL Hint 功能需要用户提前开启解析注释的配置,设置 `sqlCommen
/* ShardingSphere hint: writeRouteOnly=true */
SELECT * FROM t_order;
```
-
-### 使用 Hint 路由至指定数据库
-
-#### 使用手动编程的方式
-
-##### 获取 HintManager
-
-与基于 Hint 的数据分片相同。
-
-##### 设置路由至指定数据库
-
-- 使用 `hintManager.setDataSourceName` 设置数据库名称。
-
-##### 完整代码示例
-
-```java
-String sql = "SELECT * FROM t_order";
-try (HintManager hintManager = HintManager.getInstance();
- Connection conn = dataSource.getConnection();
- PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
- hintManager.setDataSourceName("ds_0");
- try (ResultSet rs = preparedStatement.executeQuery()) {
- while (rs.next()) {
- // ...
- }
- }
-}
-```
-
-#### 使用 SQL 注释的方式
-
-##### 使用规范
-
-SQL Hint 功能需要用户提前开启解析注释的配置,设置 `sqlCommentParseEnabled` 为 `true`,目前只支持路由至一个数据源。
-注释格式暂时只支持 `/* */`,内容需要以 `ShardingSphere hint:` 开始,属性名为 `dataSourceName`。
-如果使用 `MySQL` 客户端连接需要添加 `-c` 选项保留注释,客户端默认是 `--skip-comments` 过滤注释。
-
-##### 完整示例
-```sql
-/* ShardingSphere hint: dataSourceName=ds_0 */
-SELECT * FROM t_order;
-```
diff --git
a/docs/document/content/user-manual/shardingsphere-jdbc/special-api/sharding/hint.en.md
b/docs/document/content/user-manual/shardingsphere-jdbc/special-api/sharding/hint.en.md
index 66d38ec48c9..b169bd7f30c 100644
---
a/docs/document/content/user-manual/shardingsphere-jdbc/special-api/sharding/hint.en.md
+++
b/docs/document/content/user-manual/shardingsphere-jdbc/special-api/sharding/hint.en.md
@@ -142,44 +142,3 @@ The comment format only supports `/* */` for now. The
content needs to start wit
/* ShardingSphere hint: writeRouteOnly=true */
SELECT * FROM t_order;
```
-### Route to the specified database with Hint
-
-#### Use manual programming
-
-##### Get HintManager
-
-Be the same as sharding based on hint.
-
-##### Configure Database Route
-
-- Use `hintManager.setDataSourceName` to configure database route.
-
-##### Codes:
-
-```java
-String sql = "SELECT * FROM t_order";
-try (HintManager hintManager = HintManager.getInstance();
- Connection conn = dataSource.getConnection();
- PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
- hintManager.setDataSourceName("ds_0");
- try (ResultSet rs = preparedStatement.executeQuery()) {
- while (rs.next()) {
- // ...
- }
- }
-}
-```
-
-#### Use special SQL comments
-
-##### Terms of Use
-
-To use SQL Hint function, users need to set `sqlCommentParseEnabled` to
`true`. Currently, only support routing to one data source.
-The comment format only supports `/* */` for now. The content needs to start
with `ShardingSphere hint:`, and the attribute name needs to be
`dataSourceName`.
-Client connections using `MySQL` need to add the `-c` option to preserve
comments, because the client defaults to `--skip-comments` to filter comments.
-
-##### Codes:
-```sql
-/* ShardingSphere hint: dataSourceName=ds_0 */
-SELECT * FROM t_order;
-```
\ No newline at end of file
diff --git
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/CommonSQLStatementContext.java
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/CommonSQLStatementContext.java
index 97070ab9398..199bf3ae846 100644
---
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/CommonSQLStatementContext.java
+++
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/CommonSQLStatementContext.java
@@ -31,7 +31,6 @@ import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.SQL92Sta
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.SQLServerStatement;
import java.util.Collections;
-import java.util.Optional;
/**
* Common SQL statement context.
@@ -78,15 +77,6 @@ public class CommonSQLStatementContext<T extends
SQLStatement> implements SQLSta
throw new
UnsupportedOperationException(sqlStatement.getClass().getName());
}
- /**
- * Find hint data source name.
- *
- * @return dataSource name
- */
- public Optional<String> findHintDataSourceName() {
- return sqlHintExtractor.findHintDataSourceName();
- }
-
/**
* Judge whether is hint routed to write data source or not.
*
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/HintManager.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/HintManager.java
index 2e3b8e98ba7..6d1e7a3edd1 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/HintManager.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/HintManager.java
@@ -22,11 +22,9 @@ import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import lombok.Setter;
import java.util.Collection;
import java.util.Collections;
-import java.util.Optional;
/**
* The manager that use hint to inject sharding key directly through {@code
ThreadLocal}.
@@ -44,9 +42,6 @@ public final class HintManager implements AutoCloseable {
private boolean writeRouteOnly;
- @Setter
- private String dataSourceName;
-
/**
* Get a new instance for {@code HintManager}.
*
@@ -191,15 +186,6 @@ public final class HintManager implements AutoCloseable {
return null != HINT_MANAGER_HOLDER.get();
}
- /**
- * Get data source name.
- *
- * @return dataSource name
- */
- public static Optional<String> getDataSourceName() {
- return Optional.ofNullable(HINT_MANAGER_HOLDER.get()).map(optional ->
optional.dataSourceName);
- }
-
@Override
public void close() {
clear();
diff --git
a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java
b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java
index 95f19aa9fce..7d762d65352 100644
---
a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java
+++
b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java
@@ -18,11 +18,7 @@
package org.apache.shardingsphere.infra.route.engine.impl;
import org.apache.shardingsphere.infra.binder.LogicSQL;
-import
org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
-import org.apache.shardingsphere.infra.hint.HintManager;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.SQLRouter;
import org.apache.shardingsphere.infra.route.SQLRouterFactory;
@@ -32,12 +28,10 @@ import
org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.infra.route.engine.SQLRouteExecutor;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import javax.sql.DataSource;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Optional;
/**
* Partial SQL route executor.
@@ -58,11 +52,6 @@ public final class PartialSQLRouteExecutor implements
SQLRouteExecutor {
@SuppressWarnings({"unchecked", "rawtypes"})
public RouteContext route(final LogicSQL logicSQL, final
ShardingSphereDatabase database) {
RouteContext result = new RouteContext();
- Optional<String> dataSourceName =
findDataSourceByHint(logicSQL.getSqlStatementContext(),
database.getResource().getDataSources());
- if (dataSourceName.isPresent()) {
- result.getRouteUnits().add(new RouteUnit(new
RouteMapper(dataSourceName.get(), dataSourceName.get()),
Collections.emptyList()));
- return result;
- }
for (Entry<ShardingSphereRule, SQLRouter> entry : routers.entrySet()) {
if (result.getRouteUnits().isEmpty()) {
result = entry.getValue().createRouteContext(logicSQL,
database, entry.getKey(), props);
@@ -77,16 +66,4 @@ public final class PartialSQLRouteExecutor implements
SQLRouteExecutor {
return result;
}
- private Optional<String> findDataSourceByHint(final SQLStatementContext<?>
sqlStatementContext, final Map<String, DataSource> dataSources) {
- Optional<String> result;
- if (HintManager.isInstantiated() &&
HintManager.getDataSourceName().isPresent()) {
- result = HintManager.getDataSourceName();
- } else {
- result = ((CommonSQLStatementContext<?>)
sqlStatementContext).findHintDataSourceName();
- }
- if (result.isPresent() && !dataSources.containsKey(result.get())) {
- throw new ShardingSphereException("Hint datasource: %s is not
exist!", result.get());
- }
- return result;
- }
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/PartialSQLRouteExecutorTest.java
b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/PartialSQLRouteExecutorTest.java
deleted file mode 100644
index 0eb316f47b0..00000000000
---
a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/PartialSQLRouteExecutorTest.java
+++ /dev/null
@@ -1,101 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.shardingsphere.infra.route.engine;
-
-import org.apache.shardingsphere.infra.binder.LogicSQL;
-import
org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
-import org.apache.shardingsphere.infra.hint.HintManager;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import
org.apache.shardingsphere.infra.route.engine.impl.PartialSQLRouteExecutor;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Answers;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import javax.sql.DataSource;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
-public final class PartialSQLRouteExecutorTest {
-
- private final PartialSQLRouteExecutor partialSQLRouteExecutor = new
PartialSQLRouteExecutor(Collections.emptyList(), new
ConfigurationProperties(new Properties()));
-
- @Mock(answer = Answers.RETURNS_DEEP_STUBS)
- private ShardingSphereDatabase database;
-
- @Mock
- private CommonSQLStatementContext<AbstractSQLStatement>
commonSQLStatementContext;
-
- @Before
- public void setup() {
- Map<String, DataSource> dataSourceMap = new HashMap<>();
- dataSourceMap.put("ds_0", null);
- dataSourceMap.put("ds_1", null);
-
when(database.getResource().getDataSources()).thenReturn(dataSourceMap);
- }
-
- @Test
- public void assertRouteBySQLCommentHint() {
-
when(commonSQLStatementContext.findHintDataSourceName()).thenReturn(Optional.of("ds_1"));
- LogicSQL logicSQL = new LogicSQL(commonSQLStatementContext, "",
Collections.emptyList());
- RouteContext routeContext = partialSQLRouteExecutor.route(logicSQL,
database);
- assertThat(routeContext.getRouteUnits().size(), is(1));
-
assertThat(routeContext.getRouteUnits().iterator().next().getDataSourceMapper().getActualName(),
is("ds_1"));
- }
-
- @Test
- public void assertRouteByHintManagerHint() {
- try (HintManager hintManager = HintManager.getInstance()) {
- hintManager.setDataSourceName("ds_1");
- LogicSQL logicSQL = new LogicSQL(commonSQLStatementContext, "",
Collections.emptyList());
- RouteContext routeContext =
partialSQLRouteExecutor.route(logicSQL, database);
- assertThat(routeContext.getRouteUnits().size(), is(1));
-
assertThat(routeContext.getRouteUnits().iterator().next().getDataSourceMapper().getActualName(),
is("ds_1"));
- }
- }
-
- @Test(expected = ShardingSphereException.class)
- public void assertRouteBySQLCommentHintWithException() {
-
when(commonSQLStatementContext.findHintDataSourceName()).thenReturn(Optional.of("ds_3"));
- LogicSQL logicSQL = new LogicSQL(commonSQLStatementContext, "",
Collections.emptyList());
- partialSQLRouteExecutor.route(logicSQL, database);
- }
-
- @Test(expected = ShardingSphereException.class)
- public void assertRouteByHintManagerHintWithException() {
- try (HintManager hintManager = HintManager.getInstance()) {
- hintManager.setDataSourceName("ds-3");
- LogicSQL logicSQL = new LogicSQL(commonSQLStatementContext, "",
Collections.emptyList());
- partialSQLRouteExecutor.route(logicSQL, database);
- }
- }
-}