Search in sources :

Example 26 with DefaultConnection

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));
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) ShardingTable(io.mycat.calcite.table.ShardingTable) NameMap(io.mycat.util.NameMap) DrdsTest(io.mycat.drdsrunner.DrdsTest) Disabled(org.junit.jupiter.api.Disabled)

Example 27 with DefaultConnection

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;
}
Also used : NormalTableConfig(io.mycat.config.NormalTableConfig) NormalBackEndTableInfoConfig(io.mycat.config.NormalBackEndTableInfoConfig) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 28 with DefaultConnection

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);
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData)

Example 29 with DefaultConnection

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);
// }
// }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)

Example 30 with DefaultConnection

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());
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) MemPlanCache(io.mycat.calcite.spm.MemPlanCache) PlanResultSet(io.mycat.calcite.spm.PlanResultSet) Baseline(io.mycat.calcite.spm.Baseline)

Aggregations

DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)53 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)39 SneakyThrows (lombok.SneakyThrows)23 Connection (java.sql.Connection)20 MetadataManager (io.mycat.MetadataManager)6 java.util (java.util)6 SQLException (java.sql.SQLException)5 Statement (java.sql.Statement)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Collectors (java.util.stream.Collectors)5 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)4 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)4 CopyMycatRowMetaData (io.mycat.beans.mycat.CopyMycatRowMetaData)4 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)4 NameMap (io.mycat.util.NameMap)4 NotNull (org.jetbrains.annotations.NotNull)4 SQLUtils (com.alibaba.druid.sql.SQLUtils)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)3