Search in sources :

Example 26 with DiscoveryNodes

use of org.opensearch.cluster.node.DiscoveryNodes in project OpenSearch by opensearch-project.

the class JoinTaskExecutor method execute.

@Override
public ClusterTasksResult<Task> execute(ClusterState currentState, List<Task> joiningNodes) throws Exception {
    final ClusterTasksResult.Builder<Task> results = ClusterTasksResult.builder();
    final DiscoveryNodes currentNodes = currentState.nodes();
    boolean nodesChanged = false;
    ClusterState.Builder newState;
    if (joiningNodes.size() == 1 && joiningNodes.get(0).isFinishElectionTask()) {
        return results.successes(joiningNodes).build(currentState);
    } else if (currentNodes.getMasterNode() == null && joiningNodes.stream().anyMatch(Task::isBecomeMasterTask)) {
        assert joiningNodes.stream().anyMatch(Task::isFinishElectionTask) : "becoming a cluster-manager but election is not finished " + joiningNodes;
        // use these joins to try and become the cluster-manager.
        // Note that we don't have to do any validation of the amount of joining nodes - the commit
        // during the cluster state publishing guarantees that we have enough
        newState = becomeClusterManagerAndTrimConflictingNodes(currentState, joiningNodes);
        nodesChanged = true;
    } else if (currentNodes.isLocalNodeElectedMaster() == false) {
        logger.trace("processing node joins, but we are not the cluster-manager. current cluster-manager: {}", currentNodes.getMasterNode());
        throw new NotMasterException("Node [" + currentNodes.getLocalNode() + "] not cluster-manager for join request");
    } else {
        newState = ClusterState.builder(currentState);
    }
    DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(newState.nodes());
    assert nodesBuilder.isLocalNodeElectedMaster();
    Version minClusterNodeVersion = newState.nodes().getMinNodeVersion();
    Version maxClusterNodeVersion = newState.nodes().getMaxNodeVersion();
    // we only enforce major version transitions on a fully formed clusters
    final boolean enforceMajorVersion = currentState.getBlocks().hasGlobalBlock(STATE_NOT_RECOVERED_BLOCK) == false;
    // processing any joins
    Map<String, String> joiniedNodeNameIds = new HashMap<>();
    for (final Task joinTask : joiningNodes) {
        if (joinTask.isBecomeMasterTask() || joinTask.isFinishElectionTask()) {
        // noop
        } else if (currentNodes.nodeExistsWithSameRoles(joinTask.node()) && !currentNodes.nodeExistsWithBWCVersion(joinTask.node())) {
            logger.debug("received a join request for an existing node [{}]", joinTask.node());
        } else {
            final DiscoveryNode node = joinTask.node();
            try {
                if (enforceMajorVersion) {
                    ensureMajorVersionBarrier(node.getVersion(), minClusterNodeVersion);
                }
                ensureNodesCompatibility(node.getVersion(), minClusterNodeVersion, maxClusterNodeVersion);
                // we do this validation quite late to prevent race conditions between nodes joining and importing dangling indices
                // we have to reject nodes that don't support all indices we have in this cluster
                ensureIndexCompatibility(node.getVersion(), currentState.getMetadata());
                nodesBuilder.add(node);
                nodesChanged = true;
                minClusterNodeVersion = Version.min(minClusterNodeVersion, node.getVersion());
                maxClusterNodeVersion = Version.max(maxClusterNodeVersion, node.getVersion());
                if (node.isMasterNode()) {
                    joiniedNodeNameIds.put(node.getName(), node.getId());
                }
            } catch (IllegalArgumentException | IllegalStateException e) {
                results.failure(joinTask, e);
                continue;
            }
        }
        results.success(joinTask);
    }
    if (nodesChanged) {
        rerouteService.reroute("post-join reroute", Priority.HIGH, ActionListener.wrap(r -> logger.trace("post-join reroute completed"), e -> logger.debug("post-join reroute failed", e)));
        if (joiniedNodeNameIds.isEmpty() == false) {
            Set<CoordinationMetadata.VotingConfigExclusion> currentVotingConfigExclusions = currentState.getVotingConfigExclusions();
            Set<CoordinationMetadata.VotingConfigExclusion> newVotingConfigExclusions = currentVotingConfigExclusions.stream().map(e -> {
                // Update nodeId in VotingConfigExclusion when a new node with excluded node name joins
                if (CoordinationMetadata.VotingConfigExclusion.MISSING_VALUE_MARKER.equals(e.getNodeId()) && joiniedNodeNameIds.containsKey(e.getNodeName())) {
                    return new CoordinationMetadata.VotingConfigExclusion(joiniedNodeNameIds.get(e.getNodeName()), e.getNodeName());
                } else {
                    return e;
                }
            }).collect(Collectors.toSet());
            // if VotingConfigExclusions did get updated
            if (newVotingConfigExclusions.equals(currentVotingConfigExclusions) == false) {
                CoordinationMetadata.Builder coordMetadataBuilder = CoordinationMetadata.builder(currentState.coordinationMetadata()).clearVotingConfigExclusions();
                newVotingConfigExclusions.forEach(coordMetadataBuilder::addVotingConfigExclusion);
                Metadata newMetadata = Metadata.builder(currentState.metadata()).coordinationMetadata(coordMetadataBuilder.build()).build();
                return results.build(allocationService.adaptAutoExpandReplicas(newState.nodes(nodesBuilder).metadata(newMetadata).build()));
            }
        }
        return results.build(allocationService.adaptAutoExpandReplicas(newState.nodes(nodesBuilder).build()));
    } else {
        // for the joining node to finalize its join and set us as a cluster-manager
        return results.build(newState.build());
    }
}
Also used : DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) STATE_NOT_RECOVERED_BLOCK(org.opensearch.gateway.GatewayService.STATE_NOT_RECOVERED_BLOCK) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) Version(org.opensearch.Version) Priority(org.opensearch.common.Priority) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) LegacyESVersion(org.opensearch.LegacyESVersion) Map(java.util.Map) NotMasterException(org.opensearch.cluster.NotMasterException) RerouteService(org.opensearch.cluster.routing.RerouteService) BiConsumer(java.util.function.BiConsumer) ActionListener(org.opensearch.action.ActionListener) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) Collection(java.util.Collection) PersistentTasksCustomMetadata(org.opensearch.persistent.PersistentTasksCustomMetadata) Set(java.util.Set) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor) Settings(org.opensearch.common.settings.Settings) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Collections(java.util.Collections) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) PersistentTasksCustomMetadata(org.opensearch.persistent.PersistentTasksCustomMetadata) Version(org.opensearch.Version) LegacyESVersion(org.opensearch.LegacyESVersion) NotMasterException(org.opensearch.cluster.NotMasterException) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 27 with DiscoveryNodes

use of org.opensearch.cluster.node.DiscoveryNodes in project OpenSearch by opensearch-project.

the class JoinTaskExecutor method becomeClusterManagerAndTrimConflictingNodes.

protected ClusterState.Builder becomeClusterManagerAndTrimConflictingNodes(ClusterState currentState, List<Task> joiningNodes) {
    assert currentState.nodes().getMasterNodeId() == null : currentState;
    DiscoveryNodes currentNodes = currentState.nodes();
    DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(currentNodes);
    nodesBuilder.masterNodeId(currentState.nodes().getLocalNodeId());
    for (final Task joinTask : joiningNodes) {
        if (joinTask.isBecomeMasterTask()) {
            refreshDiscoveryNodeVersionAfterUpgrade(currentNodes, nodesBuilder);
        } else if (joinTask.isFinishElectionTask()) {
        // no-op
        } else {
            final DiscoveryNode joiningNode = joinTask.node();
            final DiscoveryNode nodeWithSameId = nodesBuilder.get(joiningNode.getId());
            if (nodeWithSameId != null && nodeWithSameId.equals(joiningNode) == false) {
                logger.debug("removing existing node [{}], which conflicts with incoming join from [{}]", nodeWithSameId, joiningNode);
                nodesBuilder.remove(nodeWithSameId.getId());
            }
            final DiscoveryNode nodeWithSameAddress = currentNodes.findByAddress(joiningNode.getAddress());
            if (nodeWithSameAddress != null && nodeWithSameAddress.equals(joiningNode) == false) {
                logger.debug("removing existing node [{}], which conflicts with incoming join from [{}]", nodeWithSameAddress, joiningNode);
                nodesBuilder.remove(nodeWithSameAddress.getId());
            }
        }
    }
    // now trim any left over dead nodes - either left there when the previous cluster-manager stepped down
    // or removed by us above
    ClusterState tmpState = ClusterState.builder(currentState).nodes(nodesBuilder).blocks(ClusterBlocks.builder().blocks(currentState.blocks()).removeGlobalBlock(NoMasterBlockService.NO_MASTER_BLOCK_ID)).build();
    logger.trace("becomeClusterManagerAndTrimConflictingNodes: {}", tmpState.nodes());
    allocationService.cleanCaches();
    tmpState = PersistentTasksCustomMetadata.disassociateDeadNodes(tmpState);
    return ClusterState.builder(allocationService.disassociateDeadNodes(tmpState, false, "removed dead nodes on election"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 28 with DiscoveryNodes

use of org.opensearch.cluster.node.DiscoveryNodes in project OpenSearch by opensearch-project.

the class Coordinator method clusterStateWithNoClusterManagerBlock.

private ClusterState clusterStateWithNoClusterManagerBlock(ClusterState clusterState) {
    if (clusterState.nodes().getMasterNodeId() != null) {
        // remove block if it already exists before adding new one
        assert clusterState.blocks().hasGlobalBlockWithId(NO_MASTER_BLOCK_ID) == false : "NO_MASTER_BLOCK should only be added by Coordinator";
        final ClusterBlocks clusterBlocks = ClusterBlocks.builder().blocks(clusterState.blocks()).addGlobalBlock(noClusterManagerBlockService.getNoMasterBlock()).build();
        final DiscoveryNodes discoveryNodes = new DiscoveryNodes.Builder(clusterState.nodes()).masterNodeId(null).build();
        return ClusterState.builder(clusterState).blocks(clusterBlocks).nodes(discoveryNodes).build();
    } else {
        return clusterState;
    }
}
Also used : ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 29 with DiscoveryNodes

use of org.opensearch.cluster.node.DiscoveryNodes in project OpenSearch by opensearch-project.

the class VerifyNodeRepositoryAction method verify.

public void verify(String repository, String verificationToken, final ActionListener<List<DiscoveryNode>> listener) {
    final DiscoveryNodes discoNodes = clusterService.state().nodes();
    final DiscoveryNode localNode = discoNodes.getLocalNode();
    final ObjectContainer<DiscoveryNode> masterAndDataNodes = discoNodes.getMasterAndDataNodes().values();
    final List<DiscoveryNode> nodes = new ArrayList<>();
    for (ObjectCursor<DiscoveryNode> cursor : masterAndDataNodes) {
        DiscoveryNode node = cursor.value;
        if (RepositoriesService.isDedicatedVotingOnlyNode(node.getRoles()) == false) {
            nodes.add(node);
        }
    }
    final CopyOnWriteArrayList<VerificationFailure> errors = new CopyOnWriteArrayList<>();
    final AtomicInteger counter = new AtomicInteger(nodes.size());
    for (final DiscoveryNode node : nodes) {
        if (node.equals(localNode)) {
            try {
                doVerify(repository, verificationToken, localNode);
            } catch (Exception e) {
                logger.warn(() -> new ParameterizedMessage("[{}] failed to verify repository", repository), e);
                errors.add(new VerificationFailure(node.getId(), e));
            }
            if (counter.decrementAndGet() == 0) {
                finishVerification(repository, listener, nodes, errors);
            }
        } else {
            transportService.sendRequest(node, ACTION_NAME, new VerifyNodeRepositoryRequest(repository, verificationToken), new EmptyTransportResponseHandler(ThreadPool.Names.SAME) {

                @Override
                public void handleResponse(TransportResponse.Empty response) {
                    if (counter.decrementAndGet() == 0) {
                        finishVerification(repository, listener, nodes, errors);
                    }
                }

                @Override
                public void handleException(TransportException exp) {
                    errors.add(new VerificationFailure(node.getId(), exp));
                    if (counter.decrementAndGet() == 0) {
                        finishVerification(repository, listener, nodes, errors);
                    }
                }
            });
        }
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TransportResponse(org.opensearch.transport.TransportResponse) TransportException(org.opensearch.transport.TransportException) IOException(java.io.IOException) TransportException(org.opensearch.transport.TransportException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) EmptyTransportResponseHandler(org.opensearch.transport.EmptyTransportResponseHandler) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 30 with DiscoveryNodes

use of org.opensearch.cluster.node.DiscoveryNodes in project OpenSearch by opensearch-project.

the class IndexingPressureIT method getPrimaryReplicaNodeNames.

private Tuple<String, String> getPrimaryReplicaNodeNames() {
    IndicesStatsResponse response = client().admin().indices().prepareStats(INDEX_NAME).get();
    String primaryId = Stream.of(response.getShards()).map(ShardStats::getShardRouting).filter(ShardRouting::primary).findAny().get().currentNodeId();
    String replicaId = Stream.of(response.getShards()).map(ShardStats::getShardRouting).filter(sr -> sr.primary() == false).findAny().get().currentNodeId();
    DiscoveryNodes nodes = client().admin().cluster().prepareState().get().getState().nodes();
    String primaryName = nodes.get(primaryId).getName();
    String replicaName = nodes.get(replicaId).getName();
    return new Tuple<>(primaryName, replicaName);
}
Also used : ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) Arrays(java.util.Arrays) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IndexResponse(org.opensearch.action.index.IndexResponse) ThreadPool(org.opensearch.threadpool.ThreadPool) BulkRequest(org.opensearch.action.bulk.BulkRequest) Releasable(org.opensearch.common.lease.Releasable) MockTransportService(org.opensearch.test.transport.MockTransportService) InternalTestCluster(org.opensearch.test.InternalTestCluster) ArrayList(java.util.ArrayList) TransportShardBulkAction(org.opensearch.action.bulk.TransportShardBulkAction) InternalSettingsPlugin(org.opensearch.test.InternalSettingsPlugin) UUIDs(org.opensearch.common.UUIDs) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) Collection(java.util.Collection) Settings(org.opensearch.common.settings.Settings) TransportService(org.opensearch.transport.TransportService) Plugin(org.opensearch.plugins.Plugin) ActionFuture(org.opensearch.action.ActionFuture) Tuple(org.opensearch.common.collect.Tuple) ShardRouting(org.opensearch.cluster.routing.ShardRouting) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) BulkResponse(org.opensearch.action.bulk.BulkResponse) IndicesStatsResponse(org.opensearch.action.admin.indices.stats.IndicesStatsResponse) ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) IndexRequest(org.opensearch.action.index.IndexRequest) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) Collections(java.util.Collections) IndicesStatsResponse(org.opensearch.action.admin.indices.stats.IndicesStatsResponse) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) Tuple(org.opensearch.common.collect.Tuple)

Aggregations

DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)128 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)82 ClusterState (org.opensearch.cluster.ClusterState)49 Settings (org.opensearch.common.settings.Settings)32 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)27 HashSet (java.util.HashSet)22 ArrayList (java.util.ArrayList)21 ClusterName (org.opensearch.cluster.ClusterName)21 Metadata (org.opensearch.cluster.metadata.Metadata)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 TransportService (org.opensearch.transport.TransportService)19 Collections (java.util.Collections)18 List (java.util.List)18 IOException (java.io.IOException)17 Map (java.util.Map)17 CountDownLatch (java.util.concurrent.CountDownLatch)17 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 ShardRouting (org.opensearch.cluster.routing.ShardRouting)17 ShardId (org.opensearch.index.shard.ShardId)17 Version (org.opensearch.Version)16