Search in sources :

Example 31 with ClusterConfig

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

the class SingleTest 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.SINGLE_NODE.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());
    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

ClusterConfig (io.mycat.config.ClusterConfig)31 DatasourceConfig (io.mycat.config.DatasourceConfig)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)7 CreateClusterHint (io.mycat.hint.CreateClusterHint)6 CreateDataSourceHint (io.mycat.hint.CreateDataSourceHint)6 HeartBeatStrategy (io.mycat.replica.heartbeat.HeartBeatStrategy)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)3 SchemaHandler (io.mycat.calcite.table.SchemaHandler)2 ServerConfig (io.mycat.config.ServerConfig)2 DruidDatasourceProvider (io.mycat.datasource.jdbc.DruidDatasourceProvider)2 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)2 NameMap (io.mycat.util.NameMap)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2