Search in sources :

Example 6 with DiscoveryNodes

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

the class JoinTaskExecutor method becomeMasterAndTrimConflictingNodes.

protected ClusterState.Builder becomeMasterAndTrimConflictingNodes(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 master 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("becomeMasterAndTrimConflictingNodes: {}", 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 7 with DiscoveryNodes

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

the class Coordinator method clusterStateWithNoMasterBlock.

private ClusterState clusterStateWithNoMasterBlock(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(noMasterBlockService.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 8 with DiscoveryNodes

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

the class JoinTaskExecutorTests method testUpdatesNodeWithOpenSearchVersionForExistingAndNewNodes.

public void testUpdatesNodeWithOpenSearchVersionForExistingAndNewNodes() throws Exception {
    // During the upgrade from Elasticsearch, OpenSearch node send their version as 7.10.2 to Elasticsearch master
    // in order to successfully join the cluster. But as soon as OpenSearch node becomes the master, cluster state
    // should show the OpenSearch nodes version as 1.x. As the cluster state was carry forwarded from ES master,
    // version in DiscoveryNode is stale 7.10.2.
    final AllocationService allocationService = mock(AllocationService.class);
    when(allocationService.adaptAutoExpandReplicas(any())).then(invocationOnMock -> invocationOnMock.getArguments()[0]);
    when(allocationService.disassociateDeadNodes(any(), anyBoolean(), any())).then(invocationOnMock -> invocationOnMock.getArguments()[0]);
    final RerouteService rerouteService = (reason, priority, listener) -> listener.onResponse(null);
    Map<String, Version> channelVersions = new HashMap<>();
    // OpenSearch node running BWC version
    String node_1 = UUIDs.base64UUID();
    // OpenSearch node running BWC version
    String node_2 = UUIDs.base64UUID();
    // OpenSearch node running BWC version, sending new join request and no active channel
    String node_3 = UUIDs.base64UUID();
    // ES node 7.10.2
    String node_4 = UUIDs.base64UUID();
    // ES node 7.10.2 in cluster but missing channel version
    String node_5 = UUIDs.base64UUID();
    // ES node 7.9.0
    String node_6 = UUIDs.base64UUID();
    // ES node 7.9.0 in cluster but missing channel version
    String node_7 = UUIDs.base64UUID();
    channelVersions.put(node_1, Version.CURRENT);
    channelVersions.put(node_2, Version.CURRENT);
    channelVersions.put(node_4, LegacyESVersion.V_7_10_2);
    channelVersions.put(node_6, LegacyESVersion.V_7_10_0);
    final TransportService transportService = mock(TransportService.class);
    when(transportService.getChannelVersion(any())).thenReturn(channelVersions);
    DiscoveryNodes.Builder nodes = new DiscoveryNodes.Builder().localNodeId(node_1);
    nodes.add(new DiscoveryNode(node_1, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_2));
    nodes.add(new DiscoveryNode(node_2, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_2));
    nodes.add(new DiscoveryNode(node_3, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_2));
    nodes.add(new DiscoveryNode(node_4, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_2));
    nodes.add(new DiscoveryNode(node_5, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_2));
    nodes.add(new DiscoveryNode(node_6, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_1));
    nodes.add(new DiscoveryNode(node_7, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_0));
    final ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).nodes(nodes).build();
    final JoinTaskExecutor joinTaskExecutor = new JoinTaskExecutor(Settings.EMPTY, allocationService, logger, rerouteService, transportService);
    final DiscoveryNode existing_node_3 = clusterState.nodes().get(node_3);
    final DiscoveryNode node_3_new_join = new DiscoveryNode(existing_node_3.getName(), existing_node_3.getId(), existing_node_3.getEphemeralId(), existing_node_3.getHostName(), existing_node_3.getHostAddress(), existing_node_3.getAddress(), existing_node_3.getAttributes(), existing_node_3.getRoles(), Version.CURRENT);
    final ClusterStateTaskExecutor.ClusterTasksResult<JoinTaskExecutor.Task> result = joinTaskExecutor.execute(clusterState, List.of(new JoinTaskExecutor.Task(node_3_new_join, "test"), JoinTaskExecutor.newBecomeMasterTask(), JoinTaskExecutor.newFinishElectionTask()));
    final ClusterStateTaskExecutor.TaskResult taskResult = result.executionResults.values().iterator().next();
    assertTrue(taskResult.isSuccess());
    DiscoveryNodes resultNodes = result.resultingState.getNodes();
    assertEquals(Version.CURRENT, resultNodes.get(node_1).getVersion());
    assertEquals(Version.CURRENT, resultNodes.get(node_2).getVersion());
    // 7.10.2 in old state but sent new join and processed
    assertEquals(Version.CURRENT, resultNodes.get(node_3).getVersion());
    assertEquals(LegacyESVersion.V_7_10_2, resultNodes.get(node_4).getVersion());
    // 7.10.2 node without active channel will be removed and should rejoin
    assertFalse(resultNodes.nodeExists(node_5));
    assertEquals(LegacyESVersion.V_7_10_0, resultNodes.get(node_6).getVersion());
    // 7.9.0 node without active channel but shouldn't get removed
    assertEquals(LegacyESVersion.V_7_10_0, resultNodes.get(node_7).getVersion());
}
Also used : DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) VersionUtils.maxCompatibleVersion(org.opensearch.test.VersionUtils.maxCompatibleVersion) VersionUtils.randomCompatibleVersion(org.opensearch.test.VersionUtils.randomCompatibleVersion) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) Version(org.opensearch.Version) HashMap(java.util.HashMap) VersionUtils.randomVersionBetween(org.opensearch.test.VersionUtils.randomVersionBetween) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) LegacyESVersion(org.opensearch.LegacyESVersion) VersionUtils(org.opensearch.test.VersionUtils) Mockito.anyBoolean(org.mockito.Mockito.anyBoolean) Map(java.util.Map) RerouteService(org.opensearch.cluster.routing.RerouteService) Matchers.hasSize(org.hamcrest.Matchers.hasSize) UUIDs(org.opensearch.common.UUIDs) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) VersionUtils.randomVersion(org.opensearch.test.VersionUtils.randomVersion) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) TransportService(org.opensearch.transport.TransportService) List(org.opensearch.common.collect.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ClusterName(org.opensearch.cluster.ClusterName) Mockito.any(org.mockito.Mockito.any) Mockito.mock(org.mockito.Mockito.mock) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) VersionUtils.maxCompatibleVersion(org.opensearch.test.VersionUtils.maxCompatibleVersion) VersionUtils.randomCompatibleVersion(org.opensearch.test.VersionUtils.randomCompatibleVersion) Version(org.opensearch.Version) LegacyESVersion(org.opensearch.LegacyESVersion) VersionUtils.randomVersion(org.opensearch.test.VersionUtils.randomVersion) TransportService(org.opensearch.transport.TransportService) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor) RerouteService(org.opensearch.cluster.routing.RerouteService) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 9 with DiscoveryNodes

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

the class AbstractDisruptionTestCase method assertNoMaster.

void assertNoMaster(final String node, @Nullable final ClusterBlock expectedBlocks, TimeValue maxWaitTime) throws Exception {
    assertBusy(() -> {
        ClusterState state = getNodeClusterState(node);
        final DiscoveryNodes nodes = state.nodes();
        assertNull("node [" + node + "] still has [" + nodes.getMasterNode() + "] as master", nodes.getMasterNode());
        if (expectedBlocks != null) {
            for (ClusterBlockLevel level : expectedBlocks.levels()) {
                assertTrue("node [" + node + "] does have level [" + level + "] in it's blocks", state.getBlocks().hasGlobalBlockWithLevel(level));
            }
        }
    }, maxWaitTime.getMillis(), TimeUnit.MILLISECONDS);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 10 with DiscoveryNodes

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

the class PublicationTests method testPublishingToMastersFirst.

public void testPublishingToMastersFirst() {
    VotingConfiguration singleNodeConfig = VotingConfiguration.of(n1);
    initializeCluster(singleNodeConfig);
    DiscoveryNodes.Builder discoNodesBuilder = DiscoveryNodes.builder();
    randomNodes(10).forEach(dn -> discoNodesBuilder.add(dn));
    DiscoveryNodes discoveryNodes = discoNodesBuilder.add(n1).localNodeId(n1.getId()).build();
    MockPublication publication = node1.publish(CoordinationStateTests.clusterState(1L, 2L, discoveryNodes, singleNodeConfig, singleNodeConfig, 42L), null, Collections.emptySet());
    List<DiscoveryNode> publicationTargets = new ArrayList<>(publication.pendingPublications.keySet());
    List<DiscoveryNode> sortedPublicationTargets = new ArrayList<>(publicationTargets);
    Collections.sort(sortedPublicationTargets, Comparator.comparing(n -> n.isMasterNode() == false));
    assertEquals(sortedPublicationTargets, publicationTargets);
}
Also used : DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) Arrays(java.util.Arrays) LongSupplier(java.util.function.LongSupplier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Version(org.opensearch.Version) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) Collector(java.util.stream.Collector) Matchers.empty(org.hamcrest.Matchers.empty) TimeValue(org.opensearch.common.unit.TimeValue) VotingConfiguration(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfiguration) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) Discovery(org.opensearch.discovery.Discovery) TransportResponse(org.opensearch.transport.TransportResponse) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) Collectors(java.util.stream.Collectors) Nullable(org.opensearch.common.Nullable) Tuple(org.opensearch.common.collect.Tuple) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Sets(org.opensearch.common.util.set.Sets) List(java.util.List) Matchers.emptyIterable(org.hamcrest.Matchers.emptyIterable) Stream(java.util.stream.Stream) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) TransportException(org.opensearch.transport.TransportException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) VotingConfiguration(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfiguration)

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