Search in sources :

Example 1 with ProcessedClusterStateUpdateTask

use of org.elasticsearch.cluster.ProcessedClusterStateUpdateTask in project fabric8 by jboss-fuse.

the class FabricDiscovery method updateCluster.

private void updateCluster() {
    try {
        singleton.update(new ESNode(clusterName.value(), localNode, singleton.isMaster()));
    } catch (Exception e) {
    // Ignore if not joined
    }
    if (singleton.isMaster()) {
        if (logger.isDebugEnabled()) {
            String master = singleton.master() != null ? singleton.master().node.name() : null;
            List<String> slaves = new ArrayList<>();
            for (ESNode s : singleton.slaves()) {
                slaves.add(s.node.name());
            }
            logger.debug("Updating cluster: master {}, slaves {}", master, slaves);
        }
        clusterService.submitStateUpdateTask("fabric-discovery-master", Priority.URGENT, new ProcessedClusterStateUpdateTask() {

            @Override
            public ClusterState execute(ClusterState currentState) {
                // Rebuild state
                ClusterState.Builder stateBuilder = ClusterState.builder(currentState);
                // Rebuild nodes
                DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder().localNodeId(localNode.id()).masterNodeId(singleton.master().getNode().id()).put(singleton.master().getNode());
                for (ESNode node : singleton.slaves()) {
                    nodesBuilder.put(node.getNode());
                }
                latestDiscoNodes = nodesBuilder.build();
                stateBuilder.nodes(latestDiscoNodes);
                for (DiscoveryNode node : latestDiscoNodes) {
                    if (!currentState.nodes().nodeExists(node.id())) {
                        transportService.connectToNode(node);
                    }
                }
                // update the fact that we are the master...
                if (!localNode().id().equals(currentState.nodes().masterNodeId())) {
                    ClusterBlocks clusterBlocks = ClusterBlocks.builder().blocks(currentState.blocks()).removeGlobalBlock(NO_MASTER_BLOCK).build();
                    stateBuilder.blocks(clusterBlocks);
                }
                return stateBuilder.build();
            }

            @Override
            public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                logger.debug("Cluster updated");
                sendInitialStateEventIfNeeded();
            }

            @Override
            public void onFailure(String source, Throwable t) {
                logger.error("unexpected failure during [{}]", t, source);
            }
        });
    } else if (singleton.master() != null) {
        DiscoveryNode masterNode = singleton.master().getNode();
        try {
            // first, make sure we can connect to the master
            transportService.connectToNode(masterNode);
        } catch (Exception e) {
            logger.warn("failed to connect to master [{}], retrying...", e, masterNode);
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) ProcessedClusterStateUpdateTask(org.elasticsearch.cluster.ProcessedClusterStateUpdateTask) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchIllegalStateException(org.elasticsearch.ElasticsearchIllegalStateException) IOException(java.io.IOException)

Example 2 with ProcessedClusterStateUpdateTask

use of org.elasticsearch.cluster.ProcessedClusterStateUpdateTask in project fabric8 by jboss-fuse.

the class FabricDiscovery method onNewClusterState.

@Override
public void onNewClusterState(final ClusterState newState, final NewStateProcessed newStateProcessed) {
    if (singleton.isMaster()) {
        logger.warn("master should not receive new cluster state from [{}]", newState.nodes().masterNode());
    } else {
        if (newState.nodes().localNode() == null) {
            logger.warn("received a cluster state from [{}] and not part of the cluster, should not happen", newState.nodes().masterNode());
        } else {
            if (logger.isDebugEnabled()) {
                String master = singleton.master() != null ? singleton.master().node.name() : null;
                List<String> slaves = new ArrayList<String>();
                for (ESNode s : singleton.slaves()) {
                    slaves.add(s.node.name());
                }
                logger.debug("Cluster state received: master {}, slaves {}", master, slaves);
            }
            final ProcessClusterState processClusterState = new ProcessClusterState(newState, newStateProcessed);
            processNewClusterStates.add(processClusterState);
            clusterService.submitStateUpdateTask("fabric-discovery-slave", new ProcessedClusterStateUpdateTask() {

                @Override
                public ClusterState execute(ClusterState currentState) {
                    // we already processed it in a previous event
                    if (processClusterState.processed) {
                        return currentState;
                    }
                    // try and get the state with the highest version out of all the ones with the same master node id
                    ProcessClusterState stateToProcess = processNewClusterStates.poll();
                    if (stateToProcess == null) {
                        return currentState;
                    }
                    stateToProcess.processed = true;
                    while (true) {
                        ProcessClusterState potentialState = processNewClusterStates.peek();
                        // nothing else in the queue, bail
                        if (potentialState == null) {
                            break;
                        }
                        // if its not from the same master, then bail
                        if (!Objects.equal(stateToProcess.clusterState.nodes().masterNodeId(), potentialState.clusterState.nodes().masterNodeId())) {
                            break;
                        }
                        // we are going to use it for sure, poll (remove) it
                        potentialState = processNewClusterStates.poll();
                        potentialState.processed = true;
                        if (potentialState.clusterState.version() > stateToProcess.clusterState.version()) {
                            // we found a new one
                            stateToProcess = potentialState;
                        }
                    }
                    ClusterState updatedState = stateToProcess.clusterState;
                    // if the new state has a smaller version, and it has the same master node, then no need to process it
                    if (updatedState.version() < currentState.version() && Objects.equal(updatedState.nodes().masterNodeId(), currentState.nodes().masterNodeId())) {
                        return currentState;
                    }
                    latestDiscoNodes = updatedState.nodes();
                    ClusterState.Builder builder = ClusterState.builder(updatedState);
                    // if the routing table did not change, use the original one
                    if (updatedState.routingTable().version() == currentState.routingTable().version()) {
                        builder.routingTable(currentState.routingTable());
                    }
                    // same for metadata
                    if (updatedState.metaData().version() == currentState.metaData().version()) {
                        builder.metaData(currentState.metaData());
                    } else {
                        // if its not the same version, only copy over new indices or ones that changed the version
                        MetaData.Builder metaDataBuilder = MetaData.builder(updatedState.metaData()).removeAllIndices();
                        for (IndexMetaData indexMetaData : updatedState.metaData()) {
                            IndexMetaData currentIndexMetaData = currentState.metaData().index(indexMetaData.index());
                            if (currentIndexMetaData == null || currentIndexMetaData.version() != indexMetaData.version()) {
                                metaDataBuilder.put(indexMetaData, false);
                            } else {
                                metaDataBuilder.put(currentIndexMetaData, false);
                            }
                        }
                        builder.metaData(metaDataBuilder);
                    }
                    return builder.build();
                }

                @Override
                public void clusterStateProcessed(String s, ClusterState clusterState, ClusterState clusterState2) {
                    logger.debug("Cluster state processed");
                    sendInitialStateEventIfNeeded();
                    newStateProcessed.onNewClusterStateProcessed();
                }

                @Override
                public void onFailure(String source, Throwable t) {
                    logger.error("unexpected failure during [{}]", t, source);
                    newStateProcessed.onNewClusterStateFailed(t);
                }
            });
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ProcessedClusterStateUpdateTask(org.elasticsearch.cluster.ProcessedClusterStateUpdateTask) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 ClusterState (org.elasticsearch.cluster.ClusterState)2 ProcessedClusterStateUpdateTask (org.elasticsearch.cluster.ProcessedClusterStateUpdateTask)2 IOException (java.io.IOException)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 ElasticsearchIllegalStateException (org.elasticsearch.ElasticsearchIllegalStateException)1 ClusterBlocks (org.elasticsearch.cluster.block.ClusterBlocks)1 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1