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);
}
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;
}
}
}
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;
}
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
}
}
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);
}
}
}
Aggregations