use of io.mycat.config.model.SchemaConfig 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.SchemaConfig in project Mycat-Server by MyCATApache.
the class RuleDataPathChildrenCacheListener method reloadRuleData.
private void reloadRuleData(String name) {
String tableName = name.substring(name.lastIndexOf("_") + 1, name.indexOf("."));
String ruleName = name.substring(0, name.indexOf("."));
Map<String, SchemaConfig> schemaConfigMap = MycatServer.getInstance().getConfig().getSchemas();
for (SchemaConfig schemaConfig : schemaConfigMap.values()) {
TableConfig tableConfig = schemaConfig.getTables().get(tableName.toUpperCase());
if (tableConfig == null)
continue;
RuleConfig rule = tableConfig.getRule();
AbstractPartitionAlgorithm function = rule.getRuleAlgorithm();
if (function instanceof ReloadFunction) {
((ReloadFunction) function).reload();
}
}
}
use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.
the class ReloadConfig method reload.
public static boolean reload() {
/**
* 1、载入新的配置, ConfigInitializer 内部完成自检工作, 由于不更新数据源信息,此处不自检 dataHost dataNode
*/
ConfigInitializer loader = new ConfigInitializer(false);
Map<String, UserConfig> users = loader.getUsers();
Map<String, SchemaConfig> schemas = loader.getSchemas();
Map<String, PhysicalDBNode> dataNodes = loader.getDataNodes();
Map<String, PhysicalDBPool> dataHosts = loader.getDataHosts();
MycatCluster cluster = loader.getCluster();
FirewallConfig firewall = loader.getFirewall();
/**
* 2、在老的配置上,应用新的配置
*/
MycatServer.getInstance().getConfig().reload(users, schemas, dataNodes, dataHosts, cluster, firewall, false);
/**
* 3、清理缓存
*/
MycatServer.getInstance().getCacheService().clearCache();
MycatServer.getInstance().initRuleData();
return true;
}
use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.
the class ReloadConfig method reload_all.
public static boolean reload_all() {
/**
* 1、载入新的配置
* 1.1、ConfigInitializer 初始化,基本自检
* 1.2、DataNode/DataHost 实际链路检测
*/
ConfigInitializer loader = new ConfigInitializer(true);
Map<String, UserConfig> newUsers = loader.getUsers();
Map<String, SchemaConfig> newSchemas = loader.getSchemas();
Map<String, PhysicalDBNode> newDataNodes = loader.getDataNodes();
Map<String, PhysicalDBPool> newDataHosts = loader.getDataHosts();
MycatCluster newCluster = loader.getCluster();
FirewallConfig newFirewall = loader.getFirewall();
/**
* 1.2、实际链路检测
*/
loader.testConnection();
/**
* 2、承接
* 2.1、老的 dataSource 继续承接新建请求
* 2.2、新的 dataSource 开始初始化, 完毕后交由 2.3
* 2.3、新的 dataSource 开始承接新建请求
* 2.4、老的 dataSource 内部的事务执行完毕, 相继关闭
* 2.5、老的 dataSource 超过阀值的,强制关闭
*/
MycatConfig config = MycatServer.getInstance().getConfig();
/**
* 2.1 、老的 dataSource 继续承接新建请求, 此处什么也不需要做
*/
boolean isReloadStatusOK = true;
/**
* 2.2、新的 dataHosts 初始化
*/
for (PhysicalDBPool dbPool : newDataHosts.values()) {
String hostName = dbPool.getHostName();
// 设置 schemas
ArrayList<String> dnSchemas = new ArrayList<String>(30);
for (PhysicalDBNode dn : newDataNodes.values()) {
if (dn.getDbPool().getHostName().equals(hostName)) {
dnSchemas.add(dn.getDatabase());
}
}
dbPool.setSchemas(dnSchemas.toArray(new String[dnSchemas.size()]));
// 获取 data host
String dnIndex = DnPropertyUtil.loadDnIndexProps().getProperty(dbPool.getHostName(), "0");
if (!"0".equals(dnIndex)) {
LOGGER.info("init datahost: " + dbPool.getHostName() + " to use datasource index:" + dnIndex);
}
dbPool.init(Integer.valueOf(dnIndex));
if (!dbPool.isInitSuccess()) {
isReloadStatusOK = false;
break;
}
}
/**
* TODO: 确认初始化情况
*
* 新的 dataHosts 是否初始化成功
*/
if (isReloadStatusOK) {
/**
* 2.3、 在老的配置上,应用新的配置,开始准备承接任务
*/
config.reload(newUsers, newSchemas, newDataNodes, newDataHosts, newCluster, newFirewall, true);
/**
* 2.4、 处理旧的资源
*/
LOGGER.warn("1、clear old backend connection(size): " + NIOProcessor.backends_old.size());
// 清除前一次 reload 转移出去的 old Cons
Iterator<BackendConnection> iter = NIOProcessor.backends_old.iterator();
while (iter.hasNext()) {
BackendConnection con = iter.next();
con.close("clear old datasources");
iter.remove();
}
Map<String, PhysicalDBPool> oldDataHosts = config.getBackupDataHosts();
for (PhysicalDBPool dbPool : oldDataHosts.values()) {
dbPool.stopHeartbeat();
// 提取数据源下的所有连接
for (PhysicalDatasource ds : dbPool.getAllDataSources()) {
//
for (NIOProcessor processor : MycatServer.getInstance().getProcessors()) {
for (BackendConnection con : processor.getBackends().values()) {
if (con instanceof MySQLConnection) {
MySQLConnection mysqlCon = (MySQLConnection) con;
if (mysqlCon.getPool() == ds) {
NIOProcessor.backends_old.add(con);
}
} else if (con instanceof JDBCConnection) {
JDBCConnection jdbcCon = (JDBCConnection) con;
if (jdbcCon.getPool() == ds) {
NIOProcessor.backends_old.add(con);
}
}
}
}
}
}
LOGGER.warn("2、to be recycled old backend connection(size): " + NIOProcessor.backends_old.size());
//清理缓存
MycatServer.getInstance().getCacheService().clearCache();
MycatServer.getInstance().initRuleData();
return true;
} else {
// 如果重载不成功,则清理已初始化的资源。
LOGGER.warn("reload failed, clear previously created datasources ");
for (PhysicalDBPool dbPool : newDataHosts.values()) {
dbPool.clearDataSources("reload config");
dbPool.stopHeartbeat();
}
return false;
}
}
use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.
the class ShowDataNode method execute.
public static void execute(ManagerConnection c, String name) {
ByteBuffer buffer = c.allocate();
// write header
buffer = header.write(buffer, c, true);
// write fields
for (FieldPacket field : fields) {
buffer = field.write(buffer, c, true);
}
// write eof
buffer = eof.write(buffer, c, true);
// write rows
byte packetId = eof.packetId;
MycatConfig conf = MycatServer.getInstance().getConfig();
Map<String, PhysicalDBNode> dataNodes = conf.getDataNodes();
List<String> keys = new ArrayList<String>();
if (StringUtil.isEmpty(name)) {
keys.addAll(dataNodes.keySet());
} else {
SchemaConfig sc = conf.getSchemas().get(name);
if (null != sc) {
keys.addAll(sc.getAllDataNodes());
}
}
Collections.sort(keys, new Comparators<String>());
for (String key : keys) {
RowDataPacket row = getRow(dataNodes.get(key), c.getCharset());
row.packetId = ++packetId;
buffer = row.write(buffer, c, true);
}
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.packetId = ++packetId;
buffer = lastEof.write(buffer, c, true);
// post write
c.write(buffer);
}
Aggregations