private void removeMainSession(Session session) { InetSocketAddress remoteSocketAddress = session.getRemoteSocketAddress(); // If it was in failure mode,we don't remove closed session from list. if (this.failureMode) { log.warn( "Client in failure mode,we don't remove session " + SystemUtils.getRawAddress(remoteSocketAddress) + ":" + remoteSocketAddress.getPort()); return; } log.warn( "Remove a session: " + SystemUtils.getRawAddress(remoteSocketAddress) + ":" + remoteSocketAddress.getPort()); Queue<Session> sessionQueue = this.sessionMap.get(session.getRemoteSocketAddress()); if (null != sessionQueue) { sessionQueue.remove(session); if (sessionQueue.size() == 0) { this.sessionMap.remove(session.getRemoteSocketAddress()); } this.updateSessions(); } }
private void addMainSession(Session session) { InetSocketAddress remoteSocketAddress = session.getRemoteSocketAddress(); log.warn( "Add a session: " + SystemUtils.getRawAddress(remoteSocketAddress) + ":" + remoteSocketAddress.getPort()); Queue<Session> sessions = this.sessionMap.get(remoteSocketAddress); if (sessions == null) { sessions = new ConcurrentLinkedQueue<Session>(); Queue<Session> oldSessions = this.sessionMap.putIfAbsent(remoteSocketAddress, sessions); if (null != oldSessions) { sessions = oldSessions; } } // If it is in failure mode,remove closed session from list if (this.failureMode) { Iterator<Session> it = sessions.iterator(); while (it.hasNext()) { Session tmp = it.next(); if (tmp.isClosed()) { it.remove(); break; } } } sessions.offer(session); // Remove old session and close it while (sessions.size() > this.connectionPoolSize) { Session oldSession = sessions.poll(); ((MemcachedSession) oldSession).setAllowReconnect(false); oldSession.close(); } }
public void testOpenSelector() throws IOException { Selector selector = SystemUtils.openSelector(); assertNotNull(selector); assertTrue(selector.isOpen()); if (SystemUtils.isLinuxPlatform()) { final String pollClassName = selector.provider().getClass().getCanonicalName(); assertTrue( pollClassName.equals("sun.nio.ch.EPollSelectorProvider") || pollClassName.equals("sun.nio.ch.PollSelectorProvider")); } Selector selector2 = SystemUtils.openSelector(); ; assertNotSame(selector, selector2); selector.close(); selector2.close(); }
private void addStandbySession(Session session, InetSocketAddress mainNodeAddress) { InetSocketAddress remoteSocketAddress = session.getRemoteSocketAddress(); log.warn( "Add a standby session: " + SystemUtils.getRawAddress(remoteSocketAddress) + ":" + remoteSocketAddress.getPort() + " for " + SystemUtils.getRawAddress(mainNodeAddress) + ":" + mainNodeAddress.getPort()); List<Session> sessions = this.standbySessionMap.get(mainNodeAddress); if (sessions == null) { sessions = new CopyOnWriteArrayList<Session>(); List<Session> oldSessions = this.standbySessionMap.putIfAbsent(mainNodeAddress, sessions); if (null != oldSessions) { sessions = oldSessions; } } sessions.add(session); }
@Override public void onConnect(SelectionKey key) throws IOException { key.interestOps(key.interestOps() & ~SelectionKey.OP_CONNECT); ConnectFuture future = (ConnectFuture) key.attachment(); if (future == null || future.isCancelled()) { key.channel().close(); key.cancel(); return; } try { if (!((SocketChannel) key.channel()).finishConnect()) { future.failure( new IOException( "Connect to " + SystemUtils.getRawAddress( future.getInetSocketAddressWrapper().getInetSocketAddress()) + ":" + future.getInetSocketAddressWrapper().getInetSocketAddress().getPort() + " fail")); } else { key.attach(null); this.addSession( this.createSession( (SocketChannel) key.channel(), future.getInetSocketAddressWrapper())); future.setResult(Boolean.TRUE); } } catch (Exception e) { future.failure(e); key.cancel(); throw new IOException( "Connect to " + SystemUtils.getRawAddress( future.getInetSocketAddressWrapper().getInetSocketAddress()) + ":" + future.getInetSocketAddressWrapper().getInetSocketAddress().getPort() + " fail," + e.getMessage()); } }
public void send(final Command msg) throws MemcachedException { MemcachedSession session = (MemcachedSession) this.findSessionByKey(msg.getKey()); if (session == null) { throw new MemcachedException("There is no available connection at this moment"); } // If session was closed,try to use standby memcached node if (session.isClosed()) { session = this.findStandbySession(session); } if (session.isClosed()) { throw new MemcachedException( "Session(" + SystemUtils.getRawAddress(session.getRemoteSocketAddress()) + ":" + session.getRemoteSocketAddress().getPort() + ") has been closed"); } if (session.isAuthFailed()) { throw new MemcachedException("Auth failed to connection " + session.getRemoteSocketAddress()); } session.write(msg); }