Search in sources :

Example 1 with MySQLPacket

use of com.alibaba.cobar.net.mysql.MySQLPacket in project cobar by alibaba.

the class SingleNodeExecutor method handleResultSet.

/**
     * 处理结果集数据
     */
private void handleResultSet(RouteResultsetNode rrn, BlockingSession ss, MySQLChannel mc, BinaryPacket bin, int flag) throws IOException {
    final ServerConnection sc = ss.getSource();
    // HEADER
    bin.packetId = ++packetId;
    List<MySQLPacket> headerList = new LinkedList<MySQLPacket>();
    headerList.add(bin);
    for (; ; ) {
        bin = mc.receive();
        switch(bin.data[0]) {
            case ErrorPacket.FIELD_COUNT:
                {
                    LOGGER.warn(mc.getErrLog(rrn.getStatement(), mc.getErrMessage(bin), sc));
                    mc.setRunning(false);
                    if (mc.isAutocommit()) {
                        ss.clear();
                    }
                    endRunning();
                    // ERROR_PACKET
                    bin.packetId = ++packetId;
                    sc.write(bin.write(sc.allocate(), sc));
                    return;
                }
            case EOFPacket.FIELD_COUNT:
                {
                    // FIELD_EOF
                    bin.packetId = ++packetId;
                    ByteBuffer bb = sc.allocate();
                    for (MySQLPacket packet : headerList) {
                        bb = packet.write(bb, sc);
                    }
                    bb = bin.write(bb, sc);
                    headerList = null;
                    handleRowData(rrn, ss, mc, bb, packetId);
                    return;
                }
            default:
                // FIELDS
                bin.packetId = ++packetId;
                switch(flag) {
                    case RouteResultset.REWRITE_FIELD:
                        StringBuilder fieldName = new StringBuilder();
                        fieldName.append("Tables_in_").append(ss.getSource().getSchema());
                        FieldPacket field = PacketUtil.getField(bin, fieldName.toString());
                        headerList.add(field);
                        break;
                    default:
                        headerList.add(bin);
                }
        }
    }
}
Also used : MySQLPacket(com.alibaba.cobar.net.mysql.MySQLPacket) ServerConnection(com.alibaba.cobar.server.ServerConnection) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket) LinkedList(java.util.LinkedList)

Example 2 with MySQLPacket

use of com.alibaba.cobar.net.mysql.MySQLPacket in project cobar by alibaba.

the class MultiNodeExecutor method execute0.

/**
     * 执行
     */
private void execute0(RouteResultsetNode rrn, Channel c, boolean autocommit, BlockingSession ss, int flag) {
    ServerConnection sc = ss.getSource();
    if (isFail.get() || sc.isClosed()) {
        c.setRunning(false);
        handleFailure(ss, rrn, null);
        return;
    }
    try {
        // 执行并等待返回
        BinaryPacket bin = ((MySQLChannel) c).execute(rrn, sc, autocommit);
        // 接收和处理数据
        final ReentrantLock lock = MultiNodeExecutor.this.lock;
        lock.lock();
        try {
            switch(bin.data[0]) {
                case ErrorPacket.FIELD_COUNT:
                    c.setRunning(false);
                    handleFailure(ss, rrn, new BinaryErrInfo((MySQLChannel) c, bin, sc, rrn));
                    break;
                case OkPacket.FIELD_COUNT:
                    OkPacket ok = new OkPacket();
                    ok.read(bin);
                    affectedRows += ok.affectedRows;
                    // set lastInsertId
                    if (ok.insertId > 0) {
                        insertId = (insertId == 0) ? ok.insertId : Math.min(insertId, ok.insertId);
                    }
                    c.setRunning(false);
                    handleSuccessOK(ss, rrn, autocommit, ok);
                    break;
                default:
                    // HEADER|FIELDS|FIELD_EOF|ROWS|LAST_EOF
                    final MySQLChannel mc = (MySQLChannel) c;
                    if (fieldEOF) {
                        for (; ; ) {
                            bin = mc.receive();
                            switch(bin.data[0]) {
                                case ErrorPacket.FIELD_COUNT:
                                    c.setRunning(false);
                                    handleFailure(ss, rrn, new BinaryErrInfo(mc, bin, sc, rrn));
                                    return;
                                case EOFPacket.FIELD_COUNT:
                                    handleRowData(rrn, c, ss);
                                    return;
                                default:
                                    continue;
                            }
                        }
                    } else {
                        // HEADER
                        bin.packetId = ++packetId;
                        List<MySQLPacket> headerList = new LinkedList<MySQLPacket>();
                        headerList.add(bin);
                        for (; ; ) {
                            bin = mc.receive();
                            switch(bin.data[0]) {
                                case ErrorPacket.FIELD_COUNT:
                                    c.setRunning(false);
                                    handleFailure(ss, rrn, new BinaryErrInfo(mc, bin, sc, rrn));
                                    return;
                                case EOFPacket.FIELD_COUNT:
                                    // FIELD_EOF
                                    bin.packetId = ++packetId;
                                    for (MySQLPacket packet : headerList) {
                                        buffer = packet.write(buffer, sc);
                                    }
                                    headerList = null;
                                    buffer = bin.write(buffer, sc);
                                    fieldEOF = true;
                                    handleRowData(rrn, c, ss);
                                    return;
                                default:
                                    // FIELDS
                                    bin.packetId = ++packetId;
                                    switch(flag) {
                                        case RouteResultset.REWRITE_FIELD:
                                            StringBuilder fieldName = new StringBuilder();
                                            fieldName.append("Tables_in_").append(ss.getSource().getSchema());
                                            FieldPacket field = PacketUtil.getField(bin, fieldName.toString());
                                            headerList.add(field);
                                            break;
                                        default:
                                            headerList.add(bin);
                                    }
                            }
                        }
                    }
            }
        } finally {
            lock.unlock();
        }
    } catch (final IOException e) {
        c.close();
        handleFailure(ss, rrn, new SimpleErrInfo(e, ErrorCode.ER_YES, sc, rrn));
    } catch (final RuntimeException e) {
        c.close();
        handleFailure(ss, rrn, new SimpleErrInfo(e, ErrorCode.ER_YES, sc, rrn));
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) OkPacket(com.alibaba.cobar.net.mysql.OkPacket) ServerConnection(com.alibaba.cobar.server.ServerConnection) IOException(java.io.IOException) LinkedList(java.util.LinkedList) MySQLPacket(com.alibaba.cobar.net.mysql.MySQLPacket) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Aggregations

FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)2 MySQLPacket (com.alibaba.cobar.net.mysql.MySQLPacket)2 ServerConnection (com.alibaba.cobar.server.ServerConnection)2 LinkedList (java.util.LinkedList)2 MySQLChannel (com.alibaba.cobar.mysql.bio.MySQLChannel)1 BinaryPacket (com.alibaba.cobar.net.mysql.BinaryPacket)1 OkPacket (com.alibaba.cobar.net.mysql.OkPacket)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1