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);
}
}
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);
}
}
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;
}
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();
}
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);
}
}
Aggregations