Search in sources :

Example 6 with RowDataPacket

use of io.mycat.net.mysql.RowDataPacket in project Mycat_plus by coderczp.

the class ShareRowOutPutDataHandler method onRowData.

@Override
public boolean onRowData(String dataNode, byte[] rowData) {
    RowDataPacket rowDataPkgold = ResultSetUtil.parseRowData(rowData, bfields);
    // 拷贝一份batchRows
    Map<String, byte[]> batchRowsCopy = new ConcurrentHashMap<String, byte[]>();
    batchRowsCopy.putAll(arows);
    // 获取Id字段,
    String id = ByteUtil.getString(rowDataPkgold.fieldValues.get(joinR));
    // 查找ID对应的A表的记录
    // arows.remove(id);
    byte[] arow = getRow(batchRowsCopy, id, joinL);
    // byte[] arow = getRow(id,joinL);//arows.remove(id);
    while (arow != null) {
        // ctx.getAllFields());
        RowDataPacket rowDataPkg = ResultSetUtil.parseRowData(arow, afields);
        for (int i = 1; i < rowDataPkgold.fieldCount; i++) {
            // 设置b.name 字段
            byte[] bname = rowDataPkgold.fieldValues.get(i);
            rowDataPkg.add(bname);
            rowDataPkg.addFieldCount(1);
        }
        // RowData(rowDataPkg);
        // huangyiming add
        MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
        if (null == middlerResultHandler) {
            ctx.writeRow(rowDataPkg);
        } else {
            if (middlerResultHandler instanceof MiddlerQueryResultHandler) {
                // if(middlerResultHandler.getDataType().equalsIgnoreCase("string")){
                byte[] columnData = rowDataPkg.fieldValues.get(0);
                if (columnData != null && columnData.length > 0) {
                    String rowValue = new String(columnData);
                    middlerResultHandler.add(rowValue);
                }
            // }
            }
        }
        arow = getRow(batchRowsCopy, id, joinL);
    // arow = getRow(id,joinL);
    }
    return false;
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) MiddlerResultHandler(io.mycat.backend.mysql.nio.handler.MiddlerResultHandler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MiddlerQueryResultHandler(io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler)

Example 7 with RowDataPacket

use of io.mycat.net.mysql.RowDataPacket in project Mycat_plus by coderczp.

the class SingleNodeHandler method rowResponse.

/**
 * select
 *
 * 行数据返回时触发,将行数据写入缓冲区中
 */
@Override
public void rowResponse(byte[] row, BackendConnection conn) {
    this.netOutBytes += row.length;
    this.selectRows++;
    if (isDefaultNodeShowTable || isDefaultNodeShowFullTable) {
        RowDataPacket rowDataPacket = new RowDataPacket(1);
        rowDataPacket.read(row);
        String table = StringUtil.decode(rowDataPacket.fieldValues.get(0), session.getSource().getCharset());
        if (shardingTablesSet.contains(table.toUpperCase())) {
            return;
        }
    }
    row[3] = ++packetId;
    if (prepared) {
        RowDataPacket rowDataPk = new RowDataPacket(fieldCount);
        rowDataPk.read(row);
        BinaryRowDataPacket binRowDataPk = new BinaryRowDataPacket();
        binRowDataPk.read(fieldPackets, rowDataPk);
        binRowDataPk.packetId = rowDataPk.packetId;
        // binRowDataPk.write(session.getSource());
        /*
			 * [fix bug] : 这里不能直接将包写到前端连接,
			 * 因为在fieldEofResponse()方法结束后buffer还没写出,
			 * 所以这里应该将包数据顺序写入buffer(如果buffer满了就写出),然后再将buffer写出
			 */
        buffer = binRowDataPk.write(buffer, session.getSource(), true);
    } else {
        MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
        if (null == middlerResultHandler) {
            buffer = session.getSource().writeToBuffer(row, allocBuffer());
        } else {
            if (middlerResultHandler instanceof MiddlerQueryResultHandler) {
                byte[] rv = ResultSetUtil.getColumnVal(row, fields, 0);
                String rowValue = rv == null ? "" : new String(rv);
                middlerResultHandler.add(rowValue);
            }
        }
    }
}
Also used : BinaryRowDataPacket(io.mycat.net.mysql.BinaryRowDataPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) BinaryRowDataPacket(io.mycat.net.mysql.BinaryRowDataPacket)

Example 8 with RowDataPacket

use of io.mycat.net.mysql.RowDataPacket in project Mycat_plus by coderczp.

the class ShowDatasourceSyn method getRows.

private static List<RowDataPacket> getRows(String charset) {
    List<RowDataPacket> list = new LinkedList<RowDataPacket>();
    MycatConfig conf = MycatServer.getInstance().getConfig();
    // host nodes
    Map<String, PhysicalDBPool> dataHosts = conf.getDataHosts();
    for (PhysicalDBPool pool : dataHosts.values()) {
        for (PhysicalDatasource ds : pool.getAllDataSources()) {
            DBHeartbeat hb = ds.getHeartbeat();
            DataSourceSyncRecorder record = hb.getAsynRecorder();
            Map<String, String> states = record.getRecords();
            RowDataPacket row = new RowDataPacket(FIELD_COUNT);
            if (!states.isEmpty()) {
                row.add(StringUtil.encode(ds.getName(), charset));
                row.add(StringUtil.encode(ds.getConfig().getIp(), charset));
                row.add(LongUtil.toBytes(ds.getConfig().getPort()));
                row.add(StringUtil.encode(states.get("Master_Host"), charset));
                row.add(LongUtil.toBytes(Long.valueOf(states.get("Master_Port"))));
                row.add(StringUtil.encode(states.get("Master_Use"), charset));
                String secords = states.get("Seconds_Behind_Master");
                row.add(secords == null ? null : LongUtil.toBytes(Long.valueOf(secords)));
                row.add(StringUtil.encode(states.get("Slave_IO_Running"), charset));
                row.add(StringUtil.encode(states.get("Slave_SQL_Running"), charset));
                row.add(StringUtil.encode(states.get("Slave_IO_State"), charset));
                row.add(LongUtil.toBytes(Long.valueOf(states.get("Connect_Retry"))));
                row.add(StringUtil.encode(states.get("Last_IO_Error"), charset));
                list.add(row);
            }
        }
    }
    return list;
}
Also used : PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) DBHeartbeat(io.mycat.backend.heartbeat.DBHeartbeat) RowDataPacket(io.mycat.net.mysql.RowDataPacket) DataSourceSyncRecorder(io.mycat.statistic.DataSourceSyncRecorder) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) MycatConfig(io.mycat.config.MycatConfig) LinkedList(java.util.LinkedList)

Example 9 with RowDataPacket

use of io.mycat.net.mysql.RowDataPacket in project Mycat_plus by coderczp.

the class ShowDatasourceSyn method response.

public static void response(ManagerConnection c, String stmt) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    for (RowDataPacket row : getRows(c.getCharset())) {
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // post write
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 10 with RowDataPacket

use of io.mycat.net.mysql.RowDataPacket in project Mycat_plus by coderczp.

the class ShowDirectMemory method showDirectMemoryDetail.

public static void showDirectMemoryDetail(ManagerConnection c) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = detailHeader.write(buffer, c, true);
    // write fields
    for (FieldPacket field : detailFields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = detailEof.write(buffer, c, true);
    // write rows
    byte packetId = detailEof.packetId;
    ConcurrentHashMap<Long, Long> bufferpoolUsageMap = bufferPool.getNetDirectMemoryUsage();
    try {
        if (useOffHeapForMerge == 1) {
            ConcurrentHashMap<Long, Long> concurrentHashMap = MycatServer.getInstance().getMyCatMemory().getResultMergeMemoryManager().getDirectMemorUsage();
            for (Long key : concurrentHashMap.keySet()) {
                RowDataPacket row = new RowDataPacket(DETAILl_FIELD_COUNT);
                Long value = concurrentHashMap.get(key);
                row.add(String.valueOf(key).getBytes(c.getCharset()));
                /**
                 * 该DIRECTMEMORY内存被结果集处理使用了
                 */
                row.add("MergeMemoryPool".getBytes(c.getCharset()));
                row.add(value > 0 ? JavaUtils.bytesToString2(value).getBytes(c.getCharset()) : "0".getBytes(c.getCharset()));
                row.packetId = ++packetId;
                buffer = row.write(buffer, c, true);
            }
        }
        if (processorBufferPoolType == 2) {
        } else {
            for (Long key : bufferpoolUsageMap.keySet()) {
                RowDataPacket row = new RowDataPacket(DETAILl_FIELD_COUNT);
                Long value = bufferpoolUsageMap.get(key);
                row.add(String.valueOf(key).getBytes(c.getCharset()));
                /**
                 * 该DIRECTMEMORY内存属于Buffer Pool管理的!
                 */
                row.add("NetWorkBufferPool".getBytes(c.getCharset()));
                row.add(value > 0 ? JavaUtils.bytesToString2(value).getBytes(c.getCharset()) : "0".getBytes(c.getCharset()));
                row.packetId = ++packetId;
                buffer = row.write(buffer, c, true);
            }
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Aggregations

RowDataPacket (io.mycat.net.mysql.RowDataPacket)269 FieldPacket (io.mycat.net.mysql.FieldPacket)143 ByteBuffer (java.nio.ByteBuffer)140 EOFPacket (io.mycat.net.mysql.EOFPacket)137 MycatConfig (io.mycat.config.MycatConfig)20 LinkedList (java.util.LinkedList)16 PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)14 NIOProcessor (io.mycat.net.NIOProcessor)14 SchemaConfig (io.mycat.config.model.SchemaConfig)13 ResultSetHeaderPacket (io.mycat.net.mysql.ResultSetHeaderPacket)13 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)12 UserStat (io.mycat.statistic.stat.UserStat)12 IOException (java.io.IOException)12 Map (java.util.Map)12 DBHeartbeat (io.mycat.backend.heartbeat.DBHeartbeat)10 ServerConnection (io.mycat.server.ServerConnection)10 BinaryRowDataPacket (io.mycat.net.mysql.BinaryRowDataPacket)8 File (java.io.File)8 DataSourceSyncRecorder (io.mycat.statistic.DataSourceSyncRecorder)6 BufferedReader (java.io.BufferedReader)6