Search in sources :

Example 16 with RowDataPacket

use of com.actiontech.dble.net.mysql.RowDataPacket in project dble by actiontech.

the class SortedResultDiskBuffer method addRows.

@Override
public final int addRows(List<RowDataPacket> rows) {
    /**
     * we should make rows sorted first, then write them into file
     */
    if (logger.isDebugEnabled()) {
        logger.debug(" convert list to array start:" + TimeUtil.currentTimeMillis());
    }
    RowDataPacket[] rowArray = new RowDataPacket[rows.size()];
    rows.toArray(rowArray);
    final long start = file.getFilePointer();
    for (RowDataPacket row : rowArray) {
        byte[] b = row.toBytes();
        writeBuffer = writeToBuffer(b, writeBuffer);
    }
    // help for gc
    rowArray = null;
    writeBuffer.flip();
    file.write(writeBuffer);
    writeBuffer.clear();
    /* make a new tape */
    ResultDiskTape tape = makeResultDiskTape();
    tape.start = start;
    tape.filePos = start;
    tape.end = file.getFilePointer();
    tapes.add(tape);
    rowCount += rows.size();
    if (logger.isDebugEnabled()) {
        logger.debug("write rows to disk end:" + TimeUtil.currentTimeMillis());
    }
    return rowCount;
}
Also used : RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket)

Example 17 with RowDataPacket

use of com.actiontech.dble.net.mysql.RowDataPacket in project dble by actiontech.

the class SortedResultDiskBuffer method next.

@Override
public RowDataPacket next() {
    if (heap.isEmpty())
        return null;
    TapeItem tapeItem = heap.poll();
    RowDataPacket newRow = tapeItem.tape.nextRow();
    if (newRow != null) {
        heap.add(new TapeItem(newRow, tapeItem.tape));
    }
    return tapeItem.row;
}
Also used : RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket)

Example 18 with RowDataPacket

use of com.actiontech.dble.net.mysql.RowDataPacket in project dble by actiontech.

the class ShowServerLog method showLogSum.

private static PackageBufINf showLogSum(ManagerConnection c, ByteBuffer buffer, byte packetId) {
    PackageBufINf bufINf = new PackageBufINf();
    File[] logFiles = new File(SystemConfig.getHomePath(), "logs").listFiles();
    StringBuilder fileNames = new StringBuilder();
    if (logFiles != null) {
        for (File f : logFiles) {
            if (f.isFile()) {
                fileNames.append("  ");
                fileNames.append(f.getName());
            }
        }
    }
    File file = getLogFile(DEFAULT_LOGFILE);
    BufferedReader br = null;
    int totalLines = 0;
    CircularArrayList<String> queue = new CircularArrayList<>(50);
    try {
        br = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = br.readLine()) != null) {
            totalLines++;
            if (queue.size() == queue.capacity()) {
                queue.remove(0);
            }
            queue.add(line);
        }
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode("files in log dir:" + totalLines + fileNames, c.getCharset().getResults()));
        row.setPacketId(++packetId);
        buffer = row.write(buffer, c, true);
        row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode("Total lines " + totalLines + " ,tail " + queue.size() + " line is following:", c.getCharset().getResults()));
        row.setPacketId(++packetId);
        buffer = row.write(buffer, c, true);
        int size = queue.size() - 1;
        for (int i = size; i >= 0; i--) {
            String data = queue.get(i);
            row = new RowDataPacket(FIELD_COUNT);
            row.add(StringUtil.encode(data, c.getCharset().getResults()));
            row.setPacketId(++packetId);
            buffer = row.write(buffer, c, true);
        }
        bufINf.setBuffer(buffer);
        bufINf.setPacketId(packetId);
        return bufINf;
    } catch (Exception e) {
        LOGGER.info("showLogSumError", e);
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode(e.toString(), c.getCharset().getResults()));
        row.setPacketId(++packetId);
        buffer = row.write(buffer, c, true);
        bufINf.setBuffer(buffer);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                LOGGER.info("showLogSumError", e);
            }
        }
    }
    bufINf.setPacketId(packetId);
    return bufINf;
}
Also used : RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) CircularArrayList(com.actiontech.dble.util.CircularArrayList) IOException(java.io.IOException) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 19 with RowDataPacket

use of com.actiontech.dble.net.mysql.RowDataPacket in project dble by actiontech.

the class ShowServerLog method showLogRange.

public static PackageBufINf showLogRange(ManagerConnection c, ByteBuffer buffer, byte packetId, String key, String regex, int start, int end, String logFile) {
    PackageBufINf bufINf = new PackageBufINf();
    Pattern pattern = null;
    if (regex != null) {
        pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    }
    if (key != null) {
        key = key.toLowerCase();
    }
    File file = getLogFile(logFile);
    BufferedReader br = null;
    int curLine = 0;
    try {
        br = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = br.readLine()) != null) {
            curLine++;
            if (curLine >= start && curLine <= end && ((pattern != null && pattern.matcher(line).find()) || (pattern == null && key == null) || (key != null && line.toLowerCase().contains(key)))) {
                RowDataPacket row = new RowDataPacket(FIELD_COUNT);
                row.add(StringUtil.encode(curLine + "->" + line, c.getCharset().getResults()));
                row.setPacketId(++packetId);
                buffer = row.write(buffer, c, true);
            }
        }
        bufINf.setBuffer(buffer);
        bufINf.setPacketId(packetId);
        return bufINf;
    } catch (Exception e) {
        LOGGER.info("showLogRangeError", e);
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode(e.toString(), c.getCharset().getResults()));
        row.setPacketId(++packetId);
        buffer = row.write(buffer, c, true);
        bufINf.setBuffer(buffer);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                LOGGER.info("showLogRangeError", e);
            }
        }
    }
    bufINf.setPacketId(packetId);
    return bufINf;
}
Also used : Pattern(java.util.regex.Pattern) RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException)

Example 20 with RowDataPacket

use of com.actiontech.dble.net.mysql.RowDataPacket in project dble by actiontech.

the class ShowSQL method execute.

public static void execute(ManagerConnection c, boolean isClear) {
    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.getPacketId();
    Map<String, UserStat> statMap = UserStatAnalyzer.getInstance().getUserStatMap();
    for (UserStat userStat : statMap.values()) {
        String user = userStat.getUser();
        List<UserSqlLastStat.SqlLast> queries = userStat.getSqlLastStat().getQueries();
        int i = 1;
        for (UserSqlLastStat.SqlLast sqlLast : queries) {
            if (sqlLast != null) {
                RowDataPacket row = getRow(user, sqlLast, i, c.getCharset().getResults());
                row.setPacketId(++packetId);
                i++;
                buffer = row.write(buffer, c, true);
            }
        }
        if (isClear) {
            userStat.getSqlLastStat().clear();
        }
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.setPacketId(++packetId);
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) UserStat(com.actiontech.dble.statistic.stat.UserStat) EOFPacket(com.actiontech.dble.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) UserSqlLastStat(com.actiontech.dble.statistic.stat.UserSqlLastStat) FieldPacket(com.actiontech.dble.net.mysql.FieldPacket)

Aggregations

RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)141 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)59 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)59 ByteBuffer (java.nio.ByteBuffer)59 ServerConfig (com.actiontech.dble.config.ServerConfig)8 NIOProcessor (com.actiontech.dble.net.NIOProcessor)8 Map (java.util.Map)8 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)7 PhysicalDatasource (com.actiontech.dble.backend.datasource.PhysicalDatasource)7 MySQLConnection (com.actiontech.dble.backend.mysql.nio.MySQLConnection)7 LocalResult (com.actiontech.dble.backend.mysql.store.LocalResult)6 UnSortedLocalResult (com.actiontech.dble.backend.mysql.store.UnSortedLocalResult)6 DBHeartbeat (com.actiontech.dble.backend.heartbeat.DBHeartbeat)5 ItemSum (com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum)5 UserStat (com.actiontech.dble.statistic.stat.UserStat)5 BackendConnection (com.actiontech.dble.backend.BackendConnection)4 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)4 FrontendConnection (com.actiontech.dble.net.FrontendConnection)4 ResultSetHeaderPacket (com.actiontech.dble.net.mysql.ResultSetHeaderPacket)4 LinkedList (java.util.LinkedList)4