use of io.mycat.config.DatasourceConfig 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.config.DatasourceConfig in project Mycat2 by MyCATApache.
the class MycatMySQLManagerImpl method createNativeDatasourcePool.
@NotNull
public static MycatDatasourcePool createNativeDatasourcePool(DatasourceConfig datasource, String targetName) {
ConnectionUrlParser connectionUrlParser = ConnectionUrlParser.parseConnectionString(datasource.getUrl());
HostInfo hostInfo = connectionUrlParser.getHosts().get(0);
int port = hostInfo.getPort();
String host = hostInfo.getHost();
String username = datasource.getUser();
String password = datasource.getPassword();
VertxPoolConnectionImpl.Config config = new VertxPoolConnectionImpl.Config();
config.setHost(host);
config.setPassword(password);
config.setUsername(username);
config.setPort(port);
config.setDatabase(connectionUrlParser.getPath());
config.setRetry(datasource.getMaxRetryCount());
config.setTimer(datasource.getIdleTimeout());
config.setClientDeprecateEof(NewMycatConnectionConfig.CLIENT_DEPRECATE_EOF);
Vertx vertx = MetaClusterCurrent.wrapper(Vertx.class);
VertxPoolConnectionImpl vertxConnectionPool = new VertxPoolConnectionImpl(config, vertx);
return new MycatNativeDatasourcePool(vertxConnectionPool, targetName);
}
use of io.mycat.config.DatasourceConfig in project Mycat2 by MyCATApache.
the class MigrateUtil method read.
@SneakyThrows
public static Flowable<Object[]> read(MigrateUtil.MigrateJdbcInput migrateJdbcInput, Partition backend) {
MycatRouterConfig routerConfig = MetaClusterCurrent.wrapper(MycatRouterConfig.class);
ReplicaSelectorManager replicaSelectorRuntime = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
String targetName = backend.getTargetName();
String tableName = backend.getTable();
String schemaName = backend.getSchema();
String datasourceName = replicaSelectorRuntime.getDatasourceNameByReplicaName(targetName, true, null);
List<DatasourceConfig> datasources = routerConfig.getDatasources();
DatasourceConfig datasourceConfig = datasources.stream().filter(i -> i.getName().equals(datasourceName)).findFirst().orElseThrow((Supplier<Throwable>) () -> {
MycatException mycatException = new MycatException("can not found datasource " + datasourceName);
LOGGER.error("", mycatException);
return mycatException;
});
return read(migrateJdbcInput, tableName, schemaName, datasourceConfig.getUrl(), datasourceConfig.getUser(), datasourceConfig.getPassword());
}
use of io.mycat.config.DatasourceConfig 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();
}
use of io.mycat.config.DatasourceConfig in project Mycat2 by MyCATApache.
the class DatasourcePoolTest method main.
@SneakyThrows
public static void main(String[] args) {
DatasourceConfig datasourceConfig = new DatasourceConfig();
datasourceConfig.setDbType("mysql");
datasourceConfig.setUser("root");
datasourceConfig.setPassword("123456");
datasourceConfig.setName("prototypeDs");
datasourceConfig.setUrl("jdbc:mysql://localhost:3307/mysql");
datasourceConfig.setMaxCon(10);
Map<String, DatasourceConfig> datasources = Maps.of("prototypeDs", datasourceConfig);
MetaClusterCurrent.register(JdbcConnectionManager.class, new JdbcConnectionManager(datasources, new DruidDatasourceProvider()));
MetaClusterCurrent.register(IOExecutor.class, IOExecutor.DEFAULT);
MycatDatasourcePool jdbcDs = new JdbcDatasourcePoolImpl("prototypeDs");
NewMycatConnection newMycatConnection = jdbcDs.getConnection().toCompletionStage().toCompletableFuture().get();
Observable<VectorSchemaRoot> observable = newMycatConnection.prepareQuery("select 1", Collections.emptyList(), new RootAllocator());
List<VectorSchemaRoot> vectorSchemaRoots = observable.toList().blockingGet();
System.out.println();
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
MycatDatasourcePool nativeDs = new VertxMySQLDatasourcePoolImpl(datasourceConfig, "prototypeDs");
MycatRelDataType sqlMycatRelType = getSqlMycatRelDataType();
TestResult testResult = getTestResult(jdbcDs);
TestResult testResult2 = getTestResult(nativeDs);
// List<String> collect = newMycatConnection.prepareQuery(querySql, Collections.emptyList()).toList().blockingGet().stream().map(i -> i.toString()).collect(Collectors.toList());
MycatRowMetaData mycatRowMetaData1 = testResult.queryResult.getMycatRowMetaData();
MycatRowMetaData mycatRowMetaData2 = testResult2.queryResult.getMycatRowMetaData();
MycatRelDataType mycatRelDataType1 = mycatRowMetaData1.getMycatRelDataType();
MycatRelDataType mycatRelDataType2 = mycatRowMetaData2.getMycatRelDataType();
System.out.println(mycatRelDataType1);
System.out.println(mycatRelDataType2);
System.out.println("=====================================================================================================");
boolean equals = mycatRelDataType1.equals(mycatRelDataType2);
// Assert.assertTrue(equals);
equals = mycatRelDataType1.equals(sqlMycatRelType);
System.out.println(sqlMycatRelType);
System.out.println(mycatRelDataType1);
observable = newMycatConnection.prepareQuery("select * from testSchema.testColumnTable", Collections.emptyList(), new RootAllocator());
vectorSchemaRoots = observable.toList().blockingGet();
System.out.println();
}
Aggregations