Search in sources :

Example 1 with Session

use of io.mycat.proxy.session.Session in project Mycat2 by MyCATApache.

the class NIOAcceptor method processReadKey.

/**
 * 仅后台维护的主动创建的连接使用
 */
@SuppressWarnings("unchecked")
protected void processReadKey(SelectionKey curKey) throws IOException {
    // only from cluster server socket
    Session session = (Session) curKey.attachment();
    assert session != null;
    session.getCurNIOHandler().onSocketRead(session);
}
Also used : Session(io.mycat.proxy.session.Session)

Example 2 with Session

use of io.mycat.proxy.session.Session in project Mycat2 by MyCATApache.

the class NIOAcceptor method processWriteKey.

/**
 * 仅后台维护的主动创建的连接使用
 */
protected void processWriteKey(SelectionKey curKey) throws IOException {
    // only from cluster server socket
    Session session = (Session) curKey.attachment();
    assert session != null;
    session.getCurNIOHandler().onSocketWrite(session);
}
Also used : Session(io.mycat.proxy.session.Session)

Example 3 with Session

use of io.mycat.proxy.session.Session in project Mycat2 by MyCATApache.

the class MycatMonitorCallback method getSession.

static Session getSession() {
    SessionThread thread = getThread();
    Session curSession = thread.getCurSession();
    if (curSession instanceof MycatSession) {
        return curSession;
    } else if (curSession instanceof MySQLClientSession) {
        MycatSession mycatSession = ((MySQLClientSession) curSession).getMycat();
        if (mycatSession == null) {
            return curSession;
        } else {
            return mycatSession;
        }
    } else {
        throw new MycatException("unknown session");
    }
}
Also used : MycatSession(io.mycat.proxy.session.MycatSession) SessionThread(io.mycat.proxy.reactor.SessionThread) MycatException(io.mycat.MycatException) MySQLClientSession(io.mycat.proxy.session.MySQLClientSession) MycatSession(io.mycat.proxy.session.MycatSession) Session(io.mycat.proxy.session.Session) MySQLClientSession(io.mycat.proxy.session.MySQLClientSession)

Example 4 with Session

use of io.mycat.proxy.session.Session in project Mycat2 by MyCATApache.

the class ProxyReactorThread method run.

public void run() {
    while (!this.isInterrupted()) {
        try {
            pendingJobsEmpty = pendingJobs.isEmpty();
            long startTime = System.nanoTime();
            if (pendingJobsEmpty) {
                // /////////////epoll///////////////////
                int numOfKeys = selector.select(SELECTOR_TIMEOUT);
                // ////////////////////////////////
                if (numOfKeys == 0) {
                    long dis = System.nanoTime() - startTime;
                    if (dis < (SELECTOR_TIMEOUT / 2)) {
                        invalidSelectCount++;
                    }
                }
            // //////epoll///////////
            } else {
                selector.selectNow();
            }
            updateLastActiveTime();
            final Set<SelectionKey> keys = selector.selectedKeys();
            if (keys.isEmpty()) {
                if (!pendingJobsEmpty) {
                    ioTimes = 0;
                    this.processNIOJob();
                }
                continue;
            } else if ((ioTimes > 5) & !pendingJobsEmpty) {
                ioTimes = 0;
                this.processNIOJob();
            }
            ioTimes++;
            for (final SelectionKey key : keys) {
                try {
                    if (!key.isValid() || !key.channel().isOpen()) {
                        continue;
                    }
                    int readdyOps = key.readyOps();
                    setCurSession(null);
                    // 如果当前收到连接请求
                    if ((readdyOps & SelectionKey.OP_ACCEPT) != 0) {
                        processAcceptKey(key);
                    } else // 如果当前连接事件
                    if ((readdyOps & SelectionKey.OP_CONNECT) != 0) {
                        this.processConnectKey(key);
                    } else if ((readdyOps & SelectionKey.OP_READ) != 0) {
                        this.processReadKey(key);
                    } else if ((readdyOps & SelectionKey.OP_WRITE) != 0) {
                        this.processWriteKey(key);
                    }
                } catch (Exception e) {
                    // 如果设置为IOException方便调试,避免吞没其他类型异常
                    LOGGER.error("", e);
                    Session curSession = getCurSession();
                    if (curSession != null) {
                        NIOHandler curNIOHandler = curSession.getCurNIOHandler();
                        if (curNIOHandler != null) {
                            curNIOHandler.onException(curSession, e);
                        } else {
                            curSession.close(false, curSession.setLastMessage(e));
                        }
                        setCurSession(null);
                    }
                }
            }
            keys.clear();
            // ///////epoll //////////
            if (invalidSelectCount > 512) {
                Selector newSelector = SelectorUtil.rebuildSelector(this.selector);
                if (newSelector != null) {
                    this.selector = newSelector;
                }
                invalidSelectCount = 0;
            }
        // ///////epoll//////////
        } catch (ClosedSelectorException e) {
            LOGGER.warn("selector is closed");
            break;
        } catch (Throwable e) {
            LOGGER.warn("Unknown session:{}", getCurSession(), e);
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) NIOHandler(io.mycat.proxy.handler.NIOHandler) BackendNIOHandler(io.mycat.proxy.handler.BackendNIOHandler) IOException(java.io.IOException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) ConnectException(java.net.ConnectException) Session(io.mycat.proxy.session.Session) Selector(java.nio.channels.Selector) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Aggregations

Session (io.mycat.proxy.session.Session)4 MycatException (io.mycat.MycatException)1 BackendNIOHandler (io.mycat.proxy.handler.BackendNIOHandler)1 NIOHandler (io.mycat.proxy.handler.NIOHandler)1 SessionThread (io.mycat.proxy.reactor.SessionThread)1 MySQLClientSession (io.mycat.proxy.session.MySQLClientSession)1 MycatSession (io.mycat.proxy.session.MycatSession)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 ClosedSelectorException (java.nio.channels.ClosedSelectorException)1 SelectionKey (java.nio.channels.SelectionKey)1 Selector (java.nio.channels.Selector)1