Skip to content

Commit

Permalink
for #2900, remove @SneakyThrows for TableMetaDataInitializer
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Aug 21, 2019
1 parent ddf43a7 commit 8e2102d
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private List<DataNode> generateDataNodes(final List<String> actualDataNodes, fin
/**
* Get data node groups.
*
* @return data node groups, key is data source name, value is tables belong to this data source
* @return data node groups, key is data source name, value is data nodes belong to this data source
*/
public Map<String, List<DataNode>> getDataNodeGroups() {
Map<String, List<DataNode>> result = new LinkedHashMap<>(actualDataNodes.size(), 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.shardingsphere.core.execute.metadata;

import com.google.common.base.Optional;
import lombok.SneakyThrows;
import org.apache.shardingsphere.core.execute.ShardingExecuteEngine;
import org.apache.shardingsphere.core.metadata.datasource.DataSourceMetas;
import org.apache.shardingsphere.core.metadata.table.impl.TableMetaData;
Expand Down Expand Up @@ -61,9 +60,9 @@ public TableMetaDataInitializer(final DataSourceMetas dataSourceMetas, final Sha
* @param logicTableName logic table name
* @param shardingRule sharding rule
* @return table meta data
* @throws SQLException SQL exception
*/
@SneakyThrows
public TableMetaData load(final String logicTableName, final ShardingRule shardingRule) {
public TableMetaData load(final String logicTableName, final ShardingRule shardingRule) throws SQLException {
return tableMetaDataLoader.load(logicTableName, shardingRule);
}

Expand All @@ -72,9 +71,9 @@ public TableMetaData load(final String logicTableName, final ShardingRule shardi
*
* @param shardingRule sharding rule
* @return all table meta data
* @throws SQLException SQL exception
*/
@SneakyThrows
public Map<String, TableMetaData> load(final ShardingRule shardingRule) {
public Map<String, TableMetaData> load(final ShardingRule shardingRule) throws SQLException {
Map<String, TableMetaData> result = new HashMap<>();
result.putAll(loadShardingTables(shardingRule));
result.putAll(loadDefaultTables(shardingRule));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.shardingsphere.core.rule.DataNode;
import org.apache.shardingsphere.core.rule.ShardingDataSourceNames;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.core.rule.TableRule;
import org.apache.shardingsphere.spi.database.DataSourceMetaData;

import java.sql.Connection;
Expand Down Expand Up @@ -71,13 +72,13 @@ public final class TableMetaDataLoader {
* @throws SQLException SQL exception
*/
public TableMetaData load(final String logicTableName, final ShardingRule shardingRule) throws SQLException {
List<TableMetaData> actualTableMetaDataList = load(getDataNodeGroups(logicTableName, shardingRule), shardingRule.getShardingDataSourceNames());
List<TableMetaData> actualTableMetaDataList = load(getDataNodeGroups(shardingRule.getTableRule(logicTableName)), shardingRule.getShardingDataSourceNames());
checkUniformed(logicTableName, actualTableMetaDataList);
return actualTableMetaDataList.iterator().next();
}

private List<TableMetaData> load(final Map<String, List<DataNode>> dataNodeGroups, final ShardingDataSourceNames shardingDataSourceNames) throws SQLException {
return executeEngine.groupExecute(getDataNodeGroups(dataNodeGroups), new ShardingGroupExecuteCallback<DataNode, TableMetaData>() {
return executeEngine.groupExecute(getDataNodeExecuteGroups(dataNodeGroups), new ShardingGroupExecuteCallback<DataNode, TableMetaData>() {

@Override
public Collection<TableMetaData> execute(final Collection<DataNode> dataNodes, final boolean isTrunkThread, final Map<String, Object> shardingExecuteDataMap) throws SQLException {
Expand All @@ -99,24 +100,24 @@ private Collection<TableMetaData> load(final String dataSourceName, final String
return result;
}

private Map<String, List<DataNode>> getDataNodeGroups(final String logicTableName, final ShardingRule shardingRule) {
Map<String, List<DataNode>> result = shardingRule.getTableRule(logicTableName).getDataNodeGroups();
if (isCheckingMetaData) {
return result;
}
String firstKey = result.keySet().iterator().next();
return Collections.singletonMap(firstKey, Collections.singletonList(result.get(firstKey).get(0)));
private Map<String, List<DataNode>> getDataNodeGroups(final TableRule tableRule) {
return isCheckingMetaData ? tableRule.getDataNodeGroups() : getFirstDataNodeWithGroups(tableRule);
}

private Map<String, List<DataNode>> getFirstDataNodeWithGroups(final TableRule tableRule) {
DataNode firstDataNode = tableRule.getActualDataNodes().iterator().next();
return Collections.singletonMap(firstDataNode.getDataSourceName(), Collections.singletonList(firstDataNode));
}

private Collection<ShardingExecuteGroup<DataNode>> getDataNodeGroups(final Map<String, List<DataNode>> dataNodeGroups) {
private Collection<ShardingExecuteGroup<DataNode>> getDataNodeExecuteGroups(final Map<String, List<DataNode>> dataNodeGroups) {
Collection<ShardingExecuteGroup<DataNode>> result = new LinkedList<>();
for (Entry<String, List<DataNode>> entry : dataNodeGroups.entrySet()) {
result.addAll(getDataNodeGroups(entry.getValue()));
result.addAll(getDataNodeExecuteGroups(entry.getValue()));
}
return result;
}

private Collection<ShardingExecuteGroup<DataNode>> getDataNodeGroups(final List<DataNode> dataNodes) {
private Collection<ShardingExecuteGroup<DataNode>> getDataNodeExecuteGroups(final List<DataNode> dataNodes) {
Collection<ShardingExecuteGroup<DataNode>> result = new LinkedList<>();
for (List<DataNode> each : Lists.partition(dataNodes, Math.max(dataNodes.size() / maxConnectionsSizePerQuery, 1))) {
result.add(new ShardingExecuteGroup<>(each));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void clearStatements() throws SQLException {
}
}

private void refreshMetaDataIfNeeded(final ShardingRuntimeContext runtimeContext, final OptimizedStatement optimizedStatement) {
private void refreshMetaDataIfNeeded(final ShardingRuntimeContext runtimeContext, final OptimizedStatement optimizedStatement) throws SQLException {
if (null == optimizedStatement) {
return;
}
Expand All @@ -175,12 +175,12 @@ private void refreshMetaDataIfNeeded(final ShardingRuntimeContext runtimeContext
}
}

private void refreshTableMetaDataForCreateTable(final ShardingRuntimeContext runtimeContext, final OptimizedStatement optimizedStatement) {
private void refreshTableMetaDataForCreateTable(final ShardingRuntimeContext runtimeContext, final OptimizedStatement optimizedStatement) throws SQLException {
String tableName = optimizedStatement.getTables().getSingleTableName();
runtimeContext.getMetaData().getTables().put(tableName, getTableMetaDataInitializer().load(tableName, runtimeContext.getRule()));
}

private void refreshTableMetaDataForAlterTable(final ShardingRuntimeContext runtimeContext, final OptimizedStatement optimizedStatement) {
private void refreshTableMetaDataForAlterTable(final ShardingRuntimeContext runtimeContext, final OptimizedStatement optimizedStatement) throws SQLException {
String tableName = optimizedStatement.getTables().getSingleTableName();
runtimeContext.getMetaData().getTables().put(tableName, getTableMetaDataInitializer().load(tableName, runtimeContext.getRule()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.shardingsphere.shardingproxy.context.ShardingProxyContext;
import org.apache.shardingsphere.shardingproxy.util.DataSourceConverter;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -119,7 +120,8 @@ public final synchronized void renew(final DataSourceChangedEvent dataSourceChan
* Refresh table meta data.
*
* @param optimizedStatement optimized statement
* @throws SQLException SQL exception
*/
public void refreshTableMetaData(final OptimizedStatement optimizedStatement) {
public void refreshTableMetaData(final OptimizedStatement optimizedStatement) throws SQLException {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.shardingsphere.shardingproxy.util.DataSourceConverter;
import org.apache.shardingsphere.spi.database.DatabaseType;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
Expand Down Expand Up @@ -80,8 +81,9 @@ public static LogicSchemas getInstance() {
*
* @param schemaDataSources data source map
* @param schemaRules schema rule map
* @throws SQLException SQL exception
*/
public void init(final Map<String, Map<String, YamlDataSourceParameter>> schemaDataSources, final Map<String, RuleConfiguration> schemaRules) {
public void init(final Map<String, Map<String, YamlDataSourceParameter>> schemaDataSources, final Map<String, RuleConfiguration> schemaRules) throws SQLException {
init(schemaRules.keySet(), schemaDataSources, schemaRules, false);
}

Expand All @@ -92,16 +94,17 @@ public void init(final Map<String, Map<String, YamlDataSourceParameter>> schemaD
* @param schemaDataSources data source map
* @param schemaRules schema rule map
* @param isUsingRegistry is using registry or not
* @throws SQLException SQL exception
*/
public void init(final Collection<String> localSchemaNames, final Map<String, Map<String, YamlDataSourceParameter>> schemaDataSources,
final Map<String, RuleConfiguration> schemaRules, final boolean isUsingRegistry) {
final Map<String, RuleConfiguration> schemaRules, final boolean isUsingRegistry) throws SQLException {
databaseType = DatabaseTypes.getActualDatabaseType(
JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer(schemaDataSources.values().iterator().next().values().iterator().next().getUrl()).getDatabaseType());
initSchemas(localSchemaNames, schemaDataSources, schemaRules, isUsingRegistry);
}

private void initSchemas(final Collection<String> localSchemaNames,
final Map<String, Map<String, YamlDataSourceParameter>> schemaDataSources, final Map<String, RuleConfiguration> schemaRules, final boolean isUsingRegistry) {
private void initSchemas(final Collection<String> localSchemaNames, final Map<String, Map<String, YamlDataSourceParameter>> schemaDataSources,
final Map<String, RuleConfiguration> schemaRules, final boolean isUsingRegistry) throws SQLException {
if (schemaRules.isEmpty()) {
logicSchemas.put(schemaDataSources.keySet().iterator().next(), createLogicSchema(schemaDataSources.keySet().iterator().next(), schemaDataSources, null, isUsingRegistry));
}
Expand All @@ -112,24 +115,18 @@ private void initSchemas(final Collection<String> localSchemaNames,
}
}

private LogicSchema createLogicSchema(
final String schemaName, final Map<String, Map<String, YamlDataSourceParameter>> schemaDataSources, final RuleConfiguration ruleConfiguration, final boolean isUsingRegistry) {
LogicSchema result;
try {
if (ruleConfiguration instanceof ShardingRuleConfiguration) {
result = new ShardingSchema(schemaName, schemaDataSources.get(schemaName), (ShardingRuleConfiguration) ruleConfiguration, isUsingRegistry);
} else if (ruleConfiguration instanceof MasterSlaveRuleConfiguration) {
result = new MasterSlaveSchema(schemaName, schemaDataSources.get(schemaName), (MasterSlaveRuleConfiguration) ruleConfiguration, isUsingRegistry);
} else if (ruleConfiguration instanceof EncryptRuleConfiguration) {
result = new EncryptSchema(schemaName, schemaDataSources.get(schemaName), (EncryptRuleConfiguration) ruleConfiguration);
} else {
result = new TransparentSchema(schemaName, schemaDataSources.get(schemaName));
}
} catch (final Exception ex) {
log.error("Exception occur when create schema {}.\nThe exception detail is {}.", schemaName, ex.getMessage());
throw ex;
private LogicSchema createLogicSchema(final String schemaName, final Map<String, Map<String, YamlDataSourceParameter>> schemaDataSources,
final RuleConfiguration ruleConfiguration, final boolean isUsingRegistry) throws SQLException {
if (ruleConfiguration instanceof ShardingRuleConfiguration) {
return new ShardingSchema(schemaName, schemaDataSources.get(schemaName), (ShardingRuleConfiguration) ruleConfiguration, isUsingRegistry);
}
if (ruleConfiguration instanceof MasterSlaveRuleConfiguration) {
return new MasterSlaveSchema(schemaName, schemaDataSources.get(schemaName), (MasterSlaveRuleConfiguration) ruleConfiguration, isUsingRegistry);
}
if (ruleConfiguration instanceof EncryptRuleConfiguration) {
return new EncryptSchema(schemaName, schemaDataSources.get(schemaName), (EncryptRuleConfiguration) ruleConfiguration);
}
return result;
return new TransparentSchema(schemaName, schemaDataSources.get(schemaName));
}

/**
Expand Down Expand Up @@ -165,9 +162,10 @@ public List<String> getSchemaNames() {
* Renew to add new schema.
*
* @param schemaAddedEvent schema add changed event
* @throws SQLException SQL exception
*/
@Subscribe
public synchronized void renew(final SchemaAddedEvent schemaAddedEvent) {
public synchronized void renew(final SchemaAddedEvent schemaAddedEvent) throws SQLException {
logicSchemas.put(schemaAddedEvent.getShardingSchemaName(), createLogicSchema(schemaAddedEvent.getShardingSchemaName(),
Collections.singletonMap(schemaAddedEvent.getShardingSchemaName(), DataSourceConverter.getDataSourceParameterMap(schemaAddedEvent.getDataSourceConfigurations())),
schemaAddedEvent.getRuleConfiguration(), true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.shardingsphere.shardingproxy.backend.schema.LogicSchemas;
import org.apache.shardingsphere.shardingproxy.config.yaml.YamlDataSourceParameter;

import java.sql.SQLException;
import java.util.Map;

/**
Expand All @@ -50,14 +51,14 @@ public final class EncryptSchema extends LogicSchema {

private EncryptRule encryptRule;

public EncryptSchema(final String name, final Map<String, YamlDataSourceParameter> dataSources, final EncryptRuleConfiguration encryptRuleConfiguration) {
public EncryptSchema(final String name, final Map<String, YamlDataSourceParameter> dataSources, final EncryptRuleConfiguration encryptRuleConfiguration) throws SQLException {
super(name, dataSources);
encryptRule = new EncryptRule(encryptRuleConfiguration);
shardingRule = new ShardingRule(new ShardingRuleConfiguration(), getDataSources().keySet());
metaData = createMetaData();
}

private ShardingSphereMetaData createMetaData() {
private ShardingSphereMetaData createMetaData() throws SQLException {
DataSourceMetas dataSourceMetas = new DataSourceMetas(getDataSourceURLs(getDataSources()), LogicSchemas.getInstance().getDatabaseType());
TableMetas tableMetas = new TableMetas(getTableMetaDataInitializer(dataSourceMetas).load(shardingRule));
return new ShardingSphereMetaData(dataSourceMetas, tableMetas);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.shardingsphere.shardingproxy.backend.schema.LogicSchemas;
import org.apache.shardingsphere.shardingproxy.config.yaml.YamlDataSourceParameter;

import java.sql.SQLException;
import java.util.Map;

/**
Expand All @@ -52,7 +53,8 @@ public final class MasterSlaveSchema extends LogicSchema {

private final ShardingSphereMetaData metaData;

public MasterSlaveSchema(final String name, final Map<String, YamlDataSourceParameter> dataSources, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final boolean isUsingRegistry) {
public MasterSlaveSchema(final String name,
final Map<String, YamlDataSourceParameter> dataSources, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final boolean isUsingRegistry) throws SQLException {
super(name, dataSources);
masterSlaveRule = createMasterSlaveRule(masterSlaveRuleConfig, isUsingRegistry);
// TODO we should remove it after none-sharding parsingEngine completed.
Expand All @@ -64,7 +66,7 @@ private MasterSlaveRule createMasterSlaveRule(final MasterSlaveRuleConfiguration
return isUsingRegistry ? new OrchestrationMasterSlaveRule(masterSlaveRuleConfig) : new MasterSlaveRule(masterSlaveRuleConfig);
}

private ShardingSphereMetaData createMetaData() {
private ShardingSphereMetaData createMetaData() throws SQLException {
DataSourceMetas dataSourceMetas = new DataSourceMetas(getDataSourceURLs(getDataSources()), LogicSchemas.getInstance().getDatabaseType());
TableMetas tableMetas = new TableMetas(getTableMetaDataInitializer(dataSourceMetas).load(shardingRule));
return new ShardingSphereMetaData(dataSourceMetas, tableMetas);
Expand Down
Loading

0 comments on commit 8e2102d

Please sign in to comment.