use of io.mycat.config.model.DataHostConfig in project Mycat-Server by MyCATApache.
the class XMLSchemaLoader method loadSchemas.
private void loadSchemas(Element root) {
NodeList list = root.getElementsByTagName("schema");
for (int i = 0, n = list.getLength(); i < n; i++) {
Element schemaElement = (Element) list.item(i);
//读取各个属性
String name = schemaElement.getAttribute("name");
String dataNode = schemaElement.getAttribute("dataNode");
String checkSQLSchemaStr = schemaElement.getAttribute("checkSQLschema");
String sqlMaxLimitStr = schemaElement.getAttribute("sqlMaxLimit");
int sqlMaxLimit = -1;
//读取sql返回结果集限制
if (sqlMaxLimitStr != null && !sqlMaxLimitStr.isEmpty()) {
sqlMaxLimit = Integer.parseInt(sqlMaxLimitStr);
}
// check dataNode already exists or not,看schema标签中是否有datanode
String defaultDbType = null;
//校验检查并添加dataNode
if (dataNode != null && !dataNode.isEmpty()) {
List<String> dataNodeLst = new ArrayList<String>(1);
dataNodeLst.add(dataNode);
checkDataNodeExists(dataNodeLst);
String dataHost = dataNodes.get(dataNode).getDataHost();
defaultDbType = dataHosts.get(dataHost).getDbType();
} else {
dataNode = null;
}
//加载schema下所有tables
Map<String, TableConfig> tables = loadTables(schemaElement);
//判断schema是否重复
if (schemas.containsKey(name)) {
throw new ConfigException("schema " + name + " duplicated!");
}
// 设置了table的不需要设置dataNode属性,没有设置table的必须设置dataNode属性
if (dataNode == null && tables.size() == 0) {
throw new ConfigException("schema " + name + " didn't config tables,so you must set dataNode property!");
}
SchemaConfig schemaConfig = new SchemaConfig(name, dataNode, tables, sqlMaxLimit, "true".equalsIgnoreCase(checkSQLSchemaStr));
//设定DB类型,这对之后的sql语句路由解析有帮助
if (defaultDbType != null) {
schemaConfig.setDefaultDataNodeDbType(defaultDbType);
if (!"mysql".equalsIgnoreCase(defaultDbType)) {
schemaConfig.setNeedSupportMultiDBType(true);
}
}
// 判断是否有不是mysql的数据库类型,方便解析判断是否启用多数据库分页语法解析
for (TableConfig tableConfig : tables.values()) {
if (isHasMultiDbType(tableConfig)) {
schemaConfig.setNeedSupportMultiDBType(true);
break;
}
}
//记录每种dataNode的DB类型
Map<String, String> dataNodeDbTypeMap = new HashMap<>();
for (String dataNodeName : dataNodes.keySet()) {
DataNodeConfig dataNodeConfig = dataNodes.get(dataNodeName);
String dataHost = dataNodeConfig.getDataHost();
DataHostConfig dataHostConfig = dataHosts.get(dataHost);
if (dataHostConfig != null) {
String dbType = dataHostConfig.getDbType();
dataNodeDbTypeMap.put(dataNodeName, dbType);
}
}
schemaConfig.setDataNodeDbTypeMap(dataNodeDbTypeMap);
schemas.put(name, schemaConfig);
}
}
use of io.mycat.config.model.DataHostConfig in project Mycat-Server by MyCATApache.
the class ConfigComparer method getDataNodes.
//获取拆分表对应节点列表,具体到实例地址、库
private List<DataNode> getDataNodes(TableConfig tableConfig, Map<String, DataNodeConfig> dnConfig, Map<String, DataHostConfig> dhConfig) {
List<DataNode> dataNodes = new ArrayList<DataNode>();
//TO-DO
ArrayList<String> dataNodeNames = tableConfig.getDataNodes();
int i = 0;
for (String name : dataNodeNames) {
DataNodeConfig config = dnConfig.get(name);
String db = config.getDatabase();
String dataHost = config.getDataHost();
DataHostConfig dh = dhConfig.get(dataHost);
String dbType = dh.getDbType();
DBHostConfig[] writeHosts = dh.getWriteHosts();
DBHostConfig currentWriteHost;
if (isAwaysUseMaster) {
currentWriteHost = writeHosts[0];
} else {
//迁移数据发生在当前切换后的数据源
currentWriteHost = writeHosts[Integer.valueOf(dnIndexProps.getProperty(dh.getName()))];
}
DataNode dn = new DataNode(name, currentWriteHost.getIp(), currentWriteHost.getPort(), currentWriteHost.getUser(), currentWriteHost.getPassword(), db, dbType, i++);
dataNodes.add(dn);
}
return dataNodes;
}
use of io.mycat.config.model.DataHostConfig in project Mycat-Server by MyCATApache.
the class ConfigTest method testTempReadHostAvailable.
/**
* 测试 临时读可用 配置
*/
@Test
public void testTempReadHostAvailable() {
PhysicalDBPool pool = this.dataHosts.get("localhost2");
DataHostConfig hostConfig = pool.getSource().getHostConfig();
Assert.assertTrue(hostConfig.isTempReadHostAvailable() == true);
}
Aggregations