Example usage for org.apache.hadoop.net NetUtils connect

List of usage examples for org.apache.hadoop.net NetUtils connect

Introduction

In this page you can find the example usage for org.apache.hadoop.net NetUtils connect.

Prototype

public static void connect(Socket socket, SocketAddress endpoint, SocketAddress localAddr, int timeout)
        throws IOException 

Source Link

Document

Like NetUtils#connect(Socket,SocketAddress,int) but also takes a local address and port to bind the socket to.

Usage

From source file:com.mellanox.r4h.DFSClient.java

License:Apache License

@Override
// RemotePeerFactory
public Peer newConnectedPeer(InetSocketAddress addr, Token<BlockTokenIdentifier> blockToken,
        DatanodeID datanodeId) throws IOException {
    Peer peer = null;/*from  w w w .ja  v  a 2s  . c  om*/
    boolean success = false;
    Socket sock = null;
    try {
        sock = socketFactory.createSocket();
        NetUtils.connect(sock, addr, getRandomLocalInterfaceAddr(), dfsClientConf.getSocketTimeout());
        peer = TcpPeerServer.peerFromSocketAndKey(saslClient, sock, this, blockToken, datanodeId);
        peer.setReadTimeout(dfsClientConf.socketTimeout());
        success = true;
        return peer;
    } finally {
        if (!success) {
            IOUtils.cleanup(LOG, peer);
            IOUtils.closeSocket(sock);
        }
    }
}

From source file:com.mellanox.r4h.DFSOutputStream.java

License:Apache License

/**
 * Create a socket for a write pipeline/*from w  w  w  . j a va 2 s . c  om*/
 * 
 * @param first
 *            the first datanode
 * @param length
 *            the pipeline length
 * @param client
 *            client
 * @return the socket connected to the first datanode
 */
static Socket createSocketForPipeline(final DatanodeInfo first, final int length, final DFSClient client)
        throws IOException {
    final String dnAddr = first.getXferAddr(client.getConf().getConnectToDnViaHostname());
    if (DFSClient.LOG.isDebugEnabled()) {
        DFSClient.LOG.debug("Connecting to datanode " + dnAddr);
    }
    final InetSocketAddress isa = NetUtils.createSocketAddr(dnAddr);
    final Socket sock = client.socketFactory.createSocket();
    final int timeout = client.getDatanodeReadTimeout(length);
    NetUtils.connect(sock, isa, client.getRandomLocalInterfaceAddr(), client.getConf().getSocketTimeout());
    sock.setSoTimeout(timeout);
    sock.setSendBufferSize(HdfsConstants.DEFAULT_DATA_SOCKET_SIZE);
    if (DFSClient.LOG.isDebugEnabled()) {
        DFSClient.LOG.debug("Send buf size " + sock.getSendBufferSize());
    }
    return sock;
}