use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class StatisticCenterTest method testSharding.
@Test
@Disabled
@Ignore
public void testSharding() throws Exception {
DrdsSqlCompiler drds = getDrds();
String schemaName = "db1";
String tableName = "sharding";
MetadataManager metadataManager = getMetadataManager();
TableHandler tableHandler = metadataManager.getTable(schemaName, tableName);
if (tableHandler == null) {
return;
}
tableHandler.createPhysicalTables();
ShardingTable table = (ShardingTable) tableHandler;
List<Partition> backends = table.getBackends();
Partition c0 = backends.get(0);
Partition c1 = backends.get(1);
try (DefaultConnection connection = jdbcManager.getConnection("prototype")) {
deleteData(connection.getRawConnection(), schemaName, tableName);
JdbcUtils.execute(connection.getRawConnection(), "insert db1.sharding(id) values(?)", Arrays.asList(1));
}
Double count = statisticCenter.computeTableRowCount(tableHandler);
Assert.assertTrue(count.equals(count));
statisticCenter.fetchTableRowCount(tableHandler);
try (DefaultConnection connection = jdbcManager.getConnection("prototype")) {
List<Map<String, Object>> maps = JdbcUtils.executeQuery(connection.getRawConnection(), "select * from mycat.analyze_table ", Arrays.asList());
Assert.assertTrue(maps.toString().contains(tableName));
}
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class PrototypeService method getDefaultNormalTable.
public Map<String, NormalTableConfig> getDefaultNormalTable(String targetName, String schemaName, Predicate<String> tableFilter) {
Set<String> tables = new HashSet<>();
Optional<JdbcConnectionManager> jdbcConnectionManagerOptional = getPrototypeConnectionManager();
if (!jdbcConnectionManagerOptional.isPresent()) {
return Collections.emptyMap();
}
JdbcConnectionManager jdbcConnectionManager = jdbcConnectionManagerOptional.get();
try (DefaultConnection connection = jdbcConnectionManager.getConnection(targetName)) {
RowBaseIterator tableIterator = connection.executeQuery("show tables from " + schemaName);
while (tableIterator.next()) {
tables.add(tableIterator.getString(0));
}
} catch (Exception e) {
LOGGER.error("", e);
return Collections.emptyMap();
}
Map<String, NormalTableConfig> res = new ConcurrentHashMap<>();
tables.stream().filter(tableFilter).parallel().forEach(tableName -> {
NormalBackEndTableInfoConfig normalBackEndTableInfoConfig = new NormalBackEndTableInfoConfig(targetName, schemaName, tableName);
try {
String createTableSQLByJDBC = getCreateTableSQLByJDBC(schemaName, tableName, Collections.singletonList(new BackendTableInfo(targetName, schemaName, tableName))).orElse(null);
if (createTableSQLByJDBC != null) {
res.put(tableName, new NormalTableConfig(createTableSQLByJDBC, normalBackEndTableInfoConfig));
} else {
// exception
}
} catch (Throwable e) {
LOGGER.warn("", e);
}
});
return res;
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class PrototypeService method getColumnInfo.
public List<SimpleColumnInfo> getColumnInfo(String sql, String schema, String table) {
try {
SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
MycatRowMetaData mycatRowMetaData = null;
if (sqlStatement instanceof MySqlCreateTableStatement) {
mycatRowMetaData = SQL2ResultSetUtil.getMycatRowMetaData((MySqlCreateTableStatement) sqlStatement);
} else {
Optional<JdbcConnectionManager> prototypeConnectionManagerOptional = getPrototypeConnectionManager();
if (!prototypeConnectionManagerOptional.isPresent())
return Collections.emptyList();
if (sqlStatement instanceof SQLCreateViewStatement) {
if (schema == null || table == null) {
schema = ((SQLCreateViewStatement) sqlStatement).getSchema();
table = ((SQLCreateViewStatement) sqlStatement).getName().getSimpleName();
}
mycatRowMetaData = SQL2ResultSetUtil.getMycatRowMetaData(prototypeConnectionManagerOptional.get(), MetadataManager.getPrototype(), schema, table);
} else if (sqlStatement instanceof SQLSelectStatement) {
JdbcConnectionManager jdbcConnectionManager = prototypeConnectionManagerOptional.get();
try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
RowBaseIterator baseIterator = connection.executeQuery(sql);
mycatRowMetaData = new CopyMycatRowMetaData(baseIterator.getMetaData());
}
}
}
return CalciteConvertors.getColumnInfo(Objects.requireNonNull(mycatRowMetaData));
} catch (Exception e) {
LOGGER.error("sql:{}", sql, e);
return getSimpleColumnInfos(schema, table);
}
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class NormalTable method dropPhysicalTables.
@Override
public void dropPhysicalTables() {
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
Partition dataNode = getDataNode();
try (DefaultConnection connection = jdbcConnectionManager.getConnection(dataNode.getTargetName())) {
connection.deleteTable(dataNode.getSchema(), dataNode.getTable());
}
// for (DataNode node :Collections.singleton(getDataNode())) {
// try (DefaultConnection connection = jdbcConnectionManager.getConnection(node.getTargetName())) {
// connection.executeUpdate(String.format(dropTemplate,node.getSchema(),node.getTable()), false);
// }
// }
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class SpmTest method testAdd.
private void testAdd(boolean fix) throws Exception {
dbPlanManagerPersistor.checkStore();
try (DefaultConnection defaultConnection = jdbcManager.getConnection("prototypeDs")) {
Connection rawConnection = defaultConnection.getRawConnection();
deleteData(rawConnection, "mycat", "spm_baseline");
deleteData(rawConnection, "mycat", "spm_plan");
MemPlanCache memPlanCache = new MemPlanCache(dbPlanManagerPersistor);
DrdsSqlWithParams n = DrdsRunnerHelper.preParse("select 1", null);
DrdsSqlWithParams c = DrdsRunnerHelper.preParse("select '1'", null);
memPlanCache.getBaseline(n);
memPlanCache.getBaseline(c);
Assert.assertFalse(hasData(rawConnection, "mycat", "spm_baseline"));
Assert.assertFalse(hasData(rawConnection, "mycat", "spm_plan"));
// saveBaselines
memPlanCache.saveBaselines();
Assert.assertTrue(hasData(rawConnection, "mycat", "spm_baseline"));
Assert.assertFalse(hasData(rawConnection, "mycat", "spm_plan"));
PlanResultSet add1 = memPlanCache.add(fix, n);
Assert.assertTrue(add1.isOk());
if (fix) {
Assert.assertTrue(null != memPlanCache.getBaseline(n).getFixPlan());
}
PlanResultSet add2 = memPlanCache.add(fix, c);
if (fix) {
Assert.assertTrue(null != memPlanCache.getBaseline(n).getFixPlan());
}
Assert.assertTrue(add2.isOk());
// saveBaselines
memPlanCache.saveBaselines();
Assert.assertTrue(hasData(rawConnection, "mycat", "spm_plan"));
// list
List<Baseline> list = memPlanCache.list();
Assert.assertEquals(2, list.size());
Assert.assertTrue(list.contains(memPlanCache.getBaseline(n)));
Assert.assertTrue(list.contains(memPlanCache.getBaseline(c)));
long planId = memPlanCache.getBaseline(c).getPlanList().stream().findFirst().get().getId();
Baseline backupBaseline = memPlanCache.getBaseline(n);
Baseline baseline = memPlanCache.getBaseline(n);
// persistPlan
memPlanCache.persistPlan(planId);
Assert.assertFalse(JdbcUtils.executeQuery(rawConnection, "select * from mycat.spm_plan where id = ?", Arrays.asList(planId)).isEmpty());
// loadPlan
memPlanCache.loadPlan(planId);
Assert.assertFalse(memPlanCache.getBaseline(c).getPlanList().isEmpty());
Assert.assertFalse(baseline.getPlanList().isEmpty());
// deletePlan
memPlanCache.deletePlan(planId);
Assert.assertTrue(JdbcUtils.executeQuery(rawConnection, "select * from mycat.spm_plan where id = ?", Arrays.asList(planId)).isEmpty());
// persistPlan
memPlanCache.persistPlan(planId);
Assert.assertFalse(JdbcUtils.executeQuery(rawConnection, "select * from mycat.spm_plan where id = ?", Arrays.asList(planId)).isEmpty());
// deleteBaseline
memPlanCache.deleteBaseline(baseline.getBaselineId());
Assert.assertTrue(JdbcUtils.executeQuery(rawConnection, "select * from mycat.spm_baseline where id = ?", Arrays.asList(baseline.getBaselineId())).isEmpty());
// persistBaseline
memPlanCache.persistBaseline(baseline.getBaselineId());
Assert.assertTrue(!JdbcUtils.executeQuery(rawConnection, "select * from mycat.spm_baseline where id = ?", Arrays.asList(baseline.getBaselineId())).isEmpty());
// deleteBaseline
memPlanCache.deleteBaseline(baseline.getBaselineId());
Assert.assertTrue(JdbcUtils.executeQuery(rawConnection, "select * from mycat.spm_baseline where id = ?", Arrays.asList(baseline.getBaselineId())).isEmpty());
if (fix) {
Assert.assertTrue(null != memPlanCache.getBaseline(baseline.getBaselineId()).getFixPlan());
Assert.assertTrue(null != memPlanCache.getBaseline(n).getFixPlan());
Assert.assertTrue(null != memPlanCache.getBaseline(c).getFixPlan());
}
// persistBaseline
memPlanCache.persistBaseline(baseline.getBaselineId());
Assert.assertTrue(!JdbcUtils.executeQuery(rawConnection, "select * from mycat.spm_baseline where id = ?", Arrays.asList(baseline.getBaselineId())).isEmpty());
// loadBaseline
memPlanCache.loadBaseline(baseline.getBaselineId());
Assert.assertFalse(baseline.getPlanList().isEmpty());
if (fix) {
Assert.assertTrue(null != memPlanCache.getBaseline(baseline.getBaselineId()).getFixPlan());
Assert.assertTrue(null != memPlanCache.getBaseline(n).getFixPlan());
Assert.assertTrue(null != memPlanCache.getBaseline(c).getFixPlan());
}
// clearPlan
memPlanCache.clearPlan(planId);
Assert.assertTrue(memPlanCache.getBaseline(c).getPlanList().isEmpty());
// clearBaseline
memPlanCache.clearBaseline(memPlanCache.getBaseline(n).getBaselineId());
Assert.assertNotEquals(backupBaseline.getBaselineId(), memPlanCache.getBaseline(n).getBaselineId());
memPlanCache.clearBaseline(memPlanCache.getBaseline(n).getBaselineId());
}
}
Aggregations