From ebe58d252e428d1d68248d81cb034ccc1e034aa4 Mon Sep 17 00:00:00 2001 From: Haoran Meng Date: Fri, 8 Oct 2021 12:48:05 +0800 Subject: [PATCH] Refresh actual meta data for DDL (#12927) --- .../refresher/MetaDataRefreshEngine.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/refresher/MetaDataRefreshEngine.java b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/refresher/MetaDataRefreshEngine.java index 9924dc0ad13c7..209e7367d5778 100644 --- a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/refresher/MetaDataRefreshEngine.java +++ b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/refresher/MetaDataRefreshEngine.java @@ -17,21 +17,27 @@ package org.apache.shardingsphere.infra.context.refresher; +import org.apache.shardingsphere.infra.config.RuleConfiguration; import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus; import org.apache.shardingsphere.infra.metadata.MetaDataRefresher; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.mapper.SQLStatementEventMapper; import org.apache.shardingsphere.infra.metadata.mapper.SQLStatementEventMapperFactory; +import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema; import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials; +import org.apache.shardingsphere.infra.metadata.schema.loader.SchemaLoader; import org.apache.shardingsphere.infra.metadata.schema.refresher.SchemaRefresher; import org.apache.shardingsphere.infra.metadata.schema.refresher.event.SchemaAlteredEvent; import org.apache.shardingsphere.infra.optimize.metadata.FederationSchemaMetaData; import org.apache.shardingsphere.infra.optimize.metadata.refresher.FederationMetaDataRefresher; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; +import javax.sql.DataSource; import java.sql.SQLException; import java.util.Collection; +import java.util.Collections; +import java.util.Map; import java.util.Optional; /** @@ -45,9 +51,12 @@ public final class MetaDataRefreshEngine { private final SchemaBuilderMaterials materials; + private final ConfigurationProperties props; + public MetaDataRefreshEngine(final ShardingSphereMetaData schemaMetaData, final FederationSchemaMetaData federationMetaData, final ConfigurationProperties props) { this.schemaMetaData = schemaMetaData; this.federationMetaData = federationMetaData; + this.props = props; materials = new SchemaBuilderMaterials(schemaMetaData.getResource().getDatabaseType(), schemaMetaData.getResource().getDataSources(), schemaMetaData.getRuleMetaData().getRules(), props); } @@ -80,6 +89,13 @@ private void refresh(final SQLStatement sqlStatement, final Collection l ((FederationMetaDataRefresher) each).refresh(federationMetaData, logicDataSourceNames, sqlStatement, materials); } } - ShardingSphereEventBus.getInstance().post(new SchemaAlteredEvent(schemaMetaData.getName(), schemaMetaData.getSchema())); + ShardingSphereEventBus.getInstance().post(new SchemaAlteredEvent(schemaMetaData.getName(), loadActualSchema(schemaMetaData))); + } + + private ShardingSphereSchema loadActualSchema(final ShardingSphereMetaData schemaMetaData) throws SQLException { + Map> dataSourcesMap = Collections.singletonMap(schemaMetaData.getName(), schemaMetaData.getResource().getDataSources()); + Map> schemaRuleConfigs = Collections.singletonMap(schemaMetaData.getName(), schemaMetaData.getRuleMetaData().getConfigurations()); + Map schemas = new SchemaLoader(dataSourcesMap, schemaRuleConfigs, props.getProps()).load(); + return schemas.get(schemaMetaData.getName()); } }