Search in sources :

Example 21 with DatasourceConfig

use of io.mycat.config.DatasourceConfig in project Mycat2 by MyCATApache.

the class MycatMySQLManagerImpl method getWriteableConnectionMap.

@Override
@SneakyThrows
public Map<String, java.sql.Connection> getWriteableConnectionMap() {
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    Map<String, JdbcDataSource> datasourceInfo = jdbcConnectionManager.getDatasourceInfo();
    HashMap<String, Connection> map = new HashMap<>();
    for (String string : datasourceInfo.keySet()) {
        // /////////////////////////////////////
        JdbcDataSource jdbcDataSource = datasourceInfo.get(string);
        DatasourceConfig config = jdbcDataSource.getConfig();
        if (jdbcDataSource.isMySQLType()) {
            if (Optional.ofNullable(config.getInstanceType()).map(i -> InstanceType.valueOf(i.toUpperCase())).orElse(InstanceType.READ_WRITE).isWriteType()) {
                Connection connection = jdbcDataSource.getDataSource().getConnection();
                if (connection.isReadOnly()) {
                    JdbcUtils.close(connection);
                    continue;
                }
                map.put(string, connection);
            }
        }
    }
    return map;
}
Also used : DatasourceConfig(io.mycat.config.DatasourceConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) Connection(java.sql.Connection) NewMycatConnection(io.mycat.newquery.NewMycatConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SneakyThrows(lombok.SneakyThrows)

Example 22 with DatasourceConfig

use of io.mycat.config.DatasourceConfig in project Mycat2 by MyCATApache.

the class MycatMySQLManagerImpl method createNativeDatasourcePool.

@NotNull
public static MycatDatasourcePool createNativeDatasourcePool(DatasourceConfig datasource, String targetName) {
    ConnectionUrlParser connectionUrlParser = ConnectionUrlParser.parseConnectionString(datasource.getUrl());
    HostInfo hostInfo = connectionUrlParser.getHosts().get(0);
    int port = hostInfo.getPort();
    String host = hostInfo.getHost();
    String username = datasource.getUser();
    String password = datasource.getPassword();
    VertxPoolConnectionImpl.Config config = new VertxPoolConnectionImpl.Config();
    config.setHost(host);
    config.setPassword(password);
    config.setUsername(username);
    config.setPort(port);
    config.setDatabase(connectionUrlParser.getPath());
    config.setRetry(datasource.getMaxRetryCount());
    config.setTimer(datasource.getIdleTimeout());
    config.setClientDeprecateEof(NewMycatConnectionConfig.CLIENT_DEPRECATE_EOF);
    Vertx vertx = MetaClusterCurrent.wrapper(Vertx.class);
    VertxPoolConnectionImpl vertxConnectionPool = new VertxPoolConnectionImpl(config, vertx);
    return new MycatNativeDatasourcePool(vertxConnectionPool, targetName);
}
Also used : VertxPoolConnectionImpl(io.mycat.mysqlclient.VertxPoolConnectionImpl) MycatNativeDatasourcePool(io.mycat.mysqlclient.MycatNativeDatasourcePool) NewMycatConnectionConfig(io.mycat.newquery.NewMycatConnectionConfig) DatasourceConfig(io.mycat.config.DatasourceConfig) Vertx(io.vertx.core.Vertx) ConnectionUrlParser(com.mysql.cj.conf.ConnectionUrlParser) HostInfo(com.mysql.cj.conf.HostInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with DatasourceConfig

use of io.mycat.config.DatasourceConfig in project Mycat2 by MyCATApache.

the class MigrateUtil method read.

@SneakyThrows
public static Flowable<Object[]> read(MigrateUtil.MigrateJdbcInput migrateJdbcInput, Partition backend) {
    MycatRouterConfig routerConfig = MetaClusterCurrent.wrapper(MycatRouterConfig.class);
    ReplicaSelectorManager replicaSelectorRuntime = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
    String targetName = backend.getTargetName();
    String tableName = backend.getTable();
    String schemaName = backend.getSchema();
    String datasourceName = replicaSelectorRuntime.getDatasourceNameByReplicaName(targetName, true, null);
    List<DatasourceConfig> datasources = routerConfig.getDatasources();
    DatasourceConfig datasourceConfig = datasources.stream().filter(i -> i.getName().equals(datasourceName)).findFirst().orElseThrow((Supplier<Throwable>) () -> {
        MycatException mycatException = new MycatException("can not found datasource " + datasourceName);
        LOGGER.error("", mycatException);
        return mycatException;
    });
    return read(migrateJdbcInput, tableName, schemaName, datasourceConfig.getUrl(), datasourceConfig.getUser(), datasourceConfig.getPassword());
}
Also used : ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) DatasourceConfig(io.mycat.config.DatasourceConfig) MycatException(io.mycat.MycatException) MycatRouterConfig(io.mycat.config.MycatRouterConfig) ToString(groovy.transform.ToString) SneakyThrows(lombok.SneakyThrows)

Example 24 with DatasourceConfig

use of io.mycat.config.DatasourceConfig in project Mycat2 by MyCATApache.

the class StatisticCenterTest method init.

@BeforeClass
public static void init() throws Exception {
    HashMap<Class, Object> context = new HashMap<>();
    context.put(Vertx.class, Vertx.vertx());
    context.put(ServerConfig.class, new ServerConfig());
    context.put(DrdsSqlCompiler.class, new DrdsSqlCompiler(new DrdsConst() {

        @Override
        public NameMap<SchemaHandler> schemas() {
            return new NameMap<>();
        }
    }));
    MetaClusterCurrent.register(context);
    String customerDatasourceProvider = DruidDatasourceProvider.class.getName();
    DatasourceConfig datasourceConfig = new DatasourceConfig();
    datasourceConfig.setDbType("mysql");
    datasourceConfig.setUser("root");
    datasourceConfig.setPassword("123456");
    datasourceConfig.setName("prototypeDs");
    datasourceConfig.setUrl("jdbc:mysql://localhost:3306/mysql");
    Map<String, DatasourceConfig> datasources = Maps.of("prototypeDs", datasourceConfig);
    ClusterConfig clusterConfig = new ClusterConfig();
    clusterConfig.setName("prototype");
    clusterConfig.setMasters(Arrays.asList("prototypeDs"));
    Map<String, ClusterConfig> clusterConfigs = Maps.of("prototype", clusterConfig);
    LinkedList<Runnable> runnables = new LinkedList<>();
    ReplicaSelectorManager manager = ReplicaSelectorRuntime.create(new ArrayList<>(clusterConfigs.values()), datasources, new LoadBalanceManager(), name -> 0, (command, initialDelay, period, unit) -> {
        runnables.add(command);
        return () -> {
        };
    });
    context.put(ReplicaSelectorManager.class, manager);
    context.put(JdbcConnectionManager.class, jdbcManager = new JdbcConnectionManager(DruidDatasourceProvider.class.getName(), datasources));
    MetaClusterCurrent.register(context);
    statisticCenter.init();
}
Also used : SchemaHandler(io.mycat.calcite.table.SchemaHandler) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) LoadBalanceManager(io.mycat.plug.loadBalance.LoadBalanceManager) DruidDatasourceProvider(io.mycat.datasource.jdbc.DruidDatasourceProvider) ServerConfig(io.mycat.config.ServerConfig) DatasourceConfig(io.mycat.config.DatasourceConfig) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ClusterConfig(io.mycat.config.ClusterConfig)

Example 25 with DatasourceConfig

use of io.mycat.config.DatasourceConfig in project Mycat2 by MyCATApache.

the class DatasourcePoolTest method main.

@SneakyThrows
public static void main(String[] args) {
    DatasourceConfig datasourceConfig = new DatasourceConfig();
    datasourceConfig.setDbType("mysql");
    datasourceConfig.setUser("root");
    datasourceConfig.setPassword("123456");
    datasourceConfig.setName("prototypeDs");
    datasourceConfig.setUrl("jdbc:mysql://localhost:3307/mysql");
    datasourceConfig.setMaxCon(10);
    Map<String, DatasourceConfig> datasources = Maps.of("prototypeDs", datasourceConfig);
    MetaClusterCurrent.register(JdbcConnectionManager.class, new JdbcConnectionManager(datasources, new DruidDatasourceProvider()));
    MetaClusterCurrent.register(IOExecutor.class, IOExecutor.DEFAULT);
    MycatDatasourcePool jdbcDs = new JdbcDatasourcePoolImpl("prototypeDs");
    NewMycatConnection newMycatConnection = jdbcDs.getConnection().toCompletionStage().toCompletableFuture().get();
    Observable<VectorSchemaRoot> observable = newMycatConnection.prepareQuery("select 1", Collections.emptyList(), new RootAllocator());
    List<VectorSchemaRoot> vectorSchemaRoots = observable.toList().blockingGet();
    System.out.println();
    // ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    MycatDatasourcePool nativeDs = new VertxMySQLDatasourcePoolImpl(datasourceConfig, "prototypeDs");
    MycatRelDataType sqlMycatRelType = getSqlMycatRelDataType();
    TestResult testResult = getTestResult(jdbcDs);
    TestResult testResult2 = getTestResult(nativeDs);
    // List<String> collect = newMycatConnection.prepareQuery(querySql, Collections.emptyList()).toList().blockingGet().stream().map(i -> i.toString()).collect(Collectors.toList());
    MycatRowMetaData mycatRowMetaData1 = testResult.queryResult.getMycatRowMetaData();
    MycatRowMetaData mycatRowMetaData2 = testResult2.queryResult.getMycatRowMetaData();
    MycatRelDataType mycatRelDataType1 = mycatRowMetaData1.getMycatRelDataType();
    MycatRelDataType mycatRelDataType2 = mycatRowMetaData2.getMycatRelDataType();
    System.out.println(mycatRelDataType1);
    System.out.println(mycatRelDataType2);
    System.out.println("=====================================================================================================");
    boolean equals = mycatRelDataType1.equals(mycatRelDataType2);
    // Assert.assertTrue(equals);
    equals = mycatRelDataType1.equals(sqlMycatRelType);
    System.out.println(sqlMycatRelType);
    System.out.println(mycatRelDataType1);
    observable = newMycatConnection.prepareQuery("select * from testSchema.testColumnTable", Collections.emptyList(), new RootAllocator());
    vectorSchemaRoots = observable.toList().blockingGet();
    System.out.println();
}
Also used : VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) MycatDatasourcePool(io.mycat.commands.MycatDatasourcePool) NewMycatConnection(io.mycat.newquery.NewMycatConnection) MycatRelDataType(io.mycat.beans.mycat.MycatRelDataType) ToString(lombok.ToString) DruidDatasourceProvider(io.mycat.datasource.jdbc.DruidDatasourceProvider) DatasourceConfig(io.mycat.config.DatasourceConfig) JdbcDatasourcePoolImpl(io.mycat.commands.JdbcDatasourcePoolImpl) VertxMySQLDatasourcePoolImpl(io.mycat.commands.VertxMySQLDatasourcePoolImpl) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) SneakyThrows(lombok.SneakyThrows)

Aggregations

DatasourceConfig (io.mycat.config.DatasourceConfig)44 ClusterConfig (io.mycat.config.ClusterConfig)26 LoadBalanceManager (io.mycat.plug.loadBalance.LoadBalanceManager)24 TimerConfig (io.mycat.config.TimerConfig)22 Test (org.junit.Test)22 ConfigReporter (io.mycat.ConfigReporter)16 Consumer (java.util.function.Consumer)8 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)7 CreateClusterHint (io.mycat.hint.CreateClusterHint)6 CreateDataSourceHint (io.mycat.hint.CreateDataSourceHint)6 Connection (java.sql.Connection)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 SneakyThrows (lombok.SneakyThrows)6 HeartBeatStrategy (io.mycat.replica.heartbeat.HeartBeatStrategy)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 ConnectionUrlParser (com.mysql.cj.conf.ConnectionUrlParser)3 HostInfo (com.mysql.cj.conf.HostInfo)3 MycatException (io.mycat.MycatException)3 SchemaHandler (io.mycat.calcite.table.SchemaHandler)3 DruidDatasourceProvider (io.mycat.datasource.jdbc.DruidDatasourceProvider)3