Search in sources :

Example 16 with JdbcConnectionManager

use of io.mycat.datasource.jdbc.datasource.JdbcConnectionManager in project Mycat2 by MyCATApache.

the class MycatRouterConfigOps method getJdbcConnectionManager.

@NotNull
private Resource<JdbcConnectionManager> getJdbcConnectionManager(UpdateSet<DatasourceConfig> datasourceConfigUpdateSet) {
    if (MetaClusterCurrent.exist(JdbcConnectionManager.class)) {
        JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
        if (!datasourceConfigUpdateSet.isEmpty()) {
            if (datasourceConfigUpdateSet.getDelete().isEmpty() && !datasourceConfigUpdateSet.getCreate().isEmpty()) {
                for (DatasourceConfig datasourceConfig : datasourceConfigUpdateSet.getCreate()) {
                    jdbcConnectionManager.addDatasource(datasourceConfig);
                }
                return Resource.of(jdbcConnectionManager, true);
            } else {
                boolean success = false;
                try {
                    JdbcConnectionManager newJdbcConnectionManager = new JdbcConnectionManager(DruidDatasourceProvider.class.getCanonicalName(), datasourceConfigUpdateSet.getTargetAsMap());
                    success = true;
                    return Resource.of(newJdbcConnectionManager, false);
                } finally {
                    if (success) {
                        jdbcConnectionManager.close();
                    }
                }
            }
        } else {
            return Resource.of(jdbcConnectionManager, true);
        }
    } else {
        return Resource.of(new JdbcConnectionManager(DruidDatasourceProvider.class.getCanonicalName(), datasourceConfigUpdateSet.getTargetAsMap()), false);
    }
}
Also used : DruidDatasourceProvider(io.mycat.datasource.jdbc.DruidDatasourceProvider) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with JdbcConnectionManager

use of io.mycat.datasource.jdbc.datasource.JdbcConnectionManager 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 18 with JdbcConnectionManager

use of io.mycat.datasource.jdbc.datasource.JdbcConnectionManager in project Mycat2 by MyCATApache.

the class MycatMySQLManagerImpl method getWriteableConnectionMap.

@Override
@SneakyThrows
public Map<String, java.sql.Connection> getWriteableConnectionMap() {
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    Map<String, JdbcDataSource> datasourceInfo = jdbcConnectionManager.getDatasourceInfo();
    HashMap<String, Connection> map = new HashMap<>();
    for (String string : datasourceInfo.keySet()) {
        // /////////////////////////////////////
        JdbcDataSource jdbcDataSource = datasourceInfo.get(string);
        DatasourceConfig config = jdbcDataSource.getConfig();
        if (jdbcDataSource.isMySQLType()) {
            if (Optional.ofNullable(config.getInstanceType()).map(i -> InstanceType.valueOf(i.toUpperCase())).orElse(InstanceType.READ_WRITE).isWriteType()) {
                Connection connection = jdbcDataSource.getDataSource().getConnection();
                if (connection.isReadOnly()) {
                    JdbcUtils.close(connection);
                    continue;
                }
                map.put(string, connection);
            }
        }
    }
    return map;
}
Also used : DatasourceConfig(io.mycat.config.DatasourceConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) Connection(java.sql.Connection) NewMycatConnection(io.mycat.newquery.NewMycatConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SneakyThrows(lombok.SneakyThrows)

Example 19 with JdbcConnectionManager

use of io.mycat.datasource.jdbc.datasource.JdbcConnectionManager in project Mycat2 by MyCATApache.

the class JdbcDatasourcePoolImpl method getUsedNumber.

@Override
public Integer getUsedNumber() {
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    JdbcDataSource jdbcDataSource = jdbcConnectionManager.getDatasourceInfo().get(targetName);
    return jdbcDataSource.getUsedCount();
}
Also used : JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)

Example 20 with JdbcConnectionManager

use of io.mycat.datasource.jdbc.datasource.JdbcConnectionManager in project Mycat2 by MyCATApache.

the class JdbcDatasourcePoolImpl method getConnection.

@Override
public Future<NewMycatConnection> getConnection() {
    try {
        JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
        DefaultConnection defaultConnection = jdbcConnectionManager.getConnection(targetName);
        DatabaseInstanceEntry stat = DatabaseInstanceEntry.stat(targetName);
        stat.plusCon();
        stat.plusQps();
        NewMycatConnectionImpl newMycatConnection = new NewMycatConnectionImpl(targetName, defaultConnection.getRawConnection()) {

            long start;

            final AtomicBoolean closeFlag = new AtomicBoolean(false);

            @Override
            public void onSend() {
                start = System.currentTimeMillis();
            }

            @Override
            public void onRev() {
                long end = System.currentTimeMillis();
                InstanceMonitor.plusPrt(end - start);
            }

            @Override
            public Future<Void> close() {
                if (closeFlag.compareAndSet(false, true)) {
                    stat.decCon();
                    defaultConnection.close();
                }
                return Future.succeededFuture();
            }

            @Override
            public void abandonConnection() {
                if (closeFlag.compareAndSet(false, true)) {
                    stat.decCon();
                    Connection rawConnection = defaultConnection.getRawConnection();
                    if (rawConnection instanceof DruidPooledConnection) {
                        DruidPooledConnection connection = (DruidPooledConnection) rawConnection;
                        connection.abandond();
                    }
                    defaultConnection.close();
                }
            }
        };
        return Future.succeededFuture(new ThreadMycatConnectionImplWrapper(stat, newMycatConnection));
    } catch (Throwable throwable) {
        return Future.failedFuture(throwable);
    }
}
Also used : DatabaseInstanceEntry(io.mycat.monitor.DatabaseInstanceEntry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) NewMycatConnectionImpl(io.mycat.newquery.NewMycatConnectionImpl) Connection(java.sql.Connection) NewMycatConnection(io.mycat.newquery.NewMycatConnection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ThreadMycatConnectionImplWrapper(io.mycat.monitor.ThreadMycatConnectionImplWrapper)

Aggregations

JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)57 DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)38 SneakyThrows (lombok.SneakyThrows)21 Connection (java.sql.Connection)16 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)8 java.util (java.util)8 NotNull (org.jetbrains.annotations.NotNull)8 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)7 DatasourceConfig (io.mycat.config.DatasourceConfig)7 JdbcDataSource (io.mycat.datasource.jdbc.datasource.JdbcDataSource)7 Collectors (java.util.stream.Collectors)7 MetadataManager (io.mycat.MetadataManager)6 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)6 NameMap (io.mycat.util.NameMap)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)5 MycatRowMetaData (io.mycat.beans.mycat.MycatRowMetaData)5 SQLException (java.sql.SQLException)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Logger (org.slf4j.Logger)5