This is an automated email from the ASF dual-hosted git repository.
jiangmaolin 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 ee6734330ae Add InUsedStorageUnitRetriever for single rule (#35131)
ee6734330ae is described below
commit ee6734330ae64fb7a226375a7ae1516c295a5f6e
Author: Raigor <[email protected]>
AuthorDate: Sun Apr 6 20:35:58 2025 +0800
Add InUsedStorageUnitRetriever for single rule (#35131)
* Add InUsedStorageUnitRetriever for single rule
* Fix result name in InUsedSingleStorageUnitRetriever
* Update RELEASE-NOTES.md
---
RELEASE-NOTES.md | 3 +-
...UsedReadwriteSplittingStorageUnitRetriever.java | 8 +---
...ReadwriteSplittingStorageUnitRetrieverTest.java | 7 ---
.../query/InUsedShadowStorageUnitRetriever.java | 8 +---
.../InUsedShadowStorageUnitRetrieverTest.java | 7 ---
.../resource/ShowRulesUsedStorageUnitExecutor.java | 3 +-
.../query/InUsedSingleStorageUnitRetriever.java | 53 ++++++++++++++++++++++
...xecutor.rql.resource.InUsedStorageUnitRetriever | 18 ++++++++
.../InUsedSingleStorageUnitRetrieverTest.java | 42 +++++++----------
.../ShowRulesUsedStorageUnitStatement.java | 13 +-----
10 files changed, 96 insertions(+), 66 deletions(-)
diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index 940c7f6b5dc..49f7c9b580a 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -29,7 +29,8 @@
1. Encrypt: Add quotes to encrypt rewrite derived columns -
[#34950](https://github.com/apache/shardingsphere/pull/34950)
1. SQL Router: Add check for select with union all routing to multi data
sources - [#35037](https://github.com/apache/shardingsphere/pull/35037)
1. DistSQL: Add job sharding nodes to the result set of `SHOW MIGRATION LIST`
- [#35053](https://github.com/apache/shardingsphere/pull/35053)
-2. SQL Parser: Enhance combineType in Oracle to support EXCEPT ALL and
INTERSECT ALL - [#35099](https://github.com/apache/shardingsphere/pull/35099)
+1. SQL Parser: Enhance combineType in Oracle to support EXCEPT ALL and
INTERSECT ALL - [#35099](https://github.com/apache/shardingsphere/pull/35099)
+1. DistSQL: Add InUsedStorageUnitRetriever for single rule -
[#35131](https://github.com/apache/shardingsphere/pull/35131)
### Bug Fixes
diff --git
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/InUsedReadwriteSplittingStorageUnitRetriever.java
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/InUsedReadwriteSplittingStorageUnitRetriever.java
index 898ade8f2d5..f3a34ef8873 100644
---
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/InUsedReadwriteSplittingStorageUnitRetriever.java
+++
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/InUsedReadwriteSplittingStorageUnitRetriever.java
@@ -24,7 +24,6 @@ import
org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplitti
import
org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashSet;
/**
@@ -34,15 +33,12 @@ public final class
InUsedReadwriteSplittingStorageUnitRetriever implements InUse
@Override
public Collection<String> getInUsedResources(final
ShowRulesUsedStorageUnitStatement sqlStatement, final ReadwriteSplittingRule
rule) {
- if (!sqlStatement.getStorageUnitName().isPresent()) {
- return Collections.emptyList();
- }
Collection<String> result = new HashSet<>(1, 1F);
for (ReadwriteSplittingDataSourceGroupRuleConfiguration each :
rule.getConfiguration().getDataSourceGroups()) {
- if
(each.getWriteDataSourceName().equalsIgnoreCase(sqlStatement.getStorageUnitName().get()))
{
+ if
(each.getWriteDataSourceName().equalsIgnoreCase(sqlStatement.getStorageUnitName()))
{
result.add(each.getName());
}
- if (new
CaseInsensitiveSet<>(each.getReadDataSourceNames()).contains(sqlStatement.getStorageUnitName().get()))
{
+ if (new
CaseInsensitiveSet<>(each.getReadDataSourceNames()).contains(sqlStatement.getStorageUnitName()))
{
result.add(each.getName());
}
}
diff --git
a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/InUsedReadwriteSplittingStorageUnitRetrieverTest.java
b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/InUsedReadwriteSplittingStorageUnitRetrieverTest.java
index bdad6bc2cd0..257fe78127e 100644
---
a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/InUsedReadwriteSplittingStorageUnitRetrieverTest.java
+++
b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/InUsedReadwriteSplittingStorageUnitRetrieverTest.java
@@ -28,7 +28,6 @@ import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -38,12 +37,6 @@ class InUsedReadwriteSplittingStorageUnitRetrieverTest {
@SuppressWarnings("unchecked")
private final InUsedStorageUnitRetriever<ReadwriteSplittingRule> retriever
= TypedSPILoader.getService(InUsedStorageUnitRetriever.class,
ReadwriteSplittingRule.class);
- @Test
- void assertGetInUsedResourcesWithoutStorageUnit() {
- ShowRulesUsedStorageUnitStatement sqlStatement = new
ShowRulesUsedStorageUnitStatement(null, null);
- assertTrue(retriever.getInUsedResources(sqlStatement,
mock(ReadwriteSplittingRule.class)).isEmpty());
- }
-
@Test
void assertGetInUsedResourcesWithWriteDataSource() {
ShowRulesUsedStorageUnitStatement sqlStatement = new
ShowRulesUsedStorageUnitStatement("foo_unit_write", null);
diff --git
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetriever.java
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetriever.java
index 1b2ad9f90e5..877f65494b0 100644
---
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetriever.java
+++
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetriever.java
@@ -23,7 +23,6 @@ import
org.apache.shardingsphere.shadow.config.datasource.ShadowDataSourceConfig
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import java.util.Collection;
-import java.util.Collections;
import java.util.stream.Collectors;
/**
@@ -33,12 +32,9 @@ public final class InUsedShadowStorageUnitRetriever
implements InUsedStorageUnit
@Override
public Collection<String> getInUsedResources(final
ShowRulesUsedStorageUnitStatement sqlStatement, final ShadowRule rule) {
- if (!sqlStatement.getStorageUnitName().isPresent()) {
- return Collections.emptyList();
- }
return rule.getConfiguration().getDataSources().stream()
- .filter(each ->
each.getShadowDataSourceName().equalsIgnoreCase(sqlStatement.getStorageUnitName().get())
- ||
each.getProductionDataSourceName().equalsIgnoreCase(sqlStatement.getStorageUnitName().get()))
+ .filter(each ->
each.getShadowDataSourceName().equalsIgnoreCase(sqlStatement.getStorageUnitName())
+ ||
each.getProductionDataSourceName().equalsIgnoreCase(sqlStatement.getStorageUnitName()))
.map(ShadowDataSourceConfiguration::getName).collect(Collectors.toList());
}
diff --git
a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetrieverTest.java
b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetrieverTest.java
index a9808066345..964f545b683 100644
---
a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetrieverTest.java
+++
b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetrieverTest.java
@@ -28,7 +28,6 @@ import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -38,12 +37,6 @@ class InUsedShadowStorageUnitRetrieverTest {
@SuppressWarnings("unchecked")
private final InUsedStorageUnitRetriever<ShadowRule> retriever =
TypedSPILoader.getService(InUsedStorageUnitRetriever.class, ShadowRule.class);
- @Test
- void assertGetInUsedResourcesWithoutStorageUnit() {
- ShowRulesUsedStorageUnitStatement sqlStatement = new
ShowRulesUsedStorageUnitStatement(null, null);
- assertTrue(retriever.getInUsedResources(sqlStatement,
mock(ShadowRule.class)).isEmpty());
- }
-
@Test
void assertGetInUsedResourcesWithShadowDataSource() {
ShowRulesUsedStorageUnitStatement sqlStatement = new
ShowRulesUsedStorageUnitStatement("prod_ds", null);
diff --git
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowRulesUsedStorageUnitExecutor.java
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowRulesUsedStorageUnitExecutor.java
index ecaab9b2cee..1b66dcedf8a 100644
---
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowRulesUsedStorageUnitExecutor.java
+++
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowRulesUsedStorageUnitExecutor.java
@@ -49,8 +49,7 @@ public final class ShowRulesUsedStorageUnitExecutor
implements DistSQLQueryExecu
@Override
public Collection<LocalDataQueryResultRow> getRows(final
ShowRulesUsedStorageUnitStatement sqlStatement, final ContextManager
contextManager) {
- String resourceName = sqlStatement.getStorageUnitName().orElse(null);
- return
database.getResourceMetaData().getStorageUnits().containsKey(resourceName) ?
getRows(sqlStatement) : Collections.emptyList();
+ return
database.getResourceMetaData().getStorageUnits().containsKey(sqlStatement.getStorageUnitName())
? getRows(sqlStatement) : Collections.emptyList();
}
@SuppressWarnings("unchecked")
diff --git
a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/InUsedSingleStorageUnitRetriever.java
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/InUsedSingleStorageUnitRetriever.java
new file mode 100644
index 00000000000..399d44393b4
--- /dev/null
+++
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/InUsedSingleStorageUnitRetriever.java
@@ -0,0 +1,53 @@
+/*
+ * 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.single.distsql.handler.query;
+
+import
org.apache.shardingsphere.distsql.handler.executor.rql.resource.InUsedStorageUnitRetriever;
+import
org.apache.shardingsphere.distsql.statement.rql.rule.database.ShowRulesUsedStorageUnitStatement;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.single.rule.SingleDataNodeRuleAttribute;
+import org.apache.shardingsphere.single.rule.SingleRule;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Map;
+
+/**
+ * In used single storage unit retriever.
+ */
+public final class InUsedSingleStorageUnitRetriever implements
InUsedStorageUnitRetriever<SingleRule> {
+
+ @Override
+ public Collection<String> getInUsedResources(final
ShowRulesUsedStorageUnitStatement sqlStatement, final SingleRule rule) {
+ SingleDataNodeRuleAttribute attribute =
rule.getAttributes().getAttribute(SingleDataNodeRuleAttribute.class);
+ Map<String, Collection<DataNode>> dataNodes =
attribute.getAllDataNodes();
+ Collection<String> result = new HashSet<>(dataNodes.size(), 1F);
+ for (Collection<DataNode> each : dataNodes.values()) {
+ String storageUnitName =
each.iterator().next().getDataSourceName();
+ if
(storageUnitName.equalsIgnoreCase(sqlStatement.getStorageUnitName())) {
+ result.add(each.iterator().next().getTableName());
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public Class<SingleRule> getType() {
+ return SingleRule.class;
+ }
+}
diff --git
a/kernel/single/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.executor.rql.resource.InUsedStorageUnitRetriever
b/kernel/single/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.executor.rql.resource.InUsedStorageUnitRetriever
new file mode 100644
index 00000000000..348650f3dfa
--- /dev/null
+++
b/kernel/single/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.executor.rql.resource.InUsedStorageUnitRetriever
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.single.distsql.handler.query.InUsedSingleStorageUnitRetriever
diff --git
a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetrieverTest.java
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/InUsedSingleStorageUnitRetrieverTest.java
similarity index 50%
copy from
features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetrieverTest.java
copy to
kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/InUsedSingleStorageUnitRetrieverTest.java
index a9808066345..e939291b072 100644
---
a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetrieverTest.java
+++
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/InUsedSingleStorageUnitRetrieverTest.java
@@ -15,51 +15,41 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.shadow.distsql.handler.query;
+package org.apache.shardingsphere.single.distsql.handler.query;
import
org.apache.shardingsphere.distsql.handler.executor.rql.resource.InUsedStorageUnitRetriever;
import
org.apache.shardingsphere.distsql.statement.rql.rule.database.ShowRulesUsedStorageUnitStatement;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import
org.apache.shardingsphere.shadow.config.datasource.ShadowDataSourceConfiguration;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.apache.shardingsphere.single.rule.SingleDataNodeRuleAttribute;
+import org.apache.shardingsphere.single.rule.SingleRule;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-class InUsedShadowStorageUnitRetrieverTest {
+class InUsedSingleStorageUnitRetrieverTest {
@SuppressWarnings("unchecked")
- private final InUsedStorageUnitRetriever<ShadowRule> retriever =
TypedSPILoader.getService(InUsedStorageUnitRetriever.class, ShadowRule.class);
+ private final InUsedStorageUnitRetriever<SingleRule> retriever =
TypedSPILoader.getService(InUsedStorageUnitRetriever.class, SingleRule.class);
@Test
- void assertGetInUsedResourcesWithoutStorageUnit() {
- ShowRulesUsedStorageUnitStatement sqlStatement = new
ShowRulesUsedStorageUnitStatement(null, null);
- assertTrue(retriever.getInUsedResources(sqlStatement,
mock(ShadowRule.class)).isEmpty());
+ void assertGetInUsedResources() {
+ ShowRulesUsedStorageUnitStatement sqlStatement = new
ShowRulesUsedStorageUnitStatement("foo_ds", null);
+ assertThat(retriever.getInUsedResources(sqlStatement, mockRule()),
is(Collections.singleton("foo_table")));
}
- @Test
- void assertGetInUsedResourcesWithShadowDataSource() {
- ShowRulesUsedStorageUnitStatement sqlStatement = new
ShowRulesUsedStorageUnitStatement("prod_ds", null);
- assertThat(retriever.getInUsedResources(sqlStatement, mockRule()),
is(Collections.singletonList("foo_ds")));
- }
-
- @Test
- void assertGetInUsedResourcesWithProductionDataSource() {
- ShowRulesUsedStorageUnitStatement sqlStatement = new
ShowRulesUsedStorageUnitStatement("shadow_ds", null);
- assertThat(retriever.getInUsedResources(sqlStatement, mockRule()),
is(Collections.singletonList("foo_ds")));
- }
-
- private ShadowRule mockRule() {
- ShadowRule result = mock(ShadowRule.class, RETURNS_DEEP_STUBS);
- ShadowDataSourceConfiguration dataSourceConfig = new
ShadowDataSourceConfiguration("foo_ds", "prod_ds", "shadow_ds");
-
when(result.getConfiguration().getDataSources()).thenReturn(Collections.singleton(dataSourceConfig));
+ private SingleRule mockRule() {
+ SingleRule result = mock(SingleRule.class);
+ SingleDataNodeRuleAttribute attribute =
mock(SingleDataNodeRuleAttribute.class);
+ DataNode dataNode = new DataNode("foo_ds", "foo_table");
+
when(attribute.getAllDataNodes()).thenReturn(Collections.singletonMap("foo_table",
Collections.singletonList(dataNode)));
+ when(result.getAttributes()).thenReturn(new RuleAttributes(attribute));
return result;
}
}
diff --git
a/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rql/rule/database/ShowRulesUsedStorageUnitStatement.java
b/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rql/rule/database/ShowRulesUsedStorageUnitStatement.java
index 7a389b9fc62..30a355bf9d2 100644
---
a/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rql/rule/database/ShowRulesUsedStorageUnitStatement.java
+++
b/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rql/rule/database/ShowRulesUsedStorageUnitStatement.java
@@ -17,13 +17,13 @@
package org.apache.shardingsphere.distsql.statement.rql.rule.database;
+import lombok.Getter;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.DatabaseSegment;
-import java.util.Optional;
-
/**
* Show rules used storage unit statement.
*/
+@Getter
public final class ShowRulesUsedStorageUnitStatement extends
ShowDatabaseRulesStatement {
private final String storageUnitName;
@@ -32,13 +32,4 @@ public final class ShowRulesUsedStorageUnitStatement extends
ShowDatabaseRulesSt
super(database);
this.storageUnitName = storageUnitName;
}
-
- /**
- * Get resource name.
- *
- * @return resource name
- */
- public Optional<String> getStorageUnitName() {
- return Optional.ofNullable(storageUnitName);
- }
}