Search in sources :

Example 1 with MycatDataContextImpl

use of io.mycat.runtime.MycatDataContextImpl in project Mycat2 by MyCATApache.

the class SqlResultSetService method loadResultSet.

@SneakyThrows
private Optional<Observable<MysqlPayloadObject>> loadResultSet(SQLSelectStatement sqlSelectStatement) {
    return cache.get(sqlSelectStatement.toString(), () -> {
        if (!MetaClusterCurrent.exist(DrdsSqlCompiler.class)) {
            return Optional.empty();
        }
        MycatDataContext context = new MycatDataContextImpl();
        try {
            DrdsSqlWithParams drdsSql = DrdsRunnerHelper.preParse(sqlSelectStatement, context.getDefaultSchema());
            Plan plan = DrdsRunnerHelper.getPlan(drdsSql);
            XaSqlConnection transactionSession = (XaSqlConnection) context.getTransactionSession();
            AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = new AsyncMycatDataContextImpl.SqlMycatDataContextImpl(context, plan.getCodeExecuterContext(), drdsSql);
            PrepareExecutor prepare = MetaClusterCurrent.wrapper(ExecutorProvider.class).prepare(plan);
            Observable<MysqlPayloadObject> observable = prepare.getMysqlPayloadObjectObservable(prepare.getArrayBindable(), sqlMycatDataContext, plan.getMetaData());
            observable = observable.doOnTerminate(new Action() {

                @Override
                public void run() throws Throwable {
                    transactionSession.closeStatementState().onComplete(event -> context.close());
                }
            });
            List<MysqlPayloadObject> mysqlPayloadObjects = observable.toList().blockingGet();
            observable = Observable.fromIterable(mysqlPayloadObjects);
            return Optional.ofNullable(observable);
        } catch (Throwable t) {
            context.kill();
            log.error("", t);
            return Optional.empty();
        }
    });
}
Also used : PrepareExecutor(io.mycat.calcite.PrepareExecutor) MycatDataContextImpl(io.mycat.runtime.MycatDataContextImpl) Action(io.reactivex.rxjava3.functions.Action) Plan(io.mycat.calcite.spm.Plan) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) ExecutorProvider(io.mycat.calcite.ExecutorProvider) SneakyThrows(lombok.SneakyThrows)

Example 2 with MycatDataContextImpl

use of io.mycat.runtime.MycatDataContextImpl in project Mycat2 by MyCATApache.

the class MycatSessionManager method acceptNewSocketChannel.

@Override
public void acceptNewSocketChannel(Object keyAttachement, BufferPool bufPool, Selector nioSelector, SocketChannel frontChannel) throws IOException {
    MySQLClientAuthHandler mySQLClientAuthHandler = new MySQLClientAuthHandler(this);
    MycatSession mycat = new MycatSession(new MycatDataContextImpl(), bufPool, mySQLClientAuthHandler, this);
    // 用于monitor监控获取session
    SessionThread thread = (SessionThread) Thread.currentThread();
    thread.setCurSession(mycat);
    try {
        mycat.register(nioSelector, frontChannel, SelectionKey.OP_READ);
        MycatMonitor.onNewMycatSession(mycat);
        mySQLClientAuthHandler.sendAuthPackge(mycat);
        this.mycatSessions.add(mycat);
    } catch (Exception e) {
        MycatMonitor.onAuthHandlerWriteException(mycat, e);
        mycat.close(false, e);
    }
}
Also used : MycatDataContextImpl(io.mycat.runtime.MycatDataContextImpl) SessionThread(io.mycat.proxy.reactor.SessionThread) MySQLClientAuthHandler(io.mycat.proxy.handler.front.MySQLClientAuthHandler) IOException(java.io.IOException)

Aggregations

MycatDataContextImpl (io.mycat.runtime.MycatDataContextImpl)2 XaSqlConnection (cn.mycat.vertx.xa.XaSqlConnection)1 MysqlPayloadObject (io.mycat.api.collector.MysqlPayloadObject)1 ExecutorProvider (io.mycat.calcite.ExecutorProvider)1 PrepareExecutor (io.mycat.calcite.PrepareExecutor)1 Plan (io.mycat.calcite.spm.Plan)1 MySQLClientAuthHandler (io.mycat.proxy.handler.front.MySQLClientAuthHandler)1 SessionThread (io.mycat.proxy.reactor.SessionThread)1 Action (io.reactivex.rxjava3.functions.Action)1 IOException (java.io.IOException)1 SneakyThrows (lombok.SneakyThrows)1