Search in sources :

Example 1 with InternalTestCluster

use of org.opensearch.test.InternalTestCluster 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 InternalTestCluster

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

the class ClusterInfoServiceIT method testClusterInfoServiceCollectsInformation.

public void testClusterInfoServiceCollectsInformation() {
    internalCluster().startNodes(2);
    final String indexName = randomBoolean() ? randomAlphaOfLength(5).toLowerCase(Locale.ROOT) : TEST_SYSTEM_INDEX_NAME;
    assertAcked(prepareCreate(indexName).setSettings(Settings.builder().put(Store.INDEX_STORE_STATS_REFRESH_INTERVAL_SETTING.getKey(), 0).put(EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), EnableAllocationDecider.Rebalance.NONE).put(IndexMetadata.SETTING_INDEX_HIDDEN, randomBoolean()).build()));
    if (randomBoolean()) {
        assertAcked(client().admin().indices().prepareClose(indexName));
    }
    ensureGreen(indexName);
    InternalTestCluster internalTestCluster = internalCluster();
    // Get the cluster info service on the master node
    final InternalClusterInfoService infoService = (InternalClusterInfoService) internalTestCluster.getInstance(ClusterInfoService.class, internalTestCluster.getMasterName());
    infoService.setUpdateFrequency(TimeValue.timeValueMillis(200));
    ClusterInfo info = infoService.refresh();
    assertNotNull("info should not be null", info);
    ImmutableOpenMap<String, DiskUsage> leastUsages = info.getNodeLeastAvailableDiskUsages();
    ImmutableOpenMap<String, DiskUsage> mostUsages = info.getNodeMostAvailableDiskUsages();
    ImmutableOpenMap<String, Long> shardSizes = info.shardSizes;
    assertNotNull(leastUsages);
    assertNotNull(shardSizes);
    assertThat("some usages are populated", leastUsages.values().size(), Matchers.equalTo(2));
    assertThat("some shard sizes are populated", shardSizes.values().size(), greaterThan(0));
    for (ObjectCursor<DiskUsage> usage : leastUsages.values()) {
        logger.info("--> usage: {}", usage.value);
        assertThat("usage has be retrieved", usage.value.getFreeBytes(), greaterThan(0L));
    }
    for (ObjectCursor<DiskUsage> usage : mostUsages.values()) {
        logger.info("--> usage: {}", usage.value);
        assertThat("usage has be retrieved", usage.value.getFreeBytes(), greaterThan(0L));
    }
    for (ObjectCursor<Long> size : shardSizes.values()) {
        logger.info("--> shard size: {}", size.value);
        assertThat("shard size is greater than 0", size.value, greaterThanOrEqualTo(0L));
    }
    ClusterService clusterService = internalTestCluster.getInstance(ClusterService.class, internalTestCluster.getMasterName());
    ClusterState state = clusterService.state();
    for (ShardRouting shard : state.routingTable().allShards()) {
        String dataPath = info.getDataPath(shard);
        assertNotNull(dataPath);
        String nodeId = shard.currentNodeId();
        DiscoveryNode discoveryNode = state.getNodes().get(nodeId);
        IndicesService indicesService = internalTestCluster.getInstance(IndicesService.class, discoveryNode.getName());
        IndexService indexService = indicesService.indexService(shard.index());
        IndexShard indexShard = indexService.getShardOrNull(shard.id());
        assertEquals(indexShard.shardPath().getRootDataPath().toString(), dataPath);
        assertTrue(info.getReservedSpace(nodeId, dataPath).containsShardId(shard.shardId()));
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) IndexService(org.opensearch.index.IndexService) IndexShard(org.opensearch.index.shard.IndexShard) IndicesService(org.opensearch.indices.IndicesService) InternalTestCluster(org.opensearch.test.InternalTestCluster) ClusterService(org.opensearch.cluster.service.ClusterService) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 3 with InternalTestCluster

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

the class RepositoriesServiceIT method testUpdateRepository.

public void testUpdateRepository() {
    final InternalTestCluster cluster = internalCluster();
    final String repositoryName = "test-repo";
    final Client client = client();
    final RepositoriesService repositoriesService = cluster.getDataOrMasterNodeInstances(RepositoriesService.class).iterator().next();
    final Settings.Builder repoSettings = Settings.builder().put("location", randomRepoPath());
    assertAcked(client.admin().cluster().preparePutRepository(repositoryName).setType(FsRepository.TYPE).setSettings(repoSettings).get());
    final GetRepositoriesResponse originalGetRepositoriesResponse = client.admin().cluster().prepareGetRepositories(repositoryName).get();
    assertThat(originalGetRepositoriesResponse.repositories(), hasSize(1));
    RepositoryMetadata originalRepositoryMetadata = originalGetRepositoriesResponse.repositories().get(0);
    assertThat(originalRepositoryMetadata.type(), equalTo(FsRepository.TYPE));
    final Repository originalRepository = repositoriesService.repository(repositoryName);
    assertThat(originalRepository, instanceOf(FsRepository.class));
    final boolean updated = randomBoolean();
    final String updatedRepositoryType = updated ? "mock" : FsRepository.TYPE;
    assertAcked(client.admin().cluster().preparePutRepository(repositoryName).setType(updatedRepositoryType).setSettings(repoSettings).get());
    final GetRepositoriesResponse updatedGetRepositoriesResponse = client.admin().cluster().prepareGetRepositories(repositoryName).get();
    assertThat(updatedGetRepositoriesResponse.repositories(), hasSize(1));
    final RepositoryMetadata updatedRepositoryMetadata = updatedGetRepositoriesResponse.repositories().get(0);
    assertThat(updatedRepositoryMetadata.type(), equalTo(updatedRepositoryType));
    final Repository updatedRepository = repositoriesService.repository(repositoryName);
    assertThat(updatedRepository, updated ? not(sameInstance(originalRepository)) : sameInstance(originalRepository));
}
Also used : FsRepository(org.opensearch.repositories.fs.FsRepository) MockRepository(org.opensearch.snapshots.mockstore.MockRepository) RepositoryMetadata(org.opensearch.cluster.metadata.RepositoryMetadata) InternalTestCluster(org.opensearch.test.InternalTestCluster) Client(org.opensearch.client.Client) GetRepositoriesResponse(org.opensearch.action.admin.cluster.repositories.get.GetRepositoriesResponse) Settings(org.opensearch.common.settings.Settings) FsRepository(org.opensearch.repositories.fs.FsRepository)

Example 4 with InternalTestCluster

use of org.opensearch.test.InternalTestCluster 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 5 with InternalTestCluster

use of org.opensearch.test.InternalTestCluster 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)

Aggregations

InternalTestCluster (org.opensearch.test.InternalTestCluster)13 NodeConfigurationSource (org.opensearch.test.NodeConfigurationSource)8 Path (java.nio.file.Path)4 ClusterService (org.opensearch.cluster.service.ClusterService)4 Settings (org.opensearch.common.settings.Settings)4 ArrayList (java.util.ArrayList)3 Client (org.opensearch.client.Client)3 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)3 DiscoverySettings (org.opensearch.node.Node.DiscoverySettings)3 HashMap (java.util.HashMap)2 Random (java.util.Random)2 ClusterState (org.opensearch.cluster.ClusterState)2 ShardRouting (org.opensearch.cluster.routing.ShardRouting)2 Plugin (org.opensearch.plugins.Plugin)2 MockRepository (org.opensearch.snapshots.mockstore.MockRepository)2 MockHttpTransport (org.opensearch.test.MockHttpTransport)2 TransportService (org.opensearch.transport.TransportService)2 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Arrays (java.util.Arrays)1