use of io.mycat.newquery.NewMycatConnection in project Mycat2 by MyCATApache.
the class MycatViewPlan method execute.
@Override
public Observable<VectorSchemaRoot> execute(RootContext rootContext) {
RootAllocator rootAllocator = new RootAllocator();
NewMycatDataContext context = (NewMycatDataContext) rootContext.getContext();
DrdsSqlWithParams drdsSql = context.getDrdsSql();
MycatView view = (MycatView) mycatView;
CopyMycatRowMetaData rowMetaData = new CopyMycatRowMetaData(new CalciteRowMetaData(view.getRowType().getFieldList()));
MycatRelDatasourceSourceInfo mycatRelDatasourceSourceInfo = new MycatRelDatasourceSourceInfo(rowMetaData, view.getSQLTemplate(DrdsSql.isForUpdate(drdsSql.getParameterizedStatement())), view);
SqlNode sqlTemplate = mycatRelDatasourceSourceInfo.getSqlTemplate();
List<PartitionGroup> partitionGroups = AsyncMycatDataContextImpl.getSqlMap(Collections.emptyMap(), view, drdsSql, drdsSql.getHintDataNodeFilter());
ImmutableMultimap<String, SqlString> stringSqlStringImmutableMultimap = view.apply(-1, sqlTemplate, partitionGroups, drdsSql.getParams());
List<Observable<VectorSchemaRoot>> observableList = new ArrayList<>();
for (Map.Entry<String, SqlString> entry : stringSqlStringImmutableMultimap.entries()) {
String key = entry.getKey();
SqlString sqlString = entry.getValue();
observableList.add(Observable.create(emitter -> {
Future<NewMycatConnection> connectionFuture = context.getConnection(context.getContext().resolveDatasourceTargetName(key));
Future<Observable<VectorSchemaRoot>> observableFuture = connectionFuture.map(connection -> {
Observable<VectorSchemaRoot> observable = connection.prepareQuery(sqlString.getSql(), MycatPreparedStatementUtil.extractParams(drdsSql.getParams(), sqlString.getDynamicParameters()), MycatRelDataType.getMycatRelType(MycatViewPlan.this.schema()), rootAllocator);
return observable.doOnComplete(() -> context.recycleConnection(context.getContext().resolveDatasourceTargetName(key), Future.succeededFuture(connection)));
});
observableFuture.onFailure(event -> emitter.tryOnError(event));
observableFuture.onSuccess(event -> {
event = event.doOnComplete(() -> emitter.onComplete());
event = event.doOnError(throwable -> emitter.tryOnError(throwable));
event.forEach(vectorSchemaRoot -> emitter.onNext(vectorSchemaRoot));
});
}));
}
return Observable.fromIterable(observableList).flatMap(i -> i);
}
use of io.mycat.newquery.NewMycatConnection in project Mycat2 by MyCATApache.
the class MycatNativeDatasourcePool method getConnection.
@Override
public Future<NewMycatConnection> getConnection() {
return vertxPoolConnection.getConnection().map(connection -> {
DatabaseInstanceEntry stat = DatabaseInstanceEntry.stat(targetName);
stat.plusCon();
stat.plusQps();
return new VertxMycatConnectionPool(targetName, connection, vertxPoolConnection) {
long start;
@Override
public void onSend() {
start = System.currentTimeMillis();
onActiveTimestamp(start);
}
@Override
public void onRev() {
long end = System.currentTimeMillis();
onActiveTimestamp(end);
InstanceMonitor.plusPrt(end - start);
}
@Override
public Future<Void> close() {
stat.decCon();
return super.close();
}
@Override
public Observable<VectorSchemaRoot> prepareQuery(String sql, List<Object> params, BufferAllocator allocator) {
return Observable.create(emitter -> {
JdbcDatasourcePoolImpl jdbcDatasourcePool = new JdbcDatasourcePoolImpl(targetName);
Future<NewMycatConnection> connectionFuture = jdbcDatasourcePool.getConnection();
connectionFuture.onSuccess(event -> {
Observable<VectorSchemaRoot> observable = event.prepareQuery(sql, params, allocator);
observable.subscribe(vectorSchemaRoot -> emitter.onNext(vectorSchemaRoot), throwable -> emitter.onError(throwable), () -> emitter.onComplete());
});
connectionFuture.onFailure(event -> emitter.onError(event));
});
}
@Override
public Future<List<Object>> call(String sql) {
JdbcDatasourcePoolImpl jdbcDatasourcePool = new JdbcDatasourcePoolImpl(targetName);
Future<NewMycatConnection> connectionFuture = jdbcDatasourcePool.getConnection();
return connectionFuture.flatMap(connection -> connection.call(sql));
}
};
});
}
use of io.mycat.newquery.NewMycatConnection in project Mycat2 by MyCATApache.
the class ReceiverImpl method proxyProcedure.
@Override
public Future<Void> proxyProcedure(String sql, String targetName) {
targetName = dataContext.resolveDatasourceTargetName(targetName, true);
XaSqlConnection transactionSession = (XaSqlConnection) dataContext.getTransactionSession();
Future<NewMycatConnection> mySQLManagerConnection = transactionSession.getConnection(targetName);
Future<List<Object>> objectFuture = mySQLManagerConnection.flatMap(newMycatConnection -> {
Future<List<Object>> call = newMycatConnection.call(sql);
return (Future) call;
});
Future<List<Object>> rowBaseIteratorFuture = objectFuture.map(objects -> objects.stream().map(o -> {
if (o instanceof long[])
return o;
if (o instanceof SqlResult)
return ((SqlResult) o).toLongs();
if (o instanceof RowSet)
return ((RowSet) o).toRowBaseIterator();
throw new UnsupportedOperationException();
}).collect(Collectors.toList()));
return rowBaseIteratorFuture.flatMap(objectList -> {
if (objectList instanceof List) {
List list = (List) objectList;
int thisStmtResultSetSize = list.size();
int resultSetCounter = getResultSetCounter();
resetResultSetCounter(resultSetCounter + thisStmtResultSetSize - 1);
Future<Void> future = Future.succeededFuture();
for (Object o : list) {
if (o instanceof long[]) {
long[] r = (long[]) o;
future = future.flatMap(unused -> sendOk(r[0], r[1]));
} else if (o instanceof RowBaseIterator) {
RowBaseIterator rs = (RowBaseIterator) o;
future = future.flatMap(unused -> sendResultSet(rs));
}
return future;
}
}
throw new UnsupportedOperationException();
});
}
use of io.mycat.newquery.NewMycatConnection in project Mycat2 by MyCATApache.
the class BaseSavepointSuite method baseSavepointReleaseSavepointInTwoConnection.
@Test
@SneakyThrows
public void baseSavepointReleaseSavepointInTwoConnection(VertxTestContext testContext) {
mySQLManager.getConnection("ds1").flatMap(connection -> {
return connection.update("delete FROM `db1`.`travelrecord`").map(u -> connection);
}).flatMap(c -> c.close()).toCompletionStage().toCompletableFuture().get();
mySQLManager.getConnection("ds2").flatMap(connection -> {
return connection.update("delete FROM `db1`.`travelrecord`").map(u -> connection);
}).flatMap(c -> c.close()).toCompletionStage().toCompletableFuture().get();
XaSqlConnection baseXaSqlConnection = factory.apply(mySQLManager, xaLog);
Assert.assertTrue(baseXaSqlConnection instanceof SavepointSqlConnection);
SavepointSqlConnection savepointSqlConnection = (SavepointSqlConnection) baseXaSqlConnection;
baseXaSqlConnection.begin().onComplete(new Handler<AsyncResult<Void>>() {
@Override
@SneakyThrows
public void handle(AsyncResult<Void> event) {
NewMycatConnection ds1 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
ds1.insert("insert into `db1`.`travelrecord` (`id`) values ('2')").toCompletionStage().toCompletableFuture().get();
NewMycatConnection ds2 = savepointSqlConnection.getConnection("ds2").toCompletionStage().toCompletableFuture().get();
ds2.insert("insert into `db1`.`travelrecord` (`id`) values ('2')").toCompletionStage().toCompletableFuture().get();
savepointSqlConnection.createSavepoint("sss").toCompletionStage().toCompletableFuture().get();
RowSet objects = ds1.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() > 0);
RowSet objects2 = ds2.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects2.size() > 0);
Assert.assertEquals("[sss]", savepointSqlConnection.getExistedSavepoints().toString());
savepointSqlConnection.releaseSavepoint("sss").toCompletionStage().toCompletableFuture().get();
objects = ds1.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() > 0);
objects2 = ds2.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects2.size() > 0);
Assert.assertEquals("[]", savepointSqlConnection.getExistedSavepoints().toString());
Assert.assertTrue(savepointSqlConnection.isInTransaction());
ds1 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
objects = ds1.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() > 0);
ds2 = savepointSqlConnection.getConnection("ds2").toCompletionStage().toCompletableFuture().get();
objects = ds2.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() > 0);
savepointSqlConnection.commit().toCompletionStage().toCompletableFuture().get();
Assert.assertEquals("[]", savepointSqlConnection.getExistedSavepoints().toString());
Assert.assertFalse(savepointSqlConnection.isInTransaction());
ds1 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
objects = ds1.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() > 0);
ds2 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
objects = ds2.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() > 0);
savepointSqlConnection.close();
testContext.completeNow();
}
});
}
use of io.mycat.newquery.NewMycatConnection in project Mycat2 by MyCATApache.
the class BaseSavepointSuite method baseSavepointRollbackInSingleConnection.
@Test
@SneakyThrows
public void baseSavepointRollbackInSingleConnection(VertxTestContext testContext) {
mySQLManager.getConnection("ds1").flatMap(connection -> {
return connection.update("delete FROM `db1`.`travelrecord`").map(u -> connection);
}).flatMap(c -> c.close()).toCompletionStage().toCompletableFuture().get();
XaSqlConnection baseXaSqlConnection = factory.apply(mySQLManager, xaLog);
Assert.assertTrue(baseXaSqlConnection instanceof SavepointSqlConnection);
SavepointSqlConnection savepointSqlConnection = (SavepointSqlConnection) baseXaSqlConnection;
baseXaSqlConnection.begin().onComplete(new Handler<AsyncResult<Void>>() {
@Override
@SneakyThrows
public void handle(AsyncResult<Void> event) {
savepointSqlConnection.createSavepoint("sss").toCompletionStage().toCompletableFuture().get();
NewMycatConnection ds1 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
ds1.insert("insert into `db1`.`travelrecord` (`id`) values ('2')").toCompletionStage().toCompletableFuture().get();
RowSet objects = ds1.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() > 0);
Assert.assertEquals("[sss]", savepointSqlConnection.getExistedSavepoints().toString());
savepointSqlConnection.rollback().toCompletionStage().toCompletableFuture().get();
Assert.assertEquals("[]", savepointSqlConnection.getExistedSavepoints().toString());
ds1 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
objects = ds1.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() == 0);
savepointSqlConnection.close();
testContext.completeNow();
}
});
}
Aggregations