Example usage for org.apache.cassandra.gms EndpointState isAlive

List of usage examples for org.apache.cassandra.gms EndpointState isAlive

Introduction

In this page you can find the example usage for org.apache.cassandra.gms EndpointState isAlive.

Prototype

boolean isAlive

To view the source code for org.apache.cassandra.gms EndpointState isAlive.

Click Source Link

Usage

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

License:Apache License

@Override
protected void doStart() {

    synchronized (clusterGroup) {
        logger.debug("Connected to cluster [{}]", clusterName.value());
        // initialize cluster from cassandra system.peers 
        // WARNING: system.peers may be incomplete because commitlogs not yet applied
        for (UntypedResultSet.Row row : executeInternal(
                "SELECT peer, data_center, rack, rpc_address, host_id from system." + SystemKeyspace.PEERS)) {
            InetAddress peer = row.getInetAddress("peer");
            InetAddress rpc_address = row.getInetAddress("rpc_address");
            String datacenter = row.getString("data_center");
            if ((!peer.equals(localAddress)) && (localDc.equals(datacenter))) {
                Map<String, String> attrs = Maps.newHashMap();
                attrs.put("data", "true");
                attrs.put("master", "true");
                attrs.put("data_center", datacenter);
                attrs.put("rack", row.getString("rack"));

                DiscoveryNode dn = new DiscoveryNode(buildNodeName(peer), row.getUUID("host_id").toString(),
                        new InetSocketTransportAddress(rpc_address, publishPort()), attrs, version);
                EndpointState endpointState = Gossiper.instance.getEndpointStateForEndpoint(peer);
                if (endpointState == null) {
                    dn.status(DiscoveryNodeStatus.UNKNOWN);
                } else {
                    dn.status((endpointState.isAlive()) ? DiscoveryNodeStatus.ALIVE : DiscoveryNodeStatus.DEAD);
                }//  w  w w. ja va2 s.  c o  m
                clusterGroup.put(dn.getId(), dn);
                logger.debug("  node internal_ip={} host_id={} node_name={} ", NetworkAddress.format(peer),
                        dn.getId(), dn.getName());
            }
        }

        Gossiper.instance.register(this);
        updateClusterGroupsFromGossiper();
        updateRoutingTable("starting-cassandra-discovery");
    }
}

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  ww w  . jav  a  2  s .  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

@Override
public void onAlive(InetAddress arg0, EndpointState arg1) {
    logger.debug("onAlive Endpoint={} ApplicationState={} isAlive={} => update node + connecting", arg0, arg1,
            arg1.isAlive());
    updateNode(arg0, arg1);//from  w w  w .  j av  a  2  s  .  c om
    connectToNode(arg0);
}

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

License:Apache License

@Override
public void onDead(InetAddress arg0, EndpointState arg1) {
    logger.debug("onDead Endpoint={}  ApplicationState={} isAlive={} => update node + disconnecting", arg0,
            arg1, arg1.isAlive());
    updateNode(arg0, arg1);//from   w  ww .  j ava 2 s  .c  o m
    //disconnectFromNode(arg0);
}

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 a  v a 2 s .c om
                } 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();// ww w .j  ava2  s . 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);
    }
}

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

License:Apache License

public void updateNode(InetAddress addr, EndpointState state) {

    DiscoveryNodeStatus status = (state.isAlive()) ? DiscoveryNode.DiscoveryNodeStatus.ALIVE
            : DiscoveryNode.DiscoveryNodeStatus.DEAD;
    boolean updatedNode = false;
    if (DatabaseDescriptor.getEndpointSnitch().getDatacenter(addr).equals(localDc)) {
        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));

            dn = new DiscoveryNode(buildNodeName(addr), hostId.toString(),
                    new InetSocketTransportAddress(addr, settings.getAsInt("transport.tcp.port", 9300)), attrs,
                    version);//  w  w w  .  j  a v  a2s.  co m
            dn.status(status);
            logger.debug("New node soure=updateNode addr_ip={} node_name={} host_id={} status={} timestamp={}",
                    addr.getHostAddress(), dn.getId(), dn.getName(), status, state.getUpdateTimestamp());
            clusterGroup.members.put(dn.getId(), dn);
            updatedNode = true;
        } else {
            // may update DiscoveryNode status.
            if (!dn.getStatus().equals(status)) {
                dn.status(status);
                updatedNode = true;
            }
        }
    }
    if (updatedNode)
        updateClusterState("update-node-" + addr.getHostAddress(), null);
}

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

License:Apache License

@Override
public void onAlive(InetAddress arg0, EndpointState arg1) {
    logger.debug("onAlive Endpoint={} ApplicationState={} isAlive={} => update node", arg0, arg1,
            arg1.isAlive());
    updateNode(arg0, arg1);//from w w  w . j  a va 2  s . c  o m
}

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

License:Apache License

@Override
public void onDead(InetAddress arg0, EndpointState arg1) {
    logger.debug("onDead Endpoint={}  ApplicationState={} isAlive={} => update node", arg0, arg1,
            arg1.isAlive());
    updateNode(arg0, arg1);//  w w w  . j  av a 2s.  c o m
}