use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.
the class SwitchCommitListener method checkCommit.
private void checkCommit(PathChildrenCacheEvent event) {
InterProcessMutex taskLock = null;
try {
String path = event.getData().getPath();
String taskPath = path.substring(0, path.lastIndexOf("/_commit/"));
String taskID = taskPath.substring(taskPath.lastIndexOf('/') + 1, taskPath.length());
String lockPath = ZKUtils.getZKBasePath() + "lock/" + taskID + ".lock";
List<String> sucessDataHost = ZKUtils.getConnection().getChildren().forPath(path.substring(0, path.lastIndexOf('/')));
String custerName = ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_CLUSTERID);
ClusterInfo clusterInfo = JSON.parseObject(ZKUtils.getConnection().getData().forPath("/mycat/" + custerName), ClusterInfo.class);
List<String> clusterNodeList = Splitter.on(',').omitEmptyStrings().splitToList(clusterInfo.getClusterNodes());
if (sucessDataHost.size() == clusterNodeList.size()) {
List<String> taskDataHost = ZKUtils.getConnection().getChildren().forPath(taskPath);
List<MigrateTask> allTaskList = MigrateUtils.queryAllTask(taskPath, taskDataHost);
taskLock = new InterProcessMutex(ZKUtils.getConnection(), lockPath);
taskLock.acquire(120, TimeUnit.SECONDS);
TaskNode taskNode = JSON.parseObject(ZKUtils.getConnection().getData().forPath(taskPath), TaskNode.class);
if (taskNode.getStatus() == 2) {
taskNode.setStatus(3);
//开始切换 且个节点已经禁止写入并且无原有写入在执行
try {
CuratorTransactionFinal transactionFinal = null;
check(taskID, allTaskList);
SchemaConfig schemaConfig = MycatServer.getInstance().getConfig().getSchemas().get(taskNode.getSchema());
TableConfig tableConfig = schemaConfig.getTables().get(taskNode.getTable().toUpperCase());
List<String> newDataNodes = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(taskNode.getAdd());
List<String> allNewDataNodes = tableConfig.getDataNodes();
allNewDataNodes.addAll(newDataNodes);
//先修改rule config
InterProcessMutex ruleLock = new InterProcessMutex(ZKUtils.getConnection(), ZKUtils.getZKBasePath() + "lock/rules.lock");
;
try {
ruleLock.acquire(30, TimeUnit.SECONDS);
transactionFinal = modifyZkRules(transactionFinal, tableConfig.getRule().getFunctionName(), newDataNodes);
transactionFinal = modifyTableConfigRules(transactionFinal, taskNode.getSchema(), taskNode.getTable(), newDataNodes);
} finally {
ruleLock.release();
}
transactionFinal = modifyRuleData(transactionFinal, allTaskList, tableConfig, allNewDataNodes);
transactionFinal.setData().forPath(taskPath, JSON.toJSONBytes(taskNode));
clean(taskID, allTaskList);
transactionFinal.commit();
forceTableRuleToLocal();
pushACKToClean(taskPath);
} catch (Exception e) {
//todo 异常to Zk
LOGGER.error("error:", e);
}
//todo 清理规则 顺利拉下ruledata保证一定更新到本地
} else if (taskNode.getStatus() == 3) {
forceTableRuleToLocal();
pushACKToClean(taskPath);
}
}
} catch (Exception e) {
LOGGER.error("error:", e);
} finally {
if (taskLock != null) {
try {
taskLock.release();
} catch (Exception ignored) {
}
}
}
}
use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.
the class HintSchemaHandler method route.
/**
* 从全局的schema列表中查询指定的schema是否存在, 如果存在则替换connection属性中原有的schema,
* 如果不存在,则throws SQLNonTransientException,表示指定的schema 不存在
*
* @param sysConfig
* @param schema
* @param sqlType
* @param realSQL
* @param charset
* @param info
* @param cachePool
* @param hintSQLValue
* @return
* @throws SQLNonTransientException
*/
@Override
public RouteResultset route(SystemConfig sysConfig, SchemaConfig schema, int sqlType, String realSQL, String charset, ServerConnection sc, LayerCachePool cachePool, String hintSQLValue, int hintSqlType, Map hintMap) throws SQLNonTransientException {
SchemaConfig tempSchema = MycatServer.getInstance().getConfig().getSchemas().get(hintSQLValue);
if (tempSchema != null) {
return routeStrategy.route(sysConfig, tempSchema, sqlType, realSQL, charset, sc, cachePool);
} else {
String msg = "can't find hint schema:" + hintSQLValue;
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
}
use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.
the class ServerConnection method execute.
public void execute(String sql, int type) {
//连接状态检查
if (this.isClosed()) {
LOGGER.warn("ignore execute ,server connection is closed " + this);
return;
}
// 事务状态检查
if (txInterrupted) {
writeErrMessage(ErrorCode.ER_YES, "Transaction error, need to rollback." + txInterrputMsg);
return;
}
// 检查当前使用的DB
String db = this.schema;
boolean isDefault = true;
if (db == null) {
db = SchemaUtil.detectDefaultDb(sql, type);
if (db == null) {
writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "No MyCAT Database selected");
return;
}
isDefault = false;
}
//// TODO: 2016/5/20 支持更多information_schema特性
if (ServerParse.SELECT == type && db.equalsIgnoreCase("information_schema")) {
MysqlInformationSchemaHandler.handle(sql, this);
return;
}
if (ServerParse.SELECT == type && sql.contains("mysql") && sql.contains("proc")) {
SchemaUtil.SchemaInfo schemaInfo = SchemaUtil.parseSchema(sql);
if (schemaInfo != null && "mysql".equalsIgnoreCase(schemaInfo.schema) && "proc".equalsIgnoreCase(schemaInfo.table)) {
// 兼容MySQLWorkbench
MysqlProcHandler.handle(sql, this);
return;
}
}
SchemaConfig schema = MycatServer.getInstance().getConfig().getSchemas().get(db);
if (schema == null) {
writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "Unknown MyCAT Database '" + db + "'");
return;
}
//fix navicat SELECT STATE AS `State`, ROUND(SUM(DURATION),7) AS `Duration`, CONCAT(ROUND(SUM(DURATION)/*100,3), '%') AS `Percentage` FROM INFORMATION_SCHEMA.PROFILING WHERE QUERY_ID= GROUP BY STATE ORDER BY SEQ
if (ServerParse.SELECT == type && sql.contains(" INFORMATION_SCHEMA.PROFILING ") && sql.contains("CONCAT(ROUND(SUM(DURATION)/")) {
InformationSchemaProfiling.response(this);
return;
}
/* 当已经设置默认schema时,可以通过在sql中指定其它schema的方式执行
* 相关sql,已经在mysql客户端中验证。
* 所以在此处增加关于sql中指定Schema方式的支持。
*/
if (isDefault && schema.isCheckSQLSchema() && isNormalSql(type)) {
SchemaUtil.SchemaInfo schemaInfo = SchemaUtil.parseSchema(sql);
if (schemaInfo != null && schemaInfo.schema != null && !schemaInfo.schema.equals(db)) {
SchemaConfig schemaConfig = MycatServer.getInstance().getConfig().getSchemas().get(schemaInfo.schema);
if (schemaConfig != null)
schema = schemaConfig;
}
}
routeEndExecuteSQL(sql, type, schema);
}
use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.
the class ExplainHandler method getRouteResultset.
private static RouteResultset getRouteResultset(ServerConnection c, String stmt) {
String db = c.getSchema();
int sqlType = ServerParse.parse(stmt) & 0xff;
if (db == null) {
db = SchemaUtil.detectDefaultDb(stmt, sqlType);
if (db == null) {
c.writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No database selected");
return null;
}
}
SchemaConfig schema = MycatServer.getInstance().getConfig().getSchemas().get(db);
if (schema == null) {
c.writeErrMessage(ErrorCode.ER_BAD_DB_ERROR, "Unknown database '" + db + "'");
return null;
}
try {
if (ServerParse.INSERT == sqlType && isMycatSeq(stmt, schema)) {
c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, "insert sql using mycat seq,you must provide primaryKey value for explain");
return null;
}
SystemConfig system = MycatServer.getInstance().getConfig().getSystem();
return MycatServer.getInstance().getRouterservice().route(system, schema, sqlType, stmt, c.getCharset(), c);
} catch (Exception e) {
StringBuilder s = new StringBuilder();
logger.warn(s.append(c).append(stmt).toString() + " error:" + e);
String msg = e.getMessage();
c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
return null;
}
}
use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.
the class ShowCobarCluster method getRows.
private static List<RowDataPacket> getRows(ServerConnection c) {
List<RowDataPacket> rows = new LinkedList<RowDataPacket>();
MycatConfig config = MycatServer.getInstance().getConfig();
MycatCluster cluster = config.getCluster();
Map<String, SchemaConfig> schemas = config.getSchemas();
SchemaConfig schema = (c.getSchema() == null) ? null : schemas.get(c.getSchema());
// 如果没有指定schema或者schema为null,则使用全部集群。
if (schema == null) {
Map<String, MycatNode> nodes = cluster.getNodes();
for (MycatNode n : nodes.values()) {
if (n != null && n.isOnline()) {
rows.add(getRow(n, c.getCharset()));
}
}
} else {
Map<String, MycatNode> nodes = cluster.getNodes();
for (MycatNode n : nodes.values()) {
if (n != null && n.isOnline()) {
rows.add(getRow(n, c.getCharset()));
}
}
}
if (rows.size() == 0) {
alarm.error(Alarms.CLUSTER_EMPTY + c.toString());
}
return rows;
}
Aggregations