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());
}
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());
}
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());
}
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()));
}
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());
}
Aggregations