Search in sources :

Example 1 with MySQLDatasource

use of io.mycat.beans.MySQLDatasource in project Mycat2 by MyCATApache.

the class MySQLDatasourcePool method innerCreateCon.

private Future<MySQLClientSession> innerCreateCon() {
    PromiseInternal<MySQLClientSession> promise = VertxUtil.newPromise();
    new BackendConCreateHandler(this, promise);
    return promise.flatMap(session -> {
        synchronized (allSessions) {
            allSessions.add(session);
        }
        MySQLDatasource datasource = session.getDatasource();
        String sql = datasource.getInitSqlForProxy();
        if (!StringUtil.isEmpty(sql)) {
            PromiseInternal<MySQLClientSession> promiseInternal = VertxUtil.newPromise();
            ResultSetHandler.DEFAULT.request(session, COM_QUERY, sql.getBytes(StandardCharsets.UTF_8), promiseInternal);
            return promiseInternal.onFailure(event -> {
                session.close(false, "initSql fail");
            });
        } else {
            return Future.succeededFuture(session);
        }
    });
}
Also used : BackendConCreateHandler(io.mycat.proxy.handler.backend.BackendConCreateHandler) MySQLDatasource(io.mycat.beans.MySQLDatasource) MySQLClientSession(io.mycat.proxy.session.MySQLClientSession)

Example 2 with MySQLDatasource

use of io.mycat.beans.MySQLDatasource in project Mycat2 by MyCATApache.

the class NativeMycatServer method showNativeDataSources.

@Override
public RowBaseIterator showNativeDataSources() {
    MycatRouterConfig mycatRouterConfig = MetaClusterCurrent.wrapper(MycatRouterConfig.class);
    Map<String, DatasourceConfig> datasourceConfigMap = mycatRouterConfig.getDatasources().stream().collect(Collectors.toMap(k -> k.getName(), v -> v));
    ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
    resultSetBuilder.addColumnInfo("NAME", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("USERNAME", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("PASSWORD", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("MAX_CON", JDBCType.BIGINT);
    resultSetBuilder.addColumnInfo("MIN_CON", JDBCType.BIGINT);
    resultSetBuilder.addColumnInfo("EXIST_CON", JDBCType.BIGINT);
    resultSetBuilder.addColumnInfo("USE_CON", JDBCType.BIGINT);
    resultSetBuilder.addColumnInfo("MAX_RETRY_COUNT", JDBCType.BIGINT);
    resultSetBuilder.addColumnInfo("MAX_CONNECT_TIMEOUT", JDBCType.BIGINT);
    resultSetBuilder.addColumnInfo("DB_TYPE", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("URL", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("WEIGHT", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("INIT_SQL", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("INIT_SQL_GET_CONNECTION", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("INSTANCE_TYPE", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("IDLE_TIMEOUT", JDBCType.BIGINT);
    resultSetBuilder.addColumnInfo("DRIVER", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("TYPE", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("IS_MYSQL", JDBCType.VARCHAR);
    Map<String, Integer> map = getDatasourceMap().values().stream().collect(Collectors.toMap(k -> k.getName(), v -> v.getAllSessions().size()));
    for (MySQLDatasource value : getDatasourceMap().values()) {
        String NAME = value.getName();
        Optional<DatasourceConfig> e = Optional.ofNullable(datasourceConfigMap.get(NAME));
        String IP = value.getIp();
        int PORT = value.getPort();
        String USERNAME = value.getUsername();
        String PASSWORD = value.getPassword();
        int MAX_CON = value.getSessionLimitCount();
        int MIN_CON = value.getSessionMinCount();
        long USED_CON = map.getOrDefault(NAME, -1);
        int EXIST_CON = value.getConnectionCounter();
        int MAX_RETRY_COUNT = value.gerMaxRetry();
        long MAX_CONNECT_TIMEOUT = value.getMaxConnectTimeout();
        String DB_TYPE = "mysql";
        String URL = null;
        int WEIGHT = e.map(i -> i.getWeight()).orElse(-1);
        String INIT_SQL = value.getInitSqlForProxy();
        boolean INIT_SQL_GET_CONNECTION = false;
        ReplicaSelectorManager selectorRuntime = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
        String INSTANCE_TYPE = Optional.ofNullable(selectorRuntime.getPhysicsInstanceByName(NAME)).map(i -> i.getType().name()).orElse(e.map(i -> i.getInstanceType()).orElse(null));
        long IDLE_TIMEOUT = value.getIdleTimeout();
        // 保留属性
        String DRIVER = "native";
        String TYPE = e.map(i -> i.getType()).orElse(null);
        boolean IS_MYSQL = true;
        resultSetBuilder.addObjectRowPayload(Arrays.asList(NAME, IP, PORT, USERNAME, PASSWORD, MAX_CON, MIN_CON, EXIST_CON, USED_CON, MAX_RETRY_COUNT, MAX_CONNECT_TIMEOUT, DB_TYPE, URL, WEIGHT, INIT_SQL, INIT_SQL_GET_CONNECTION, INSTANCE_TYPE, IDLE_TIMEOUT, DRIVER, TYPE, IS_MYSQL));
    }
    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) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) MySQLDatasource(io.mycat.beans.MySQLDatasource)

Aggregations

MySQLDatasource (io.mycat.beans.MySQLDatasource)2 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)1 ResultSetBuilder (io.mycat.beans.mycat.ResultSetBuilder)1 MySQLAutoCommit (io.mycat.beans.mysql.MySQLAutoCommit)1 DefaultReactorBufferPool (io.mycat.buffer.DefaultReactorBufferPool)1 CommandDispatcher (io.mycat.command.CommandDispatcher)1 io.mycat.config (io.mycat.config)1 MySQLDatasourcePool (io.mycat.proxy.MySQLDatasourcePool)1 BackendConCreateHandler (io.mycat.proxy.handler.backend.BackendConCreateHandler)1 io.mycat.proxy.reactor (io.mycat.proxy.reactor)1 io.mycat.proxy.session (io.mycat.proxy.session)1 MySQLClientSession (io.mycat.proxy.session.MySQLClientSession)1 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 JDBCType (java.sql.JDBCType)1 Timestamp (java.sql.Timestamp)1 LocalDateTime (java.time.LocalDateTime)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1