Java Code Examples for org.jboss.netty.channel.ChannelFuture

The following are top voted examples for showing how to use org.jboss.netty.channel.ChannelFuture. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: HeliosStreams   File: Netty3JSONResponse.java   Source Code and License 7 votes vote down vote up
/**
 * Sends this response to all the passed channels as a {@link TextWebSocketFrame}
 * @param listener A channel future listener to attach to each channel future. Ignored if null.
 * @param channels The channels to send this response to
 * @return An array of the futures for the write of this response to each channel written to
 */
public ChannelFuture[] send(ChannelFutureListener listener, Channel...channels) {
	if(channels!=null && channels.length>0) {
		Set<ChannelFuture> futures = new HashSet<ChannelFuture>(channels.length);
		if(opCode==null) {
			opCode = "ok";
		}
		TextWebSocketFrame frame = new TextWebSocketFrame(this.toChannelBuffer());
		for(Channel channel: channels) {
			if(channel!=null && channel.isWritable()) {
				ChannelFuture cf = Channels.future(channel);
				if(listener!=null) cf.addListener(listener);
				channel.getPipeline().sendDownstream(new DownstreamMessageEvent(channel, cf, frame, channel.getRemoteAddress()));
				futures.add(cf);
			}
		}
		return futures.toArray(new ChannelFuture[futures.size()]);
	}		
	return EMPTY_CHANNEL_FUTURE_ARR;
}
 
Example 2
Project: EatDubbo   File: NettyChannel.java   Source Code and License 6 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);
    
    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example 3
Project: dubbo2   File: NettyChannel.java   Source Code and License 6 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);
    
    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example 4
Project: https-github.com-apache-zookeeper   File: NettyServerCnxnFactory.java   Source Code and License 6 votes vote down vote up
@Override
public void channelConnected(ChannelHandlerContext ctx,
        ChannelStateEvent e) throws Exception
{
    if (LOG.isTraceEnabled()) {
        LOG.trace("Channel connected " + e);
    }

    NettyServerCnxn cnxn = new NettyServerCnxn(ctx.getChannel(),
            zkServer, NettyServerCnxnFactory.this);
    ctx.setAttachment(cnxn);

    if (secure) {
        SslHandler sslHandler = ctx.getPipeline().get(SslHandler.class);
        ChannelFuture handshakeFuture = sslHandler.handshake();
        handshakeFuture.addListener(new CertificateVerifier(sslHandler, cnxn));
    } else {
        allChannels.add(ctx.getChannel());
        addCnxn(cnxn);
    }
}
 
Example 5
Project: dubbox-hystrix   File: NettyChannel.java   Source Code and License 6 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);
    
    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example 6
Project: hadoop   File: TestShuffleHandler.java   Source Code and License 6 votes vote down vote up
/**
 * Validate shuffle connection and input/output metrics.
 *
 * @throws Exception exception
 */
@Test (timeout = 10000)
public void testShuffleMetrics() throws Exception {
  MetricsSystem ms = new MetricsSystemImpl();
  ShuffleHandler sh = new ShuffleHandler(ms);
  ChannelFuture cf = make(stub(ChannelFuture.class).
      returning(true, false).from.isSuccess());

  sh.metrics.shuffleConnections.incr();
  sh.metrics.shuffleOutputBytes.incr(1*MiB);
  sh.metrics.shuffleConnections.incr();
  sh.metrics.shuffleOutputBytes.incr(2*MiB);

  checkShuffleMetrics(ms, 3*MiB, 0 , 0, 2);

  sh.metrics.operationComplete(cf);
  sh.metrics.operationComplete(cf);

  checkShuffleMetrics(ms, 3*MiB, 1, 1, 0);
}
 
Example 7
Project: hadoop   File: SimpleTcpClient.java   Source Code and License 6 votes vote down vote up
public void run() {
  // Configure the client.
  ChannelFactory factory = new NioClientSocketChannelFactory(
      Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1);
  ClientBootstrap bootstrap = new ClientBootstrap(factory);

  // Set up the pipeline factory.
  bootstrap.setPipelineFactory(setPipelineFactory());

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

  // Start the connection attempt.
  ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

  if (oneShot) {
    // Wait until the connection is closed or the connection attempt fails.
    future.getChannel().getCloseFuture().awaitUninterruptibly();

    // Shut down thread pools to exit.
    bootstrap.releaseExternalResources();
  }
}
 
Example 8
Project: iTAP-controller   File: RPCService.java   Source Code and License 6 votes vote down vote up
@Override
public void operationComplete(ChannelFuture cf) throws Exception {
    if (!cf.isSuccess()) {
        synchronized (connections) {
            NodeConnection c = connections.remove(node.getNodeId());
            if (c != null) c.nuke();
            cf.getChannel().close();
        }
        
        String message = "[unknown error]";
        if (cf.isCancelled()) message = "Timed out on connect";
        if (cf.getCause() != null) message = cf.getCause().getMessage();
        logger.debug("[{}->{}] Could not connect to RPC " +
                     "node: {}", 
                     new Object[]{syncManager.getLocalNodeId(), 
                                  node.getNodeId(), 
                                  message});
    } else {
        logger.trace("[{}->{}] Channel future successful", 
                     syncManager.getLocalNodeId(), 
                     node.getNodeId());
    }
}
 
Example 9
Project: iTAP-controller   File: Bootstrap.java   Source Code and License 6 votes vote down vote up
public boolean bootstrap(HostAndPort seed, 
                         Node localNode) throws SyncException {
    this.localNode = localNode;
    succeeded = false;
    SocketAddress sa =
            new InetSocketAddress(seed.getHostText(), seed.getPort());
    ChannelFuture future = bootstrap.connect(sa);
    future.awaitUninterruptibly();
    if (!future.isSuccess()) {
        logger.debug("Could not connect to " + seed, future.getCause());
        return false;
    }
    Channel channel = future.getChannel();
    logger.debug("[{}] Connected to {}", 
                 localNode != null ? localNode.getNodeId() : null,
                 seed);
    
    try {
        channel.getCloseFuture().await();
    } catch (InterruptedException e) {
        logger.debug("Interrupted while waiting for bootstrap");
        return succeeded;
    }
    return succeeded;
}
 
Example 10
Project: iTAP-controller   File: RemoteSyncManager.java   Source Code and License 6 votes vote down vote up
protected boolean connect(String hostname, int port) {
    ready = false;
    if (channel == null || !channel.isConnected()) {
        SocketAddress sa =
                new InetSocketAddress(hostname, port);
        ChannelFuture future = clientBootstrap.connect(sa);
        future.awaitUninterruptibly();
        if (!future.isSuccess()) {
            logger.error("Could not connect to " + hostname + 
                         ":" + port, future.getCause());
            return false;
        }
        channel = future.getChannel();
    }
    while (!ready && channel != null && channel.isConnected()) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) { }
    }
    if (!ready || channel == null || !channel.isConnected()) {
        logger.warn("Timed out connecting to {}:{}", hostname, port);
        return false;
    }
    logger.debug("Connected to {}:{}", hostname, port);
    return true;
}
 
Example 11
Project: dubbocloud   File: NettyChannel.java   Source Code and License 6 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);
    
    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example 12
Project: aliyun-oss-hadoop-fs   File: TestShuffleHandler.java   Source Code and License 6 votes vote down vote up
/**
 * Validate shuffle connection and input/output metrics.
 *
 * @throws Exception exception
 */
@Test (timeout = 10000)
public void testShuffleMetrics() throws Exception {
  MetricsSystem ms = new MetricsSystemImpl();
  ShuffleHandler sh = new ShuffleHandler(ms);
  ChannelFuture cf = make(stub(ChannelFuture.class).
      returning(true, false).from.isSuccess());

  sh.metrics.shuffleConnections.incr();
  sh.metrics.shuffleOutputBytes.incr(1*MiB);
  sh.metrics.shuffleConnections.incr();
  sh.metrics.shuffleOutputBytes.incr(2*MiB);

  checkShuffleMetrics(ms, 3*MiB, 0 , 0, 2);

  sh.metrics.operationComplete(cf);
  sh.metrics.operationComplete(cf);

  checkShuffleMetrics(ms, 3*MiB, 1, 1, 0);
}
 
Example 13
Project: aliyun-oss-hadoop-fs   File: TestShuffleHandler.java   Source Code and License 6 votes vote down vote up
public ChannelFuture createMockChannelFuture(Channel mockCh,
    final List<ShuffleHandler.ReduceMapFileCount> listenerList) {
  final ChannelFuture mockFuture = Mockito.mock(ChannelFuture.class);
  Mockito.when(mockFuture.getChannel()).thenReturn(mockCh);
  Mockito.doReturn(true).when(mockFuture).isSuccess();
  Mockito.doAnswer(new Answer() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      //Add ReduceMapFileCount listener to a list
      if (invocation.getArguments()[0].getClass() ==
          ShuffleHandler.ReduceMapFileCount.class)
        listenerList.add((ShuffleHandler.ReduceMapFileCount)
            invocation.getArguments()[0]);
      return null;
    }
  }).when(mockFuture).addListener(Mockito.any(
      ShuffleHandler.ReduceMapFileCount.class));
  return mockFuture;
}
 
Example 14
Project: aliyun-oss-hadoop-fs   File: SimpleTcpClient.java   Source Code and License 6 votes vote down vote up
public void run() {
  // Configure the client.
  ChannelFactory factory = new NioClientSocketChannelFactory(
      Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1);
  ClientBootstrap bootstrap = new ClientBootstrap(factory);

  // Set up the pipeline factory.
  bootstrap.setPipelineFactory(setPipelineFactory());

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

  // Start the connection attempt.
  ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

  if (oneShot) {
    // Wait until the connection is closed or the connection attempt fails.
    future.getChannel().getCloseFuture().awaitUninterruptibly();

    // Shut down thread pools to exit.
    bootstrap.releaseExternalResources();
  }
}
 
Example 15
Project: dubbos   File: NettyChannel.java   Source Code and License 6 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);
    
    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example 16
Project: bigstreams   File: ServerUtil.java   Source Code and License 6 votes vote down vote up
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
		throws Exception {
	super.messageReceived(ctx, e);

	System.out.println("-------- Server  Channel messageRecieved "
			+ System.currentTimeMillis());

	if (induceError.get()) {
		System.out
				.println("Inducing Error in Server messageReceived method");
		throw new IOException("Induced error ");
	}

	MessageEventBag bag = new MessageEventBag();
	bag.setBytes(e);
	bagList.add(bag);

	ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
	buffer.writeInt(200);

	ChannelFuture future = e.getChannel().write(buffer);

	future.addListener(ChannelFutureListener.CLOSE);

}
 
Example 17
Project: bigstreams   File: ServerUtil.java   Source Code and License 6 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
		throws Exception {
	System.out.println("Server Exception Caught");
	e.getCause().printStackTrace();

	/**
	 * Very important to respond here.
	 * The agent will always be listening for some kind of feedback.
	 */
	ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
	buffer.writeInt(500);

	ChannelFuture future = e.getChannel().write(buffer);

	future.addListener(ChannelFutureListener.CLOSE);

}
 
Example 18
Project: QoS-floodlight   File: RPCService.java   Source Code and License 6 votes vote down vote up
@Override
public void operationComplete(ChannelFuture cf) throws Exception {
    if (!cf.isSuccess()) {
        synchronized (connections) {
            NodeConnection c = connections.remove(node.getNodeId());
            if (c != null) c.nuke();
            cf.getChannel().close();
        }
        
        String message = "[unknown error]";
        if (cf.isCancelled()) message = "Timed out on connect";
        if (cf.getCause() != null) message = cf.getCause().getMessage();
        logger.debug("[{}->{}] Could not connect to RPC " +
                     "node: {}", 
                     new Object[]{syncManager.getLocalNodeId(), 
                                  node.getNodeId(), 
                                  message});
    } else {
        logger.trace("[{}->{}] Channel future successful", 
                     syncManager.getLocalNodeId(), 
                     node.getNodeId());
    }
}
 
Example 19
Project: QoS-floodlight   File: Bootstrap.java   Source Code and License 6 votes vote down vote up
public boolean bootstrap(HostAndPort seed, 
                         Node localNode) throws SyncException {
    this.localNode = localNode;
    succeeded = false;
    SocketAddress sa =
            new InetSocketAddress(seed.getHostText(), seed.getPort());
    ChannelFuture future = bootstrap.connect(sa);
    future.awaitUninterruptibly();
    if (!future.isSuccess()) {
        logger.debug("Could not connect to " + seed, future.getCause());
        return false;
    }
    Channel channel = future.getChannel();
    logger.debug("[{}] Connected to {}", 
                 localNode != null ? localNode.getNodeId() : null,
                 seed);
    
    try {
        channel.getCloseFuture().await();
    } catch (InterruptedException e) {
        logger.debug("Interrupted while waiting for bootstrap");
        return succeeded;
    }
    return succeeded;
}
 
Example 20
Project: dubbo-comments   File: NettyChannel.java   Source Code and License 6 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);
    
    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        //FIXME  sent为true的话   要等待数据写完才返回,失败抛出异常   add by jileng
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            // 写超时了,难道不能取消这次写操作?
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example 21
Project: dubbox   File: NettyChannel.java   Source Code and License 6 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);
    
    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example 22
Project: dubbo   File: NettyChannel.java   Source Code and License 6 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);
    
    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example 23
Project: jahhan   File: NettyChannel.java   Source Code and License 6 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);
    
    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example 24
Project: big-c   File: TestShuffleHandler.java   Source Code and License 6 votes vote down vote up
/**
 * Validate shuffle connection and input/output metrics.
 *
 * @throws Exception exception
 */
@Test (timeout = 10000)
public void testShuffleMetrics() throws Exception {
  MetricsSystem ms = new MetricsSystemImpl();
  ShuffleHandler sh = new ShuffleHandler(ms);
  ChannelFuture cf = make(stub(ChannelFuture.class).
      returning(true, false).from.isSuccess());

  sh.metrics.shuffleConnections.incr();
  sh.metrics.shuffleOutputBytes.incr(1*MiB);
  sh.metrics.shuffleConnections.incr();
  sh.metrics.shuffleOutputBytes.incr(2*MiB);

  checkShuffleMetrics(ms, 3*MiB, 0 , 0, 2);

  sh.metrics.operationComplete(cf);
  sh.metrics.operationComplete(cf);

  checkShuffleMetrics(ms, 3*MiB, 1, 1, 0);
}
 
Example 25
Project: big-c   File: SimpleTcpClient.java   Source Code and License 6 votes vote down vote up
public void run() {
  // Configure the client.
  ChannelFactory factory = new NioClientSocketChannelFactory(
      Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1);
  ClientBootstrap bootstrap = new ClientBootstrap(factory);

  // Set up the pipeline factory.
  bootstrap.setPipelineFactory(setPipelineFactory());

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

  // Start the connection attempt.
  ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

  if (oneShot) {
    // Wait until the connection is closed or the connection attempt fails.
    future.getChannel().getCloseFuture().awaitUninterruptibly();

    // Shut down thread pools to exit.
    bootstrap.releaseExternalResources();
  }
}
 
Example 26
Project: dubbo3   File: NettyChannel.java   Source Code and License 6 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent);
    
    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.write(message);
        if (sent) {
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example 27
Project: fastcatsearch3   File: TransportChannel.java   Source Code and License 6 votes vote down vote up
public void sendResponse(Object obj) throws IOException {
	byte type = 0;
	type = TransportOption.setTypeMessage(type);
	byte status = 0;
	status = TransportOption.setResponse(status);
	status = TransportOption.setResponseObject(status);
	byte resType = 0;
	resType = TransportOption.setResponseObject(resType);
    CachedStreamOutput.Entry cachedEntry = CachedStreamOutput.popEntry();
    BytesStreamOutput stream = cachedEntry.bytes();
    stream.skip(MessageProtocol.HEADER_SIZE);
    stream.writeGenericValue(obj);
    stream.close();
    
    ChannelBuffer buffer = stream.bytesReference().toChannelBuffer();
    MessageProtocol.writeHeader(buffer, type, requestId, status);
    ChannelFuture future = channel.write(buffer);
    future.addListener(new TransportModule.CacheFutureListener(cachedEntry));
}
 
Example 28
Project: fastcatsearch3   File: TransportChannel.java   Source Code and License 6 votes vote down vote up
public void sendResponse(Streamable response) throws IOException {
	byte type = 0;
	type = TransportOption.setTypeMessage(type);
	byte status = 0;
	status = TransportOption.setResponse(status);
    CachedStreamOutput.Entry cachedEntry = CachedStreamOutput.popEntry();
    BytesStreamOutput stream = cachedEntry.bytes();
    stream.skip(MessageProtocol.HEADER_SIZE);
    stream.writeString(response.getClass().getName());
    response.writeTo(stream);
    stream.close();
    
    ChannelBuffer buffer = stream.bytesReference().toChannelBuffer();
    MessageProtocol.writeHeader(buffer, type, requestId, status);
    ChannelFuture future = channel.write(buffer);
    future.addListener(new TransportModule.CacheFutureListener(cachedEntry));
}
 
Example 29
Project: Camel   File: NettyHelper.java   Source Code and License 6 votes vote down vote up
/**
 * Writes the given body to Netty channel. Will <b>not</b >wait until the body has been written.
 *
 * @param log             logger to use
 * @param channel         the Netty channel
 * @param remoteAddress   the remote address when using UDP
 * @param body            the body to write (send)
 * @param exchange        the exchange
 * @param listener        listener with work to be executed when the operation is complete
 */
public static void writeBodyAsync(Logger log, Channel channel, SocketAddress remoteAddress, Object body,
                                  Exchange exchange, ChannelFutureListener listener) {
    ChannelFuture future;
    if (remoteAddress != null) {
        if (log.isDebugEnabled()) {
            log.debug("Channel: {} remote address: {} writing body: {}", new Object[]{channel, remoteAddress, body});
        }
        future = channel.write(body, remoteAddress);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Channel: {} writing body: {}", new Object[]{channel, body});
        }
        future = channel.write(body);
    }

    if (listener != null) {
        future.addListener(listener);
    }
}
 
Example 30
Project: HeliosStreams   File: Netty3JSONResponse.java   Source Code and License 6 votes vote down vote up
/**
 * Sends this response to all the passed channels as a {@link TextWebSocketFrame}
 * @param listener A channel future listener to attach to each channel future. Ignored if null.
 * @param channels The channels to send this response to
 * @return An array of the futures for the write of this response to each channel written to
 */
public ChannelFuture[] send(ChannelFutureListener listener, Channel...channels) {
	if(channels!=null && channels.length>0) {
		Set<ChannelFuture> futures = new HashSet<ChannelFuture>(channels.length);
		if(opCode==null) {
			opCode = "ok";
		}
		TextWebSocketFrame frame = new TextWebSocketFrame(this.toChannelBuffer());
		for(Channel channel: channels) {
			if(channel!=null && channel.isWritable()) {
				ChannelFuture cf = Channels.future(channel);
				if(listener!=null) cf.addListener(listener);
				channel.getPipeline().sendDownstream(new DownstreamMessageEvent(channel, cf, frame, channel.getRemoteAddress()));
				futures.add(cf);
			}
		}
		return futures.toArray(new ChannelFuture[futures.size()]);
	}		
	return EMPTY_CHANNEL_FUTURE_ARR;
}
 
Example 31
Project: HeliosStreams   File: InvocationChannel.java   Source Code and License 6 votes vote down vote up
/**
 * {@inheritDoc}
 * @see org.jboss.netty.channel.Channel#write(java.lang.Object)
 */
@Override
public ChannelFuture write(Object message) {
	if(message!=null) {
		if(message instanceof FileRegion) {
			try {
				Pipe pipe = Pipe.open();
				FileRegion fr = (FileRegion)message;
				
				long bytesToRead = fr.getCount();
				fr.transferTo(pipe.sink(), 0L);
				byte[] content = new byte[(int)bytesToRead];
				pipe.source().read(ByteBuffer.wrap(content));
				channelWrites.add(content);
			} catch (Exception ex) {
				log.error("Failed to read content from pipe", ex);
				channelWrites.add(ex);
			}
		} else {
			channelWrites.add(message);
		}
		log.info("Received Channel Write [{}]  type:[{}]", message, message.getClass().getName());
	}
	
	return Channels.succeededFuture(this);
}
 
Example 32
Project: SecureKeeper   File: NettyServerCnxnFactory.java   Source Code and License 6 votes vote down vote up
@Override
public void channelConnected(ChannelHandlerContext ctx,
        ChannelStateEvent e) throws Exception
{
    if (LOG.isTraceEnabled()) {
        LOG.trace("Channel connected " + e);
    }

    NettyServerCnxn cnxn = new NettyServerCnxn(ctx.getChannel(),
            zkServer, NettyServerCnxnFactory.this);
    ctx.setAttachment(cnxn);

    //SECUREKEEPER: Enable ssl only if specified
    //if (secure) {
    if(encryption.equals("ssl")){
        SslHandler sslHandler = ctx.getPipeline().get(SslHandler.class);
        ChannelFuture handshakeFuture = sslHandler.handshake();
        handshakeFuture.addListener(new CertificateVerifier(sslHandler, cnxn));
    } else {
        allChannels.add(ctx.getChannel());
        addCnxn(cnxn);
    }
}
 
Example 33
Project: dubbo-learning   File: NettyChannel.java   Source Code and License 6 votes vote down vote up
public void send(Object message, boolean sent) throws RemotingException {
    super.send(message, sent); //调用父类的send方法进行异常判断
    
    boolean success = true;
    int timeout = 0;
    try {
        //NIO框架通知执行写操作
        ChannelFuture future = channel.write(message);
        if (sent) {//如果已经发送成功
            timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        //获得失败原因
        Throwable cause = future.getCause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
    }
    
    if(! success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}
 
Example 34
Project: nfs-client-java   File: Connection.java   Source Code and License 6 votes vote down vote up
/**
 * This attempts to bind to privileged ports, starting with 1023 and working downwards, and returns when the first binding succeeds.
 * 
 * <p>
 * Some NFS servers apparently may require that some requests originate on
 * an Internet port below IPPORT_RESERVED (1024). This is generally not
 * used, though, as the client then has to run as a user authorized for
 * privileged, which is dangerous. It is also not generally needed.
 * </p>
 * 
 * @return
 *         <ul>
 *         <li><code>true</code> if the binding succeeds,</li>
 *         <li><code>false</code> otherwise.</li>
 *         </ul>
 * @throws RpcException If an exception occurs, or if no binding succeeds.
 */
private Channel bindToPrivilegedPort() throws RpcException {
    System.out.println("Attempting to use privileged port.");
    for (int port = 1023; port > 0; --port) {
        try {
            ChannelPipeline pipeline = _clientBootstrap.getPipelineFactory().getPipeline();
            Channel channel = _clientBootstrap.getFactory().newChannel(pipeline);
            channel.getConfig().setOptions(_clientBootstrap.getOptions());
            ChannelFuture bindFuture = channel.bind(new InetSocketAddress(port)).awaitUninterruptibly();
            if (bindFuture.isSuccess()) {
                System.out.println("Success! Bound to port " + port);
                return bindFuture.getChannel();
            }
        } catch (Exception e) {
            String msg = String.format("rpc request bind error for address: %s", 
                    getRemoteAddress());
            throw new RpcException(RpcStatus.NETWORK_ERROR, msg, e);
        }
    }

    throw new RpcException(RpcStatus.LOCAL_BINDING_ERROR, String.format("Cannot bind a port < 1024: %s", getRemoteAddress()));
}
 
Example 35
Project: voyage   File: InvokeFuture.java   Source Code and License 5 votes vote down vote up
/**
 * 发送请求
 */
public void send() {
	ChannelFuture writeFuture = channel.write(request);
	//阻塞等待,若超时则返回已完成和失败
	boolean ret = writeFuture.awaitUninterruptibly(1000, TimeUnit.MILLISECONDS);
	if (ret && writeFuture.isSuccess()) {
		return;
	} else if(writeFuture.getCause() != null) {
		invokeMap.remove(request.getRequestID());
		throw new RpcException(writeFuture.getCause());
	} else {
		invokeMap.remove(request.getRequestID());
		throw new RpcException("sendRequest error");
	}
}
 
Example 36
Project: voyage   File: NettyRpcConnection.java   Source Code and License 5 votes vote down vote up
/**
 * 尝试连接
 */
public void connect() {
       ChannelFuture future = bootstrap.connect(inetAddr);
       try{
           boolean ret = future.awaitUninterruptibly(Constants.TIMEOUT_CONNECTION_MILLSECOND, TimeUnit.MILLISECONDS);
           if (ret && future.isSuccess()) {
               Channel newChannel = future.getChannel();
               newChannel.setInterestOps(Channel.OP_READ_WRITE);
               try {
                   // 关闭旧的连接
                   Channel oldChannel = NettyRpcConnection.this.channel;
                   if (oldChannel != null) {
                       logger.info("Close old netty channel {} on create new netty channel {}", oldChannel, newChannel);
                       oldChannel.close();
                   }
               } finally {
                   if (!isConnected()) {
                       try {
                           logger.info("Close new netty channel {}, because the client closed.", newChannel);
                           newChannel.close();
                       } finally {
                       	NettyRpcConnection.this.channel = null;
                       }
                   } else {
                   	NettyRpcConnection.this.channel = newChannel;
                   }
               }
           } else if (null != future.getCause()) {
           	logger.error("connect fail", future.getCause());
           	throw new RuntimeException("connect error", future.getCause());
           } else {
           	logger.error("connect fail,connstr: "+this.getConnStr());
           	throw new RuntimeException("connect error");
           }
       }finally{
           if (! isConnected()) {
               future.cancel();
           }
       }
}
 
Example 37
Project: fuck_zookeeper   File: NettyServerCnxn.java   Source Code and License 5 votes vote down vote up
private ChannelFuture disableRecvNoWait() {
    throttled = true;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Throttling - disabling recv " + this);
    }
    return channel.setReadable(false);
}
 
Example 38
Project: https-github.com-apache-zookeeper   File: NettyServerCnxnFactory.java   Source Code and License 5 votes vote down vote up
/**
 * Only allow the connection to stay open if certificate passes auth
 */
public void operationComplete(ChannelFuture future)
        throws SSLPeerUnverifiedException {
    if (future.isSuccess()) {
        LOG.debug("Successful handshake with session 0x{}",
                Long.toHexString(cnxn.sessionId));
        SSLEngine eng = sslHandler.getEngine();
        SSLSession session = eng.getSession();
        cnxn.setClientCertificateChain(session.getPeerCertificates());

        String authProviderProp
                = System.getProperty(ZKConfig.SSL_AUTHPROVIDER, "x509");

        X509AuthenticationProvider authProvider =
                (X509AuthenticationProvider)
                        ProviderRegistry.getProvider(authProviderProp);

        if (authProvider == null) {
            LOG.error("Auth provider not found: {}", authProviderProp);
            cnxn.close();
            return;
        }

        if (KeeperException.Code.OK !=
                authProvider.handleAuthentication(cnxn, null)) {
            LOG.error("Authentication failed for session 0x{}",
                    Long.toHexString(cnxn.sessionId));
            cnxn.close();
            return;
        }

        allChannels.add(future.getChannel());
        addCnxn(cnxn);
    } else {
        LOG.error("Unsuccessful handshake with session 0x{}",
                Long.toHexString(cnxn.sessionId));
        cnxn.close();
    }
}
 
Example 39
Project: https-github.com-apache-zookeeper   File: NettyServerCnxn.java   Source Code and License 5 votes vote down vote up
private ChannelFuture disableRecvNoWait() {
    throttled = true;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Throttling - disabling recv " + this);
    }
    return channel.setReadable(false);
}
 
Example 40
Project: Elasticsearch   File: NettyTransport.java   Source Code and License 5 votes vote down vote up
protected void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    if (!lifecycle.started()) {
        // ignore
        return;
    }
    if (isCloseConnectionException(e.getCause())) {
        logger.trace("close connection exception caught on transport layer [{}], disconnecting from relevant node", e.getCause(), ctx.getChannel());
        // close the channel, which will cause a node to be disconnected if relevant
        ctx.getChannel().close();
        disconnectFromNodeChannel(ctx.getChannel(), e.getCause());
    } else if (isConnectException(e.getCause())) {
        logger.trace("connect exception caught on transport layer [{}]", e.getCause(), ctx.getChannel());
        // close the channel as safe measure, which will cause a node to be disconnected if relevant
        ctx.getChannel().close();
        disconnectFromNodeChannel(ctx.getChannel(), e.getCause());
    } else if (e.getCause() instanceof CancelledKeyException) {
        logger.trace("cancelled key exception caught on transport layer [{}], disconnecting from relevant node", e.getCause(), ctx.getChannel());
        // close the channel as safe measure, which will cause a node to be disconnected if relevant
        ctx.getChannel().close();
        disconnectFromNodeChannel(ctx.getChannel(), e.getCause());
    } else if (e.getCause() instanceof SizeHeaderFrameDecoder.HttpOnTransportException) {
        // in case we are able to return data, serialize the exception content and sent it back to the client
        if (ctx.getChannel().isOpen()) {
            ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(e.getCause().getMessage().getBytes(Charsets.UTF_8));
            ChannelFuture channelFuture = ctx.getChannel().write(buffer);
            channelFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    future.getChannel().close();
                }
            });
        }
    } else {
        logger.warn("exception caught on transport layer [{}], closing connection", e.getCause(), ctx.getChannel());
        // close the channel, which will cause a node to be disconnected if relevant
        ctx.getChannel().close();
        disconnectFromNodeChannel(ctx.getChannel(), e.getCause());
    }
}
 
Example 41
Project: Elasticsearch   File: NettyTransport.java   Source Code and License 5 votes vote down vote up
protected NodeChannels connectToChannelsLight(DiscoveryNode node) {
    InetSocketAddress address = ((InetSocketTransportAddress) node.address()).address();
    ChannelFuture connect = clientBootstrap.connect(address);
    connect.awaitUninterruptibly((long) (connectTimeout.millis() * 1.5));
    if (!connect.isSuccess()) {
        throw new ConnectTransportException(node, "connect_timeout[" + connectTimeout + "]", connect.getCause());
    }
    Channel[] channels = new Channel[1];
    channels[0] = connect.getChannel();
    channels[0].getCloseFuture().addListener(new ChannelCloseListener(node));
    return new NodeChannels(channels, channels, channels, channels, channels);
}
 
Example 42
Project: Elasticsearch   File: NettyTransport.java   Source Code and License 5 votes vote down vote up
@Override
public void operationComplete(final ChannelFuture future) throws Exception {
    NodeChannels nodeChannels = connectedNodes.get(node);
    if (nodeChannels != null && nodeChannels.hasChannel(future.getChannel())) {
        threadPool().generic().execute(new Runnable() {
            @Override
            public void run() {
                disconnectFromNode(node, future.getChannel(), "channel closed event");
            }
        });
    }
}
 
Example 43
Project: Elasticsearch   File: NettyTransport.java   Source Code and License 5 votes vote down vote up
public synchronized void close() {
    List<ChannelFuture> futures = new ArrayList<>();
    for (Channel channel : allChannels) {
        try {
            if (channel != null && channel.isOpen()) {
                futures.add(channel.close());
            }
        } catch (Exception e) {
            //ignore
        }
    }
    for (ChannelFuture future : futures) {
        future.awaitUninterruptibly();
    }
}
 
Example 44
Project: Elasticsearch   File: NettyTransportChannel.java   Source Code and License 5 votes vote down vote up
@Override
public void sendResponse(TransportResponse response, TransportResponseOptions options) throws IOException {
    if (transport.compress) {
        options = TransportResponseOptions.builder(options).withCompress(transport.compress).build();
    }

    byte status = 0;
    status = TransportStatus.setResponse(status);

    ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(transport.bigArrays);
    boolean addedReleaseListener = false;
    try {
        bStream.skip(NettyHeader.HEADER_SIZE);
        StreamOutput stream = bStream;
        if (options.compress()) {
            status = TransportStatus.setCompress(status);
            stream = CompressorFactory.defaultCompressor().streamOutput(stream);
        }
        stream.setVersion(version);
        response.writeTo(stream);
        stream.close();

        ReleasablePagedBytesReference bytes = bStream.bytes();
        ChannelBuffer buffer = bytes.toChannelBuffer();
        NettyHeader.writeHeader(buffer, requestId, status, version);
        ChannelFuture future = channel.write(buffer);
        ReleaseChannelFutureListener listener = new ReleaseChannelFutureListener(bytes);
        future.addListener(listener);
        addedReleaseListener = true;
        transportServiceAdapter.onResponseSent(requestId, action, response, options);
    } finally {
        if (!addedReleaseListener) {
            Releasables.close(bStream.bytes());
        }
    }
}
 
Example 45
Project: hadoop   File: ShuffleHandler.java   Source Code and License 5 votes vote down vote up
@Override
public void operationComplete(ChannelFuture future) throws Exception {
  if (future.isSuccess()) {
    shuffleOutputsOK.incr();
  } else {
    shuffleOutputsFailed.incr();
  }
  shuffleConnections.decr();
}
 
Example 46
Project: athena   File: Controller.java   Source Code and License 5 votes vote down vote up
@Override
public void run() {
    log.debug("Connect to peer {}", OspfUtil.SHOST);
    initConnection();
    ospfChannelHandler.sentConfigPacket(configPacket);
    InetSocketAddress connectToSocket = new InetSocketAddress(OspfUtil.SHOST, isisPort.toInt());
    try {
        peerBootstrap.connect(connectToSocket).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    connectRetryCounter++;
                    log.error("Connection failed, ConnectRetryCounter {} remote host {}", connectRetryCounter,
                              OspfUtil.SHOST);
                    /*
                     * Reconnect to peer on failure is exponential till 4 mins, later on retry after every 4
                     * mins.
                     */
                    if (connectRetryTime < RETRY_INTERVAL) {
                        connectRetryTime = (connectRetryTime != 0) ? connectRetryTime * 2 : 1;
                    }
                    scheduleConnectionRetry(connectRetryTime);
                } else {
                    //Send the config packet
                    ospfChannelHandler.sentConfigPacket(configPacket);
                    connectRetryCounter++;
                    log.info("Connected to remote host {}, Connect Counter {}", OspfUtil.SHOST,
                             connectRetryCounter);
                    disconnectExecutor();

                    return;
                }
            }
        });
    } catch (Exception e) {
        log.info("Connect peer exception : " + e.toString());
        disconnectExecutor();
    }
}
 
Example 47
Project: athena   File: Controller.java   Source Code and License 5 votes vote down vote up
@Override
public void run() {
    log.debug("Connect to peer {}", IsisConstants.SHOST);
    initConnection();
    isisChannelHandler.sentConfigPacket(configPacket);
    InetSocketAddress connectToSocket = new InetSocketAddress(IsisConstants.SHOST, isisPort.toInt());
    try {
        peerBootstrap.connect(connectToSocket).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    connectRetryCounter++;
                    log.error("Connection failed, ConnectRetryCounter {} remote host {}", connectRetryCounter,
                            IsisConstants.SHOST);
                    /*
                     * Reconnect to peer on failure is exponential till 4 mins, later on retry after every 4
                     * mins.
                     */
                    if (connectRetryTime < RETRY_INTERVAL) {
                        connectRetryTime = (connectRetryTime != 0) ? connectRetryTime * 2 : 1;
                    }
                    scheduleConnectionRetry(connectRetryTime);
                } else {
                    //Send the config packet
                    isisChannelHandler.sentConfigPacket(configPacket);
                    connectRetryCounter++;
                    log.info("Connected to remote host {}, Connect Counter {}", IsisConstants.SHOST,
                            connectRetryCounter);
                    disconnectExecutor();

                    return;
                }
            }
        });
    } catch (Exception e) {
        log.info("Connect peer exception : " + e.toString());
        disconnectExecutor();
    }
}
 
Example 48
Project: athena   File: BgpConnectPeerImpl.java   Source Code and License 5 votes vote down vote up
@Override
public void run() {
    log.debug("Connect to peer {}", peerHost);

    InetSocketAddress connectToSocket = new InetSocketAddress(peerHost, peerPort);

    try {
        bgpconfig.setPeerConnState(peerHost, BgpPeerCfg.State.CONNECT);
        peerBootstrap.connect(connectToSocket).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    bgpconfig.setPeerConnState(peerHost, BgpPeerCfg.State.ACTIVE);
                    connectRetryCounter++;
                    log.error("Connection failed, ConnectRetryCounter {} remote host {}", connectRetryCounter,
                              peerHost);
                    /*
                     * Reconnect to peer on failure is exponential till 4 mins, later on retry after every 4
                     * mins.
                     */
                    if (connectRetryTime < RETRY_INTERVAL) {
                        connectRetryTime = (connectRetryTime != 0) ? connectRetryTime * 2 : 1;
                    }
                    scheduleConnectionRetry(connectRetryTime);
                } else {

                    connectRetryCounter++;
                    log.info("Connected to remote host {}, Connect Counter {}", peerHost, connectRetryCounter);
                    disconnectPeer();
                    return;
                }
            }
        });
    } catch (Exception e) {
        log.info("Connect peer exception : " + e.toString());
        disconnectPeer();
    }
}
 
Example 49
Project: ZooKeeper   File: NettyServerCnxn.java   Source Code and License 5 votes vote down vote up
private ChannelFuture disableRecvNoWait() {
    throttled = true;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Throttling - disabling recv " + this);
    }
    return channel.setReadable(false);
}
 
Example 50
Project: aliyun-oss-hadoop-fs   File: ShuffleHandler.java   Source Code and License 5 votes vote down vote up
@Override
public void operationComplete(ChannelFuture future) throws Exception {
  if (future.isSuccess()) {
    shuffleOutputsOK.incr();
  } else {
    shuffleOutputsFailed.incr();
  }
  shuffleConnections.decr();
}
 
Example 51
Project: aliyun-oss-hadoop-fs   File: ShuffleHandler.java   Source Code and License 5 votes vote down vote up
@Override
public void operationComplete(ChannelFuture future) throws Exception {
  if (!future.isSuccess()) {
    future.getChannel().close();
    return;
  }
  int waitCount = this.reduceContext.getMapsToWait().decrementAndGet();
  if (waitCount == 0) {
    metrics.operationComplete(future);
    future.getChannel().close();
  } else {
    pipelineFact.getSHUFFLE().sendMap(reduceContext);
  }
}
 
Example 52
Project: aliyun-oss-hadoop-fs   File: ShuffleHandler.java   Source Code and License 5 votes vote down vote up
/**
 * Calls sendMapOutput for the mapId pointed by ReduceContext.mapsToSend
 * and increments it. This method is first called by messageReceived()
 * maxSessionOpenFiles times and then on the completion of every
 * sendMapOutput operation. This limits the number of open files on a node,
 * which can get really large(exhausting file descriptors on the NM) if all
 * sendMapOutputs are called in one go, as was done previous to this change.
 * @param reduceContext used to call sendMapOutput with correct params.
 * @return the ChannelFuture of the sendMapOutput, can be null.
 */
public ChannelFuture sendMap(ReduceContext reduceContext)
    throws Exception {

  ChannelFuture nextMap = null;
  if (reduceContext.getMapsToSend().get() <
      reduceContext.getMapIds().size()) {
    int nextIndex = reduceContext.getMapsToSend().getAndIncrement();
    String mapId = reduceContext.getMapIds().get(nextIndex);

    try {
      MapOutputInfo info = reduceContext.getInfoMap().get(mapId);
      if (info == null) {
        info = getMapOutputInfo(reduceContext.getOutputBasePathStr() +
                   mapId, mapId, reduceContext.getReduceId(),
                   reduceContext.getUser());
      }
      nextMap = sendMapOutput(
          reduceContext.getCtx(),
          reduceContext.getCtx().getChannel(),
          reduceContext.getUser(), mapId,
          reduceContext.getReduceId(), info);
      if (null == nextMap) {
        sendError(reduceContext.getCtx(), NOT_FOUND);
        return null;
      }
      nextMap.addListener(new ReduceMapFileCount(reduceContext));
    } catch (IOException e) {
      LOG.error("Shuffle error :", e);
      String errorMessage = getErrorMessage(e);
      sendError(reduceContext.getCtx(), errorMessage,
          INTERNAL_SERVER_ERROR);
      return null;
    }
  }
  return nextMap;
}
 
Example 53
Project: Camel   File: NettyProducer.java   Source Code and License 5 votes vote down vote up
@Override
public Channel makeObject() throws Exception {
    ChannelFuture channelFuture = openConnection();
    Channel answer = openChannel(channelFuture);
    LOG.trace("Created channel: {}", answer);
    return answer;
}
 
Example 54
Project: StreamProcessingInfrastructure   File: NettyServerCnxn.java   Source Code and License 5 votes vote down vote up
private ChannelFuture disableRecvNoWait() {
    throttled = true;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Throttling - disabling recv " + this);
    }
    return channel.setReadable(false);
}
 
Example 55
Project: Camel   File: NettyProducer.java   Source Code and License 5 votes vote down vote up
protected Channel openChannel(ChannelFuture channelFuture) throws Exception {
    // blocking for channel to be done
    if (LOG.isTraceEnabled()) {
        LOG.trace("Waiting for operation to complete {} for {} millis", channelFuture, configuration.getConnectTimeout());
    }
    // here we need to wait it in other thread
    final CountDownLatch channelLatch = new CountDownLatch(1);
    channelFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture cf) throws Exception {
            channelLatch.countDown();
        }
    });
     
    try {
        channelLatch.await(configuration.getConnectTimeout(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException ex) {
        throw new CamelException("Interrupted while waiting for " + "connection to "
                                 + configuration.getAddress());
    }
    

    if (!channelFuture.isDone() || !channelFuture.isSuccess()) {
        ConnectException cause = new ConnectException("Cannot connect to " + configuration.getAddress());
        if (channelFuture.getCause() != null) {
            cause.initCause(channelFuture.getCause());
        }
        throw cause;
    }
    Channel answer = channelFuture.getChannel();
    // to keep track of all channels in use
    allChannels.add(answer);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating connector to address: {}", configuration.getAddress());
    }
    return answer;
}
 
Example 56
Project: bigstreams   File: TestFilesSendWorkerConflict.java   Source Code and License 5 votes vote down vote up
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
		throws Exception {
	super.messageReceived(ctx, e);
	System.out.println("-------- Channel messageRecieved "
			+ System.currentTimeMillis());

	MessageEventBag bag = new MessageEventBag();
	bag.setBytes(e);
	bagList.add(bag);

	Protocol p = new ProtocolImpl(pf);
	Header header = p.read(conf, new DataInputStream(
			new ByteArrayInputStream(bag.getBytes())));

	ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();

	if (counter++ == 10) {
		counter = 0;
		buffer.writeInt(409);
		buffer.writeLong(filePointer);
	} else {

		buffer.writeInt(200);
	}

	filePointer = header.getFilePointer();

	ChannelFuture future = e.getChannel().write(buffer);

	future.addListener(ChannelFutureListener.CLOSE);

}
 
Example 57
Project: Camel   File: SingleTCPNettyServerBootstrapFactory.java   Source Code and License 5 votes vote down vote up
@Override
protected void doResume() throws Exception {
    if (channel != null) {
        LOG.debug("ServerBootstrap binding to {}:{}", configuration.getHost(), configuration.getPort());
        ChannelFuture future = channel.bind(new InetSocketAddress(configuration.getHost(), configuration.getPort()));
        future.awaitUninterruptibly();
        if (!future.isSuccess()) {
            // if we cannot bind, the re-create channel
            allChannels.remove(channel);
            channel = serverBootstrap.bind(new InetSocketAddress(configuration.getHost(), configuration.getPort()));
            allChannels.add(channel);
        }
    }
}
 
Example 58
Project: QoS-floodlight   File: RemoteSyncManager.java   Source Code and License 5 votes vote down vote up
protected boolean connect(String hostname, int port) {
    ready = false;
    if (channel == null || !channel.isConnected()) {
        SocketAddress sa =
                new InetSocketAddress(hostname, port);
        ChannelFuture future = clientBootstrap.connect(sa);
        future.awaitUninterruptibly();
        if (!future.isSuccess()) {
            logger.error("Could not connect to " + hostname + 
                         ":" + port, future.getCause());
            return false;
        }
        channel = future.getChannel();
    }
    while (!ready && channel != null && channel.isConnected()) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) { }
    }
    if (!ready || channel == null || !channel.isConnected()) {
        logger.warn("Timed out connecting to {}:{}", hostname, port);
        return false;
    }
    logger.debug("Connected to {}:{}", hostname, port);
    return true;
}
 
Example 59
Project: nfs-rpc   File: NettyClient.java   Source Code and License 5 votes vote down vote up
public void sendRequest(final RequestWrapper wrapper, final int timeout) throws Exception {
  final long beginTime = System.currentTimeMillis();
  final Client self = this;
  ChannelFuture writeFuture = cf.getChannel().write(wrapper);
  // use listener to avoid wait for write & thread context switch
  writeFuture.addListener(new ChannelFutureListener() {
    public void operationComplete(ChannelFuture future) throws Exception {
      if (future.isSuccess()) {
        return;
      }
      String errorMsg = "";
      // write timeout
      if (System.currentTimeMillis() - beginTime >= timeout) {
        errorMsg = "write to send buffer consume too long time(" + (System.currentTimeMillis() - beginTime) + "),request id is:"
            + wrapper.getId();
      }
      if (future.isCancelled()) {
        errorMsg = "Send request to " + cf.getChannel().toString() + " cancelled by user,request id is:" + wrapper.getId();
      }
      if (!future.isSuccess()) {
        if (cf.getChannel().isConnected()) {
          // maybe some exception,so close the channel
          cf.getChannel().close();
        } else {
          NettyClientFactory.getInstance().removeClient(key, self);
        }
        errorMsg = "Send request to " + cf.getChannel().toString() + " error" + future.getCause();
      }
      LOGGER.error(errorMsg);
      ResponseWrapper response = new ResponseWrapper(wrapper.getId(), wrapper.getCodecType(), wrapper.getProtocolType());
      response.setException(new Exception(errorMsg));
      self.putResponse(response);
    }
  });
}
 
Example 60
Project: nfs-rpc   File: NettyClientFactory.java   Source Code and License 5 votes vote down vote up
protected Client createClient(String targetIP, int targetPort, int connectTimeout, String key) throws Exception {
  ClientBootstrap bootstrap = new ClientBootstrap(nioClient);
  bootstrap.setOption("tcpNoDelay", Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.nodelay", "true")));
  bootstrap.setOption("reuseAddress", Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.reuseaddress", "true")));
  if (connectTimeout < 1000) {
    bootstrap.setOption("connectTimeoutMillis", 1000);
  } else {
    bootstrap.setOption("connectTimeoutMillis", connectTimeout);
  }
  NettyClientHandler handler = new NettyClientHandler(this, key);
  bootstrap.setPipelineFactory(new NettyClientPipelineFactory(handler));
  ChannelFuture future = bootstrap.connect(new InetSocketAddress(targetIP, targetPort));
  future.awaitUninterruptibly(connectTimeout);
  if (!future.isDone()) {
    LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " timeout!");
    throw new Exception("Create connection to " + targetIP + ":" + targetPort + " timeout!");
  }
  if (future.isCancelled()) {
    LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!");
    throw new Exception("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!");
  }
  if (!future.isSuccess()) {
    LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " error", future.getCause());
    throw new Exception("Create connection to " + targetIP + ":" + targetPort + " error", future.getCause());
  }
  NettyClient client = new NettyClient(future, key, connectTimeout);
  handler.setClient(client);
  return client;
}