Search in sources :

Example 51 with Channel

use of org.jboss.netty.channel.Channel in project hadoop by apache.

the class OpenFileCtx method doSingleWrite.

private void doSingleWrite(final WriteCtx writeCtx) {
    Channel channel = writeCtx.getChannel();
    int xid = writeCtx.getXid();
    long offset = writeCtx.getOffset();
    int count = writeCtx.getCount();
    WriteStableHow stableHow = writeCtx.getStableHow();
    FileHandle handle = writeCtx.getHandle();
    if (LOG.isDebugEnabled()) {
        LOG.debug("do write, fileId: " + handle.getFileId() + " offset: " + offset + " length: " + count + " stableHow: " + stableHow.name());
    }
    try {
        // The write is not protected by lock. asyncState is used to make sure
        // there is one thread doing write back at any time    
        writeCtx.writeData(fos);
        RpcProgramNfs3.metrics.incrBytesWritten(writeCtx.getCount());
        long flushedOffset = getFlushedOffset();
        if (flushedOffset != (offset + count)) {
            throw new IOException("output stream is out of sync, pos=" + flushedOffset + " and nextOffset should be" + (offset + count));
        }
        // Reduce memory occupation size if request was allowed dumped
        if (writeCtx.getDataState() == WriteCtx.DataState.ALLOW_DUMP) {
            synchronized (writeCtx) {
                if (writeCtx.getDataState() == WriteCtx.DataState.ALLOW_DUMP) {
                    writeCtx.setDataState(WriteCtx.DataState.NO_DUMP);
                    updateNonSequentialWriteInMemory(-count);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("After writing " + handle.getFileId() + " at offset " + offset + ", updated the memory count, new value: " + nonSequentialWriteInMemory.get());
                    }
                }
            }
        }
        if (!writeCtx.getReplied()) {
            if (stableHow != WriteStableHow.UNSTABLE) {
                LOG.info("Do sync for stable write: " + writeCtx);
                try {
                    if (stableHow == WriteStableHow.DATA_SYNC) {
                        fos.hsync();
                    } else {
                        Preconditions.checkState(stableHow == WriteStableHow.FILE_SYNC, "Unknown WriteStableHow: " + stableHow);
                        // Sync file data and length
                        fos.hsync(EnumSet.of(SyncFlag.UPDATE_LENGTH));
                    }
                } catch (IOException e) {
                    LOG.error("hsync failed with writeCtx: " + writeCtx, e);
                    throw e;
                }
            }
            WccAttr preOpAttr = latestAttr.getWccAttr();
            WccData fileWcc = new WccData(preOpAttr, latestAttr);
            if (writeCtx.getOriginalCount() != WriteCtx.INVALID_ORIGINAL_COUNT) {
                LOG.warn("Return original count: " + writeCtx.getOriginalCount() + " instead of real data count: " + count);
                count = writeCtx.getOriginalCount();
            }
            WRITE3Response response = new WRITE3Response(Nfs3Status.NFS3_OK, fileWcc, count, stableHow, Nfs3Constant.WRITE_COMMIT_VERF);
            RpcProgramNfs3.metrics.addWrite(Nfs3Utils.getElapsedTime(writeCtx.startTime));
            Nfs3Utils.writeChannel(channel, response.serialize(new XDR(), xid, new VerifierNone()), xid);
        }
        // Handle the waiting commits without holding any lock
        processCommits(writeCtx.getOffset() + writeCtx.getCount());
    } catch (IOException e) {
        LOG.error("Error writing to fileId " + handle.getFileId() + " at offset " + offset + " and length " + count, e);
        if (!writeCtx.getReplied()) {
            WRITE3Response response = new WRITE3Response(Nfs3Status.NFS3ERR_IO);
            Nfs3Utils.writeChannel(channel, response.serialize(new XDR(), xid, new VerifierNone()), xid);
        // Keep stream open. Either client retries or SteamMonitor closes it.
        }
        LOG.info("Clean up open file context for fileId: " + latestAttr.getFileId());
        cleanup();
    }
}
Also used : WccData(org.apache.hadoop.nfs.nfs3.response.WccData) WriteStableHow(org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) Channel(org.jboss.netty.channel.Channel) XDR(org.apache.hadoop.oncrpc.XDR) VerifierNone(org.apache.hadoop.oncrpc.security.VerifierNone) WccAttr(org.apache.hadoop.nfs.nfs3.response.WccAttr) IOException(java.io.IOException) WRITE3Response(org.apache.hadoop.nfs.nfs3.response.WRITE3Response)

Example 52 with Channel

use of org.jboss.netty.channel.Channel in project storm by nathanmarz.

the class StormClientHandler method channelConnected.

@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent event) {
    //register the newly established channel
    Channel channel = event.getChannel();
    client.setChannel(channel);
    LOG.debug("connection established to a remote host");
    //send next request
    try {
        sendRequests(channel, client.takeMessages());
    } catch (InterruptedException e) {
        channel.close();
    }
}
Also used : Channel(org.jboss.netty.channel.Channel)

Example 53 with Channel

use of org.jboss.netty.channel.Channel in project pinpoint by naver.

the class DefaultPinpointClientFactory method reconnect.

public ChannelFuture reconnect(final SocketAddress remoteAddress) {
    if (remoteAddress == null) {
        throw new NullPointerException("remoteAddress");
    }
    ChannelPipeline pipeline;
    final ClientBootstrap bootstrap = this.bootstrap;
    try {
        pipeline = bootstrap.getPipelineFactory().getPipeline();
    } catch (Exception e) {
        throw new ChannelPipelineException("Failed to initialize a pipeline.", e);
    }
    PinpointClientHandler pinpointClientHandler = (DefaultPinpointClientHandler) pipeline.getLast();
    pinpointClientHandler.initReconnect();
    // Set the options.
    Channel ch = bootstrap.getFactory().newChannel(pipeline);
    boolean success = false;
    try {
        ch.getConfig().setOptions(bootstrap.getOptions());
        success = true;
    } finally {
        if (!success) {
            ch.close();
        }
    }
    // Connect.
    return ch.connect(remoteAddress);
}
Also used : ChannelPipelineException(org.jboss.netty.channel.ChannelPipelineException) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) Channel(org.jboss.netty.channel.Channel) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) PinpointSocketException(com.navercorp.pinpoint.rpc.PinpointSocketException) ChannelPipelineException(org.jboss.netty.channel.ChannelPipelineException)

Example 54 with Channel

use of org.jboss.netty.channel.Channel in project pinpoint by naver.

the class PinpointClientStateTest method getSocketHandler.

PinpointClientHandler getSocketHandler(ChannelFuture channelConnectFuture, SocketAddress address) {
    if (address == null) {
        throw new NullPointerException("address");
    }
    Channel channel = channelConnectFuture.getChannel();
    PinpointClientHandler pinpointClientHandler = (PinpointClientHandler) channel.getPipeline().getLast();
    pinpointClientHandler.setConnectSocketAddress(address);
    return pinpointClientHandler;
}
Also used : Channel(org.jboss.netty.channel.Channel)

Example 55 with Channel

use of org.jboss.netty.channel.Channel in project neo4j by neo4j.

the class NetworkReceiver method listen.

private int listen(int minPort, int maxPort) throws URISyntaxException, ChannelException {
    ChannelException ex = null;
    for (int checkPort = minPort; checkPort <= maxPort; checkPort++) {
        try {
            String address = config.clusterServer().getHost();
            InetSocketAddress localAddress;
            if (address == null || address.equals(INADDR_ANY)) {
                localAddress = new InetSocketAddress(checkPort);
            } else {
                localAddress = new InetSocketAddress(address, checkPort);
                bindingDetected = true;
            }
            Channel listenChannel = serverBootstrap.bind(localAddress);
            listeningAt(getURI(localAddress));
            channels.add(listenChannel);
            return checkPort;
        } catch (ChannelException e) {
            ex = e;
        }
    }
    nioChannelFactory.releaseExternalResources();
    throw ex;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) ChannelException(org.jboss.netty.channel.ChannelException)

Aggregations

Channel (org.jboss.netty.channel.Channel)187 InetSocketAddress (java.net.InetSocketAddress)57 Test (org.junit.Test)52 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)40 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)37 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)34 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)34 SocketAddress (java.net.SocketAddress)33 ChannelFuture (org.jboss.netty.channel.ChannelFuture)33 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)30 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)27 Test (org.testng.annotations.Test)23 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)22 IOException (java.io.IOException)21 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)19 Logger (org.apache.log4j.Logger)19 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)17 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)16 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)16 ArrayList (java.util.ArrayList)14