Beispiel #1
0
  public static void main(String[] args) throws Exception {
    String host = "localhost";
    int port = Integer.parseInt("8080");

    ChannelFactory factory =
        new NioClientSocketChannelFactory(
            Executors.newCachedThreadPool(), Executors.newCachedThreadPool());

    ClientBootstrap bootstrap = new ClientBootstrap(factory);

    bootstrap.setPipelineFactory(
        new ChannelPipelineFactory() {
          public ChannelPipeline getPipeline() {
            return Channels.pipeline(new TimeDecoder(), new TimeClientHandler());
          }
        });

    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("keepAlive", true);

    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    future.awaitUninterruptibly();
    if (!future.isSuccess()) {
      future.getCause().printStackTrace();
    }
    future.getChannel().getCloseFuture().awaitUninterruptibly();
    factory.releaseExternalResources();
  }
Beispiel #2
0
  public static void shutdownChannelFactory(
      ChannelFactory channelFactory,
      ExecutorService bossExecutor,
      ExecutorService workerExecutor,
      ChannelGroup allChannels) {
    // Close all channels
    if (allChannels != null) {
      closeChannels(allChannels);
    }

    // Shutdown the channel factory
    if (channelFactory != null) {
      channelFactory.shutdown();
    }

    // Stop boss threads
    if (bossExecutor != null) {
      shutdownExecutor(bossExecutor, "bossExecutor");
    }

    // Finally stop I/O workers
    if (workerExecutor != null) {
      shutdownExecutor(workerExecutor, "workerExecutor");
    }

    // Release any other resources netty might be holding onto via this channelFactory
    if (channelFactory != null) {
      channelFactory.releaseExternalResources();
    }
  }
 /** Stop the network manager, closing all connections and cleaning up all resources. */
 public void stop() {
   started.set(false);
   logger.info("Network manager stopping!");
   ChannelGroupFuture future = allChannels.close();
   future.awaitUninterruptibly();
   channelFactory.releaseExternalResources();
 }
  @Test
  public void shouldPropagateDownstreamFlushOnPipelineFutureSuccess() throws Exception {

    context.checking(
        new Expectations() {
          {
            oneOf(upstream)
                .handleUpstream(
                    with(any(ChannelHandlerContext.class)), with(any(PreparationEvent.class)));
            oneOf(upstream)
                .handleUpstream(
                    with(any(ChannelHandlerContext.class)), with(channelState(OPEN, TRUE)));
            oneOf(downstream)
                .handleDownstream(
                    with(any(ChannelHandlerContext.class)), with(any(FlushEvent.class)));
          }
        });

    channelFactory.newChannel(pipeline);
    ChannelFuture executionFuture = execution.getHandlerFuture();
    executionFuture.setSuccess();

    ChannelFuture handlerFuture = handler.getHandlerFuture();
    handlerFuture.sync();

    context.assertIsSatisfied();
  }
Beispiel #5
0
 /** close all channels, and release resources */
 public synchronized void close() {
   if (allChannels != null) {
     allChannels.close().awaitUninterruptibly();
     factory.releaseExternalResources();
     allChannels = null;
   }
 }
Beispiel #6
0
 private static final void unrefChannelFactory() {
   synchronized (channelFactoryRefs) {
     if (channelFactoryRefs.decrementAndGet() == 0) {
       channelFactory.releaseExternalResources();
       channelFactory = null;
     }
   }
 }
 public void stop() {
   try {
     myScheduler.shutdown();
     myBuildsExecutor.shutdown();
     final ChannelGroupFuture closeFuture = myAllOpenChannels.close();
     closeFuture.awaitUninterruptibly();
   } finally {
     myChannelFactory.releaseExternalResources();
   }
 }
  @Test
  public void shouldNotPropagateDownstreamFlushOnPipelineFutureFailure() throws Exception {

    context.checking(
        new Expectations() {
          {
            oneOf(upstream)
                .handleUpstream(
                    with(any(ChannelHandlerContext.class)), with(any(PreparationEvent.class)));
            oneOf(upstream)
                .handleUpstream(
                    with(any(ChannelHandlerContext.class)), with(channelState(OPEN, TRUE)));
          }
        });

    channelFactory.newChannel(pipeline);
    ChannelFuture executionFuture = execution.getHandlerFuture();
    executionFuture.setFailure(new ChannelException("pipeline already failed"));

    ChannelFuture handlerFuture = handler.getHandlerFuture();
    assertFalse(handlerFuture.isDone());

    context.assertIsSatisfied();
  }
  @Test
  public void shouldPropagateDownstreamMessageOnPipelineFutureSuccess() throws Exception {

    final ChannelBuffer[] expectedArr = new ChannelBuffer[3];
    expectedArr[0] = wrappedBuffer(new byte[] {0x01, 0x02, 0x03});
    expectedArr[1] = copiedBuffer("Hello, world", UTF_8);
    expectedArr[2] = wrappedBuffer(new byte[] {0x01, 0x02, 0x03});
    final ChannelBuffer expected = masker.applyMask(wrappedBuffer(expectedArr));

    context.checking(
        new Expectations() {
          {
            oneOf(upstream)
                .handleUpstream(
                    with(any(ChannelHandlerContext.class)), with(any(PreparationEvent.class)));
            oneOf(upstream)
                .handleUpstream(
                    with(any(ChannelHandlerContext.class)), with(channelState(OPEN, TRUE)));
            oneOf(downstream)
                .handleDownstream(with(any(ChannelHandlerContext.class)), with(message(expected)));
            oneOf(upstream)
                .handleUpstream(
                    with(any(ChannelHandlerContext.class)), with(any(WriteCompletionEvent.class)));
          }
        });

    expression.setValue(environment, new byte[] {0x01, 0x02, 0x03});
    channelFactory.newChannel(pipeline);
    ChannelFuture executionFuture = execution.getHandlerFuture();
    executionFuture.setSuccess();

    ChannelFuture handlerFuture = handler.getHandlerFuture();
    handlerFuture.sync();

    context.assertIsSatisfied();
  }
Beispiel #10
0
 public static void shutdown() {
   if (serverSocketChannelFactory != null) {
     serverSocketChannelFactory.releaseExternalResources();
   }
 }
Beispiel #11
0
  @Override
  public void run() {
    Configuration conf = OmidConfiguration.create();
    // *** Start the Netty configuration ***
    // Start server with Nb of active threads = 2*NB CPU + 1 as maximum.
    // int maxSocketThreads = conf.getInt("tso.maxsocketthread",
    // (Runtime.getRuntime().availableProcessors() * 2 + 1) * 2);
    // more worder threads has an inverse impact on performance, unless the one is saturated
    int maxSocketThreads = conf.getInt("tso.maxsocketthread", 1);
    ChannelFactory factory =
        new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), maxSocketThreads);

    ServerBootstrap bootstrap = new ServerBootstrap(factory);
    // Create the global ChannelGroup
    ChannelGroup channelGroup = new DefaultChannelGroup(TSOServer.class.getName());
    // threads max
    // int maxThreads = Runtime.getRuntime().availableProcessors() *2 + 1;
    // More concurrency gives lower performance due to synchronizations
    int maxThreads = conf.getInt("tso.maxthread", 4);
    System.out.println("maxThreads: " + maxThreads);
    // int maxThreads = 5;
    // Memory limitation: 1MB by channel, 1GB global, 100 ms of timeout
    ThreadPoolExecutor pipelineExecutor =
        new OrderedMemoryAwareThreadPoolExecutor(
            maxThreads,
            1048576,
            1073741824,
            100,
            TimeUnit.MILLISECONDS,
            Executors.defaultThreadFactory());

    // This is the only object of timestamp oracle
    // TODO: make it singleton
    // TimestampOracle timestampOracle = new TimestampOracle();
    // The wrapper for the shared state of TSO
    state = BookKeeperStateBuilder.getState(this.config);

    if (state == null) {
      LOG.error("Couldn't build state");
      return;
    }
    TSOState.BATCH_SIZE = config.getBatchSize();
    System.out.println("PARAM MAX_ITEMS: " + TSOState.MAX_ITEMS);
    System.out.println("PARAM BATCH_SIZE: " + TSOState.BATCH_SIZE);
    System.out.println("PARAM LOAD_FACTOR: " + TSOState.LOAD_FACTOR);
    System.out.println("PARAM MAX_THREADS: " + maxThreads);

    final TSOHandler handler = new TSOHandler(channelGroup, state);

    bootstrap.setPipelineFactory(new TSOPipelineFactory(pipelineExecutor, handler));
    bootstrap.setOption("tcpNoDelay", false);
    bootstrap.setOption("child.tcpNoDelay", false);
    bootstrap.setOption("child.keepAlive", true);
    bootstrap.setOption("child.reuseAddress", true);
    bootstrap.setOption("child.connectTimeoutMillis", 60000);
    bootstrap.setOption("readWriteFair", true);

    // *** Start the Netty running ***

    // Create the monitor
    ThroughputMonitor monitor = new ThroughputMonitor(state);
    // Add the parent channel to the group
    Channel channel = bootstrap.bind(new InetSocketAddress(config.getPort()));
    channelGroup.add(channel);

    // Compacter handler
    ChannelFactory comFactory =
        new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool(),
            (Runtime.getRuntime().availableProcessors() * 2 + 1) * 2);
    ServerBootstrap comBootstrap = new ServerBootstrap(comFactory);
    ChannelGroup comGroup = new DefaultChannelGroup("compacter");
    final CompacterHandler comHandler = new CompacterHandler(comGroup, state);
    comBootstrap.setPipelineFactory(
        new ChannelPipelineFactory() {

          @Override
          public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", new ObjectDecoder());
            pipeline.addLast("encoder", new ObjectEncoder());
            pipeline.addLast("handler", comHandler);
            return pipeline;
          }
        });
    comBootstrap.setOption("tcpNoDelay", false);
    comBootstrap.setOption("child.tcpNoDelay", false);
    comBootstrap.setOption("child.keepAlive", true);
    comBootstrap.setOption("child.reuseAddress", true);
    comBootstrap.setOption("child.connectTimeoutMillis", 100);
    comBootstrap.setOption("readWriteFair", true);
    channel = comBootstrap.bind(new InetSocketAddress(config.getPort() + 1));

    // Starts the monitor
    monitor.start();
    synchronized (lock) {
      while (!finish) {
        try {
          lock.wait();
        } catch (InterruptedException e) {
          break;
        }
      }
    }

    // timestampOracle.stop();
    handler.stop();
    comHandler.stop();
    state.stop();

    // *** Start the Netty shutdown ***

    // End the monitor
    System.out.println("End of monitor");
    monitor.interrupt();
    // Now close all channels
    System.out.println("End of channel group");
    channelGroup.close().awaitUninterruptibly();
    comGroup.close().awaitUninterruptibly();
    // Close the executor for Pipeline
    System.out.println("End of pipeline executor");
    pipelineExecutor.shutdownNow();
    // Now release resources
    System.out.println("End of resources");
    factory.releaseExternalResources();
    comFactory.releaseExternalResources();
  }
Beispiel #12
0
 @Override
 public void close() {
   allChannels.close().awaitUninterruptibly();
   channelFactory.releaseExternalResources();
 }
  /**
   * Start a HyperGateClient
   *
   * <p>for that case use
   *
   * @throws Exception
   */
  @Override
  public void start() throws Exception {

    if (this.running) {
      throw new InstantiationException("HyperGate client " + this.getName() + "is running");
    }

    // Configure the client.
    // TODO make it more robust & speedy implements Kryo serializer
    this.channelFactory =
        new NioClientSocketChannelFactory(
            // TODO implement other executors
            Executors.newCachedThreadPool(), Executors.newCachedThreadPool());

    this.clientBootstrap = new ClientBootstrap(channelFactory);

    ChannelPipelineFactory channelPipelineFactory =
        new ChannelPipelineFactory() {
          public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(
                // TODO make Encoder & Decoder - like a Strategy Pattern, and changeable via
                // external settings
                //                        new KryoObjectEncoder(),
                //                        new
                // KryoObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader()))
                // TODO #6
                new ObjectEncoder(),
                new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())),
                new SimpleChannelUpstreamHandler() {

                  @Override
                  public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e)
                      throws Exception {
                    if (e instanceof ChannelStateEvent
                        && ((ChannelStateEvent) e).getState() != ChannelState.INTEREST_OPS) {
                      LOG.info(e.toString());
                    }
                    super.handleUpstream(ctx, e);
                  }

                  @Override
                  public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent event) {
                    channel = event.getChannel();
                    // Send the first message
                    // channel.write(firstMessage);
                  }

                  @Override
                  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
                    // Get
                    serverListener(e.getMessage());
                    ctx.sendUpstream(e);

                    // e.getChannel().write(e.getStatus());
                    // e.getChannel().close();
                  }

                  @Override
                  public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
                    LOG.warn("Unexpected exception from downstream.", e.getCause());
                    e.getChannel().close();
                  }
                });
          }
        };

    // Set up the pipeline factory.
    this.clientBootstrap.setPipelineFactory(channelPipelineFactory);

    // Start the connection attempt.
    // ChannelFuture channelFuture = this.clientBootstrap.connect(new InetSocketAddress(host,
    // setPort));
    LOG.debug("CLIENT GOING TO CONNECT - host:" + host + " post:" + port);
    this.channelFuture = this.clientBootstrap.connect(new InetSocketAddress(host, port));

    // INFO

    // correct shut down a client
    // see http://netty.io/3.6/guide/#start.12 - 9.  Shutting Down Your Application

    // http://stackoverflow.com/questions/10911988/shutting-down-netty-server-when-client-connections-are-open
    //    Netty Server Shutdown
    //
    //    Close server channel
    //    Shutdown boss and worker executor
    //    Release server bootstrap resource
    //    Example code
    //
    //    ChannelFuture cf = serverChannel.close();
    //    cf.awaitUninterruptibly();
    //    bossExecutor.shutdown();
    //    workerExecutor.shutdown();
    //    thriftServer.releaseExternalResources();

    // TODO Need to repair it

    boolean goAway = false;
    long countGoAway = 0;
    final long stepGoAway = 50; // ms

    LOG.warn("Warming up 0.6.0-SNAPSHOT ...");
    while (goAway == false | countGoAway < (SERVER_CONNECTION_TIMEOUT / stepGoAway)) {

      Thread.sleep(stepGoAway);
      if (!channelFuture.isSuccess()) {
        this.running = false;
        goAway = true;
      } else {
        this.running = true;
        goAway = true;
        countGoAway = (SERVER_CONNECTION_TIMEOUT / stepGoAway) + 10;
      }
      countGoAway++;
      LOG.warn(
          "Count down for connection tomeout:"
              + countGoAway * stepGoAway
              + ":ms future:"
              + channelFuture.isSuccess()
              + " running:"
              + this.running);
    }

    if (this.running == false) {
      LOG.warn("After ");
      channelFuture.getCause().printStackTrace();
      channelFactory.releaseExternalResources();
      channelFuture = null;
    }
  }
 private void shutDownChannelFuture(ChannelFuture channelFutureLocal) {
   channelFutureLocal.getCause().printStackTrace();
   channelFactory.releaseExternalResources();
   channelFutureLocal = null;
   running = false;
 }
 @Override
 public void releaseExternalResources() {
   for (ChannelFactory channelFactory : channelFactories) {
     channelFactory.releaseExternalResources();
   }
 }
 @Override
 public void shutdown() {
   for (ChannelFactory channelFactory : channelFactories) {
     channelFactory.shutdown();
   }
 }