Search in sources :

Example 21 with Socket

use of java.net.Socket in project hadoop by apache.

the class TestDiskError method testReplicationError.

/**
   * Test that when there is a failure replicating a block the temporary
   * and meta files are cleaned up and subsequent replication succeeds.
   */
@Test
public void testReplicationError() throws Exception {
    // create a file of replication factor of 1
    final Path fileName = new Path("/test.txt");
    final int fileLen = 1;
    DFSTestUtil.createFile(fs, fileName, 1, (short) 1, 1L);
    DFSTestUtil.waitReplication(fs, fileName, (short) 1);
    // get the block belonged to the created file
    LocatedBlocks blocks = NameNodeAdapter.getBlockLocations(cluster.getNameNode(), fileName.toString(), 0, (long) fileLen);
    assertEquals("Should only find 1 block", blocks.locatedBlockCount(), 1);
    LocatedBlock block = blocks.get(0);
    // bring up a second datanode
    cluster.startDataNodes(conf, 1, true, null, null);
    cluster.waitActive();
    final int sndNode = 1;
    DataNode datanode = cluster.getDataNodes().get(sndNode);
    FsDatasetTestUtils utils = cluster.getFsDatasetTestUtils(datanode);
    // replicate the block to the second datanode
    InetSocketAddress target = datanode.getXferAddress();
    Socket s = new Socket(target.getAddress(), target.getPort());
    // write the header.
    DataOutputStream out = new DataOutputStream(s.getOutputStream());
    DataChecksum checksum = DataChecksum.newDataChecksum(DataChecksum.Type.CRC32, 512);
    new Sender(out).writeBlock(block.getBlock(), StorageType.DEFAULT, BlockTokenSecretManager.DUMMY_TOKEN, "", new DatanodeInfo[0], new StorageType[0], null, BlockConstructionStage.PIPELINE_SETUP_CREATE, 1, 0L, 0L, 0L, checksum, CachingStrategy.newDefaultStrategy(), false, false, null);
    out.flush();
    // close the connection before sending the content of the block
    out.close();
    // the temporary block & meta files should be deleted
    String bpid = cluster.getNamesystem().getBlockPoolId();
    while (utils.getStoredReplicas(bpid).hasNext()) {
        Thread.sleep(100);
    }
    // then increase the file's replication factor
    fs.setReplication(fileName, (short) 2);
    // replication should succeed
    DFSTestUtil.waitReplication(fs, fileName, (short) 1);
    // clean up the file
    fs.delete(fileName, false);
}
Also used : Path(org.apache.hadoop.fs.Path) Sender(org.apache.hadoop.hdfs.protocol.datatransfer.Sender) InetSocketAddress(java.net.InetSocketAddress) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) DataOutputStream(java.io.DataOutputStream) LocatedBlocks(org.apache.hadoop.hdfs.protocol.LocatedBlocks) LocatedBlock(org.apache.hadoop.hdfs.protocol.LocatedBlock) Socket(java.net.Socket) DataChecksum(org.apache.hadoop.util.DataChecksum) Test(org.junit.Test)

Example 22 with Socket

use of java.net.Socket in project hadoop by apache.

the class TestIPC method doIpcVersionTest.

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();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 23 with Socket

use of java.net.Socket in project hadoop by apache.

the class TestIPC method testRTEDuringConnectionSetup.

/**
   * Test that, if a RuntimeException is thrown after creating a socket
   * but before successfully connecting to the IPC server, that the
   * failure is handled properly. This is a regression test for
   * HADOOP-7428.
   */
@Test(timeout = 60000)
public void testRTEDuringConnectionSetup() throws IOException {
    // Set up a socket factory which returns sockets which
    // throw an RTE when setSoTimeout is called.
    SocketFactory spyFactory = spy(NetUtils.getDefaultSocketFactory(conf));
    Mockito.doAnswer(new Answer<Socket>() {

        @Override
        public Socket answer(InvocationOnMock invocation) throws Throwable {
            Socket s = spy((Socket) invocation.callRealMethod());
            doThrow(new RuntimeException("Injected fault")).when(s).setSoTimeout(anyInt());
            return s;
        }
    }).when(spyFactory).createSocket();
    Server server = new TestServer(1, true);
    Client client = new Client(LongWritable.class, conf, spyFactory);
    server.start();
    try {
        // Call should fail due to injected exception.
        InetSocketAddress address = NetUtils.getConnectAddress(server);
        try {
            call(client, RANDOM.nextLong(), address, conf);
            fail("Expected an exception to have been thrown");
        } catch (Exception e) {
            LOG.info("caught expected exception", e);
            assertTrue(StringUtils.stringifyException(e).contains("Injected fault"));
        }
        // Resetting to the normal socket behavior should succeed
        // (i.e. it should not have cached a half-constructed connection)
        Mockito.reset(spyFactory);
        call(client, RANDOM.nextLong(), address, conf);
    } finally {
        client.stop();
        server.stop();
    }
}
Also used : SocketFactory(javax.net.SocketFactory) InvocationOnMock(org.mockito.invocation.InvocationOnMock) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) TimeoutException(java.util.concurrent.TimeoutException) ConnectTimeoutException(org.apache.hadoop.net.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test)

Example 24 with Socket

use of java.net.Socket in project hadoop by apache.

the class TestSaslDataTransfer method TestPeerFromSocketAndKeyReadTimeout.

/**
   * Verifies that peerFromSocketAndKey honors socket read timeouts.
   */
@Test(timeout = 60000)
public void TestPeerFromSocketAndKeyReadTimeout() throws Exception {
    HdfsConfiguration conf = createSecureConfig("authentication,integrity,privacy");
    AtomicBoolean fallbackToSimpleAuth = new AtomicBoolean(false);
    SaslDataTransferClient saslClient = new SaslDataTransferClient(conf, DataTransferSaslUtil.getSaslPropertiesResolver(conf), TrustedChannelResolver.getInstance(conf), fallbackToSimpleAuth);
    DatanodeID fakeDatanodeId = new DatanodeID("127.0.0.1", "localhost", "beefbeef-beef-beef-beef-beefbeefbeef", 1, 2, 3, 4);
    DataEncryptionKeyFactory dataEncKeyFactory = new DataEncryptionKeyFactory() {

        @Override
        public DataEncryptionKey newDataEncryptionKey() {
            return new DataEncryptionKey(123, "456", new byte[8], new byte[8], 1234567, "fakeAlgorithm");
        }
    };
    ServerSocket serverSocket = null;
    Socket socket = null;
    try {
        serverSocket = new ServerSocket(0, -1);
        socket = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
        Peer peer = DFSUtilClient.peerFromSocketAndKey(saslClient, socket, dataEncKeyFactory, new Token(), fakeDatanodeId, 1);
        peer.close();
        Assert.fail("Expected DFSClient#peerFromSocketAndKey to time out.");
    } catch (SocketTimeoutException e) {
        GenericTestUtils.assertExceptionContains("Read timed out", e);
    } finally {
        IOUtils.cleanup(null, socket, serverSocket);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DatanodeID(org.apache.hadoop.hdfs.protocol.DatanodeID) DataEncryptionKey(org.apache.hadoop.hdfs.security.token.block.DataEncryptionKey) SocketTimeoutException(java.net.SocketTimeoutException) Peer(org.apache.hadoop.hdfs.net.Peer) ServerSocket(java.net.ServerSocket) Token(org.apache.hadoop.security.token.Token) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 25 with Socket

use of java.net.Socket in project hadoop by apache.

the class DFSClient method newConnectedPeer.

// RemotePeerFactory
@Override
public Peer newConnectedPeer(InetSocketAddress addr, Token<BlockTokenIdentifier> blockToken, DatanodeID datanodeId) throws IOException {
    Peer peer = null;
    boolean success = false;
    Socket sock = null;
    final int socketTimeout = dfsClientConf.getSocketTimeout();
    try {
        sock = socketFactory.createSocket();
        NetUtils.connect(sock, addr, getRandomLocalInterfaceAddr(), socketTimeout);
        peer = DFSUtilClient.peerFromSocketAndKey(saslClient, sock, this, blockToken, datanodeId, socketTimeout);
        success = true;
        return peer;
    } finally {
        if (!success) {
            IOUtilsClient.cleanup(LOG, peer);
            IOUtils.closeSocket(sock);
        }
    }
}
Also used : Peer(org.apache.hadoop.hdfs.net.Peer) Socket(java.net.Socket)

Aggregations

Socket (java.net.Socket)1553 IOException (java.io.IOException)664 ServerSocket (java.net.ServerSocket)548 OutputStream (java.io.OutputStream)364 InetSocketAddress (java.net.InetSocketAddress)350 Test (org.junit.Test)348 InputStream (java.io.InputStream)249 InputStreamReader (java.io.InputStreamReader)170 BufferedReader (java.io.BufferedReader)151 SocketException (java.net.SocketException)132 SSLSocket (javax.net.ssl.SSLSocket)103 SocketTimeoutException (java.net.SocketTimeoutException)97 UnknownHostException (java.net.UnknownHostException)84 ConnectException (java.net.ConnectException)82 ByteArrayOutputStream (java.io.ByteArrayOutputStream)75 InetAddress (java.net.InetAddress)73 OutputStreamWriter (java.io.OutputStreamWriter)70 ServletOutputStream (javax.servlet.ServletOutputStream)68 DataOutputStream (java.io.DataOutputStream)67 CountDownLatch (java.util.concurrent.CountDownLatch)64