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());
}
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();
}
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);
}
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);
}
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"));
}
}
Aggregations