test_class.file: flink-runtime/src/test/java/org/apache/flink/runtime/net/ConnectionUtilsTest.java
test_class.identifier: ConnectionUtilsTest
test_case.body: @Test
  	public void testReturnLocalHostAddressUsingHeuristics() {
  		int unusedPort;
  		try {
  			unusedPort = org.apache.flink.util.NetUtils.getAvailablePort();
  		}
  		catch (Throwable t) {
  			// if this system cannot find an available port,
  			// skip this test
  			return;
  		}
  
  		try {
  			// create an unreachable target address
  			InetSocketAddress unreachable = new InetSocketAddress("localhost", unusedPort);
  
  			final long start = System.currentTimeMillis();
  			InetAddress add = ConnectionUtils.findConnectingAddress(unreachable, 2000, 400);
  
  			// check that it did not take forever
  			assertTrue(System.currentTimeMillis() - start < (OperatingSystem.isWindows() ? 30000 : 8000));
  
  			// we should have found a heuristic address
  			assertNotNull(add);
  
  			// make sure that we returned the InetAddress.getLocalHost as a heuristic
  			assertEquals(InetAddress.getLocalHost(), add);
  		}
  		catch (Exception e) {
  			e.printStackTrace();
  			fail(e.getMessage());
  		}
  	}
test_case.class_method_signature: ConnectionUtilsTest.testReturnLocalHostAddressUsingHeuristics()
test_case.full_signature: @Test public void testReturnLocalHostAddressUsingHeuristics()
test_case.identifier: testReturnLocalHostAddressUsingHeuristics
test_case.invocations: getAvailablePort currentTimeMillis findConnectingAddress assertTrue currentTimeMillis isWindows assertNotNull assertEquals getLocalHost printStackTrace fail getMessage
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testReturnLocalHostAddressUsingHeuristics()
focal_class.declarator: LOG = LoggerFactory.getLogger(ConnectionUtils.class)
focal_class.modifier: private static final
focal_class.original_string: private static final Logger LOG = LoggerFactory.getLogger(ConnectionUtils.class);
focal_class.type: Logger
focal_class.var_name: LOG
focal_class.declarator: MIN_SLEEP_TIME = 50
focal_class.modifier: private static final
focal_class.original_string: private static final long MIN_SLEEP_TIME = 50;
focal_class.type: long
focal_class.var_name: MIN_SLEEP_TIME
focal_class.declarator: MAX_SLEEP_TIME = 20000
focal_class.modifier: private static final
focal_class.original_string: private static final long MAX_SLEEP_TIME = 20000;
focal_class.type: long
focal_class.var_name: MAX_SLEEP_TIME
focal_class.file: flink-runtime/src/main/java/org/apache/flink/runtime/net/ConnectionUtils.java
focal_class.identifier: ConnectionUtils
focal_class.class_method_signature: ConnectionUtils.findConnectingAddress(InetSocketAddress targetAddress,
  							long maxWaitMillis, long startLoggingAfter)
focal_class.full_signature: public static InetAddress findConnectingAddress(InetSocketAddress targetAddress,
  							long maxWaitMillis, long startLoggingAfter)
focal_class.identifier: findConnectingAddress
focal_class.modifiers: public static
focal_class.parameters: (InetSocketAddress targetAddress,
  							long maxWaitMillis, long startLoggingAfter)
focal_class.return: InetAddress
focal_class.signature: InetAddress findConnectingAddress(InetSocketAddress targetAddress,
  							long maxWaitMillis, long startLoggingAfter)
focal_class.class_method_signature: ConnectionUtils.tryLocalHostBeforeReturning(
  				InetAddress preliminaryResult, SocketAddress targetAddress, boolean logging)
focal_class.full_signature: private static InetAddress tryLocalHostBeforeReturning(
  				InetAddress preliminaryResult, SocketAddress targetAddress, boolean logging)
focal_class.identifier: tryLocalHostBeforeReturning
focal_class.modifiers: private static
focal_class.parameters: (
  				InetAddress preliminaryResult, SocketAddress targetAddress, boolean logging)
focal_class.return: InetAddress
focal_class.signature: InetAddress tryLocalHostBeforeReturning(
  				InetAddress preliminaryResult, SocketAddress targetAddress, boolean logging)
focal_class.class_method_signature: ConnectionUtils.findAddressUsingStrategy(AddressDetectionState strategy,
  														InetSocketAddress targetAddress,
  														boolean logging)
focal_class.full_signature: private static InetAddress findAddressUsingStrategy(AddressDetectionState strategy,
  														InetSocketAddress targetAddress,
  														boolean logging)
focal_class.identifier: findAddressUsingStrategy
focal_class.modifiers: private static
focal_class.parameters: (AddressDetectionState strategy,
  														InetSocketAddress targetAddress,
  														boolean logging)
focal_class.return: InetAddress
focal_class.signature: InetAddress findAddressUsingStrategy(AddressDetectionState strategy,
  														InetSocketAddress targetAddress,
  														boolean logging)
focal_class.class_method_signature: ConnectionUtils.hasCommonPrefix(byte[] address, byte[] address2)
focal_class.full_signature: private static boolean hasCommonPrefix(byte[] address, byte[] address2)
focal_class.identifier: hasCommonPrefix
focal_class.modifiers: private static
focal_class.parameters: (byte[] address, byte[] address2)
focal_class.return: boolean
focal_class.signature: boolean hasCommonPrefix(byte[] address, byte[] address2)
focal_class.class_method_signature: ConnectionUtils.tryToConnect(InetAddress fromAddress, SocketAddress toSocket,
  										int timeout, boolean logFailed)
focal_class.full_signature: private static boolean tryToConnect(InetAddress fromAddress, SocketAddress toSocket,
  										int timeout, boolean logFailed)
focal_class.identifier: tryToConnect
focal_class.modifiers: private static
focal_class.parameters: (InetAddress fromAddress, SocketAddress toSocket,
  										int timeout, boolean logFailed)
focal_class.return: boolean
focal_class.signature: boolean tryToConnect(InetAddress fromAddress, SocketAddress toSocket,
  										int timeout, boolean logFailed)
focal_method.body: public static InetAddress findConnectingAddress(InetSocketAddress targetAddress,
  							long maxWaitMillis, long startLoggingAfter) throws IOException
  	{
  		if (targetAddress == null) {
  			throw new NullPointerException("targetAddress must not be null");
  		}
  		if (maxWaitMillis <= 0) {
  			throw new IllegalArgumentException("Max wait time must be positive");
  		}
  
  		final long startTime = System.currentTimeMillis();
  
  		long currentSleepTime = MIN_SLEEP_TIME;
  		long elapsedTime = 0;
  
  		// loop while there is time left
  		while (elapsedTime < maxWaitMillis) {
  			AddressDetectionState strategy = AddressDetectionState.LOCAL_HOST;
  
  			boolean logging = elapsedTime >= startLoggingAfter;
  			if (logging) {
  				LOG.info("Trying to connect to " + targetAddress);
  			}
  			// go over the strategies ADDRESS - FAST_CONNECT - SLOW_CONNECT
  			do {
  				InetAddress address = findAddressUsingStrategy(strategy, targetAddress, logging);
  				if (address != null) {
  					return address;
  				}
  
  				// pick the next strategy
  				switch (strategy) {
  					case LOCAL_HOST:
  						strategy = AddressDetectionState.ADDRESS;
  						break;
  					case ADDRESS:
  						strategy = AddressDetectionState.FAST_CONNECT;
  						break;
  					case FAST_CONNECT:
  						strategy = AddressDetectionState.SLOW_CONNECT;
  						break;
  					case SLOW_CONNECT:
  						strategy = null;
  						break;
  					default:
  						throw new RuntimeException("Unsupported strategy: " + strategy);
  				}
  			}
  			while (strategy != null);
  
  			// we have made a pass with all strategies over all interfaces
  			// sleep for a while before we make the next pass
  			elapsedTime = System.currentTimeMillis() - startTime;
  
  			long toWait = Math.min(maxWaitMillis - elapsedTime, currentSleepTime);
  			if (toWait > 0) {
  				if (logging) {
  					LOG.info("Could not connect. Waiting for {} msecs before next attempt", toWait);
  				} else {
  					LOG.debug("Could not connect. Waiting for {} msecs before next attempt", toWait);
  				}
  
  				try {
  					Thread.sleep(toWait);
  				}
  				catch (InterruptedException e) {
  					throw new IOException("Connection attempts have been interrupted.");
  				}
  			}
  
  			// increase the exponential backoff timer
  			currentSleepTime = Math.min(2 * currentSleepTime, MAX_SLEEP_TIME);
  		}
  
  		// our attempts timed out. use the heuristic fallback
  		LOG.warn("Could not connect to {}. Selecting a local address using heuristics.", targetAddress);
  		InetAddress heuristic = findAddressUsingStrategy(AddressDetectionState.HEURISTIC, targetAddress, true);
  		if (heuristic != null) {
  			return heuristic;
  		}
  		else {
  			LOG.warn("Could not find any IPv4 address that is not loopback or link-local. Using localhost address.");
  			return InetAddress.getLocalHost();
  		}
  	}
focal_method.class_method_signature: ConnectionUtils.findConnectingAddress(InetSocketAddress targetAddress,
  							long maxWaitMillis, long startLoggingAfter)
focal_method.full_signature: public static InetAddress findConnectingAddress(InetSocketAddress targetAddress,
  							long maxWaitMillis, long startLoggingAfter)
focal_method.identifier: findConnectingAddress
focal_method.invocations: currentTimeMillis info findAddressUsingStrategy currentTimeMillis min info debug sleep min warn findAddressUsingStrategy warn getLocalHost
focal_method.modifiers: public static
focal_method.parameters: (InetSocketAddress targetAddress,
  							long maxWaitMillis, long startLoggingAfter)
focal_method.return: InetAddress
focal_method.signature: InetAddress findConnectingAddress(InetSocketAddress targetAddress,
  							long maxWaitMillis, long startLoggingAfter)