Skip to content

Commit

Permalink
Merge pull request #324 from MyCATApache/hbt2
Browse files Browse the repository at this point in the history
发布版本
  • Loading branch information
junwen12221 committed Jun 7, 2020
2 parents 8bf94d1 + c25896a commit 4436713
Show file tree
Hide file tree
Showing 34 changed files with 751 additions and 182 deletions.
14 changes: 13 additions & 1 deletion README.md
Expand Up @@ -554,12 +554,24 @@ targetName是目标名字,它可以是数据源的名字或者集群的名字



##### 规律1:与逻辑库名称路径一致的物理表

用于

1. schema上的配置默认目标
2. 在代理架构下即mycat proxy没有设置成自动返回show语句的情况(比如使用命令配置),则配置的默认命令指向的默认目标



此默认目标应该有非分片表与分片表的名字的物理库
此默认目标应该有非分片表与分片表的名字的物理库,此物理库具有提供查询逻辑表信息的作用(因为表名,库名与逻辑表一致),无需经过sql改写即可查询逻辑表的数据



##### 规律2:若有全局表,则每一个跨服务器的分片建议建立一个相同名称路径的物理表

这样的好处是涉及分片表与全局表的sql无需复杂分析,若判断sql涉及一个分片的分片表和任意多个全局表,只需改写分片表部分即可查询





Expand Down
2 changes: 1 addition & 1 deletion autohandler/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>io.mycat</groupId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
8 changes: 4 additions & 4 deletions booster/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>io.mycat</groupId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -21,17 +21,17 @@
<dependency>
<groupId>io.mycat</groupId>
<artifactId>config</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.mycat</groupId>
<artifactId>common</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.mycat</groupId>
<artifactId>plug</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions common/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>io.mycat</groupId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -15,7 +15,7 @@
<dependency>
<groupId>io.mycat</groupId>
<artifactId>config</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion config/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.mycat</groupId>
<artifactId>parent</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
10 changes: 5 additions & 5 deletions datasource/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>io.mycat</groupId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -20,22 +20,22 @@
<dependency>
<groupId>io.mycat</groupId>
<artifactId>config</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.mycat</groupId>
<artifactId>common</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.mycat</groupId>
<artifactId>plug</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.mycat</groupId>
<artifactId>replica</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
Expand Down
4 changes: 2 additions & 2 deletions example/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>io.mycat</groupId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -15,7 +15,7 @@
<dependency>
<groupId>io.mycat</groupId>
<artifactId>mycat2</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</dependency>
</dependencies>

Expand Down
Expand Up @@ -30,7 +30,7 @@ interceptors:
[{
user: {ip: '.', password: '123456', username: root},
sqls:[
{sql: 'SELECT COUNT(1) FROM db1.travelrecord',command: distributedQuery, cache: 'initialDelay = 1s,refreshInterval = 15s'}
# {sql: 'SELECT COUNT(1) FROM db1.travelrecord',command: distributedQuery, cache: 'initialDelay = 1s,refreshInterval = 15s'}
]
}]
datasource:
Expand Down Expand Up @@ -82,7 +82,7 @@ server:
maxPengdingLimit: 65535, #每个线程处理任务队列的最大长度
maxThread: 1000,
minThread: 2,
timeUnit: SECONDS,
timeUnit: MINUTES,
waitTaskTimeout: 5 #超时后将结束闲置的线程
}
#负载均衡类型 BALANCE_ALL:所有数据源参与负载均衡 BALANCE_ALL_READ:所以非master数据源参与负载均衡 BALANCE_NONE:只有master(一个)参与负载
Expand Down
10 changes: 5 additions & 5 deletions hbt/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>io.mycat</groupId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -16,22 +16,22 @@
<dependency>
<groupId>io.mycat</groupId>
<artifactId>common</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.mycat</groupId>
<artifactId>plug</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.mycat</groupId>
<artifactId>datasource</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.mycat</groupId>
<artifactId>config</artifactId>
<version>1.07-SNAPSHOT</version>
<version>1.08-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastsql</groupId>
Expand Down
27 changes: 23 additions & 4 deletions hbt/src/main/java/io/mycat/calcite/CalciteRunners.java
Expand Up @@ -19,12 +19,12 @@
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.RelShuttleImpl;
import org.apache.calcite.rel.core.TableScan;
import org.apache.calcite.rel.logical.LogicalUnion;
import org.apache.calcite.runtime.ArrayBindable;
import org.apache.calcite.sql.SqlNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.StringWriter;
import java.util.*;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
Expand All @@ -47,8 +47,8 @@ public static RelNode compile(MycatCalcitePlanner planner, RelNode relNode, bool
relNode = planner.pullUpUnion(relNode);
relNode = planner.pushDownBySQL(relNode, forUpdate);
return relNode;
}catch (Throwable e){
LOGGER.error("",e);
} catch (Throwable e) {
LOGGER.error("", e);
}
return null;
}
Expand Down Expand Up @@ -76,6 +76,18 @@ public RelNode visit(TableScan scan) {
final HepPlanner planner2 = new HepPlanner(hepProgramBuilder.build());
planner2.setRoot(relNode);
relNode = planner2.findBestExp();

//check
relNode.accept(new RelShuttleImpl() {
@Override
public RelNode visit(LogicalUnion union) {
if (union.getInputs().size() > 2) {
throw new AssertionError("union input more 2");
}
return super.visit(union);
}
});

fork(calciteDataContext, map);
ArrayBindable bindable1 = Interpreters.bindable(relNode);
Enumerable<Object[]> bind = bindable1.bind(calciteDataContext);
Expand Down Expand Up @@ -126,7 +138,7 @@ public Enumerator<Object[]> enumerator() {
@Override
@SneakyThrows
public Enumerator<Object[]> enumerator() {
LOGGER.info("拉取数据"+v.getTargetName()+" sql:"+v.getSql());
LOGGER.info("拉取数据" + v.getTargetName() + " sql:" + v.getSql());
return new MyCatResultSetEnumerator(cancelFlag, submit.get());
}
};
Expand All @@ -135,4 +147,11 @@ public Enumerator<Object[]> enumerator() {
}
}
}

@SneakyThrows
public static RelNode compile(MycatCalcitePlanner planner, SqlNode sql, boolean forUpdate) {
SqlNode validate = planner.validate(sql);
RelNode relNode = planner.convert(validate);
return compile(planner, relNode, forUpdate);
}
}
Expand Up @@ -127,7 +127,7 @@ public RelBuilder create(RelOptCluster cluster, RelOptSchema schema) {
.withRelBuilderFactory(relBuilderFactory).build();

public final SqlValidator.Config getValidatorConfig() {
return SqlValidator.Config.DEFAULT;
return SqlValidator.Config.DEFAULT.withSqlConformance(calciteConnectionConfig.conformance());
// .withSqlConformance(calciteConnectionConfig.conformance());
}
// new SqlValidator.Config() {
Expand Down
@@ -0,0 +1,32 @@
package io.mycat.calcite.prepare;

import org.apache.calcite.adapter.java.JavaTypeFactory;
import org.apache.calcite.prepare.CalciteCatalogReader;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.SqlInsert;
import org.apache.calcite.sql.SqlOperatorTable;
import org.apache.calcite.sql.validate.SqlValidatorImpl;

/** Validator. */
class CalciteSqlValidator extends SqlValidatorImpl {

CalciteSqlValidator(SqlOperatorTable opTab,
CalciteCatalogReader catalogReader, JavaTypeFactory typeFactory,
Config config) {
super(opTab, catalogReader, typeFactory, config);
}

@Override protected RelDataType getLogicalSourceRowType(
RelDataType sourceRowType, SqlInsert insert) {
final RelDataType superType =
super.getLogicalSourceRowType(sourceRowType, insert);
return ((JavaTypeFactory) typeFactory).toSql(superType);
}

@Override protected RelDataType getLogicalTargetRowType(
RelDataType targetRowType, SqlInsert insert) {
final RelDataType superType =
super.getLogicalTargetRowType(targetRowType, insert);
return ((JavaTypeFactory) typeFactory).toSql(superType);
}
}

0 comments on commit 4436713

Please sign in to comment.