Search in sources :

Example 1 with NodeConfigurationSource

use of org.opensearch.test.NodeConfigurationSource in project OpenSearch by opensearch-project.

the class SingleNodeDiscoveryIT method testCannotJoinNodeWithSingleNodeDiscovery.

public void testCannotJoinNodeWithSingleNodeDiscovery() throws Exception {
    Logger clusterLogger = LogManager.getLogger(JoinHelper.class);
    try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(clusterLogger)) {
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test", JoinHelper.class.getCanonicalName(), Level.INFO, "failed to join") {

            @Override
            public boolean innerMatch(final LogEvent event) {
                return event.getThrown() != null && event.getThrown().getClass() == RemoteTransportException.class && event.getThrown().getCause() != null && event.getThrown().getCause().getClass() == IllegalStateException.class && event.getThrown().getCause().getMessage().contains("cannot join node with [discovery.type] set to [single-node]");
            }
        });
        final TransportService service = internalCluster().getInstance(TransportService.class);
        final int port = service.boundAddress().publishAddress().getPort();
        final NodeConfigurationSource configurationSource = new NodeConfigurationSource() {

            @Override
            public Settings nodeSettings(int nodeOrdinal) {
                return Settings.builder().put("discovery.type", "zen").put("transport.type", getTestTransportType()).put(DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING.getKey(), "0s").put("transport.port", port + "-" + (port + 5 - 1)).build();
            }

            @Override
            public Path nodeConfigPath(int nodeOrdinal) {
                return null;
            }
        };
        try (InternalTestCluster other = new InternalTestCluster(randomLong(), createTempDir(), false, false, 1, 1, internalCluster().getClusterName(), configurationSource, 0, "other", Arrays.asList(getTestTransportPlugin(), MockHttpTransport.TestPlugin.class), Function.identity())) {
            other.beforeTest(random());
            final ClusterState first = internalCluster().getInstance(ClusterService.class).state();
            assertThat(first.nodes().getSize(), equalTo(1));
            assertBusy(() -> mockAppender.assertAllExpectationsMatched());
        }
    }
}
Also used : RemoteTransportException(org.opensearch.transport.RemoteTransportException) ClusterState(org.opensearch.cluster.ClusterState) MockLogAppender(org.opensearch.test.MockLogAppender) LogEvent(org.apache.logging.log4j.core.LogEvent) InternalTestCluster(org.opensearch.test.InternalTestCluster) NodeConfigurationSource(org.opensearch.test.NodeConfigurationSource) Logger(org.apache.logging.log4j.Logger) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService)

Example 2 with NodeConfigurationSource

use of org.opensearch.test.NodeConfigurationSource in project OpenSearch by opensearch-project.

the class MultiClusterRepoAccessIT method startSecondCluster.

@Before
public void startSecondCluster() throws IOException, InterruptedException {
    repoPath = randomRepoPath();
    secondCluster = new InternalTestCluster(randomLong(), createTempDir(), true, true, 0, 0, "second_cluster", new NodeConfigurationSource() {

        @Override
        public Settings nodeSettings(int nodeOrdinal) {
            return Settings.builder().put(MultiClusterRepoAccessIT.this.nodeSettings(nodeOrdinal)).put(NetworkModule.TRANSPORT_TYPE_KEY, getTestTransportType()).put(Environment.PATH_REPO_SETTING.getKey(), repoPath).build();
        }

        @Override
        public Path nodeConfigPath(int nodeOrdinal) {
            return null;
        }
    }, 0, "leader", Arrays.asList(OpenSearchIntegTestCase.TestSeedPlugin.class, MockHttpTransport.TestPlugin.class, MockTransportService.TestPlugin.class, MockNioTransportPlugin.class, InternalSettingsPlugin.class, MockRepository.Plugin.class), Function.identity());
    secondCluster.beforeTest(random());
}
Also used : OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) MockHttpTransport(org.opensearch.test.MockHttpTransport) MockRepository(org.opensearch.snapshots.mockstore.MockRepository) MockTransportService(org.opensearch.test.transport.MockTransportService) InternalTestCluster(org.opensearch.test.InternalTestCluster) NodeConfigurationSource(org.opensearch.test.NodeConfigurationSource) Before(org.junit.Before)

Example 3 with NodeConfigurationSource

use of org.opensearch.test.NodeConfigurationSource in project OpenSearch by opensearch-project.

the class InternalTestClusterTests method testTwoNodeCluster.

public void testTwoNodeCluster() throws Exception {
    NodeConfigurationSource nodeConfigurationSource = new NodeConfigurationSource() {

        @Override
        public Settings nodeSettings(int nodeOrdinal) {
            return Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, getTestTransportType()).putList(DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), "file").putList(SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING.getKey()).build();
        }

        @Override
        public Path nodeConfigPath(int nodeOrdinal) {
            return null;
        }
    };
    String nodePrefix = "test";
    Path baseDir = createTempDir();
    List<Class<? extends Plugin>> plugins = new ArrayList<>(mockPlugins());
    plugins.add(NodeAttrCheckPlugin.class);
    InternalTestCluster cluster = new InternalTestCluster(randomLong(), baseDir, false, true, 2, 2, "test", nodeConfigurationSource, 0, nodePrefix, plugins, Function.identity());
    try {
        cluster.beforeTest(random());
        switch(randomInt(2)) {
            case 0:
                cluster.stopRandomDataNode();
                cluster.startNode();
                break;
            case 1:
                cluster.rollingRestart(InternalTestCluster.EMPTY_CALLBACK);
                break;
            case 2:
                cluster.fullRestart();
                break;
        }
    } finally {
        cluster.close();
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) InternalTestCluster(org.opensearch.test.InternalTestCluster) NodeConfigurationSource(org.opensearch.test.NodeConfigurationSource) Plugin(org.opensearch.plugins.Plugin)

Example 4 with NodeConfigurationSource

use of org.opensearch.test.NodeConfigurationSource in project OpenSearch by opensearch-project.

the class InternalTestClusterTests method testDifferentRolesMaintainPathOnRestart.

public void testDifferentRolesMaintainPathOnRestart() throws Exception {
    final Path baseDir = createTempDir();
    final int numNodes = 5;
    InternalTestCluster cluster = new InternalTestCluster(randomLong(), baseDir, false, false, 0, 0, "test", new NodeConfigurationSource() {

        @Override
        public Settings nodeSettings(int nodeOrdinal) {
            return Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, getTestTransportType()).put(DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING.getKey(), 0).putList(DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), "file").putList(SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING.getKey()).build();
        }

        @Override
        public Path nodeConfigPath(int nodeOrdinal) {
            return null;
        }
    }, 0, "", mockPlugins(), Function.identity());
    cluster.beforeTest(random());
    List<DiscoveryNodeRole> roles = new ArrayList<>();
    for (int i = 0; i < numNodes; i++) {
        final DiscoveryNodeRole role = i == numNodes - 1 && roles.contains(DiscoveryNodeRole.MASTER_ROLE) == false ? DiscoveryNodeRole.MASTER_ROLE : // last node and still no master
        randomFrom(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.INGEST_ROLE);
        roles.add(role);
    }
    cluster.setBootstrapMasterNodeIndex(randomIntBetween(0, (int) roles.stream().filter(role -> role.equals(DiscoveryNodeRole.MASTER_ROLE)).count() - 1));
    try {
        Map<DiscoveryNodeRole, Set<String>> pathsPerRole = new HashMap<>();
        for (int i = 0; i < numNodes; i++) {
            final DiscoveryNodeRole role = roles.get(i);
            final String node;
            if (role == DiscoveryNodeRole.MASTER_ROLE) {
                node = cluster.startMasterOnlyNode();
            } else if (role == DiscoveryNodeRole.DATA_ROLE) {
                node = cluster.startDataOnlyNode();
            } else if (role == DiscoveryNodeRole.INGEST_ROLE) {
                node = cluster.startCoordinatingOnlyNode(Settings.EMPTY);
            } else {
                throw new IllegalStateException("get your story straight");
            }
            Set<String> rolePaths = pathsPerRole.computeIfAbsent(role, k -> new HashSet<>());
            for (Path path : getNodePaths(cluster, node)) {
                assertTrue(rolePaths.add(path.toString()));
            }
        }
        cluster.validateClusterFormed();
        cluster.fullRestart();
        Map<DiscoveryNodeRole, Set<String>> result = new HashMap<>();
        for (String name : cluster.getNodeNames()) {
            DiscoveryNode node = cluster.getInstance(ClusterService.class, name).localNode();
            List<String> paths = Arrays.stream(getNodePaths(cluster, name)).map(Path::toString).collect(Collectors.toList());
            if (node.isMasterNode()) {
                result.computeIfAbsent(DiscoveryNodeRole.MASTER_ROLE, k -> new HashSet<>()).addAll(paths);
            } else if (node.isDataNode()) {
                result.computeIfAbsent(DiscoveryNodeRole.DATA_ROLE, k -> new HashSet<>()).addAll(paths);
            } else {
                result.computeIfAbsent(DiscoveryNodeRole.INGEST_ROLE, k -> new HashSet<>()).addAll(paths);
            }
        }
        assertThat(result.size(), equalTo(pathsPerRole.size()));
        for (DiscoveryNodeRole role : result.keySet()) {
            assertThat("path are not the same for " + role, result.get(role), equalTo(pathsPerRole.get(role)));
        }
    } finally {
        cluster.close();
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) Matchers.not(org.hamcrest.Matchers.not) HashMap(java.util.HashMap) Random(java.util.Random) InternalTestCluster(org.opensearch.test.InternalTestCluster) Function(java.util.function.Function) ArrayList(java.util.ArrayList) MockHttpTransport(org.opensearch.test.MockHttpTransport) HashSet(java.util.HashSet) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) NetworkModule(org.opensearch.common.network.NetworkModule) OpenSearchAssertions.assertFileNotExists(org.opensearch.test.hamcrest.OpenSearchAssertions.assertFileNotExists) Map(java.util.Map) OpenSearchAssertions.assertFileExists(org.opensearch.test.hamcrest.OpenSearchAssertions.assertFileExists) Path(java.nio.file.Path) Environment(org.opensearch.env.Environment) NodeEnvironment(org.opensearch.env.NodeEnvironment) Client(org.opensearch.client.Client) Iterator(java.util.Iterator) Files(java.nio.file.Files) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Collection(java.util.Collection) Set(java.util.Set) DISCOVERY_SEED_PROVIDERS_SETTING(org.opensearch.discovery.DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) SettingsBasedSeedHostsProvider(org.opensearch.discovery.SettingsBasedSeedHostsProvider) DiscoverySettings(org.opensearch.node.Node.DiscoverySettings) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) Collectors(java.util.stream.Collectors) Plugin(org.opensearch.plugins.Plugin) IOUtils(org.opensearch.core.internal.io.IOUtils) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ClusterService(org.opensearch.cluster.service.ClusterService) NodeConfigurationSource(org.opensearch.test.NodeConfigurationSource) LuceneTestCase(org.apache.lucene.util.LuceneTestCase) Collections(java.util.Collections) DiscoveryModule(org.opensearch.discovery.DiscoveryModule) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashSet(java.util.HashSet) Set(java.util.Set) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalTestCluster(org.opensearch.test.InternalTestCluster) NodeConfigurationSource(org.opensearch.test.NodeConfigurationSource) ClusterService(org.opensearch.cluster.service.ClusterService) Settings(org.opensearch.common.settings.Settings) DiscoverySettings(org.opensearch.node.Node.DiscoverySettings) HashSet(java.util.HashSet)

Example 5 with NodeConfigurationSource

use of org.opensearch.test.NodeConfigurationSource in project OpenSearch by opensearch-project.

the class InternalTestClusterTests method testBeforeTest.

public void testBeforeTest() throws Exception {
    final boolean autoManageMinMasterNodes = randomBoolean();
    long clusterSeed = randomLong();
    final boolean masterNodes;
    final int minNumDataNodes;
    final int maxNumDataNodes;
    final int bootstrapMasterNodeIndex;
    if (autoManageMinMasterNodes) {
        masterNodes = randomBoolean();
        minNumDataNodes = randomIntBetween(0, 3);
        maxNumDataNodes = randomIntBetween(minNumDataNodes, 4);
        bootstrapMasterNodeIndex = -1;
    } else {
        // if we manage min master nodes, we need to lock down the number of nodes
        minNumDataNodes = randomIntBetween(0, 4);
        maxNumDataNodes = minNumDataNodes;
        masterNodes = false;
        bootstrapMasterNodeIndex = maxNumDataNodes == 0 ? -1 : randomIntBetween(0, maxNumDataNodes - 1);
    }
    final int numClientNodes = randomIntBetween(0, 2);
    NodeConfigurationSource nodeConfigurationSource = new NodeConfigurationSource() {

        @Override
        public Settings nodeSettings(int nodeOrdinal) {
            final Settings.Builder settings = Settings.builder().put(DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), "file").putList(SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING.getKey()).put(NetworkModule.TRANSPORT_TYPE_KEY, getTestTransportType());
            if (autoManageMinMasterNodes == false) {
                assert minNumDataNodes == maxNumDataNodes;
                assert masterNodes == false;
            }
            return settings.build();
        }

        @Override
        public Path nodeConfigPath(int nodeOrdinal) {
            return null;
        }
    };
    String nodePrefix = "foobar";
    InternalTestCluster cluster0 = new InternalTestCluster(clusterSeed, createTempDir(), masterNodes, autoManageMinMasterNodes, minNumDataNodes, maxNumDataNodes, "clustername", nodeConfigurationSource, numClientNodes, nodePrefix, mockPlugins(), Function.identity());
    cluster0.setBootstrapMasterNodeIndex(bootstrapMasterNodeIndex);
    InternalTestCluster cluster1 = new InternalTestCluster(clusterSeed, createTempDir(), masterNodes, autoManageMinMasterNodes, minNumDataNodes, maxNumDataNodes, "clustername", nodeConfigurationSource, numClientNodes, nodePrefix, mockPlugins(), Function.identity());
    cluster1.setBootstrapMasterNodeIndex(bootstrapMasterNodeIndex);
    assertClusters(cluster0, cluster1, false);
    long seed = randomLong();
    try {
        {
            Random random = new Random(seed);
            cluster0.beforeTest(random);
        }
        {
            Random random = new Random(seed);
            cluster1.beforeTest(random);
        }
        assertArrayEquals(cluster0.getNodeNames(), cluster1.getNodeNames());
        Iterator<Client> iterator1 = cluster1.getClients().iterator();
        for (Client client : cluster0.getClients()) {
            assertTrue(iterator1.hasNext());
            Client other = iterator1.next();
            assertSettings(client.settings(), other.settings(), false);
        }
        cluster0.afterTest();
        cluster1.afterTest();
    } finally {
        IOUtils.close(cluster0, cluster1);
    }
}
Also used : Random(java.util.Random) InternalTestCluster(org.opensearch.test.InternalTestCluster) NodeConfigurationSource(org.opensearch.test.NodeConfigurationSource) Client(org.opensearch.client.Client) Settings(org.opensearch.common.settings.Settings) DiscoverySettings(org.opensearch.node.Node.DiscoverySettings)

Aggregations

InternalTestCluster (org.opensearch.test.InternalTestCluster)8 NodeConfigurationSource (org.opensearch.test.NodeConfigurationSource)8 Path (java.nio.file.Path)4 ClusterService (org.opensearch.cluster.service.ClusterService)3 Settings (org.opensearch.common.settings.Settings)3 DiscoverySettings (org.opensearch.node.Node.DiscoverySettings)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Random (java.util.Random)2 Client (org.opensearch.client.Client)2 ClusterState (org.opensearch.cluster.ClusterState)2 Plugin (org.opensearch.plugins.Plugin)2 MockHttpTransport (org.opensearch.test.MockHttpTransport)2 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1