use of org.jboss.netty.channel.ChannelException in project cdap by caskdata.
the class NettyRouter method bootstrapServer.
private void bootstrapServer(final ChannelUpstreamHandler connectionTracker) throws ServiceBindException {
ExecutorService serverBossExecutor = createExecutorService(serverBossThreadPoolSize, "router-server-boss-thread-%d");
ExecutorService serverWorkerExecutor = createExecutorService(serverWorkerThreadPoolSize, "router-server-worker-thread-%d");
serverBootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(serverBossExecutor, serverWorkerExecutor));
serverBootstrap.setOption("backlog", serverConnectionBacklog);
serverBootstrap.setOption("child.bufferFactory", new DirectChannelBufferFactory());
// Setup the pipeline factory
serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
if (isSSLEnabled()) {
// Add SSLHandler is SSL is enabled
pipeline.addLast("ssl", sslHandlerFactory.create());
}
pipeline.addLast("tracker", connectionTracker);
pipeline.addLast("http-response-encoder", new HttpResponseEncoder());
pipeline.addLast("http-decoder", new HttpRequestDecoder());
pipeline.addLast("http-status-request-handler", new HttpStatusRequestHandler());
if (securityEnabled) {
pipeline.addLast("access-token-authenticator", new SecurityAuthenticationHttpHandler(realm, tokenValidator, configuration, accessTokenTransformer, discoveryServiceClient));
}
// for now there's only one hardcoded rule, but if there will be more, we may want it generic and configurable
pipeline.addLast("http-request-handler", new HttpRequestHandler(clientBootstrap, serviceLookup, ImmutableList.<ProxyRule>of()));
return pipeline;
}
});
// Start listening on ports.
ImmutableMap.Builder<Integer, String> serviceMapBuilder = ImmutableMap.builder();
for (Map.Entry<String, Integer> forward : serviceToPortMap.entrySet()) {
int port = forward.getValue();
String service = forward.getKey();
String boundService = serviceLookup.getService(port);
if (boundService != null) {
LOG.warn("Port {} is already configured to service {}, ignoring forward for service {}", port, boundService, service);
continue;
}
InetSocketAddress bindAddress = new InetSocketAddress(hostname, port);
LOG.info("Starting Netty Router for service {} on address {}...", service, bindAddress);
try {
Channel channel = serverBootstrap.bind(bindAddress);
InetSocketAddress boundAddress = (InetSocketAddress) channel.getLocalAddress();
serviceMapBuilder.put(boundAddress.getPort(), service);
channelGroup.add(channel);
// Update service map
serviceLookup.updateServiceMap(serviceMapBuilder.build());
LOG.info("Started Netty Router for service {} on address {}.", service, boundAddress);
} catch (ChannelException e) {
if ((Throwables.getRootCause(e) instanceof BindException)) {
throw new ServiceBindException("Router", hostname.getCanonicalHostName(), port, e);
}
throw e;
}
}
}
use of org.jboss.netty.channel.ChannelException in project druid by druid-io.
the class RemoteTaskActionClient method submit.
@Override
public <RetType> RetType submit(TaskAction<RetType> taskAction) throws IOException {
log.info("Performing action for task[%s]: %s", task.getId(), taskAction);
byte[] dataToSend = jsonMapper.writeValueAsBytes(new TaskActionHolder(task, taskAction));
final RetryPolicy retryPolicy = retryPolicyFactory.makeRetryPolicy();
while (true) {
try {
final Server server;
final URI serviceUri;
try {
server = getServiceInstance();
serviceUri = makeServiceUri(server);
} catch (Exception e) {
// Want to retry, so throw an IOException.
throw new IOException("Failed to locate service uri", e);
}
final StatusResponseHolder response;
log.info("Submitting action for task[%s] to overlord[%s]: %s", task.getId(), serviceUri, taskAction);
try {
response = httpClient.go(new Request(HttpMethod.POST, serviceUri.toURL()).setContent(MediaType.APPLICATION_JSON, dataToSend), new StatusResponseHandler(Charsets.UTF_8)).get();
} catch (Exception e) {
Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
Throwables.propagateIfInstanceOf(e.getCause(), ChannelException.class);
throw Throwables.propagate(e);
}
if (response.getStatus().getCode() / 100 == 2) {
final Map<String, Object> responseDict = jsonMapper.readValue(response.getContent(), new TypeReference<Map<String, Object>>() {
});
return jsonMapper.convertValue(responseDict.get("result"), taskAction.getReturnTypeReference());
} else {
// Want to retry, so throw an IOException.
throw new IOException(String.format("Scary HTTP status returned: %s. Check your overlord[%s] logs for exceptions.", response.getStatus(), server.getHost()));
}
} catch (IOException | ChannelException e) {
log.warn(e, "Exception submitting action for task[%s]", task.getId());
final Duration delay = retryPolicy.getAndIncrementRetryDelay();
if (delay == null) {
throw e;
} else {
try {
final long sleepTime = jitter(delay.getMillis());
log.info("Will try again in [%s].", new Duration(sleepTime).toString());
Thread.sleep(sleepTime);
} catch (InterruptedException e2) {
throw Throwables.propagate(e2);
}
}
}
}
}
use of org.jboss.netty.channel.ChannelException in project neo4j by neo4j.
the class PortRangeSocketBinder method bindToFirstAvailablePortInRange.
public Connection bindToFirstAvailablePortInRange(HostnamePort serverAddress) throws ChannelException {
int[] ports = serverAddress.getPorts();
String host = serverAddress.getHost();
Channel channel;
InetSocketAddress socketAddress;
ChannelException lastException = null;
PortIterator portIterator = new PortIterator(ports);
while (portIterator.hasNext()) {
Integer port = portIterator.next();
if (host == null || host.equals(ALL_INTERFACES_ADDRESS)) {
socketAddress = new InetSocketAddress(port);
} else {
socketAddress = new InetSocketAddress(host, port);
}
try {
channel = bootstrap.bind(socketAddress);
return new Connection(socketAddress, channel);
} catch (ChannelException e) {
if (lastException != null) {
e.addSuppressed(lastException);
}
lastException = e;
}
}
throw lastException;
}
use of org.jboss.netty.channel.ChannelException in project neo4j by neo4j.
the class PortRangeSocketBinderTest method shouldReturnChannelAndSocketIfAnyPortsAreFree.
@Test
public void shouldReturnChannelAndSocketIfAnyPortsAreFree() {
// given
HostnamePort localhost = new HostnamePort("localhost", 9000, 9001);
ServerBootstrap bootstrap = mock(ServerBootstrap.class);
Channel channel = mock(Channel.class);
when(bootstrap.bind(new InetSocketAddress("localhost", 9000))).thenThrow(new ChannelException());
when(bootstrap.bind(new InetSocketAddress("localhost", 9001))).thenReturn(channel);
// when
Connection connection = new PortRangeSocketBinder(bootstrap).bindToFirstAvailablePortInRange(localhost);
//then
assertEquals(channel, connection.getChannel());
assertEquals(new InetSocketAddress(localhost.getHost(), 9001), connection.getSocketAddress());
}
use of org.jboss.netty.channel.ChannelException in project neo4j by neo4j.
the class PortRangeSocketBinderTest method shouldReThrowExceptionIfCannotBindToAnyOfThePortsInTheRange.
@Test
public void shouldReThrowExceptionIfCannotBindToAnyOfThePortsInTheRange() {
// given
HostnamePort localhost = new HostnamePort("localhost", 9000, 9002);
ServerBootstrap bootstrap = mock(ServerBootstrap.class);
when(bootstrap.bind(new InetSocketAddress("localhost", 9000))).thenThrow(new ChannelException("Failed to bind to: 9000"));
when(bootstrap.bind(new InetSocketAddress("localhost", 9001))).thenThrow(new ChannelException("Failed to bind to: 9001"));
when(bootstrap.bind(new InetSocketAddress("localhost", 9002))).thenThrow(new ChannelException("Failed to bind to: 9002"));
try {
// when
new PortRangeSocketBinder(bootstrap).bindToFirstAvailablePortInRange(localhost);
fail("should have thrown ChannelException");
} catch (ChannelException ex) {
// expected
assertEquals(2, suppressedExceptions(ex));
}
}
Aggregations