Search in sources :

Example 1 with PhysicalDBNode

use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.

the class MultiNodeQueryHandler method execute.

public void execute() throws Exception {
    lock.lock();
    try {
        this.reset(rrs.getNodes().length);
        this.fieldsReturned = false;
        this.affectedRows = 0L;
        this.insertId = 0L;
    } finally {
        lock.unlock();
    }
    startTime = System.currentTimeMillis();
    LOGGER.debug("rrs.getRunOnSlave()-" + rrs.getRunOnSlave());
    StringBuilder sb = new StringBuilder();
    for (final RouteResultsetNode node : rrs.getNodes()) {
        if (node.isModifySQL()) {
            sb.append("[").append(node.getName()).append("]").append(node.getStatement()).append(";\n");
        }
    }
    if (sb.length() > 0) {
        TxnLogHelper.putTxnLog(session.getSource(), sb.toString());
    }
    for (final RouteResultsetNode node : rrs.getNodes()) {
        BackendConnection conn = session.getTarget(node);
        if (session.tryExistsCon(conn, node)) {
            node.setRunOnSlave(rrs.getRunOnSlave());
            innerExecute(conn, node);
        } else {
            // create new connection
            node.setRunOnSlave(rrs.getRunOnSlave());
            PhysicalDBNode dn = DbleServer.getInstance().getConfig().getDataNodes().get(node.getName());
            dn.getConnection(dn.getDatabase(), session.getSource().isTxStart(), sessionAutocommit, node, this, node);
        }
    }
    session.endDelive();
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) BackendConnection(com.actiontech.dble.backend.BackendConnection) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode)

Example 2 with PhysicalDBNode

use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.

the class BaseSelectHandler method initConnection.

public MySQLConnection initConnection() throws Exception {
    if (session.closed()) {
        return null;
    }
    MySQLConnection exeConn = (MySQLConnection) session.getTarget(rrss);
    if (session.tryExistsCon(exeConn, rrss)) {
        return exeConn;
    } else {
        PhysicalDBNode dn = DbleServer.getInstance().getConfig().getDataNodes().get(rrss.getName());
        // autocommit is session.getSource().isAutocommit() && !session.getSource().isTxStart()
        final BackendConnection newConn = dn.getConnection(dn.getDatabase(), autocommit, autocommit);
        session.bindConnection(rrss, newConn);
        return (MySQLConnection) newConn;
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) BackendConnection(com.actiontech.dble.backend.BackendConnection) MySQLConnection(com.actiontech.dble.backend.mysql.nio.MySQLConnection)

Example 3 with PhysicalDBNode

use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.

the class FetchStoreNodeOfChildTableHandler method execute.

public String execute(String schema, ArrayList<String> dataNodes) {
    String key = schema + ":" + sql;
    CachePool cache = DbleServer.getInstance().getCacheService().getCachePool("ER_SQL2PARENTID");
    if (cache != null) {
        String cacheResult = (String) cache.get(key);
        if (cacheResult != null) {
            return cacheResult;
        }
    }
    int totalCount = dataNodes.size();
    LOGGER.debug("find child node with sql:" + sql);
    for (String dn : dataNodes) {
        if (!LOGGER.isDebugEnabled()) {
            // no early return when debug
            if (dataNode != null) {
                LOGGER.debug(" found return ");
                return dataNode;
            }
        }
        PhysicalDBNode mysqlDN = DbleServer.getInstance().getConfig().getDataNodes().get(dn);
        try {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("execute in data_node " + dn);
            }
            RouteResultsetNode node = new RouteResultsetNode(dn, ServerParse.SELECT, sql);
            // get child node from master
            node.setRunOnSlave(false);
            BackendConnection conn = session.getTarget(node);
            if (session.tryExistsCon(conn, node)) {
                if (session.closed()) {
                    session.clearResources(true);
                    return null;
                }
                conn.setResponseHandler(this);
                conn.setSession(session);
                ((MySQLConnection) conn).setComplexQuery(true);
                conn.execute(node, session.getSource(), isAutoCommit());
            } else {
                mysqlDN.getConnection(mysqlDN.getDatabase(), session.getSource().isTxStart(), session.getSource().isAutocommit(), node, this, node);
            }
        } catch (Exception e) {
            LOGGER.info("get connection err " + e);
        }
    }
    lock.lock();
    try {
        while (dataNode == null) {
            try {
                result.await(50, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                break;
            }
            if (dataNode != null || finished.get() >= totalCount) {
                break;
            }
        }
    } finally {
        lock.unlock();
    }
    if (!LOGGER.isDebugEnabled()) {
        // no cached when debug
        if (dataNode != null && cache != null) {
            cache.putIfAbsent(key, dataNode);
        }
    }
    return dataNode;
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) BackendConnection(com.actiontech.dble.backend.BackendConnection) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) CachePool(com.actiontech.dble.cache.CachePool) MySQLConnection(com.actiontech.dble.backend.mysql.nio.MySQLConnection)

Example 4 with PhysicalDBNode

use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.

the class MultiNodeDdlHandler method execute.

public void execute() throws Exception {
    lock.lock();
    try {
        this.reset(rrs.getNodes().length);
    } finally {
        lock.unlock();
    }
    LOGGER.debug("rrs.getRunOnSlave()-" + rrs.getRunOnSlave());
    StringBuilder sb = new StringBuilder();
    for (final RouteResultsetNode node : rrs.getNodes()) {
        if (node.isModifySQL()) {
            sb.append("[").append(node.getName()).append("]").append(node.getStatement()).append(";\n");
        }
    }
    if (sb.length() > 0) {
        TxnLogHelper.putTxnLog(session.getSource(), sb.toString());
    }
    for (final RouteResultsetNode node : rrs.getNodes()) {
        BackendConnection conn = session.getTarget(node);
        if (session.tryExistsCon(conn, node)) {
            node.setRunOnSlave(rrs.getRunOnSlave());
            innerExecute(conn, node);
        } else {
            // create new connection
            node.setRunOnSlave(rrs.getRunOnSlave());
            PhysicalDBNode dn = DbleServer.getInstance().getConfig().getDataNodes().get(node.getName());
            dn.getConnection(dn.getDatabase(), true, sessionAutocommit, node, this, node);
        }
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) BackendConnection(com.actiontech.dble.backend.BackendConnection) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode)

Example 5 with PhysicalDBNode

use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.

the class ConfigInitializer method initDataNodes.

private Map<String, PhysicalDBNode> initDataNodes(SchemaLoader schemaLoader) {
    Map<String, DataNodeConfig> nodeConf = schemaLoader.getDataNodes();
    Map<String, PhysicalDBNode> nodes = new HashMap<>(nodeConf.size());
    for (DataNodeConfig conf : nodeConf.values()) {
        PhysicalDBPool pool = this.dataHosts.get(conf.getDataHost());
        if (pool == null) {
            throw new ConfigException("dataHost not exists " + conf.getDataHost());
        }
        PhysicalDBNode dataNode = new PhysicalDBNode(conf.getName(), conf.getDatabase(), pool);
        nodes.put(dataNode.getName(), dataNode);
    }
    return nodes;
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool) ConfigException(com.actiontech.dble.config.util.ConfigException)

Aggregations

PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)27 ServerConfig (com.actiontech.dble.config.ServerConfig)9 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)9 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)8 BackendConnection (com.actiontech.dble.backend.BackendConnection)7 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)5 SQLJob (com.actiontech.dble.sqlengine.SQLJob)4 PhysicalDatasource (com.actiontech.dble.backend.datasource.PhysicalDatasource)3 MySQLConnection (com.actiontech.dble.backend.mysql.nio.MySQLConnection)3 ERTable (com.actiontech.dble.config.model.ERTable)3 FirewallConfig (com.actiontech.dble.config.model.FirewallConfig)3 UserConfig (com.actiontech.dble.config.model.UserConfig)3 ConfigException (com.actiontech.dble.config.util.ConfigException)3 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)3 OneRawSQLQueryResultHandler (com.actiontech.dble.sqlengine.OneRawSQLQueryResultHandler)3 Map (java.util.Map)3 Set (java.util.Set)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConfigInitializer (com.actiontech.dble.config.ConfigInitializer)2 DBHostConfig (com.actiontech.dble.config.model.DBHostConfig)2