Example usage for org.apache.cassandra.config DatabaseDescriptor getEndpointSnitch

List of usage examples for org.apache.cassandra.config DatabaseDescriptor getEndpointSnitch

Introduction

In this page you can find the example usage for org.apache.cassandra.config DatabaseDescriptor getEndpointSnitch.

Prototype

public static IEndpointSnitch getEndpointSnitch() 

Source Link

Usage

From source file:com.datastax.brisk.BriskServer.java

License:Apache License

private List<String> getKeyLocations(ByteBuffer key) {
    List<InetAddress> endpoints = StorageService.instance.getLiveNaturalEndpoints(cfsKeyspace, key);
    DatabaseDescriptor.getEndpointSnitch().sortByProximity(FBUtilities.getLocalAddress(), endpoints);

    List<String> hosts = new ArrayList<String>(endpoints.size());

    for (InetAddress endpoint : endpoints) {
        hosts.add(endpoint.getHostName());
    }/*from w ww  .j a v  a2  s.c o m*/

    return hosts;
}

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

@Override
public boolean isDatacenterGroupMember(InetAddress endpoint) {
    String endpointDc = DatabaseDescriptor.getEndpointSnitch().getDatacenter(endpoint);
    KSMetaData elasticAdminMetadata = Schema.instance.getKSMetaData(this.elasticAdminKeyspaceName);
    if (elasticAdminMetadata != null && elasticAdminMetadata.strategyOptions.get(endpointDc) != null) {
        return true;
    }//from   ww  w  .  java  2s . c  om
    return false;
}

From source file:org.elassandra.discovery.CassandraDiscovery.java

License:Apache License

@Inject
public CassandraDiscovery(Settings settings, ClusterName clusterName, TransportService transportService,
        ClusterService clusterService, Version version) {
    super(settings);
    this.clusterName = clusterName;
    this.clusterService = clusterService;
    this.transportService = transportService;
    this.version = version;

    this.localAddress = FBUtilities.getBroadcastAddress();
    this.localDc = DatabaseDescriptor.getEndpointSnitch().getDatacenter(localAddress);

    Map<String, String> attrs = Maps.newHashMap();
    attrs.put("data", "true");
    attrs.put("master", "true");
    attrs.put("data_center", localDc);
    attrs.put("rack", DatabaseDescriptor.getEndpointSnitch().getRack(localAddress));

    String localHostId = SystemKeyspace.getLocalHostId().toString();
    localNode = new DiscoveryNode(buildNodeName(localAddress), localHostId,
            new InetSocketTransportAddress(FBUtilities.getBroadcastAddress(), publishPort()), attrs, version);
    localNode.status(DiscoveryNodeStatus.ALIVE);
    this.transportService.setLocalNode(localNode);

    this.clusterGroup = new ClusterGroup();
    clusterGroups.put(clusterName, clusterGroup);
    clusterGroup.put(this.localNode.getId(), this.localNode);

    logger.info("localNode name={} id={} localAddress={} publish_host={}", this.localNode.getName(),
            this.localNode.getId(), localAddress, this.localNode.address());
}

From source file:org.elassandra.discovery.CassandraDiscovery.java

License:Apache License

/**
 * Update cluster group members from cassandra topology (should only be triggered by IEndpointStateChangeSubscriber events).
 * This should trigger re-sharding of index for new nodes (when token distribution change).
 *///  w  w  w. ja  va 2  s. co m
public void updateClusterGroupsFromGossiper() {
    for (Entry<InetAddress, EndpointState> entry : Gossiper.instance.getEndpointStates()) {
        DiscoveryNodeStatus status = (entry.getValue().isAlive()) ? DiscoveryNode.DiscoveryNodeStatus.ALIVE
                : DiscoveryNode.DiscoveryNodeStatus.DEAD;

        if (DatabaseDescriptor.getEndpointSnitch().getDatacenter(entry.getKey()).equals(localDc)) {
            VersionedValue vv = entry.getValue().getApplicationState(ApplicationState.HOST_ID);
            if (vv != null) {
                String hostId = vv.value;
                DiscoveryNode dn = clusterGroup.get(hostId);
                if (dn == null) {
                    Map<String, String> attrs = Maps.newHashMap();
                    attrs.put("data", "true");
                    attrs.put("master", "true");
                    attrs.put("data_center", localDc);
                    attrs.put("rack", DatabaseDescriptor.getEndpointSnitch().getRack(entry.getKey()));

                    InetAddress rpc_address = com.google.common.net.InetAddresses.forString(
                            entry.getValue().getApplicationState(ApplicationState.RPC_ADDRESS).value);
                    dn = new DiscoveryNode(buildNodeName(entry.getKey()), hostId.toString(),
                            new InetSocketTransportAddress(rpc_address, publishPort()), attrs, version);
                    dn.status(status);

                    if (localAddress.equals(entry.getKey())) {
                        logger.debug("Update local node host_id={} status={} timestamp={}",
                                NetworkAddress.format(entry.getKey()), dn.getId(), dn.getName(),
                                entry.getValue().isAlive(), entry.getValue().getUpdateTimestamp());
                        clusterGroup.remove(this.localNode.id());
                        this.localNode = dn;
                    } else {
                        logger.debug("New node addr_ip={} node_name={} host_id={} status={} timestamp={}",
                                NetworkAddress.format(entry.getKey()), dn.getId(), dn.getName(),
                                entry.getValue().isAlive(), entry.getValue().getUpdateTimestamp());
                    }
                    clusterGroup.put(dn.getId(), dn);
                    if (entry.getValue().getApplicationState(ApplicationState.X1) != null
                            || entry.getValue().getApplicationState(ApplicationState.X2) != null) {
                        SystemKeyspace.updatePeerInfo(entry.getKey(), "workload", "elasticsearch");
                    }
                } else {
                    // may update DiscoveryNode status.
                    if (!dn.getStatus().equals(status)) {
                        dn.status(status);
                    }
                }
            }
        }
    }
}

From source file:org.elassandra.discovery.CassandraDiscovery.java

License:Apache License

public void updateNode(InetAddress addr, EndpointState state) {
    if (DatabaseDescriptor.getEndpointSnitch().getDatacenter(addr).equals(localDc)) {
        DiscoveryNodeStatus status = (state.isAlive()) ? DiscoveryNode.DiscoveryNodeStatus.ALIVE
                : DiscoveryNode.DiscoveryNodeStatus.DEAD;
        boolean updatedNode = false;
        String hostId = state.getApplicationState(ApplicationState.HOST_ID).value;
        DiscoveryNode dn = clusterGroup.get(hostId);
        if (dn == null) {
            Map<String, String> attrs = Maps.newHashMap();
            attrs.put("data", "true");
            attrs.put("master", "true");
            attrs.put("data_center", localDc);
            attrs.put("rack", DatabaseDescriptor.getEndpointSnitch().getRack(addr));

            InetAddress rpc_address = com.google.common.net.InetAddresses
                    .forString(state.getApplicationState(ApplicationState.RPC_ADDRESS).value);
            dn = new DiscoveryNode(buildNodeName(addr), hostId.toString(),
                    new InetSocketTransportAddress(rpc_address, publishPort()), attrs, version);
            dn.status(status);//from   w ww  . j  av a2s  . c  om
            logger.debug(
                    "New node soure=updateNode internal_ip={} rpc_address={}, node_name={} host_id={} status={} timestamp={}",
                    NetworkAddress.format(addr), NetworkAddress.format(rpc_address), dn.getId(), dn.getName(),
                    status, state.getUpdateTimestamp());
            clusterGroup.members.put(dn.getId(), dn);
            if (state.getApplicationState(ApplicationState.X1) != null
                    || state.getApplicationState(ApplicationState.X2) != null) {
                SystemKeyspace.updatePeerInfo(addr, "workload", "elasticsearch");
            }
            updatedNode = true;
        } else {
            // may update DiscoveryNode status.
            if (!dn.getStatus().equals(status)) {
                dn.status(status);
                updatedNode = true;
            }
        }
        if (updatedNode)
            updateRoutingTable("update-node-" + NetworkAddress.format(addr));
    }
}

From source file:org.elassandra.discovery.CassandraDiscovery.java

License:Apache License

/**
 * Release listeners who have reached the expected metadat version.
 *//*  ww  w  .  ja va 2s .  c om*/
public void checkMetaDataVersion() {
    for (Iterator<MetaDataVersionListener> it = this.metaDataVersionListeners.iterator(); it.hasNext();) {
        MetaDataVersionListener listener = it.next();
        boolean versionReached = true;
        for (InetAddress addr : Gossiper.instance.getLiveTokenOwners()) {
            if (DatabaseDescriptor.getEndpointSnitch().getDatacenter(addr).equals(localDc)) {
                EndpointState endPointState = Gossiper.instance.getEndpointStateForEndpoint(addr);
                VersionedValue vv = endPointState.getApplicationState(ELASTIC_META_DATA);
                if (vv != null && vv.value.lastIndexOf('/') > 0) {
                    Long version = Long.valueOf(vv.value.substring(vv.value.lastIndexOf('/') + 1));
                    if (version < listener.version()) {
                        versionReached = false;
                        break;
                    }
                }
            }
        }
        if (versionReached) {
            logger.debug("MetaData.version = {} reached", listener.version());
            listener.release();
            metaDataVersionListeners.remove(listener);
        }
    }
}

From source file:org.elassandra.discovery.CassandraDiscovery.java

License:Apache License

@Override
public void onChange(InetAddress endpoint, ApplicationState state, VersionedValue versionValue) {
    EndpointState epState = Gossiper.instance.getEndpointStateForEndpoint(endpoint);
    if (epState == null || Gossiper.instance.isDeadState(epState)) {
        if (logger.isTraceEnabled())
            logger.trace("Ignoring state change for dead or unknown endpoint: {}", endpoint);
        return;//from w  ww.j a v  a  2s .c o  m
    }
    if (!this.localAddress.equals(endpoint)) {
        if (state == ApplicationState.X1
                && DatabaseDescriptor.getEndpointSnitch().getDatacenter(endpoint).equals(localDc)) {
            // X1: update local shard state
            if (logger.isTraceEnabled())
                logger.trace("Endpoint={} ApplicationState={} value={} => update routingTable", endpoint, state,
                        versionValue.value);
            updateRoutingTable("onChange-" + endpoint + "-" + state.toString() + " X1=" + versionValue.value);
            connectToNode(endpoint);
        } else if (state == ApplicationState.X2 && clusterService.isDatacenterGroupMember(endpoint)) {
            // X2 from datacenter.group: update metadata if metadata version is higher than our.
            if (versionValue != null) {
                int i = versionValue.value.lastIndexOf('/');
                if (i > 0) {
                    Long version = Long.valueOf(versionValue.value.substring(i + 1));
                    if (version > this.clusterService.state().metaData().version()) {
                        MetaData metadata = clusterService.checkForNewMetaData(version);
                        if (metadata != null) {
                            if (logger.isTraceEnabled())
                                logger.trace(
                                        "Endpoint={} ApplicationState={} value={} => update metaData {}/{}",
                                        endpoint, state, versionValue.value, metadata.uuid(),
                                        metadata.version());
                            updateMetadata("onChange-" + endpoint + "-" + state.toString() + " metadata="
                                    + metadata.uuid() + "/" + metadata.version(), metadata);
                        }
                    }
                }
            }
            if (metaDataVersionListeners.size() > 0
                    && DatabaseDescriptor.getEndpointSnitch().getDatacenter(endpoint).equals(localDc)) {
                checkMetaDataVersion();
            }
        }
    }
}

From source file:org.elassandra.discovery.CassandraDiscovery.java

License:Apache License

@Override
public Map<UUID, ShardRoutingState> getShardRoutingStates(String index) {
    Map<UUID, ShardRoutingState> shardsStates = new HashMap<UUID, ShardRoutingState>(
            this.clusterGroup.members.size());
    for (Entry<InetAddress, EndpointState> entry : Gossiper.instance.getEndpointStates()) {
        InetAddress endpoint = entry.getKey();
        EndpointState state = entry.getValue();
        if (!endpoint.equals(this.localAddress)
                && DatabaseDescriptor.getEndpointSnitch().getDatacenter(endpoint).equals(this.localDc)
                && state != null && state.isAlive()) {
            VersionedValue value = state.getApplicationState(ELASTIC_SHARDS_STATES);
            if (value != null) {
                try {
                    Map<String, ShardRoutingState> shardsStateMap = jsonMapper.readValue(value.value,
                            indexShardStateTypeReference);
                    ShardRoutingState shardState = shardsStateMap.get(index);
                    if (shardState != null) {
                        shardsStates.put(Gossiper.instance.getHostId(endpoint), shardState);
                    }//from w  w  w. j  av  a2 s .  c o m
                } catch (Exception e) {
                    logger.warn("Failed to parse gossip index shard state", e);
                }
            }
        }
    }
    return shardsStates;
}

From source file:org.elasticsearch.cassandra.discovery.CassandraDiscovery.java

License:Apache License

@Override
protected void doStart() {

    synchronized (clusterGroup) {
        logger.debug("Connected to cluster [{}]", clusterName.value());

        localAddress = FBUtilities.getLocalAddress();
        localDc = DatabaseDescriptor.getEndpointSnitch().getDatacenter(localAddress);

        InetSocketTransportAddress elasticAddress = (InetSocketTransportAddress) transportService.boundAddress()
                .publishAddress();// w w  w.j  a  v  a 2  s  . com
        logger.info("Listening address Cassandra=" + localAddress + " Elastic=" + elasticAddress.toString());

        // get local node from cassandra cluster
        {
            Map<String, String> attrs = Maps.newHashMap();
            attrs.put("data", "true");
            attrs.put("master", "true");
            attrs.put("data_center", localDc);
            attrs.put("rack", DatabaseDescriptor.getEndpointSnitch().getRack(localAddress));

            String hostId = SystemKeyspace.getLocalHostId().toString();
            localNode = new DiscoveryNode(buildNodeName(localAddress), hostId,
                    transportService.boundAddress().publishAddress(), attrs, version);
            localNode.status(DiscoveryNodeStatus.ALIVE);
            this.transportService.setLocalNode(localNode); // clusterService start before DiscoveryService.
            master = true;
            clusterGroup.put(this.localNode.getId(), this.localNode);
            logger.info("localNode name={} id={}", this.localNode.getName(), this.localNode.getId());
        }

        // initialize cluster from cassandra system.peers 
        Map<InetAddress, UUID> peers = SystemKeyspace.loadHostIds();
        Map<InetAddress, Map<String, String>> endpointInfo = SystemKeyspace.loadDcRackInfo();
        for (Entry<InetAddress, UUID> entry : peers.entrySet()) {
            if ((!entry.getKey().equals(localAddress))
                    && (localDc.equals(endpointInfo.get(entry.getKey()).get("data_center")))) {
                Map<String, String> attrs = Maps.newHashMap();
                attrs.put("data", "true");
                attrs.put("master", "true");
                attrs.putAll(endpointInfo.get(entry.getKey()));
                DiscoveryNode dn = new DiscoveryNode(buildNodeName(entry.getKey()), entry.getValue().toString(),
                        new InetSocketTransportAddress(entry.getKey(),
                                settings.getAsInt("transport.tcp.port", 9300)),
                        attrs, version);
                EndpointState endpointState = Gossiper.instance.getEndpointStateForEndpoint(entry.getKey());
                if (endpointState == null) {
                    dn.status(DiscoveryNodeStatus.UNKNOWN);
                } else {
                    dn.status((endpointState.isAlive()) ? DiscoveryNodeStatus.ALIVE : DiscoveryNodeStatus.DEAD);
                }
                clusterGroup.put(dn.getId(), dn);
                logger.debug("remanent node addr_ip={} node_name={} host_id={} ", entry.getKey().toString(),
                        dn.getId(), dn.getName());
            }
        }

        Gossiper.instance.register(this);
        updateClusterGroupsFromGossiper();
        updateClusterState("starting-cassandra-discovery", null);
    }
}

From source file:org.elasticsearch.cassandra.discovery.CassandraDiscovery.java

License:Apache License

/**
 * Update cluster group members from cassandra topology (should only be triggered by IEndpointStateChangeSubscriber events).
 * This should trigger re-sharding of index for new nodes (when token distribution change).
 *//* w  ww  .  j av a  2 s  .  co m*/
public void updateClusterGroupsFromGossiper() {
    for (Entry<InetAddress, EndpointState> entry : Gossiper.instance.getEndpointStates()) {
        DiscoveryNodeStatus status = (entry.getValue().isAlive()) ? DiscoveryNode.DiscoveryNodeStatus.ALIVE
                : DiscoveryNode.DiscoveryNodeStatus.DEAD;

        if (DatabaseDescriptor.getEndpointSnitch().getDatacenter(entry.getKey()).equals(localDc)) {
            VersionedValue vv = entry.getValue().getApplicationState(ApplicationState.HOST_ID);
            if (vv != null) {
                String hostId = vv.value;
                DiscoveryNode dn = clusterGroup.get(hostId);
                if (dn == null) {
                    Map<String, String> attrs = Maps.newHashMap();
                    attrs.put("data", "true");
                    attrs.put("master", "true");
                    attrs.put("data_center", localDc);
                    attrs.put("rack", DatabaseDescriptor.getEndpointSnitch().getRack(entry.getKey()));

                    dn = new DiscoveryNode(buildNodeName(entry.getKey()), hostId.toString(),
                            new InetSocketTransportAddress(entry.getKey(),
                                    settings.getAsInt("transport.tcp.port", 9300)),
                            attrs, version);
                    dn.status(status);

                    if (localAddress.equals(entry.getKey())) {
                        logger.debug("Update local node host_id={} status={} timestamp={}",
                                entry.getKey().toString(), dn.getId(), dn.getName(), entry.getValue().isAlive(),
                                entry.getValue().getUpdateTimestamp());
                        clusterGroup.remove(this.localNode.id());
                        this.localNode = dn;
                    } else {
                        logger.debug("New node addr_ip={} node_name={} host_id={} status={} timestamp={}",
                                entry.getKey().toString(), dn.getId(), dn.getName(), entry.getValue().isAlive(),
                                entry.getValue().getUpdateTimestamp());
                    }
                    clusterGroup.put(dn.getId(), dn);
                } else {
                    // may update DiscoveryNode status.
                    if (!dn.getStatus().equals(status)) {
                        dn.status(status);
                    }
                }
            }
        }
    }

}