Search in sources :

Example 1 with ChannelException

use of org.jboss.netty.channel.ChannelException in project flink by apache.

the class RpcServiceUtils method createRpcService.

// ------------------------------------------------------------------------
//  RPC instantiation
// ------------------------------------------------------------------------
/**
	 * Utility method to create RPC service from configuration and hostname, port.
	 *
	 * @param hostname   The hostname/address that describes the TaskManager's data location.
	 * @param port           If true, the TaskManager will not initiate the TCP network stack.
	 * @param configuration                 The configuration for the TaskManager.
	 * @return   The rpc service which is used to start and connect to the TaskManager RpcEndpoint .
	 * @throws IOException      Thrown, if the actor system can not bind to the address
	 * @throws Exception      Thrown is some other error occurs while creating akka actor system
	 */
public static RpcService createRpcService(String hostname, int port, Configuration configuration) throws Exception {
    LOG.info("Starting AkkaRpcService at {}.", NetUtils.hostAndPortToUrlString(hostname, port));
    final ActorSystem actorSystem;
    try {
        Config akkaConfig;
        if (hostname != null && !hostname.isEmpty()) {
            // remote akka config
            akkaConfig = AkkaUtils.getAkkaConfig(configuration, hostname, port);
        } else {
            // local akka config
            akkaConfig = AkkaUtils.getAkkaConfig(configuration);
        }
        LOG.debug("Using akka configuration \n {}.", akkaConfig);
        actorSystem = AkkaUtils.createActorSystem(akkaConfig);
    } catch (Throwable t) {
        if (t instanceof ChannelException) {
            Throwable cause = t.getCause();
            if (cause != null && t.getCause() instanceof java.net.BindException) {
                String address = NetUtils.hostAndPortToUrlString(hostname, port);
                throw new IOException("Unable to bind AkkaRpcService actor system to address " + address + " - " + cause.getMessage(), t);
            }
        }
        throw new Exception("Could not create TaskManager actor system", t);
    }
    final Time timeout = Time.milliseconds(AkkaUtils.getTimeout(configuration).toMillis());
    return new AkkaRpcService(actorSystem, timeout);
}
Also used : ActorSystem(akka.actor.ActorSystem) AkkaRpcService(org.apache.flink.runtime.rpc.akka.AkkaRpcService) Config(com.typesafe.config.Config) Time(org.apache.flink.api.common.time.Time) IOException(java.io.IOException) ChannelException(org.jboss.netty.channel.ChannelException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ChannelException(org.jboss.netty.channel.ChannelException)

Example 2 with ChannelException

use of org.jboss.netty.channel.ChannelException in project druid by druid-io.

the class KafkaIndexTaskClient method submitRequest.

private FullResponseHolder submitRequest(String id, HttpMethod method, String pathSuffix, String query, byte[] content, boolean retry) {
    final RetryPolicy retryPolicy = retryPolicyFactory.makeRetryPolicy();
    while (true) {
        FullResponseHolder response = null;
        Request request = null;
        TaskLocation location = TaskLocation.unknown();
        String path = String.format("%s/%s/%s", BASE_PATH, id, pathSuffix);
        Optional<TaskStatus> status = taskInfoProvider.getTaskStatus(id);
        if (!status.isPresent() || !status.get().isRunnable()) {
            throw new TaskNotRunnableException(String.format("Aborting request because task [%s] is not runnable", id));
        }
        try {
            location = taskInfoProvider.getTaskLocation(id);
            if (location.equals(TaskLocation.unknown())) {
                throw new NoTaskLocationException(String.format("No TaskLocation available for task [%s]", id));
            }
            // Netty throws some annoying exceptions if a connection can't be opened, which happens relatively frequently
            // for tasks that happen to still be starting up, so test the connection first to keep the logs clean.
            checkConnection(location.getHost(), location.getPort());
            try {
                URI serviceUri = new URI("http", null, location.getHost(), location.getPort(), path, query, null);
                request = new Request(method, serviceUri.toURL());
                // used to validate that we are talking to the correct worker
                request.addHeader(ChatHandlerResource.TASK_ID_HEADER, id);
                if (content.length > 0) {
                    request.setContent(MediaType.APPLICATION_JSON, content);
                }
                log.debug("HTTP %s: %s", method.getName(), serviceUri.toString());
                response = httpClient.go(request, new FullResponseHandler(Charsets.UTF_8), httpTimeout).get();
            } catch (Exception e) {
                Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
                Throwables.propagateIfInstanceOf(e.getCause(), ChannelException.class);
                throw Throwables.propagate(e);
            }
            int responseCode = response.getStatus().getCode();
            if (responseCode / 100 == 2) {
                return response;
            } else if (responseCode == 400) {
                // don't bother retrying if it's a bad request
                throw new IAE("Received 400 Bad Request with body: %s", response.getContent());
            } else {
                throw new IOException(String.format("Received status [%d]", responseCode));
            }
        } catch (IOException | ChannelException e) {
            // Since workers are free to move tasks around to different ports, there is a chance that a task may have been
            // moved but our view of its location has not been updated yet from ZK. To detect this case, we send a header
            // identifying our expected recipient in the request; if this doesn't correspond to the worker we messaged, the
            // worker will return an HTTP 404 with its ID in the response header. If we get a mismatching task ID, then
            // we will wait for a short period then retry the request indefinitely, expecting the task's location to
            // eventually be updated.
            final Duration delay;
            if (response != null && response.getStatus().equals(HttpResponseStatus.NOT_FOUND)) {
                String headerId = response.getResponse().headers().get(ChatHandlerResource.TASK_ID_HEADER);
                if (headerId != null && !headerId.equals(id)) {
                    log.warn("Expected worker to have taskId [%s] but has taskId [%s], will retry in [%d]s", id, headerId, TASK_MISMATCH_RETRY_DELAY_SECONDS);
                    delay = Duration.standardSeconds(TASK_MISMATCH_RETRY_DELAY_SECONDS);
                } else {
                    delay = retryPolicy.getAndIncrementRetryDelay();
                }
            } else {
                delay = retryPolicy.getAndIncrementRetryDelay();
            }
            String urlForLog = (request != null ? request.getUrl().toString() : String.format("http://%s:%d%s", location.getHost(), location.getPort(), path));
            if (!retry) {
                // if retry=false, we probably aren't too concerned if the operation doesn't succeed (i.e. the request was
                // for informational purposes only) so don't log a scary stack trace
                log.info("submitRequest failed for [%s], with message [%s]", urlForLog, e.getMessage());
                Throwables.propagate(e);
            } else if (delay == null) {
                log.warn(e, "Retries exhausted for [%s], last exception:", urlForLog);
                Throwables.propagate(e);
            } else {
                try {
                    final long sleepTime = delay.getMillis();
                    log.debug("Bad response HTTP [%s] from [%s]; will try again in [%s] (body/exception: [%s])", (response != null ? response.getStatus().getCode() : "no response"), urlForLog, new Duration(sleepTime).toString(), (response != null ? response.getContent() : e.getMessage()));
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e2) {
                    Throwables.propagate(e2);
                }
            }
        } catch (NoTaskLocationException e) {
            log.info("No TaskLocation available for task [%s], this task may not have been assigned to a worker yet or " + "may have already completed", id);
            throw e;
        } catch (Exception e) {
            log.warn(e, "Exception while sending request");
            throw e;
        }
    }
}
Also used : Request(com.metamx.http.client.Request) Duration(org.joda.time.Duration) IOException(java.io.IOException) TaskStatus(io.druid.indexing.common.TaskStatus) IAE(io.druid.java.util.common.IAE) URI(java.net.URI) TaskLocation(io.druid.indexing.common.TaskLocation) ChannelException(org.jboss.netty.channel.ChannelException) IOException(java.io.IOException) FullResponseHandler(com.metamx.http.client.response.FullResponseHandler) FullResponseHolder(com.metamx.http.client.response.FullResponseHolder) RetryPolicy(io.druid.indexing.common.RetryPolicy) ChannelException(org.jboss.netty.channel.ChannelException)

Example 3 with ChannelException

use of org.jboss.netty.channel.ChannelException in project neo4j by neo4j.

the class NetworkReceiver method listen.

private int listen(int minPort, int maxPort) throws URISyntaxException, ChannelException {
    ChannelException ex = null;
    for (int checkPort = minPort; checkPort <= maxPort; checkPort++) {
        try {
            String address = config.clusterServer().getHost();
            InetSocketAddress localAddress;
            if (address == null || address.equals(INADDR_ANY)) {
                localAddress = new InetSocketAddress(checkPort);
            } else {
                localAddress = new InetSocketAddress(address, checkPort);
                bindingDetected = true;
            }
            Channel listenChannel = serverBootstrap.bind(localAddress);
            listeningAt(getURI(localAddress));
            channels.add(listenChannel);
            return checkPort;
        } catch (ChannelException e) {
            ex = e;
        }
    }
    nioChannelFactory.releaseExternalResources();
    throw ex;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) ChannelException(org.jboss.netty.channel.ChannelException)

Example 4 with ChannelException

use of org.jboss.netty.channel.ChannelException in project neo4j by neo4j.

the class PortRangeSocketBinderTest method shouldReThrowExceptionIfCannotBindToPort.

@Test
public void shouldReThrowExceptionIfCannotBindToPort() {
    // given
    HostnamePort localhost = new HostnamePort("localhost", 9000);
    ServerBootstrap bootstrap = mock(ServerBootstrap.class);
    when(bootstrap.bind(new InetSocketAddress("localhost", 9000))).thenThrow(new ChannelException());
    try {
        // when
        new PortRangeSocketBinder(bootstrap).bindToFirstAvailablePortInRange(localhost);
        fail("should have thrown ChannelException");
    } catch (ChannelException ignored) {
    // expected
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HostnamePort(org.neo4j.helpers.HostnamePort) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelException(org.jboss.netty.channel.ChannelException) Test(org.junit.Test)

Example 5 with ChannelException

use of org.jboss.netty.channel.ChannelException in project distributedlog by twitter.

the class DistributedLogClientImpl method handleRequestException.

void handleRequestException(SocketAddress addr, ProxyClient sc, Optional<StreamOp> op, Throwable cause) {
    boolean resendOp = false;
    boolean removeOwnerFromStream = false;
    SocketAddress previousAddr = addr;
    String reason = cause.getMessage();
    if (cause instanceof ConnectionFailedException || cause instanceof java.net.ConnectException) {
        routingService.removeHost(addr, cause);
        onServerLeft(addr, sc);
        removeOwnerFromStream = true;
        // redirect the request to other host.
        resendOp = true;
    } else if (cause instanceof ChannelException) {
        // no process listening on remote address/port.
        if (cause.getCause() instanceof java.net.ConnectException) {
            routingService.removeHost(addr, cause.getCause());
            onServerLeft(addr);
            reason = cause.getCause().getMessage();
        } else {
            routingService.removeHost(addr, cause);
            reason = cause.getMessage();
        }
        removeOwnerFromStream = true;
        // redirect the request to other host.
        resendOp = true;
    } else if (cause instanceof ServiceTimeoutException) {
        // redirect the request to itself again, which will backoff for a while
        resendOp = true;
        previousAddr = null;
    } else if (cause instanceof WriteException) {
        // redirect the request to other host.
        resendOp = true;
    } else if (cause instanceof ServiceException) {
        // redirect the request to other host.
        clientManager.removeClient(addr, sc);
        resendOp = true;
    } else if (cause instanceof TApplicationException) {
        handleTApplicationException(cause, op, addr, sc);
    } else if (cause instanceof Failure) {
        handleFinagleFailure((Failure) cause, op, addr);
    } else {
        // Default handler
        handleException(cause, op, addr);
    }
    if (op.isPresent()) {
        if (removeOwnerFromStream) {
            ownershipCache.removeOwnerFromStream(op.get().stream, addr, reason);
        }
        if (resendOp) {
            doSend(op.get(), previousAddr);
        }
    }
}
Also used : WriteException(com.twitter.finagle.WriteException) ServiceException(com.twitter.finagle.ServiceException) SocketAddress(java.net.SocketAddress) DLSocketAddress(com.twitter.distributedlog.service.DLSocketAddress) ServiceTimeoutException(com.twitter.finagle.ServiceTimeoutException) ConnectionFailedException(com.twitter.finagle.ConnectionFailedException) Failure(com.twitter.finagle.Failure) ChannelException(org.jboss.netty.channel.ChannelException) TApplicationException(org.apache.thrift.TApplicationException)

Aggregations

ChannelException (org.jboss.netty.channel.ChannelException)12 InetSocketAddress (java.net.InetSocketAddress)6 ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)4 Channel (org.jboss.netty.channel.Channel)4 IOException (java.io.IOException)3 Request (com.metamx.http.client.Request)2 RetryPolicy (io.druid.indexing.common.RetryPolicy)2 URI (java.net.URI)2 Map (java.util.Map)2 Test (org.junit.Test)2 HostnamePort (org.neo4j.helpers.HostnamePort)2 ActorSystem (akka.actor.ActorSystem)1 ServiceBindException (co.cask.cdap.common.ServiceBindException)1 HttpRequestHandler (co.cask.cdap.gateway.router.handlers.HttpRequestHandler)1 HttpStatusRequestHandler (co.cask.cdap.gateway.router.handlers.HttpStatusRequestHandler)1 SecurityAuthenticationHttpHandler (co.cask.cdap.gateway.router.handlers.SecurityAuthenticationHttpHandler)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 FullResponseHandler (com.metamx.http.client.response.FullResponseHandler)1 FullResponseHolder (com.metamx.http.client.response.FullResponseHolder)1 StatusResponseHandler (com.metamx.http.client.response.StatusResponseHandler)1