Search in sources :

Example 1 with ServerConnection

use of com.actiontech.dble.server.ServerConnection in project dble by actiontech.

the class MultiNodeQueryHandler method okResponse.

@Override
public void okResponse(byte[] data, BackendConnection conn) {
    this.netOutBytes += data.length;
    boolean executeResponse = conn.syncAndExecute();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("received ok response ,executeResponse:" + executeResponse + " from " + conn);
    }
    if (executeResponse) {
        ServerConnection source = session.getSource();
        OkPacket ok = new OkPacket();
        ok.read(data);
        lock.lock();
        try {
            // the affected rows of global table will use the last node's response
            if (!rrs.isGlobalTable()) {
                affectedRows += ok.getAffectedRows();
            } else {
                affectedRows = ok.getAffectedRows();
            }
            if (ok.getInsertId() > 0) {
                insertId = (insertId == 0) ? ok.getInsertId() : Math.min(insertId, ok.getInsertId());
            }
            if (--nodeCount > 0)
                return;
            if (isFail()) {
                session.handleSpecial(rrs, source.getSchema(), false);
                handleEndPacket(err.toBytes(), AutoTxOperation.ROLLBACK, conn);
                return;
            }
            session.handleSpecial(rrs, source.getSchema(), true);
            if (rrs.isLoadData()) {
                byte lastPackId = source.getLoadDataInfileHandler().getLastPackId();
                // OK_PACKET
                ok.setPacketId(++lastPackId);
                ok.setMessage(("Records: " + affectedRows + "  Deleted: 0  Skipped: 0  Warnings: 0").getBytes());
                source.getLoadDataInfileHandler().clear();
            } else {
                // OK_PACKET
                ok.setPacketId(++packetId);
            }
            ok.setAffectedRows(affectedRows);
            ok.setServerStatus(source.isAutocommit() ? 2 : 1);
            if (insertId > 0) {
                ok.setInsertId(insertId);
                source.setLastInsertId(insertId);
            }
            handleEndPacket(ok.toBytes(), AutoTxOperation.COMMIT, conn);
        } finally {
            lock.unlock();
        }
    }
}
Also used : ServerConnection(com.actiontech.dble.server.ServerConnection)

Example 2 with ServerConnection

use of com.actiontech.dble.server.ServerConnection in project dble by actiontech.

the class SingleNodeHandler method okResponse.

/**
 * insert/update/delete
 * <p>
 * okResponse():
 * read data, make an OKPacket, write to writeQueue in FrontendConnection by ok.write(source)
 */
@Override
public void okResponse(byte[] data, BackendConnection conn) {
    this.netOutBytes += data.length;
    boolean executeResponse = conn.syncAndExecute();
    if (executeResponse) {
        session.handleSpecial(rrs, session.getSource().getSchema(), true);
        ServerConnection source = session.getSource();
        OkPacket ok = new OkPacket();
        ok.read(data);
        if (rrs.isLoadData()) {
            byte lastPackId = source.getLoadDataInfileHandler().getLastPackId();
            // OK_PACKET
            ok.setPacketId(++lastPackId);
            source.getLoadDataInfileHandler().clear();
        } else {
            // OK_PACKET
            ok.setPacketId(++packetId);
        }
        ok.setServerStatus(source.isAutocommit() ? 2 : 1);
        source.setLastInsertId(ok.getInsertId());
        // handleSpecial
        session.releaseConnectionIfSafe(conn, false);
        session.setResponseTime();
        ok.write(source);
        waitingResponse = false;
    }
}
Also used : ServerConnection(com.actiontech.dble.server.ServerConnection)

Example 3 with ServerConnection

use of com.actiontech.dble.server.ServerConnection in project dble by actiontech.

the class SingleNodeHandler method fieldEofResponse.

@Override
public void fieldEofResponse(byte[] header, List<byte[]> fields, List<FieldPacket> fieldPacketsNull, byte[] eof, boolean isLeft, BackendConnection conn) {
    this.netOutBytes += header.length;
    for (byte[] field : fields) {
        this.netOutBytes += field.length;
    }
    String primaryKey = null;
    if (rrs.hasPrimaryKeyToCache()) {
        String[] items = rrs.getPrimaryKeyItems();
        primaryKeyTable = items[0];
        primaryKey = items[1];
    }
    header[3] = ++packetId;
    ServerConnection source = session.getSource();
    buffer = source.writeToBuffer(header, allocBuffer());
    for (int i = 0, len = fields.size(); i < len; ++i) {
        byte[] field = fields.get(i);
        field[3] = ++packetId;
        // save field
        FieldPacket fieldPk = new FieldPacket();
        fieldPk.read(field);
        if (rrs.getSchema() != null) {
            fieldPk.setDb(rrs.getSchema().getBytes());
        }
        if (rrs.getTableAlias() != null) {
            fieldPk.setTable(rrs.getTableAlias().getBytes());
        }
        if (rrs.getTable() != null) {
            fieldPk.setOrgTable(rrs.getTable().getBytes());
        }
        fieldPackets.add(fieldPk);
        // find primary key index
        if (primaryKey != null && primaryKeyIndex == -1) {
            String fieldName = new String(fieldPk.getName());
            if (primaryKey.equalsIgnoreCase(fieldName)) {
                primaryKeyIndex = i;
            }
        }
        buffer = fieldPk.write(buffer, source, false);
    }
    fieldCount = fieldPackets.size();
    eof[3] = ++packetId;
    buffer = source.writeToBuffer(eof, buffer);
}
Also used : ServerConnection(com.actiontech.dble.server.ServerConnection)

Example 4 with ServerConnection

use of com.actiontech.dble.server.ServerConnection in project dble by actiontech.

the class SingleNodeHandler method backConnectionErr.

private void backConnectionErr(ErrorPacket errPkg, BackendConnection conn) {
    ServerConnection source = session.getSource();
    String errUser = source.getUser();
    String errHost = source.getHost();
    int errPort = source.getLocalPort();
    String errMsg = " errNo:" + errPkg.getErrNo() + " " + new String(errPkg.getMessage());
    LOGGER.info("execute sql err :" + errMsg + " con:" + conn + " frontend host:" + errHost + "/" + errPort + "/" + errUser);
    session.releaseConnectionIfSafe(conn, false);
    source.setTxInterrupt(errMsg);
    session.handleSpecial(rrs, session.getSource().getSchema(), false);
    // 
    if (waitingResponse) {
        errPkg.setPacketId(1);
        errPkg.write(source);
        waitingResponse = false;
    }
    recycleResources();
}
Also used : ServerConnection(com.actiontech.dble.server.ServerConnection)

Example 5 with ServerConnection

use of com.actiontech.dble.server.ServerConnection in project dble by actiontech.

the class OutputHandler method fieldEofResponse.

@Override
public void fieldEofResponse(byte[] headerNull, List<byte[]> fieldsNull, List<FieldPacket> fieldPackets, byte[] eofNull, boolean isLeft, BackendConnection conn) {
    if (terminate.get()) {
        return;
    }
    lock.lock();
    try {
        if (this.isBinary)
            this.fieldPackets = fieldPackets;
        ResultSetHeaderPacket hp = new ResultSetHeaderPacket();
        hp.setFieldCount(fieldPackets.size());
        hp.setPacketId(++packetId);
        ServerConnection source = session.getSource();
        buffer = hp.write(buffer, source, true);
        for (FieldPacket fp : fieldPackets) {
            fp.setPacketId(++packetId);
            buffer = fp.write(buffer, source, true);
        }
        EOFPacket ep = new EOFPacket();
        ep.setPacketId(++packetId);
        buffer = ep.write(buffer, source, true);
    } finally {
        lock.unlock();
    }
}
Also used : ServerConnection(com.actiontech.dble.server.ServerConnection)

Aggregations

ServerConnection (com.actiontech.dble.server.ServerConnection)20 FrontendConnection (com.actiontech.dble.net.FrontendConnection)3 NIOProcessor (com.actiontech.dble.net.NIOProcessor)3 BackendConnection (com.actiontech.dble.backend.BackendConnection)2 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)2 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)2 ByteBuffer (java.nio.ByteBuffer)2 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)1 MySQLConnection (com.actiontech.dble.backend.mysql.nio.MySQLConnection)1 NormalAutoCommitNodesHandler (com.actiontech.dble.backend.mysql.nio.handler.transaction.normal.NormalAutoCommitNodesHandler)1 NormalAutoRollbackNodesHandler (com.actiontech.dble.backend.mysql.nio.handler.transaction.normal.NormalAutoRollbackNodesHandler)1 XAAutoCommitNodesHandler (com.actiontech.dble.backend.mysql.nio.handler.transaction.xa.XAAutoCommitNodesHandler)1 XAAutoRollbackNodesHandler (com.actiontech.dble.backend.mysql.nio.handler.transaction.xa.XAAutoRollbackNodesHandler)1 TxState (com.actiontech.dble.backend.mysql.xa.TxState)1 ServerConfig (com.actiontech.dble.config.ServerConfig)1 PackageBufINf (com.actiontech.dble.manager.handler.PackageBufINf)1 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)1 MySQLItemVisitor (com.actiontech.dble.plan.visitor.MySQLItemVisitor)1 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)1 NonBlockingSession (com.actiontech.dble.server.NonBlockingSession)1