Search in sources :

Example 1 with SQLRecorder

use of com.alibaba.cobar.statistic.SQLRecorder 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 2 with SQLRecorder

use of com.alibaba.cobar.statistic.SQLRecorder 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 3 with SQLRecorder

use of com.alibaba.cobar.statistic.SQLRecorder 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)3 SQLRecorder (com.alibaba.cobar.statistic.SQLRecorder)3 CobarConfig (com.alibaba.cobar.CobarConfig)1 SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)1 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)1 MySQLDataSource (com.alibaba.cobar.mysql.MySQLDataSource)1 EOFPacket (com.alibaba.cobar.net.mysql.EOFPacket)1 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)1 RowDataPacket (com.alibaba.cobar.net.mysql.RowDataPacket)1 ByteBuffer (java.nio.ByteBuffer)1