Search in sources :

Example 6 with ResultSetBuilder

use of io.mycat.beans.mycat.ResultSetBuilder in project Mycat2 by MyCATApache.

the class HintHandler method getSqlTimeFilter.

private Future<Void> getSqlTimeFilter(Response response, String body, MetadataManager metadataManager) {
    ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
    resultSetBuilder.addColumnInfo("value", JDBCType.VARCHAR);
    long sqlTimeFilter = -1;
    if (MetaClusterCurrent.exist(MycatSQLLogMonitor.class)) {
        MycatSQLLogMonitor mycatSQLLogMonitor = MetaClusterCurrent.wrapper(MycatSQLLogMonitor.class);
        sqlTimeFilter = mycatSQLLogMonitor.getSqlTimeFilter();
    }
    resultSetBuilder.addObjectRowPayload(Arrays.asList(sqlTimeFilter));
    return response.sendResultSet(resultSetBuilder.build());
}
Also used : ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) MycatSQLLogMonitor(io.mycat.monitor.MycatSQLLogMonitor)

Example 7 with ResultSetBuilder

use of io.mycat.beans.mycat.ResultSetBuilder in project Mycat2 by MyCATApache.

the class HintHandler method showSlowSql.

private Future<Void> showSlowSql(Response response, String body) {
    ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
    resultSetBuilder.addColumnInfo("trace_id", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("sql", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("sql_rows", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("start_time", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("end_time", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("execute_time", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("target_name", JDBCType.VARCHAR);
    Stream<SqlEntry> sqlRecords = SqlRecorderRuntime.INSTANCE.getRecords().stream().sorted(Comparator.comparingLong(SqlEntry::getSqlTime).reversed());
    Map map = JsonUtil.from(body, Map.class);
    Object idText = map.get("trace_id");
    if (idText != null) {
        sqlRecords = sqlRecords.filter(i -> idText == i.getTraceId());
    }
    sqlRecords.forEach(r -> {
        resultSetBuilder.addObjectRowPayload(Arrays.asList(Objects.toString(r.getTraceId()), Objects.toString(r.getSql()), Objects.toString(r.getAffectRow()), Objects.toString(r.getResponseTime().minus(Duration.ofMillis(r.getSqlTime()))), Objects.toString(r.getResponseTime()), Objects.toString(r.getSqlTime()), Objects.toString(null)));
    });
    return response.sendResultSet(resultSetBuilder.build());
}
Also used : MycatdbCommand(io.mycat.commands.MycatdbCommand) io.mycat(io.mycat) CSVRecord(org.apache.commons.csv.CSVRecord) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) CSVFormat(org.apache.commons.csv.CSVFormat) HeartbeatFlow(io.mycat.replica.heartbeat.HeartbeatFlow) Duration(java.time.Duration) MySQLErrorCode(io.mycat.beans.mysql.MySQLErrorCode) JsonUtil(io.mycat.util.JsonUtil) MySqlHintStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHintStatement) io.mycat.config(io.mycat.config) Timestamp(java.sql.Timestamp) Reader(java.io.Reader) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) ArrayBindable(org.apache.calcite.runtime.ArrayBindable) MycatSQLLogMonitor(io.mycat.monitor.MycatSQLLogMonitor) io.mycat.sqlhandler(io.mycat.sqlhandler) NormalTable(io.mycat.calcite.table.NormalTable) Nullable(org.jetbrains.annotations.Nullable) ReplicaSelector(io.mycat.replica.ReplicaSelector) UpdateSQLHandler(io.mycat.sqlhandler.dml.UpdateSQLHandler) Stream(java.util.stream.Stream) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) DrdsRunnerHelper(io.mycat.calcite.DrdsRunnerHelper) PhysicsInstance(io.mycat.replica.PhysicsInstance) NotNull(org.jetbrains.annotations.NotNull) ShardingTable(io.mycat.calcite.table.ShardingTable) VertxExecuter(io.mycat.vertx.VertxExecuter) SqlEntry(io.mycat.monitor.SqlEntry) java.util(java.util) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) LocalDateTime(java.time.LocalDateTime) XaLog(cn.mycat.vertx.xa.XaLog) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) SqlResultSetService(io.mycat.commands.SqlResultSetService) InterruptThreadHint(io.mycat.hint.InterruptThreadHint) MycatSQLExprTableSourceUtil(io.mycat.util.MycatSQLExprTableSourceUtil) KillThreadHint(io.mycat.hint.KillThreadHint) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) JDBCType(java.sql.JDBCType) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) ImmutableList(com.google.common.collect.ImmutableList) DatasourceStatus(io.mycat.replica.heartbeat.DatasourceStatus) Observable(io.reactivex.rxjava3.core.Observable) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) StreamSupport(java.util.stream.StreamSupport) GlobalTable(io.mycat.calcite.table.GlobalTable) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLUtils(com.alibaba.druid.sql.SQLUtils) Flowable(io.reactivex.rxjava3.core.Flowable) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) io.mycat.hint(io.mycat.hint) VertxUpdateExecuter(io.mycat.vertx.VertxUpdateExecuter) StringWriter(java.io.StringWriter) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) ExecutorProvider(io.mycat.calcite.ExecutorProvider) IOException(java.io.IOException) QuoteMode(org.apache.commons.csv.QuoteMode) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) SqlRecorderRuntime(io.mycat.exporter.SqlRecorderRuntime) VertxUtil(io.mycat.util.VertxUtil) ReplicaSwitchType(io.mycat.replica.ReplicaSwitchType) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) MycatInsertRel(io.mycat.calcite.physical.MycatInsertRel) io.mycat.calcite.spm(io.mycat.calcite.spm) NameMap(io.mycat.util.NameMap) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) JdbcUtils(com.alibaba.druid.util.JdbcUtils) CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) HeartBeatStatus(io.mycat.replica.heartbeat.HeartBeatStatus) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) UnmodifiableIterator(com.google.common.collect.UnmodifiableIterator) FileReader(java.io.FileReader) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) StorageManager(io.mycat.sqlhandler.config.StorageManager) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SqlEntry(io.mycat.monitor.SqlEntry) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) NameMap(io.mycat.util.NameMap)

Example 8 with ResultSetBuilder

use of io.mycat.beans.mycat.ResultSetBuilder in project Mycat2 by MyCATApache.

the class HintHandler method showTopology.

private Future<Void> showTopology(Response response, String body, MetadataManager metadataManager) {
    Map map = JsonUtil.from(body, Map.class);
    TableHandler table = metadataManager.getTable((String) map.get("schemaName"), (String) map.get("tableName"));
    LogicTableType type = table.getType();
    List<Partition> backends = null;
    switch(type) {
        case SHARDING:
            backends = ((ShardingTable) table).getBackends();
            break;
        case GLOBAL:
            backends = ((GlobalTable) table).getGlobalDataNode();
            break;
        case NORMAL:
            backends = Collections.singletonList(((NormalTable) table).getDataNode());
            break;
        case CUSTOM:
            throw new UnsupportedOperationException("unsupport custom table");
    }
    ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
    resultSetBuilder.addColumnInfo("targetName", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("schemaName", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("tableName", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("dbIndex", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("tableIndex", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("index", JDBCType.VARCHAR);
    for (Partition partition : backends) {
        String targetName = partition.getTargetName();
        String schemaName = partition.getSchema();
        String tableName = partition.getTable();
        Integer dbIndex = partition.getDbIndex();
        Integer tableIndex = partition.getTableIndex();
        Integer index = partition.getIndex();
        resultSetBuilder.addObjectRowPayload(Arrays.asList(targetName, schemaName, tableName, dbIndex, tableIndex, index));
    }
    return response.sendResultSet(resultSetBuilder.build());
}
Also used : NormalTable(io.mycat.calcite.table.NormalTable) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) NameMap(io.mycat.util.NameMap)

Example 9 with ResultSetBuilder

use of io.mycat.beans.mycat.ResultSetBuilder in project Mycat2 by MyCATApache.

the class ExplainSQLHandler method onExecute.

@Override
@SneakyThrows
protected Future<Void> onExecute(SQLRequest<MySqlExplainStatement> request, MycatDataContext dataContext, Response response) {
    MySqlExplainStatement explainAst = request.getAst();
    explainAst.accept0(new MySqlASTVisitorAdapter() {

        @Override
        public boolean visit(SQLExprTableSource x) {
            if (x.getSchema() == null) {
                x.setSchema(dataContext.getDefaultSchema());
            }
            return super.visit(x);
        }
    });
    if (explainAst.isDescribe()) {
        SQLName tableName = explainAst.getTableName();
        if (tableName instanceof SQLIdentifierExpr && dataContext.getDefaultSchema() != null) {
            explainAst.setTableName(new SQLPropertyExpr(new SQLIdentifierExpr(dataContext.getDefaultSchema()), ((SQLIdentifierExpr) tableName).getName()));
        }
        return response.proxySelectToPrototype(explainAst.toUnformattedString(), Collections.emptyList());
    }
    SQLStatement statement = request.getAst().getStatement();
    boolean forUpdate = false;
    if (statement instanceof SQLSelectStatement) {
        forUpdate = ((SQLSelectStatement) explainAst.getStatement()).getSelect().getFirstQueryBlock().isForUpdate();
    }
    ResultSetBuilder builder = ResultSetBuilder.create().addColumnInfo("plan", JDBCType.VARCHAR);
    try {
        HackRouter hackRouter = new HackRouter(statement, dataContext);
        if (hackRouter.analyse()) {
            Pair<String, String> plan = hackRouter.getPlan();
            builder.addObjectRowPayload(Arrays.asList("targetName: " + plan.getKey() + "   sql: " + plan.getValue()));
        } else {
            List<SQLCommentHint> hints = explainAst.getHints();
            if (hints != null) {
                statement.setHeadHints(hints);
            }
            DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(statement, dataContext.getDefaultSchema());
            Plan plan;
            if (statement instanceof SQLInsertStatement || statement instanceof SQLUpdateStatement || statement instanceof SQLDeleteStatement) {
                plan = UpdateSQLHandler.getPlan(drdsSqlWithParams);
            } else {
                plan = DrdsRunnerHelper.getPlan(drdsSqlWithParams);
            }
            List<String> explain = plan.explain(dataContext, drdsSqlWithParams, true);
            for (String s1 : explain) {
                builder.addObjectRowPayload(Arrays.asList(s1));
            }
        }
    } catch (Throwable th) {
        LOGGER.error("", th);
        builder.addObjectRowPayload(Arrays.asList(th.toString()));
    }
    return response.sendResultSet(RowIterable.create(builder.build()));
}
Also used : MySqlExplainStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExplainStatement) MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) Plan(io.mycat.calcite.spm.Plan) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) HackRouter(io.mycat.prototypeserver.mysql.HackRouter) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) SneakyThrows(lombok.SneakyThrows)

Example 10 with ResultSetBuilder

use of io.mycat.beans.mycat.ResultSetBuilder in project Mycat2 by MyCATApache.

the class MySQLCheckHandler method onExecute.

@Override
protected Future<Void> onExecute(SQLRequest<MySqlCheckTableStatement> request, MycatDataContext dataContext, Response response) {
    ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
    resultSetBuilder.addColumnInfo("TABLE", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("OP", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("MSG_TYPE", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("MSG_TEXT", JDBCType.VARCHAR);
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    List<Throwable> throwables = Collections.synchronizedList(new LinkedList<>());
    MySqlCheckTableStatement ast = request.getAst();
    for (SQLExprTableSource table : ast.getTables()) {
        resolveSQLExprTableSource(table, dataContext);
        String schemaName = SQLUtils.normalize(table.getSchema());
        String tableName = SQLUtils.normalize(table.getTableName());
        TableHandler tableHandler = metadataManager.getTable(schemaName, tableName);
        Set<String> errorInfo = new HashSet<>();
        switch(tableHandler.getType()) {
            case SHARDING:
                {
                    ShardingTableHandler shardingTableHandler = (ShardingTableHandler) tableHandler;
                    errorInfo = check(metadataManager, jdbcConnectionManager, throwables, shardingTableHandler.dataNodes().parallelStream());
                    break;
                }
            case GLOBAL:
                {
                    GlobalTableHandler globalTableHandler = (GlobalTableHandler) tableHandler;
                    errorInfo = check(metadataManager, jdbcConnectionManager, throwables, globalTableHandler.getGlobalDataNode().parallelStream());
                    break;
                }
            case NORMAL:
                {
                    break;
                }
            case CUSTOM:
                {
                    break;
                }
            default:
                throw new IllegalStateException("Unexpected value: " + tableHandler.getType());
        }
        resultSetBuilder.addObjectRowPayload(Arrays.asList(table.toString(), "check", errorInfo.isEmpty() ? "Ok" : "Error", String.join(",", errorInfo)));
    }
    return response.sendResultSet(resultSetBuilder.build());
}
Also used : ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) ShardingTableHandler(io.mycat.router.ShardingTableHandler) GlobalTableHandler(io.mycat.calcite.table.GlobalTableHandler) GlobalTableHandler(io.mycat.calcite.table.GlobalTableHandler) ShardingTableHandler(io.mycat.router.ShardingTableHandler) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) MySqlCheckTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCheckTableStatement)

Aggregations

ResultSetBuilder (io.mycat.beans.mycat.ResultSetBuilder)32 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)13 JDBCType (java.sql.JDBCType)12 LocalDateTime (java.time.LocalDateTime)11 io.mycat.config (io.mycat.config)10 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)10 IOException (java.io.IOException)10 Timestamp (java.sql.Timestamp)10 java.util (java.util)10 Collectors (java.util.stream.Collectors)10 NotNull (org.jetbrains.annotations.NotNull)10 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)9 NameMap (io.mycat.util.NameMap)9 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)8 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)8 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)8 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)7 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)7 ImmutableList (com.google.common.collect.ImmutableList)7 MysqlPayloadObject (io.mycat.api.collector.MysqlPayloadObject)7