Java Code Examples for org.apache.hadoop.io.IOUtils#closeSocket()

The following examples show how to use org.apache.hadoop.io.IOUtils#closeSocket() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DFSClient.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Connect to the given datanode's datantrasfer port, and return
 * the resulting IOStreamPair. This includes encryption wrapping, etc.
 */
private IOStreamPair connectToDN(DatanodeInfo dn, int timeout,
    LocatedBlock lb) throws IOException {
  boolean success = false;
  Socket sock = null;
  try {
    sock = socketFactory.createSocket();
    String dnAddr = dn.getXferAddr(getConf().connectToDnViaHostname);
    if (LOG.isDebugEnabled()) {
      LOG.debug("Connecting to datanode " + dnAddr);
    }
    NetUtils.connect(sock, NetUtils.createSocketAddr(dnAddr), timeout);
    sock.setSoTimeout(timeout);

    OutputStream unbufOut = NetUtils.getOutputStream(sock);
    InputStream unbufIn = NetUtils.getInputStream(sock);
    IOStreamPair ret = saslClient.newSocketSend(sock, unbufOut, unbufIn, this,
      lb.getBlockToken(), dn);
    success = true;
    return ret;
  } finally {
    if (!success) {
      IOUtils.closeSocket(sock);
    }
  }
}
 
Example 2
Source File: DFSClient.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@Override // RemotePeerFactory
public Peer newConnectedPeer(InetSocketAddress addr,
    Token<BlockTokenIdentifier> blockToken, DatanodeID datanodeId)
    throws IOException {
  Peer peer = null;
  boolean success = false;
  Socket sock = null;
  try {
    sock = socketFactory.createSocket();
    NetUtils.connect(sock, addr,
      getRandomLocalInterfaceAddr(),
      dfsClientConf.socketTimeout);
    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);
    }
  }
}
 
Example 3
Source File: DFSClient.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Connect to the given datanode's datantrasfer port, and return
 * the resulting IOStreamPair. This includes encryption wrapping, etc.
 */
private IOStreamPair connectToDN(DatanodeInfo dn, int timeout,
    LocatedBlock lb) throws IOException {
  boolean success = false;
  Socket sock = null;
  try {
    sock = socketFactory.createSocket();
    String dnAddr = dn.getXferAddr(getConf().connectToDnViaHostname);
    if (LOG.isDebugEnabled()) {
      LOG.debug("Connecting to datanode " + dnAddr);
    }
    NetUtils.connect(sock, NetUtils.createSocketAddr(dnAddr), timeout);
    sock.setSoTimeout(timeout);

    OutputStream unbufOut = NetUtils.getOutputStream(sock);
    InputStream unbufIn = NetUtils.getInputStream(sock);
    IOStreamPair ret = saslClient.newSocketSend(sock, unbufOut, unbufIn, this,
      lb.getBlockToken(), dn);
    success = true;
    return ret;
  } finally {
    if (!success) {
      IOUtils.closeSocket(sock);
    }
  }
}
 
Example 4
Source File: DFSClient.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Override // RemotePeerFactory
public Peer newConnectedPeer(InetSocketAddress addr,
    Token<BlockTokenIdentifier> blockToken, DatanodeID datanodeId)
    throws IOException {
  Peer peer = null;
  boolean success = false;
  Socket sock = null;
  try {
    sock = socketFactory.createSocket();
    NetUtils.connect(sock, addr,
      getRandomLocalInterfaceAddr(),
      dfsClientConf.socketTimeout);
    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);
    }
  }
}
 
Example 5
Source File: TestIPC.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void doIpcVersionTest(
    byte[] requestData,
    byte[] expectedResponse) throws IOException {
  Server server = new TestServer(1, true);
  InetSocketAddress addr = NetUtils.getConnectAddress(server);
  server.start();
  Socket socket = new Socket();

  try {
    NetUtils.connect(socket, addr, 5000);
    
    OutputStream out = socket.getOutputStream();
    InputStream in = socket.getInputStream();
    out.write(requestData, 0, requestData.length);
    out.flush();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copyBytes(in, baos, 256);
    
    byte[] responseData = baos.toByteArray();
    
    assertEquals(
        StringUtils.byteToHexString(expectedResponse),
        StringUtils.byteToHexString(responseData));
  } finally {
    IOUtils.closeSocket(socket);
    server.stop();
  }
}
 
Example 6
Source File: TestIPC.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void doIpcVersionTest(
    byte[] requestData,
    byte[] expectedResponse) throws IOException {
  Server server = new TestServer(1, true);
  InetSocketAddress addr = NetUtils.getConnectAddress(server);
  server.start();
  Socket socket = new Socket();

  try {
    NetUtils.connect(socket, addr, 5000);
    
    OutputStream out = socket.getOutputStream();
    InputStream in = socket.getInputStream();
    out.write(requestData, 0, requestData.length);
    out.flush();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copyBytes(in, baos, 256);
    
    byte[] responseData = baos.toByteArray();
    
    assertEquals(
        StringUtils.byteToHexString(expectedResponse),
        StringUtils.byteToHexString(responseData));
  } finally {
    IOUtils.closeSocket(socket);
    server.stop();
  }
}
 
Example 7
Source File: BlockingRpcConnection.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void closeSocket() {
  IOUtils.closeStream(out);
  IOUtils.closeStream(in);
  IOUtils.closeSocket(socket);
  out = null;
  in = null;
  socket = null;
}
 
Example 8
Source File: TestWebHdfsTimeouts.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Starts a background thread that accepts one and only one client connection
 * on the server socket, sends an HTTP 307 Temporary Redirect response, and
 * then exits.  This is useful for testing timeouts on the second step of
 * methods that issue 2 HTTP requests (request 1, redirect, request 2).
 * 
 * For handling the first request, this method sets socket timeout to use the
 * initial values defined in URLUtils.  Afterwards, it guarantees that the
 * second request will use a very short timeout.
 * 
 * Optionally, the thread may consume the connection backlog immediately after
 * receiving its one and only client connection.  This is useful for forcing a
 * connection timeout on the second request.
 * 
 * On tearDown, open client connections are closed, and the thread is joined.
 * 
 * @param consumeConnectionBacklog boolean whether or not to consume connection
 *   backlog and thus force a connection timeout on the second request
 */
private void startSingleTemporaryRedirectResponseThread(
    final boolean consumeConnectionBacklog) {
  fs.connectionFactory = URLConnectionFactory.DEFAULT_SYSTEM_CONNECTION_FACTORY;
  serverThread = new Thread() {
    @Override
    public void run() {
      Socket clientSocket = null;
      OutputStream out = null;
      InputStream in = null;
      InputStreamReader isr = null;
      BufferedReader br = null;
      try {
        // Accept one and only one client connection.
        clientSocket = serverSocket.accept();

        // Immediately setup conditions for subsequent connections.
        fs.connectionFactory = connectionFactory;
        if (consumeConnectionBacklog) {
          consumeConnectionBacklog();
        }

        // Consume client's HTTP request by reading until EOF or empty line.
        in = clientSocket.getInputStream();
        isr = new InputStreamReader(in);
        br = new BufferedReader(isr);
        for (;;) {
          String line = br.readLine();
          if (line == null || line.isEmpty()) {
            break;
          }
        }

        // Write response.
        out = clientSocket.getOutputStream();
        out.write(temporaryRedirect().getBytes("UTF-8"));
      } catch (IOException e) {
        // Fail the test on any I/O error in the server thread.
        LOG.error("unexpected IOException in server thread", e);
        fail("unexpected IOException in server thread: " + e);
      } finally {
        // Clean it all up.
        IOUtils.cleanup(LOG, br, isr, in, out);
        IOUtils.closeSocket(clientSocket);
      }
    }
  };
  serverThread.start();
}
 
Example 9
Source File: TestWebHdfsTimeouts.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Starts a background thread that accepts one and only one client connection
 * on the server socket, sends an HTTP 307 Temporary Redirect response, and
 * then exits.  This is useful for testing timeouts on the second step of
 * methods that issue 2 HTTP requests (request 1, redirect, request 2).
 * 
 * For handling the first request, this method sets socket timeout to use the
 * initial values defined in URLUtils.  Afterwards, it guarantees that the
 * second request will use a very short timeout.
 * 
 * Optionally, the thread may consume the connection backlog immediately after
 * receiving its one and only client connection.  This is useful for forcing a
 * connection timeout on the second request.
 * 
 * On tearDown, open client connections are closed, and the thread is joined.
 * 
 * @param consumeConnectionBacklog boolean whether or not to consume connection
 *   backlog and thus force a connection timeout on the second request
 */
private void startSingleTemporaryRedirectResponseThread(
    final boolean consumeConnectionBacklog) {
  fs.connectionFactory = URLConnectionFactory.DEFAULT_SYSTEM_CONNECTION_FACTORY;
  serverThread = new Thread() {
    @Override
    public void run() {
      Socket clientSocket = null;
      OutputStream out = null;
      InputStream in = null;
      InputStreamReader isr = null;
      BufferedReader br = null;
      try {
        // Accept one and only one client connection.
        clientSocket = serverSocket.accept();

        // Immediately setup conditions for subsequent connections.
        fs.connectionFactory = connectionFactory;
        if (consumeConnectionBacklog) {
          consumeConnectionBacklog();
        }

        // Consume client's HTTP request by reading until EOF or empty line.
        in = clientSocket.getInputStream();
        isr = new InputStreamReader(in);
        br = new BufferedReader(isr);
        for (;;) {
          String line = br.readLine();
          if (line == null || line.isEmpty()) {
            break;
          }
        }

        // Write response.
        out = clientSocket.getOutputStream();
        out.write(temporaryRedirect().getBytes("UTF-8"));
      } catch (IOException e) {
        // Fail the test on any I/O error in the server thread.
        LOG.error("unexpected IOException in server thread", e);
        fail("unexpected IOException in server thread: " + e);
      } finally {
        // Clean it all up.
        IOUtils.cleanup(LOG, br, isr, in, out);
        IOUtils.closeSocket(clientSocket);
      }
    }
  };
  serverThread.start();
}