You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2025/05/15 05:06:23 UTC
(shardingsphere) branch master updated: Improve default database protocol type handling (#35419)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 515b905d5cd Improve default database protocol type handling (#35419)
515b905d5cd is described below
commit 515b905d5cd0e0e47db5c4a75e5c210526729e82
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu May 15 13:06:16 2025 +0800
Improve default database protocol type handling (#35419)
- Add DEFAULT_FRONTEND_DATABASE_PROTOCOL_TYPE constant for MySQL
- Refactor logic to handle multiple DatabaseProtocolFrontendEngine implementations
- Use TypedSPILoader for better type safety when getting default DatabaseType
---
.../protocol/FrontDatabaseProtocolTypeFactory.java | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactory.java b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactory.java
index a294e3cc427..cdd21c5122b 100644
--- a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactory.java
+++ b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactory.java
@@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngine;
@@ -37,6 +38,8 @@ import java.util.Optional;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class FrontDatabaseProtocolTypeFactory {
+ private static final String DEFAULT_FRONTEND_DATABASE_PROTOCOL_TYPE = "MySQL";
+
/**
* Get front database protocol type.
*
@@ -47,16 +50,18 @@ public final class FrontDatabaseProtocolTypeFactory {
if (configuredDatabaseType.isPresent()) {
return configuredDatabaseType.get();
}
- Collection<DatabaseProtocolFrontendEngine> databaseProtocolFrontendEngine = ShardingSphereServiceLoader.getServiceInstances(DatabaseProtocolFrontendEngine.class);
- ShardingSpherePreconditions.checkNotEmpty(databaseProtocolFrontendEngine,
+ Collection<DatabaseProtocolFrontendEngine> databaseProtocolFrontendEngines = ShardingSphereServiceLoader.getServiceInstances(DatabaseProtocolFrontendEngine.class);
+ ShardingSpherePreconditions.checkNotEmpty(databaseProtocolFrontendEngines,
() -> new IllegalArgumentException(String.format("Can not find any SPI implementation of `%s`.", DatabaseProtocolFrontendEngine.class.getName())));
- DatabaseProtocolFrontendEngine defaultDatabaseProtocolFrontendEngine = databaseProtocolFrontendEngine.iterator().next();
+ DatabaseType defaultDatabaseType = 1 == databaseProtocolFrontendEngines.size()
+ ? databaseProtocolFrontendEngines.iterator().next().getType()
+ : TypedSPILoader.getService(DatabaseType.class, DEFAULT_FRONTEND_DATABASE_PROTOCOL_TYPE);
MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
if (metaDataContexts.getMetaData().getAllDatabases().isEmpty()) {
- return defaultDatabaseProtocolFrontendEngine.getType();
+ return defaultDatabaseType;
}
Optional<ShardingSphereDatabase> database = metaDataContexts.getMetaData().getAllDatabases().stream().filter(ShardingSphereDatabase::containsDataSource).findFirst();
- return database.isPresent() ? database.get().getResourceMetaData().getStorageUnits().values().iterator().next().getStorageType() : defaultDatabaseProtocolFrontendEngine.getType();
+ return database.isPresent() ? database.get().getResourceMetaData().getStorageUnits().values().iterator().next().getStorageType() : defaultDatabaseType;
}
private static Optional<DatabaseType> findConfiguredDatabaseType() {