Search in sources :

Example 11 with LoadBalanceManager

use of io.mycat.plug.loadBalance.LoadBalanceManager in project Mycat2 by MyCATApache.

the class SingleTest method testSlaveBroken.

@Test
public void testSlaveBroken() {
    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 () -> {
        };
    });
    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.start();
    manager.putHeartFlow("c0", "dsw1", checkMasterSlave());
    manager.putHeartFlow("c0", "dsw2", checkMasterSlave());
    manager.putHeartFlow("c0", "dsr1", makeBroken());
    for (Runnable runnable : runnables) {
        runnable.run();
    }
    for (Runnable runnable : runnables) {
        runnable.run();
    }
    for (Runnable runnable : runnables) {
        runnable.run();
    }
    // 测试已经切换
    Set<String> balanceTargets = new HashSet<>();
    test(() -> {
        balanceTargets.add(manager.getDatasourceNameByReplicaName("c0", false, null));
        return false;
    });
    Assert.assertFalse(balanceTargets.contains("dsr1"));
    Set<String> masterTargets = new HashSet<>();
    test(() -> {
        masterTargets.add(manager.getDatasourceNameByReplicaName("c0", true, null));
        return false;
    });
    Assert.assertEquals(1, masterTargets.size());
    Assert.assertTrue(masterTargets.contains("dsw1"));
    manager.putHeartFlow("c0", "dsr1", checkMasterSlave());
    for (Runnable runnable : runnables) {
        runnable.run();
    }
    PhysicsInstance dsr1 = manager.getPhysicsInstanceByName("dsr1");
    Assert.assertTrue(dsr1.asSelectRead());
    Assert.assertTrue(dsr1.isAlive());
}
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 12 with LoadBalanceManager

use of io.mycat.plug.loadBalance.LoadBalanceManager 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 13 with LoadBalanceManager

use of io.mycat.plug.loadBalance.LoadBalanceManager in project Mycat2 by MyCATApache.

the class MycatRouterConfigOps method getReplicaSelectorManager.

private Resource<ReplicaSelectorManager> getReplicaSelectorManager(UpdateSet<ClusterConfig> clusterConfigUpdateSet, UpdateSet<DatasourceConfig> datasourceConfigUpdateSet, Resource<JdbcConnectionManager> jdbcConnectionManagerResource) {
    boolean hasChanged = !clusterConfigUpdateSet.isEmpty() || !datasourceConfigUpdateSet.isEmpty();
    if (!hasChanged && MetaClusterCurrent.exist(ReplicaSelectorManager.class)) {
        return Resource.of(MetaClusterCurrent.wrapper(ReplicaSelectorManager.class), true);
    }
    LoadBalanceManager loadBalanceManager = MetaClusterCurrent.wrapper(LoadBalanceManager.class);
    Map<String, DatasourceConfig> datasourceConfigMap = datasourceConfigUpdateSet.getTarget().stream().collect(Collectors.toMap(k -> k.getName(), v -> v));
    ArrayList<ClusterConfig> clusterConfigs = new ArrayList<>(clusterConfigUpdateSet.getTarget());
    ReplicaSelectorManager replicaSelector = new ReplicaSelectorRuntime(clusterConfigs, datasourceConfigMap, loadBalanceManager, name -> {
        try {
            MySQLManager manager = MetaClusterCurrent.wrapper(MySQLManager.class);
            return manager.getSessionCount(name);
        } catch (Exception e) {
            LOGGER.error("", e);
            return 0;
        }
    }, (command, initialDelay, period, unit) -> {
        ScheduledFuture<?> scheduled = ScheduleUtil.getTimer().scheduleAtFixedRate(command, initialDelay, period, unit);
        return () -> {
            try {
                if (scheduled != null && (!scheduled.isDone() || !scheduled.isCancelled())) {
                    scheduled.cancel(true);
                }
            } catch (Throwable throwable) {
                LOGGER.error("", throwable);
            }
        };
    });
    JdbcConnectionManager jdbcConnectionManager = jdbcConnectionManagerResource.get();
    replicaSelector = new MonitorReplicaSelectorManager(replicaSelector);
    jdbcConnectionManager.register(replicaSelector);
    if (!replicaSelector.getConfig().equals(clusterConfigUpdateSet.getTargetAsList())) {
        throw new UnsupportedOperationException();
    }
    return Resource.of(replicaSelector, false);
}
Also used : ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) MonitorReplicaSelectorManager(io.mycat.monitor.MonitorReplicaSelectorManager) TransactionType(io.mycat.beans.mycat.TransactionType) QueryPlanner(io.mycat.calcite.spm.QueryPlanner) MycatSQLUtils(com.alibaba.druid.sql.MycatSQLUtils) ScheduledFuture(java.util.concurrent.ScheduledFuture) io.mycat(io.mycat) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) DruidDatasourceProvider(io.mycat.datasource.jdbc.DruidDatasourceProvider) LoggerFactory(org.slf4j.LoggerFactory) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) DbPlanManagerPersistorImpl(io.mycat.calcite.spm.DbPlanManagerPersistorImpl) ReplicaSelectorRuntime(io.mycat.replica.ReplicaSelectorRuntime) KV(io.mycat.sqlhandler.config.KV) MySQLManager(cn.mycat.vertx.xa.MySQLManager) JsonUtil(io.mycat.util.JsonUtil) MemPlanCache(io.mycat.calcite.spm.MemPlanCache) XaLogImpl(cn.mycat.vertx.xa.impl.XaLogImpl) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) MycatMySQLManagerImpl(io.mycat.commands.MycatMySQLManagerImpl) Collectors(java.util.stream.Collectors) ReplicaType(io.mycat.replica.ReplicaType) MySqlTableIndex(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex) SequenceGenerator(io.mycat.plug.sequence.SequenceGenerator) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NotNull(org.jetbrains.annotations.NotNull) Json(io.vertx.core.json.Json) java.util(java.util) AuthenticatorImpl(io.mycat.proxy.session.AuthenticatorImpl) Getter(lombok.Getter) UpdatePlanCache(io.mycat.calcite.spm.UpdatePlanCache) XaLog(cn.mycat.vertx.xa.XaLog) UpdateSet(io.mycat.sqlhandler.config.UpdateSet) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) SqlResultSetService(io.mycat.commands.SqlResultSetService) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) MySqlPrimaryKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey) MonitorReplicaSelectorManager(io.mycat.monitor.MonitorReplicaSelectorManager) SchemaHandler(io.mycat.calcite.table.SchemaHandler) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SQLIndexDefinition(com.alibaba.druid.sql.ast.SQLIndexDefinition) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) DualCustomTableHandler(io.mycat.calcite.table.DualCustomTableHandler) LocalXaMemoryRepositoryImpl(cn.mycat.vertx.xa.impl.LocalXaMemoryRepositoryImpl) PrototypeService(io.mycat.prototypeserver.mysql.PrototypeService) StatisticCenter(io.mycat.statistic.StatisticCenter) ReplicaSwitchType(io.mycat.replica.ReplicaSwitchType) LoadBalanceManager(io.mycat.plug.loadBalance.LoadBalanceManager) NameMap(io.mycat.util.NameMap) StorageManager(io.mycat.sqlhandler.config.StorageManager) LoadBalanceManager(io.mycat.plug.loadBalance.LoadBalanceManager) MonitorReplicaSelectorManager(io.mycat.monitor.MonitorReplicaSelectorManager) MySQLManager(cn.mycat.vertx.xa.MySQLManager) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ReplicaSelectorRuntime(io.mycat.replica.ReplicaSelectorRuntime)

Example 14 with LoadBalanceManager

use of io.mycat.plug.loadBalance.LoadBalanceManager in project Mycat2 by MyCATApache.

the class SpmTest 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(IOExecutor.class, IOExecutor.DEFAULT);
    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);
}
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) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ClusterConfig(io.mycat.config.ClusterConfig) BeforeClass(org.junit.BeforeClass)

Example 15 with LoadBalanceManager

use of io.mycat.plug.loadBalance.LoadBalanceManager 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)

Aggregations

LoadBalanceManager (io.mycat.plug.loadBalance.LoadBalanceManager)25 ClusterConfig (io.mycat.config.ClusterConfig)24 DatasourceConfig (io.mycat.config.DatasourceConfig)24 TimerConfig (io.mycat.config.TimerConfig)22 Test (org.junit.Test)22 ConfigReporter (io.mycat.ConfigReporter)16 CreateClusterHint (io.mycat.hint.CreateClusterHint)6 CreateDataSourceHint (io.mycat.hint.CreateDataSourceHint)6 HeartBeatStrategy (io.mycat.replica.heartbeat.HeartBeatStrategy)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Consumer (java.util.function.Consumer)5 SchemaHandler (io.mycat.calcite.table.SchemaHandler)3 DruidDatasourceProvider (io.mycat.datasource.jdbc.DruidDatasourceProvider)3 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)3 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)3 NameMap (io.mycat.util.NameMap)3 ServerConfig (io.mycat.config.ServerConfig)2 MySQLManager (cn.mycat.vertx.xa.MySQLManager)1 XaLog (cn.mycat.vertx.xa.XaLog)1 LocalXaMemoryRepositoryImpl (cn.mycat.vertx.xa.impl.LocalXaMemoryRepositoryImpl)1