@Override protected FrontendConnection getConnection(NetworkChannel channel) throws IOException { SystemConfig sys = MycatServer.getInstance().getConfig().getSystem(); ServerConnection c = new ServerConnection(channel); MycatServer.getInstance().getConfig().setSocketParams(c, true); c.setPrivileges(MycatPrivileges.instance()); // 每个新的ServerConnection都会绑定 // 一个新的ServerQueryHandler负责处理sql指令 c.setQueryHandler(new ServerQueryHandler(c)); // 一个ServerLoadDataInfileHandler负责处理文件载入命令 c.setLoadDataInfileHandler(new ServerLoadDataInfileHandler(c)); c.setPrepareHandler(new ServerPrepareHandler(c)); c.setTxIsolation(sys.getTxIsolation()); // 一个session负责处理事务 c.setSession2(new NonBlockingSession(c)); return c; }
public void setSocketParams(Connection con, boolean isFrontChannel) throws IOException { int sorcvbuf = 0; int sosndbuf = 0; int soNoDelay = 0; if (isFrontChannel) { sorcvbuf = netConfig.getFrontsocketsorcvbuf(); sosndbuf = netConfig.getFrontsocketsosndbuf(); soNoDelay = netConfig.getFrontSocketNoDelay(); } else { sorcvbuf = netConfig.getBacksocketsorcvbuf(); sosndbuf = netConfig.getBacksocketsosndbuf(); soNoDelay = netConfig.getBackSocketNoDelay(); } NetworkChannel channel = con.getChannel(); channel.setOption(StandardSocketOptions.SO_RCVBUF, sorcvbuf); channel.setOption(StandardSocketOptions.SO_SNDBUF, sosndbuf); channel.setOption(StandardSocketOptions.TCP_NODELAY, soNoDelay == 1); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); }
@SuppressWarnings({"unchecked", "rawtypes"}) public MySQLConnection make(MySQLDataSource pool, ResponseHandler handler, String schema) throws IOException { DBHostConfig dsc = pool.getConfig(); NetworkChannel channel = openSocketChannel(DbleServer.getInstance().isAIO()); MySQLConnection c = new MySQLConnection(channel, pool.isReadNode()); c.setSocketParams(false); c.setHost(dsc.getIp()); c.setPort(dsc.getPort()); c.setUser(dsc.getUser()); c.setPassword(dsc.getPassword()); c.setSchema(schema); c.setHandler(new MySQLConnectionAuthenticator(c, handler)); c.setPool(pool); c.setIdleTimeout(pool.getConfig().getIdleTimeout()); if (channel instanceof AsynchronousSocketChannel) { ((AsynchronousSocketChannel) channel).connect( new InetSocketAddress(dsc.getIp(), dsc.getPort()), c, (CompletionHandler) DbleServer.getInstance().getConnector()); } else { ((NIOConnector) DbleServer.getInstance().getConnector()).postConnect(c); } return c; }
public void setSocketParams(Connection con, boolean isFrontChannel) throws IOException { int sorcvbuf = 0; int sosndbuf = 0; int soNoDelay = 0; if (isFrontChannel) { sorcvbuf = netConfig.getFrontsocketsorcvbuf(); sosndbuf = netConfig.getFrontsocketsosndbuf(); soNoDelay = netConfig.getFrontSocketNoDelay(); } else { sorcvbuf = netConfig.getBacksocketsorcvbuf(); sosndbuf = netConfig.getBacksocketsosndbuf(); soNoDelay = netConfig.getBackSocketNoDelay(); } NetworkChannel channel = con.getChannel(); channel.setOption(StandardSocketOptions.SO_RCVBUF, sorcvbuf); channel.setOption(StandardSocketOptions.SO_SNDBUF, sosndbuf); channel.setOption(StandardSocketOptions.TCP_NODELAY, soNoDelay == 1); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); con.setMaxPacketSize(netConfig.getMaxPacketSize()); con.setPacketHeaderSize(netConfig.getPacketHeaderSize()); }
/** * {@inheritDoc} */ @Override public NetworkChannel bind(final SocketAddress local, final int backlog) throws IOException { if (!(local instanceof GroupSocketAddress)) { throw new IllegalArgumentException("GroupChannel can only bind to a GroupSocketAddress"); } final GroupSocketAddress group = (GroupSocketAddress) local; if (group.size() != channels.length) { throw new IllegalArgumentException("local.size() != channels.length"); } final int k = channels.length; for (int i = 0; i < k; i++) { channels[i].bind(group.get(i), backlog); } return this; }
@Override protected FrontendConnection getConnection(NetworkChannel channel) throws IOException { ManagerConnection c = new ManagerConnection(channel); MycatServer.getInstance().getConfig().setSocketParams(c, true); c.setPrivileges(MycatPrivileges.instance()); c.setQueryHandler(new ManagerQueryHandler(c)); return c; }
public void setSocketParams(AbstractConnection con, boolean isFrontChannel) throws IOException { int sorcvbuf = 0; int sosndbuf = 0; int soNoDelay = 0; if ( isFrontChannel ) { sorcvbuf = system.getFrontsocketsorcvbuf(); sosndbuf = system.getFrontsocketsosndbuf(); soNoDelay = system.getFrontSocketNoDelay(); } else { sorcvbuf = system.getBacksocketsorcvbuf(); sosndbuf = system.getBacksocketsosndbuf(); soNoDelay = system.getBackSocketNoDelay(); } NetworkChannel channel = con.getChannel(); channel.setOption(StandardSocketOptions.SO_RCVBUF, sorcvbuf); channel.setOption(StandardSocketOptions.SO_SNDBUF, sosndbuf); channel.setOption(StandardSocketOptions.TCP_NODELAY, soNoDelay == 1); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); con.setMaxPacketSize(system.getMaxPacketSize()); con.setPacketHeaderSize(system.getPacketHeaderSize()); con.setIdleTimeout(system.getIdleTimeout()); con.setCharset(system.getCharset()); }
protected NetworkChannel openSocketChannel(boolean isAIO) throws IOException { if (isAIO) { return AsynchronousSocketChannel .open(MycatServer.getInstance().getNextAsyncChannelGroup()); } else { SocketChannel channel = null; channel = SocketChannel.open(); channel.configureBlocking(false); return channel; } }
public FrontendConnection make(NetworkChannel channel) throws IOException { channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); FrontendConnection c = getConnection(channel); MycatServer.getInstance().getConfig().setSocketParams(c, true); return c; }
public AbstractConnection(NetworkChannel channel) { this.channel = channel; boolean isAIO = (channel instanceof AsynchronousChannel); if (isAIO) { socketWR = new AIOSocketWR(this); } else { socketWR = new NIOSocketWR(this); } this.isClosed = new AtomicBoolean(false); this.startupTime = TimeUtil.currentTimeMillis(); this.lastReadTime = startupTime; this.lastWriteTime = startupTime; }
private void accept(NetworkChannel channel, Long id) { try { FrontendConnection c = factory.make(channel); c.setAccepted(true); c.setId(id); NIOProcessor processor = MycatServer.getInstance().nextProcessor(); c.setProcessor(processor); c.register(); } catch (Exception e) { LOGGER.error("AioAcceptorError", e); closeChannel(channel); } }
private static void closeChannel(NetworkChannel channel) { if (channel == null) { return; } try { channel.close(); } catch (IOException e) { LOGGER.error("AioAcceptorError", e); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) public PostgreSQLBackendConnection make(PostgreSQLDataSource pool, ResponseHandler handler, final String schema) throws IOException { final DBHostConfig dsc = pool.getConfig(); NetworkChannel channel = this.openSocketChannel(MycatServer .getInstance().isAIO()); final PostgreSQLBackendConnection c = new PostgreSQLBackendConnection( channel, pool.isReadNode()); MycatServer.getInstance().getConfig().setSocketParams(c, false); // 设置NIOHandler c.setHandler(new PostgreSQLBackendConnectionHandler(c)); c.setHost(dsc.getIp()); c.setPort(dsc.getPort()); c.setUser(dsc.getUser()); c.setPassword(dsc.getPassword()); c.setSchema(schema); c.setPool(pool); c.setResponseHandler(handler); c.setIdleTimeout(pool.getConfig().getIdleTimeout()); if (channel instanceof AsynchronousSocketChannel) { ((AsynchronousSocketChannel) channel).connect( new InetSocketAddress(dsc.getIp(), dsc.getPort()), c, (CompletionHandler) MycatServer.getInstance() .getConnector()); } else { ((NIOConnector) MycatServer.getInstance().getConnector()) .postConnect(c); } return c; }
public MySQLConnection(NetworkChannel channel, boolean fromSlaveDB) { super(channel); this.clientFlags = CLIENT_FLAGS; this.lastTime = TimeUtil.currentTimeMillis(); this.isQuit = new AtomicBoolean(false); this.autocommit = true; this.fromSlaveDB = fromSlaveDB; // 设为默认值,免得每个初始化好的连接都要去同步一下 this.txIsolation = MycatServer.getInstance().getConfig().getSystem().getTxIsolation(); }
@SuppressWarnings({ "unchecked", "rawtypes" }) // 这里传入的ResponseHandler为DelegateResponseHandler,在连接建立验证之后,会调用 public MySQLConnection make(MySQLDataSource pool, ResponseHandler handler, String schema) throws IOException { // DBHost配置 DBHostConfig dsc = pool.getConfig(); // 根据是否为NIO返回SocketChannel或者AIO的AsynchronousSocketChannel NetworkChannel channel = openSocketChannel(MycatServer.getInstance() .isAIO()); // 新建MySQLConnection MySQLConnection c = new MySQLConnection(channel, pool.isReadNode()); // 根据配置初始化MySQLConnection MycatServer.getInstance().getConfig().setSocketParams(c, false); c.setHost(dsc.getIp()); c.setPort(dsc.getPort()); c.setUser(dsc.getUser()); c.setPassword(dsc.getPassword()); c.setSchema(schema); // 目前实际连接还未建立,handler为MySQL连接认证MySQLConnectionAuthenticatorHandler c.setHandler(new MySQLConnectionAuthenticatorHandler(c, handler)); c.setPool(pool); c.setIdleTimeout(pool.getConfig().getIdleTimeout()); // AIO和NIO连接方式建立实际的MySQL连接 if (channel instanceof AsynchronousSocketChannel) { ((AsynchronousSocketChannel) channel).connect( new InetSocketAddress(dsc.getIp(), dsc.getPort()), c, (CompletionHandler) MycatServer.getInstance() .getConnector()); } else { // 通过NIOConnector建立连接 // 通过NIOConnector建立实际连接的过程与前端连接的建立相似, // 也是先放在队列中,之后由NIOConnector去建立连接 ((NIOConnector) MycatServer.getInstance().getConnector()) .postConnect(c); } return c; }
@Override protected FrontendConnection getConnection(NetworkChannel channel) throws IOException { ManagerConnection c = new ManagerConnection(channel); c.setSocketParams(true); c.setPrivileges(ManagerPrivileges.instance()); c.setHandler(new ManagerAuthenticator(c)); c.setQueryHandler(new ManagerQueryHandler(c)); return c; }
public ServerConnection(NetworkChannel channel) throws IOException { super(channel); this.txInterrupted = false; this.autocommit = true; this.txID = new AtomicLong(1); this.sptprepare = new ServerSptPrepare(this); this.usrVariables = new LinkedHashMap<>(); this.sysVariables = new LinkedHashMap<>(); }
@Override protected FrontendConnection getConnection(NetworkChannel channel) throws IOException { ServerConnection c = new ServerConnection(channel); c.setSocketParams(true); c.setPrivileges(ServerPrivileges.instance()); c.setQueryHandler(new ServerQueryHandler(c)); c.setLoadDataInfileHandler(new ServerLoadDataInfileHandler(c)); c.setPrepareHandler(new ServerPrepareHandler(c)); SystemConfig sys = DbleServer.getInstance().getConfig().getSystem(); c.setTxIsolation(sys.getTxIsolation()); c.setSession2(new NonBlockingSession(c)); return c; }
protected NetworkChannel openSocketChannel(boolean isAIO) throws IOException { if (isAIO) { return AsynchronousSocketChannel.open(DbleServer.getInstance().getNextAsyncChannelGroup()); } else { SocketChannel channel = null; channel = SocketChannel.open(); channel.configureBlocking(false); return channel; } }
public FrontendConnection make(NetworkChannel channel) throws IOException { channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); FrontendConnection c = getConnection(channel); c.setSocketParams(true); return c; }
public MySQLConnection(NetworkChannel channel, boolean fromSlaveDB) { super(channel); this.clientFlags = CLIENT_FLAGS; this.lastTime = TimeUtil.currentTimeMillis(); this.isQuit = new AtomicBoolean(false); this.autocommit = true; this.fromSlaveDB = fromSlaveDB; /* if the txIsolation in server.xml is different from the isolation level in MySQL node, * it need to sync the status firstly for new idle connection*/ this.txIsolation = -1; this.complexQuery = false; this.usrVariables = new LinkedHashMap<>(); this.sysVariables = new LinkedHashMap<>(); }
public static void main(String argv[]) { Socket s = new Socket(); s.getInputStream(); MulticastSocket ms = new MulticastSocket(); ms.close(); DatagramSocket ds = new DatagramSocket(); ds.close(); InetSocketAddress isa = new MyInetSocketAddress(); isa.isUnresolved(); NetworkChannel nc = new MyNetworkChannel(); nc.getLocalAddress(); MulticastChannel mc = new MyMulticastChannel(); mc.close(); DatagramChannel dc = new MyDatagramChannel(); dc.getLocalAddress(); AsynchronousSocketChannel asc = new MyAsynchronousSocketChannel(); asc.getLocalAddress(); SocketChannel sc = new MySocketChannel(); sc.isConnected(); ServerSocket ss = new ServerSocket(); ss.close(); AsynchronousServerSocketChannel assd = new MyAsynchronousServerSocketChannel(); assd.getLocalAddress(); ServerSocketChannel ssc = new MyServerSocketChannel(); ssc.getLocalAddress(); }
/** * {@inheritDoc} */ @Override public NetworkChannel bind(final SocketAddress local, final int backlog) throws IOException { assert isOpen(); channel.bind(local, backlog); selector.bind(channel, this); return this; }
public static String info (NetworkChannel ch) { try { return ch.getLocalAddress ().toString (); } catch (IOException e) { return ch.toString (); } }
public ManagerConnection(NetworkChannel channel) throws IOException { super(channel); }
public ServerConnection(NetworkChannel channel) throws IOException { super(channel); this.txInterrupted = false; this.autocommit = true; }
protected abstract FrontendConnection getConnection(NetworkChannel channel) throws IOException;
public NetworkChannel getChannel() { return channel; }
public BackendAIOConnection(NetworkChannel channel) { super(channel); }
public PostgreSQLBackendConnection(NetworkChannel channel, boolean fromSlaveDB) { super(channel); this.fromSlaveDB = fromSlaveDB; }
private void setChannelOptions(final NetworkChannel channel) throws IOException { channel.setOption(StandardSocketOptions.SO_RCVBUF, PeerConnectionController.SO_RCVBUF_VALUE); }
@Override public void apply(Object channel, T value) throws IOException { ((NetworkChannel)channel).setOption(option, value); }
@Override public T lookup(Object channel) throws IOException { return ((NetworkChannel)channel).getOption(option); }
/** * @see NetworkChannel#bind(SocketAddress) */ @Nonnull NetworkChannel bind(@Nullable SocketAddress local) throws IOException;
/** * The {@code backlog} parameter is the maximum number of pending * connections on the socket. Its exact semantics are implementation * specific. In particular, an implementation may impose a maximum length or * may choose to ignore the parameter altogether. If the {@code backlog} * parameter has the value {@code 0}, or a negative value, then an * implementation specific default is used. * * @see NetworkChannel#bind(SocketAddress) */ @Nonnull NetworkChannel bind(@Nullable SocketAddress local, int backlog) throws IOException;