Search in sources :

Example 1 with DatabaseInstanceEntry

use of io.mycat.monitor.DatabaseInstanceEntry in project Mycat2 by MyCATApache.

the class VertxMySQLDatasourcePoolImpl method getConnection.

@Override
public Future<NewMycatConnection> getConnection() {
    LOGGER.debug("getConnection");
    Future<NewVertxConnectionImpl> map = mySQLPool.getConnection().map(sqlConnection -> {
        DatabaseInstanceEntry stat = DatabaseInstanceEntry.stat(targetName);
        stat.plusCon();
        stat.plusQps();
        return new NewVertxConnectionImpl((MySQLConnectionImpl) sqlConnection) {

            long start;

            @Override
            public void onSend() {
                start = System.currentTimeMillis();
            }

            @Override
            public void onRev() {
                long end = System.currentTimeMillis();
                InstanceMonitor.plusPrt(end - start);
            }

            @Override
            public Future<Void> close() {
                stat.decCon();
                return super.close();
            }
        };
    });
    map = map.onFailure(new Handler<Throwable>() {

        @Override
        public void handle(Throwable event) {
            System.out.println(event);
        }
    });
    return (Future) map;
}
Also used : DatabaseInstanceEntry(io.mycat.monitor.DatabaseInstanceEntry) NewVertxConnectionImpl(io.mycat.newquery.NewVertxConnectionImpl) Handler(io.vertx.core.Handler) Future(io.vertx.core.Future)

Example 2 with DatabaseInstanceEntry

use of io.mycat.monitor.DatabaseInstanceEntry in project Mycat2 by MyCATApache.

the class JdbcDatasourcePoolImpl method getConnection.

@Override
public Future<NewMycatConnection> getConnection() {
    try {
        JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
        DefaultConnection defaultConnection = jdbcConnectionManager.getConnection(targetName);
        DatabaseInstanceEntry stat = DatabaseInstanceEntry.stat(targetName);
        stat.plusCon();
        stat.plusQps();
        NewMycatConnectionImpl newMycatConnection = new NewMycatConnectionImpl(targetName, defaultConnection.getRawConnection()) {

            long start;

            final AtomicBoolean closeFlag = new AtomicBoolean(false);

            @Override
            public void onSend() {
                start = System.currentTimeMillis();
            }

            @Override
            public void onRev() {
                long end = System.currentTimeMillis();
                InstanceMonitor.plusPrt(end - start);
            }

            @Override
            public Future<Void> close() {
                if (closeFlag.compareAndSet(false, true)) {
                    stat.decCon();
                    defaultConnection.close();
                }
                return Future.succeededFuture();
            }

            @Override
            public void abandonConnection() {
                if (closeFlag.compareAndSet(false, true)) {
                    stat.decCon();
                    Connection rawConnection = defaultConnection.getRawConnection();
                    if (rawConnection instanceof DruidPooledConnection) {
                        DruidPooledConnection connection = (DruidPooledConnection) rawConnection;
                        connection.abandond();
                    }
                    defaultConnection.close();
                }
            }
        };
        return Future.succeededFuture(new ThreadMycatConnectionImplWrapper(stat, newMycatConnection));
    } catch (Throwable throwable) {
        return Future.failedFuture(throwable);
    }
}
Also used : DatabaseInstanceEntry(io.mycat.monitor.DatabaseInstanceEntry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) NewMycatConnectionImpl(io.mycat.newquery.NewMycatConnectionImpl) Connection(java.sql.Connection) NewMycatConnection(io.mycat.newquery.NewMycatConnection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ThreadMycatConnectionImplWrapper(io.mycat.monitor.ThreadMycatConnectionImplWrapper)

Example 3 with DatabaseInstanceEntry

use of io.mycat.monitor.DatabaseInstanceEntry in project Mycat2 by MyCATApache.

the class DatabaseInstanceChart method play.

private void play(String newValue) {
    if (StringUtil.isEmpty(newValue)) {
        return;
    }
    timeline.stop();
    timeline.getKeyFrames().clear();
    timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(Double.parseDouble(newValue)), actionEvent -> {
        Future<DatabaseInstanceEntry.DatabaseInstanceMap> instanceEntryFuture = monitorService.fetchDBEntry();
        instanceEntryFuture.onSuccess(databaseInstanceEntry -> {
            LinkedList<String[]> objects = new LinkedList<>();
            for (Map.Entry<String, DatabaseInstanceEntry.DatabaseInstanceEntryViewEntry> entry : databaseInstanceEntry.getDatabaseInstanceMap().entrySet()) {
                String key = entry.getKey();
                DatabaseInstanceEntry.DatabaseInstanceEntryViewEntry value = entry.getValue();
                long qpsSum = value.qps;
                long con = value.con;
                long thread = value.thread;
                objects.add(new String[] { key, qpsSum + "", con + "", thread + "" });
            }
            if (objects.isEmpty()) {
                objects.add(new String[] { "无数据", "无数据", "无数据", "无数据", "无数据" });
            }
            Platform.runLater(() -> {
                TableViewOuter tableViewOuter = new TableViewOuter(tableView);
                TableData tableData = new TableData(Arrays.asList("name", "qps", "con", "thread"), objects);
                tableViewOuter.appendData(tableData);
            });
        });
    }));
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.play();
}
Also used : Scene(javafx.scene.Scene) KeyFrame(javafx.animation.KeyFrame) TextField(javafx.scene.control.TextField) java.util(java.util) Label(javafx.scene.control.Label) DatabaseInstanceEntry(io.mycat.monitor.DatabaseInstanceEntry) Node(javafx.scene.Node) Timeline(javafx.animation.Timeline) VBox(javafx.scene.layout.VBox) Future(io.vertx.core.Future) Application(javafx.application.Application) Platform(javafx.application.Platform) StringUtil(io.mycat.util.StringUtil) TableData(io.mycat.ui.TableData) Duration(javafx.util.Duration) Stage(javafx.stage.Stage) TableView(javafx.scene.control.TableView) MonitorService(io.mycat.ui.MonitorService) Animation(javafx.animation.Animation) TableViewOuter(io.mycat.ui.TableViewOuter) GridPane(javafx.scene.layout.GridPane) DatabaseInstanceEntry(io.mycat.monitor.DatabaseInstanceEntry) DatabaseInstanceEntry(io.mycat.monitor.DatabaseInstanceEntry) TableViewOuter(io.mycat.ui.TableViewOuter) KeyFrame(javafx.animation.KeyFrame) Future(io.vertx.core.Future) TableData(io.mycat.ui.TableData)

Example 4 with DatabaseInstanceEntry

use of io.mycat.monitor.DatabaseInstanceEntry in project Mycat2 by MyCATApache.

the class MycatNativeDatasourcePool method getConnection.

@Override
public Future<NewMycatConnection> getConnection() {
    return vertxPoolConnection.getConnection().map(connection -> {
        DatabaseInstanceEntry stat = DatabaseInstanceEntry.stat(targetName);
        stat.plusCon();
        stat.plusQps();
        return new VertxMycatConnectionPool(targetName, connection, vertxPoolConnection) {

            long start;

            @Override
            public void onSend() {
                start = System.currentTimeMillis();
            }

            @Override
            public void onRev() {
                long end = System.currentTimeMillis();
                InstanceMonitor.plusPrt(end - start);
            }

            @Override
            public Future<Void> close() {
                stat.decCon();
                return super.close();
            }

            @Override
            public Observable<VectorSchemaRoot> prepareQuery(String sql, List<Object> params, BufferAllocator allocator) {
                return Observable.create(emitter -> {
                    JdbcDatasourcePoolImpl jdbcDatasourcePool = new JdbcDatasourcePoolImpl(targetName);
                    Future<NewMycatConnection> connectionFuture = jdbcDatasourcePool.getConnection();
                    connectionFuture.onSuccess(event -> {
                        Observable<VectorSchemaRoot> observable = event.prepareQuery(sql, params, allocator);
                        observable.subscribe(vectorSchemaRoot -> emitter.onNext(vectorSchemaRoot), throwable -> emitter.onError(throwable), () -> emitter.onComplete());
                    });
                    connectionFuture.onFailure(event -> emitter.onError(event));
                });
            }

            @Override
            public Future<List<Object>> call(String sql) {
                JdbcDatasourcePoolImpl jdbcDatasourcePool = new JdbcDatasourcePoolImpl(targetName);
                Future<NewMycatConnection> connectionFuture = jdbcDatasourcePool.getConnection();
                return connectionFuture.flatMap(connection -> connection.call(sql));
            }
        };
    });
}
Also used : DatabaseInstanceEntry(io.mycat.monitor.DatabaseInstanceEntry) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) NewMycatConnection(io.mycat.newquery.NewMycatConnection) JdbcDatasourcePoolImpl(io.mycat.commands.JdbcDatasourcePoolImpl) List(java.util.List) BufferAllocator(org.apache.arrow.memory.BufferAllocator)

Aggregations

DatabaseInstanceEntry (io.mycat.monitor.DatabaseInstanceEntry)4 NewMycatConnection (io.mycat.newquery.NewMycatConnection)2 Future (io.vertx.core.Future)2 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)1 JdbcDatasourcePoolImpl (io.mycat.commands.JdbcDatasourcePoolImpl)1 DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)1 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)1 ThreadMycatConnectionImplWrapper (io.mycat.monitor.ThreadMycatConnectionImplWrapper)1 NewMycatConnectionImpl (io.mycat.newquery.NewMycatConnectionImpl)1 NewVertxConnectionImpl (io.mycat.newquery.NewVertxConnectionImpl)1 MonitorService (io.mycat.ui.MonitorService)1 TableData (io.mycat.ui.TableData)1 TableViewOuter (io.mycat.ui.TableViewOuter)1 StringUtil (io.mycat.util.StringUtil)1 Handler (io.vertx.core.Handler)1 Connection (java.sql.Connection)1 java.util (java.util)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Animation (javafx.animation.Animation)1