use of io.mycat.config.TimerConfig 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.config.TimerConfig in project Mycat2 by MyCATApache.
the class MycatSQLLogMonitorImpl method initBaseMonitor.
private void initBaseMonitor(Vertx vertx) {
{
TimerConfig instanceMonitorConfig = this.monitorConfig.getInstanceMonitor();
TimeUnit timeUnit = TimeUnit.valueOf(instanceMonitorConfig.getTimeUnit());
vertx.setTimer(timeUnit.toMillis(instanceMonitorConfig.getInitialDelay()), event -> vertx.setPeriodic(timeUnit.toMillis(instanceMonitorConfig.getPeriod()), event1 -> {
IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
ioExecutor.executeBlocking((Handler<Promise<Void>>) promise -> {
try {
instanceSnapshot = InstanceEntry.snapshot();
InstanceEntry.reset();
} finally {
promise.tryComplete();
}
});
}));
}
{
TimerConfig clusterConfig = this.monitorConfig.getClusterMonitor();
TimeUnit timeUnit = TimeUnit.valueOf(clusterConfig.getTimeUnit());
long readWriteRatioMillis = timeUnit.toMillis(clusterConfig.getPeriod());
vertx.setTimer(timeUnit.toMillis(clusterConfig.getInitialDelay()), event -> vertx.setPeriodic(readWriteRatioMillis, event12 -> {
IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
ioExecutor.executeBlocking((Handler<Promise<Void>>) promise -> {
try {
rwEntryMapSnapshot = RWEntry.snapshot();
RWEntry.reset();
} finally {
promise.tryComplete();
}
});
}));
}
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
TimerConfig databaseInstanceMonitor = this.monitorConfig.getDatabaseInstanceMonitor();
TimeUnit timeUnit = TimeUnit.valueOf(databaseInstanceMonitor.getTimeUnit());
long databaseInstanceMonitorMillis = timeUnit.toMillis(databaseInstanceMonitor.getPeriod());
vertx.setTimer(timeUnit.toMillis(databaseInstanceMonitor.getInitialDelay()), event -> vertx.setPeriodic(databaseInstanceMonitorMillis, event12 -> {
IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
ioExecutor.executeBlocking((Handler<Promise<Void>>) promise -> {
try {
databaseInstanceMapSnapshot = DatabaseInstanceEntry.snapshot();
DatabaseInstanceEntry.reset();
} finally {
promise.tryComplete();
}
});
}));
}
}
use of io.mycat.config.TimerConfig 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"));
}
}
use of io.mycat.config.TimerConfig 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());
}
use of io.mycat.config.TimerConfig in project Mycat2 by MyCATApache.
the class MasterSlaveTest 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);
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", checkMasterSlave());
manager.putHeartFlow("c0", "dsw2", checkMasterSlave());
manager.putHeartFlow("c0", "dsr1", makeBroken());
manager.start();
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());
}
Aggregations