Search in sources :

Example 26 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class HintDataNodeHandler method route.

@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 {
    String stmt = realSQL;
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("route datanode sql hint from " + stmt);
    }
    RouteResultset rrs = new RouteResultset(stmt, sqlType);
    PhysicalDBNode dataNode = MycatServer.getInstance().getConfig().getDataNodes().get(hintSQLValue);
    if (dataNode != null) {
        rrs = RouterUtil.routeToSingleNode(rrs, dataNode.getName(), stmt);
    } else {
        String msg = "can't find hint datanode:" + hintSQLValue;
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    return rrs;
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultset(io.mycat.route.RouteResultset)

Example 27 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class HintMasterDBHandler method route.

@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 {
    //		LOGGER.debug("realSQL: " + realSQL); // select * from travelrecord limit 1
    //		LOGGER.debug("sqlType: " + sqlType); // 7
    //		LOGGER.debug("schema.getName(): " + schema.getName()); // TESTDB
    //		LOGGER.debug("schema.getName(): " + schema.getDataNode()); // null
    //		LOGGER.debug("hintSQLValue: " + hintSQLValue); // master/slave
    RouteResultset rrs = RouteStrategyFactory.getRouteStrategy().route(sysConfig, schema, sqlType, realSQL, charset, sc, cachePool);
    // master
    LOGGER.debug("schema.rrs(): " + rrs);
    // 默认不施加任何影响
    Boolean isRouteToMaster = null;
    // slave
    LOGGER.debug("hintSQLValue:::::::::" + hintSQLValue);
    if (hintSQLValue != null && !hintSQLValue.trim().equals("")) {
        if (hintSQLValue.trim().equalsIgnoreCase("master")) {
            isRouteToMaster = true;
        }
        if (hintSQLValue.trim().equalsIgnoreCase("slave")) {
            //				}
            if (sqlType == ServerParse.DELETE || sqlType == ServerParse.INSERT || sqlType == ServerParse.REPLACE || sqlType == ServerParse.UPDATE || sqlType == ServerParse.DDL) {
                LOGGER.error("should not use hint 'db_type' to route 'delete', 'insert', 'replace', 'update', 'ddl' to a slave db.");
                // 不施加任何影响
                isRouteToMaster = null;
            } else {
                isRouteToMaster = false;
            }
        }
    }
    if (isRouteToMaster == null) {
        // 默认不施加任何影响
        LOGGER.warn(" sql hint 'db_type' error, ignore this hint.");
        return rrs;
    }
    if (isRouteToMaster) {
        // 强制走 master
        rrs.setRunOnSlave(false);
    }
    if (!isRouteToMaster) {
        // 强制走slave
        rrs.setRunOnSlave(true);
    }
    LOGGER.debug("rrs.getRunOnSlave():" + rrs.getRunOnSlave());
    return rrs;
}
Also used : RouteResultset(io.mycat.route.RouteResultset)

Example 28 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class AbstractRouteStrategy method route.

@Override
public RouteResultset route(SystemConfig sysConfig, SchemaConfig schema, int sqlType, String origSQL, String charset, ServerConnection sc, LayerCachePool cachePool) throws SQLNonTransientException {
    //对应schema标签checkSQLschema属性,把表示schema的字符去掉
    if (schema.isCheckSQLSchema()) {
        origSQL = RouterUtil.removeSchema(origSQL, schema.getName());
    }
    /**
     * 处理一些路由之前的逻辑
     * 全局序列号,父子表插入
     */
    if (beforeRouteProcess(schema, sqlType, origSQL, sc)) {
        return null;
    }
    /**
		 * SQL 语句拦截
		 */
    String stmt = MycatServer.getInstance().getSqlInterceptor().interceptSQL(origSQL, sqlType);
    if (!origSQL.equals(stmt) && LOGGER.isDebugEnabled()) {
        LOGGER.debug("sql intercepted to " + stmt + " from " + origSQL);
    }
    RouteResultset rrs = new RouteResultset(stmt, sqlType);
    /**
		 * 优化debug loaddata输出cache的日志会极大降低性能
		 */
    if (LOGGER.isDebugEnabled() && origSQL.startsWith(LoadData.loadDataHint)) {
        rrs.setCacheAble(false);
    }
    /**
         * rrs携带ServerConnection的autocommit状态用于在sql解析的时候遇到
         * select ... for update的时候动态设定RouteResultsetNode的canRunInReadDB属性
         */
    if (sc != null) {
        rrs.setAutocommit(sc.isAutocommit());
    }
    /**
		 * DDL 语句的路由
		 */
    if (ServerParse.DDL == sqlType) {
        return RouterUtil.routeToDDLNode(rrs, sqlType, stmt, schema);
    }
    /**
		 * 检查是否有分片
		 */
    if (schema.isNoSharding() && ServerParse.SHOW != sqlType) {
        rrs = RouterUtil.routeToSingleNode(rrs, schema.getDataNode(), stmt);
    } else {
        RouteResultset returnedSet = routeSystemInfo(schema, sqlType, stmt, rrs);
        if (returnedSet == null) {
            rrs = routeNormalSqlWithAST(schema, stmt, rrs, charset, cachePool);
        }
    }
    return rrs;
}
Also used : RouteResultset(io.mycat.route.RouteResultset)

Example 29 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class DruidSelectParser method tryRoute.

private void tryRoute(SchemaConfig schema, RouteResultset rrs, LayerCachePool cachePool) throws SQLNonTransientException {
    if (rrs.isFinishedRoute()) {
        //避免重复路由
        return;
    }
    //无表的select语句直接路由带任一节点
    if ((ctx.getTables() == null || ctx.getTables().size() == 0) && (ctx.getTableAliasMap() == null || ctx.getTableAliasMap().isEmpty())) {
        rrs = RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode(), ctx.getSql());
        rrs.setFinishedRoute(true);
        return;
    }
    //		RouterUtil.tryRouteForTables(schema, ctx, rrs, true, cachePool);
    SortedSet<RouteResultsetNode> nodeSet = new TreeSet<RouteResultsetNode>();
    boolean isAllGlobalTable = RouterUtil.isAllGlobalTable(ctx, schema);
    for (RouteCalculateUnit unit : ctx.getRouteCalculateUnits()) {
        RouteResultset rrsTmp = RouterUtil.tryRouteForTables(schema, ctx, unit, rrs, true, cachePool);
        if (rrsTmp != null && rrsTmp.getNodes() != null) {
            for (RouteResultsetNode node : rrsTmp.getNodes()) {
                nodeSet.add(node);
            }
        }
        if (isAllGlobalTable) {
            //都是全局表时只计算一遍路由
            break;
        }
    }
    if (nodeSet.size() == 0) {
        Collection<String> stringCollection = ctx.getTableAliasMap().values();
        for (String table : stringCollection) {
            if (table != null && table.toLowerCase().contains("information_schema.")) {
                rrs = RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode(), ctx.getSql());
                rrs.setFinishedRoute(true);
                return;
            }
        }
        String msg = " find no Route:" + ctx.getSql();
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSet.size()];
    int i = 0;
    for (Iterator<RouteResultsetNode> iterator = nodeSet.iterator(); iterator.hasNext(); ) {
        nodes[i] = (RouteResultsetNode) iterator.next();
        i++;
    }
    rrs.setNodes(nodes);
    rrs.setFinishedRoute(true);
}
Also used : RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) SQLNonTransientException(java.sql.SQLNonTransientException) TreeSet(java.util.TreeSet) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 30 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class ServerLoadDataInfileHandler method end.

@Override
public void end(byte packID) {
    isStartLoadData = false;
    this.packID = packID;
    //load in data空包 结束
    saveByteOrToFile(null, true);
    List<SQLExpr> columns = statement.getColumns();
    String tableName = statement.getTableName().getSimpleName();
    if (isHasStoreToFile) {
        parseFileByLine(tempFile, loadData.getCharset(), loadData.getLineTerminatedBy());
    } else {
        String content = new String(tempByteBuffer.toByteArray(), Charset.forName(loadData.getCharset()));
        // List<String> lines = Splitter.on(loadData.getLineTerminatedBy()).omitEmptyStrings().splitToList(content);
        CsvParserSettings settings = new CsvParserSettings();
        settings.setMaxColumns(65535);
        settings.setMaxCharsPerColumn(65535);
        settings.getFormat().setLineSeparator(loadData.getLineTerminatedBy());
        settings.getFormat().setDelimiter(loadData.getFieldTerminatedBy().charAt(0));
        if (loadData.getEnclose() != null) {
            settings.getFormat().setQuote(loadData.getEnclose().charAt(0));
        }
        if (loadData.getEscape() != null) {
            settings.getFormat().setQuoteEscape(loadData.getEscape().charAt(0));
        }
        settings.getFormat().setNormalizedNewline(loadData.getLineTerminatedBy().charAt(0));
        /*
             *  fix bug #1074 : LOAD DATA local INFILE导入的所有Boolean类型全部变成了false
             *  不可见字符将在CsvParser被当成whitespace过滤掉, 使用settings.trimValues(false)来避免被过滤掉
             *  TODO : 设置trimValues(false)之后, 会引起字段值前后的空白字符无法被过滤!
             */
        settings.trimValues(false);
        CsvParser parser = new CsvParser(settings);
        try {
            parser.beginParsing(new StringReader(content));
            String[] row = null;
            while ((row = parser.parseNext()) != null) {
                parseOneLine(columns, tableName, row, false, null);
            }
        } finally {
            parser.stopParsing();
        }
    }
    RouteResultset rrs = buildResultSet(routeResultMap);
    if (rrs != null) {
        flushDataToFile();
        serverConnection.getSession2().execute(rrs, ServerParse.LOAD_DATA_INFILE_SQL);
    }
// sendOk(++packID);
}
Also used : CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings) CsvParser(com.univocity.parsers.csv.CsvParser) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) RouteResultset(io.mycat.route.RouteResultset)

Aggregations

RouteResultset (io.mycat.route.RouteResultset)52 SchemaConfig (io.mycat.config.model.SchemaConfig)34 SystemConfig (io.mycat.config.model.SystemConfig)32 RouteResultsetNode (io.mycat.route.RouteResultsetNode)15 Test (org.junit.Test)13 SQLNonTransientException (java.sql.SQLNonTransientException)11 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 TableConfig (io.mycat.config.model.TableConfig)6 CacheService (io.mycat.cache.CacheService)4 RouteService (io.mycat.route.RouteService)4 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 NoSuchElementException (java.util.NoSuchElementException)4 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 DruidShardingParseInfo (io.mycat.route.parser.druid.DruidShardingParseInfo)3 RouteCalculateUnit (io.mycat.route.parser.druid.RouteCalculateUnit)3 TreeSet (java.util.TreeSet)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)2