Search in sources :

Example 31 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.

the class SingleNodeHandler method connectionError.

@Override
public void connectionError(Throwable e, BackendConnection conn) {
    endRunning();
    ErrorPacket err = new ErrorPacket();
    err.packetId = ++packetId;
    err.errno = ErrorCode.ER_NEW_ABORTING_CONNECTION;
    err.message = StringUtil.encode(e.getMessage(), session.getSource().getCharset());
    ServerConnection source = session.getSource();
    source.write(err.write(allocBuffer(), source, true));
}
Also used : ErrorPacket(io.mycat.net.mysql.ErrorPacket) ServerConnection(io.mycat.server.ServerConnection)

Example 32 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.

the class SingleNodeHandler method rowEofResponse.

/**
 * select
 *
 * 行结束标志返回时触发,将EOF标志写入缓冲区,最后调用source.write(buffer)将缓冲区放入前端连接的写缓冲队列中,等待NIOSocketWR将其发送给应用
 */
@Override
public void rowEofResponse(byte[] eof, BackendConnection conn) {
    this.netOutBytes += eof.length;
    ServerConnection source = session.getSource();
    conn.recordSql(source.getHost(), source.getSchema(), node.getStatement());
    // 判断是调用存储过程的话不能在这里释放链接
    if (!rrs.isCallStatement() || (rrs.isCallStatement() && rrs.getProcedure().isResultSimpleValue())) {
        session.releaseConnectionIfSafe(conn, LOGGER.isDebugEnabled(), false);
        endRunning();
    }
    eof[3] = ++packetId;
    buffer = source.writeToBuffer(eof, allocBuffer());
    int resultSize = source.getWriteQueue().size() * MycatServer.getInstance().getConfig().getSystem().getBufferPoolPageSize();
    resultSize = resultSize + buffer.position();
    MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
    if (middlerResultHandler != null) {
        middlerResultHandler.secondEexcute();
    } else {
        source.write(buffer);
    }
    source.setExecuteSql(null);
    // TODO: add by zhuam
    // 查询结果派发
    QueryResult queryResult = new QueryResult(session.getSource().getUser(), rrs.getSqlType(), rrs.getStatement(), affectedRows, netInBytes, netOutBytes, startTime, System.currentTimeMillis(), resultSize);
    QueryResultDispatcher.dispatchQuery(queryResult);
}
Also used : QueryResult(io.mycat.statistic.stat.QueryResult) ServerConnection(io.mycat.server.ServerConnection)

Example 33 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.

the class SingleNodeHandler method backConnectionErr.

private void backConnectionErr(ErrorPacket errPkg, BackendConnection conn) {
    endRunning();
    ServerConnection source = session.getSource();
    String errUser = source.getUser();
    String errHost = source.getHost();
    int errPort = source.getLocalPort();
    String errmgs = " errno:" + errPkg.errno + " " + new String(errPkg.message);
    LOGGER.warn("execute  sql err :" + errmgs + " con:" + conn + " frontend host:" + errHost + "/" + errPort + "/" + errUser);
    session.releaseConnectionIfSafe(conn, LOGGER.isDebugEnabled(), false);
    source.setTxInterrupt(errmgs);
    /**
     * TODO: 修复全版本BUG
     *
     * BUG复现:
     * 1、MysqlClient:  SELECT 9223372036854775807 + 1;
     * 2、MyCatServer:  ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'
     * 3、MysqlClient: ERROR 2013 (HY000): Lost connection to MySQL server during query
     *
     * Fixed后
     * 1、MysqlClient:  SELECT 9223372036854775807 + 1;
     * 2、MyCatServer:  ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'
     * 3、MysqlClient: ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'
     */
    // 由于 pakcetId != 1 造成的问题
    errPkg.packetId = 1;
    errPkg.write(source);
    recycleResources();
}
Also used : ServerConnection(io.mycat.server.ServerConnection)

Example 34 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.

the class DataNodeMergeManager method run.

@Override
public void run() {
    if (!running.compareAndSet(false, true)) {
        return;
    }
    boolean nulpack = false;
    try {
        for (; ; ) {
            final PackWraper pack = packs.poll();
            if (pack == null) {
                nulpack = true;
                break;
            }
            if (pack == END_FLAG_PACK) {
                hasEndFlag = true;
                if (packs.peek() != null) {
                    packs.add(pack);
                    continue;
                }
                /**
                 * 最后一个节点datenode发送了row eof packet说明了整个
                 * 分片数据全部接收完成,进而将结果集全部发给你Mycat 客户端
                 */
                final int warningCount = 0;
                final EOFPacket eofp = new EOFPacket();
                final ByteBuffer eof = ByteBuffer.allocate(9);
                BufferUtil.writeUB3(eof, eofp.calcPacketSize());
                eof.put(eofp.packetId);
                eof.put(eofp.fieldCount);
                BufferUtil.writeUB2(eof, warningCount);
                BufferUtil.writeUB2(eof, eofp.status);
                final ServerConnection source = multiQueryHandler.getSession().getSource();
                final byte[] array = eof.array();
                Iterator<UnsafeRow> iters = null;
                if (unsafeRowGrouper != null) {
                    /**
                     * group by里面需要排序情况
                     */
                    if (globalSorter != null) {
                        iters = unsafeRowGrouper.getResult(globalSorter);
                    } else {
                        iters = unsafeRowGrouper.getResult(globalMergeResult);
                    }
                } else if (globalSorter != null) {
                    iters = globalSorter.sort();
                } else if (!isStreamOutputResult) {
                    iters = globalMergeResult.sort();
                }
                if (iters != null) {
                    multiQueryHandler.outputMergeResult(source, array, iters, isMiddleResultDone);
                }
                break;
            }
            unsafeRow = new UnsafeRow(fieldCount);
            bufferHolder = new BufferHolder(unsafeRow, 0);
            unsafeRowWriter = new UnsafeRowWriter(bufferHolder, fieldCount);
            bufferHolder.reset();
            /**
             *构造一行row,将对应的col填充.
             */
            MySQLMessage mm = new MySQLMessage(pack.rowData);
            mm.readUB3();
            mm.read();
            int nullnum = 0;
            for (int i = 0; i < fieldCount; i++) {
                byte[] colValue = mm.readBytesWithLength();
                if (colValue != null)
                    unsafeRowWriter.write(i, colValue);
                else {
                    if (mergeColsIndex != null && mergeColsIndex.length > 0) {
                        if (Arrays.binarySearch(mergeColsIndex, i) < 0) {
                            nullnum++;
                        }
                    }
                    unsafeRow.setNullAt(i);
                }
            }
            if (mergeColsIndex != null && mergeColsIndex.length > 0) {
                if (nullnum == (fieldCount - mergeColsIndex.length)) {
                    if (!hasEndFlag) {
                        packs.add(pack);
                        continue;
                    }
                }
            }
            unsafeRow.setTotalSize(bufferHolder.totalSize());
            if (unsafeRowGrouper != null) {
                unsafeRowGrouper.addRow(unsafeRow);
            } else if (globalSorter != null) {
                globalSorter.insertRow(unsafeRow);
            } else {
                globalMergeResult.insertRow(unsafeRow);
            }
            unsafeRow = null;
            bufferHolder = null;
            unsafeRowWriter = null;
        }
    } catch (final Exception e) {
        e.printStackTrace();
        multiQueryHandler.handleDataProcessException(e);
    } finally {
        running.set(false);
        if (nulpack && !packs.isEmpty()) {
            this.run();
        }
    }
}
Also used : EOFPacket(io.mycat.net.mysql.EOFPacket) ServerConnection(io.mycat.server.ServerConnection) UnsafeRowWriter(io.mycat.memory.unsafe.row.UnsafeRowWriter) ByteBuffer(java.nio.ByteBuffer) UnsafeRow(io.mycat.memory.unsafe.row.UnsafeRow) BufferHolder(io.mycat.memory.unsafe.row.BufferHolder) IOException(java.io.IOException) MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

Example 35 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.

the class EngineCtx method writeRow.

public void writeRow(RowDataPacket rowDataPkg) {
    ServerConnection sc = session.getSource();
    try {
        writeLock.lock();
        rowDataPkg.packetId = incPackageId();
        // 输出完整的 记录到客户端
        ByteBuffer buf = rowDataPkg.write(sc.allocate(), sc, true);
        sc.write(buf);
    // LOGGER.info("write  row ,packgId:" + rowDataPkg.packetId);
    } finally {
        writeLock.unlock();
    }
}
Also used : ServerConnection(io.mycat.server.ServerConnection) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ServerConnection (io.mycat.server.ServerConnection)36 ByteBuffer (java.nio.ByteBuffer)18 EOFPacket (io.mycat.net.mysql.EOFPacket)12 RowDataPacket (io.mycat.net.mysql.RowDataPacket)10 FieldPacket (io.mycat.net.mysql.FieldPacket)8 QueryResult (io.mycat.statistic.stat.QueryResult)8 IOException (java.io.IOException)8 FrontendConnection (io.mycat.net.FrontendConnection)4 NIOProcessor (io.mycat.net.NIOProcessor)4 OkPacket (io.mycat.net.mysql.OkPacket)4 ResultSetHeaderPacket (io.mycat.net.mysql.ResultSetHeaderPacket)4 BackendConnection (io.mycat.backend.BackendConnection)2 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)2 MySQLMessage (io.mycat.backend.mysql.MySQLMessage)2 MySQLConnection (io.mycat.backend.mysql.nio.MySQLConnection)2 MycatConfig (io.mycat.config.MycatConfig)2 BufferHolder (io.mycat.memory.unsafe.row.BufferHolder)2 UnsafeRow (io.mycat.memory.unsafe.row.UnsafeRow)2 UnsafeRowWriter (io.mycat.memory.unsafe.row.UnsafeRowWriter)2 BinaryRowDataPacket (io.mycat.net.mysql.BinaryRowDataPacket)2