Search in sources :

Example 6 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class ShowTables method parserAndExecuteShowTables.

private static void parserAndExecuteShowTables(ServerConnection c, String originSql, String node, ShowCreateStmtInfo info) throws Exception {
    RouteResultset rrs = new RouteResultset(originSql, ServerParse.SHOW);
    if (info.getSchema() != null) {
        StringBuilder sql = new StringBuilder();
        sql.append("SHOW ");
        if (info.isFull()) {
            sql.append("FULL ");
        }
        sql.append("TABLES ");
        if (info.getCond() != null) {
            sql.append(info.getCond());
        }
        rrs.setStatement(sql.toString());
    }
    RouterUtil.routeToSingleNode(rrs, node);
    ShowTablesHandler showTablesHandler = new ShowTablesHandler(rrs, c.getSession2(), info);
    showTablesHandler.execute();
}
Also used : ShowTablesHandler(com.actiontech.dble.backend.mysql.nio.handler.ShowTablesHandler) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 7 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class HintMasterDBHandler method route.

@Override
public RouteResultset route(SchemaConfig schema, int sqlType, String realSQL, ServerConnection sc, LayerCachePool cachePool, String hintSQLValue, int hintSqlType, Map hintMap) throws SQLException {
    RouteResultset rrs = RouteStrategyFactory.getRouteStrategy().route(schema, sqlType, realSQL, 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.info("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.info(" sql hint 'db_type' error, ignore this hint.");
        return rrs;
    }
    if (isRouteToMaster) {
        rrs.setRunOnSlave(false);
    }
    if (!isRouteToMaster) {
        rrs.setRunOnSlave(true);
    }
    LOGGER.debug("rrs.getRunOnSlave():" + rrs.getRunOnSlave());
    return rrs;
}
Also used : RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 8 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class HintDataNodeHandler method route.

@Override
public RouteResultset route(SchemaConfig schema, int sqlType, String realSQL, ServerConnection sc, LayerCachePool cachePool, String hintSQLValue, int hintSqlType, Map hintMap) throws SQLNonTransientException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("route datanode sql hint from " + realSQL);
    }
    RouteResultset rrs = new RouteResultset(realSQL, sqlType);
    PhysicalDBNode dataNode = DbleServer.getInstance().getConfig().getDataNodes().get(hintSQLValue);
    if (dataNode != null) {
        rrs = RouterUtil.routeToSingleNode(rrs, dataNode.getName());
    } else {
        String msg = "can't find hint datanode:" + hintSQLValue;
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    return rrs;
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 9 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class ServerConnection method routeSystemInfoAndExecuteSQL.

public void routeSystemInfoAndExecuteSQL(String stmt, SchemaUtil.SchemaInfo schemaInfo, int sqlType) {
    ServerConfig conf = DbleServer.getInstance().getConfig();
    UserConfig user = conf.getUsers().get(this.getUser());
    if (user == null || !user.getSchemas().contains(schemaInfo.getSchema())) {
        writeErrMessage("42000", "Access denied for user '" + this.getUser() + "' to database '" + schemaInfo.getSchema() + "'", ErrorCode.ER_DBACCESS_DENIED_ERROR);
        return;
    }
    RouteResultset rrs = new RouteResultset(stmt, sqlType);
    try {
        if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
            RouterUtil.routeToSingleNode(rrs, schemaInfo.getSchemaConfig().getDataNode());
        } else {
            TableConfig tc = schemaInfo.getSchemaConfig().getTables().get(schemaInfo.getTable());
            if (tc == null) {
                String msg = "Table '" + schemaInfo.getSchema() + "." + schemaInfo.getTable() + "' doesn't exist";
                writeErrMessage("42S02", msg, ErrorCode.ER_NO_SUCH_TABLE);
                return;
            }
            RouterUtil.routeToRandomNode(rrs, schemaInfo.getSchemaConfig(), schemaInfo.getTable());
        }
        session.execute(rrs);
    } catch (Exception e) {
        executeException(e, stmt);
    }
}
Also used : ServerConfig(com.actiontech.dble.config.ServerConfig) TableConfig(com.actiontech.dble.config.model.TableConfig) UserConfig(com.actiontech.dble.config.model.UserConfig) SQLNonTransientException(java.sql.SQLNonTransientException) SQLException(java.sql.SQLException) IOException(java.io.IOException) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 10 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class DruidSelectParser method tryRouteSingleTable.

private void tryRouteSingleTable(SchemaConfig schema, RouteResultset rrs, LayerCachePool cachePool) throws SQLException {
    if (rrs.isFinishedRoute()) {
        return;
    }
    SortedSet<RouteResultsetNode> nodeSet = new TreeSet<>();
    String table = ctx.getTables().get(0);
    if (RouterUtil.isNoSharding(schema, table)) {
        RouterUtil.routeToSingleNode(rrs, schema.getDataNode());
        return;
    }
    for (RouteCalculateUnit unit : ctx.getRouteCalculateUnits()) {
        RouteResultset rrsTmp = RouterUtil.tryRouteForOneTable(schema, unit, table, rrs, true, cachePool);
        if (rrsTmp != null && rrsTmp.getNodes() != null) {
            Collections.addAll(nodeSet, rrsTmp.getNodes());
            if (rrsTmp.isGlobalTable()) {
                break;
            }
        }
    }
    if (nodeSet.size() == 0) {
        String msg = " find no Route:" + rrs.getStatement();
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSet.size()];
    int i = 0;
    for (RouteResultsetNode aNodeSet : nodeSet) {
        nodes[i] = aNodeSet;
        i++;
    }
    rrs.setNodes(nodes);
    rrs.setFinishedRoute(true);
}
Also used : RouteCalculateUnit(com.actiontech.dble.route.parser.druid.RouteCalculateUnit) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) RouteResultset(com.actiontech.dble.route.RouteResultset)

Aggregations

RouteResultset (com.actiontech.dble.route.RouteResultset)22 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)8 SQLNonTransientException (java.sql.SQLNonTransientException)8 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)6 TableConfig (com.actiontech.dble.config.model.TableConfig)4 SQLException (java.sql.SQLException)4 RouteCalculateUnit (com.actiontech.dble.route.parser.druid.RouteCalculateUnit)3 IOException (java.io.IOException)3 FetchStoreNodeOfChildTableHandler (com.actiontech.dble.backend.mysql.nio.handler.FetchStoreNodeOfChildTableHandler)2 DruidShardingParseInfo (com.actiontech.dble.route.parser.druid.DruidShardingParseInfo)2 LoadData (com.actiontech.dble.sqlengine.mpp.LoadData)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 ByteBuffer (java.nio.ByteBuffer)2 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)1 ShowTablesHandler (com.actiontech.dble.backend.mysql.nio.handler.ShowTablesHandler)1 ShowVariablesHandler (com.actiontech.dble.backend.mysql.nio.handler.ShowVariablesHandler)1 SingleNodeHandler (com.actiontech.dble.backend.mysql.nio.handler.SingleNodeHandler)1 LayerCachePool (com.actiontech.dble.cache.LayerCachePool)1 ServerConfig (com.actiontech.dble.config.ServerConfig)1