use of io.mycat.api.collector.RowBaseIterator in project Mycat2 by MyCATApache.
the class PrototypeService method getCreateTableSQLByJDBC.
public Optional<String> getCreateTableSQLByJDBC(String schemaName, String tableName, List<Partition> backends) {
Optional<JdbcConnectionManager> jdbcConnectionManagerOptional = getPrototypeConnectionManager();
if (!jdbcConnectionManagerOptional.isPresent()) {
return Optional.empty();
}
JdbcConnectionManager jdbcConnectionManager = jdbcConnectionManagerOptional.get();
backends = new ArrayList<>(backends);
backends.add(new BackendTableInfo(PROTOTYPE, schemaName, tableName));
if (backends == null || backends.isEmpty()) {
return Optional.empty();
}
for (Partition backend : backends) {
try {
Partition backendTableInfo = backend;
String targetName = backendTableInfo.getTargetName();
String targetSchemaTable = backendTableInfo.getTargetSchemaTable();
try (DefaultConnection connection = jdbcConnectionManager.getConnection(targetName)) {
String sql = "SHOW CREATE TABLE " + targetSchemaTable;
SQLStatement sqlStatement = null;
try (RowBaseIterator rowBaseIterator = connection.executeQuery(sql)) {
rowBaseIterator.next();
String string = rowBaseIterator.getString(1);
sqlStatement = SQLUtils.parseSingleMysqlStatement(string);
if (sqlStatement instanceof MySqlCreateTableStatement) {
MySqlCreateTableStatement sqlStatement1 = (MySqlCreateTableStatement) sqlStatement;
sqlStatement1.setTableName(SQLUtils.normalize(tableName));
// 顺序不能颠倒
sqlStatement1.setSchema(SQLUtils.normalize(schemaName));
return Optional.of(sqlStatement1.toString());
}
} catch (Exception e) {
LOGGER.error("", e);
}
if (sqlStatement == null || !(sqlStatement instanceof SQLCreateTableStatement)) {
try (RowBaseIterator rowBaseIterator = connection.executeQuery("select * from " + targetSchemaTable + " where 0 limit 0")) {
MycatRowMetaData metaData = rowBaseIterator.getMetaData();
String createTableSql = generateSql(schemaName, tableName, metaData.metaData());
return Optional.of(createTableSql);
}
}
continue;
} catch (Exception e) {
LOGGER.error("", e);
}
continue;
} catch (Throwable e) {
LOGGER.error("can not get create table sql from:" + backend.getTargetName() + backend.getTargetSchemaTable(), e);
continue;
}
}
return Optional.empty();
}
use of io.mycat.api.collector.RowBaseIterator in project Mycat2 by MyCATApache.
the class HeartbeatCollector method collect.
@Override
public List<MetricFamilySamples> collect() {
try {
RowBaseIterator rowBaseIterator = HintHandler.showHeatbeatStat();
List<String> columnList = ImmutableList.of("NAME");
List<Map<String, Object>> resultSetMap = rowBaseIterator.getResultSetMap();
GaugeMetricFamily gaugeMetricFamily = new GaugeMetricFamily("heartbeat_stat", "heartbeat_stat", columnList);
for (Map<String, Object> stringObjectMap : resultSetMap) {
Timestamp LAST_SEND_QUERY_TIME = Timestamp.valueOf((java.time.LocalDateTime) stringObjectMap.get("LAST_SEND_QUERY_TIME"));
Timestamp LAST_RECEIVED_QUERY_TIME = Timestamp.valueOf((java.time.LocalDateTime) stringObjectMap.get("LAST_RECEIVED_QUERY_TIME"));
// 查询时间之差
long l = LAST_RECEIVED_QUERY_TIME.getTime() - LAST_SEND_QUERY_TIME.getTime();
List<String> collect = columnList.stream().map(i -> stringObjectMap.get(i)).map(i -> Objects.toString(i)).collect(Collectors.toList());
gaugeMetricFamily.addMetric(collect, l);
}
return ImmutableList.of(gaugeMetricFamily);
} catch (Throwable e) {
LOGGER.error("", e);
throw e;
}
}
use of io.mycat.api.collector.RowBaseIterator in project Mycat2 by MyCATApache.
the class ExecutorProviderImpl method runAsObjectArray.
@Override
public RowBaseIterator runAsObjectArray(MycatDataContext context, String sqlStatement) {
DrdsSqlWithParams drdsSql = DrdsRunnerHelper.preParse(sqlStatement, context.getDefaultSchema());
Plan plan = DrdsRunnerHelper.getPlan(drdsSql);
AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = new AsyncMycatDataContextImpl.SqlMycatDataContextImpl(context, plan.getCodeExecuterContext(), drdsSql);
PrepareExecutor prepareExecutor = prepare(plan);
RowBaseIterator baseIterator = prepareExecutor.asRowBaseIterator(sqlMycatDataContext, plan.getMetaData());
return baseIterator;
}
use of io.mycat.api.collector.RowBaseIterator in project Mycat2 by MyCATApache.
the class NativeMycatServer method showNativeBackends.
@Override
public RowBaseIterator showNativeBackends() {
ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
resultSetBuilder.addColumnInfo("SESSION_ID", JDBCType.BIGINT).addColumnInfo("THREAD_NAME", JDBCType.VARCHAR).addColumnInfo("THREAD_ID", JDBCType.BIGINT).addColumnInfo("DS_NAME", JDBCType.VARCHAR).addColumnInfo("LAST_MESSAGE", JDBCType.VARCHAR).addColumnInfo("MYCAT_SESSION_ID", JDBCType.BIGINT).addColumnInfo("IS_IDLE", JDBCType.VARCHAR).addColumnInfo("SELECT_LIMIT", JDBCType.BIGINT).addColumnInfo("IS_AUTOCOMMIT", JDBCType.VARCHAR).addColumnInfo("IS_RESPONSE_FINISHED", JDBCType.VARCHAR).addColumnInfo("RESPONSE_TYPE", JDBCType.VARCHAR).addColumnInfo("IS_IN_TRANSACTION", JDBCType.VARCHAR).addColumnInfo("IS_REQUEST_SUCCESS", JDBCType.VARCHAR).addColumnInfo("IS_READ_ONLY", JDBCType.VARCHAR);
for (MycatReactorThread i : getReactorManager().getList()) {
List<MySQLClientSession> sqlClientSessions = datasourceMap.values().stream().flatMap(s -> s.getAllSessions().stream()).collect(Collectors.toList());
for (MySQLClientSession session : sqlClientSessions) {
long SESSION_ID = session.sessionId();
String THREAD_NAME = i.getName();
long THREAD_ID = i.getId();
String DS_NAME = session.getDatasource().getName();
String LAST_MESSAGE = session.getLastMessage();
Long MYCAT_SESSION_ID = Optional.ofNullable(session.getMycat()).map(m -> m.sessionId()).orElse(null);
boolean IS_IDLE = session.isIdle();
long SELECT_LIMIT = session.getSelectLimit();
boolean IS_AUTOCOMMIT = session.isAutomCommit() == MySQLAutoCommit.ON;
boolean IS_RESPONSE_FINISHED = session.isResponseFinished();
String RESPONSE_TYPE = Optional.ofNullable(session.getResponseType()).map(j -> j.name()).orElse(null);
boolean IS_IN_TRANSACTION = session.isMonopolizedByTransaction();
boolean IS_REQUEST_SUCCESS = session.isRequestSuccess();
boolean IS_READ_ONLY = session.isReadOnly();
resultSetBuilder.addObjectRowPayload(Arrays.asList(SESSION_ID, THREAD_NAME, THREAD_ID, DS_NAME, LAST_MESSAGE, MYCAT_SESSION_ID, IS_IDLE, SELECT_LIMIT, IS_AUTOCOMMIT, IS_RESPONSE_FINISHED, RESPONSE_TYPE, IS_IN_TRANSACTION, IS_REQUEST_SUCCESS, IS_READ_ONLY));
}
}
return resultSetBuilder.build();
}
use of io.mycat.api.collector.RowBaseIterator in project Mycat2 by MyCATApache.
the class NativeMycatServer method showConnections.
@Override
public RowBaseIterator showConnections() {
List<MycatSession> sessions = getMycatSessions();
ResultSetBuilder builder = ResultSetBuilder.create();
builder.addColumnInfo("ID", JDBCType.BIGINT);
builder.addColumnInfo("USER_NAME", JDBCType.VARCHAR);
builder.addColumnInfo("HOST", JDBCType.VARCHAR);
builder.addColumnInfo("SCHEMA", JDBCType.VARCHAR);
builder.addColumnInfo("AFFECTED_ROWS", JDBCType.BIGINT);
builder.addColumnInfo("AUTOCOMMIT", JDBCType.VARCHAR);
builder.addColumnInfo("IN_TRANSACTION", JDBCType.VARCHAR);
builder.addColumnInfo("CHARSET", JDBCType.VARCHAR);
builder.addColumnInfo("CHARSET_INDEX", JDBCType.BIGINT);
builder.addColumnInfo("OPEN", JDBCType.VARCHAR);
builder.addColumnInfo("SERVER_CAPABILITIES", JDBCType.BIGINT);
builder.addColumnInfo("ISOLATION", JDBCType.VARCHAR);
builder.addColumnInfo("LAST_ERROR_CODE", JDBCType.BIGINT);
builder.addColumnInfo("LAST_INSERT_ID", JDBCType.BIGINT);
builder.addColumnInfo("LAST_MESSAGE", JDBCType.VARCHAR);
builder.addColumnInfo("PROCESS_STATE", JDBCType.VARCHAR);
builder.addColumnInfo("WARNING_COUNT", JDBCType.BIGINT);
builder.addColumnInfo("MYSQL_SESSION_ID", JDBCType.BIGINT);
builder.addColumnInfo("TRANSACTION_TYPE", JDBCType.VARCHAR);
builder.addColumnInfo("TRANSCATION_SNAPSHOT", JDBCType.VARCHAR);
builder.addColumnInfo("CANCEL_FLAG", JDBCType.VARCHAR);
for (MycatSession session : sessions) {
long ID = session.sessionId();
MycatUser user = session.getUser();
String USER_NAME = user.getUserName();
String HOST = user.getHost();
String SCHEMA = session.getSchema();
long AFFECTED_ROWS = session.getAffectedRows();
boolean AUTOCOMMIT = session.isAutocommit();
boolean IN_TRANSACTION = session.isInTransaction();
String CHARSET = Optional.ofNullable(session.charset()).map(i -> i.displayName()).orElse("");
int CHARSET_INDEX = session.charsetIndex();
boolean OPEN = session.checkOpen();
int SERVER_CAPABILITIES = session.getServerCapabilities();
String ISOLATION = session.getIsolation().getText();
int LAST_ERROR_CODE = session.getLastErrorCode();
long LAST_INSERT_ID = session.getLastInsertId();
String LAST_MESSAGE = session.getLastMessage();
String PROCESS_STATE = session.getProcessState().name();
int WARNING_COUNT = session.getWarningCount();
Long MYSQL_SESSION_ID = Optional.ofNullable(session.getMySQLSession()).map(i -> i.sessionId()).orElse(null);
MycatDataContext dataContext = session.getDataContext();
String TRANSACTION_TYPE = Optional.ofNullable(dataContext.transactionType()).map(i -> i.getName()).orElse("");
TransactionSession transactionSession = dataContext.getTransactionSession();
String TRANSCATION_SMAPSHOT = transactionSession.snapshot().toString("|");
boolean CANCEL_FLAG = dataContext.getCancelFlag().get();
builder.addObjectRowPayload(Arrays.asList(ID, USER_NAME, HOST, SCHEMA, AFFECTED_ROWS, AUTOCOMMIT, IN_TRANSACTION, CHARSET, CHARSET_INDEX, OPEN, SERVER_CAPABILITIES, ISOLATION, LAST_ERROR_CODE, LAST_INSERT_ID, LAST_MESSAGE, PROCESS_STATE, WARNING_COUNT, MYSQL_SESSION_ID, TRANSACTION_TYPE, TRANSCATION_SMAPSHOT, CANCEL_FLAG));
}
return builder.build();
}
Aggregations