Search in sources :

Example 1 with ClosedSelectorException

use of java.nio.channels.ClosedSelectorException in project voltdb by VoltDB.

the class SocketJoiner method runPrimary.

/*
     * After startup everything is a primary and can accept
     * new nodes into the cluster. This loop accepts the new socket
     * and passes it off the HostMessenger via the JoinHandler interface
     */
private void runPrimary() throws Exception {
    try {
        // start the server socket on the right interface
        doBind();
        while (true) {
            try {
                final int selectedKeyCount = m_selector.select();
                if (selectedKeyCount == 0)
                    continue;
                Set<SelectionKey> selectedKeys = m_selector.selectedKeys();
                try {
                    for (SelectionKey key : selectedKeys) {
                        processSSC((ServerSocketChannel) key.channel());
                    }
                } finally {
                    selectedKeys.clear();
                }
            } catch (ClosedByInterruptException e) {
                throw new InterruptedException();
            } catch (ClosedSelectorException e) {
                throw new InterruptedException();
            } catch (Exception e) {
                LOG.error("fault occurrent in the connection accept loop", e);
            }
        }
    } finally {
        for (ServerSocketChannel ssc : m_listenerSockets) {
            try {
                ssc.close();
            } catch (IOException e) {
            }
        }
        m_listenerSockets.clear();
        try {
            m_selector.close();
        } catch (IOException e) {
        }
        m_selector = null;
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) SelectionKey(java.nio.channels.SelectionKey) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) JSONException(org.json_voltpatches.JSONException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 2 with ClosedSelectorException

use of java.nio.channels.ClosedSelectorException in project robovm by robovm.

the class ClosedSelectorExceptionTest method test_Constructor.

/**
     * @tests {@link java.nio.channels.ClosedSelectorException#ClosedSelectorException()}
     */
public void test_Constructor() {
    ClosedSelectorException e = new ClosedSelectorException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
Also used : ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 3 with ClosedSelectorException

use of java.nio.channels.ClosedSelectorException in project robovm by robovm.

the class SelectorTest method test_selectedKeys.

/**
     * @tests java.nio.channels.Selector#keys()
     */
public void test_selectedKeys() throws IOException {
    SocketChannel sc = SocketChannel.open();
    ssc.register(selector, SelectionKey.OP_ACCEPT);
    try {
        int count = 0;
        sc.connect(localAddress);
        count = blockingSelect(SelectType.NULL, 0);
        assertEquals(1, count);
        Set<SelectionKey> selectedKeys = selector.selectedKeys();
        Set<SelectionKey> selectedKeys2 = selector.selectedKeys();
        assertSame(selectedKeys, selectedKeys2);
        assertEquals(1, selectedKeys.size());
        assertEquals(ssc.keyFor(selector), selectedKeys.iterator().next());
        // add one key into selectedKeys
        try {
            selectedKeys.add(ssc.keyFor(selector));
            fail("Should throw UnsupportedOperationException");
        } catch (UnsupportedOperationException e) {
        // expected
        }
        // no exception should be thrown
        selectedKeys.clear();
        Set<SelectionKey> selectedKeys3 = selector.selectedKeys();
        assertSame(selectedKeys, selectedKeys3);
        ssc.keyFor(selector).cancel();
        assertEquals(0, selectedKeys.size());
        selector.close();
        try {
            selector.selectedKeys();
            fail("should throw ClosedSelectorException");
        } catch (ClosedSelectorException e) {
        // expected
        }
    } finally {
        sc.close();
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 4 with ClosedSelectorException

use of java.nio.channels.ClosedSelectorException in project jdk8u_jdk by JetBrains.

the class WindowsSelectorImpl method implRegister.

protected void implRegister(SelectionKeyImpl ski) {
    synchronized (closeLock) {
        if (pollWrapper == null)
            throw new ClosedSelectorException();
        growIfNeeded();
        channelArray[totalChannels] = ski;
        ski.setIndex(totalChannels);
        fdMap.put(ski);
        keys.add(ski);
        pollWrapper.addEntry(totalChannels, ski);
        totalChannels++;
    }
}
Also used : ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 5 with ClosedSelectorException

use of java.nio.channels.ClosedSelectorException in project geode by apache.

the class AcceptorImpl method runSelectorLoop.

public void runSelectorLoop() {
    // int zeroEventsCount = 0;
    try {
        logger.info(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_SELECTOR_ENABLED));
        while (this.selector.isOpen() && !Thread.currentThread().isInterrupted()) {
            {
                SystemFailure.checkFailure();
                // this.cache.getDistributedSystem().getCancelCriterion().checkCancelInProgress(null);
                if (this.cache.isClosed()) {
                    // TODO should just ask cache's CancelCriterion
                    break;
                }
                if (this.cache.getCancelCriterion().isCancelInProgress()) {
                    break;
                }
                ServerConnection sc;
                registeredKeys = checkRegisteredKeys(registeredKeys);
                if (registeredKeys == 0) {
                    // do blocking wait on queue until we get some guys registered
                    // with the selector
                    sc = (ServerConnection) this.selectorQueue.take();
                } else {
                    // we already have some guys registered so just do a poll on queue
                    sc = (ServerConnection) this.selectorQueue.poll();
                }
                while (sc != null) {
                    try {
                        sc.registerWithSelector2(this.selector);
                        registeredKeys++;
                        this.selectorRegistrations.add(sc);
                    } catch (ClosedChannelException cce) {
                        // for bug bug 38474
                        finishCon(sc);
                    } catch (IOException ex) {
                        finishCon(sc);
                        logger.warn(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_IGNORING, ex));
                    } catch (RuntimeException ex) {
                        finishCon(sc);
                        logger.warn(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_IGNORING, ex));
                    }
                    sc = (ServerConnection) this.selectorQueue.poll();
                }
            }
            if (registeredKeys == 0) {
                continue;
            }
            int events = this.selector.select();
            // select() could have returned due to wakeup() during close of cache
            if (this.cache.getCancelCriterion().isCancelInProgress()) {
                break;
            }
            if (events == 0) {
                // zeroEventsCount++;
                // if (zeroEventsCount > 0) {
                // zeroEventsCount = 0;
                checkForStuckKeys();
            // try {
            // this.selector.close(); // this selector is sick!
            // } catch (IOException ignore) {
            // }
            // this.selector = Selector.open();
            // {
            // Iterator it = selectorRegistrations.iterator();
            // while (it.hasNext()) {
            // ServerConnection sc = (ServerConnection)it.next();
            // sc.registerWithSelector2(this.selector);
            // }
            // }
            // }
            // ArrayList al = new ArrayList();
            // Iterator keysIt = this.selector.keys().iterator();
            // while (keysIt.hasNext()) {
            // SelectionKey sk = (SelectionKey)keysIt.next();
            // al.add(sk.attachment());
            // sk.cancel();
            // }
            // events = this.selector.selectNow();
            // Iterator alIt = al.iterator();
            // while (alIt.hasNext()) {
            // ServerConnection sc = (ServerConnection)alIt.next();
            // sc.registerWithSelector2(this.selector);
            // }
            // events = this.selector.select();
            // } else {
            // zeroEventsCount = 0;
            }
            while (events > 0) {
                int cancelCount = 0;
                Set sk = this.selector.selectedKeys();
                if (sk == null) {
                    // something really bad has happened I'm not even sure this is possible
                    // but lhughes so an NPE during close one time so perhaps it can happen
                    // during selector close.
                    events = 0;
                    break;
                }
                Iterator keysIterator = sk.iterator();
                while (keysIterator.hasNext()) {
                    SelectionKey key = (SelectionKey) keysIterator.next();
                    // Remove the key from the selector's selectedKeys
                    keysIterator.remove();
                    final ServerConnection sc = (ServerConnection) key.attachment();
                    try {
                        if (key.isValid() && key.isReadable()) {
                            // this is the only event we currently register for
                            try {
                                key.cancel();
                                this.selectorRegistrations.remove(sc);
                                registeredKeys--;
                                cancelCount++;
                                sc.makeBlocking();
                                // we need to say we are processing a message
                                // so that that client health monitor will not
                                // kill us while we wait for a thread in the thread pool.
                                // This is also be used to determine how long we are
                                // in the thread pool queue and to cancel operations that
                                // have waited too long in the queue.
                                sc.setProcessingMessage();
                            } catch (ClosedChannelException ignore) {
                                finishCon(sc);
                                continue;
                            } catch (IOException ex) {
                                finishCon(sc);
                                if (isRunning()) {
                                    logger.warn(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_UNEXPECTED, ex));
                                }
                                continue;
                            }
                            try {
                                AcceptorImpl.this.stats.incThreadQueueSize();
                                AcceptorImpl.this.pool.execute(sc);
                            } catch (RejectedExecutionException rejected) {
                                finishCon(sc);
                                AcceptorImpl.this.stats.decThreadQueueSize();
                                if (!isRunning()) {
                                    break;
                                }
                                logger.warn(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_UNEXPECTED, rejected));
                            }
                        // } else if (key.isValid() && key.isConnectable()) {
                        // logger.info("DEBUG isConnectable and isValid key=" + key);
                        // finishCon(sc);
                        } else {
                            finishCon(sc);
                            if (key.isValid()) {
                                logger.warn(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_IGNORING_EVENT_ON_SELECTOR_KEY__0, key));
                            // } else {
                            // logger.info("DEBUG !isValid key=" + key);
                            }
                        }
                    } catch (CancelledKeyException ex) {
                        // fix for bug 37739
                        finishCon(sc);
                    }
                }
                if (cancelCount > 0 && this.selector.isOpen()) {
                    // we need to do a select to cause the cancel to be unregisters.
                    events = this.selector.selectNow();
                } else {
                    events = 0;
                }
            }
        }
    } catch (InterruptedException ex) {
        // allow this thread to die
        Thread.currentThread().interrupt();
    } catch (ClosedSelectorException ex) {
    // allow this thread to exit
    } catch (IOException ex) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.AcceptorImpl_UNEXPECTED, ex));
    } finally {
        try {
            drainSelectorQueue();
        } finally {
            // note that if this method was called by close then the
            // following call is a noop since the first thing it does
            // is call isRunning.
            // make sure this is called to fix bug 37749
            close();
        }
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) SelectionKey(java.nio.channels.SelectionKey) Set(java.util.Set) HashSet(java.util.HashSet) CancelledKeyException(java.nio.channels.CancelledKeyException) Iterator(java.util.Iterator) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Aggregations

ClosedSelectorException (java.nio.channels.ClosedSelectorException)30 SelectionKey (java.nio.channels.SelectionKey)20 IOException (java.io.IOException)16 ServerSocketChannel (java.nio.channels.ServerSocketChannel)8 SocketChannel (java.nio.channels.SocketChannel)8 CancelledKeyException (java.nio.channels.CancelledKeyException)6 ClosedChannelException (java.nio.channels.ClosedChannelException)5 Selector (java.nio.channels.Selector)4 NioConnectionException (com.cloud.utils.exception.NioConnectionException)2 Iterator (java.util.Iterator)2 EOFException (java.io.EOFException)1 InterruptedIOException (java.io.InterruptedIOException)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)1 SelectableChannel (java.nio.channels.SelectableChannel)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 HashMap (java.util.HashMap)1