Search in sources :

Example 6 with InetAddress

use of java.net.InetAddress in project camel by apache.

the class MinaUdpTest method sendUdpMessages.

protected void sendUdpMessages() throws Exception {
    DatagramSocket socket = new DatagramSocket();
    try {
        InetAddress address = InetAddress.getByName("127.0.0.1");
        for (int i = 0; i < messageCount; i++) {
            String text = "Hello Message: " + i;
            byte[] data = text.getBytes();
            DatagramPacket packet = new DatagramPacket(data, data.length, address, getPort());
            socket.send(packet);
            Thread.sleep(100);
        }
    } finally {
        socket.close();
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 7 with InetAddress

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

the class TaskManagerRunner method createRpcService.

// --------------------------------------------------------------------------------------------
//  Static utilities
// --------------------------------------------------------------------------------------------
/**
	 * Create a RPC service for the task manager.
	 *
	 * @param configuration The configuration for the TaskManager.
	 * @param haServices to use for the task manager hostname retrieval
	 */
public static RpcService createRpcService(final Configuration configuration, final HighAvailabilityServices haServices) throws Exception {
    checkNotNull(configuration);
    checkNotNull(haServices);
    String taskManagerHostname = configuration.getString(ConfigConstants.TASK_MANAGER_HOSTNAME_KEY, null);
    if (taskManagerHostname != null) {
        LOG.info("Using configured hostname/address for TaskManager: {}.", taskManagerHostname);
    } else {
        Time lookupTimeout = Time.milliseconds(AkkaUtils.getLookupTimeout(configuration).toMillis());
        InetAddress taskManagerAddress = LeaderRetrievalUtils.findConnectingAddress(haServices.getResourceManagerLeaderRetriever(), lookupTimeout);
        taskManagerHostname = taskManagerAddress.getHostName();
        LOG.info("TaskManager will use hostname/address '{}' ({}) for communication.", taskManagerHostname, taskManagerAddress.getHostAddress());
    }
    final int rpcPort = configuration.getInteger(ConfigConstants.TASK_MANAGER_IPC_PORT_KEY, 0);
    Preconditions.checkState(rpcPort >= 0 && rpcPort <= 65535, "Invalid value for " + "'%s' (port for the TaskManager actor system) : %d - Leave config parameter empty or " + "use 0 to let the system choose port automatically.", ConfigConstants.TASK_MANAGER_IPC_PORT_KEY, rpcPort);
    return RpcServiceUtils.createRpcService(taskManagerHostname, rpcPort, configuration);
}
Also used : Time(org.apache.flink.api.common.time.Time) InetAddress(java.net.InetAddress)

Example 8 with InetAddress

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

the class ZooKeeperLeaderRetrievalTest method testConnectingAddressRetrievalWithDelayedLeaderElection.

/**
	 * Tests that LeaderRetrievalUtils.findConnectingAdress finds the correct connecting address
	 * in case of an old leader address in ZooKeeper and a subsequent election of a new leader.
	 * The findConnectingAddress should block until the new leader has been elected and his
	 * address has been written to ZooKeeper.
	 */
@Test
public void testConnectingAddressRetrievalWithDelayedLeaderElection() throws Exception {
    FiniteDuration timeout = new FiniteDuration(1, TimeUnit.MINUTES);
    Configuration config = new Configuration();
    long sleepingTime = 1000;
    config.setString(HighAvailabilityOptions.HA_MODE, "zookeeper");
    config.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, testingServer.getConnectString());
    LeaderElectionService leaderElectionService = null;
    LeaderElectionService faultyLeaderElectionService;
    ServerSocket serverSocket;
    InetAddress localHost;
    Thread thread;
    CuratorFramework[] client = new CuratorFramework[2];
    try {
        client[0] = ZooKeeperUtils.startCuratorFramework(config);
        client[1] = ZooKeeperUtils.startCuratorFramework(config);
        String wrongHostPort = NetUtils.unresolvedHostAndPortToNormalizedString("1.1.1.1", 1234);
        String wrongAddress = JobManager.getRemoteJobManagerAkkaURL(AkkaUtils.getAkkaProtocol(config), wrongHostPort, Option.<String>empty());
        try {
            localHost = InetAddress.getLocalHost();
            serverSocket = new ServerSocket(0, 50, localHost);
        } catch (UnknownHostException e) {
            // may happen if disconnected. skip test.
            System.err.println("Skipping 'testNetworkInterfaceSelection' test.");
            return;
        } catch (IOException e) {
            // may happen in certain test setups, skip test.
            System.err.println("Skipping 'testNetworkInterfaceSelection' test.");
            return;
        }
        InetSocketAddress correctInetSocketAddress = new InetSocketAddress(localHost, serverSocket.getLocalPort());
        String hostPort = NetUtils.unresolvedHostAndPortToNormalizedString(localHost.getHostName(), correctInetSocketAddress.getPort());
        String correctAddress = JobManager.getRemoteJobManagerAkkaURL(AkkaUtils.getAkkaProtocol(config), hostPort, Option.<String>empty());
        faultyLeaderElectionService = ZooKeeperUtils.createLeaderElectionService(client[0], config);
        TestingContender wrongLeaderAddressContender = new TestingContender(wrongAddress, faultyLeaderElectionService);
        faultyLeaderElectionService.start(wrongLeaderAddressContender);
        FindConnectingAddress findConnectingAddress = new FindConnectingAddress(config, timeout);
        thread = new Thread(findConnectingAddress);
        thread.start();
        leaderElectionService = ZooKeeperUtils.createLeaderElectionService(client[1], config);
        TestingContender correctLeaderAddressContender = new TestingContender(correctAddress, leaderElectionService);
        Thread.sleep(sleepingTime);
        faultyLeaderElectionService.stop();
        leaderElectionService.start(correctLeaderAddressContender);
        thread.join();
        InetAddress result = findConnectingAddress.getInetAddress();
        // check that we can connect to the localHost
        Socket socket = new Socket();
        try {
            // port 0 = let the OS choose the port
            SocketAddress bindP = new InetSocketAddress(result, 0);
            // machine
            socket.bind(bindP);
            socket.connect(correctInetSocketAddress, 1000);
        } finally {
            socket.close();
        }
    } finally {
        if (leaderElectionService != null) {
            leaderElectionService.stop();
        }
        if (client[0] != null) {
            client[0].close();
        }
        if (client[1] != null) {
            client[1].close();
        }
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) FiniteDuration(scala.concurrent.duration.FiniteDuration) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) CuratorFramework(org.apache.curator.framework.CuratorFramework) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 9 with InetAddress

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

the class ZooKeeperLeaderRetrievalTest method testTimeoutOfFindConnectingAddress.

/**
	 * Tests that the LeaderRetrievalUtils.findConnectingAddress stops trying to find the
	 * connecting address if no leader address has been specified. The call should return
	 * then InetAddress.getLocalHost().
	 */
@Test
public void testTimeoutOfFindConnectingAddress() throws Exception {
    Configuration config = new Configuration();
    config.setString(HighAvailabilityOptions.HA_MODE, "zookeeper");
    config.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, testingServer.getConnectString());
    FiniteDuration timeout = new FiniteDuration(10, TimeUnit.SECONDS);
    LeaderRetrievalService leaderRetrievalService = LeaderRetrievalUtils.createLeaderRetrievalService(config);
    InetAddress result = LeaderRetrievalUtils.findConnectingAddress(leaderRetrievalService, timeout);
    assertEquals(InetAddress.getLocalHost(), result);
}
Also used : Configuration(org.apache.flink.configuration.Configuration) LeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.LeaderRetrievalService) FiniteDuration(scala.concurrent.duration.FiniteDuration) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 10 with InetAddress

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

the class SecurityUtil method getByName.

/**
   * Resolves a host subject to the security requirements determined by
   * hadoop.security.token.service.use_ip. Optionally logs slow resolutions.
   * 
   * @param hostname host or ip to resolve
   * @return a resolved host
   * @throws UnknownHostException if the host doesn't exist
   */
@InterfaceAudience.Private
public static InetAddress getByName(String hostname) throws UnknownHostException {
    if (logSlowLookups || LOG.isTraceEnabled()) {
        StopWatch lookupTimer = new StopWatch().start();
        InetAddress result = hostResolver.getByName(hostname);
        long elapsedMs = lookupTimer.stop().now(TimeUnit.MILLISECONDS);
        if (elapsedMs >= slowLookupThresholdMs) {
            LOG.warn("Slow name lookup for " + hostname + ". Took " + elapsedMs + " ms.");
        } else if (LOG.isTraceEnabled()) {
            LOG.trace("Name lookup for " + hostname + " took " + elapsedMs + " ms.");
        }
        return result;
    } else {
        return hostResolver.getByName(hostname);
    }
}
Also used : InetAddress(java.net.InetAddress) StopWatch(org.apache.hadoop.util.StopWatch)

Aggregations

InetAddress (java.net.InetAddress)2063 UnknownHostException (java.net.UnknownHostException)377 IOException (java.io.IOException)284 Test (org.junit.Test)272 InetSocketAddress (java.net.InetSocketAddress)240 NetworkInterface (java.net.NetworkInterface)178 ArrayList (java.util.ArrayList)158 SocketException (java.net.SocketException)148 Inet6Address (java.net.Inet6Address)97 HashMap (java.util.HashMap)95 Inet4Address (java.net.Inet4Address)88 Socket (java.net.Socket)73 LinkAddress (android.net.LinkAddress)70 DatagramPacket (java.net.DatagramPacket)67 Token (org.apache.cassandra.dht.Token)67 DatagramSocket (java.net.DatagramSocket)61 RouteInfo (android.net.RouteInfo)56 Map (java.util.Map)55 LinkProperties (android.net.LinkProperties)52 ServerSocket (java.net.ServerSocket)50