This is an automated email from the ASF dual-hosted git repository. jiangmaolin pushed a commit to branch dev-5.5.1 in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
commit 4bd2f0a9f03bf89951387985f490a4218c5b8e83 Author: jiangML <[email protected]> AuthorDate: Thu Oct 31 23:18:58 2024 +0800 Improve DataSourceStateManager --- .../engine/type/ShardingRouteEngineFactory.java | 8 +++-- .../type/unicast/ShardingUnicastRoutingEngine.java | 12 +++++-- .../unicast/ShardingUnicastRoutingEngineTest.java | 27 ++++++++++++---- .../state/datasource/DataSourceStateManager.java | 23 +++++++++----- .../exception/UnavailableDataSourceException.java | 12 ++----- .../infra/database/DatabaseTypeEngine.java | 37 ++++++++++++++-------- .../metadata/database/ShardingSphereDatabase.java | 8 +++-- .../builder/GenericSchemaBuilderMaterial.java | 22 +++++++++++-- .../infra/database/DatabaseTypeEngineTest.java | 14 +++++--- .../metadata/factory/ExternalMetaDataFactory.java | 10 ++++-- .../metadata/factory/InternalMetaDataFactory.java | 5 ++- .../SingleRuleConfigurationDecorator.java | 8 +++-- .../shardingsphere/single/rule/SingleRule.java | 6 +++- .../handler/update/LoadSingleTableExecutor.java | 2 +- .../mode/manager/ContextManager.java | 4 ++- .../table/AlterTableStatementSchemaRefresher.java | 5 ++- .../table/CreateTableStatementSchemaRefresher.java | 5 ++- .../table/RenameTableStatementSchemaRefresher.java | 5 ++- .../view/AlterViewStatementSchemaRefresher.java | 5 ++- .../view/CreateViewStatementSchemaRefresher.java | 5 ++- .../proxy/version/ShardingSphereProxyVersion.java | 5 ++- 21 files changed, 162 insertions(+), 66 deletions(-) diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java index 5afdc67648f..cc0d589a682 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java @@ -165,7 +165,9 @@ public final class ShardingRouteEngineFactory { : new ShardingTableBroadcastRoutingEngine(database, sqlStatementContext, shardingRuleTableNames); } if (!shardingRuleTableNames.isEmpty()) { - return new ShardingUnicastRoutingEngine(sqlStatementContext, shardingRuleTableNames, connectionContext); + // SPEX CHANGED: BEGIN + return new ShardingUnicastRoutingEngine(sqlStatementContext, database.getName(), shardingRuleTableNames, connectionContext); + // SPEX CHANGED: END } return new ShardingDataSourceGroupBroadcastRoutingEngine(); } @@ -198,7 +200,9 @@ public final class ShardingRouteEngineFactory { final ConnectionContext connectionContext) { Collection<String> tableNames = sqlStatementContext instanceof TableAvailable ? ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames() : Collections.emptyList(); if (sqlStatementContext.getSqlStatement() instanceof DMLStatement && shardingConditions.isAlwaysFalse() || tableNames.isEmpty()) { - return new ShardingUnicastRoutingEngine(sqlStatementContext, tableNames, connectionContext); + // SPEX CHANGED: BEGIN + return new ShardingUnicastRoutingEngine(sqlStatementContext, database.getName(), tableNames, connectionContext); + // SPEX CHANGED: END } Collection<String> shardingLogicTableNames = shardingRule.getShardingLogicTableNames(tableNames); if (shardingLogicTableNames.isEmpty()) { diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java index d10b3568a74..27a120f99d3 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java @@ -18,17 +18,19 @@ package org.apache.shardingsphere.sharding.route.engine.type.unicast; import com.google.common.collect.Sets; +import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.infra.state.datasource.DataSourceStateManager; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterViewStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateViewStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.ddl.DropViewStatementContext; import org.apache.shardingsphere.infra.binder.context.type.CursorAvailable; -import org.apache.shardingsphere.infra.session.connection.ConnectionContext; import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; +import org.apache.shardingsphere.infra.session.connection.ConnectionContext; import org.apache.shardingsphere.sharding.exception.syntax.DataSourceIntersectionNotFoundException; import org.apache.shardingsphere.sharding.route.engine.type.ShardingRouteEngine; import org.apache.shardingsphere.sharding.rule.ShardingRule; @@ -51,6 +53,9 @@ public final class ShardingUnicastRoutingEngine implements ShardingRouteEngine { private final SQLStatementContext sqlStatementContext; + @SphereEx + private final String logicDatabaseName; + private final Collection<String> logicTables; private final ConnectionContext connectionContext; @@ -112,6 +117,9 @@ public final class ShardingUnicastRoutingEngine implements ShardingRouteEngine { private String getRandomDataSourceName(final Collection<String> dataSourceNames) { Collection<String> usedDataSourceNames = connectionContext.getUsedDataSourceNames(); List<String> availableDataSourceNames = new ArrayList<>(usedDataSourceNames.isEmpty() ? dataSourceNames : usedDataSourceNames); - return availableDataSourceNames.get(ThreadLocalRandom.current().nextInt(availableDataSourceNames.size())); + // SPEX CHANGED: BEGIN + List<String> enabledDataSourceNames = DataSourceStateManager.getInstance().getEnabledDataSourceNames(logicDatabaseName, availableDataSourceNames); + return enabledDataSourceNames.get(ThreadLocalRandom.current().nextInt(enabledDataSourceNames.size())); + // SPEX CHANGED: END } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngineTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngineTest.java index edb3a260902..a1d55537b22 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngineTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngineTest.java @@ -17,6 +17,8 @@ package org.apache.shardingsphere.sharding.route.engine.type.unicast; +import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.SphereEx.Type; import org.apache.groovy.util.Maps; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.ddl.CursorStatementContext; @@ -55,39 +57,49 @@ class ShardingUnicastRoutingEngineTest { @Test void assertRoutingForShardingTable() { - RouteContext actual = new ShardingUnicastRoutingEngine(mock(SQLStatementContext.class), Collections.singleton("t_order"), new ConnectionContext(Collections::emptySet)).route(shardingRule); + @SphereEx(Type.MODIFY) + RouteContext actual = new ShardingUnicastRoutingEngine( + mock(SQLStatementContext.class), "sharding_db", Collections.singleton("t_order"), new ConnectionContext(Collections::emptySet)).route(shardingRule); assertThat(actual.getRouteUnits().size(), is(1)); assertFalse("ds_2".equalsIgnoreCase(actual.getRouteUnits().iterator().next().getDataSourceMapper().getLogicName())); } @Test void assertRoutingForBroadcastTable() { - RouteContext actual = new ShardingUnicastRoutingEngine(mock(SQLStatementContext.class), Collections.singleton("t_config"), new ConnectionContext(Collections::emptySet)).route(shardingRule); + @SphereEx(Type.MODIFY) + RouteContext actual = new ShardingUnicastRoutingEngine( + mock(SQLStatementContext.class), "sharding_db", Collections.singleton("t_config"), new ConnectionContext(Collections::emptySet)).route(shardingRule); assertThat(actual.getRouteUnits().size(), is(1)); } @Test void assertRoutingForNoTable() { - RouteContext actual = new ShardingUnicastRoutingEngine(mock(SQLStatementContext.class), Collections.emptyList(), new ConnectionContext(Collections::emptySet)).route(shardingRule); + @SphereEx(Type.MODIFY) + RouteContext actual = new ShardingUnicastRoutingEngine( + mock(SQLStatementContext.class), "sharding_db", Collections.emptyList(), new ConnectionContext(Collections::emptySet)).route(shardingRule); assertThat(actual.getRouteUnits().size(), is(1)); } + @SphereEx(Type.MODIFY) @Test void assertRouteForWithNoIntersection() { assertThrows(ShardingTableRuleNotFoundException.class, () -> new ShardingUnicastRoutingEngine( - mock(SQLStatementContext.class), Arrays.asList("t_order", "t_config", "t_product"), new ConnectionContext(Collections::emptySet)).route(shardingRule)); + mock(SQLStatementContext.class), "sharding_db", Arrays.asList("t_order", "t_config", "t_product"), new ConnectionContext(Collections::emptySet)).route(shardingRule)); } @Test void assertRoutingForTableWithoutTableRule() { - RouteContext actual = new ShardingUnicastRoutingEngine(mock(SQLStatementContext.class), Collections.singleton("t_other"), new ConnectionContext(Collections::emptySet)).route(shardingRule); + @SphereEx(Type.MODIFY) + RouteContext actual = new ShardingUnicastRoutingEngine( + mock(SQLStatementContext.class), "sharding_db", Collections.singleton("t_other"), new ConnectionContext(Collections::emptySet)).route(shardingRule); assertThat(actual.getRouteUnits().size(), is(1)); } @Test void assertRoutingForBroadcastTableWithCursorStatement() { + @SphereEx(Type.MODIFY) RouteContext actual = new ShardingUnicastRoutingEngine( - mock(CursorStatementContext.class), Collections.singleton("t_config"), new ConnectionContext(Collections::emptySet)).route(shardingRule); + mock(CursorStatementContext.class), "sharding_db", Collections.singleton("t_config"), new ConnectionContext(Collections::emptySet)).route(shardingRule); assertThat(actual.getRouteUnits().size(), is(1)); assertThat(actual.getRouteUnits().iterator().next().getDataSourceMapper().getActualName(), is("ds_0")); } @@ -95,7 +107,8 @@ class ShardingUnicastRoutingEngineTest { @Test void assertRoutingForBroadcastTableWithPreferredDataSource() { ConnectionContext connectionContext = new ConnectionContext(() -> Collections.singleton("ds_1")); - RouteContext actual = new ShardingUnicastRoutingEngine(mock(SelectStatementContext.class), Collections.singleton("t_config"), connectionContext).route(shardingRule); + @SphereEx(Type.MODIFY) + RouteContext actual = new ShardingUnicastRoutingEngine(mock(SelectStatementContext.class), "sharding_db", Collections.singleton("t_config"), connectionContext).route(shardingRule); assertThat(actual.getRouteUnits().size(), is(1)); assertThat(actual.getRouteUnits().iterator().next().getDataSourceMapper().getActualName(), is("ds_1")); } diff --git a/infra/common/src/main/java/com/sphereex/dbplusengine/infra/state/datasource/DataSourceStateManager.java b/infra/common/src/main/java/com/sphereex/dbplusengine/infra/state/datasource/DataSourceStateManager.java index f85b226c1f8..ed0351bdbee 100644 --- a/infra/common/src/main/java/com/sphereex/dbplusengine/infra/state/datasource/DataSourceStateManager.java +++ b/infra/common/src/main/java/com/sphereex/dbplusengine/infra/state/datasource/DataSourceStateManager.java @@ -18,6 +18,7 @@ package com.sphereex.dbplusengine.infra.state.datasource; import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.SphereEx.Type; import com.sphereex.dbplusengine.infra.state.datasource.exception.UnavailableDataSourceException; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -54,7 +55,8 @@ public final class DataSourceStateManager { private volatile boolean forceStart; - private final AtomicBoolean initialized = new AtomicBoolean(false); + @SphereEx + private final Map<String, AtomicBoolean> databaseInitializedFlags = new ConcurrentHashMap<>(); /** * Get data source state manager. @@ -73,10 +75,13 @@ public final class DataSourceStateManager { * @param storageDataSourceStates storage node data source state * @param forceStart whether to force start */ + @SphereEx(Type.MODIFY) public void initStates(final String databaseName, final Map<String, StorageUnit> storageUnits, final Map<String, DataSourceState> storageDataSourceStates, final boolean forceStart) { this.forceStart = forceStart; + AtomicBoolean initialized = databaseInitializedFlags.getOrDefault(databaseName, new AtomicBoolean(false)); if (initialized.compareAndSet(false, true)) { storageUnits.forEach((key, value) -> initState(databaseName, storageDataSourceStates, key, value.getDataSource())); + databaseInitializedFlags.putIfAbsent(databaseName, initialized); } } @@ -89,12 +94,16 @@ public final class DataSourceStateManager { } } + @SphereEx(Type.MODIFY) private void checkState(final String databaseName, final String actualDataSourceName, final DataSource dataSource) { try (Connection ignored = dataSource.getConnection()) { dataSourceStates.put(getCacheKey(databaseName, actualDataSourceName), DataSourceState.ENABLED); - } catch (final SQLException ex) { - ShardingSpherePreconditions.checkState(forceStart, () -> new UnavailableDataSourceException(actualDataSourceName, ex)); - log.error("Data source unavailable, ignored with the -f parameter.", ex); + // CHECKSTYLE:OFF + } catch (final Exception ex) { + // CHECKSTYLE:ON + ShardingSpherePreconditions.checkState(forceStart, () -> new UnavailableDataSourceException(databaseName, actualDataSourceName, ex)); + log.error("Data source `{}` in `{}` is unavailable, ignored with the -f parameter.", actualDataSourceName, databaseName, ex); + dataSourceStates.put(getCacheKey(databaseName, actualDataSourceName), DataSourceState.DISABLED); } } @@ -119,12 +128,10 @@ public final class DataSourceStateManager { * @return enabled data sources */ public Map<String, DataSource> getEnabledDataSources(final String databaseName, final Map<String, DataSource> dataSources) { - if (dataSources.isEmpty() || !initialized.get()) { + if (dataSources.isEmpty() || !databaseInitializedFlags.getOrDefault(databaseName, new AtomicBoolean(false)).get()) { return dataSources; } - Map<String, DataSource> result = filterDisabledDataSources(databaseName, dataSources); - checkForceConnection(result); - return result; + return filterDisabledDataSources(databaseName, dataSources); } private Map<String, DataSource> filterDisabledDataSources(final String databaseName, final Map<String, DataSource> dataSources) { diff --git a/infra/common/src/main/java/com/sphereex/dbplusengine/infra/state/datasource/exception/UnavailableDataSourceException.java b/infra/common/src/main/java/com/sphereex/dbplusengine/infra/state/datasource/exception/UnavailableDataSourceException.java index 918473bcca0..efac36e4220 100644 --- a/infra/common/src/main/java/com/sphereex/dbplusengine/infra/state/datasource/exception/UnavailableDataSourceException.java +++ b/infra/common/src/main/java/com/sphereex/dbplusengine/infra/state/datasource/exception/UnavailableDataSourceException.java @@ -17,11 +17,8 @@ package com.sphereex.dbplusengine.infra.state.datasource.exception; -import com.sphereex.dbplusengine.SphereEx; import org.apache.shardingsphere.infra.exception.core.external.server.ShardingSphereServerException; -import java.sql.SQLException; - /** * Data source state exception. */ @@ -33,12 +30,7 @@ public final class UnavailableDataSourceException extends ShardingSphereServerEx private static final int ERROR_CODE = 1; - @SphereEx - public UnavailableDataSourceException(final String dataSourceName) { - super(ERROR_CATEGORY, ERROR_CODE, String.format("Data source '%s' is unavailable.", dataSourceName)); - } - - public UnavailableDataSourceException(final String dataSourceName, final SQLException cause) { - super(ERROR_CATEGORY, ERROR_CODE, String.format("Data source '%s' is unavailable.", dataSourceName), cause); + public UnavailableDataSourceException(final String databaseName, final String dataSourceName, final Exception cause) { + super(ERROR_CATEGORY, ERROR_CODE, String.format("Data source '%s' in database '%s' is unavailable.", dataSourceName, databaseName), cause); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java index 2472f46df61..a0baffd6315 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java @@ -17,6 +17,9 @@ package org.apache.shardingsphere.infra.database; +import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.SphereEx.Type; +import com.sphereex.dbplusengine.infra.state.datasource.DataSourceStateManager; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; @@ -24,9 +27,9 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory; -import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource; import org.apache.shardingsphere.infra.datasource.pool.hikari.metadata.HikariDataSourcePoolFieldMetaData; import org.apache.shardingsphere.infra.datasource.pool.hikari.metadata.HikariDataSourcePoolMetaData; +import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; import org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.infra.util.reflection.ReflectionUtils; @@ -40,7 +43,6 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; -import java.util.stream.Collectors; /** * Database type engine. @@ -55,14 +57,16 @@ public final class DatabaseTypeEngine { * * @param databaseConfig database configuration * @param props configuration properties + * @param databaseName database name * @return protocol type */ - public static DatabaseType getProtocolType(final DatabaseConfiguration databaseConfig, final ConfigurationProperties props) { + public static DatabaseType getProtocolType(final DatabaseConfiguration databaseConfig, final ConfigurationProperties props, @SphereEx final String databaseName) { Optional<DatabaseType> configuredDatabaseType = findConfiguredDatabaseType(props); if (configuredDatabaseType.isPresent()) { return configuredDatabaseType.get(); } - Collection<DataSource> dataSources = getDataSources(databaseConfig).values(); + @SphereEx(Type.MODIFY) + Collection<DataSource> dataSources = getDataSources(databaseConfig, databaseName).values(); return dataSources.isEmpty() ? getDefaultStorageType() : getStorageType(dataSources.iterator().next()); } @@ -90,25 +94,29 @@ public final class DatabaseTypeEngine { private static Map<String, DataSource> getDataSources(final Map<String, ? extends DatabaseConfiguration> databaseConfigs) { Map<String, DataSource> result = new LinkedHashMap<>(); for (Entry<String, ? extends DatabaseConfiguration> entry : databaseConfigs.entrySet()) { - result.putAll(getDataSources(entry.getValue())); + // SPEX CHANGED: BEGIN + result.putAll(getDataSources(entry.getValue(), entry.getKey())); + // SPEX CHANGED: END } return result; } - private static Map<String, DataSource> getDataSources(final DatabaseConfiguration databaseConfig) { - return databaseConfig.getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + @SphereEx(Type.MODIFY) + private static Map<String, DataSource> getDataSources(final DatabaseConfiguration databaseConfig, @SphereEx final String databaseName) { + return DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, databaseConfig); } /** * Get storage types. * * @param databaseConfig database configuration + * @param databaseName database name * @return storage types */ - public static Map<String, DatabaseType> getStorageTypes(final DatabaseConfiguration databaseConfig) { + public static Map<String, DatabaseType> getStorageTypes(final DatabaseConfiguration databaseConfig, @SphereEx final String databaseName) { Map<String, DatabaseType> result = new LinkedHashMap<>(databaseConfig.getStorageUnits().size(), 1F); - Map<String, DataSource> dataSources = getDataSources(databaseConfig); + @SphereEx(Type.MODIFY) + Map<String, DataSource> dataSources = getDataSources(databaseConfig, databaseName); for (Entry<String, DataSource> entry : dataSources.entrySet()) { result.put(entry.getKey(), getStorageType(entry.getValue())); } @@ -118,7 +126,6 @@ public final class DatabaseTypeEngine { /** * Get storage type. * Similar to apache/hive 4.0.0's `org.apache.hive.jdbc.HiveDatabaseMetaData`, it does not implement {@link java.sql.DatabaseMetaData#getURL()}. - * So use {@link CatalogSwitchableDataSource#getUrl()} and {@link ReflectionUtils#getFieldValue(Object, String)} to try fuzzy matching. * * @param dataSource data source * @return storage type @@ -128,9 +135,6 @@ public final class DatabaseTypeEngine { try (Connection connection = dataSource.getConnection()) { return DatabaseTypeFactory.get(connection.getMetaData().getURL()); } catch (final SQLFeatureNotSupportedException sqlFeatureNotSupportedException) { - if (dataSource instanceof CatalogSwitchableDataSource) { - return DatabaseTypeFactory.get(((CatalogSwitchableDataSource) dataSource).getUrl()); - } if (dataSource.getClass().getName().equals(new HikariDataSourcePoolMetaData().getType())) { HikariDataSourcePoolFieldMetaData dataSourcePoolFieldMetaData = new HikariDataSourcePoolFieldMetaData(); String jdbcUrlFieldName = ReflectionUtils.<String>getFieldValue(dataSource, dataSourcePoolFieldMetaData.getJdbcUrlFieldName()) @@ -139,6 +143,11 @@ public final class DatabaseTypeEngine { } throw new SQLWrapperException(sqlFeatureNotSupportedException); } catch (final SQLException ex) { + // SPEX ADDED: BEGIN + if ("Method not supported".equalsIgnoreCase(ex.getMessage())) { + return DatabaseTypeFactory.get(String.valueOf(DataSourcePoolPropertiesCreator.create(dataSource).getAllStandardProperties().get("url"))); + } + // SPEX ADDED: END throw new SQLWrapperException(ex); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java index af6b6be0e05..569fc3242d6 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java @@ -18,6 +18,9 @@ package org.apache.shardingsphere.infra.metadata.database; import com.cedarsoftware.util.CaseInsensitiveMap; +import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.SphereEx.Type; +import com.sphereex.dbplusengine.infra.state.datasource.DataSourceStateManager; import lombok.Getter; import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration; @@ -90,9 +93,10 @@ public final class ShardingSphereDatabase { final ComputeNodeInstanceContext computeNodeInstanceContext) throws SQLException { ResourceMetaData resourceMetaData = createResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits()); Collection<ShardingSphereRule> databaseRules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, computeNodeInstanceContext, resourceMetaData); + @SphereEx(Type.MODIFY) Map<String, ShardingSphereSchema> schemas = new ConcurrentHashMap<>(GenericSchemaBuilder - .build(new GenericSchemaBuilderMaterial(protocolType, storageTypes, resourceMetaData.getDataSourceMap(), databaseRules, - props, new DatabaseTypeRegistry(protocolType).getDefaultSchemaName(name)))); + .build(new GenericSchemaBuilderMaterial(protocolType, storageTypes, DataSourceStateManager.getInstance().getEnabledDataSources(name, databaseConfig), databaseRules, + props, new DatabaseTypeRegistry(protocolType).getDefaultSchemaName(name), computeNodeInstanceContext))); SystemSchemaBuilder.build(name, protocolType, props).forEach(schemas::putIfAbsent); return create(name, protocolType, databaseRules, schemas, resourceMetaData); } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java index 3aeb4f5e8d1..20170ffa77b 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java @@ -17,10 +17,12 @@ package org.apache.shardingsphere.infra.metadata.database.schema.builder; +import com.sphereex.dbplusengine.SphereEx; import lombok.Getter; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; @@ -50,13 +52,29 @@ public final class GenericSchemaBuilderMaterial { private final String defaultSchemaName; + @SphereEx + private final ComputeNodeInstanceContext computeNodeInstanceContext; + public GenericSchemaBuilderMaterial(final DatabaseType protocolType, final Map<String, StorageUnit> storageUnits, - final Collection<ShardingSphereRule> rules, final ConfigurationProperties props, final String defaultSchemaName) { + final Collection<ShardingSphereRule> rules, final ConfigurationProperties props, final String defaultSchemaName, + @SphereEx final ComputeNodeInstanceContext computeNodeInstanceContext) { this(protocolType, storageUnits.entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageType(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), storageUnits.entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), - rules, props, defaultSchemaName); + rules, props, defaultSchemaName, computeNodeInstanceContext); + } + + @SphereEx + public GenericSchemaBuilderMaterial(final DatabaseType protocolType, final Map<String, DatabaseType> storageTypes, final Map<String, DataSource> dataSourceMap, + final Collection<ShardingSphereRule> rules, final ConfigurationProperties props, final String defaultSchemaName) { + this.protocolType = protocolType; + this.storageTypes = storageTypes; + this.dataSourceMap = dataSourceMap; + this.rules = rules; + this.props = props; + this.defaultSchemaName = defaultSchemaName; + computeNodeInstanceContext = null; } /** diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngineTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngineTest.java index bc49a70a10e..e0a0e4bdbdc 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngineTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngineTest.java @@ -53,24 +53,30 @@ class DatabaseTypeEngineTest { void assertGetProtocolTypeFromConfiguredProperties() { Properties props = PropertiesBuilder.build(new Property(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE.getKey(), "MySQL")); DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(), Collections.singleton(new FixtureRuleConfiguration())); - assertThat(DatabaseTypeEngine.getProtocolType(databaseConfig, new ConfigurationProperties(props)), instanceOf(MySQLDatabaseType.class)); + // SPEX CHANGED: BEGIN + assertThat(DatabaseTypeEngine.getProtocolType(databaseConfig, new ConfigurationProperties(props), "sharding_db"), instanceOf(MySQLDatabaseType.class)); assertThat(DatabaseTypeEngine.getProtocolType(Collections.singletonMap("foo_db", databaseConfig), new ConfigurationProperties(props)), instanceOf(MySQLDatabaseType.class)); + // SPEX CHANGED: END } @Test void assertGetProtocolTypeFromDataSource() throws SQLException { DataSource datasource = mockDataSource(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")); DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("foo_ds", datasource), Collections.singleton(new FixtureRuleConfiguration())); - assertThat(DatabaseTypeEngine.getProtocolType(databaseConfig, new ConfigurationProperties(new Properties())), instanceOf(PostgreSQLDatabaseType.class)); + // SPEX CHANGED: BEGIN + assertThat(DatabaseTypeEngine.getProtocolType(databaseConfig, new ConfigurationProperties(new Properties()), "sharding_db"), instanceOf(PostgreSQLDatabaseType.class)); assertThat(DatabaseTypeEngine.getProtocolType(Collections.singletonMap("foo_db", databaseConfig), new ConfigurationProperties(new Properties())), instanceOf(PostgreSQLDatabaseType.class)); + // SPEX CHANGED: END } @Test void assertGetStorageTypes() throws SQLException { DataSource datasource = mockDataSource(TypedSPILoader.getService(DatabaseType.class, "MySQL")); DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("foo_db", datasource), Collections.singletonList(new FixtureRuleConfiguration())); - assertTrue(DatabaseTypeEngine.getStorageTypes(databaseConfig).containsKey("foo_db")); - assertThat(DatabaseTypeEngine.getStorageTypes(databaseConfig).get("foo_db"), instanceOf(MySQLDatabaseType.class)); + // SPEX CHANGED: BEGIN + assertTrue(DatabaseTypeEngine.getStorageTypes(databaseConfig, "foo_db").containsKey("foo_db")); + assertThat(DatabaseTypeEngine.getStorageTypes(databaseConfig, "foo_db").get("foo_db"), instanceOf(MySQLDatabaseType.class)); + // SPEX CHANGED: END } @Test diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactory.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactory.java index 3746fd2d408..ff210c2c48b 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactory.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactory.java @@ -17,6 +17,8 @@ package org.apache.shardingsphere.metadata.factory; +import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.SphereEx.Type; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; @@ -49,10 +51,11 @@ public final class ExternalMetaDataFactory { * @return database meta data * @throws SQLException SQL exception */ + @SphereEx(Type.MODIFY) public static ShardingSphereDatabase create(final String databaseName, final DatabaseConfiguration databaseConfig, final ConfigurationProperties props, final ComputeNodeInstanceContext computeNodeInstanceContext) throws SQLException { - return ShardingSphereDatabase.create(databaseName, DatabaseTypeEngine.getProtocolType(databaseConfig, props), - DatabaseTypeEngine.getStorageTypes(databaseConfig), databaseConfig, props, computeNodeInstanceContext); + return ShardingSphereDatabase.create(databaseName, DatabaseTypeEngine.getProtocolType(databaseConfig, props, databaseName), + DatabaseTypeEngine.getStorageTypes(databaseConfig, databaseName), databaseConfig, props, computeNodeInstanceContext); } /** @@ -81,7 +84,8 @@ public final class ExternalMetaDataFactory { for (Entry<String, DatabaseConfiguration> entry : databaseConfigMap.entrySet()) { String databaseName = entry.getKey(); if (!entry.getValue().getStorageUnits().isEmpty() || !systemDatabase.getSystemSchemas().contains(databaseName)) { - Map<String, DatabaseType> storageTypes = DatabaseTypeEngine.getStorageTypes(entry.getValue()); + @SphereEx(Type.MODIFY) + Map<String, DatabaseType> storageTypes = DatabaseTypeEngine.getStorageTypes(entry.getValue(), databaseName); result.put(databaseName.toLowerCase(), ShardingSphereDatabase.create(databaseName, protocolType, storageTypes, entry.getValue(), props, computeNodeInstanceContext)); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java index d19ba8b4aa7..1ae74715471 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java @@ -17,6 +17,8 @@ package org.apache.shardingsphere.metadata.factory; +import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.SphereEx.Type; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; @@ -49,7 +51,8 @@ public final class InternalMetaDataFactory { */ public static ShardingSphereDatabase create(final String databaseName, final MetaDataPersistService persistService, final DatabaseConfiguration databaseConfig, final ConfigurationProperties props, final ComputeNodeInstanceContext computeNodeInstanceContext) { - DatabaseType protocolType = DatabaseTypeEngine.getProtocolType(databaseConfig, props); + @SphereEx(Type.MODIFY) + DatabaseType protocolType = DatabaseTypeEngine.getProtocolType(databaseConfig, props, databaseName); return ShardingSphereDatabase.create(databaseName, protocolType, databaseConfig, computeNodeInstanceContext, persistService.getDatabaseMetaDataFacade().getSchema().load(databaseName)); } diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/decorator/SingleRuleConfigurationDecorator.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/decorator/SingleRuleConfigurationDecorator.java index 78e4cdb3911..5162eb3221f 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/decorator/SingleRuleConfigurationDecorator.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/decorator/SingleRuleConfigurationDecorator.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.single.decorator; +import com.sphereex.dbplusengine.infra.state.datasource.DataSourceStateManager; import org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator; import org.apache.shardingsphere.infra.database.DatabaseTypeEngine; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; @@ -63,8 +64,11 @@ public final class SingleRuleConfigurationDecorator implements RuleConfiguration if (!isExpandRequired(splitTables)) { return splitTables; } - Map<String, DataSource> aggregatedDataSources = PhysicalResourceAggregator.getAggregatedResources(dataSources, builtRules); - DatabaseType databaseType = dataSources.isEmpty() ? DatabaseTypeEngine.getDefaultStorageType() : DatabaseTypeEngine.getStorageType(dataSources.values().iterator().next()); + // SPEX CHANGED: BEGIN + Map<String, DataSource> enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, dataSources); + Map<String, DataSource> aggregatedDataSources = PhysicalResourceAggregator.getAggregatedResources(enabledDataSources, builtRules); + DatabaseType databaseType = enabledDataSources.isEmpty() ? DatabaseTypeEngine.getDefaultStorageType() : DatabaseTypeEngine.getStorageType(enabledDataSources.values().iterator().next()); + // SPEX CHANGED: END Collection<String> excludedTables = SingleTableLoadUtils.getExcludedTables(builtRules); Map<String, Collection<DataNode>> actualDataNodes = SingleTableDataNodeLoader.load(databaseName, aggregatedDataSources, excludedTables); boolean isSchemaSupportedDatabaseType = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData().getDefaultSchema().isPresent(); diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleRule.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleRule.java index 473026ea5a4..1423a0f7459 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleRule.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleRule.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.single.rule; import com.cedarsoftware.util.CaseInsensitiveSet; +import com.sphereex.dbplusengine.infra.state.datasource.DataSourceStateManager; import lombok.Getter; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.type.IndexAvailable; @@ -71,7 +72,10 @@ public final class SingleRule implements DatabaseRule { final DatabaseType protocolType, final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> builtRules) { configuration = ruleConfig; defaultDataSource = ruleConfig.getDefaultDataSource().orElse(null); - Map<String, DataSource> aggregateDataSourceMap = PhysicalResourceAggregator.getAggregatedResources(dataSourceMap, builtRules); + // SPEX CHANGED: BEGIN + Map<String, DataSource> enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, dataSourceMap); + Map<String, DataSource> aggregateDataSourceMap = PhysicalResourceAggregator.getAggregatedResources(enabledDataSources, builtRules); + // SPEX CHANGED: END dataSourceNames = new CaseInsensitiveSet<>(aggregateDataSourceMap.keySet()); this.protocolType = protocolType; singleTableDataNodes = SingleTableDataNodeLoader.load(databaseName, protocolType, aggregateDataSourceMap, builtRules, configuration.getTables()); diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java index 644adff1042..908a0046966 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java @@ -123,7 +123,7 @@ public final class LoadSingleTableExecutor implements DatabaseRuleCreateExecutor if (!SingleTableConstants.ASTERISK.equals(tableName)) { String storageUnitName = each.getStorageUnitName(); ShardingSpherePreconditions.checkState(actualTableNodes.containsKey(storageUnitName) && actualTableNodes.get(storageUnitName).get(defaultSchemaName).contains(tableName), - () -> new TableNotFoundException(storageUnitName, tableName)); + () -> new TableNotFoundException(tableName, storageUnitName)); } } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java index 9219e2a4587..f6be74240b5 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.mode.manager; import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.SphereEx.Type; import com.sphereex.dbplusengine.mode.lock.ResourceLockDefinitionManager; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -162,8 +163,9 @@ public final class ContextManager implements AutoCloseable { * @param tableName to be reloaded table name */ public void reloadTable(final ShardingSphereDatabase database, final String schemaName, final String tableName) { + @SphereEx(Type.MODIFY) GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(database.getProtocolType(), - database.getResourceMetaData().getStorageUnits(), database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName); + database.getResourceMetaData().getStorageUnits(), database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName, computeNodeInstanceContext); try { persistTable(database, schemaName, tableName, material); } catch (final SQLException ex) { diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/table/AlterTableStatementSchemaRefresher.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/table/AlterTableStatementSchemaRefresher.java index e83fe46c790..c52bcced707 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/table/AlterTableStatementSchemaRefresher.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/table/AlterTableStatementSchemaRefresher.java @@ -17,6 +17,8 @@ package org.apache.shardingsphere.mode.metadata.refresher.type.table; +import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.SphereEx.Type; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -65,8 +67,9 @@ public final class AlterTableStatementSchemaRefresher implements MetaDataRefresh if (TableRefreshUtils.isSingleTable(tableName, database)) { ruleMetaData.getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, tableName)); } + @SphereEx(Type.MODIFY) GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial( - database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName); + database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName, null); Map<String, ShardingSphereSchema> schemaMap = GenericSchemaBuilder.build(Collections.singletonList(tableName), material); return Optional.ofNullable(schemaMap.get(schemaName)).map(optional -> optional.getTable(tableName)) .orElseGet(() -> new ShardingSphereTable(tableName, Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/table/CreateTableStatementSchemaRefresher.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/table/CreateTableStatementSchemaRefresher.java index 57823f37c5f..4d8eb2c1154 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/table/CreateTableStatementSchemaRefresher.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/table/CreateTableStatementSchemaRefresher.java @@ -18,6 +18,8 @@ package org.apache.shardingsphere.mode.metadata.refresher.type.table; import com.google.common.base.Preconditions; +import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.SphereEx.Type; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -53,8 +55,9 @@ public final class CreateTableStatementSchemaRefresher implements MetaDataRefres if (isSingleTable) { ruleMetaData.getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, tableName)); } + @SphereEx(Type.MODIFY) GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial( - database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName); + database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName, null); Map<String, ShardingSphereSchema> schemaMap = GenericSchemaBuilder.build(Collections.singletonList(tableName), material); Optional<ShardingSphereTable> actualTableMetaData = Optional.ofNullable(schemaMap.get(schemaName)).map(optional -> optional.getTable(tableName)); Preconditions.checkState(actualTableMetaData.isPresent(), "Load actual table metadata '%s' failed.", tableName); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/table/RenameTableStatementSchemaRefresher.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/table/RenameTableStatementSchemaRefresher.java index a1e133e948b..714e55bacb6 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/table/RenameTableStatementSchemaRefresher.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/table/RenameTableStatementSchemaRefresher.java @@ -17,6 +17,8 @@ package org.apache.shardingsphere.mode.metadata.refresher.type.table; +import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.SphereEx.Type; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -63,8 +65,9 @@ public final class RenameTableStatementSchemaRefresher implements MetaDataRefres if (TableRefreshUtils.isSingleTable(tableName, database) && !logicDataSourceNames.isEmpty()) { ruleMetaData.getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, tableName)); } + @SphereEx(Type.MODIFY) GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial( - database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName); + database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName, null); Map<String, ShardingSphereSchema> schemaMap = GenericSchemaBuilder.build(Collections.singletonList(tableName), material); return Optional.ofNullable(schemaMap.get(schemaName)).map(optional -> optional.getTable(tableName)) .orElseGet(() -> new ShardingSphereTable(tableName, Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/view/AlterViewStatementSchemaRefresher.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/view/AlterViewStatementSchemaRefresher.java index bfc994501d9..f631e97c2e3 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/view/AlterViewStatementSchemaRefresher.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/view/AlterViewStatementSchemaRefresher.java @@ -17,6 +17,8 @@ package org.apache.shardingsphere.mode.metadata.refresher.type.view; +import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.SphereEx.Type; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -76,8 +78,9 @@ public final class AlterViewStatementSchemaRefresher implements MetaDataRefreshe if (TableRefreshUtils.isSingleTable(viewName, database)) { ruleMetaData.getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, viewName)); } + @SphereEx(Type.MODIFY) GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial( - database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName); + database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName, null); Map<String, ShardingSphereSchema> schemaMap = GenericSchemaBuilder.build(Collections.singletonList(viewName), material); Optional<ShardingSphereTable> actualViewMetaData = Optional.ofNullable(schemaMap.get(schemaName)).map(optional -> optional.getTable(viewName)); ShardingSphereSchema result = new ShardingSphereSchema(schemaName); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/view/CreateViewStatementSchemaRefresher.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/view/CreateViewStatementSchemaRefresher.java index 07fe62b6454..3315f578bb7 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/view/CreateViewStatementSchemaRefresher.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/type/view/CreateViewStatementSchemaRefresher.java @@ -18,6 +18,8 @@ package org.apache.shardingsphere.mode.metadata.refresher.type.view; import com.google.common.base.Preconditions; +import com.sphereex.dbplusengine.SphereEx; +import com.sphereex.dbplusengine.SphereEx.Type; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -54,8 +56,9 @@ public final class CreateViewStatementSchemaRefresher implements MetaDataRefresh if (TableRefreshUtils.isSingleTable(viewName, database)) { ruleMetaData.getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, viewName)); } + @SphereEx(Type.MODIFY) GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial( - database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName); + database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName, null); Map<String, ShardingSphereSchema> schemaMap = GenericSchemaBuilder.build(Collections.singletonList(viewName), material); Optional<ShardingSphereTable> actualTableMetaData = Optional.ofNullable(schemaMap.get(schemaName)).map(optional -> optional.getTable(viewName)); Preconditions.checkState(actualTableMetaData.isPresent(), "Load actual view metadata '%s' failed.", viewName); diff --git a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java index 52306024517..71de4f4de05 100644 --- a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java +++ b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.proxy.version; import com.google.common.base.Strings; +import com.sphereex.dbplusengine.infra.state.datasource.DataSourceStateManager; import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -77,6 +78,8 @@ public final class ShardingSphereProxyVersion { .stream().filter(entry -> entry.getValue().getStorageType().equals(database.getProtocolType())).map(Entry::getKey).findFirst(); Map<String, DataSource> dataSources = database.getResourceMetaData().getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - return dataSourceName.flatMap(optional -> Optional.ofNullable(dataSources.get(optional))); + // SPEX CHANGED: BEGIN + return dataSourceName.flatMap(optional -> Optional.ofNullable(DataSourceStateManager.getInstance().getEnabledDataSources(database.getName(), dataSources).get(optional))); + // SPEX CHANGED: END } }
