This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 73a3e3d04ed Remove TypedSPILoader.contains (#27925)
73a3e3d04ed is described below
commit 73a3e3d04edf9c8dfc88fb6710e483d8433f6a9b
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Aug 4 22:19:31 2023 +0800
Remove TypedSPILoader.contains (#27925)
---
...CreateReadwriteSplittingRuleStatementUpdaterTest.java | 10 ----------
.../infra/spi/type/typed/TypedSPILoader.java | 16 ++--------------
.../infra/spi/type/typed/TypedSPILoaderTest.java | 7 -------
.../xa/XAShardingSphereTransactionManager.java | 10 +++++++++-
4 files changed, 11 insertions(+), 32 deletions(-)
diff --git
a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java
b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java
index dfc16c8b8b5..25c4c3b056e 100644
---
a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java
+++
b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java
@@ -24,13 +24,11 @@ import
org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.resource.ShardingSphereResourceMetaData;
import
org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
-import
org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.distsql.parser.segment.ReadwriteSplittingRuleSegment;
import
org.apache.shardingsphere.readwritesplitting.distsql.parser.statement.CreateReadwriteSplittingRuleStatement;
-import
org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
import org.junit.jupiter.api.BeforeEach;
@@ -100,13 +98,6 @@ class CreateReadwriteSplittingRuleStatementUpdaterTest {
assertThrows(InvalidRuleConfigurationException.class, () ->
updater.checkSQLStatement(database, createSQLStatement(false, ruleSegment),
null));
}
- @Test
- void assertCheckSQLStatementWithoutToBeCreatedLoadBalancers() {
-
when(database.getRuleMetaData().findRules(any())).thenReturn(Collections.emptyList());
- when(TypedSPILoader.checkService(any(), any(),
any())).thenCallRealMethod();
- assertThrows(ServiceProviderNotFoundException.class, () ->
updater.checkSQLStatement(database, createSQLStatement("INVALID_TYPE"), null));
- }
-
@Test
void assertCheckSQLStatementWithDuplicateWriteResourceNamesInStatement() {
assertThrows(InvalidRuleConfigurationException.class,
@@ -142,7 +133,6 @@ class CreateReadwriteSplittingRuleStatementUpdaterTest {
DataSourceContainedRule dataSourceContainedRule =
mock(DataSourceContainedRule.class);
when(dataSourceContainedRule.getDataSourceMapper()).thenReturn(Collections.singletonMap("ms_group",
Collections.singleton("ds_0")));
when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.singleton(dataSourceContainedRule));
- when(TypedSPILoader.contains(ReadQueryLoadBalanceAlgorithm.class,
"TEST")).thenReturn(true);
ReadwriteSplittingRuleSegment staticSegment = new
ReadwriteSplittingRuleSegment("static_rule", "write_ds_0",
Arrays.asList("read_ds_0", "read_ds_1"),
new AlgorithmSegment("TEST", new Properties()));
CreateReadwriteSplittingRuleStatement statement =
createSQLStatement(false, staticSegment);
diff --git
a/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoader.java
b/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoader.java
index 262e8bdc540..b5634480fe4 100644
---
a/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoader.java
+++
b/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoader.java
@@ -31,17 +31,6 @@ import java.util.Properties;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TypedSPILoader {
- /**
- * Judge whether contains service.
- *
- * @param serviceInterface typed SPI service interface to be judged
- * @param type type
- * @return contains or not
- */
- public static boolean contains(final Class<? extends TypedSPI>
serviceInterface, final Object type) {
- return
ShardingSphereServiceLoader.getServiceInstances(serviceInterface).stream().anyMatch(each
-> matchesType(type, each));
- }
-
/**
* Find service.
*
@@ -128,14 +117,13 @@ public final class TypedSPILoader {
* @param type type
* @param props properties
* @param <T> SPI class type
- * @return is valid service or not
* @throws ServiceProviderNotFoundException service provider not found
server exception
*/
- public static <T extends TypedSPI> boolean checkService(final Class<T>
serviceInterface, final Object type, final Properties props) {
+ public static <T extends TypedSPI> void checkService(final Class<T>
serviceInterface, final Object type, final Properties props) {
for (T each :
ShardingSphereServiceLoader.getServiceInstances(serviceInterface)) {
if (matchesType(type, each)) {
each.init(null == props ? new Properties() :
convertToStringTypedProperties(props));
- return true;
+ return;
}
}
throw new ServiceProviderNotFoundException(serviceInterface, type);
diff --git
a/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoaderTest.java
b/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoaderTest.java
index 65919295a71..a1ff5ee74b2 100644
---
a/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoaderTest.java
+++
b/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoaderTest.java
@@ -29,19 +29,12 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class TypedSPILoaderTest {
- @Test
- void assertContains() {
- assertTrue(TypedSPILoader.contains(TypedSPIFixture.class,
"TYPED.FIXTURE"));
- assertFalse(TypedSPILoader.contains(TypedSPIFixture.class,
"NOT_EXISTED"));
- }
-
@Test
void assertFindServiceWithoutProperties() {
assertTrue(TypedSPILoader.findService(TypedSPIFixture.class,
"TYPED.FIXTURE").isPresent());
diff --git
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
index 92a42f80755..30a66eb8aa1 100644
---
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
+++
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
@@ -21,6 +21,7 @@ import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import
org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.transaction.api.TransactionType;
import org.apache.shardingsphere.transaction.core.ResourceDataSource;
@@ -44,6 +45,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Properties;
/**
* ShardingSphere Transaction manager for XA.
@@ -141,7 +143,13 @@ public final class XAShardingSphereTransactionManager
implements ShardingSphereT
@Override
public boolean containsProviderType(final String providerType) {
- return TypedSPILoader.contains(XATransactionManagerProvider.class,
providerType);
+ try {
+ TypedSPILoader.checkService(XATransactionManagerProvider.class,
providerType, new Properties());
+ return true;
+ } catch (final ServiceProviderNotFoundException ex) {
+ return false;
+ }
+
}
@Override