Search in sources :

Example 41 with SchemaConfig

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) {
            }
        }
    }
}
Also used : ClusterInfo(io.mycat.config.loader.zkprocess.zookeeper.ClusterInfo) SchemaConfig(io.mycat.config.model.SchemaConfig) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) TableConfig(io.mycat.config.model.TableConfig) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 42 with SchemaConfig

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);
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(io.mycat.config.model.SchemaConfig)

Example 43 with SchemaConfig

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);
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) SchemaUtil(io.mycat.server.util.SchemaUtil)

Example 44 with SchemaConfig

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;
    }
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig)

Example 45 with SchemaConfig

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;
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) MycatNode(io.mycat.config.MycatNode) RowDataPacket(io.mycat.net.mysql.RowDataPacket) MycatCluster(io.mycat.config.MycatCluster) MycatConfig(io.mycat.config.MycatConfig) LinkedList(java.util.LinkedList)

Aggregations

SchemaConfig (io.mycat.config.model.SchemaConfig)79 SystemConfig (io.mycat.config.model.SystemConfig)54 RouteResultset (io.mycat.route.RouteResultset)34 Test (org.junit.Test)31 TableConfig (io.mycat.config.model.TableConfig)12 MycatConfig (io.mycat.config.MycatConfig)9 RouteResultsetNode (io.mycat.route.RouteResultsetNode)7 SQLNonTransientException (java.sql.SQLNonTransientException)7 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)6 RowDataPacket (io.mycat.net.mysql.RowDataPacket)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)5 CacheService (io.mycat.cache.CacheService)5 MycatCluster (io.mycat.config.MycatCluster)5 UserConfig (io.mycat.config.model.UserConfig)5 ArrayList (java.util.ArrayList)5 NoSuchElementException (java.util.NoSuchElementException)5 EOFPacket (io.mycat.net.mysql.EOFPacket)4 FieldPacket (io.mycat.net.mysql.FieldPacket)4 RouteService (io.mycat.route.RouteService)4