未验证 提交 ed1603ac 编写于 作者: J Jason King 提交者: GitHub

issue-4785: Check uniformed with all actual tables' meta data in once when loading them (#4826)

* issue-4785: Check uniformed with all actual tables' meta data in once when loading them.

* issue-4785: Add Object for holding error meta data while checking uniformed table meta data configuration.

* issue-4785: Move the new class as inner class; extract private method and rename some variables.

* issue-4785: Add final for class and its fields; remove inner class accessLevel, etc.

* issue-4785: Add final for class and its fields; remove inner class accessLevel, etc.

* issue-4785: A few more better readable optimizations.

* issue-4785: Optimize exception details.

* issue-4785: Remove unnecessary local variable.

* issue-4785: Remove an unnecessary append operation for initializing error message.
上级 93123ece
......@@ -17,6 +17,7 @@
package org.apache.shardingsphere.core.metadata;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.core.rule.DataNode;
......@@ -30,10 +31,12 @@ import org.apache.shardingsphere.underlying.common.exception.ShardingSphereExcep
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.List;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Collections;
import java.util.Map.Entry;
import java.util.Optional;
......@@ -43,7 +46,9 @@ import java.util.Optional;
@RequiredArgsConstructor
@Slf4j(topic = "ShardingSphere-metadata")
public final class ShardingMetaDataLoader {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private final Map<String, DataSource> dataSourceMap;
private final ShardingRule shardingRule;
......@@ -103,17 +108,36 @@ public final class ShardingMetaDataLoader {
return actualDefaultDataSourceName.isPresent()
? SchemaMetaDataLoader.load(dataSourceMap.get(actualDefaultDataSourceName.get()), maxConnectionsSizePerQuery) : new SchemaMetaData(Collections.emptyMap());
}
// TODO check all meta data in once
private void checkUniformed(final String logicTableName, final Map<String, TableMetaData> actualTableMetaDataMap) {
ShardingTableMetaDataDecorator decorator = new ShardingTableMetaDataDecorator();
TableMetaData sample = decorator.decorate(actualTableMetaDataMap.values().iterator().next(), logicTableName, shardingRule);
Collection<TableMetaDataViolation> violations = new LinkedList<>();
for (Entry<String, TableMetaData> entry : actualTableMetaDataMap.entrySet()) {
if (!sample.equals(decorator.decorate(entry.getValue(), logicTableName, shardingRule))) {
throw new ShardingSphereException(
"Cannot get uniformed table structure for logic table `%s` and actual table `%s`. The different meta data of actual tables are as follows:\n%s\n%s.",
logicTableName, entry.getKey(), sample, entry.getValue());
violations.add(new TableMetaDataViolation(entry.getKey(), entry.getValue()));
}
}
throwExceptionIfNecessary(violations, logicTableName);
}
private void throwExceptionIfNecessary(final Collection<TableMetaDataViolation> violations, final String logicTableName) {
if (!violations.isEmpty()) {
StringBuilder errorMessage = new StringBuilder(
"Cannot get uniformed table structure for logic table `%s`, it has different meta data of actual tables are as follows:").append(LINE_SEPARATOR);
for (TableMetaDataViolation each : violations) {
errorMessage.append("actual table: ").append(each.getActualTableName()).append(", meta data: ").append(each.getTableMetaData()).append(LINE_SEPARATOR);
}
throw new ShardingSphereException(errorMessage.toString(), logicTableName);
}
}
@RequiredArgsConstructor
@Getter
private final class TableMetaDataViolation {
private final String actualTableName;
private final TableMetaData tableMetaData;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册