|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.ccr.action; |
| 8 | + |
| 9 | +import org.elasticsearch.Version; |
| 10 | +import org.elasticsearch.client.Client; |
| 11 | +import org.elasticsearch.cluster.ClusterName; |
| 12 | +import org.elasticsearch.cluster.ClusterState; |
| 13 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 14 | +import org.elasticsearch.cluster.node.DiscoveryNodeRole; |
| 15 | +import org.elasticsearch.cluster.node.DiscoveryNodes; |
| 16 | +import org.elasticsearch.cluster.service.ClusterService; |
| 17 | +import org.elasticsearch.common.UUIDs; |
| 18 | +import org.elasticsearch.common.settings.ClusterSettings; |
| 19 | +import org.elasticsearch.common.settings.Settings; |
| 20 | +import org.elasticsearch.common.settings.SettingsModule; |
| 21 | +import org.elasticsearch.persistent.PersistentTasksCustomMetaData.Assignment; |
| 22 | +import org.elasticsearch.test.ESTestCase; |
| 23 | +import org.elasticsearch.threadpool.ThreadPool; |
| 24 | +import org.elasticsearch.xpack.ccr.CcrSettings; |
| 25 | + |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.HashSet; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.function.BiConsumer; |
| 31 | +import java.util.function.Supplier; |
| 32 | + |
| 33 | +import static org.hamcrest.Matchers.equalTo; |
| 34 | +import static org.mockito.Mockito.mock; |
| 35 | +import static org.mockito.Mockito.when; |
| 36 | + |
| 37 | +public class ShardFollowTasksExecutorAssignmentTests extends ESTestCase { |
| 38 | + |
| 39 | + public void testAssignmentToNodeWithDataAndRemoteClusterClientRoles() { |
| 40 | + runAssignmentTest( |
| 41 | + new HashSet<>(Arrays.asList(DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE)), |
| 42 | + randomIntBetween(0, 8), |
| 43 | + () -> new HashSet<>(randomSubsetOf(new HashSet<>(Arrays.asList(DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.INGEST_ROLE)))), |
| 44 | + (theSpecial, assignment) -> { |
| 45 | + assertTrue(assignment.isAssigned()); |
| 46 | + assertThat(assignment.getExecutorNode(), equalTo(theSpecial.getId())); |
| 47 | + } |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public void testDataRoleWithoutRemoteClusterServiceRole() { |
| 52 | + runNoAssignmentTest(Collections.singleton(DiscoveryNodeRole.DATA_ROLE)); |
| 53 | + } |
| 54 | + |
| 55 | + public void testRemoteClusterClientRoleWithoutDataRole() { |
| 56 | + runNoAssignmentTest(Collections.singleton(DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE)); |
| 57 | + } |
| 58 | + |
| 59 | + private void runNoAssignmentTest(final Set<DiscoveryNodeRole> roles) { |
| 60 | + runAssignmentTest( |
| 61 | + roles, |
| 62 | + 0, |
| 63 | + Collections::emptySet, |
| 64 | + (theSpecial, assignment) -> { |
| 65 | + assertFalse(assignment.isAssigned()); |
| 66 | + assertThat(assignment.getExplanation(), equalTo("no nodes found with data and remote cluster client roles")); |
| 67 | + } |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + private void runAssignmentTest( |
| 72 | + final Set<DiscoveryNodeRole> theSpecialRoles, |
| 73 | + final int numberOfOtherNodes, |
| 74 | + final Supplier<Set<DiscoveryNodeRole>> otherNodesRolesSupplier, |
| 75 | + final BiConsumer<DiscoveryNode, Assignment> consumer |
| 76 | + ) { |
| 77 | + final ClusterService clusterService = mock(ClusterService.class); |
| 78 | + when(clusterService.getClusterSettings()) |
| 79 | + .thenReturn(new ClusterSettings(Settings.EMPTY, Collections.singleton(CcrSettings.CCR_WAIT_FOR_METADATA_TIMEOUT))); |
| 80 | + final SettingsModule settingsModule = mock(SettingsModule.class); |
| 81 | + when(settingsModule.getSettings()).thenReturn(Settings.EMPTY); |
| 82 | + final ShardFollowTasksExecutor executor = |
| 83 | + new ShardFollowTasksExecutor(mock(Client.class), mock(ThreadPool.class), clusterService, settingsModule); |
| 84 | + final ClusterState.Builder clusterStateBuilder = ClusterState.builder(new ClusterName("test")); |
| 85 | + final DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(); |
| 86 | + final DiscoveryNode theSpecial = newNode(theSpecialRoles); |
| 87 | + nodesBuilder.add(theSpecial); |
| 88 | + for (int i = 0; i < numberOfOtherNodes; i++) { |
| 89 | + nodesBuilder.add(newNode(otherNodesRolesSupplier.get())); |
| 90 | + } |
| 91 | + clusterStateBuilder.nodes(nodesBuilder); |
| 92 | + final Assignment assignment = executor.getAssignment(mock(ShardFollowTask.class), clusterStateBuilder.build()); |
| 93 | + consumer.accept(theSpecial, assignment); |
| 94 | + } |
| 95 | + |
| 96 | + private static DiscoveryNode newNode(final Set<DiscoveryNodeRole> roles) { |
| 97 | + return new DiscoveryNode( |
| 98 | + "node_" + UUIDs.randomBase64UUID(random()), |
| 99 | + buildNewFakeTransportAddress(), |
| 100 | + Collections.emptyMap(), |
| 101 | + roles, |
| 102 | + Version.CURRENT |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments