Пример #1
0
    /**
     * Connect to the server and set up the I/O streams. It then sends a header to the server and
     * starts the connection thread that waits for responses.
     *
     * @throws java.io.IOException e
     */
    protected synchronized void setupIOstreams() throws IOException, InterruptedException {

      if (socket != null || shouldCloseConnection.get()) {
        return;
      }

      if (failedServers.isFailedServer(remoteId.getAddress())) {
        if (LOG.isDebugEnabled()) {
          LOG.debug(
              "Not trying to connect to "
                  + remoteId.getAddress()
                  + " this server is in the failed servers list");
        }
        IOException e =
            new FailedServerException(
                "This server is in the failed servers list: " + remoteId.getAddress());
        markClosed(e);
        close();
        throw e;
      }

      try {
        if (LOG.isDebugEnabled()) {
          LOG.debug("Connecting to " + remoteId);
        }
        setupConnection();
        this.in =
            new DataInputStream(
                new BufferedInputStream(new PingInputStream(NetUtils.getInputStream(socket))));
        this.out = new DataOutputStream(new BufferedOutputStream(NetUtils.getOutputStream(socket)));
        writeHeader();

        // update last activity time
        touch();

        // start the receiver thread after the socket connection has been set up
        start();
      } catch (IOException e) {
        failedServers.addToFailedServers(remoteId.address);
        markClosed(e);
        close();

        throw e;
      }
    }
  /** For {@link TestTransferRbw} */
  public static BlockOpResponseProto transferRbw(
      final ExtendedBlock b, final DFSClient dfsClient, final DatanodeInfo... datanodes)
      throws IOException {
    assertEquals(2, datanodes.length);
    final Socket s =
        DFSOutputStream.createSocketForPipeline(datanodes[0], datanodes.length, dfsClient);
    final long writeTimeout = dfsClient.getDatanodeWriteTimeout(datanodes.length);
    final DataOutputStream out =
        new DataOutputStream(
            new BufferedOutputStream(
                NetUtils.getOutputStream(s, writeTimeout), HdfsConstants.SMALL_BUFFER_SIZE));
    final DataInputStream in = new DataInputStream(NetUtils.getInputStream(s));

    // send the request
    new Sender(out)
        .transferBlock(
            b,
            new Token<BlockTokenIdentifier>(),
            dfsClient.clientName,
            new DatanodeInfo[] {datanodes[1]});
    out.flush();

    return BlockOpResponseProto.parseDelimitedFrom(in);
  }