You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zi...@apache.org on 2023/07/09 22:22:45 UTC
[shardingsphere] branch master updated: Refactor TypedSPI to support more types of return value (#26864)
This is an automated email from the ASF dual-hosted git repository.
zichaowang 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 5032c9d7337 Refactor TypedSPI to support more types of return value (#26864)
5032c9d7337 is described below
commit 5032c9d733755c34a29c40fcb7d72a62d260b72b
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Jul 10 06:22:38 2023 +0800
Refactor TypedSPI to support more types of return value (#26864)
* Refactor TypedSPI to support more types of return value
* Refactor TypedSPI to support more types of return value
* Refactor TypedSPI to support more types of return value
* Refactor TypedSPI to support more types of return value
* Refactor TypedSPI to support more types of return value
* Refactor TypedSPI to support more types of return value
* Refactor TypedSPI to support more types of return value
* Refactor TypedSPI to support more types of return value
* Refactor TypedSPI to support more types of return value
* Refactor TypedSPI to support more types of return value
---
.../impl/proxy/ProxyMetaDataInfoExporterTest.java | 10 ++++++----
.../infra/database/type/DatabaseType.java | 3 +++
.../infra/util/spi/type/typed/TypedSPI.java | 2 +-
.../infra/util/spi/type/typed/TypedSPILoader.java | 10 ++++++++--
.../type/typed/algorithm/ShardingSphereAlgorithm.java | 5 +++++
.../pipeline/spi/sqlbuilder/PipelineSQLBuilder.java | 3 +++
.../data/pipeline/common/job/type/JobType.java | 3 +++
.../query/ShowMigrationCheckAlgorithmsExecutor.java | 4 ++--
.../shardingsphere/mode/metadata/MetaDataContexts.java | 18 +++++++++++++-----
.../repository/cluster/consul/ConsulRepository.java | 5 -----
.../cluster/zookeeper/ZookeeperRepositoryTest.java | 2 +-
.../spi/FeaturedDistSQLStatementParserFacade.java | 3 +++
.../admin/executor/MySQLSetCharsetExecutor.java | 3 +--
.../frontend/spi/DatabaseProtocolFrontendEngine.java | 3 +++
14 files changed, 52 insertions(+), 22 deletions(-)
diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java
index 17c3c8ca431..a56951501e8 100644
--- a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java
+++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java
@@ -22,11 +22,12 @@ import org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeM
import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.collector.MetricsCollectorFixture;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
-import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
@@ -74,9 +75,10 @@ class ProxyMetaDataInfoExporterTest {
private ContextManager mockContextManager() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getResourceMetaData().getDataSources()).thenReturn(Collections.singletonMap("ds_0", mock(DataSource.class)));
- ShardingSphereMetaData shardingSphereMetaData = mock(ShardingSphereMetaData.class);
- when(shardingSphereMetaData.getDatabases()).thenReturn(Collections.singletonMap("sharding_db", database));
- MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), shardingSphereMetaData);
+ when(database.getProtocolType()).thenReturn(new MySQLDatabaseType());
+ ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
+ when(metaData.getDatabases()).thenReturn(Collections.singletonMap("sharding_db", database));
+ MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), metaData);
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(result.getMetaDataContexts()).thenReturn(metaDataContexts);
return result;
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseType.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseType.java
index e0c6e101198..d918c2f913a 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseType.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseType.java
@@ -114,4 +114,7 @@ public interface DatabaseType extends TypedSPI {
*/
default void handleRollbackOnly(final boolean rollbackOnly, final SQLStatement statement) throws SQLException {
}
+
+ @Override
+ String getType();
}
diff --git a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPI.java b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPI.java
index fc41a922ff6..44b2f8b1f98 100644
--- a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPI.java
+++ b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPI.java
@@ -39,7 +39,7 @@ public interface TypedSPI {
*
* @return type
*/
- default String getType() {
+ default Object getType() {
return null;
}
diff --git a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoader.java b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoader.java
index 629bec0bdf1..550f287e61c 100644
--- a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoader.java
+++ b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoader.java
@@ -120,7 +120,7 @@ public final class TypedSPILoader {
* @return service
*/
public static <T extends TypedSPI> T getService(final Class<T> spiClass, final String type, final Properties props) {
- return findService(spiClass, type, props).orElseGet(() -> findService(spiClass).orElseThrow(() -> new ServiceProviderNotFoundServerException(spiClass)));
+ return findService(spiClass, type, props).orElseGet(() -> findService(spiClass).orElseThrow(() -> new ServiceProviderNotFoundServerException(spiClass, type)));
}
/**
@@ -145,6 +145,12 @@ public final class TypedSPILoader {
}
private static boolean matchesType(final String type, final TypedSPI instance) {
- return null != instance.getType() && (instance.getType().equalsIgnoreCase(type) || instance.getTypeAliases().contains(type));
+ if (null == instance.getType()) {
+ return false;
+ }
+ if (instance.getType() instanceof String) {
+ return instance.getType().toString().equalsIgnoreCase(type) || instance.getTypeAliases().contains(type);
+ }
+ return instance.getType().equals(type) || instance.getTypeAliases().contains(type);
}
}
diff --git a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/algorithm/ShardingSphereAlgorithm.java b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/algorithm/ShardingSphereAlgorithm.java
index a1f7c9e4505..5e9e423f08a 100644
--- a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/algorithm/ShardingSphereAlgorithm.java
+++ b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/algorithm/ShardingSphereAlgorithm.java
@@ -23,4 +23,9 @@ import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPI;
* ShardingSphere algorithm SPI.
*/
public interface ShardingSphereAlgorithm extends TypedSPI {
+
+ @Override
+ default String getType() {
+ return "";
+ }
}
diff --git a/kernel/data-pipeline/api/src/main/java/org/apache/shardingsphere/data/pipeline/spi/sqlbuilder/PipelineSQLBuilder.java b/kernel/data-pipeline/api/src/main/java/org/apache/shardingsphere/data/pipeline/spi/sqlbuilder/PipelineSQLBuilder.java
index 440ff4f8363..743410b9bd6 100644
--- a/kernel/data-pipeline/api/src/main/java/org/apache/shardingsphere/data/pipeline/spi/sqlbuilder/PipelineSQLBuilder.java
+++ b/kernel/data-pipeline/api/src/main/java/org/apache/shardingsphere/data/pipeline/spi/sqlbuilder/PipelineSQLBuilder.java
@@ -189,4 +189,7 @@ public interface PipelineSQLBuilder extends TypedSPI {
default Optional<String> buildCRC32SQL(final String schemaName, final String tableName, final String column) {
return Optional.empty();
}
+
+ @Override
+ String getType();
}
diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/job/type/JobType.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/job/type/JobType.java
index 36b47bdaf63..0376ee0c1b0 100644
--- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/job/type/JobType.java
+++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/job/type/JobType.java
@@ -32,4 +32,7 @@ public interface JobType extends TypedSPI {
* @return job type code
*/
String getCode();
+
+ @Override
+ String getType();
}
diff --git a/kernel/data-pipeline/distsql/handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/query/ShowMigrationCheckAlgorithmsExecutor.java b/kernel/data-pipeline/distsql/handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/query/ShowMigrationCheckAlgorithmsExecutor.java
index 20d80ebe3b3..3674cef6c5f 100644
--- a/kernel/data-pipeline/distsql/handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/query/ShowMigrationCheckAlgorithmsExecutor.java
+++ b/kernel/data-pipeline/distsql/handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/query/ShowMigrationCheckAlgorithmsExecutor.java
@@ -20,8 +20,8 @@ package org.apache.shardingsphere.migration.distsql.handler.query;
import org.apache.shardingsphere.data.pipeline.core.job.service.InventoryIncrementalJobAPI;
import org.apache.shardingsphere.data.pipeline.core.job.service.PipelineJobAPI;
import org.apache.shardingsphere.distsql.handler.ral.query.QueryableRALExecutor;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
-import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPI;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.migration.distsql.statement.ShowMigrationCheckAlgorithmsStatement;
@@ -39,7 +39,7 @@ public final class ShowMigrationCheckAlgorithmsExecutor implements QueryableRALE
InventoryIncrementalJobAPI jobAPI = (InventoryIncrementalJobAPI) TypedSPILoader.getService(PipelineJobAPI.class, "MIGRATION");
return jobAPI.listDataConsistencyCheckAlgorithms().stream().map(
each -> new LocalDataQueryResultRow(each.getType(),
- each.getSupportedDatabaseTypes().stream().map(TypedSPI::getType).collect(Collectors.joining(",")), each.getDescription()))
+ each.getSupportedDatabaseTypes().stream().map(DatabaseType::getType).collect(Collectors.joining(",")), each.getDescription()))
.collect(Collectors.toList());
}
diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java
index 2c72f21d4e1..9759590d4d7 100644
--- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java
+++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.mode.metadata;
import lombok.Getter;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.SchemaSupportedDatabaseType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
@@ -54,11 +55,18 @@ public final class MetaDataContexts implements AutoCloseable {
if (metaData.getDatabases().isEmpty()) {
return new ShardingSphereStatistics();
}
- ShardingSphereStatistics result = Optional.ofNullable(metaData.getDatabases().values().iterator().next().getProtocolType())
- // TODO can `protocolType instanceof SchemaSupportedDatabaseType ? "PostgreSQL" : protocolType.getType()` replace to trunk database type?
- .flatMap(protocolType -> TypedSPILoader.findService(ShardingSphereStatisticsBuilder.class, protocolType instanceof SchemaSupportedDatabaseType ? "PostgreSQL" : protocolType.getType())
- .map(builder -> builder.build(metaData)))
- .orElseGet(ShardingSphereStatistics::new);
+ DatabaseType protocolType = metaData.getDatabases().values().iterator().next().getProtocolType();
+ if (null == protocolType) {
+ return new ShardingSphereStatistics();
+ }
+ // TODO can `protocolType instanceof SchemaSupportedDatabaseType ? "PostgreSQL" : protocolType.getType()` replace to trunk database type?
+ Optional<ShardingSphereStatisticsBuilder> statisticsBuilder = TypedSPILoader.findService(ShardingSphereStatisticsBuilder.class, protocolType instanceof SchemaSupportedDatabaseType
+ ? "PostgreSQL"
+ : protocolType.getType());
+ if (!statisticsBuilder.isPresent()) {
+ return new ShardingSphereStatistics();
+ }
+ ShardingSphereStatistics result = statisticsBuilder.get().build(metaData);
Optional<ShardingSphereStatistics> loadedStatistics = Optional.ofNullable(persistService.getShardingSphereDataPersistService())
.flatMap(shardingSphereDataPersistService -> shardingSphereDataPersistService.load(metaData));
loadedStatistics.ifPresent(optional -> useLoadedToReplaceInit(result, optional));
diff --git a/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepository.java b/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepository.java
index 36e692419eb..3c0f363a123 100644
--- a/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepository.java
+++ b/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepository.java
@@ -199,9 +199,4 @@ public final class ConsulRepository implements ClusterPersistRepository {
public String getType() {
return "Consul";
}
-
- @Override
- public Collection<String> getTypeAliases() {
- return ClusterPersistRepository.super.getTypeAliases();
- }
}
diff --git a/mode/type/cluster/repository/provider/zookeeper/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/ZookeeperRepositoryTest.java b/mode/type/cluster/repository/provider/zookeeper/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/ZookeeperRepositoryTest.java
index cccd9bba31a..cd5824293bf 100644
--- a/mode/type/cluster/repository/provider/zookeeper/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/ZookeeperRepositoryTest.java
+++ b/mode/type/cluster/repository/provider/zookeeper/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/ZookeeperRepositoryTest.java
@@ -128,7 +128,7 @@ class ZookeeperRepositoryTest {
@SneakyThrows(ReflectiveOperationException.class)
private void mockDistributedLockHolder() {
- DistributedLockHolder distributedLockHolder = new DistributedLockHolder("Zookeeper", client, new ZookeeperProperties(new Properties()));
+ DistributedLockHolder distributedLockHolder = new DistributedLockHolder("ZooKeeper", client, new ZookeeperProperties(new Properties()));
Plugins.getMemberAccessor().set(DistributedLockHolder.class.getDeclaredField("locks"), distributedLockHolder, Collections.singletonMap("/locks/glock", mock(ZookeeperDistributedLock.class)));
Plugins.getMemberAccessor().set(ZookeeperRepository.class.getDeclaredField("distributedLockHolder"), REPOSITORY, distributedLockHolder);
}
diff --git a/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/engine/spi/FeaturedDistSQLStatementParserFacade.java b/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/engine/spi/FeaturedDistSQLStatementParserFacade.java
index 501ceee2108..c8820dae8e3 100644
--- a/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/engine/spi/FeaturedDistSQLStatementParserFacade.java
+++ b/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/engine/spi/FeaturedDistSQLStatementParserFacade.java
@@ -34,4 +34,7 @@ public interface FeaturedDistSQLStatementParserFacade extends SQLParserFacade {
* @return visitor class
*/
Class<? extends SQLVisitor<ASTNode>> getVisitorClass();
+
+ @Override
+ String getType();
}
diff --git a/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/MySQLSetCharsetExecutor.java b/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/MySQLSetCharsetExecutor.java
index 392215d6abd..9c08474e5b1 100644
--- a/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/MySQLSetCharsetExecutor.java
+++ b/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/MySQLSetCharsetExecutor.java
@@ -28,7 +28,6 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Locale;
-import java.util.Set;
import java.util.TreeSet;
/**
@@ -36,7 +35,7 @@ import java.util.TreeSet;
*/
public final class MySQLSetCharsetExecutor implements MySQLSessionVariableHandler {
- private static final Set<String> TYPE_ALIASES = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+ private static final Collection<String> TYPE_ALIASES = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
static {
TYPE_ALIASES.add("character_set_client");
diff --git a/proxy/frontend/spi/src/main/java/org/apache/shardingsphere/proxy/frontend/spi/DatabaseProtocolFrontendEngine.java b/proxy/frontend/spi/src/main/java/org/apache/shardingsphere/proxy/frontend/spi/DatabaseProtocolFrontendEngine.java
index 83b3a097736..44fab75ceea 100644
--- a/proxy/frontend/spi/src/main/java/org/apache/shardingsphere/proxy/frontend/spi/DatabaseProtocolFrontendEngine.java
+++ b/proxy/frontend/spi/src/main/java/org/apache/shardingsphere/proxy/frontend/spi/DatabaseProtocolFrontendEngine.java
@@ -72,4 +72,7 @@ public interface DatabaseProtocolFrontendEngine extends TypedSPI {
* @param exception exception
*/
void handleException(ConnectionSession connectionSession, Exception exception);
+
+ @Override
+ String getType();
}