Example usage for org.apache.cassandra.db SystemKeyspace getLocalHostId

List of usage examples for org.apache.cassandra.db SystemKeyspace getLocalHostId

Introduction

In this page you can find the example usage for org.apache.cassandra.db SystemKeyspace getLocalHostId.

Prototype

public static UUID getLocalHostId() 

Source Link

Document

Read the host ID from the system keyspace, creating (and storing) one if none exists.

Usage

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.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();/* ww  w .ja va2s .c o m*/
        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);
    }
}