Search in sources :

Example 6 with SchemaHandler

use of io.mycat.calcite.table.SchemaHandler in project Mycat2 by MyCATApache.

the class StatisticCenterTest method init.

@BeforeClass
public static void init() throws Exception {
    HashMap<Class, Object> context = new HashMap<>();
    context.put(Vertx.class, Vertx.vertx());
    context.put(ServerConfig.class, new ServerConfig());
    context.put(DrdsSqlCompiler.class, new DrdsSqlCompiler(new DrdsConst() {

        @Override
        public NameMap<SchemaHandler> schemas() {
            return new NameMap<>();
        }
    }));
    MetaClusterCurrent.register(context);
    String customerDatasourceProvider = DruidDatasourceProvider.class.getName();
    DatasourceConfig datasourceConfig = new DatasourceConfig();
    datasourceConfig.setDbType("mysql");
    datasourceConfig.setUser("root");
    datasourceConfig.setPassword("123456");
    datasourceConfig.setName("prototypeDs");
    datasourceConfig.setUrl("jdbc:mysql://localhost:3306/mysql");
    Map<String, DatasourceConfig> datasources = Maps.of("prototypeDs", datasourceConfig);
    ClusterConfig clusterConfig = new ClusterConfig();
    clusterConfig.setName("prototype");
    clusterConfig.setMasters(Arrays.asList("prototypeDs"));
    Map<String, ClusterConfig> clusterConfigs = Maps.of("prototype", clusterConfig);
    LinkedList<Runnable> runnables = new LinkedList<>();
    ReplicaSelectorManager manager = ReplicaSelectorRuntime.create(new ArrayList<>(clusterConfigs.values()), datasources, new LoadBalanceManager(), name -> 0, (command, initialDelay, period, unit) -> {
        runnables.add(command);
        return () -> {
        };
    });
    context.put(ReplicaSelectorManager.class, manager);
    context.put(JdbcConnectionManager.class, jdbcManager = new JdbcConnectionManager(DruidDatasourceProvider.class.getName(), datasources));
    MetaClusterCurrent.register(context);
    statisticCenter.init();
}
Also used : SchemaHandler(io.mycat.calcite.table.SchemaHandler) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) LoadBalanceManager(io.mycat.plug.loadBalance.LoadBalanceManager) DruidDatasourceProvider(io.mycat.datasource.jdbc.DruidDatasourceProvider) ServerConfig(io.mycat.config.ServerConfig) DatasourceConfig(io.mycat.config.DatasourceConfig) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ClusterConfig(io.mycat.config.ClusterConfig)

Example 7 with SchemaHandler

use of io.mycat.calcite.table.SchemaHandler in project Mycat2 by MyCATApache.

the class SpmTest method init.

@BeforeClass
public static void init() throws Exception {
    HashMap<Class, Object> context = new HashMap<>();
    context.put(Vertx.class, Vertx.vertx());
    context.put(ServerConfig.class, new ServerConfig());
    context.put(IOExecutor.class, IOExecutor.DEFAULT);
    context.put(DrdsSqlCompiler.class, new DrdsSqlCompiler(new DrdsConst() {

        @Override
        public NameMap<SchemaHandler> schemas() {
            return new NameMap<>();
        }
    }));
    MetaClusterCurrent.register(context);
    String customerDatasourceProvider = DruidDatasourceProvider.class.getName();
    DatasourceConfig datasourceConfig = new DatasourceConfig();
    datasourceConfig.setDbType("mysql");
    datasourceConfig.setUser("root");
    datasourceConfig.setPassword("123456");
    datasourceConfig.setName("prototypeDs");
    datasourceConfig.setUrl("jdbc:mysql://localhost:3306/mysql");
    Map<String, DatasourceConfig> datasources = Maps.of("prototypeDs", datasourceConfig);
    ClusterConfig clusterConfig = new ClusterConfig();
    clusterConfig.setName("prototype");
    clusterConfig.setMasters(Arrays.asList("prototypeDs"));
    Map<String, ClusterConfig> clusterConfigs = Maps.of("prototype", clusterConfig);
    LinkedList<Runnable> runnables = new LinkedList<>();
    ReplicaSelectorManager manager = ReplicaSelectorRuntime.create(new ArrayList<>(clusterConfigs.values()), datasources, new LoadBalanceManager(), name -> 0, (command, initialDelay, period, unit) -> {
        runnables.add(command);
        return () -> {
        };
    });
    context.put(ReplicaSelectorManager.class, manager);
    context.put(JdbcConnectionManager.class, jdbcManager = new JdbcConnectionManager(DruidDatasourceProvider.class.getName(), datasources));
    MetaClusterCurrent.register(context);
}
Also used : SchemaHandler(io.mycat.calcite.table.SchemaHandler) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) LoadBalanceManager(io.mycat.plug.loadBalance.LoadBalanceManager) DruidDatasourceProvider(io.mycat.datasource.jdbc.DruidDatasourceProvider) ServerConfig(io.mycat.config.ServerConfig) DatasourceConfig(io.mycat.config.DatasourceConfig) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ClusterConfig(io.mycat.config.ClusterConfig) BeforeClass(org.junit.BeforeClass)

Example 8 with SchemaHandler

use of io.mycat.calcite.table.SchemaHandler in project Mycat2 by MyCATApache.

the class HintHandler method showTables.

@Nullable
private Future<Void> showTables(Response response, String body, MetadataManager metadataManager, MycatRouterConfig routerConfig) {
    Map map = JsonUtil.from(body, Map.class);
    String type = (String) map.get("type");
    String schemaName = (String) map.get("schemaName");
    Stream<TableHandler> tables;
    Stream<TableHandler> tableHandlerStream;
    if (schemaName == null) {
        tableHandlerStream = metadataManager.getSchemaMap().values().stream().flatMap(i -> i.logicTables().values().stream());
    } else {
        SchemaHandler schemaHandler = Objects.requireNonNull(metadataManager.getSchemaMap().get(schemaName));
        NameMap<TableHandler> logicTables = schemaHandler.logicTables();
        tableHandlerStream = logicTables.values().stream();
    }
    if ("global".equalsIgnoreCase(type)) {
        tables = tableHandlerStream.filter(i -> i.getType() == LogicTableType.GLOBAL);
    } else if ("sharding".equalsIgnoreCase(type)) {
        tables = tableHandlerStream.filter(i -> i.getType() == LogicTableType.SHARDING);
    } else if ("normal".equalsIgnoreCase(type)) {
        tables = tableHandlerStream.filter(i -> i.getType() == LogicTableType.NORMAL);
    } else if ("custom".equalsIgnoreCase(type)) {
        tables = tableHandlerStream.filter(i -> i.getType() == LogicTableType.CUSTOM);
    } else {
        tables = tableHandlerStream;
    }
    ResultSetBuilder builder = ResultSetBuilder.create();
    builder.addColumnInfo("SCHEMA_NAME", JDBCType.VARCHAR).addColumnInfo("TABLE_NAME", JDBCType.VARCHAR).addColumnInfo("CREATE_TABLE_SQL", JDBCType.VARCHAR).addColumnInfo("TYPE", JDBCType.VARCHAR).addColumnInfo("COLUMNS", JDBCType.VARCHAR).addColumnInfo("CONFIG", JDBCType.VARCHAR);
    tables.forEach(table -> {
        String SCHEMA_NAME = table.getSchemaName();
        String TABLE_NAME = table.getTableName();
        String CREATE_TABLE_SQL = table.getCreateTableSQL();
        LogicTableType TYPE = table.getType();
        String COLUMNS = table.getColumns().stream().map(i -> i.toString()).collect(Collectors.joining(","));
        String CONFIG = routerConfig.getSchemas().stream().filter(i -> SCHEMA_NAME.equalsIgnoreCase(i.getSchemaName())).map(i -> {
            switch(TYPE) {
                case SHARDING:
                    return NameMap.immutableCopyOf(i.getShardingTables());
                case GLOBAL:
                    return NameMap.immutableCopyOf(i.getGlobalTables());
                case NORMAL:
                    return NameMap.immutableCopyOf(i.getNormalTables());
                case CUSTOM:
                    return NameMap.immutableCopyOf(i.getCustomTables());
                default:
                    return null;
            }
        }).map(i -> i.get(TABLE_NAME)).findFirst().map(i -> i.toString()).orElse(null);
        builder.addObjectRowPayload(Arrays.asList(SCHEMA_NAME, TABLE_NAME, CREATE_TABLE_SQL, TYPE, COLUMNS, CONFIG));
    });
    return response.sendResultSet(() -> builder.build());
}
Also used : MycatdbCommand(io.mycat.commands.MycatdbCommand) io.mycat(io.mycat) CSVRecord(org.apache.commons.csv.CSVRecord) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) CSVFormat(org.apache.commons.csv.CSVFormat) HeartbeatFlow(io.mycat.replica.heartbeat.HeartbeatFlow) Duration(java.time.Duration) MySQLErrorCode(io.mycat.beans.mysql.MySQLErrorCode) JsonUtil(io.mycat.util.JsonUtil) MySqlHintStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHintStatement) io.mycat.config(io.mycat.config) Timestamp(java.sql.Timestamp) Reader(java.io.Reader) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) ArrayBindable(org.apache.calcite.runtime.ArrayBindable) MycatSQLLogMonitor(io.mycat.monitor.MycatSQLLogMonitor) io.mycat.sqlhandler(io.mycat.sqlhandler) NormalTable(io.mycat.calcite.table.NormalTable) Nullable(org.jetbrains.annotations.Nullable) ReplicaSelector(io.mycat.replica.ReplicaSelector) UpdateSQLHandler(io.mycat.sqlhandler.dml.UpdateSQLHandler) Stream(java.util.stream.Stream) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) DrdsRunnerHelper(io.mycat.calcite.DrdsRunnerHelper) PhysicsInstance(io.mycat.replica.PhysicsInstance) NotNull(org.jetbrains.annotations.NotNull) ShardingTable(io.mycat.calcite.table.ShardingTable) VertxExecuter(io.mycat.vertx.VertxExecuter) SqlEntry(io.mycat.monitor.SqlEntry) java.util(java.util) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) LocalDateTime(java.time.LocalDateTime) XaLog(cn.mycat.vertx.xa.XaLog) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) SqlResultSetService(io.mycat.commands.SqlResultSetService) InterruptThreadHint(io.mycat.hint.InterruptThreadHint) MycatSQLExprTableSourceUtil(io.mycat.util.MycatSQLExprTableSourceUtil) KillThreadHint(io.mycat.hint.KillThreadHint) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) JDBCType(java.sql.JDBCType) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) ImmutableList(com.google.common.collect.ImmutableList) DatasourceStatus(io.mycat.replica.heartbeat.DatasourceStatus) Observable(io.reactivex.rxjava3.core.Observable) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) StreamSupport(java.util.stream.StreamSupport) GlobalTable(io.mycat.calcite.table.GlobalTable) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLUtils(com.alibaba.druid.sql.SQLUtils) Flowable(io.reactivex.rxjava3.core.Flowable) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) io.mycat.hint(io.mycat.hint) VertxUpdateExecuter(io.mycat.vertx.VertxUpdateExecuter) StringWriter(java.io.StringWriter) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) ExecutorProvider(io.mycat.calcite.ExecutorProvider) IOException(java.io.IOException) QuoteMode(org.apache.commons.csv.QuoteMode) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) SqlRecorderRuntime(io.mycat.exporter.SqlRecorderRuntime) VertxUtil(io.mycat.util.VertxUtil) ReplicaSwitchType(io.mycat.replica.ReplicaSwitchType) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) MycatInsertRel(io.mycat.calcite.physical.MycatInsertRel) io.mycat.calcite.spm(io.mycat.calcite.spm) NameMap(io.mycat.util.NameMap) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) JdbcUtils(com.alibaba.druid.util.JdbcUtils) CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) HeartBeatStatus(io.mycat.replica.heartbeat.HeartBeatStatus) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) UnmodifiableIterator(com.google.common.collect.UnmodifiableIterator) FileReader(java.io.FileReader) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) StorageManager(io.mycat.sqlhandler.config.StorageManager) SchemaHandler(io.mycat.calcite.table.SchemaHandler) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) NameMap(io.mycat.util.NameMap) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with SchemaHandler

use of io.mycat.calcite.table.SchemaHandler in project Mycat2 by MyCATApache.

the class UpdateSQLHandler method updateHandler.

@SneakyThrows
public static Future<Void> updateHandler(SQLStatement sqlStatement, MycatDataContext dataContext, SQLTableSource tableSourceArg, Response receiver) {
    boolean insert = sqlStatement instanceof SQLInsertStatement;
    if (tableSourceArg instanceof SQLExprTableSource) {
        SQLExprTableSource tableSource = (SQLExprTableSource) tableSourceArg;
        String schemaName = Optional.ofNullable(tableSource.getSchema() == null ? dataContext.getDefaultSchema() : tableSource.getSchema()).map(i -> SQLUtils.normalize(i)).orElse(null);
        String tableName = SQLUtils.normalize(tableSource.getTableName());
        if (schemaName == null) {
            return receiver.sendError("unknown schema", MySQLErrorCode.ER_UNKNOWN_ERROR);
        }
        tableSource.setSchema(schemaName);
        SchemaHandler schemaHandler;
        MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
        Optional<NameMap<SchemaHandler>> handlerMapOptional = Optional.ofNullable(metadataManager.getSchemaMap());
        Optional<String> targetNameOptional = Optional.ofNullable(metadataManager.getPrototype());
        if (!handlerMapOptional.isPresent()) {
            if (targetNameOptional.isPresent()) {
                if (insert) {
                    DrdsSqlWithParams drdsSqlWithParams = MycatPreparedStatementUtil.outputToParameterizedProxySql((MySqlInsertStatement) sqlStatement);
                    return receiver.proxyInsert(Collections.singletonList(targetNameOptional.get()), drdsSqlWithParams.getParameterizedSQL(), drdsSqlWithParams.getParams());
                } else {
                    DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sqlStatement, dataContext.getDefaultSchema());
                    return receiver.proxyUpdate(Collections.singletonList(targetNameOptional.get()), drdsSqlWithParams.getParameterizedSQL(), drdsSqlWithParams.getParams());
                }
            } else {
                return receiver.sendError(new MycatException("Unable to route:" + sqlStatement));
            }
        } else {
            NameMap<SchemaHandler> handlerMap = handlerMapOptional.get();
            schemaHandler = Optional.ofNullable(handlerMap.get(schemaName)).orElseGet(() -> {
                if (dataContext.getDefaultSchema() == null) {
                    // 可能schemaName有值,但是值名不是配置的名字
                    throw new MycatException("unknown schema:" + schemaName);
                }
                return handlerMap.get(dataContext.getDefaultSchema());
            });
            if (schemaHandler == null) {
                return receiver.sendError(new MycatException("Unable to route:" + sqlStatement));
            }
        }
        String defaultTargetName = schemaHandler.defaultTargetName();
        NameMap<TableHandler> tableMap = schemaHandler.logicTables();
        TableHandler tableHandler = tableMap.get(tableName);
        // /////////////////////////////common///////////////////////////////
        if (tableHandler == null) {
            if (insert) {
                DrdsSqlWithParams drdsSqlWithParams = MycatPreparedStatementUtil.outputToParameterizedProxySql((MySqlInsertStatement) sqlStatement);
                return receiver.proxyInsert(Collections.singletonList(Objects.requireNonNull(defaultTargetName, "can not route :" + drdsSqlWithParams)), drdsSqlWithParams.getParameterizedSQL(), drdsSqlWithParams.getParams());
            } else {
                DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sqlStatement, null);
                return receiver.proxyUpdate(Collections.singletonList(Objects.requireNonNull(defaultTargetName, "can not route :" + drdsSqlWithParams)), drdsSqlWithParams.getParameterizedSQL(), drdsSqlWithParams.getParams());
            }
        }
        switch(tableHandler.getType()) {
            case NORMAL:
                DrdsSqlWithParams drdsSqlWithParams;
                if (insert) {
                    drdsSqlWithParams = MycatPreparedStatementUtil.outputToParameterizedProxySql((MySqlInsertStatement) sqlStatement);
                } else {
                    drdsSqlWithParams = DrdsRunnerHelper.preParse(sqlStatement, dataContext.getDefaultSchema());
                }
                HackRouter hackRouter = new HackRouter(drdsSqlWithParams.getParameterizedStatement(), dataContext);
                if (hackRouter.analyse()) {
                    Pair<String, String> plan = hackRouter.getPlan();
                    if (insert) {
                        return receiver.proxyInsert(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
                    } else {
                        return receiver.proxyUpdate(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
                    }
                }
                break;
            default:
                break;
        }
        DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sqlStatement, dataContext.getDefaultSchema());
        return executeUpdate(drdsSqlWithParams, dataContext, receiver, schemaName);
    }
    DrdsSqlWithParams drdsSqlWithParams = insert ? MycatPreparedStatementUtil.outputToParameterizedProxySql((MySqlInsertStatement) sqlStatement) : DrdsRunnerHelper.preParse(sqlStatement, dataContext.getDefaultSchema());
    HackRouter hackRouter = new HackRouter(drdsSqlWithParams.getParameterizedStatement(), dataContext);
    if (hackRouter.analyse()) {
        Pair<String, String> plan = hackRouter.getPlan();
        if (insert) {
            return receiver.proxyInsert(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
        } else {
            return receiver.proxyUpdate(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
        }
    }
    return receiver.sendError(new MycatException("can not route " + sqlStatement));
}
Also used : io.mycat(io.mycat) HackRouter(io.mycat.prototypeserver.mysql.HackRouter) SneakyThrows(lombok.SneakyThrows) SQLRequest(io.mycat.sqlhandler.SQLRequest) UpdatePlanCache(io.mycat.calcite.spm.UpdatePlanCache) SchemaHandler(io.mycat.calcite.table.SchemaHandler) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) PlanImplementor(io.mycat.calcite.plan.PlanImplementor) MycatRel(io.mycat.calcite.MycatRel) MySQLErrorCode(io.mycat.beans.mysql.MySQLErrorCode) OptimizationContext(io.mycat.calcite.rewriter.OptimizationContext) Pair(io.mycat.util.Pair) SQLUtils(com.alibaba.druid.sql.SQLUtils) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) AbstractSQLHandler(io.mycat.sqlhandler.AbstractSQLHandler) Future(io.vertx.core.Future) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) NameMap(io.mycat.util.NameMap) MycatPreparedStatementUtil(io.mycat.calcite.executor.MycatPreparedStatementUtil) Optional(java.util.Optional) DrdsRunnerHelper(io.mycat.calcite.DrdsRunnerHelper) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) Collections(java.util.Collections) Plan(io.mycat.calcite.spm.Plan) SchemaHandler(io.mycat.calcite.table.SchemaHandler) NameMap(io.mycat.util.NameMap) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) HackRouter(io.mycat.prototypeserver.mysql.HackRouter) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SneakyThrows(lombok.SneakyThrows)

Aggregations

SchemaHandler (io.mycat.calcite.table.SchemaHandler)9 NameMap (io.mycat.util.NameMap)9 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)7 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)7 SQLUtils (com.alibaba.druid.sql.SQLUtils)6 PrepareExecutor (io.mycat.calcite.PrepareExecutor)5 DatasourceConfig (io.mycat.config.DatasourceConfig)5 JdbcDataSource (io.mycat.datasource.jdbc.datasource.JdbcDataSource)4 java.util (java.util)4 Supplier (java.util.function.Supplier)4 Collectors (java.util.stream.Collectors)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLLiteralExpr)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 SQLTextLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr)3 com.alibaba.druid.sql.ast.statement (com.alibaba.druid.sql.ast.statement)3 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)3 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)3 com.alibaba.druid.sql.dialect.mysql.ast.statement (com.alibaba.druid.sql.dialect.mysql.ast.statement)3