Search in sources :

Example 6 with CachePool

use of io.mycat.cache.CachePool in project Mycat-Server by MyCATApache.

the class FetchStoreNodeOfChildTableHandler method execute.

public String execute(String schema, String sql, ArrayList<String> dataNodes) {
    String key = schema + ":" + sql;
    CachePool cache = MycatServer.getInstance().getCacheService().getCachePool("ER_SQL2PARENTID");
    String result = (String) cache.get(key);
    if (result != null) {
        return result;
    }
    this.sql = sql;
    int totalCount = dataNodes.size();
    long startTime = System.currentTimeMillis();
    long endTime = startTime + 5 * 60 * 1000L;
    MycatConfig conf = MycatServer.getInstance().getConfig();
    LOGGER.debug("find child node with sql:" + sql);
    for (String dn : dataNodes) {
        if (dataNode != null) {
            return dataNode;
        }
        PhysicalDBNode mysqlDN = conf.getDataNodes().get(dn);
        try {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("execute in datanode " + dn);
            }
            RouteResultsetNode node = new RouteResultsetNode(dn, ServerParse.SELECT, sql);
            // 获取 子表节点,最好走master为好
            node.setRunOnSlave(false);
            mysqlDN.getConnection(mysqlDN.getDatabase(), true, node, this, node);
        // mysqlDN.getConnection(mysqlDN.getDatabase(), true,
        // new RouteResultsetNode(dn, ServerParse.SELECT, sql),
        // this, dn);
        } catch (Exception e) {
            LOGGER.warn("get connection err " + e);
        }
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
        }
    }
    while (dataNode == null && System.currentTimeMillis() < endTime) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            break;
        }
        if (dataNode != null || finished.get() >= totalCount) {
            break;
        }
    }
    if (dataNode != null) {
        cache.putIfAbsent(key, dataNode);
    }
    return dataNode;
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MycatConfig(io.mycat.config.MycatConfig) IOException(java.io.IOException) CachePool(io.mycat.cache.CachePool)

Aggregations

CachePool (io.mycat.cache.CachePool)6 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)4 MycatConfig (io.mycat.config.MycatConfig)4 RouteResultsetNode (io.mycat.route.RouteResultsetNode)4 IOException (java.io.IOException)4 BackendConnection (io.mycat.backend.BackendConnection)2 CacheService (io.mycat.cache.CacheService)2 CacheStatic (io.mycat.cache.CacheStatic)2 LayerCachePool (io.mycat.cache.LayerCachePool)2 EOFPacket (io.mycat.net.mysql.EOFPacket)2 FieldPacket (io.mycat.net.mysql.FieldPacket)2 RowDataPacket (io.mycat.net.mysql.RowDataPacket)2 ByteBuffer (java.nio.ByteBuffer)2 Map (java.util.Map)2