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

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

Introduction

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

Prototype

@Override
    public boolean equals(@Nullable Object other) 

Source Link

Usage

From source file:org.apache.brooklyn.demo.CumulusRDFApplication.java

/**
 * Create the application entities://from w w w.  ja v a  2 s. com
 * <ul>
 * <li>A {@link CassandraFabric} of {@link CassandraDatacenter}s containing {@link CassandraNode}s
 * <li>A {@link TomcatServer}
 * </ul>
 */
@Override
public void initApp() {
    // Cassandra cluster
    EntitySpec<CassandraDatacenter> clusterSpec = EntitySpec.create(CassandraDatacenter.class)
            .configure(CassandraDatacenter.MEMBER_SPEC, EntitySpec.create(CassandraNode.class)
                    //FIXME can probably use JMXMP_AND_RMI now, to deploy to GCE and elsewhere
                    .configure(UsesJmx.JMX_AGENT_MODE, UsesJmx.JmxAgentModes.JMX_RMI_CUSTOM_AGENT)
                    .configure(UsesJmx.JMX_PORT, PortRanges.fromString("11099+"))
                    .configure(UsesJmx.RMI_REGISTRY_PORT, PortRanges.fromString("9001+"))
                    .configure(CassandraNode.THRIFT_PORT,
                            PortRanges.fromInteger(getConfig(CASSANDRA_THRIFT_PORT)))
                    .enricher(EnricherSpec.create(ServiceFailureDetector.class))
                    .policy(PolicySpec.create(ServiceRestarter.class).configure(
                            ServiceRestarter.FAILURE_SENSOR_TO_MONITOR, ServiceFailureDetector.ENTITY_FAILED)))
            .policy(PolicySpec.create(ServiceReplacer.class).configure(
                    ServiceReplacer.FAILURE_SENSOR_TO_MONITOR, ServiceRestarter.ENTITY_RESTART_FAILED));

    if (getConfig(MULTI_REGION_FABRIC)) {
        cassandra = addChild(EntitySpec.create(CassandraFabric.class)
                .configure(CassandraDatacenter.CLUSTER_NAME, "Brooklyn")
                .configure(CassandraDatacenter.INITIAL_SIZE, getConfig(CASSANDRA_CLUSTER_SIZE)) // per location
                .configure(CassandraDatacenter.ENDPOINT_SNITCH_NAME,
                        "org.apache.brooklyn.entity.nosql.cassandra.customsnitch.MultiCloudSnitch")
                .configure(CassandraNode.CUSTOM_SNITCH_JAR_URL,
                        "classpath://org/apache/brooklyn/entity/nosql/cassandra/cassandra-multicloud-snitch.jar")
                .configure(CassandraFabric.MEMBER_SPEC, clusterSpec));
    } else {
        cassandra = addChild(
                EntitySpec.create(clusterSpec).configure(CassandraDatacenter.CLUSTER_NAME, "Brooklyn")
                        .configure(CassandraDatacenter.INITIAL_SIZE, getConfig(CASSANDRA_CLUSTER_SIZE)));
    }

    // Tomcat web-app server
    webapp = addChild(EntitySpec.create(TomcatServer.class)
            .configure(UsesJmx.JMX_AGENT_MODE, UsesJmx.JmxAgentModes.JMX_RMI_CUSTOM_AGENT)
            .configure(UsesJmx.JMX_PORT, PortRanges.fromString("11099+"))
            .configure(UsesJmx.RMI_REGISTRY_PORT, PortRanges.fromString("9001+"))
            .configure(JavaWebAppService.ROOT_WAR,
                    "https://cumulusrdf.googlecode.com/svn/wiki/downloads/cumulusrdf-1.0.1.war")
            .configure(UsesJava.JAVA_SYSPROPS, MutableMap.of("cumulusrdf.config-file", "/tmp/cumulus.yaml")));

    // Add an effector to tomcat to reconfigure with a new YAML config file
    ((EntityInternal) webapp).getMutableEntityType().addEffector(cumulusConfig, new EffectorBody<Void>() {
        @Override
        public Void call(ConfigBag parameters) {
            // Process the YAML template given in the application config
            String url = Entities.getRequiredUrlConfig(CumulusRDFApplication.this, CUMULUS_RDF_CONFIG_URL);
            Map<String, Object> config;
            synchronized (endpointMutex) {
                config = MutableMap.<String, Object>of("cassandraHostname", endpoint.getHostText(),
                        "cassandraThriftPort", endpoint.getPort());
            }
            String contents = TemplateProcessor.processTemplateContents(
                    ResourceUtils.create(CumulusRDFApplication.this).getResourceAsString(url), config);
            // Copy the file contents to the remote machine
            return DynamicTasks.queue(SshEffectorTasks.put("/tmp/cumulus.yaml").contents(contents)).get();
        }
    });

    // Listen for HOSTNAME changes from the Cassandra fabric to show at least one node is available
    subscriptions().subscribe(cassandra, CassandraDatacenter.HOSTNAME, new SensorEventListener<String>() {
        @Override
        public void onEvent(SensorEvent<String> event) {
            if (Strings.isNonBlank(event.getValue())) {
                synchronized (endpointMutex) {
                    String hostname = Entities
                            .submit(CumulusRDFApplication.this, DependentConfiguration
                                    .attributeWhenReady(cassandra, CassandraDatacenter.HOSTNAME))
                            .getUnchecked();
                    Integer thriftPort = Entities
                            .submit(CumulusRDFApplication.this, DependentConfiguration
                                    .attributeWhenReady(cassandra, CassandraDatacenter.THRIFT_PORT))
                            .getUnchecked();
                    HostAndPort current = HostAndPort.fromParts(hostname, thriftPort);

                    // Check if the cluster access point has changed
                    if (!current.equals(endpoint)) {
                        log.info("Setting cluster endpoint to {}", current.toString());
                        endpoint = current;

                        // Reconfigure the CumulusRDF application and restart tomcat if necessary
                        webapp.invoke(cumulusConfig, MutableMap.<String, Object>of());
                        if (webapp.getAttribute(Startable.SERVICE_UP)) {
                            webapp.restart();
                        }
                    }
                }
            }
        }
    });
}

From source file:brooklyn.demo.CumulusRDFApplication.java

/**
 * Create the application entities://from  w w w . j  a  va 2s. c  o m
 * <ul>
 * <li>A {@link CassandraFabric} of {@link CassandraDatacenter}s containing {@link CassandraNode}s
 * <li>A {@link TomcatServer}
 * </ul>
 */
@Override
public void initApp() {
    // Cassandra cluster
    EntitySpec<CassandraDatacenter> clusterSpec = EntitySpec.create(CassandraDatacenter.class)
            .configure(CassandraDatacenter.MEMBER_SPEC, EntitySpec.create(CassandraNode.class)
                    //FIXME can probably use JMXMP_AND_RMI now, to deploy to GCE and elsewhere
                    .configure(UsesJmx.JMX_AGENT_MODE, UsesJmx.JmxAgentModes.JMX_RMI_CUSTOM_AGENT)
                    .configure(UsesJmx.JMX_PORT, PortRanges.fromString("11099+"))
                    .configure(UsesJmx.RMI_REGISTRY_PORT, PortRanges.fromString("9001+"))
                    .configure(CassandraNode.THRIFT_PORT,
                            PortRanges.fromInteger(getConfig(CASSANDRA_THRIFT_PORT)))
                    .enricher(EnricherSpec.create(ServiceFailureDetector.class))
                    .policy(PolicySpec.create(ServiceRestarter.class).configure(
                            ServiceRestarter.FAILURE_SENSOR_TO_MONITOR, ServiceFailureDetector.ENTITY_FAILED)))
            .policy(PolicySpec.create(ServiceReplacer.class).configure(
                    ServiceReplacer.FAILURE_SENSOR_TO_MONITOR, ServiceRestarter.ENTITY_RESTART_FAILED));

    if (getConfig(MULTI_REGION_FABRIC)) {
        cassandra = addChild(
                EntitySpec.create(CassandraFabric.class).configure(CassandraDatacenter.CLUSTER_NAME, "Brooklyn")
                        .configure(CassandraDatacenter.INITIAL_SIZE, getConfig(CASSANDRA_CLUSTER_SIZE)) // per location
                        .configure(CassandraDatacenter.ENDPOINT_SNITCH_NAME,
                                "brooklyn.entity.nosql.cassandra.customsnitch.MultiCloudSnitch")
                        .configure(CassandraNode.CUSTOM_SNITCH_JAR_URL,
                                "classpath://brooklyn/entity/nosql/cassandra/cassandra-multicloud-snitch.jar")
                        .configure(CassandraFabric.MEMBER_SPEC, clusterSpec));
    } else {
        cassandra = addChild(
                EntitySpec.create(clusterSpec).configure(CassandraDatacenter.CLUSTER_NAME, "Brooklyn")
                        .configure(CassandraDatacenter.INITIAL_SIZE, getConfig(CASSANDRA_CLUSTER_SIZE)));
    }

    // Tomcat web-app server
    webapp = addChild(EntitySpec.create(TomcatServer.class)
            .configure(UsesJmx.JMX_AGENT_MODE, UsesJmx.JmxAgentModes.JMX_RMI_CUSTOM_AGENT)
            .configure(UsesJmx.JMX_PORT, PortRanges.fromString("11099+"))
            .configure(UsesJmx.RMI_REGISTRY_PORT, PortRanges.fromString("9001+"))
            .configure(JavaWebAppService.ROOT_WAR,
                    "https://cumulusrdf.googlecode.com/svn/wiki/downloads/cumulusrdf-1.0.1.war")
            .configure(UsesJava.JAVA_SYSPROPS, MutableMap.of("cumulusrdf.config-file", "/tmp/cumulus.yaml")));

    // Add an effector to tomcat to reconfigure with a new YAML config file
    ((EntityInternal) webapp).getMutableEntityType().addEffector(cumulusConfig, new EffectorBody<Void>() {
        @Override
        public Void call(ConfigBag parameters) {
            // Process the YAML template given in the application config
            String url = Entities.getRequiredUrlConfig(CumulusRDFApplication.this, CUMULUS_RDF_CONFIG_URL);
            Map<String, Object> config;
            synchronized (endpointMutex) {
                config = MutableMap.<String, Object>of("cassandraHostname", endpoint.getHostText(),
                        "cassandraThriftPort", endpoint.getPort());
            }
            String contents = TemplateProcessor.processTemplateContents(
                    ResourceUtils.create(CumulusRDFApplication.this).getResourceAsString(url), config);
            // Copy the file contents to the remote machine
            return DynamicTasks.queue(SshEffectorTasks.put("/tmp/cumulus.yaml").contents(contents)).get();
        }
    });

    // Listen for HOSTNAME changes from the Cassandra fabric to show at least one node is available
    subscribe(cassandra, CassandraDatacenter.HOSTNAME, new SensorEventListener<String>() {
        @Override
        public void onEvent(SensorEvent<String> event) {
            if (Strings.isNonBlank(event.getValue())) {
                synchronized (endpointMutex) {
                    String hostname = Entities
                            .submit(CumulusRDFApplication.this, DependentConfiguration
                                    .attributeWhenReady(cassandra, CassandraDatacenter.HOSTNAME))
                            .getUnchecked();
                    Integer thriftPort = Entities
                            .submit(CumulusRDFApplication.this, DependentConfiguration
                                    .attributeWhenReady(cassandra, CassandraDatacenter.THRIFT_PORT))
                            .getUnchecked();
                    HostAndPort current = HostAndPort.fromParts(hostname, thriftPort);

                    // Check if the cluster access point has changed
                    if (!current.equals(endpoint)) {
                        log.info("Setting cluster endpoint to {}", current.toString());
                        endpoint = current;

                        // Reconfigure the CumulusRDF application and restart tomcat if necessary
                        webapp.invoke(cumulusConfig, MutableMap.<String, Object>of());
                        if (webapp.getAttribute(Startable.SERVICE_UP)) {
                            webapp.restart();
                        }
                    }
                }
            }
        }
    });
}

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

private void updateLeader(GetMembersResponse resp) {
    String leaderUrlStr = "URL Not Set";
    try {// w w  w  . jav a 2s. co  m
        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:org.hillview.dataset.RemoteDataSet.java

/**
 * Zip operation on two IDataSet objects that need to reside on the same remote server.
 *//*w ww.j  av  a 2 s  .  c  o  m*/
@Override
public <S> Observable<PartialResult<IDataSet<Pair<T, S>>>> zip(final IDataSet<S> other) {
    if (!(other instanceof RemoteDataSet<?>)) {
        throw new RuntimeException("Unexpected type in Zip " + other);
    }
    final RemoteDataSet<S> rds = (RemoteDataSet<S>) other;

    // zip commands are not valid if the RemoteDataSet instances point to different
    // actor systems or different nodes.
    final HostAndPort leftAddress = this.serverEndpoint;
    final HostAndPort rightAddress = rds.serverEndpoint;
    if (!leftAddress.equals(rightAddress)) {
        throw new RuntimeException("Zip command invalid for RemoteDataSets "
                + "across different servers | left: " + leftAddress + ", right:" + rightAddress);
    }

    final ZipOperation zip = new ZipOperation(rds.remoteHandle);
    final byte[] serializedOp = SerializationUtils.serialize(zip);
    final UUID operationId = UUID.randomUUID();
    final Command command = Command.newBuilder().setIdsIndex(this.remoteHandle)
            .setSerializedOp(ByteString.copyFrom(serializedOp)).setHighId(operationId.getMostSignificantBits())
            .setLowId(operationId.getLeastSignificantBits()).build();
    final SerializedSubject<PartialResult<IDataSet<Pair<T, S>>>, PartialResult<IDataSet<Pair<T, S>>>> subj = createSerializedSubject();
    final StreamObserver<PartialResponse> responseObserver = new NewDataSetObserver<Pair<T, S>>(subj);
    return subj.unsubscribeOn(ExecutorUtils.getUnsubscribeScheduler()).doOnSubscribe(
            () -> this.stub.withDeadlineAfter(TIMEOUT, TimeUnit.MILLISECONDS).zip(command, responseObserver))
            .doOnUnsubscribe(() -> this.unsubscribe(operationId));
}