Search in sources :

Example 1 with RowBaseIterator

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();
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData)

Example 2 with RowBaseIterator

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;
    }
}
Also used : java.util(java.util) Collector(io.prometheus.client.Collector) Logger(org.slf4j.Logger) ImmutableList(com.google.common.collect.ImmutableList) Timestamp(java.sql.Timestamp) LoggerFactory(org.slf4j.LoggerFactory) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) GaugeMetricFamily(io.prometheus.client.GaugeMetricFamily) HintHandler(io.mycat.sqlhandler.dql.HintHandler) Collectors(java.util.stream.Collectors) Timestamp(java.sql.Timestamp) GaugeMetricFamily(io.prometheus.client.GaugeMetricFamily) RowBaseIterator(io.mycat.api.collector.RowBaseIterator)

Example 3 with RowBaseIterator

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;
}
Also used : AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) PhysicalPlan(io.ordinate.engine.physicalplan.PhysicalPlan) Plan(io.mycat.calcite.spm.Plan) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams)

Example 4 with RowBaseIterator

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();
}
Also used : java.util(java.util) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) Getter(lombok.Getter) SneakyThrows(lombok.SneakyThrows) MySQLAutoCommit(io.mycat.beans.mysql.MySQLAutoCommit) LoggerFactory(org.slf4j.LoggerFactory) LocalDateTime(java.time.LocalDateTime) DefaultReactorBufferPool(io.mycat.buffer.DefaultReactorBufferPool) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) JDBCType(java.sql.JDBCType) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) io.mycat.proxy.reactor(io.mycat.proxy.reactor) Logger(org.slf4j.Logger) io.mycat.config(io.mycat.config) Timestamp(java.sql.Timestamp) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) io.mycat.proxy.session(io.mycat.proxy.session) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) MySQLDatasource(io.mycat.beans.MySQLDatasource) CommandDispatcher(io.mycat.command.CommandDispatcher) MySQLDatasourcePool(io.mycat.proxy.MySQLDatasourcePool) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NotNull(org.jetbrains.annotations.NotNull) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder)

Example 5 with RowBaseIterator

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();
}
Also used : java.util(java.util) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) Getter(lombok.Getter) SneakyThrows(lombok.SneakyThrows) MySQLAutoCommit(io.mycat.beans.mysql.MySQLAutoCommit) LoggerFactory(org.slf4j.LoggerFactory) LocalDateTime(java.time.LocalDateTime) DefaultReactorBufferPool(io.mycat.buffer.DefaultReactorBufferPool) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) JDBCType(java.sql.JDBCType) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) io.mycat.proxy.reactor(io.mycat.proxy.reactor) Logger(org.slf4j.Logger) io.mycat.config(io.mycat.config) Timestamp(java.sql.Timestamp) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) io.mycat.proxy.session(io.mycat.proxy.session) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) MySQLDatasource(io.mycat.beans.MySQLDatasource) CommandDispatcher(io.mycat.command.CommandDispatcher) MySQLDatasourcePool(io.mycat.proxy.MySQLDatasourcePool) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NotNull(org.jetbrains.annotations.NotNull) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder)

Aggregations

RowBaseIterator (io.mycat.api.collector.RowBaseIterator)25 ResultSetBuilder (io.mycat.beans.mycat.ResultSetBuilder)11 java.util (java.util)11 Collectors (java.util.stream.Collectors)11 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)10 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)9 Timestamp (java.sql.Timestamp)9 NotNull (org.jetbrains.annotations.NotNull)9 io.mycat.config (io.mycat.config)8 IOException (java.io.IOException)8 JDBCType (java.sql.JDBCType)8 LocalDateTime (java.time.LocalDateTime)8 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)7 ImmutableList (com.google.common.collect.ImmutableList)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 SneakyThrows (lombok.SneakyThrows)6 SQLUtils (com.alibaba.druid.sql.SQLUtils)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)5 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)5