Java Code Examples for org.jboss.netty.channel.ChannelFuture.addListener()

The following are Jave code examples for showing how to use addListener() of the org.jboss.netty.channel.ChannelFuture class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: https-github.com-apache-zookeeper   File: NettyServerCnxnFactory.java   Source Code and License Vote up 6 votes
@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 2
Project: bigstreams   File: ServerUtil.java   Source Code and License Vote up 6 votes
@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 3
Project: bigstreams   File: ServerUtil.java   Source Code and License Vote up 6 votes
@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 4
Project: SecureKeeper   File: NettyServerCnxnFactory.java   Source Code and License Vote up 6 votes
@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 5
Project: Camel   File: NettyHelper.java   Source Code and License Vote up 6 votes
/**
 * 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 6
Project: fastcatsearch3   File: TransportChannel.java   Source Code and License Vote up 6 votes
public void sendResponse(Throwable error) throws IOException {
	byte type = 0;
	type = TransportOption.setTypeMessage(type);
	byte status = 0;
    status = TransportOption.setErrorResponse(status);
    
    CachedStreamOutput.Entry cachedEntry = CachedStreamOutput.popEntry();
    BytesStreamOutput stream = cachedEntry.bytes();
    stream.skip(MessageProtocol.HEADER_SIZE);
    StreamableThrowable streamableThrowable = new StreamableThrowable(error);
    streamableThrowable.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 7
Project: SecureKeeper   File: NettyServerCnxnFactory.java   Source Code and License Vote up 6 votes
@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 8
Project: Elasticsearch   File: NettyTransport.java   Source Code and License Vote up 5 votes
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 9
Project: Elasticsearch   File: NettyTransportChannel.java   Source Code and License Vote up 5 votes
@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 10
Project: aliyun-oss-hadoop-fs   File: ShuffleHandler.java   Source Code and License Vote up 5 votes
/**
 * 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 11
Project: Camel   File: ClientModeTCPNettyServerBootstrapFactory.java   Source Code and License Vote up 5 votes
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();

    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating connector to address: {}", configuration.getAddress());
    }
    return answer;
}
 
Example 12
Project: bigstreams   File: LogWriterHandler.java   Source Code and License Vote up 5 votes
/**
 * Write response back to agent
 * 
 * @param e
 * @param buffer
 * @param agentSession
 */
private final void writeToAgent(MessageEvent e, ChannelBuffer buffer,
		AgentSession agentSession) {

	// write responses back to agent
	ChannelFuture future = e.getChannel().write(buffer);
	agentSession.setSentResponseRequest();

	agentSession.setReleasedCoordinationLock();
	// check slow writes:
	if ((agentSession.getFileWriteEndTime() - agentSession
			.getFileWriteStartTime()) > 500) {
		LOG.error("File writing is slowing down please check the log directory");
	}

	if (future != null) {
		future.addListener(new ChannelFutureListener() {

			@Override
			public void operationComplete(ChannelFuture f) throws Exception {
				try {
					ChannelFutureListener.CLOSE.operationComplete(f);
				} catch (Throwable t) {
					LOG.error("ERROR While closing channel :"
							+ f.getChannel() + " " + f.getCause());
				}
			}

		});

	}
}
 
Example 13
Project: nfs-rpc   File: NettyClient.java   Source Code and License Vote up 5 votes
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 14
Project: Camel   File: NettyProducer.java   Source Code and License Vote up 5 votes
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 15
Project: nfs-rpc   File: NettyServerHandler.java   Source Code and License Vote up 5 votes
@SuppressWarnings("rawtypes")
public void run() {
  // pipeline
  if (message instanceof List) {
    List messages = (List) message;
    for (Object messageObject : messages) {
      threadPool.execute(new HandlerRunnable(ctx, messageObject, threadPool));
    }
  } else {
    RequestWrapper request = (RequestWrapper) message;
    long beginTime = System.currentTimeMillis();
    ResponseWrapper responseWrapper = ProtocolFactory.getServerHandler(request.getProtocolType()).handleRequest(request);
    final int id = request.getId();
    // already timeout,so not return
    if ((System.currentTimeMillis() - beginTime) >= request.getTimeout()) {
      LOGGER.warn("timeout,so give up send response to client,requestId is:" + id + ",client is:" + ctx.getChannel().getRemoteAddress()
          + ",consumetime is:" + (System.currentTimeMillis() - beginTime) + ",timeout is:" + request.getTimeout());
      return;
    }
    ChannelFuture wf = ctx.getChannel().write(responseWrapper);
    wf.addListener(new ChannelFutureListener() {
      public void operationComplete(ChannelFuture future) throws Exception {
        if (!future.isSuccess()) {
          LOGGER.error("server write response error,request id is: " + id);
        }
      }
    });
  }
}
 
Example 16
Project: incubator-omid   File: TSOClient.java   Source Code and License Vote up 5 votes
private void sendRequest(final StateMachine.Fsm fsm, RequestEvent request) {
    TSOProto.Request req = request.getRequest();

    if (req.hasTimestampRequest()) {
        timestampRequests.add(new RequestAndTimeout(request, newTimeout(new TimestampRequestTimeoutEvent())));
    } else if (req.hasCommitRequest()) {
        TSOProto.CommitRequest commitReq = req.getCommitRequest();
        commitRequests.put(commitReq.getStartTimestamp(), new RequestAndTimeout(
                request, newTimeout(new CommitRequestTimeoutEvent(commitReq.getStartTimestamp()))));
    } else if (req.hasFenceRequest()) {
        TSOProto.FenceRequest fenceReq = req.getFenceRequest();
        fenceRequests.put(fenceReq.getTableId(), new RequestAndTimeout(
                request, newTimeout(new FenceRequestTimeoutEvent(fenceReq.getTableId()))));
    } else {
        request.error(new IllegalArgumentException("Unknown request type"));
        return;
    }
    ChannelFuture f = channel.write(req);

    f.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) {
            if (!future.isSuccess()) {
                fsm.sendEvent(new ErrorEvent(future.getCause()));
            }
        }
    });
}
 
Example 17
Project: OpenMobster   File: HttpServerHandler.java   Source Code and License Vote up 5 votes
private void writeResponse(MessageEvent e) 
{
    // Build the response object.
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    
    response.setContent(ChannelBuffers.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));
    response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");

    // Write the response.
    ChannelFuture future = e.getChannel().write(response);

    // Close the non-keep-alive connection after the write operation is done.
    future.addListener(ChannelFutureListener.CLOSE);
}
 
Example 18
Project: HeliosStreams   File: WebSocketServiceHandler.java   Source Code and License Vote up 5 votes
/**
 * Sends an HTTP response
 * @param ctx The channel handler context
 * @param req The HTTP request being responded to
 * @param res The HTTP response to send
 */
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) {
    // Generate an error page if response status code is not OK (200).
    if (res.getStatus().getCode() != 200) {
        res.setContent(ChannelBuffers.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8));
        setContentLength(res, res.getContent().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.getChannel().write(res);
    if (!isKeepAlive(req) || res.getStatus().getCode() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}
 
Example 19
Project: hadoop   File: ShuffleHandler.java   Source Code and License Vote up 4 votes
protected ChannelFuture sendMapOutput(ChannelHandlerContext ctx, Channel ch,
    String user, String mapId, int reduce, MapOutputInfo mapOutputInfo)
    throws IOException {
  final IndexRecord info = mapOutputInfo.indexRecord;
  final ShuffleHeader header =
    new ShuffleHeader(mapId, info.partLength, info.rawLength, reduce);
  final DataOutputBuffer dob = new DataOutputBuffer();
  header.write(dob);
  ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
  final File spillfile =
      new File(mapOutputInfo.mapOutputFileName.toString());
  RandomAccessFile spill;
  try {
    spill = SecureIOUtils.openForRandomRead(spillfile, "r", user, null);
  } catch (FileNotFoundException e) {
    LOG.info(spillfile + " not found");
    return null;
  }
  ChannelFuture writeFuture;
  if (ch.getPipeline().get(SslHandler.class) == null) {
    final FadvisedFileRegion partition = new FadvisedFileRegion(spill,
        info.startOffset, info.partLength, manageOsCache, readaheadLength,
        readaheadPool, spillfile.getAbsolutePath(), 
        shuffleBufferSize, shuffleTransferToAllowed);
    writeFuture = ch.write(partition);
    writeFuture.addListener(new ChannelFutureListener() {
        // TODO error handling; distinguish IO/connection failures,
        //      attribute to appropriate spill output
      @Override
      public void operationComplete(ChannelFuture future) {
        if (future.isSuccess()) {
          partition.transferSuccessful();
        }
        partition.releaseExternalResources();
      }
    });
  } else {
    // HTTPS cannot be done with zero copy.
    final FadvisedChunkedFile chunk = new FadvisedChunkedFile(spill,
        info.startOffset, info.partLength, sslFileBufferSize,
        manageOsCache, readaheadLength, readaheadPool,
        spillfile.getAbsolutePath());
    writeFuture = ch.write(chunk);
  }
  metrics.shuffleConnections.incr();
  metrics.shuffleOutputBytes.incr(info.partLength); // optimistic
  return writeFuture;
}
 
Example 20
Project: aliyun-oss-hadoop-fs   File: ShuffleHandler.java   Source Code and License Vote up 4 votes
protected ChannelFuture sendMapOutput(ChannelHandlerContext ctx, Channel ch,
    String user, String mapId, int reduce, MapOutputInfo mapOutputInfo)
    throws IOException {
  final IndexRecord info = mapOutputInfo.indexRecord;
  final ShuffleHeader header =
    new ShuffleHeader(mapId, info.partLength, info.rawLength, reduce);
  final DataOutputBuffer dob = new DataOutputBuffer();
  header.write(dob);
  ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
  final File spillfile =
      new File(mapOutputInfo.mapOutputFileName.toString());
  RandomAccessFile spill;
  try {
    spill = SecureIOUtils.openForRandomRead(spillfile, "r", user, null);
  } catch (FileNotFoundException e) {
    LOG.info(spillfile + " not found");
    return null;
  }
  ChannelFuture writeFuture;
  if (ch.getPipeline().get(SslHandler.class) == null) {
    final FadvisedFileRegion partition = new FadvisedFileRegion(spill,
        info.startOffset, info.partLength, manageOsCache, readaheadLength,
        readaheadPool, spillfile.getAbsolutePath(), 
        shuffleBufferSize, shuffleTransferToAllowed);
    writeFuture = ch.write(partition);
    writeFuture.addListener(new ChannelFutureListener() {
        // TODO error handling; distinguish IO/connection failures,
        //      attribute to appropriate spill output
      @Override
      public void operationComplete(ChannelFuture future) {
        if (future.isSuccess()) {
          partition.transferSuccessful();
        }
        partition.releaseExternalResources();
      }
    });
  } else {
    // HTTPS cannot be done with zero copy.
    final FadvisedChunkedFile chunk = new FadvisedChunkedFile(spill,
        info.startOffset, info.partLength, sslFileBufferSize,
        manageOsCache, readaheadLength, readaheadPool,
        spillfile.getAbsolutePath());
    writeFuture = ch.write(chunk);
  }
  metrics.shuffleConnections.incr();
  metrics.shuffleOutputBytes.incr(info.partLength); // optimistic
  return writeFuture;
}
 
Example 21
Project: bigstreams   File: IpFilterHandler.java   Source Code and License Vote up 4 votes
@Override
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e)
		throws Exception {
	if (e instanceof ChannelStateEvent) {
		ChannelStateEvent evt = (ChannelStateEvent) e;
		switch (evt.getState()) {
		case OPEN:
		case BOUND:
			// Special case: OPEND and BOUND events are before CONNECTED,
			// but CLOSED and UNBOUND events are after DISCONNECTED: should
			// those events be blocked too?
			if (isBlocked(ctx) && !continues(ctx, evt)) {
				// don't pass to next level since channel was blocked early
				return;
			} else {
				ctx.sendUpstream(e);
				return;
			}
		case CONNECTED:
			if (evt.getValue() != null) {
				// CONNECTED
				InetSocketAddress inetSocketAddress = (InetSocketAddress) e
						.getChannel().getRemoteAddress();
				if (!accept(ctx, e, inetSocketAddress)) {
					ctx.setAttachment(Boolean.TRUE);
					
					final ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
					
					buffer.writeBytes("/".getBytes());
					buffer.writeBytes("ok".getBytes());
					
					ChannelFuture future = e.getChannel().write(buffer);
					future.addListener(ChannelFutureListener.CLOSE);
					LOG.info("Ignoring " + inetSocketAddress.getAddress().getHostAddress());
					
					if (isBlocked(ctx) && !continues(ctx, evt)) {
						// don't pass to next level since channel was
						// blocked early
						return;
					}
				}
				// This channel is not blocked
				ctx.setAttachment(null);
			} else {
				// DISCONNECTED
				if (isBlocked(ctx) && !continues(ctx, evt)) {
					// don't pass to next level since channel was blocked
					// early
					return;
				}
			}
			break;
		}
	}
	if (isBlocked(ctx) && !continues(ctx, e)) {
		// don't pass to next level since channel was blocked early
		return;
	}
	// Whatever it is, if not blocked, goes to the next level
	ctx.sendUpstream(e);
}
 
Example 22
Project: bigstreams   File: LogWriterHandler.java   Source Code and License Vote up 4 votes
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
		throws Exception {

	AgentSession agentSession = (AgentSession) ctx.getAttachment();

	if (agentSession == null) {
		agentSession = new AgentSession("unkown");
	}

	Throwable exception = e.getCause();
	NetworkCodes.CODE code = null;
	CollectorStatus.STATUS stat = null;

	if (exception instanceof java.net.ConnectException) {
		code = NetworkCodes.CODE.COORDINATION_CONNECTION_ERROR;
		stat = CollectorStatus.STATUS.COORDINATION_ERROR;
	} else if (exception instanceof CoordinationException) {
		CoordinationException coordExcp = (CoordinationException) exception;
		if (coordExcp.isConnectException()) {
			code = NetworkCodes.CODE.COORDINATION_CONNECTION_ERROR;
			stat = CollectorStatus.STATUS.COORDINATION_ERROR;
		} else {
			code = NetworkCodes.CODE.COORDINATION_LOCK_ERROR;
			stat = CollectorStatus.STATUS.COORDINATION_LOCK_ERROR;
		}
	} else {
		code = NetworkCodes.CODE.UNKOWN;
		stat = CollectorStatus.STATUS.UNKOWN_ERROR;
	}

	// WRITE STATUS
	try {

		/**
		 * Very important to respond here. The agent will always be
		 * listening for some kind of feedback.
		 */
		ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
		buffer.writeInt(code.num());
		buffer.writeBytes(code.msg().getBytes("UTF-8"));

		if (e.getChannel().isOpen()) {

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

			future.addListener(ChannelFutureListener.CLOSE);

		} else {
			LOG.error("Channel was closed by agent "
					+ agentSession.getAgentName() + ": exception: "
					+ code.num() + " " + code.msg() + " cause: "
					+ exception + " cannot be written to agent");
		}
		collectorStatus.setStatus(stat, e.getCause().toString());

		collectorStatus.incCounter("Errors_Caught", 1);

		LOG.error(agentSession.toString());
		LOG.error(exception.toString(), exception);
		
		// LOG.error(exception, exception);
	} catch (Throwable t) {
		LOG.error("Throwed exception in catchException " + t);
	}

}
 
Example 23
Project: bigstreams   File: CoordinationUnLockHandler.java   Source Code and License Vote up 4 votes
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
		throws Exception {
	ChannelBuffer buff = (ChannelBuffer) e.getMessage();

	ChannelBufferInputStream channelInput = new ChannelBufferInputStream(
			buff);

	final SyncPointer syncPointer = objMapper.readValue(channelInput,
			SyncPointer.class);

	if (syncPointer == null) {
		throw new RuntimeException(
				"Please send a SyncPointer object: SyncPointer is null");
	}

	if (syncPointer.getLockId() == null) {
		throw new RuntimeException(
				"Please send a SyncPointer object with a lockId: SyncPointer.getLockId == null");
	}
	
	final boolean ok = saveAndReleaseLock(syncPointer, (InetSocketAddress) e.getRemoteAddress());

	ChannelBuffer buffer = null;

	if (ok) {
		// if a syncpointer is returned the resource was not locked and is
		// now locked for the current caller.

		byte[] okBytes = "OK".getBytes();
		buffer = ChannelBuffers.buffer(okBytes.length + 8);
		buffer.writeInt(okBytes.length + 4);
		buffer.writeInt(200);
		buffer.writeBytes(okBytes);

	} else {
		// if the sync pointer is null the resource is already locked
		buffer = ChannelBuffers.buffer(CONFLICT_MESSAGE.length + 8);

		buffer.writeInt(CONFLICT_MESSAGE.length + 4);
		buffer.writeInt(409); // conflict code
		buffer.writeBytes(CONFLICT_MESSAGE);

	}

	ChannelFuture future = e.getChannel().write(buffer);
	future.addListener(ChannelFutureListener.CLOSE);
}
 
Example 24
Project: big-c   File: ShuffleHandler.java   Source Code and License Vote up 4 votes
protected ChannelFuture sendMapOutput(ChannelHandlerContext ctx, Channel ch,
    String user, String mapId, int reduce, MapOutputInfo mapOutputInfo)
    throws IOException {
  final IndexRecord info = mapOutputInfo.indexRecord;
  final ShuffleHeader header =
    new ShuffleHeader(mapId, info.partLength, info.rawLength, reduce);
  final DataOutputBuffer dob = new DataOutputBuffer();
  header.write(dob);
  ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
  final File spillfile =
      new File(mapOutputInfo.mapOutputFileName.toString());
  RandomAccessFile spill;
  try {
    spill = SecureIOUtils.openForRandomRead(spillfile, "r", user, null);
  } catch (FileNotFoundException e) {
    LOG.info(spillfile + " not found");
    return null;
  }
  ChannelFuture writeFuture;
  if (ch.getPipeline().get(SslHandler.class) == null) {
    final FadvisedFileRegion partition = new FadvisedFileRegion(spill,
        info.startOffset, info.partLength, manageOsCache, readaheadLength,
        readaheadPool, spillfile.getAbsolutePath(), 
        shuffleBufferSize, shuffleTransferToAllowed);
    writeFuture = ch.write(partition);
    writeFuture.addListener(new ChannelFutureListener() {
        // TODO error handling; distinguish IO/connection failures,
        //      attribute to appropriate spill output
      @Override
      public void operationComplete(ChannelFuture future) {
        if (future.isSuccess()) {
          partition.transferSuccessful();
        }
        partition.releaseExternalResources();
      }
    });
  } else {
    // HTTPS cannot be done with zero copy.
    final FadvisedChunkedFile chunk = new FadvisedChunkedFile(spill,
        info.startOffset, info.partLength, sslFileBufferSize,
        manageOsCache, readaheadLength, readaheadPool,
        spillfile.getAbsolutePath());
    writeFuture = ch.write(chunk);
  }
  metrics.shuffleConnections.incr();
  metrics.shuffleOutputBytes.incr(info.partLength); // optimistic
  return writeFuture;
}