Search in sources :

Example 1 with IOExecutor

use of io.mycat.IOExecutor in project Mycat2 by MyCATApache.

the class MySQLLogConsumer method accept.

@Override
@SneakyThrows
public void accept(SqlEntry sqlEntry) {
    if (!init) {
        init = true;
        try {
            init();
        } catch (Exception e) {
            LOGGER.error("", e);
        }
    }
    boolean isInRuntime = MetaClusterCurrent.exist(IOExecutor.class) && MetaClusterCurrent.exist(JdbcConnectionManager.class) && MetaClusterCurrent.exist(IOExecutor.class);
    if (isInRuntime) {
        IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
        JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
        MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
        ioExecutor.executeBlocking((Handler<Promise<Void>>) event -> {
            try {
                try (DefaultConnection connection = jdbcConnectionManager.getConnection(metadataManager.getPrototype())) {
                    JdbcUtils.execute(connection.getRawConnection(), "INSERT INTO `mycat`.`sql_log` (" + "`instanceId`," + "user," + "connectionId," + "ip," + "port," + "traceId," + "hash," + "sqlType," + "`sql`," + "transactionId," + "sqlTime," + "responseTime," + "affectRow," + "result," + "externalMessage)" + "values(?,?,?,?,?," + "?,?,?,?,?," + "?,?,?,?,?)", Arrays.asList(sqlEntry.getInstanceId(), sqlEntry.getUser(), sqlEntry.getConnectionId(), sqlEntry.getIp(), sqlEntry.getPort(), sqlEntry.getTraceId(), sqlEntry.getHash(), Objects.toString(sqlEntry.getSqlType()), sqlEntry.getSql(), sqlEntry.getTransactionId(), sqlEntry.getSqlTime(), sqlEntry.getResponseTime(), sqlEntry.getAffectRow(), sqlEntry.isResult(), sqlEntry.getExternalMessage()));
                }
            } catch (Exception e) {
                LOGGER.info(" warning sql:{} , info:{}", sqlEntry.getSql(), sqlEntry);
                LOGGER.error("", e);
            } finally {
                event.tryComplete();
            }
        });
    }
}
Also used : MetadataManager(io.mycat.MetadataManager) SqlEntry(io.mycat.monitor.SqlEntry) java.util(java.util) Logger(org.slf4j.Logger) Connection(java.sql.Connection) MetaClusterCurrent(io.mycat.MetaClusterCurrent) SneakyThrows(lombok.SneakyThrows) Promise(io.vertx.core.Promise) LoggerFactory(org.slf4j.LoggerFactory) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) IOExecutor(io.mycat.IOExecutor) Consumer(java.util.function.Consumer) SQLException(java.sql.SQLException) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) JdbcUtils(com.alibaba.druid.util.JdbcUtils) Handler(io.vertx.core.Handler) Promise(io.vertx.core.Promise) MetadataManager(io.mycat.MetadataManager) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) IOExecutor(io.mycat.IOExecutor) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SQLException(java.sql.SQLException) SneakyThrows(lombok.SneakyThrows)

Example 2 with IOExecutor

use of io.mycat.IOExecutor in project Mycat2 by MyCATApache.

the class ThreadPoolCollector method collect.

@Override
public List<MetricFamilySamples> collect() {
    try {
        List<String> columnList = ImmutableList.of("ACTIVE_COUNT");
        GaugeMetricFamily gaugeMetricFamily = new GaugeMetricFamily("thread_pool_active", "thread_pool_active", columnList);
        long count = 0;
        if (MetaClusterCurrent.exist(IOExecutor.class)) {
            IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
            count = ioExecutor.count();
        }
        gaugeMetricFamily.addMetric(columnList, count);
        return ImmutableList.of(gaugeMetricFamily);
    } catch (Throwable e) {
        LOGGER.error("", e);
        throw e;
    }
}
Also used : IOExecutor(io.mycat.IOExecutor) GaugeMetricFamily(io.prometheus.client.GaugeMetricFamily)

Example 3 with IOExecutor

use of io.mycat.IOExecutor in project Mycat2 by MyCATApache.

the class ThreadMycatConnectionImplWrapper method insert.

@Override
public Future<SqlResult> insert(String sql, List<Object> params) {
    IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
    return ioExecutor.executeBlocking(promise -> {
        try {
            this.stat.plusThread();
            newMycatConnection.insert(sql, params).onComplete(promise);
        } catch (Exception e) {
            promise.tryFail(e);
        } finally {
            this.stat.decThread();
        }
    });
}
Also used : IOExecutor(io.mycat.IOExecutor)

Example 4 with IOExecutor

use of io.mycat.IOExecutor in project Mycat2 by MyCATApache.

the class ThreadMycatConnectionImplWrapper method update.

@Override
public Future<SqlResult> update(String sql, List<Object> params) {
    IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
    return ioExecutor.executeBlocking(promise -> {
        try {
            this.stat.plusThread();
            newMycatConnection.update(sql, params).onComplete(promise);
        } catch (Exception e) {
            promise.tryFail(e);
        } finally {
            this.stat.decThread();
        }
    });
}
Also used : IOExecutor(io.mycat.IOExecutor)

Example 5 with IOExecutor

use of io.mycat.IOExecutor in project Mycat2 by MyCATApache.

the class MycatSQLLogMonitorImpl method initBaseMonitor.

private void initBaseMonitor(Vertx vertx) {
    {
        TimerConfig instanceMonitorConfig = this.monitorConfig.getInstanceMonitor();
        TimeUnit timeUnit = TimeUnit.valueOf(instanceMonitorConfig.getTimeUnit());
        vertx.setTimer(timeUnit.toMillis(instanceMonitorConfig.getInitialDelay()), event -> vertx.setPeriodic(timeUnit.toMillis(instanceMonitorConfig.getPeriod()), event1 -> {
            IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
            ioExecutor.executeBlocking((Handler<Promise<Void>>) promise -> {
                try {
                    instanceSnapshot = InstanceEntry.snapshot();
                    InstanceEntry.reset();
                } finally {
                    promise.tryComplete();
                }
            });
        }));
    }
    {
        TimerConfig clusterConfig = this.monitorConfig.getClusterMonitor();
        TimeUnit timeUnit = TimeUnit.valueOf(clusterConfig.getTimeUnit());
        long readWriteRatioMillis = timeUnit.toMillis(clusterConfig.getPeriod());
        vertx.setTimer(timeUnit.toMillis(clusterConfig.getInitialDelay()), event -> vertx.setPeriodic(readWriteRatioMillis, event12 -> {
            IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
            ioExecutor.executeBlocking((Handler<Promise<Void>>) promise -> {
                try {
                    rwEntryMapSnapshot = RWEntry.snapshot();
                    RWEntry.reset();
                } finally {
                    promise.tryComplete();
                }
            });
        }));
    }
    // //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    {
        TimerConfig databaseInstanceMonitor = this.monitorConfig.getDatabaseInstanceMonitor();
        TimeUnit timeUnit = TimeUnit.valueOf(databaseInstanceMonitor.getTimeUnit());
        long databaseInstanceMonitorMillis = timeUnit.toMillis(databaseInstanceMonitor.getPeriod());
        vertx.setTimer(timeUnit.toMillis(databaseInstanceMonitor.getInitialDelay()), event -> vertx.setPeriodic(databaseInstanceMonitorMillis, event12 -> {
            IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
            ioExecutor.executeBlocking((Handler<Promise<Void>>) promise -> {
                try {
                    databaseInstanceMapSnapshot = DatabaseInstanceEntry.snapshot();
                    DatabaseInstanceEntry.reset();
                } finally {
                    promise.tryComplete();
                }
            });
        }));
    }
}
Also used : MonitorConfig(io.mycat.config.MonitorConfig) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Json(io.vertx.core.json.Json) Logger(org.slf4j.Logger) MetaClusterCurrent(io.mycat.MetaClusterCurrent) SneakyThrows(lombok.SneakyThrows) Promise(io.vertx.core.Promise) HttpServer(io.vertx.core.http.HttpServer) MultiMap(io.vertx.core.MultiMap) Vertx(io.vertx.core.Vertx) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOExecutor(io.mycat.IOExecutor) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) NameableExecutor(io.mycat.NameableExecutor) Map(java.util.Map) Data(lombok.Data) SqlLogConfig(io.mycat.config.SqlLogConfig) Optional(java.util.Optional) Handler(io.vertx.core.Handler) ExecutorUtil(io.mycat.ExecutorUtil) TimerConfig(io.mycat.config.TimerConfig) Promise(io.vertx.core.Promise) IOExecutor(io.mycat.IOExecutor) TimerConfig(io.mycat.config.TimerConfig) TimeUnit(java.util.concurrent.TimeUnit)

Aggregations

IOExecutor (io.mycat.IOExecutor)9 MetaClusterCurrent (io.mycat.MetaClusterCurrent)2 Handler (io.vertx.core.Handler)2 Promise (io.vertx.core.Promise)2 Consumer (java.util.function.Consumer)2 SneakyThrows (lombok.SneakyThrows)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 JdbcUtils (com.alibaba.druid.util.JdbcUtils)1 ExecutorUtil (io.mycat.ExecutorUtil)1 MetadataManager (io.mycat.MetadataManager)1 NameableExecutor (io.mycat.NameableExecutor)1 MonitorConfig (io.mycat.config.MonitorConfig)1 SqlLogConfig (io.mycat.config.SqlLogConfig)1 TimerConfig (io.mycat.config.TimerConfig)1 DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)1 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)1 SqlEntry (io.mycat.monitor.SqlEntry)1 GaugeMetricFamily (io.prometheus.client.GaugeMetricFamily)1 MultiMap (io.vertx.core.MultiMap)1