Search in sources :

Example 31 with DatasourceConfig

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

the class PrototypeHandlerImpl method onJdbc.

private Optional<List<Object[]>> onJdbc(String statement) {
    String datasourceDs = null;
    if (MetaClusterCurrent.exist(ReplicaSelectorManager.class)) {
        ReplicaSelectorManager replicaSelectorManager = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
        datasourceDs = replicaSelectorManager.getDatasourceNameByReplicaName(MetadataManager.getPrototype(), true, null);
    } else {
        datasourceDs = "prototypeDs";
    }
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    Map<String, JdbcDataSource> datasourceInfo = jdbcConnectionManager.getDatasourceInfo();
    if (datasourceInfo.containsKey(datasourceDs)) {
        datasourceDs = null;
    } else {
        List<DatasourceConfig> configAsList = jdbcConnectionManager.getConfigAsList();
        if (!configAsList.isEmpty()) {
            datasourceDs = configAsList.get(0).getName();
        } else {
            datasourceDs = null;
        }
    }
    if (datasourceDs == null) {
        datasourceDs = datasourceInfo.values().stream().filter(i -> i.isMySQLType()).map(i -> i.getName()).findFirst().orElse(null);
    }
    if (datasourceDs == null) {
        return Optional.empty();
    }
    try (DefaultConnection connection = jdbcConnectionManager.getConnection(datasourceDs)) {
        Connection rawConnection = connection.getRawConnection();
        Statement jdbcStatement1 = rawConnection.createStatement();
        ResultSet resultSet = jdbcStatement1.executeQuery(statement);
        int columnCount = resultSet.getMetaData().getColumnCount();
        List<Object[]> res = new ArrayList<>();
        while (resultSet.next()) {
            Object[] objects = new Object[columnCount];
            for (int i = 0; i < columnCount; i++) {
                objects[i] = resultSet.getObject(i + 1);
            }
            res.add(objects);
        }
        return Optional.of(res);
    } catch (Exception e) {
        LOGGER.warn("", e);
    }
    return Optional.empty();
}
Also used : ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) MetadataManager(io.mycat.MetadataManager) java.util(java.util) Connection(java.sql.Connection) TableHandler(io.mycat.TableHandler) MetaClusterCurrent(io.mycat.MetaClusterCurrent) LoggerFactory(org.slf4j.LoggerFactory) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Supplier(java.util.function.Supplier) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) ResultSet(java.sql.ResultSet) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) DatasourceConfig(io.mycat.config.DatasourceConfig) com.alibaba.druid.sql.dialect.mysql.ast.statement(com.alibaba.druid.sql.dialect.mysql.ast.statement) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Collectors(java.util.stream.Collectors) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) Statement(java.sql.Statement) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr) MysqlVariableService(io.mycat.MysqlVariableService) com.alibaba.druid.sql.ast.statement(com.alibaba.druid.sql.ast.statement) Statement(java.sql.Statement) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) DatasourceConfig(io.mycat.config.DatasourceConfig) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) ResultSet(java.sql.ResultSet) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)

Example 32 with DatasourceConfig

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

the class DbStorageManagerImpl method readConfig.

@SneakyThrows
public static Config readConfig(DatasourceConfig datasourceConfig) {
    Config config = new Config();
    try (Ds ds = Ds.create(datasourceConfig);
        Connection rawConnection = ds.getConnection()) {
        rawConnection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        rawConnection.setAutoCommit(false);
        List<Map<String, Object>> maps = JdbcUtils.executeQuery(rawConnection, "select `key`,`secondKey`,`value`,`version` from mycat.config where `deleted` = 0 and `version` in ( select max(`version`) from mycat.config  group by `key`)  ", Collections.emptyList());
        for (Map<String, Object> map : maps) {
            String key = (String) Objects.toString(map.get("key"));
            String key2 = (String) Objects.toString(map.get("secondKey"));
            String value = (String) Objects.toString(map.get("value"));
            config.version = Long.parseLong((String) Objects.toString(map.get("version")));
            Map<String, String> stringStringMap = config.config.computeIfAbsent(key, s -> new HashMap<>());
            stringStringMap.put(key2, value);
        }
    }
    return config;
}
Also used : DatasourceConfig(io.mycat.config.DatasourceConfig) Connection(java.sql.Connection) KVObject(io.mycat.config.KVObject) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SneakyThrows(lombok.SneakyThrows)

Example 33 with DatasourceConfig

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

the class StdStorageManagerImpl method getPrototypeDatasourceConfig.

@Nullable
public static Optional<DatasourceConfig> getPrototypeDatasourceConfig(StorageManager fileStorageManager) {
    KV<ClusterConfig> clusterConfigKV = fileStorageManager.get(ClusterConfig.class);
    KV<DatasourceConfig> datasourceConfigKV = fileStorageManager.get(DatasourceConfig.class);
    Optional<ClusterConfig> prototypeOptional = clusterConfigKV.get("prototype");
    Optional<DatasourceConfig> datasourceConfigOptional = prototypeOptional.flatMap(clusterConfig -> {
        List<String> masters = Optional.ofNullable(clusterConfig.getMasters()).orElse(Collections.emptyList());
        List<String> replicas = Optional.ofNullable(clusterConfig.getReplicas()).orElse(Collections.emptyList());
        List<String> strings = new ArrayList<>();
        strings.addAll(masters);
        strings.addAll(replicas);
        return strings.stream().map(i -> datasourceConfigKV.get(i)).filter(i -> i != null).findFirst();
    }).orElse(Optional.ofNullable(datasourceConfigKV.get("prototype")).orElse(datasourceConfigKV.get("prototypeDs")));
    DatasourceConfig configPrototypeDs = datasourceConfigOptional.orElse(null);
    if (configPrototypeDs == null) {
        List<DatasourceConfig> values = datasourceConfigKV.values();
        if (values.isEmpty()) {
        // 不开启db
        } else {
            configPrototypeDs = values.get(0);
        }
    }
    return Optional.ofNullable(configPrototypeDs);
}
Also used : ClusterConfig(io.mycat.config.ClusterConfig) java.util(java.util) Logger(org.slf4j.Logger) MycatException(io.mycat.MycatException) Predicate(java.util.function.Predicate) SQLCallStatementHandler(io.mycat.sqlhandler.ddl.SQLCallStatementHandler) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) KVObject(io.mycat.config.KVObject) DatasourceConfig(io.mycat.config.DatasourceConfig) NotNull(org.jetbrains.annotations.NotNull) DatasourceConfig(io.mycat.config.DatasourceConfig) ClusterConfig(io.mycat.config.ClusterConfig) Nullable(org.jetbrains.annotations.Nullable)

Example 34 with DatasourceConfig

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

the class MGRTest method testSlaveDelay.

@Test
public void testSlaveDelay() {
    ClusterConfig clusterConfig = CreateClusterHint.createConfig("c0", Arrays.asList("dsw1", "dsw2"), Arrays.asList("dsr1"));
    TimerConfig timerConfig = new TimerConfig();
    timerConfig.setTimeUnit(TimeUnit.SECONDS.name());
    timerConfig.setPeriod(1);
    timerConfig.setInitialDelay(0);
    clusterConfig.setTimer(timerConfig);
    clusterConfig.setClusterType(ReplicaType.MGR.name());
    HashMap<String, DatasourceConfig> map = new HashMap<>();
    map.put("dsw1", CreateDataSourceHint.createConfig("dsw1", DB1));
    map.put("dsw2", CreateDataSourceHint.createConfig("dsw2", DB2));
    map.put("dsr1", CreateDataSourceHint.createConfig("dsr1", DB2));
    LinkedList<Runnable> runnables = new LinkedList<>();
    ReplicaSelectorManager manager = ReplicaSelectorRuntime.create(Arrays.asList(clusterConfig), map, new LoadBalanceManager(), name -> 0, (command, initialDelay, period, unit) -> {
        runnables.add(command);
        return () -> {
        };
    });
    MetaClusterCurrent.register(Maps.of(ConfigReporter.class, new ConfigReporter() {

        @Override
        public void reportReplica(Map<String, List<String>> state) {
            List<String> c0 = state.get("c0");
            Assert.assertEquals("[dsw2]", c0.toString());
        }
    }));
    manager.putHeartFlow("c0", "dsw1", checkMGR(true));
    manager.putHeartFlow("c0", "dsw2", checkMGR(false));
    manager.putHeartFlow("c0", "dsr1", checkMGR(false, Integer.MAX_VALUE));
    manager.start();
    for (Runnable runnable : runnables) {
        runnable.run();
    }
    {
        Set<String> balanceTargets = new HashSet<>();
        test(() -> {
            balanceTargets.add(manager.getDatasourceNameByReplicaName("c0", false, null));
            return false;
        });
        Assert.assertTrue(balanceTargets.size() <= 2);
        Assert.assertFalse(balanceTargets.contains("dsr1"));
    }
    // 延迟恢复
    {
        manager.putHeartFlow("c0", "dsr1", checkMGR(false, 0));
        for (Runnable runnable : runnables) {
            runnable.run();
        }
        Set<String> balanceTargets = new HashSet<>();
        test(() -> {
            balanceTargets.add(manager.getDatasourceNameByReplicaName("c0", false, null));
            return false;
        });
        Assert.assertTrue(balanceTargets.contains("dsr1"));
    }
}
Also used : TimerConfig(io.mycat.config.TimerConfig) LoadBalanceManager(io.mycat.plug.loadBalance.LoadBalanceManager) DatasourceConfig(io.mycat.config.DatasourceConfig) ConfigReporter(io.mycat.ConfigReporter) ClusterConfig(io.mycat.config.ClusterConfig) Test(org.junit.Test)

Example 35 with DatasourceConfig

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

the class MasterSlaveTest method test.

@Test
public void test() {
    ClusterConfig clusterConfig = CreateClusterHint.createConfig("c0", Arrays.asList("dsw1", "dsw2"), Arrays.asList("dsr1"));
    TimerConfig timerConfig = new TimerConfig();
    timerConfig.setTimeUnit(TimeUnit.SECONDS.name());
    timerConfig.setPeriod(1);
    timerConfig.setInitialDelay(0);
    clusterConfig.setTimer(timerConfig);
    clusterConfig.setClusterType(ReplicaType.MASTER_SLAVE.name());
    HashMap<String, DatasourceConfig> map = new HashMap<>();
    map.put("dsw1", CreateDataSourceHint.createConfig("dsw1", DB1));
    map.put("dsw2", CreateDataSourceHint.createConfig("dsw2", DB2));
    map.put("dsr1", CreateDataSourceHint.createConfig("dsr1", DB2));
    LinkedList<Runnable> runnables = new LinkedList<>();
    ReplicaSelectorManager manager = ReplicaSelectorRuntime.create(Arrays.asList(clusterConfig), map, new LoadBalanceManager(), name -> 0, (command, initialDelay, period, unit) -> {
        runnables.add(command);
        return () -> {
        };
    });
    manager.putHeartFlow("c0", "dsw1", checkMasterSlave());
    manager.putHeartFlow("c0", "dsw2", checkMasterSlave());
    manager.putHeartFlow("c0", "dsr1", checkMasterSlave());
    manager.start();
    for (Runnable runnable : runnables) {
        runnable.run();
    }
    for (PhysicsInstance physicsInstance : manager.getPhysicsInstances()) {
        Assert.assertTrue(physicsInstance.asSelectRead());
    }
    Set<String> balanceTargets = new HashSet<>();
    test(() -> {
        balanceTargets.add(manager.getDatasourceNameByReplicaName("c0", false, null));
        return balanceTargets.size() == 3;
    });
    Assert.assertEquals(3, balanceTargets.size());
    Set<String> masterTargets = new HashSet<>();
    test(() -> {
        masterTargets.add(manager.getDatasourceNameByReplicaName("c0", true, null));
        return false;
    });
    Assert.assertEquals(1, masterTargets.size());
}
Also used : TimerConfig(io.mycat.config.TimerConfig) LoadBalanceManager(io.mycat.plug.loadBalance.LoadBalanceManager) DatasourceConfig(io.mycat.config.DatasourceConfig) ClusterConfig(io.mycat.config.ClusterConfig) Test(org.junit.Test)

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