Example usage for com.google.common.net HostAndPort fromString

List of usage examples for com.google.common.net HostAndPort fromString

Introduction

In this page you can find the example usage for com.google.common.net HostAndPort fromString.

Prototype

public static HostAndPort fromString(String hostPortString) 

Source Link

Document

Split a freeform string into a host and port, without strict validation.

Usage

From source file:alluxio.hadoop.AbstractFileSystem.java

@Override
public BlockLocation[] getFileBlockLocations(FileStatus file, long start, long len) throws IOException {
    if (file == null) {
        return null;
    }//from  ww  w .  j a  va2 s.c o  m
    if (mStatistics != null) {
        mStatistics.incrementReadOps(1);
    }

    AlluxioURI path = new AlluxioURI(HadoopUtils.getPathWithoutScheme(file.getPath()));
    List<FileBlockInfo> blocks = getFileBlocks(path);
    List<BlockLocation> blockLocations = new ArrayList<>();
    for (FileBlockInfo fileBlockInfo : blocks) {
        long offset = fileBlockInfo.getOffset();
        long end = offset + fileBlockInfo.getBlockInfo().getLength();
        // Check if there is any overlapping between [start, start+len] and [offset, end]
        if (end >= start && offset <= start + len) {
            ArrayList<String> names = new ArrayList<>();
            ArrayList<String> hosts = new ArrayList<>();
            // add the existing in-memory block locations
            for (alluxio.wire.BlockLocation location : fileBlockInfo.getBlockInfo().getLocations()) {
                HostAndPort address = HostAndPort.fromParts(location.getWorkerAddress().getHost(),
                        location.getWorkerAddress().getDataPort());
                names.add(address.toString());
                hosts.add(address.getHostText());
            }
            // add under file system locations
            for (String location : fileBlockInfo.getUfsLocations()) {
                names.add(location);
                hosts.add(HostAndPort.fromString(location).getHostText());
            }
            blockLocations.add(new BlockLocation(CommonUtils.toStringArray(names),
                    CommonUtils.toStringArray(hosts), offset, fileBlockInfo.getBlockInfo().getLength()));
        }
    }

    BlockLocation[] ret = new BlockLocation[blockLocations.size()];
    blockLocations.toArray(ret);
    return ret;
}

From source file:com.intellij.util.net.HttpProxySettingsUi.java

private boolean isValid() {
    if (myUseHTTPProxyRb.isSelected()) {
        String host = getText(myProxyHostTextField);
        if (host == null) {
            return false;
        }/*from w ww . j a va2  s  . c o  m*/

        try {
            HostAndPort parsedHost = HostAndPort.fromString(host);
            if (parsedHost.hasPort()) {
                return false;
            }
            host = parsedHost.getHostText();

            try {
                InetAddresses.forString(host);
                return true;
            } catch (IllegalArgumentException e) {
                // it is not an IPv4 or IPv6 literal
            }

            InternetDomainName.from(host);
        } catch (IllegalArgumentException e) {
            return false;
        }

        if (myProxyAuthCheckBox.isSelected()) {
            return !StringUtil.isEmptyOrSpaces(myProxyLoginTextField.getText())
                    && myProxyPasswordTextField.getPassword().length > 0;
        }
    }
    return true;
}

From source file:org.voltdb.utils.MiscUtils.java

/**
 * @param server String containing a hostname/ip, or a hostname/ip:port.
 * @param defaultPort If a port isn't specified, use this one.
 * @return String in hostname/ip:port format.
 *///from www.  j  av  a 2s.  c  o  m
public static String getHostnameColonPortString(String server, int defaultPort) {
    return HostAndPort.fromString(server).withDefaultPort(defaultPort).toString();
}

From source file:org.apache.accumulo.tserver.replication.AccumuloReplicaSystem.java

/**
 * Perform replication, making a few attempts when an exception is returned.
 *
 * @param p/*from   w  ww .  j  av  a 2s  .co  m*/
 *          Path of WAL to replicate
 * @param status
 *          Current status for the WAL
 * @param target
 *          Where we're replicating to
 * @param helper
 *          A helper for replication
 * @param localConf
 *          The local instance's configuration
 * @param peerContext
 *          The ClientContext to connect to the peer
 * @return The new (or unchanged) Status for the WAL
 */
private Status _replicate(final Path p, final Status status, final ReplicationTarget target,
        final ReplicaSystemHelper helper, final AccumuloConfiguration localConf,
        final ClientContext peerContext, final UserGroupInformation accumuloUgi) {
    try {
        double tracePercent = localConf.getFraction(Property.REPLICATION_TRACE_PERCENT);
        ProbabilitySampler sampler = new ProbabilitySampler(tracePercent);
        Trace.on("AccumuloReplicaSystem", sampler);

        // Remote identifier is an integer (table id) in this case.
        final String remoteTableId = target.getRemoteIdentifier();

        // Attempt the replication of this status a number of times before giving up and
        // trying to replicate it again later some other time.
        int numAttempts = localConf.getCount(Property.REPLICATION_WORK_ATTEMPTS);
        for (int i = 0; i < numAttempts; i++) {
            log.debug("Attempt {}", i);
            String peerTserverStr;
            log.debug("Fetching peer tserver address");
            Span span = Trace.start("Fetch peer tserver");
            try {
                // Ask the master on the remote what TServer we should talk with to replicate the data
                peerTserverStr = ReplicationClient.executeCoordinatorWithReturn(peerContext,
                        new ClientExecReturn<String, ReplicationCoordinator.Client>() {

                            @Override
                            public String execute(ReplicationCoordinator.Client client) throws Exception {
                                return client.getServicerAddress(remoteTableId, peerContext.rpcCreds());
                            }

                        });
            } catch (AccumuloException | AccumuloSecurityException e) {
                // No progress is made
                log.error("Could not connect to master at {}, cannot proceed with replication. Will retry",
                        target, e);
                continue;
            } finally {
                span.stop();
            }

            if (null == peerTserverStr) {
                // Something went wrong, and we didn't get a valid tserver from the remote for some reason
                log.warn(
                        "Did not receive tserver from master at {}, cannot proceed with replication. Will retry.",
                        target);
                continue;
            }

            final HostAndPort peerTserver = HostAndPort.fromString(peerTserverStr);

            // We have a tserver on the remote -- send the data its way.
            Status finalStatus;
            final long sizeLimit = conf.getMemoryInBytes(Property.REPLICATION_MAX_UNIT_SIZE);
            try {
                if (p.getName().endsWith(RFILE_SUFFIX)) {
                    span = Trace.start("RFile replication");
                    try {
                        finalStatus = replicateRFiles(peerContext, peerTserver, target, p, status, sizeLimit,
                                remoteTableId, peerContext.rpcCreds(), helper);
                    } finally {
                        span.stop();
                    }
                } else {
                    span = Trace.start("WAL replication");
                    try {
                        finalStatus = replicateLogs(peerContext, peerTserver, target, p, status, sizeLimit,
                                remoteTableId, peerContext.rpcCreds(), helper, accumuloUgi);
                    } finally {
                        span.stop();
                    }
                }

                log.debug("New status for {} after replicating to {} is {}", p, peerContext.getInstance(),
                        ProtobufUtil.toString(finalStatus));

                return finalStatus;
            } catch (TTransportException | AccumuloException | AccumuloSecurityException e) {
                log.warn("Could not connect to remote server {}, will retry", peerTserverStr, e);
                sleepUninterruptibly(1, TimeUnit.SECONDS);
            }
        }

        log.info("No progress was made after {} attempts to replicate {}, returning so file can be re-queued",
                numAttempts, p);

        // We made no status, punt on it for now, and let it re-queue itself for work
        return status;
    } finally {
        Trace.off();
    }
}

From source file:alluxio.examples.Performance.java

/**
 * Runs the performance test./*from   ww w  . j a v a 2  s . com*/
 *
 * Usage:
 * {@code java -cp <ALLUXIO-VERSION> alluxio.examples.Performance <MasterIp> <FileNamePrefix>
 * <WriteBlockSizeInBytes> <BlocksPerFile> <DebugMode:true/false> <Threads> <FilesPerThread>
 * <TestCaseNumber> <BaseFileNumber>}
 *
 * @param args the arguments for this example
 * @throws Exception if the example fails
 */
public static void main(String[] args) throws Exception {
    if (args.length != 9) {
        System.out.println("java -cp " + RuntimeConstants.ALLUXIO_JAR + " alluxio.examples.Performance "
                + "<MasterIp> <FileNamePrefix> <WriteBlockSizeInBytes> <BlocksPerFile> "
                + "<DebugMode:true/false> <Threads> <FilesPerThread> <TestCaseNumber> " + "<BaseFileNumber>\n"
                + "1: Files Write Test\n" + "2: Files Read Test\n" + "3: RamFile Write Test \n"
                + "4: RamFile Read Test \n" + "5: ByteBuffer Write Test \n" + "6: ByteBuffer Read Test \n");
        System.exit(-1);
    }

    HostAndPort masterAddress = HostAndPort.fromString(args[0]);
    sFileName = args[1];
    sBlockSizeBytes = Integer.parseInt(args[2]);
    sBlocksPerFile = Long.parseLong(args[3]);
    sDebugMode = ("true".equals(args[4]));
    sThreads = Integer.parseInt(args[5]);
    sFiles = Integer.parseInt(args[6]) * sThreads;
    final int testCase = Integer.parseInt(args[7]);
    sBaseFileNumber = Integer.parseInt(args[8]);

    sFileBytes = sBlocksPerFile * sBlockSizeBytes;
    sFilesBytes = sFileBytes * sFiles;

    long fileBufferBytes = Configuration.getBytes(PropertyKey.USER_FILE_BUFFER_BYTES);
    sResultPrefix = String.format(
            "Threads %d FilesPerThread %d TotalFiles %d "
                    + "BLOCK_SIZE_KB %d BLOCKS_PER_FILE %d FILE_SIZE_MB %d "
                    + "Alluxio_WRITE_BUFFER_SIZE_KB %d BaseFileNumber %d : ",
            sThreads, sFiles / sThreads, sFiles, sBlockSizeBytes / 1024, sBlocksPerFile,
            sFileBytes / Constants.MB, fileBufferBytes / 1024, sBaseFileNumber);

    CommonUtils.warmUpLoop();

    Configuration.set(PropertyKey.MASTER_HOSTNAME, masterAddress.getHostText());
    Configuration.set(PropertyKey.MASTER_RPC_PORT, Integer.toString(masterAddress.getPort()));

    if (testCase == 1) {
        sResultPrefix = "AlluxioFilesWriteTest " + sResultPrefix;
        LOG.info(sResultPrefix);
        sFileSystem = FileSystem.Factory.get();
        AlluxioTest(true /* write */);
    } else if (testCase == 2 || testCase == 9) {
        sResultPrefix = "AlluxioFilesReadTest " + sResultPrefix;
        LOG.info(sResultPrefix);
        sFileSystem = FileSystem.Factory.get();
        sAlluxioStreamingRead = (9 == testCase);
        AlluxioTest(false /* read */);
    } else if (testCase == 3) {
        sResultPrefix = "RamFile Write " + sResultPrefix;
        LOG.info(sResultPrefix);
        memoryCopyTest(true, false);
    } else if (testCase == 4) {
        sResultPrefix = "RamFile Read " + sResultPrefix;
        LOG.info(sResultPrefix);
        memoryCopyTest(false, false);
    } else if (testCase == 5) {
        sResultPrefix = "ByteBuffer Write Test " + sResultPrefix;
        LOG.info(sResultPrefix);
        memoryCopyTest(true, true);
    } else if (testCase == 6) {
        sResultPrefix = "ByteBuffer Read Test " + sResultPrefix;
        LOG.info(sResultPrefix);
        memoryCopyTest(false, true);
    } else if (testCase == 7) {
        sResultPrefix = "HdfsFilesWriteTest " + sResultPrefix;
        LOG.info(sResultPrefix);
        HdfsTest(true);
    } else if (testCase == 8) {
        sResultPrefix = "HdfsFilesReadTest " + sResultPrefix;
        LOG.info(sResultPrefix);
        HdfsTest(false);
    } else {
        throw new RuntimeException("No Test Case " + testCase);
    }

    for (int k = 0; k < RESULT_ARRAY_SIZE; k++) {
        System.out.print(sResults[k] + " ");
    }
    System.out.println();
    System.exit(0);
}

From source file:org.apache.accumulo.test.performance.scan.CollectTabletStats.java

private static List<KeyExtent> findTablets(ClientContext context, boolean selectLocalTablets, String tableName,
        SortedMap<KeyExtent, String> tabletLocations) throws Exception {

    String tableId = Tables.getNameToIdMap(context.getInstance()).get(tableName);
    MetadataServicer.forTableId(context, tableId).getTabletLocations(tabletLocations);

    InetAddress localaddress = InetAddress.getLocalHost();

    List<KeyExtent> candidates = new ArrayList<KeyExtent>();

    for (Entry<KeyExtent, String> entry : tabletLocations.entrySet()) {
        String loc = entry.getValue();
        if (loc != null) {
            boolean isLocal = HostAndPort.fromString(entry.getValue()).getHostText()
                    .equals(localaddress.getHostName());

            if (selectLocalTablets && isLocal) {
                candidates.add(entry.getKey());
            } else if (!selectLocalTablets && !isLocal) {
                candidates.add(entry.getKey());
            }//  w w  w.  ja va2 s .c  om
        }
    }
    return candidates;
}

From source file:org.apache.hadoop.hive.metastore.tools.Util.java

/**
 * Get server URI.<p>//from  www  .ja  v a2s  . c  o m
 * HMS host is obtained from
 * <ol>
 * <li>Argument</li>
 * <li>HMS_HOST environment parameter</li>
 * <li>hms.host Java property</li>
 * <li>use 'localhost' if above fails</li>
 * </ol>
 * HMS Port is obtained from
 * <ol>
 * <li>Argument</li>
 * <li>host:port string</li>
 * <li>HMS_PORT environment variable</li>
 * <li>hms.port Java property</li>
 * <li>default port value</li>
 * </ol>
 *
 * @param host       HMS host string.
 * @param portString HMS port
 * @return HMS URI
 * @throws URISyntaxException if URI is is invalid
 */
public static @Nullable URI getServerUri(@Nullable String host, @Nullable String portString)
        throws URISyntaxException {
    if (host == null) {
        host = System.getenv(ENV_SERVER);
    }
    if (host == null) {
        host = System.getProperty(PROP_HOST);
    }
    if (host == null) {
        host = DEFAULT_HOST;
    }
    host = host.trim();

    if ((portString == null || portString.isEmpty() || portString.equals("0")) && !host.contains(":")) {
        portString = System.getenv(ENV_PORT);
        if (portString == null) {
            portString = System.getProperty(PROP_PORT);
        }
    }
    Integer port = Constants.HMS_DEFAULT_PORT;
    if (portString != null) {
        port = Integer.parseInt(portString);
    }

    HostAndPort hp = HostAndPort.fromString(host).withDefaultPort(port);

    LOG.info("Connecting to {}:{}", hp.getHostText(), hp.getPort());

    return new URI(THRIFT_SCHEMA, null, hp.getHostText(), hp.getPort(), null, null, null);
}

From source file:org.apache.brooklyn.core.network.AbstractOnNetworkEnricher.java

protected Maybe<String> transformHostAndPort(Entity source, MachineLocation machine, String sensorVal) {
    HostAndPort hostAndPort = HostAndPort.fromString(sensorVal);
    if (hostAndPort.hasPort()) {
        int port = hostAndPort.getPort();
        Optional<HostAndPort> mappedEndpoint = getMappedEndpoint(source, machine, port);
        if (!mappedEndpoint.isPresent()) {
            LOG.debug(/*from ww  w  .j  ava  2 s.  c om*/
                    "network-facing enricher not transforming {} host-and-port {}, because no port-mapping for {}",
                    new Object[] { source, sensorVal, machine });
            return Maybe.absent();
        }
        if (!mappedEndpoint.get().hasPort()) {
            LOG.debug(
                    "network-facing enricher not transforming {} host-and-port {}, because no port in target {} for {}",
                    new Object[] { source, sensorVal, mappedEndpoint, machine });
            return Maybe.absent();
        }
        return Maybe.of(mappedEndpoint.get().toString());
    } else {
        LOG.debug("network-facing enricher not transforming {} host-and-port {} because defines no port",
                source, hostAndPort);
        return Maybe.absent();
    }
}

From source file:org.apache.hoya.providers.accumulo.AccumuloProviderService.java

@Override
public TreeMap<String, URL> buildMonitorDetails(ClusterDescription clusterDesc) {
    TreeMap<String, URL> map = new TreeMap<String, URL>();

    map.put("Active Accumulo Master (RPC): " + getInfoAvoidingNull(clusterDesc, AccumuloKeys.MASTER_ADDRESS),
            null);/*from w ww  .  ja va  2  s . co  m*/

    String monitorKey = "Active Accumulo Monitor: ";
    String monitorAddr = getInfoAvoidingNull(clusterDesc, AccumuloKeys.MONITOR_ADDRESS);
    if (!StringUtils.isBlank(monitorAddr)) {
        try {
            HostAndPort hostPort = HostAndPort.fromString(monitorAddr);
            map.put(monitorKey, new URL("http", hostPort.getHostText(), hostPort.getPort(), ""));
        } catch (Exception e) {
            log.debug("Caught exception parsing Accumulo monitor URL", e);
            map.put(monitorKey + "N/A", null);
        }
    } else {
        map.put(monitorKey + "N/A", null);
    }

    return map;
}

From source file:clocker.docker.entity.util.DockerUtils.java

public static Map<String, Object> generateLinks(Entity source, Entity target, String alias) {
    Entities.waitForServiceUp(target);/*from w  ww. jav a  2 s. co  m*/
    String from = DockerUtils.getContainerName(source);
    String to = DockerUtils.getContainerName(target);
    boolean local = source.sensors().get(SoftwareProcess.PROVISIONING_LOCATION)
            .equals(target.sensors().get(SoftwareProcess.PROVISIONING_LOCATION));
    List networks = target.sensors().get(SdnAttributes.ATTACHED_NETWORKS);
    if (Strings.isNonBlank(to)) {
        // Copy explicitly defined environment variables from target to source
        Map<String, Object> env = MutableMap.of();
        MutableMap<String, Object> targetEnvironment = MutableMap
                .copyOf(target.getConfig(DockerContainer.DOCKER_CONTAINER_ENVIRONMENT.getConfigKey()));
        if (target.getConfig(DockerContainer.DOCKER_CONTAINER_ENVIRONMENT.getConfigKey()) != null) {
            for (Map.Entry<String, Object> envvar : targetEnvironment.entrySet()) {
                env.put(String.format("%S_ENV_%S", alias, envvar.getKey()), envvar.getValue().toString());
            }
        }

        String address = DockerUtils.getTargetAddress(source, target);
        Map<Integer, Integer> ports = MutableMap.of();
        Set<Integer> containerPorts = MutableSet
                .copyOf(target.sensors().get(DockerAttributes.DOCKER_CONTAINER_OPEN_PORTS));
        if (containerPorts.size() > 0) {
            for (Integer port : containerPorts) {
                AttributeSensor<String> sensor = Sensors
                        .newStringSensor(String.format("mapped.docker.port.%d", port));
                String hostAndPort = target.sensors().get(sensor);
                if ((local && (networks != null && networks.size() > 0)) || hostAndPort == null) {
                    ports.put(port, port);
                } else {
                    ports.put(HostAndPort.fromString(hostAndPort).getPort(), port);
                }
            }
        } else {
            ports = ImmutableMap.copyOf(DockerUtils.getMappedPorts(target));
        }
        for (Integer port : ports.keySet()) {
            Integer containerPort = ports.get(port);
            env.put(String.format("%S_NAME", alias), String.format("/%s/%s", from, alias));
            env.put(String.format("%S_PORT", alias), String.format("tcp://%s:%d", address, port));
            env.put(String.format("%S_PORT_%d_TCP", alias, containerPort),
                    String.format("tcp://%s:%d", address, port));
            env.put(String.format("%S_PORT_%d_TCP_ADDR", alias, containerPort), address);
            env.put(String.format("%S_PORT_%d_TCP_PORT", alias, containerPort), port);
            env.put(String.format("%S_PORT_%d_TCP_PROTO", alias, containerPort), "tcp");
        }
        LOG.debug("Links for {}: {}", to, Joiner.on(" ").withKeyValueSeparator("=").join(env));
        return env;
    } else {
        LOG.warn("Cannot generate links for {}: no name specified", target);
        return ImmutableMap.<String, Object>of();
    }
}