Search in sources :

Example 11 with UnresolvedAddressException

use of java.nio.channels.UnresolvedAddressException in project moleculer-java by moleculer-java.

the class TcpWriter method run.

// --- WRITER LOOP ---
@Override
public void run() {
    try {
        // Loop
        while (true) {
            // Waiting for sockets
            int n;
            try {
                n = selector.select(3000);
            } catch (NullPointerException nullPointer) {
                continue;
            } catch (Exception cause) {
                break;
            }
            // Open new connections
            SendBuffer buffer = opened.poll();
            SelectionKey key = null;
            while (buffer != null) {
                try {
                    InetSocketAddress address;
                    try {
                        address = new InetSocketAddress(buffer.host, buffer.port);
                    } catch (UnresolvedAddressException dnsError) {
                        // Workaround: unable to resolve host name
                        Tree info = transporter.getDescriptor(buffer.nodeID);
                        if (info == null) {
                            throw dnsError;
                        }
                        String ip = getHostOrIP(false, info);
                        if (ip == null || buffer.host.equalsIgnoreCase(ip)) {
                            throw dnsError;
                        }
                        if (debug) {
                            logger.info("Unable to resolve hostname \"" + buffer.host + "\", trying with \"" + ip + "\"...");
                        }
                        address = new InetSocketAddress(ip, buffer.port);
                    }
                    SocketChannel channel = SocketChannel.open(address);
                    channel.configureBlocking(false);
                    channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
                    channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
                    channel.setOption(StandardSocketOptions.SO_LINGER, -1);
                    key = channel.register(selector, SelectionKey.OP_WRITE);
                    key.attach(buffer);
                    buffer.connected(key, channel);
                    if (debug) {
                        logger.info("Client channel opened to \"" + buffer.nodeID + "\".");
                    }
                } catch (Throwable cause) {
                    if (buffer != null) {
                        synchronized (buffers) {
                            buffers.remove(buffer.nodeID);
                        }
                        transporter.unableToSend(buffer.nodeID, buffer.getUnsentPackets(), cause);
                    }
                }
                buffer = opened.poll();
            }
            if (n < 1) {
                continue;
            }
            Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
            while (keys.hasNext()) {
                key = keys.next();
                if (key == null) {
                    continue;
                }
                if (!key.isValid()) {
                    keys.remove();
                    continue;
                }
                if (key.isWritable()) {
                    // Write data
                    buffer = null;
                    try {
                        buffer = (SendBuffer) key.attachment();
                        if (buffer != null) {
                            buffer.write();
                        }
                    } catch (Exception cause) {
                        if (buffer != null) {
                            synchronized (buffers) {
                                buffers.remove(buffer.nodeID);
                            }
                            transporter.unableToSend(buffer.nodeID, buffer.getUnsentPackets(), cause);
                        }
                        close(key, cause);
                    }
                }
                keys.remove();
            }
        }
    } catch (Exception fatal) {
        logger.error("TCP writer closed!", fatal);
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) Tree(io.datatree.Tree) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConcurrentModificationException(java.util.ConcurrentModificationException)

Example 12 with UnresolvedAddressException

use of java.nio.channels.UnresolvedAddressException in project alluxio by Alluxio.

the class AbstractClient method retryRPCInternal.

private synchronized <V> V retryRPCInternal(RetryPolicy retryPolicy, RpcCallable<V> rpc, Supplier<Void> onRetry) throws AlluxioStatusException {
    Exception ex = null;
    while (retryPolicy.attempt()) {
        if (mClosed) {
            throw new FailedPreconditionException("Client is closed");
        }
        connect();
        try {
            return rpc.call();
        } catch (StatusRuntimeException e) {
            AlluxioStatusException se = AlluxioStatusException.fromStatusRuntimeException(e);
            if (se.getStatusCode() == Status.Code.UNAVAILABLE || se.getStatusCode() == Status.Code.CANCELLED || se.getStatusCode() == Status.Code.UNAUTHENTICATED || e.getCause() instanceof UnresolvedAddressException) {
                ex = se;
            } else {
                throw se;
            }
        }
        LOG.debug("Rpc failed ({}): ", retryPolicy.getAttemptCount(), ex);
        onRetry.get();
        disconnect();
    }
    throw new UnavailableException("Failed after " + retryPolicy.getAttemptCount() + " attempts: " + ex.toString(), ex);
}
Also used : FailedPreconditionException(alluxio.exception.status.FailedPreconditionException) StatusRuntimeException(io.grpc.StatusRuntimeException) UnavailableException(alluxio.exception.status.UnavailableException) AlluxioStatusException(alluxio.exception.status.AlluxioStatusException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) FailedPreconditionException(alluxio.exception.status.FailedPreconditionException) AlluxioStatusException(alluxio.exception.status.AlluxioStatusException) UnauthenticatedException(alluxio.exception.status.UnauthenticatedException) IOException(java.io.IOException) NotFoundException(alluxio.exception.status.NotFoundException) StatusRuntimeException(io.grpc.StatusRuntimeException) ServiceNotFoundException(alluxio.exception.ServiceNotFoundException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) UnavailableException(alluxio.exception.status.UnavailableException)

Example 13 with UnresolvedAddressException

use of java.nio.channels.UnresolvedAddressException in project grpc-java by grpc.

the class UtilsTest method testStatusFromThrowable.

@Test
public void testStatusFromThrowable() {
    Status s = Status.CANCELLED.withDescription("msg");
    assertSame(s, Utils.statusFromThrowable(new Exception(s.asException())));
    Throwable t;
    t = new ConnectTimeoutException("msg");
    assertStatusEquals(Status.UNAVAILABLE.withCause(t), Utils.statusFromThrowable(t));
    t = new UnresolvedAddressException();
    assertStatusEquals(Status.UNAVAILABLE.withCause(t), Utils.statusFromThrowable(t));
    t = new Http2Exception(Http2Error.INTERNAL_ERROR, "msg");
    assertStatusEquals(Status.INTERNAL.withCause(t), Utils.statusFromThrowable(t));
    t = new Exception("msg");
    assertStatusEquals(Status.UNKNOWN.withCause(t), Utils.statusFromThrowable(t));
}
Also used : Status(io.grpc.Status) Http2Exception(io.netty.handler.codec.http2.Http2Exception) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) Http2Exception(io.netty.handler.codec.http2.Http2Exception) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) Test(org.junit.Test)

Example 14 with UnresolvedAddressException

use of java.nio.channels.UnresolvedAddressException in project j2objc by google.

the class SocketChannelTest method testCFII_Unresolved.

public void testCFII_Unresolved() throws IOException {
    statusNotConnected_NotPending();
    InetSocketAddress unresolved = new InetSocketAddress("unresolved address", 1080);
    try {
        this.channel1.connect(unresolved);
        fail("Should throw an UnresolvedAddressException here.");
    } catch (UnresolvedAddressException e) {
    // OK.
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException)

Example 15 with UnresolvedAddressException

use of java.nio.channels.UnresolvedAddressException in project j2objc by google.

the class UnresolvedAddressExceptionTest method test_Constructor.

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

Aggregations

UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)30 IOException (java.io.IOException)15 InetSocketAddress (java.net.InetSocketAddress)15 SocketChannel (java.nio.channels.SocketChannel)8 ConnectException (java.net.ConnectException)5 SocketAddress (java.net.SocketAddress)4 UnsupportedAddressTypeException (java.nio.channels.UnsupportedAddressTypeException)4 Socket (java.net.Socket)3 SocketTimeoutException (java.net.SocketTimeoutException)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 SelectionKey (java.nio.channels.SelectionKey)3 ServerSocketChannel (java.nio.channels.ServerSocketChannel)3 FetchRequest (kafka.api.FetchRequest)3 FetchRequestBuilder (kafka.api.FetchRequestBuilder)3 FetchResponse (kafka.javaapi.FetchResponse)3 ByteBufferMessageSet (kafka.javaapi.message.ByteBufferMessageSet)3 Status (io.grpc.Status)2 Http2Exception (io.netty.handler.codec.http2.Http2Exception)2 ServerSocket (java.net.ServerSocket)2 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)2