Search in sources :

Example 21 with FieldPacket

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

the class ExplainHandler method handle.

public static void handle(String stmt, ServerConnection c, int offset) {
    stmt = stmt.substring(offset);
    RouteResultset rrs = getRouteResultset(c, stmt);
    if (rrs == null)
        return;
    ByteBuffer buffer = c.allocate();
    // write header
    ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT);
    byte packetId = header.packetId;
    buffer = header.write(buffer, c);
    // write fields
    for (FieldPacket field : fields) {
        field.packetId = ++packetId;
        buffer = field.write(buffer, c);
    }
    // write eof
    EOFPacket eof = new EOFPacket();
    eof.packetId = ++packetId;
    buffer = eof.write(buffer, c);
    // write rows
    RouteResultsetNode[] rrsn = (rrs != null) ? rrs.getNodes() : EMPTY_ARRAY;
    for (RouteResultsetNode node : rrsn) {
        RowDataPacket row = getRow(node, c.getCharset());
        row.packetId = ++packetId;
        buffer = row.write(buffer, c);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c);
    // post write
    c.write(buffer);
}
Also used : ResultSetHeaderPacket(com.alibaba.cobar.net.mysql.ResultSetHeaderPacket) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket) RouteResultset(com.alibaba.cobar.route.RouteResultset)

Example 22 with FieldPacket

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

the class PacketUtil method getField.

public static final FieldPacket getField(String name, int type) {
    FieldPacket packet = new FieldPacket();
    packet.charsetIndex = CharsetUtil.getIndex(CODE_PAGE_1252);
    packet.name = encode(name, CODE_PAGE_1252);
    packet.type = (byte) type;
    return packet;
}
Also used : FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 23 with FieldPacket

use of com.alibaba.cobar.net.mysql.FieldPacket 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)

Example 24 with FieldPacket

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

the class PacketUtil method getField.

public static final FieldPacket getField(BinaryPacket src, String fieldName) {
    FieldPacket field = new FieldPacket();
    field.read(src);
    field.name = encode(fieldName, CODE_PAGE_1252);
    field.packetLength = field.calcPacketSize();
    return field;
}
Also used : FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 25 with FieldPacket

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

the class ShowThreadPool method execute.

public static void execute(ManagerConnection c) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c);
    }
    // write eof
    buffer = eof.write(buffer, c);
    // write rows
    byte packetId = eof.packetId;
    List<NameableExecutor> executors = getExecutors();
    for (NameableExecutor exec : executors) {
        if (exec != null) {
            RowDataPacket row = getRow(exec, c.getCharset());
            row.packetId = ++packetId;
            buffer = row.write(buffer, c);
        }
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c);
    // write buffer
    c.write(buffer);
}
Also used : NameableExecutor(com.alibaba.cobar.util.NameableExecutor) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Aggregations

FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)43 ByteBuffer (java.nio.ByteBuffer)40 EOFPacket (com.alibaba.cobar.net.mysql.EOFPacket)38 RowDataPacket (com.alibaba.cobar.net.mysql.RowDataPacket)37 NIOProcessor (com.alibaba.cobar.net.NIOProcessor)6 CobarConfig (com.alibaba.cobar.CobarConfig)5 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)5 SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)3 MySQLDataSource (com.alibaba.cobar.mysql.MySQLDataSource)3 SQLRecord (com.alibaba.cobar.statistic.SQLRecord)3 FrontendConnection (com.alibaba.cobar.net.FrontendConnection)2 MySQLPacket (com.alibaba.cobar.net.mysql.MySQLPacket)2 ResultSetHeaderPacket (com.alibaba.cobar.net.mysql.ResultSetHeaderPacket)2 ServerConnection (com.alibaba.cobar.server.ServerConnection)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 TreeSet (java.util.TreeSet)2 DataSourceConfig (com.alibaba.cobar.config.model.DataSourceConfig)1 UserConfig (com.alibaba.cobar.config.model.UserConfig)1 MySQLChannel (com.alibaba.cobar.mysql.bio.MySQLChannel)1