Search in sources :

Example 1 with SQLRecord

use of com.alibaba.cobar.statistic.SQLRecord in project cobar by alibaba.

the class ShowSQLSlow 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;
    SQLRecord[] records = CobarServer.getInstance().getSqlRecorder().getRecords();
    for (int i = records.length - 1; i >= 0; i--) {
        if (records[i] != null) {
            RowDataPacket row = getRow(records[i], 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 : RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) SQLRecord(com.alibaba.cobar.statistic.SQLRecord) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 2 with SQLRecord

use of com.alibaba.cobar.statistic.SQLRecord in project cobar by alibaba.

the class ShowSlow method dataNode.

public static void dataNode(ManagerConnection c, String name) {
    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;
    CobarConfig conf = CobarServer.getInstance().getConfig();
    MySQLDataNode dn = conf.getDataNodes().get(name);
    MySQLDataSource ds = null;
    if (dn != null && (ds = dn.getSource()) != null) {
        SQLRecord[] records = ds.getSqlRecorder().getRecords();
        for (int i = records.length - 1; i >= 0; i--) {
            if (records[i] != null) {
                RowDataPacket row = getRow(records[i], 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 : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) MySQLDataSource(com.alibaba.cobar.mysql.MySQLDataSource) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) CobarConfig(com.alibaba.cobar.CobarConfig) SQLRecord(com.alibaba.cobar.statistic.SQLRecord) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 3 with SQLRecord

use of com.alibaba.cobar.statistic.SQLRecord in project cobar by alibaba.

the class ShowSlow method schema.

public static void schema(ManagerConnection c, String name) {
    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;
    CobarConfig conf = CobarServer.getInstance().getConfig();
    SchemaConfig schema = conf.getSchemas().get(name);
    if (schema != null) {
        SQLRecorder recorder = new SQLRecorder(conf.getSystem().getSqlRecordCount());
        Map<String, MySQLDataNode> dataNodes = conf.getDataNodes();
        for (String n : schema.getAllDataNodes()) {
            MySQLDataNode dn = dataNodes.get(n);
            MySQLDataSource ds = null;
            if (dn != null && (ds = dn.getSource()) != null) {
                for (SQLRecord r : ds.getSqlRecorder().getRecords()) {
                    if (r != null && recorder.check(r.executeTime)) {
                        recorder.add(r);
                    }
                }
            }
        }
        SQLRecord[] records = recorder.getRecords();
        for (int i = records.length - 1; i >= 0; i--) {
            if (records[i] != null) {
                RowDataPacket row = getRow(records[i], 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 : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) SQLRecorder(com.alibaba.cobar.statistic.SQLRecorder) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) CobarConfig(com.alibaba.cobar.CobarConfig) SQLRecord(com.alibaba.cobar.statistic.SQLRecord) ByteBuffer(java.nio.ByteBuffer) MySQLDataSource(com.alibaba.cobar.mysql.MySQLDataSource) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 4 with SQLRecord

use of com.alibaba.cobar.statistic.SQLRecord in project cobar by alibaba.

the class MySQLChannel method recordSql.

/**
     * 记录sql执行信息
     */
private void recordSql(String host, String schema, String stmt) {
    long time = TimeUtil.currentTimeMillis() - lastActiveTime;
    SQLRecorder sqlRecorder = dataSource.getSqlRecorder();
    if (sqlRecorder.check(time)) {
        SQLRecord recorder = new SQLRecord();
        recorder.host = host;
        recorder.schema = schema;
        recorder.statement = stmt;
        recorder.startTime = lastActiveTime;
        recorder.executeTime = time;
        recorder.dataNode = dataSource.getName();
        recorder.dataNodeIndex = dataSource.getIndex();
        sqlRecorder.add(recorder);
    }
}
Also used : SQLRecorder(com.alibaba.cobar.statistic.SQLRecorder) SQLRecord(com.alibaba.cobar.statistic.SQLRecord)

Example 5 with SQLRecord

use of com.alibaba.cobar.statistic.SQLRecord in project cobar by alibaba.

the class MySQLConnection method recordSql.

/**
     * 记录sql执行信息
     */
public void recordSql(String host, String schema, String stmt) {
    final long now = TimeUtil.currentTimeMillis();
    if (now > this.lastTime) {
        long time = now - this.lastTime;
        SQLRecorder sqlRecorder = this.pool.getSqlRecorder();
        if (sqlRecorder.check(time)) {
            SQLRecord recorder = new SQLRecord();
            recorder.host = host;
            recorder.schema = schema;
            recorder.statement = stmt;
            recorder.startTime = lastTime;
            recorder.executeTime = time;
            recorder.dataNode = pool.getName();
            recorder.dataNodeIndex = pool.getIndex();
            sqlRecorder.add(recorder);
        }
    }
    this.lastTime = now;
}
Also used : SQLRecorder(com.alibaba.cobar.statistic.SQLRecorder) SQLRecord(com.alibaba.cobar.statistic.SQLRecord)

Aggregations

SQLRecord (com.alibaba.cobar.statistic.SQLRecord)5 EOFPacket (com.alibaba.cobar.net.mysql.EOFPacket)3 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)3 RowDataPacket (com.alibaba.cobar.net.mysql.RowDataPacket)3 SQLRecorder (com.alibaba.cobar.statistic.SQLRecorder)3 ByteBuffer (java.nio.ByteBuffer)3 CobarConfig (com.alibaba.cobar.CobarConfig)2 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)2 MySQLDataSource (com.alibaba.cobar.mysql.MySQLDataSource)2 SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)1