Example usage for java.net InetSocketAddress isUnresolved

List of usage examples for java.net InetSocketAddress isUnresolved

Introduction

In this page you can find the example usage for java.net InetSocketAddress isUnresolved.

Prototype

public final boolean isUnresolved() 

Source Link

Document

Checks whether the address has been resolved or not.

Usage

From source file:org.apache.hadoop.hdfs.qjournal.server.JournalNodeJspHelper.java

/**
 * Returns the address of the host minimizing DNS lookups. 
 * @param addr/*from  www .j a va  2s.  c  o m*/
 * @return
 */
private static String getHostAddress(InetSocketAddress addr) {
    String hostToAppend = "";
    if (addr.isUnresolved()) {
        hostToAppend = addr.getHostName();
    } else {
        hostToAppend = addr.getAddress().getHostAddress();
    }
    return hostToAppend;
}

From source file:org.apache.hadoop.hdfs.server.blockmanagement.HostFileManager.java

@VisibleForTesting
static InetSocketAddress parseEntry(String type, String fn, String line) {
    try {/*from ww w .ja  v  a  2 s.com*/
        URI uri = new URI("dummy", line, null, null, null);
        int port = uri.getPort() == -1 ? 0 : uri.getPort();
        InetSocketAddress addr = new InetSocketAddress(uri.getHost(), port);
        if (addr.isUnresolved()) {
            LOG.warn(String.format("Failed to resolve address `%s` in `%s`. " + "Ignoring in the %s list.",
                    line, fn, type));
            return null;
        }
        return addr;
    } catch (URISyntaxException e) {
        LOG.warn(String.format("Failed to parse `%s` in `%s`. " + "Ignoring in " + "the %s list.", line, fn,
                type));
    }
    return null;
}

From source file:org.apache.hadoop.mapred.HAUtil.java

/**
 * Returns logicalName and jobtracker Id when the local host matches the
 * configuration parameter {@code addressKey}.<logical>.<jobtracker Id>
 * /*ww  w . j a  v  a  2  s .c  o m*/
 * @param conf Configuration
 * @param addressKey configuration key corresponding to the address.
 * @param logicalName
 * @param matcher matching criteria for matching the address
 * @return Array with logical name and jobtracker Id on success. First element
 *         in the array is logical name and second element is jobtracker Id.
 *         Null value indicates that the configuration does not have the the
 *         Id.
 * @throws HadoopIllegalArgumentException on error
 */
static String[] getSuffixIDs(final Configuration conf, final String addressKey, String logicalName,
        final AddressMatcher matcher) {
    String jobTrackerId = null;
    int found = 0;

    Collection<String> jtIds = getJtServiceIds(conf, logicalName);
    for (String jtId : emptyAsSingletonNull(jtIds)) {
        if (LOG.isTraceEnabled()) {
            LOG.trace(String.format("addressKey: %s logicalName: %s jtId: %s", addressKey, logicalName, jtId));
        }
        String key = DFSUtil.addKeySuffixes(addressKey, logicalName, jtId);
        String addr = conf.get(key);
        if (addr == null) {
            continue;
        }
        InetSocketAddress s = null;
        try {
            s = NetUtils.createSocketAddr(addr);
        } catch (Exception e) {
            LOG.warn("Exception in creating socket address " + addr, e);
            continue;
        }
        if (!s.isUnresolved() && matcher.match(s)) {
            jobTrackerId = jtId;
            found++;
        }
    }

    // Only one address must match the local address
    if (found == 0) {
        String msg = "Configuration has no addresses that match "
                + "local node's address. Please configure the system with " + MR_HA_JOBTRACKER_ID_KEY;
        throw new HadoopIllegalArgumentException(msg);
    } else if (found > 1) {
        String msg = "Configuration has multiple addresses that match "
                + "local node's address. Please configure the system with " + MR_HA_JOBTRACKER_ID_KEY;
        throw new HadoopIllegalArgumentException(msg);
    }
    return new String[] { logicalName, jobTrackerId };
}

From source file:org.apache.hadoop.security.SecurityUtil.java

/**
 * Construct the service key for a token
 * @param addr InetSocketAddress of remote connection with a token
 * @return "ip:port" or "host:port" depending on the value of
 *          hadoop.security.token.service.use_ip
 */// ww w. j  a  va2  s.c  om
public static Text buildTokenService(InetSocketAddress addr) {
    String host = null;
    if (useIpForTokenService) {
        if (addr.isUnresolved()) { // host has no ip address
            throw new IllegalArgumentException(new UnknownHostException(addr.getHostName()));
        }
        host = addr.getAddress().getHostAddress();
    } else {
        host = addr.getHostName().toLowerCase();
    }
    return new Text(host + ":" + addr.getPort());
}

From source file:org.apache.hadoop.security.TestSecurityUtil.java

private void verifyValues(InetSocketAddress addr, String host, String ip, int port) {
    assertTrue(!addr.isUnresolved());
    // don't know what the standard resolver will return for hostname.
    // should be host for host; host or ip for ip is ambiguous
    if (!SecurityUtil.useIpForTokenService) {
        assertEquals(host, addr.getHostName());
        assertEquals(host, addr.getAddress().getHostName());
    }/*  w  w  w  . j a va 2  s  .  co  m*/
    assertEquals(ip, addr.getAddress().getHostAddress());
    assertEquals(port, addr.getPort());
}

From source file:org.apache.hadoop.yarn.conf.HAUtil.java

/**
 * @param conf Configuration. Please use verifyAndSetRMHAId to check.
 * @return RM Id on success/*w  ww. j  a  v  a2s  . c  om*/
 */
public static String getRMHAId(Configuration conf) {
    int found = 0;
    String currentRMId = conf.getTrimmed(YarnConfiguration.RM_HA_ID);
    if (currentRMId == null) {
        for (String rmId : getRMHAIds(conf)) {
            String key = addSuffix(YarnConfiguration.RM_ADDRESS, rmId);
            String addr = conf.get(key);
            if (addr == null) {
                continue;
            }
            InetSocketAddress s;
            try {
                s = NetUtils.createSocketAddr(addr);
            } catch (Exception e) {
                LOG.warn("Exception in creating socket address " + addr, e);
                continue;
            }
            if (!s.isUnresolved() && NetUtils.isLocalAddress(s.getAddress())) {
                currentRMId = rmId.trim();
                found++;
            }
        }
    }
    if (found > 1) { // Only one address must match the local address
        String msg = "The HA Configuration has multiple addresses that match " + "local node's address.";
        throw new HadoopIllegalArgumentException(msg);
    }
    return currentRMId;
}

From source file:org.apache.hadoop.yarn.server.MiniYARNCluster.java

public static String getHostname() {
    try {/*from w ww.  j  a v a 2s. c o m*/
        String hostname = InetAddress.getLocalHost().getHostName();
        // Create InetSocketAddress to see whether it is resolved or not.
        // If not, just return "localhost".
        InetSocketAddress addr = NetUtils.createSocketAddrForHost(hostname, 1);
        if (addr.isUnresolved()) {
            return "localhost";
        } else {
            return hostname;
        }
    } catch (UnknownHostException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.apache.kudu.mapreduce.KuduTableInputFormat.java

/**
 * This method might seem alien, but we do this in order to resolve the hostnames the same way
 * Hadoop does. This ensures we get locality if Kudu is running along MR/YARN.
 * @param host hostname we got from the master
 * @param port port we got from the master
 * @return reverse DNS'd address/*from   www .  j a va  2s .  c  om*/
 */
private String reverseDNS(String host, Integer port) {
    String location = this.reverseDNSCacheMap.get(host);
    if (location != null) {
        return location;
    }
    // The below InetSocketAddress creation does a name resolution.
    InetSocketAddress isa = new InetSocketAddress(host, port);
    if (isa.isUnresolved()) {
        LOG.warn("Failed address resolve for: " + isa);
    }
    InetAddress tabletInetAddress = isa.getAddress();
    try {
        location = domainNamePointerToHostName(DNS.reverseDns(tabletInetAddress, this.nameServer));
        this.reverseDNSCacheMap.put(host, location);
    } catch (NamingException e) {
        LOG.warn("Cannot resolve the host name for " + tabletInetAddress + " because of " + e);
        location = host;
    }
    return location;
}

From source file:org.apache.phoenix.hive.util.PhoenixStorageHandlerUtil.java

public static String getRegionLocation(HRegionLocation location, Log log) throws IOException {
    InetSocketAddress isa = new InetSocketAddress(location.getHostname(), location.getPort());
    if (isa.isUnresolved()) {
        log.warn("Failed resolve " + isa);
    }/*w ww .  ja v  a2 s.c o m*/
    InetAddress regionAddress = isa.getAddress();
    String regionLocation = null;
    try {
        regionLocation = reverseDNS(regionAddress);
    } catch (NamingException e) {
        log.warn("Cannot resolve the host name for " + regionAddress + " because of " + e);
        regionLocation = location.getHostname();
    }

    return regionLocation;
}

From source file:org.apache.tajo.rpc.NettyClientBase.java

private static InetSocketAddress resolveAddress(InetSocketAddress address) {
    if (address.isUnresolved()) {
        return RpcUtils.createSocketAddr(address.getHostName(), address.getPort());
    }/*from   w  ww  . j a  v a2s. c  om*/
    return address;
}