Search in sources :

Example 1 with PrototypeService

use of io.mycat.prototypeserver.mysql.PrototypeService in project Mycat2 by MyCATApache.

the class MycatRouterConfigOps method commit.

@Override
public void commit() throws Exception {
    ServerConfig serverConfig = MetaClusterCurrent.wrapper(ServerConfig.class);
    boolean init = isInit();
    MycatRouterConfig newConfig = this.newConfig;
    if (!init && Objects.equals(this.original, newConfig)) {
        LOGGER.info("=======================================config no changed===========================================");
        return;
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(JsonUtil.toJson(this.original));
        LOGGER.debug("===========================================change to ===========================================");
        LOGGER.debug(JsonUtil.toJson(newConfig));
    }
    UpdateSet<LogicSchemaConfig> schemaConfigUpdateSet = UpdateSet.create(newConfig.getSchemas(), original.getSchemas());
    UpdateSet<ClusterConfig> clusterConfigUpdateSet = UpdateSet.create(newConfig.getClusters(), original.getClusters());
    UpdateSet<DatasourceConfig> datasourceConfigUpdateSet = UpdateSet.create(newConfig.getDatasources(), original.getDatasources());
    UpdateSet<SequenceConfig> sequenceConfigUpdateSet = UpdateSet.create(newConfig.getSequences(), original.getSequences());
    UpdateSet<SqlCacheConfig> sqlCacheConfigUpdateSet = UpdateSet.create(newConfig.getSqlCacheConfigs(), original.getSqlCacheConfigs());
    UpdateSet<UserConfig> userConfigUpdateSet = UpdateSet.create(newConfig.getUsers(), original.getUsers());
    List<Resource> resourceList = new ArrayList<>();
    boolean connectionUpdateSuccess = false;
    boolean allSuccess = false;
    try {
        // //////////////////////////////////////////////////////////////////////////////////////////////////////////
        if (MetaClusterCurrent.exist(ReplicaSelectorManager.class)) {
            ReplicaSelectorManager replicaSelectorManager = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
            replicaSelectorManager.stop();
        }
        // //////////////////////////////////////////////////////////////////////////////////////////////////////////
        Resource<JdbcConnectionManager> jdbcConnectionManager = getJdbcConnectionManager(datasourceConfigUpdateSet);
        resourceList.add(jdbcConnectionManager);
        Resource<DatasourceConfigProvider> datasourceConfigProvider = getDatasourceConfigProvider(datasourceConfigUpdateSet);
        resourceList.add(datasourceConfigProvider);
        Resource<MySQLManager> mycatMySQLManager = getMycatMySQLManager(datasourceConfigUpdateSet);
        resourceList.add(mycatMySQLManager);
        Resource<ReplicaSelectorManager> replicaSelectorManager = getReplicaSelectorManager(clusterConfigUpdateSet, datasourceConfigUpdateSet, jdbcConnectionManager);
        resourceList.add(replicaSelectorManager);
        JdbcConnectionManager jdbcConnectionManager1 = jdbcConnectionManager.get();
        MetaClusterCurrent.register(JdbcConnectionManager.class, jdbcConnectionManager.get());
        MetaClusterCurrent.register(ConnectionManager.class, jdbcConnectionManager.get());
        resourceList.add(jdbcConnectionManager);
        MetaClusterCurrent.register(DatasourceConfigProvider.class, datasourceConfigProvider.get());
        MetaClusterCurrent.register(ReplicaSelectorManager.class, replicaSelectorManager.get());
        MetaClusterCurrent.register(MySQLManager.class, mycatMySQLManager.get());
        // testPrototype(jdbcConnectionManager1);
        // ////////////////////////////////////////////////////////////////////////////////////////////////////////
        resourceList.clear();
        connectionUpdateSuccess = true;
        // ////////////////////////////////////////////////////////////////////////////////////////////////////////
        Map<Class, Object> context = MetaClusterCurrent.copyContext();
        Resource<Authenticator> authenticator = getAuthenticator(userConfigUpdateSet);
        resourceList.add(authenticator);
        Resource<SqlResultSetService> sqlResultSetService = getSqlResultSetService(sqlCacheConfigUpdateSet);
        resourceList.add(sqlResultSetService);
        Resource<SequenceGenerator> sequenceGenerator = getSequenceGenerator(sequenceConfigUpdateSet);
        resourceList.add(sequenceGenerator);
        PrototypeService prototypeService;
        context.put(PrototypeService.class, prototypeService = new PrototypeService());
        Resource<MetadataManager> metadataManager = getMetadataManager(schemaConfigUpdateSet, prototypeService);
        resourceList.add(metadataManager);
        Resource<DrdsSqlCompiler> drdsSqlCompiler = getDrdsSqlCompiler(schemaConfigUpdateSet, metadataManager);
        resourceList.add(drdsSqlCompiler);
        StatisticCenter statisticCenter = new StatisticCenter();
        Resource<MysqlVariableService> mysqlVariableService = getMysqlVariableService(jdbcConnectionManager);
        resourceList.add(mysqlVariableService);
        Resource<QueryPlanner> queryPlanner = getQueryPlanner(schemaConfigUpdateSet);
        resourceList.add(queryPlanner);
        Resource<XaLog> xaLog = getXaLog(serverConfig, mycatMySQLManager);
        resourceList.add(xaLog);
        Resource<UpdatePlanCache> updatePlanCache = getUpdatePlanCache(schemaConfigUpdateSet);
        resourceList.add(updatePlanCache);
        context.put(JdbcConnectionManager.class, jdbcConnectionManager.get());
        context.put(ConnectionManager.class, jdbcConnectionManager.get());
        context.put(Authenticator.class, authenticator.get());
        context.put(SqlResultSetService.class, sqlResultSetService.get());
        context.put(SequenceGenerator.class, sequenceGenerator.get());
        context.put(DatasourceConfigProvider.class, datasourceConfigProvider.get());
        context.put(ReplicaSelectorManager.class, replicaSelectorManager.get());
        context.put(MySQLManager.class, mycatMySQLManager.get());
        context.put(DrdsSqlCompiler.class, drdsSqlCompiler.get());
        context.put(MysqlVariableService.class, mysqlVariableService.get());
        context.put(XaLog.class, xaLog.get());
        context.put(UpdatePlanCache.class, updatePlanCache.get());
        context.put(QueryPlanner.class, queryPlanner.get());
        context.put(StatisticCenter.class, statisticCenter);
        context.put(MycatRouterConfig.class, newConfig);
        context.put(MetadataManager.class, metadataManager.get());
        MetaClusterCurrent.register(context);
        allSuccess = true;
        if (init) {
            recoveryXA();
            DbPlanManagerPersistorImpl dbPlanManagerPersistor = new DbPlanManagerPersistorImpl();
            dbPlanManagerPersistor.checkStore();
        }
        QueryPlanner planner = MetaClusterCurrent.wrapper(QueryPlanner.class);
    // planner.innerComputeMinCostCodeExecuterContext( DrdsRunnerHelper.preParse("select 1",null));
    // planner.innerComputeMinCostCodeExecuterContext(DrdsRunnerHelper.preParse("select 'x'",null));
    // hotswap
    // //////////////////////////////////////////////////////////////////////////////////////////////////////////
    } finally {
        if (!allSuccess) {
            resourceList.forEach(c -> c.giveup());
        }
        if (MetaClusterCurrent.exist(ReplicaSelectorManager.class)) {
            ReplicaSelectorManager replicaSelectorManager = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
            replicaSelectorManager.start();
        }
    }
    if (options.createSchemaName != null) {
        MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
        TableHandler table = metadataManager.getTable(options.createSchemaName, options.createTableName);
        if (table != null) {
            table.createPhysicalTables();
        }
    }
    if (options.persistence) {
        persistence(schemaConfigUpdateSet, clusterConfigUpdateSet, datasourceConfigUpdateSet, sequenceConfigUpdateSet, sqlCacheConfigUpdateSet, userConfigUpdateSet);
    }
}
Also used : SequenceGenerator(io.mycat.plug.sequence.SequenceGenerator) PrototypeService(io.mycat.prototypeserver.mysql.PrototypeService) MySQLManager(cn.mycat.vertx.xa.MySQLManager) StatisticCenter(io.mycat.statistic.StatisticCenter) SqlResultSetService(io.mycat.commands.SqlResultSetService) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) MonitorReplicaSelectorManager(io.mycat.monitor.MonitorReplicaSelectorManager) DbPlanManagerPersistorImpl(io.mycat.calcite.spm.DbPlanManagerPersistorImpl) UpdatePlanCache(io.mycat.calcite.spm.UpdatePlanCache) QueryPlanner(io.mycat.calcite.spm.QueryPlanner) DualCustomTableHandler(io.mycat.calcite.table.DualCustomTableHandler) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) XaLog(cn.mycat.vertx.xa.XaLog)

Example 2 with PrototypeService

use of io.mycat.prototypeserver.mysql.PrototypeService in project Mycat2 by MyCATApache.

the class MycatVertxMySQLHandler method handlePrepareStatement.

private Future<Void> handlePrepareStatement(byte[] bytes, MycatVertxMysqlSession mysqlSession) {
    boolean deprecateEOF = mysqlSession.isDeprecateEOF();
    String sql = new String(bytes);
    // ///////////////////////////////////////////////////
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("received pstmt sql:{}", sql);
    }
    SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
    boolean allow = (sqlStatement instanceof SQLSelectStatement || sqlStatement instanceof SQLInsertStatement || sqlStatement instanceof SQLUpdateStatement || sqlStatement instanceof SQLDeleteStatement);
    // MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    MycatRowMetaData fields;
    if ((sqlStatement instanceof SQLSelectStatement)) {
        PrototypeService prototypeService = MetaClusterCurrent.wrapper(PrototypeService.class);
        Optional<MycatRowMetaData> mycatRowMetaDataForPrepareStatement = prototypeService.getMycatRowMetaDataForPrepareStatement(mysqlSession.getDataContext().getDefaultSchema(), sql);
        if (!mycatRowMetaDataForPrepareStatement.isPresent()) {
            return VertxUtil.castPromise(Future.failedFuture(new SQLException("This command is not supported in the prepared statement protocol yet", "HY000", 1295)));
        }
        fields = mycatRowMetaDataForPrepareStatement.get();
    } else {
        fields = ResultSetBuilder.create().build().getMetaData();
    }
    ResultSetBuilder paramsBuilder = ResultSetBuilder.create();
    sqlStatement.accept(new MySqlASTVisitorAdapter() {

        @Override
        public void endVisit(SQLVariantRefExpr x) {
            if ("?".equalsIgnoreCase(x.getName())) {
                JDBCType res = JDBCType.VARCHAR;
                paramsBuilder.addColumnInfo("", res);
            }
            super.endVisit(x);
        }
    });
    MycatRowMetaData params = paramsBuilder.build().getMetaData();
    long stmtId = mycatDataContext.nextPrepareStatementId();
    Map<Long, io.mycat.PreparedStatement> statementMap = this.mycatDataContext.getPrepareInfo();
    PreparedStatement preparedStatement = new PreparedStatement(stmtId, sqlStatement, params.getColumnCount());
    for (int i = 0; i < params.getColumnCount(); i++) {
        preparedStatement.getParametersType()[i] = MysqlDefs.FIELD_TYPE_STRING;
    }
    statementMap.put(stmtId, preparedStatement);
    DefaultPreparedOKPacket info = new DefaultPreparedOKPacket(stmtId, fields.getColumnCount(), params.getColumnCount(), session.getWarningCount());
    Future<Void> writeEndFuture = Future.succeededFuture();
    if (info.getPrepareOkColumnsCount() == 0 && info.getPrepareOkParametersCount() == 0) {
        return session.writeBytes(MySQLPacketUtil.generatePrepareOk(info), true);
    }
    session.writeBytes(MySQLPacketUtil.generatePrepareOk(info), false);
    if (info.getPrepareOkParametersCount() > 0 && info.getPrepareOkColumnsCount() == 0) {
        for (int i = 0; i < info.getPrepareOkParametersCount(); i++) {
            writeEndFuture = session.writeBytes(MySQLPacketUtil.generateColumnDefPayload(params, i), info.getPrepareOkParametersCount() - 1 == i && deprecateEOF);
        }
        if (deprecateEOF) {
            return writeEndFuture;
        } else {
            return session.writeBytes(MySQLPacketUtil.generateEof(session.getWarningCount(), session.getServerStatusValue()), true);
        }
    } else if (info.getPrepareOkParametersCount() == 0 && info.getPrepareOkColumnsCount() > 0) {
        for (int i = 0; i < info.getPrepareOkColumnsCount(); i++) {
            writeEndFuture = session.writeBytes(MySQLPacketUtil.generateColumnDefPayload(fields, i), info.getPrepareOkColumnsCount() - 1 == i && deprecateEOF);
        }
        if (deprecateEOF) {
            return writeEndFuture;
        } else {
            return session.writeBytes(MySQLPacketUtil.generateEof(session.getWarningCount(), session.getServerStatusValue()), true);
        }
    } else {
        for (int i = 0; i < info.getPrepareOkParametersCount(); i++) {
            writeEndFuture = session.writeBytes(MySQLPacketUtil.generateColumnDefPayload(params, i), false);
        }
        writeEndFuture = session.writeColumnEndPacket(false);
        for (int i = 0; i < info.getPrepareOkColumnsCount(); i++) {
            writeEndFuture = session.writeBytes(MySQLPacketUtil.generateColumnDefPayload(fields, i), info.getPrepareOkColumnsCount() - 1 == i && deprecateEOF);
        }
        if (deprecateEOF) {
            return writeEndFuture;
        } else {
            return session.writeBytes(MySQLPacketUtil.generateEof(session.getWarningCount(), session.getServerStatusValue()), true);
        }
    }
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SQLException(java.sql.SQLException) DefaultPreparedOKPacket(io.mycat.beans.mysql.packet.DefaultPreparedOKPacket) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) JDBCType(java.sql.JDBCType) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) PrototypeService(io.mycat.prototypeserver.mysql.PrototypeService) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData)

Aggregations

PrototypeService (io.mycat.prototypeserver.mysql.PrototypeService)2 MySQLManager (cn.mycat.vertx.xa.MySQLManager)1 XaLog (cn.mycat.vertx.xa.XaLog)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)1 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)1 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)1 MySqlASTVisitorAdapter (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter)1 MycatRowMetaData (io.mycat.beans.mycat.MycatRowMetaData)1 ResultSetBuilder (io.mycat.beans.mycat.ResultSetBuilder)1 DefaultPreparedOKPacket (io.mycat.beans.mysql.packet.DefaultPreparedOKPacket)1 DbPlanManagerPersistorImpl (io.mycat.calcite.spm.DbPlanManagerPersistorImpl)1 QueryPlanner (io.mycat.calcite.spm.QueryPlanner)1 UpdatePlanCache (io.mycat.calcite.spm.UpdatePlanCache)1 DualCustomTableHandler (io.mycat.calcite.table.DualCustomTableHandler)1 SqlResultSetService (io.mycat.commands.SqlResultSetService)1 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)1 MonitorReplicaSelectorManager (io.mycat.monitor.MonitorReplicaSelectorManager)1