Search in sources :

Example 46 with SocketFactory

use of javax.net.SocketFactory in project hadoop by apache.

the class NetUtils method getSocketFactory.

/**
   * Get the socket factory for the given class according to its
   * configuration parameter
   * <tt>hadoop.rpc.socket.factory.class.&lt;ClassName&gt;</tt>. When no
   * such parameter exists then fall back on the default socket factory as
   * configured by <tt>hadoop.rpc.socket.factory.class.default</tt>. If
   * this default socket factory is not configured, then fall back on the JVM
   * default socket factory.
   * 
   * @param conf the configuration
   * @param clazz the class (usually a {@link VersionedProtocol})
   * @return a socket factory
   */
public static SocketFactory getSocketFactory(Configuration conf, Class<?> clazz) {
    SocketFactory factory = null;
    String propValue = conf.get("hadoop.rpc.socket.factory.class." + clazz.getSimpleName());
    if ((propValue != null) && (propValue.length() > 0))
        factory = getSocketFactoryFromProperty(conf, propValue);
    if (factory == null)
        factory = getDefaultSocketFactory(conf);
    return factory;
}
Also used : SocketFactory(javax.net.SocketFactory)

Example 47 with SocketFactory

use of javax.net.SocketFactory in project hadoop by apache.

the class TestIPC method assertRetriesOnSocketTimeouts.

private void assertRetriesOnSocketTimeouts(Configuration conf, int maxTimeoutRetries) throws IOException {
    SocketFactory mockFactory = Mockito.mock(SocketFactory.class);
    doThrow(new ConnectTimeoutException("fake")).when(mockFactory).createSocket();
    Client client = new Client(LongWritable.class, conf, mockFactory);
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", 9090);
    try {
        call(client, RANDOM.nextLong(), address, conf);
        fail("Not throwing the SocketTimeoutException");
    } catch (SocketTimeoutException e) {
        Mockito.verify(mockFactory, Mockito.times(maxTimeoutRetries)).createSocket();
    }
    client.stop();
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) SocketFactory(javax.net.SocketFactory) InetSocketAddress(java.net.InetSocketAddress) ConnectTimeoutException(org.apache.hadoop.net.ConnectTimeoutException)

Example 48 with SocketFactory

use of javax.net.SocketFactory in project hadoop by apache.

the class TestSocketFactory method testSocketFactoryAsKeyInMap.

@Test
public void testSocketFactoryAsKeyInMap() {
    Map<SocketFactory, Integer> dummyCache = new HashMap<SocketFactory, Integer>();
    int toBeCached1 = 1;
    int toBeCached2 = 2;
    Configuration conf = new Configuration();
    conf.set(CommonConfigurationKeys.HADOOP_RPC_SOCKET_FACTORY_CLASS_DEFAULT_KEY, "org.apache.hadoop.ipc.TestSocketFactory$DummySocketFactory");
    final SocketFactory dummySocketFactory = NetUtils.getDefaultSocketFactory(conf);
    dummyCache.put(dummySocketFactory, toBeCached1);
    conf.set(CommonConfigurationKeys.HADOOP_RPC_SOCKET_FACTORY_CLASS_DEFAULT_KEY, "org.apache.hadoop.net.StandardSocketFactory");
    final SocketFactory defaultSocketFactory = NetUtils.getDefaultSocketFactory(conf);
    dummyCache.put(defaultSocketFactory, toBeCached2);
    Assert.assertEquals("The cache contains two elements", 2, dummyCache.size());
    Assert.assertEquals("Equals of both socket factory shouldn't be same", defaultSocketFactory.equals(dummySocketFactory), false);
    assertSame(toBeCached2, dummyCache.remove(defaultSocketFactory));
    dummyCache.put(defaultSocketFactory, toBeCached2);
    assertSame(toBeCached1, dummyCache.remove(dummySocketFactory));
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) HashMap(java.util.HashMap) SocksSocketFactory(org.apache.hadoop.net.SocksSocketFactory) SocketFactory(javax.net.SocketFactory) StandardSocketFactory(org.apache.hadoop.net.StandardSocketFactory) Test(org.junit.Test)

Example 49 with SocketFactory

use of javax.net.SocketFactory in project hadoop by apache.

the class TestDataNodeTcpNoDelay method testTcpNoDelayEnabled.

@Test
public void testTcpNoDelayEnabled() throws Exception {
    Configuration testConf = new Configuration(baseConf);
    // here we do not have to config TCP_NDELAY settings, since they should be
    // active by default
    testConf.set(HADOOP_RPC_SOCKET_FACTORY_CLASS_DEFAULT_KEY, SocketFactoryWrapper.class.getName());
    SocketFactory defaultFactory = NetUtils.getDefaultSocketFactory(testConf);
    LOG.info("Socket factory is " + defaultFactory.getClass().getName());
    MiniDFSCluster dfsCluster = new MiniDFSCluster.Builder(testConf).numDataNodes(3).build();
    dfsCluster.waitActive();
    DistributedFileSystem dfs = dfsCluster.getFileSystem();
    try {
        createData(dfs);
        transferBlock(dfs);
        // check that TCP_NODELAY has been set on all sockets
        assertTrue(SocketFactoryWrapper.wasTcpNoDelayActive());
    } finally {
        SocketFactoryWrapper.reset();
        dfsCluster.shutdown();
    }
}
Also used : MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) Configuration(org.apache.hadoop.conf.Configuration) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) SocketFactory(javax.net.SocketFactory) StandardSocketFactory(org.apache.hadoop.net.StandardSocketFactory) DistributedFileSystem(org.apache.hadoop.hdfs.DistributedFileSystem) Test(org.junit.Test)

Example 50 with SocketFactory

use of javax.net.SocketFactory in project hadoop by apache.

the class TestDataNodeTcpNoDelay method testTcpNoDelayDisabled.

@Test
public void testTcpNoDelayDisabled() throws Exception {
    Configuration testConf = new Configuration(baseConf);
    // disable TCP_NODELAY in settings
    setTcpNoDelay(testConf, false);
    testConf.set(HADOOP_RPC_SOCKET_FACTORY_CLASS_DEFAULT_KEY, SocketFactoryWrapper.class.getName());
    SocketFactory defaultFactory = NetUtils.getDefaultSocketFactory(testConf);
    LOG.info("Socket factory is " + defaultFactory.getClass().getName());
    MiniDFSCluster dfsCluster = new MiniDFSCluster.Builder(testConf).numDataNodes(3).build();
    dfsCluster.waitActive();
    DistributedFileSystem dfs = dfsCluster.getFileSystem();
    try {
        createData(dfs);
        transferBlock(dfs);
        // we can only check that TCP_NODELAY was disabled on some sockets,
        // since part of the client write path always enables TCP_NODELAY
        // by necessity
        assertFalse(SocketFactoryWrapper.wasTcpNoDelayActive());
    } finally {
        SocketFactoryWrapper.reset();
        dfsCluster.shutdown();
    }
}
Also used : MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) Configuration(org.apache.hadoop.conf.Configuration) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) SocketFactory(javax.net.SocketFactory) StandardSocketFactory(org.apache.hadoop.net.StandardSocketFactory) DistributedFileSystem(org.apache.hadoop.hdfs.DistributedFileSystem) Test(org.junit.Test)

Aggregations

SocketFactory (javax.net.SocketFactory)66 Socket (java.net.Socket)25 Test (org.junit.Test)25 IOException (java.io.IOException)18 InetSocketAddress (java.net.InetSocketAddress)14 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 SSLSocket (javax.net.ssl.SSLSocket)10 OutputStream (java.io.OutputStream)9 ServerSocket (java.net.ServerSocket)9 SocketAddress (java.net.SocketAddress)6 Configuration (org.apache.hadoop.conf.Configuration)5 ServerSocketFactory (javax.net.ServerSocketFactory)4 InputStream (java.io.InputStream)3 InetAddress (java.net.InetAddress)3 UnknownHostException (java.net.UnknownHostException)3 ProtocolSocketFactory (org.apache.commons.httpclient.protocol.ProtocolSocketFactory)3 StandardSocketFactory (org.apache.hadoop.net.StandardSocketFactory)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 SocketException (java.net.SocketException)2