Search in sources :

Example 6 with ChannelException

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;
        }
    }
}
Also used : ServiceBindException(co.cask.cdap.common.ServiceBindException) HttpRequestHandler(co.cask.cdap.gateway.router.handlers.HttpRequestHandler) InetSocketAddress(java.net.InetSocketAddress) SecurityAuthenticationHttpHandler(co.cask.cdap.gateway.router.handlers.SecurityAuthenticationHttpHandler) HttpStatusRequestHandler(co.cask.cdap.gateway.router.handlers.HttpStatusRequestHandler) HttpRequestDecoder(org.jboss.netty.handler.codec.http.HttpRequestDecoder) ChannelException(org.jboss.netty.channel.ChannelException) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) Channel(org.jboss.netty.channel.Channel) BindException(java.net.BindException) ServiceBindException(co.cask.cdap.common.ServiceBindException) DirectChannelBufferFactory(org.jboss.netty.buffer.DirectChannelBufferFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelException(org.jboss.netty.channel.ChannelException) BindException(java.net.BindException) ServiceBindException(co.cask.cdap.common.ServiceBindException) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ImmutableMap(com.google.common.collect.ImmutableMap) HttpResponseEncoder(org.jboss.netty.handler.codec.http.HttpResponseEncoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 7 with ChannelException

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);
                }
            }
        }
    }
}
Also used : Server(io.druid.client.selector.Server) Request(com.metamx.http.client.Request) Duration(org.joda.time.Duration) IOException(java.io.IOException) URI(java.net.URI) ChannelException(org.jboss.netty.channel.ChannelException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) StatusResponseHolder(com.metamx.http.client.response.StatusResponseHolder) StatusResponseHandler(com.metamx.http.client.response.StatusResponseHandler) RetryPolicy(io.druid.indexing.common.RetryPolicy) Map(java.util.Map) ChannelException(org.jboss.netty.channel.ChannelException)

Example 8 with ChannelException

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;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) ChannelException(org.jboss.netty.channel.ChannelException)

Example 9 with ChannelException

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());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HostnamePort(org.neo4j.helpers.HostnamePort) Channel(org.jboss.netty.channel.Channel) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelException(org.jboss.netty.channel.ChannelException) Test(org.junit.Test)

Example 10 with ChannelException

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

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