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

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

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Rebuild the host:port string, including brackets if necessary.

Usage

From source file:com.pingcap.tikv.PDClient.java

private void updateLeader(GetMembersResponse resp) {
    String leaderUrlStr = "URL Not Set";
    try {/*from  www  .ja v  a 2  s.com*/
        long ts = System.nanoTime();
        synchronized (this) {
            // Lock for not flooding during pd error
            if (leaderWrapper != null && leaderWrapper.getCreateTime() > ts)
                return;

            if (resp == null) {
                resp = getMembers();
                if (resp == null)
                    return;
            }
            Member leader = resp.getLeader();
            List<String> leaderUrls = leader.getClientUrlsList();
            if (leaderUrls.isEmpty())
                return;
            leaderUrlStr = leaderUrls.get(0);
            // TODO: Why not strip protocol info on server side since grpc does not need it
            URL tURL = new URL(leaderUrlStr);
            HostAndPort newLeader = HostAndPort.fromParts(tURL.getHost(), tURL.getPort());
            if (leaderWrapper != null && newLeader.equals(leaderWrapper.getLeaderInfo())) {
                return;
            }

            // switch leader
            ManagedChannel clientChannel = getManagedChannel(newLeader);
            leaderWrapper = new LeaderWrapper(newLeader, PDGrpc.newBlockingStub(clientChannel),
                    PDGrpc.newStub(clientChannel), clientChannel, System.nanoTime());
            logger.info("Switched to new leader: %s", newLeader.toString());
        }
    } catch (MalformedURLException e) {
        logger.error("Client URL is not valid: %s", leaderUrlStr, e);
    } catch (Exception e) {
        logger.error("Error updating leader.", e);
    }
}

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   w ww  .j ava  2  s . co  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:clocker.docker.entity.DockerHostSshDriver.java

@Override
public void customize() {
    if (isRunning()) {
        log.info("Stopping running Docker instance at {} before customising", getMachine());
        stop();/*ww w  .  j  av  a2  s. c o  m*/
    }

    // Determine OS
    String os = getMachine().getMachineDetails().getOsDetails().getName();
    boolean centos = "centos".equalsIgnoreCase(os);
    boolean ubuntu = "ubuntu".equalsIgnoreCase(os);

    if (entity.config().get(DockerInfrastructure.DOCKER_GENERATE_TLS_CERTIFICATES)) {
        newScript(ImmutableMap.of(NON_STANDARD_LAYOUT, "true"), CUSTOMIZING).body
                .append(format("cp ca-cert.pem %s/ca.pem", getRunDir()),
                        format("cp server-cert.pem %s/cert.pem", getRunDir()),
                        format("cp server-key.pem %s/key.pem", getRunDir()))
                .failOnNonZeroResultCode().execute();
    }

    // Add the CA cert as an authorised docker CA for the first host.
    // This will be used for docker registry etc.
    String firstHost = entity.sensors().get(AbstractGroup.FIRST).sensors().get(Attributes.HOSTNAME);
    String certsPath = "/etc/docker/certs.d/" + firstHost + ":"
            + entity.config().get(DockerInfrastructure.DOCKER_REGISTRY_PORT);

    newScript(CUSTOMIZING).body
            .append(chainGroup(sudo("mkdir -p " + certsPath), sudo("cp ca.pem " + certsPath + "/ca.crt")))
            .failOnNonZeroResultCode().execute();

    // Docker daemon startup arguments
    EtcdNode etcdNode = getEntity().sensors().get(DockerHost.ETCD_NODE);
    HostAndPort etcdAuthority = HostAndPort.fromParts(etcdNode.sensors().get(Attributes.SUBNET_ADDRESS),
            etcdNode.sensors().get(EtcdNode.ETCD_CLIENT_PORT));

    List<String> args = MutableList.of(centos ? "--selinux-enabled" : null, "--userland-proxy=false",
            format("-H tcp://0.0.0.0:%d", getDockerPort()), "-H unix:///var/run/docker.sock",
            format("--cluster-store=etcd://%s", etcdAuthority.toString()),
            format("--cluster-advertise=%s:%d", getEntity().sensors().get(Attributes.SUBNET_ADDRESS),
                    getDockerPort()),
            getStorageOpts(), getDockerRegistryOpts(), "--tlsverify", "--tls",
            format("--tlscert=%s/cert.pem", getRunDir()), format("--tlskey=%s/key.pem", getRunDir()),
            format("--tlscacert=%s/ca.pem", getRunDir()));
    String argv = Joiner.on(" ").skipNulls().join(args);
    log.debug("Docker daemon args: {}", argv);

    // Upstart
    if (ubuntu) {
        newScript(CUSTOMIZING + "-upstart").body.append(
                chain(sudo("mkdir -p /etc/default"),
                        format("echo 'DOCKER_OPTS=\"%s\"' | ", argv) + sudo("tee -a /etc/default/docker")),
                sudo("groupadd -f docker"), sudo(format("gpasswd -a %s docker", getMachine().getUser())),
                sudo("newgrp docker")).failOnNonZeroResultCode().execute();
    }

    // CentOS
    if (centos) {
        newScript(CUSTOMIZING + "-sysconfig").body
                .append(chain(sudo("mkdir -p /etc/sysconfig"),
                        format("echo 'other_args=\"%s\"' | ", argv) + sudo("tee -a /etc/sysconfig/docker")))
                .failOnNonZeroResultCode().execute();
    }

    // SystemD
    boolean dockerTen = VersionComparator.getInstance().compare(getVersion(), "1.10") >= 0;
    String service = Os.mergePaths(getInstallDir(), "docker.service");
    copyTemplate("classpath://clocker/docker/entity/docker.service", service, true,
            ImmutableMap.of("args", argv, "daemon", dockerTen ? "daemon" : "-d"));
    newScript(CUSTOMIZING + "-systemd").body
            .append(chain(sudo("mkdir -p /etc/systemd/system"),
                    sudo(format("cp %s %s", service, "/etc/systemd/system/docker.service")),
                    ifExecutableElse0("systemctl", sudo("systemctl daemon-reload"))))
            .failOnNonZeroResultCode().execute();

    // Configure volume mappings for the host
    Map<String, String> mapping = MutableMap.of();
    Map<String, String> volumes = getEntity().config().get(DockerHost.DOCKER_HOST_VOLUME_MAPPING);
    if (volumes != null) {
        for (String source : volumes.keySet()) {
            if (Urls.isUrlWithProtocol(source)) {
                String path = deployArchive(source);
                mapping.put(path, volumes.get(source));
            } else {
                mapping.put(source, volumes.get(source));
            }
        }
    }
    getEntity().sensors().set(DockerHost.DOCKER_HOST_VOLUME_MAPPING, mapping);
}

From source file:org.apache.accumulo.core.client.impl.ConditionalWriterImpl.java

private void queueException(HostAndPort location, Map<Long, CMK> cmidToCm, Exception e) {
    for (CMK cmk : cmidToCm.values())
        cmk.cm.queueResult(new Result(e, cmk.cm, location.toString()));
}

From source file:brooklyn.entity.nosql.couchbase.CouchbaseClusterImpl.java

protected void addServer(Entity serverToAdd) {
    Preconditions.checkNotNull(serverToAdd);
    if (serverToAdd.equals(getPrimaryNode())) {
        // no need to add; but we pass it in anyway because it makes the calling logic easier
        return;//from ww w  .j a  v a2 s .c o  m
    }
    if (!isMemberInCluster(serverToAdd)) {
        HostAndPort webAdmin = HostAndPort.fromParts(serverToAdd.getAttribute(SoftwareProcess.SUBNET_HOSTNAME),
                serverToAdd.getAttribute(CouchbaseNode.COUCHBASE_WEB_ADMIN_PORT));
        String username = serverToAdd.getConfig(CouchbaseNode.COUCHBASE_ADMIN_USERNAME);
        String password = serverToAdd.getConfig(CouchbaseNode.COUCHBASE_ADMIN_PASSWORD);

        if (isClusterInitialized()) {
            Entities.invokeEffectorWithArgs(this, getPrimaryNode(), CouchbaseNode.SERVER_ADD_AND_REBALANCE,
                    webAdmin.toString(), username, password).getUnchecked();
        } else {
            Entities.invokeEffectorWithArgs(this, getPrimaryNode(), CouchbaseNode.SERVER_ADD,
                    webAdmin.toString(), username, password).getUnchecked();
        }
        //FIXME check feedback of whether the server was added.
        ((EntityInternal) serverToAdd).setAttribute(CouchbaseNode.IS_IN_CLUSTER, true);
    }
}

From source file:org.apache.brooklyn.entity.nosql.couchbase.CouchbaseClusterImpl.java

protected void addServer(Entity serverToAdd) {
    Preconditions.checkNotNull(serverToAdd);
    if (serverToAdd.equals(getPrimaryNode())) {
        // no need to add; but we pass it in anyway because it makes the calling logic easier
        return;// ww  w . j  a  va 2 s  . c om
    }
    if (!isMemberInCluster(serverToAdd)) {
        HostAndPort webAdmin = HostAndPort.fromParts(serverToAdd.getAttribute(SoftwareProcess.SUBNET_HOSTNAME),
                serverToAdd.getAttribute(CouchbaseNode.COUCHBASE_WEB_ADMIN_PORT));
        String username = serverToAdd.getConfig(CouchbaseNode.COUCHBASE_ADMIN_USERNAME);
        String password = serverToAdd.getConfig(CouchbaseNode.COUCHBASE_ADMIN_PASSWORD);

        if (isClusterInitialized()) {
            Entities.invokeEffectorWithArgs(this, getPrimaryNode(), CouchbaseNode.SERVER_ADD_AND_REBALANCE,
                    webAdmin.toString(), username, password).getUnchecked();
        } else {
            Entities.invokeEffectorWithArgs(this, getPrimaryNode(), CouchbaseNode.SERVER_ADD,
                    webAdmin.toString(), username, password).getUnchecked();
        }
        //FIXME check feedback of whether the server was added.
        ((EntityInternal) serverToAdd).sensors().set(CouchbaseNode.IS_IN_CLUSTER, true);
    }
}

From source file:org.apache.accumulo.core.client.impl.ConditionalWriterImpl.java

private void invalidateSession(HostAndPort location, TabletServerMutations<QCMutation> mutations,
        Map<Long, CMK> cmidToCm, SessionID sessionId) {
    if (sessionId == null) {
        queueRetry(cmidToCm, location);//from   w  w  w. j a va2  s.c  o m
    } else {
        try {
            invalidateSession(sessionId, location);
            for (CMK cmk : cmidToCm.values())
                cmk.cm.queueResult(new Result(Status.UNKNOWN, cmk.cm, location.toString()));
        } catch (Exception e2) {
            queueException(location, cmidToCm, e2);
        }
    }
}

From source file:org.apache.accumulo.core.client.impl.ConditionalWriterImpl.java

private void queueRetry(List<QCMutation> mutations, HostAndPort server) {

    if (timeout < Long.MAX_VALUE) {

        long time = System.currentTimeMillis();

        ArrayList<QCMutation> mutations2 = new ArrayList<ConditionalWriterImpl.QCMutation>(mutations.size());

        for (QCMutation qcm : mutations) {
            qcm.resetDelay();//from www . j  av  a  2s  .c o  m
            if (time + qcm.getDelay(TimeUnit.MILLISECONDS) > qcm.entryTime + timeout) {
                TimedOutException toe;
                if (server != null)
                    toe = new TimedOutException(Collections.singleton(server.toString()));
                else
                    toe = new TimedOutException("Conditional mutation timed out");

                qcm.queueResult(new Result(toe, qcm, (null == server ? null : server.toString())));
            } else {
                mutations2.add(qcm);
            }
        }

        if (mutations2.size() > 0)
            failedMutations.addAll(mutations2);

    } else {
        for (QCMutation qcm : mutations)
            qcm.resetDelay();
        failedMutations.addAll(mutations);
    }
}

From source file:ezbake.thrift.ThriftClientPool.java

private TProtocol getProtocol(String applicationName, String serviceName, int attempt) throws Exception {
    int endpointCount;
    StringBuilder exceptionList = new StringBuilder();
    synchronized (serviceMap) {

        Collection<HostAndPort> endPoints = serviceMap.get(serviceName);
        List<HostAndPort> list = Lists.newArrayList(endPoints);
        if (endPoints.size() > 1) {
            Collections.shuffle(list); // distributes load on endpoints
        }//  w ww  .  j  av a 2  s . co m
        endpointCount = endPoints.size();
        for (HostAndPort hostAndPort : list) {
            try {
                final String securityId;
                if (applicationName != null) {
                    //Getting another app's security id
                    securityId = getSecurityId(applicationName);
                } else if (commonServices.contains(serviceName)) { //isCommonService reconnects to zookeeper, don't need that here
                    //Getting a common service's security id
                    securityId = getSecurityId(serviceName);
                } else {
                    //Use your own app's security id
                    logger.debug("could not find security id for {}. using {}", serviceName,
                            applicationSecurityId);
                    securityId = applicationSecurityId;
                }
                return ThriftUtils.getProtocol(hostAndPort, securityId, configuration);
            } catch (Exception ex) {
                exceptionList.append("\nHost: ");
                exceptionList.append(hostAndPort.toString());
                exceptionList.append(" Exception: ");
                exceptionList.append(ex.getMessage());
                logger.warn("Failed to connect to host(" + hostAndPort.toString() + ") Trying next...", ex);
            }
        }
    }
    if (attempt == 1) {
        ServiceDiscovery serviceDiscoveryClient = getServiceDiscoveryClient();
        RefreshEndpoints(serviceDiscoveryClient);
        RefreshCommonEndpoints(serviceDiscoveryClient);
        closeClient(serviceDiscoveryClient);
        return getProtocol(applicationName, serviceName, 2);
    }
    throw new RuntimeException("Could not connect to service " + serviceName + " (found " + endpointCount
            + " endpoints)" + exceptionList.toString());
}

From source file:org.apache.accumulo.server.gc.SimpleGarbageCollector.java

private void getZooLock(HostAndPort addr) throws KeeperException, InterruptedException {
    String path = ZooUtil.getRoot(HdfsZooInstance.getInstance()) + Constants.ZGC_LOCK;

    LockWatcher lockWatcher = new LockWatcher() {
        @Override/*from  w  ww.  j  a  v  a2 s.c om*/
        public void lostLock(LockLossReason reason) {
            Halt.halt("GC lock in zookeeper lost (reason = " + reason + "), exiting!");
        }

        @Override
        public void unableToMonitorLockNode(final Throwable e) {
            Halt.halt(-1, new Runnable() {

                @Override
                public void run() {
                    log.fatal("No longer able to monitor lock node ", e);
                }
            });

        }
    };

    while (true) {
        lock = new ZooLock(path);
        if (lock.tryLock(lockWatcher,
                new ServerServices(addr.toString(), Service.GC_CLIENT).toString().getBytes())) {
            break;
        }
        UtilWaitThread.sleep(1000);
    }
}