Search in sources :

Example 6 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class MySQLLogConsumer method accept.

@Override
@SneakyThrows
public void accept(SqlEntry sqlEntry) {
    if (!init) {
        init = true;
        try {
            init();
        } catch (Exception e) {
            LOGGER.error("", e);
        }
    }
    boolean isInRuntime = MetaClusterCurrent.exist(IOExecutor.class) && MetaClusterCurrent.exist(JdbcConnectionManager.class) && MetaClusterCurrent.exist(IOExecutor.class);
    if (isInRuntime) {
        IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
        JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
        MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
        ioExecutor.executeBlocking((Handler<Promise<Void>>) event -> {
            try {
                try (DefaultConnection connection = jdbcConnectionManager.getConnection(metadataManager.getPrototype())) {
                    JdbcUtils.execute(connection.getRawConnection(), "INSERT INTO `mycat`.`sql_log` (" + "`instanceId`," + "user," + "connectionId," + "ip," + "port," + "traceId," + "hash," + "sqlType," + "`sql`," + "transactionId," + "sqlTime," + "responseTime," + "affectRow," + "result," + "externalMessage)" + "values(?,?,?,?,?," + "?,?,?,?,?," + "?,?,?,?,?)", Arrays.asList(sqlEntry.getInstanceId(), sqlEntry.getUser(), sqlEntry.getConnectionId(), sqlEntry.getIp(), sqlEntry.getPort(), sqlEntry.getTraceId(), sqlEntry.getHash(), Objects.toString(sqlEntry.getSqlType()), sqlEntry.getSql(), sqlEntry.getTransactionId(), sqlEntry.getSqlTime(), sqlEntry.getResponseTime(), sqlEntry.getAffectRow(), sqlEntry.isResult(), sqlEntry.getExternalMessage()));
                }
            } catch (Exception e) {
                LOGGER.info(" warning sql:{} , info:{}", sqlEntry.getSql(), sqlEntry);
                LOGGER.error("", e);
            } finally {
                event.tryComplete();
            }
        });
    }
}
Also used : MetadataManager(io.mycat.MetadataManager) SqlEntry(io.mycat.monitor.SqlEntry) java.util(java.util) Logger(org.slf4j.Logger) Connection(java.sql.Connection) MetaClusterCurrent(io.mycat.MetaClusterCurrent) SneakyThrows(lombok.SneakyThrows) Promise(io.vertx.core.Promise) LoggerFactory(org.slf4j.LoggerFactory) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) IOExecutor(io.mycat.IOExecutor) Consumer(java.util.function.Consumer) SQLException(java.sql.SQLException) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) JdbcUtils(com.alibaba.druid.util.JdbcUtils) Handler(io.vertx.core.Handler) Promise(io.vertx.core.Promise) MetadataManager(io.mycat.MetadataManager) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) IOExecutor(io.mycat.IOExecutor) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SQLException(java.sql.SQLException) SneakyThrows(lombok.SneakyThrows)

Example 7 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class EnumTest method testHashMM.

@Test
public void testHashMM() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " dbpartition by mod_hash(id) tbpartition by MM(traveldate) tbpartitions 2 dbpartitions 4;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"2\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"4\",\n" + "\t\t\t\t\t\"tableMethod\":\"MM(traveldate)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"hash(id)\"\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    LocalDate start = LocalDate.of(2021, 11, 8);
    List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.EQUAL, start))));
    Assert.assertEquals(2, calculate.size());
    for (Partition partition : calculate) {
        Assert.assertEquals(start.getMonthValue() % 4, (int) partition.getTableIndex());
    }
    {
        LocalDate end = LocalDate.of(2021, 11, 9);
        List<Partition> calculate2 = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.RANGE, start, end))));
        Assert.assertEquals(calculate, calculate2);
    }
    {
        LocalDate end = LocalDate.of(2021, 12, 9);
        List<Partition> calculate2 = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.RANGE, start, end))));
        HashSet<Integer> set = new HashSet<>();
        for (int i = 11; i <= 12; i++) {
            set.add(i % 4);
        }
        Assert.assertEquals(true, calculate2.stream().allMatch(i -> set.contains(i.getTableIndex())));
        System.out.println();
    }
    {
        LocalDate end = LocalDate.of(2022, 1, 9);
        List<Partition> calculate2 = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.RANGE, start, end))));
        HashSet<Integer> set = new HashSet<>();
        for (Integer integer : Arrays.asList(11, 12, 1)) {
            set.add(integer % 4);
        }
        Assert.assertEquals(true, calculate2.stream().allMatch(i -> set.contains(i.getTableIndex())));
        System.out.println();
    }
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) CustomRuleFunction(io.mycat.router.CustomRuleFunction) LocalDate(java.time.LocalDate) MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Example 8 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class EnumTest method testYYYYWEEK.

@Test
public void testYYYYWEEK() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " dbpartition by YYYYWEEK(traveldate) tbpartition by YYYYWEEK(traveldate) tbpartitions 2 dbpartitions 4;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"8\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"14\",\n" + "\t\t\t\t\t\"tableMethod\":\"YYYYWEEK(traveldate)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYWEEK(traveldate)\"\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    // LocalDate start = LocalDate.of(2021, 11, 8);
    // 
    // List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("traveldate",
    // (new RangeVariable("traveldate", RangeVariableType.EQUAL, start))));
    // Assert.assertEquals(1, calculate.size());
    // Assert.assertEquals( 7, (int) calculate.get(0).getTableIndex());
    // Assert.assertEquals( 3, (int) calculate.get(0).getDbIndex());
    // {
    // LocalDate end = LocalDate.of(2021, 11, 9);
    // 
    // List<Partition> calculate2 = shardingFuntion.calculate(Collections.singletonMap("traveldate",
    // (new RangeVariable("traveldate", RangeVariableType.RANGE, start, end))));
    // 
    // Assert.assertEquals(calculate, calculate2);
    // 
    // }
    // 
    // AutoFunction autoFunction = (AutoFunction) shardingFuntion;
    // 
    // ArrayList<Object> res = new ArrayList<>();
    // Optional<Set<LocalDate>> integers = autoFunction.enumMonthValue(12, new ToIntFunction<Object>() {
    // @Override
    // public int applyAsInt(Object value) {
    // res.add(value);
    // return ((LocalDate)value).getMonthValue();
    // }
    // }, LocalDate.of(2021, 11, 1), LocalDate.of(2022, 3, 1));
    // 
    // Assert.assertEquals("[2021-11-01, 2021-12-01, 2022-01-01, 2022-02-01]",res.toString());
    // Assert.assertEquals("[1, 2, 11, 12]",integers.get().toString());
    {
        List<Partition> calculate2 = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.RANGE, LocalDate.of(2021, 11, 1), LocalDate.of(2022, 3, 1)))));
        Assert.assertEquals(18, calculate2.size());
        System.out.println("");
    }
    System.out.println();
}
Also used : RangeVariable(io.mycat.RangeVariable) MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) CustomRuleFunction(io.mycat.router.CustomRuleFunction) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Example 9 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class HashFunctionTest method testHashIdHashUserId.

@Test
public void testHashIdHashUserId() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " dbpartition by mod_hash(id) tbpartition by mod_hash(user_id) tbpartitions 2 dbpartitions 4;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"2\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"4\",\n" + "\t\t\t\t\t\"tableMethod\":\"hash(user_id)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"hash(id)\"\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    ImmutableMap<String, RangeVariable> map = ImmutableMap.of("id", new RangeVariable("id", RangeVariableType.EQUAL, 15), "user_id", (new RangeVariable("user_id", RangeVariableType.EQUAL, 2)));
    List<Partition> insertRoute = shardingFuntion.calculate(map);
    Assert.assertEquals(1, insertRoute.size());
    List<Partition> firstColumn = shardingFuntion.calculate(ImmutableMap.of("id", (new RangeVariable("id", RangeVariableType.EQUAL, 15))));
    Assert.assertTrue(firstColumn.containsAll(insertRoute));
    List<Partition> secondColumn = shardingFuntion.calculate(ImmutableMap.of("user_id", (new RangeVariable("user_id", RangeVariableType.EQUAL, 2))));
    Assert.assertTrue(secondColumn.containsAll(insertRoute));
    String s1 = shardingFuntion.calculate(Collections.emptyMap()).toString();
    Assert.assertEquals("[{targetName='c0', schemaName='db1_0', tableName='sharding_0', index=0, dbIndex=0, tableIndex=0}, {targetName='c0', schemaName='db1_0', tableName='sharding_1', index=1, dbIndex=0, tableIndex=1}, {targetName='c0', schemaName='db1_0', tableName='sharding_2', index=2, dbIndex=0, tableIndex=2}, {targetName='c0', schemaName='db1_0', tableName='sharding_3', index=3, dbIndex=0, tableIndex=3}, {targetName='c0', schemaName='db1_1', tableName='sharding_0', index=4, dbIndex=1, tableIndex=0}, {targetName='c0', schemaName='db1_1', tableName='sharding_1', index=5, dbIndex=1, tableIndex=1}, {targetName='c0', schemaName='db1_1', tableName='sharding_2', index=6, dbIndex=1, tableIndex=2}, {targetName='c0', schemaName='db1_1', tableName='sharding_3', index=7, dbIndex=1, tableIndex=3}]", s1);
    System.out.println();
}
Also used : RangeVariable(io.mycat.RangeVariable) Partition(io.mycat.Partition) MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) CustomRuleFunction(io.mycat.router.CustomRuleFunction) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Example 10 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class HashFunctionTest method testHashIdHashId.

/**
 * https://help.aliyun.com/document_detail/71276.html
 * <p>
 * 若分库和分表都使用同一个拆分键进行HASH时,则根据拆分键的键值按总的分表数取余。
 * 例如有2个分库,每个分库4张分表,那么0库上保存分表0~3,1库上保存分表4~7。某个键值为15,那么根据该路由方式,则该键值15将被分到1库的表7上((15 % (2 * 4) =7))
 */
@Test
public void testHashIdHashId() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " dbpartition by mod_hash(id) tbpartition by mod_hash(id) tbpartitions 2 dbpartitions 4;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"2\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"4\",\n" + "\t\t\t\t\t\"tableMethod\":\"hash(id)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"hash(id)\"\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("id", (new RangeVariable("id", RangeVariableType.EQUAL, 15))));
    String s = calculate.toString();
    Assert.assertTrue(s.contains("[{targetName='c0', schemaName='db1_1', tableName='sharding_3', index=7, dbIndex=1, tableIndex=3}]"));
    String s1 = shardingFuntion.calculate(Collections.emptyMap()).toString();
    Assert.assertEquals("[{targetName='c0', schemaName='db1_0', tableName='sharding_0', index=0, dbIndex=0, tableIndex=0}, {targetName='c0', schemaName='db1_0', tableName='sharding_1', index=1, dbIndex=0, tableIndex=1}, {targetName='c0', schemaName='db1_0', tableName='sharding_2', index=2, dbIndex=0, tableIndex=2}, {targetName='c0', schemaName='db1_0', tableName='sharding_3', index=3, dbIndex=0, tableIndex=3}, {targetName='c0', schemaName='db1_1', tableName='sharding_0', index=4, dbIndex=1, tableIndex=0}, {targetName='c0', schemaName='db1_1', tableName='sharding_1', index=5, dbIndex=1, tableIndex=1}, {targetName='c0', schemaName='db1_1', tableName='sharding_2', index=6, dbIndex=1, tableIndex=2}, {targetName='c0', schemaName='db1_1', tableName='sharding_3', index=7, dbIndex=1, tableIndex=3}]", s1);
    System.out.println();
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) CustomRuleFunction(io.mycat.router.CustomRuleFunction) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Aggregations

MetadataManager (io.mycat.MetadataManager)34 ShardingTable (io.mycat.calcite.table.ShardingTable)22 Test (org.junit.Test)22 Partition (io.mycat.Partition)17 ShardingTableConfig (io.mycat.config.ShardingTableConfig)12 CustomRuleFunction (io.mycat.router.CustomRuleFunction)12 RangeVariable (io.mycat.RangeVariable)11 DrdsSqlCompiler (io.mycat.DrdsSqlCompiler)10 ValuePredicateAnalyzer (io.mycat.calcite.rewriter.ValuePredicateAnalyzer)10 java.util (java.util)10 Map (java.util.Map)10 RexNode (org.apache.calcite.rex.RexNode)10 TableHandler (io.mycat.TableHandler)9 DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)7 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)7 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 MetaClusterCurrent (io.mycat.MetaClusterCurrent)5 MycatCalciteSupport (io.mycat.calcite.MycatCalciteSupport)5 Connection (java.sql.Connection)5