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