Search in sources :

Example 51 with InetSocketAddress

use of java.net.InetSocketAddress in project flink by apache.

the class BlobLibraryCacheManagerTest method testLibraryCacheManagerCleanup.

@Test
public void testLibraryCacheManagerCleanup() {
    JobID jid = new JobID();
    List<BlobKey> keys = new ArrayList<BlobKey>();
    BlobServer server = null;
    BlobLibraryCacheManager libraryCacheManager = null;
    final byte[] buf = new byte[128];
    try {
        Configuration config = new Configuration();
        server = new BlobServer(config);
        InetSocketAddress blobSocketAddress = new InetSocketAddress(server.getPort());
        BlobClient bc = new BlobClient(blobSocketAddress, config);
        keys.add(bc.put(buf));
        buf[0] += 1;
        keys.add(bc.put(buf));
        long cleanupInterval = 1000l;
        libraryCacheManager = new BlobLibraryCacheManager(server, cleanupInterval);
        libraryCacheManager.registerJob(jid, keys, Collections.<URL>emptyList());
        List<File> files = new ArrayList<File>();
        for (BlobKey key : keys) {
            files.add(libraryCacheManager.getFile(key));
        }
        assertEquals(2, files.size());
        files.clear();
        libraryCacheManager.unregisterJob(jid);
        // because we cannot guarantee that there are not thread races in the build system, we
        // loop for a certain while until the references disappear
        {
            long deadline = System.currentTimeMillis() + 30000;
            do {
                Thread.sleep(500);
            } while (libraryCacheManager.getNumberOfCachedLibraries() > 0 && System.currentTimeMillis() < deadline);
        }
        // this fails if we exited via a timeout
        assertEquals(0, libraryCacheManager.getNumberOfCachedLibraries());
        int caughtExceptions = 0;
        for (BlobKey key : keys) {
            // the blob cache should no longer contain the files
            try {
                files.add(libraryCacheManager.getFile(key));
            } catch (IOException ioe) {
                caughtExceptions++;
            }
        }
        assertEquals(2, caughtExceptions);
        bc.close();
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (server != null) {
            server.shutdown();
        }
        if (libraryCacheManager != null) {
            try {
                libraryCacheManager.shutdown();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : BlobClient(org.apache.flink.runtime.blob.BlobClient) Configuration(org.apache.flink.configuration.Configuration) GlobalConfiguration(org.apache.flink.configuration.GlobalConfiguration) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) BlobKey(org.apache.flink.runtime.blob.BlobKey) BlobServer(org.apache.flink.runtime.blob.BlobServer) File(java.io.File) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 52 with InetSocketAddress

use of java.net.InetSocketAddress in project flink by apache.

the class BlobLibraryCacheManagerTest method testRegisterAndDownload.

@Test
public void testRegisterAndDownload() {
    //setWritable doesn't work on Windows.
    assumeTrue(!OperatingSystem.isWindows());
    BlobServer server = null;
    BlobCache cache = null;
    File cacheDir = null;
    try {
        // create the blob transfer services
        Configuration config = new Configuration();
        server = new BlobServer(config);
        InetSocketAddress serverAddress = new InetSocketAddress("localhost", server.getPort());
        cache = new BlobCache(serverAddress, config);
        // upload some meaningless data to the server
        BlobClient uploader = new BlobClient(serverAddress, config);
        BlobKey dataKey1 = uploader.put(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
        BlobKey dataKey2 = uploader.put(new byte[] { 11, 12, 13, 14, 15, 16, 17, 18 });
        uploader.close();
        BlobLibraryCacheManager libCache = new BlobLibraryCacheManager(cache, 1000000000L);
        assertEquals(0, libCache.getNumberOfCachedLibraries());
        // first try to access a non-existing entry
        try {
            libCache.getClassLoader(new JobID());
            fail("Should fail with an IllegalStateException");
        } catch (IllegalStateException e) {
        // that#s what we want
        }
        // now register some BLOBs as libraries
        {
            JobID jid = new JobID();
            ExecutionAttemptID executionId = new ExecutionAttemptID();
            Collection<BlobKey> keys = Collections.singleton(dataKey1);
            libCache.registerTask(jid, executionId, keys, Collections.<URL>emptyList());
            assertEquals(1, libCache.getNumberOfReferenceHolders(jid));
            assertEquals(1, libCache.getNumberOfCachedLibraries());
            assertNotNull(libCache.getClassLoader(jid));
            // un-register them again
            libCache.unregisterTask(jid, executionId);
            // Don't fail if called again
            libCache.unregisterTask(jid, executionId);
            assertEquals(0, libCache.getNumberOfReferenceHolders(jid));
            // library is still cached (but not associated with job any more)
            assertEquals(1, libCache.getNumberOfCachedLibraries());
            // should not be able to access the classloader any more
            try {
                libCache.getClassLoader(jid);
                fail("Should fail with an IllegalStateException");
            } catch (IllegalStateException e) {
            // that#s what we want
            }
        }
        cacheDir = new File(cache.getStorageDir(), "cache");
        assertTrue(cacheDir.exists());
        // make sure no further blobs can be downloaded by removing the write
        // permissions from the directory
        assertTrue("Could not remove write permissions from cache directory", cacheDir.setWritable(false, false));
        // since we cannot download this library any more, this call should fail
        try {
            libCache.registerTask(new JobID(), new ExecutionAttemptID(), Collections.singleton(dataKey2), Collections.<URL>emptyList());
            fail("This should fail with an IOException");
        } catch (IOException e) {
        // splendid!
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (cacheDir != null) {
            if (!cacheDir.setWritable(true, false)) {
                System.err.println("Could not re-add write permissions to cache directory.");
            }
        }
        if (cache != null) {
            cache.shutdown();
        }
        if (server != null) {
            server.shutdown();
        }
    }
}
Also used : BlobClient(org.apache.flink.runtime.blob.BlobClient) ExecutionAttemptID(org.apache.flink.runtime.executiongraph.ExecutionAttemptID) Configuration(org.apache.flink.configuration.Configuration) GlobalConfiguration(org.apache.flink.configuration.GlobalConfiguration) InetSocketAddress(java.net.InetSocketAddress) BlobCache(org.apache.flink.runtime.blob.BlobCache) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) BlobKey(org.apache.flink.runtime.blob.BlobKey) Collection(java.util.Collection) BlobServer(org.apache.flink.runtime.blob.BlobServer) File(java.io.File) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 53 with InetSocketAddress

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

the class HttpServer2 method getConnectorAddress.

/**
   * Get the address that corresponds to a particular connector.
   *
   * @return the corresponding address for the connector, or null if there's no
   *         such connector or the connector is not bounded or was closed.
   */
public InetSocketAddress getConnectorAddress(int index) {
    Preconditions.checkArgument(index >= 0);
    if (index > webServer.getConnectors().length)
        return null;
    ServerConnector c = (ServerConnector) webServer.getConnectors()[index];
    if (c.getLocalPort() == -1 || c.getLocalPort() == -2) {
        // The connector is not bounded or was closed
        return null;
    }
    return new InetSocketAddress(c.getHost(), c.getLocalPort());
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) InetSocketAddress(java.net.InetSocketAddress)

Example 54 with InetSocketAddress

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

the class ZKFailoverController method initRPC.

protected void initRPC() throws IOException {
    InetSocketAddress bindAddr = getRpcAddressToBindTo();
    rpcServer = new ZKFCRpcServer(conf, bindAddr, this, getPolicyProvider());
}
Also used : InetSocketAddress(java.net.InetSocketAddress)

Example 55 with InetSocketAddress

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

the class TestNetUtils method testCreateSocketAddress.

@Test
public void testCreateSocketAddress() throws Throwable {
    InetSocketAddress addr = NetUtils.createSocketAddr("127.0.0.1:12345", 1000, "myconfig");
    assertEquals("127.0.0.1", addr.getAddress().getHostAddress());
    assertEquals(12345, addr.getPort());
    addr = NetUtils.createSocketAddr("127.0.0.1", 1000, "myconfig");
    assertEquals("127.0.0.1", addr.getAddress().getHostAddress());
    assertEquals(1000, addr.getPort());
    try {
        addr = NetUtils.createSocketAddr("127.0.0.1:blahblah", 1000, "myconfig");
        fail("Should have failed to parse bad port");
    } catch (IllegalArgumentException iae) {
        assertInException(iae, "myconfig");
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2586 Test (org.junit.Test)595 IOException (java.io.IOException)592 Socket (java.net.Socket)345 InetAddress (java.net.InetAddress)242 SocketAddress (java.net.SocketAddress)176 ServerSocket (java.net.ServerSocket)170 ArrayList (java.util.ArrayList)168 Configuration (org.apache.hadoop.conf.Configuration)140 ByteBuffer (java.nio.ByteBuffer)129 UnknownHostException (java.net.UnknownHostException)122 InputStream (java.io.InputStream)102 OutputStream (java.io.OutputStream)101 SocketChannel (java.nio.channels.SocketChannel)101 SocketException (java.net.SocketException)89 File (java.io.File)88 HashMap (java.util.HashMap)78 URI (java.net.URI)72 Proxy (java.net.Proxy)65 SocketTimeoutException (java.net.SocketTimeoutException)65