Search in sources :

Example 26 with XaSqlConnection

use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.

the class XaTestSuite method beginDoubleTargetInsertCommit.

@Test
public void beginDoubleTargetInsertCommit(VertxTestContext testContext) throws Exception {
    clearData();
    XaSqlConnection baseXaSqlConnection = factory.apply(mySQLManager, xaLog);
    baseXaSqlConnection.begin().onComplete(event -> {
        Assertions.assertTrue(event.succeeded());
        Future<NewMycatConnection> ds1 = baseXaSqlConnection.getConnection("ds1");
        Future<NewMycatConnection> ds2 = baseXaSqlConnection.getConnection("ds2");
        CompositeFuture all = CompositeFuture.join(ds1.compose(connection -> {
            Future<SqlResult> future = connection.insert("INSERT INTO db1.travelrecord (id)\n" + "                       VALUES\n" + "                       (1);");
            return future.compose(rowSet -> {
                Assertions.assertEquals(1, rowSet.getAffectRows());
                return Future.succeededFuture(connection);
            });
        }), ds2.compose(connection -> {
            Future<SqlResult> future = connection.insert("INSERT INTO db1.travelrecord (id)\n" + "                       VALUES\n" + "                       (2);");
            return future.compose(rowSet -> {
                Assertions.assertEquals(1, rowSet.getAffectRows());
                return Future.succeededFuture(connection);
            });
        }));
        all.onComplete(event13 -> {
            Assertions.assertTrue(event13.succeeded());
            baseXaSqlConnection.commit().onComplete(event12 -> {
                Assertions.assertTrue(event12.succeeded());
                Assertions.assertFalse(baseXaSqlConnection.isInTransaction());
                Future<NewMycatConnection> connectionFuture = baseXaSqlConnection.getConnection("ds2");
                connectionFuture.compose(sqlConnection -> sqlConnection.query("select id from db1.travelrecord")).onComplete(event1 -> {
                    Assertions.assertTrue(event1.succeeded());
                    Assertions.assertEquals(1, event1.result().size());
                    testContext.completeNow();
                });
            });
        });
    });
}
Also used : CompositeFuture(io.vertx.core.CompositeFuture) VertxTestContext(io.vertx.junit5.VertxTestContext) Connection(java.sql.Connection) RowSet(io.mycat.newquery.RowSet) BiFunction(java.util.function.BiFunction) XaLog(cn.mycat.vertx.xa.XaLog) Disabled(org.junit.jupiter.api.Disabled) Function(java.util.function.Function) CompositeFuture(io.vertx.core.CompositeFuture) SimpleConfig(cn.mycat.vertx.xa.SimpleConfig) SQLException(java.sql.SQLException) SqlConnection(io.vertx.sqlclient.SqlConnection) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) AsyncResult(io.vertx.core.AsyncResult) MySQLManager(cn.mycat.vertx.xa.MySQLManager) JsonUtil(io.mycat.util.JsonUtil) NewMycatConnection(io.mycat.newquery.NewMycatConnection) XaLogImpl(cn.mycat.vertx.xa.impl.XaLogImpl) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) VertxExtension(io.vertx.junit5.VertxExtension) SqlResult(io.mycat.newquery.SqlResult) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcUtils(com.alibaba.druid.util.JdbcUtils) Row(io.vertx.sqlclient.Row) Assertions(org.junit.jupiter.api.Assertions) Handler(io.vertx.core.Handler) Collections(java.util.Collections) NewMycatConnection(io.mycat.newquery.NewMycatConnection) CompositeFuture(io.vertx.core.CompositeFuture) Future(io.vertx.core.Future) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) Test(org.junit.jupiter.api.Test)

Example 27 with XaSqlConnection

use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.

the class XaTestSuite method begin.

@Test
public void begin(VertxTestContext testContext) {
    XaSqlConnection baseXaSqlConnection = factory.apply(mySQLManager, xaLog);
    baseXaSqlConnection.begin().onComplete(new Handler<AsyncResult<Void>>() {

        @Override
        public void handle(AsyncResult<Void> event) {
            Assertions.assertEquals(baseXaSqlConnection.isInTransaction(), true);
            baseXaSqlConnection.close();
            testContext.completeNow();
        }
    });
}
Also used : AsyncResult(io.vertx.core.AsyncResult) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) Test(org.junit.jupiter.api.Test)

Example 28 with XaSqlConnection

use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.

the class DrdsRunnerHelper method getPlanImplementor.

@NotNull
public static PlanImplementor getPlanImplementor(MycatDataContext dataContext, Response response, DrdsSqlWithParams drdsSqlWithParams) {
    XaSqlConnection transactionSession = (XaSqlConnection) dataContext.getTransactionSession();
    List<Object> params = drdsSqlWithParams.getParams();
    return new ObservableColocatedImplementor(transactionSession, dataContext, drdsSqlWithParams, response);
}
Also used : ObservableColocatedImplementor(io.mycat.calcite.plan.ObservableColocatedImplementor) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with XaSqlConnection

use of cn.mycat.vertx.xa.XaSqlConnection 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();
    });
}
Also used : VertxExecuter(io.mycat.vertx.VertxExecuter) java.util(java.util) io.mycat(io.mycat) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) ResultSetWriter(io.mycat.beans.resultset.ResultSetWriter) io.vertx.core(io.vertx.core) RowSet(io.mycat.newquery.RowSet) LoggerFactory(org.slf4j.LoggerFactory) NonNull(io.reactivex.rxjava3.annotations.NonNull) SimpleBinaryWriterImpl(io.mycat.beans.resultset.SimpleBinaryWriterImpl) Function(java.util.function.Function) ExecuteType(io.mycat.ExecuteType) Observable(io.reactivex.rxjava3.core.Observable) SimpleTextWriterImpl(io.mycat.beans.resultset.SimpleTextWriterImpl) ResultSetMapping(io.mycat.vertx.ResultSetMapping) ObservableSource(io.reactivex.rxjava3.core.ObservableSource) SchemaBuilder(io.ordinate.engine.builder.SchemaBuilder) InnerType(io.ordinate.engine.schema.InnerType) Observer(io.reactivex.rxjava3.core.Observer) Logger(org.slf4j.Logger) NewMycatConnection(io.mycat.newquery.NewMycatConnection) MySQLServerSession(io.mycat.proxy.session.MySQLServerSession) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) SqlResult(io.mycat.newquery.SqlResult) VertxUtil(io.mycat.util.VertxUtil) ResultWriterUtil(io.ordinate.engine.util.ResultWriterUtil) Collectors(java.util.stream.Collectors) io.mycat.api.collector(io.mycat.api.collector) AtomicLong(java.util.concurrent.atomic.AtomicLong) Buffer(io.vertx.core.buffer.Buffer) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) Disposable(io.reactivex.rxjava3.disposables.Disposable) NewMycatConnection(io.mycat.newquery.NewMycatConnection) SqlResult(io.mycat.newquery.SqlResult) RowSet(io.mycat.newquery.RowSet) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection)

Example 30 with XaSqlConnection

use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.

the class AsyncMycatDataContextImpl method getConnection.

public synchronized Future<NewMycatConnection> getConnection(String key) {
    XaSqlConnection transactionSession = (XaSqlConnection) context.getTransactionSession();
    if (context.isInTransaction()) {
        return transactionConnnectionMap.computeIfAbsent(key, s -> transactionSession.getConnection(key));
    }
    MySQLManager mySQLManager = MetaClusterCurrent.wrapper(MySQLManager.class);
    Future<NewMycatConnection> connection = mySQLManager.getConnection(key);
    connnectionFutureCollection.add(connection);
    return connection;
}
Also used : MySQLManager(cn.mycat.vertx.xa.MySQLManager) NewMycatConnection(io.mycat.newquery.NewMycatConnection) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection)

Aggregations

XaSqlConnection (cn.mycat.vertx.xa.XaSqlConnection)34 AsyncResult (io.vertx.core.AsyncResult)26 Test (org.junit.jupiter.api.Test)26 Future (io.vertx.core.Future)21 NewMycatConnection (io.mycat.newquery.NewMycatConnection)20 Handler (io.vertx.core.Handler)18 MySQLManager (cn.mycat.vertx.xa.MySQLManager)17 RowSet (io.mycat.newquery.RowSet)16 XaLog (cn.mycat.vertx.xa.XaLog)15 VertxTestContext (io.vertx.junit5.VertxTestContext)15 BiFunction (java.util.function.BiFunction)15 SavepointSqlConnection (cn.mycat.vertx.xa.SavepointSqlConnection)14 SneakyThrows (lombok.SneakyThrows)14 Assert (org.junit.Assert)9 CompositeFuture (io.vertx.core.CompositeFuture)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 Function (java.util.function.Function)8 SqlResult (io.mycat.newquery.SqlResult)7 Row (io.vertx.sqlclient.Row)7 SqlConnection (io.vertx.sqlclient.SqlConnection)7