Search in sources :

Example 11 with BackendConnection

use of com.actiontech.dble.backend.BackendConnection in project dble by actiontech.

the class PhysicalDBPool method initSource.

private boolean initSource(int index, PhysicalDatasource ds) {
    int initSize = ds.getConfig().getMinCon();
    LOGGER.info("init backend mysql source ,create connections total " + initSize + " for " + ds.getName() + " index :" + index);
    CopyOnWriteArrayList<BackendConnection> list = new CopyOnWriteArrayList<>();
    GetConnectionHandler getConHandler = new GetConnectionHandler(list, initSize);
    for (int i = 0; i < initSize; i++) {
        try {
            ds.initMinConnection(this.schemas[i % schemas.length], true, getConHandler, null);
        } catch (Exception e) {
            LOGGER.info(getMessage(index, " init connection error."), e);
        }
    }
    long timeOut = System.currentTimeMillis() + 60 * 1000;
    // waiting for finish
    while (!getConHandler.finished() && (System.currentTimeMillis() < timeOut)) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            /**
             * hardly triggered no error is needed
             */
            LOGGER.info("initError", e);
        }
    }
    LOGGER.info("init result :" + getConHandler.getStatusInfo());
    return !list.isEmpty();
}
Also used : BackendConnection(com.actiontech.dble.backend.BackendConnection) GetConnectionHandler(com.actiontech.dble.backend.mysql.nio.handler.GetConnectionHandler) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 12 with BackendConnection

use of com.actiontech.dble.backend.BackendConnection in project dble by actiontech.

the class NIOProcessor method backendCheck.

private void backendCheck() {
    long sqlTimeout = DbleServer.getInstance().getConfig().getSystem().getSqlExecuteTimeout() * 1000L;
    Iterator<Entry<Long, BackendConnection>> it = backends.entrySet().iterator();
    while (it.hasNext()) {
        BackendConnection c = it.next().getValue();
        // remove empty
        if (c == null) {
            it.remove();
            continue;
        }
        // Active/IDLE/PREPARED XA backends will not be checked
        if (c instanceof MySQLConnection) {
            MySQLConnection m = (MySQLConnection) c;
            if (m.isClosedOrQuit()) {
                it.remove();
                continue;
            }
            if (m.getXaStatus() != null && m.getXaStatus() != TxState.TX_INITIALIZE_STATE) {
                continue;
            }
        }
        // close the conn which executeTimeOut
        if (!c.isDDL() && c.isBorrowed() && c.getLastTime() < TimeUtil.currentTimeMillis() - sqlTimeout) {
            LOGGER.info("found backend connection SQL timeout ,close it " + c);
            c.close("sql timeout");
        }
        // clean closed conn or check time out
        if (c.isClosed()) {
            it.remove();
        } else {
            // very important ,for some data maybe not sent
            if (c instanceof AbstractConnection) {
                checkConSendQueue((AbstractConnection) c);
            }
            c.idleCheck();
        }
    }
}
Also used : Entry(java.util.Map.Entry) BackendConnection(com.actiontech.dble.backend.BackendConnection) MySQLConnection(com.actiontech.dble.backend.mysql.nio.MySQLConnection)

Example 13 with BackendConnection

use of com.actiontech.dble.backend.BackendConnection in project dble by actiontech.

the class NonBlockingSession method initiativeTerminate.

/**
 * Only used when kill @@connection is Issued
 */
void initiativeTerminate() {
    for (BackendConnection node : target.values()) {
        node.terminate("client closed ");
    }
    target.clear();
    clearHandlesResources();
}
Also used : BackendConnection(com.actiontech.dble.backend.BackendConnection)

Example 14 with BackendConnection

use of com.actiontech.dble.backend.BackendConnection in project dble by actiontech.

the class NonBlockingSession method closeAndClearResources.

public void closeAndClearResources(String reason) {
    // XA MUST BE FINISHED
    if (source.isTxStart() && this.getXaState() != null && this.getXaState() != TxState.TX_INITIALIZE_STATE) {
        return;
    }
    for (BackendConnection node : target.values()) {
        node.terminate(reason);
    }
    target.clear();
    clearHandlesResources();
}
Also used : BackendConnection(com.actiontech.dble.backend.BackendConnection)

Example 15 with BackendConnection

use of com.actiontech.dble.backend.BackendConnection in project dble by actiontech.

the class NonBlockingSession method releaseConnection.

public void releaseConnection(BackendConnection con) {
    Iterator<Entry<RouteResultsetNode, BackendConnection>> iterator = target.entrySet().iterator();
    while (iterator.hasNext()) {
        BackendConnection theCon = iterator.next().getValue();
        if (theCon == con) {
            iterator.remove();
            con.release();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("realse connection " + con);
            }
            break;
        }
    }
}
Also used : Entry(java.util.Map.Entry) BackendConnection(com.actiontech.dble.backend.BackendConnection)

Aggregations

BackendConnection (com.actiontech.dble.backend.BackendConnection)29 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)12 MySQLConnection (com.actiontech.dble.backend.mysql.nio.MySQLConnection)8 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)7 NIOProcessor (com.actiontech.dble.net.NIOProcessor)4 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)4 IOException (java.io.IOException)3 ServerConfig (com.actiontech.dble.config.ServerConfig)2 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)2 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)2 ServerConnection (com.actiontech.dble.server.ServerConnection)2 ByteBuffer (java.nio.ByteBuffer)2 Entry (java.util.Map.Entry)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConQueue (com.actiontech.dble.backend.ConQueue)1 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)1 PhysicalDatasource (com.actiontech.dble.backend.datasource.PhysicalDatasource)1 GetConnectionHandler (com.actiontech.dble.backend.mysql.nio.handler.GetConnectionHandler)1 NewConnectionRespHandler (com.actiontech.dble.backend.mysql.nio.handler.NewConnectionRespHandler)1 ArrayMinHeap (com.actiontech.dble.backend.mysql.nio.handler.util.ArrayMinHeap)1