Search in sources :

Example 1 with UpdatePlanCache

use of io.mycat.calcite.spm.UpdatePlanCache 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 UpdatePlanCache

use of io.mycat.calcite.spm.UpdatePlanCache in project Mycat2 by MyCATApache.

the class UpdateSQLHandler method getPlan.

@Nullable
public static Plan getPlan(DrdsSqlWithParams drdsSqlWithParams) {
    UpdatePlanCache updatePlanCache = MetaClusterCurrent.wrapper(UpdatePlanCache.class);
    Plan plan = updatePlanCache.computeIfAbsent(drdsSqlWithParams.getParameterizedSQL(), s -> {
        DrdsSqlCompiler drdsRunner = MetaClusterCurrent.wrapper(DrdsSqlCompiler.class);
        OptimizationContext optimizationContext = new OptimizationContext();
        MycatRel dispatch = drdsRunner.dispatch(optimizationContext, drdsSqlWithParams);
        return DrdsExecutorCompiler.convertToExecuter(dispatch);
    });
    return plan;
}
Also used : MycatRel(io.mycat.calcite.MycatRel) OptimizationContext(io.mycat.calcite.rewriter.OptimizationContext) UpdatePlanCache(io.mycat.calcite.spm.UpdatePlanCache) Plan(io.mycat.calcite.spm.Plan) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

UpdatePlanCache (io.mycat.calcite.spm.UpdatePlanCache)2 MySQLManager (cn.mycat.vertx.xa.MySQLManager)1 XaLog (cn.mycat.vertx.xa.XaLog)1 MycatRel (io.mycat.calcite.MycatRel)1 OptimizationContext (io.mycat.calcite.rewriter.OptimizationContext)1 DbPlanManagerPersistorImpl (io.mycat.calcite.spm.DbPlanManagerPersistorImpl)1 Plan (io.mycat.calcite.spm.Plan)1 QueryPlanner (io.mycat.calcite.spm.QueryPlanner)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 SequenceGenerator (io.mycat.plug.sequence.SequenceGenerator)1 PrototypeService (io.mycat.prototypeserver.mysql.PrototypeService)1 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)1 StatisticCenter (io.mycat.statistic.StatisticCenter)1 Nullable (org.jetbrains.annotations.Nullable)1