Search in sources :

Example 1 with CachePool

use of com.actiontech.dble.cache.CachePool 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 2 with CachePool

use of com.actiontech.dble.cache.CachePool in project dble by actiontech.

the class ShowCache method execute.

public static void execute(ManagerConnection c) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = HEADER.write(buffer, c, true);
    // write fields
    for (FieldPacket field : FIELDS) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = EOF.write(buffer, c, true);
    // write rows
    byte packetId = EOF.getPacketId();
    CacheService cacheService = DbleServer.getInstance().getCacheService();
    for (Map.Entry<String, CachePool> entry : cacheService.getAllCachePools().entrySet()) {
        String cacheName = entry.getKey();
        CachePool cachePool = entry.getValue();
        if (cachePool != null) {
            if (cachePool instanceof LayerCachePool) {
                for (Map.Entry<String, CacheStatic> staticsEntry : ((LayerCachePool) cachePool).getAllCacheStatic().entrySet()) {
                    RowDataPacket row = getRow(cacheName + '.' + staticsEntry.getKey(), staticsEntry.getValue(), c.getCharset().getResults());
                    row.setPacketId(++packetId);
                    buffer = row.write(buffer, c, true);
                }
            } else {
                RowDataPacket row = getRow(cacheName, cachePool.getCacheStatic(), c.getCharset().getResults());
                row.setPacketId(++packetId);
                buffer = row.write(buffer, c, true);
            }
        }
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.setPacketId(++packetId);
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) EOFPacket(com.actiontech.dble.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) CachePool(com.actiontech.dble.cache.CachePool) LayerCachePool(com.actiontech.dble.cache.LayerCachePool) CacheStatic(com.actiontech.dble.cache.CacheStatic) LayerCachePool(com.actiontech.dble.cache.LayerCachePool) FieldPacket(com.actiontech.dble.net.mysql.FieldPacket) Map(java.util.Map) CacheService(com.actiontech.dble.cache.CacheService)

Aggregations

CachePool (com.actiontech.dble.cache.CachePool)2 BackendConnection (com.actiontech.dble.backend.BackendConnection)1 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)1 MySQLConnection (com.actiontech.dble.backend.mysql.nio.MySQLConnection)1 CacheService (com.actiontech.dble.cache.CacheService)1 CacheStatic (com.actiontech.dble.cache.CacheStatic)1 LayerCachePool (com.actiontech.dble.cache.LayerCachePool)1 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)1 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)1 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)1 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)1 ByteBuffer (java.nio.ByteBuffer)1 Map (java.util.Map)1