Search in sources :

Example 6 with RestartCallback

use of org.elasticsearch.test.InternalTestCluster.RestartCallback in project elasticsearch by elastic.

the class GatewayIndexStateIT method testIndexDeletionWhenNodeRejoins.

/**
     * This test ensures that when an index deletion takes place while a node is offline, when that
     * node rejoins the cluster, it deletes the index locally instead of importing it as a dangling index.
     */
public void testIndexDeletionWhenNodeRejoins() throws Exception {
    final String indexName = "test-index-del-on-node-rejoin-idx";
    final int numNodes = 2;
    final List<String> nodes;
    if (randomBoolean()) {
        // test with a regular index
        logger.info("--> starting a cluster with " + numNodes + " nodes");
        nodes = internalCluster().startNodes(numNodes);
        logger.info("--> create an index");
        createIndex(indexName);
    } else {
        // test with a shadow replica index
        final Path dataPath = createTempDir();
        logger.info("--> created temp data path for shadow replicas [{}]", dataPath);
        logger.info("--> starting a cluster with " + numNodes + " nodes");
        final Settings nodeSettings = Settings.builder().put("node.add_lock_id_to_custom_path", false).put(Environment.PATH_SHARED_DATA_SETTING.getKey(), dataPath.toString()).put("index.store.fs.fs_lock", randomFrom("native", "simple")).build();
        nodes = internalCluster().startNodes(numNodes, nodeSettings);
        logger.info("--> create a shadow replica index");
        createShadowReplicaIndex(indexName, dataPath, numNodes - 1);
    }
    logger.info("--> waiting for green status");
    ensureGreen();
    final String indexUUID = resolveIndex(indexName).getUUID();
    logger.info("--> restart a random date node, deleting the index in between stopping and restarting");
    internalCluster().restartRandomDataNode(new RestartCallback() {

        @Override
        public Settings onNodeStopped(final String nodeName) throws Exception {
            nodes.remove(nodeName);
            logger.info("--> stopped node[{}], remaining nodes {}", nodeName, nodes);
            assert nodes.size() > 0;
            final String otherNode = nodes.get(0);
            logger.info("--> delete index and verify it is deleted");
            final Client client = client(otherNode);
            client.admin().indices().prepareDelete(indexName).execute().actionGet();
            assertFalse(client.admin().indices().prepareExists(indexName).execute().actionGet().isExists());
            return super.onNodeStopped(nodeName);
        }
    });
    logger.info("--> wait until all nodes are back online");
    client().admin().cluster().health(Requests.clusterHealthRequest().waitForEvents(Priority.LANGUID).waitForNodes(Integer.toString(numNodes))).actionGet();
    logger.info("--> waiting for green status");
    ensureGreen();
    logger.info("--> verify that the deleted index is removed from the cluster and not reimported as dangling by the restarted node");
    assertFalse(client().admin().indices().prepareExists(indexName).execute().actionGet().isExists());
    assertBusy(() -> {
        final NodeEnvironment nodeEnv = internalCluster().getInstance(NodeEnvironment.class);
        try {
            assertFalse("index folder " + indexUUID + " should be deleted", nodeEnv.availableIndexFolders().contains(indexUUID));
        } catch (IOException e) {
            logger.error("Unable to retrieve available index folders from the node", e);
            fail("Unable to retrieve available index folders from the node");
        }
    });
}
Also used : Path(java.nio.file.Path) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) RestartCallback(org.elasticsearch.test.InternalTestCluster.RestartCallback) Client(org.elasticsearch.client.Client) Settings(org.elasticsearch.common.settings.Settings) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexClosedException(org.elasticsearch.indices.IndexClosedException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IOException(java.io.IOException)

Example 7 with RestartCallback

use of org.elasticsearch.test.InternalTestCluster.RestartCallback in project elasticsearch by elastic.

the class GatewayIndexStateIT method testDanglingIndices.

public void testDanglingIndices() throws Exception {
    logger.info("--> starting two nodes");
    final String node_1 = internalCluster().startNodes(2).get(0);
    logger.info("--> indexing a simple document");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1").setRefreshPolicy(IMMEDIATE).get();
    logger.info("--> waiting for green status");
    ensureGreen();
    logger.info("--> verify 1 doc in the index");
    for (int i = 0; i < 10; i++) {
        assertHitCount(client().prepareSearch().setQuery(matchAllQuery()).get(), 1L);
    }
    assertThat(client().prepareGet("test", "type1", "1").execute().actionGet().isExists(), equalTo(true));
    logger.info("--> restarting the nodes");
    internalCluster().fullRestart(new RestartCallback() {

        @Override
        public boolean clearData(String nodeName) {
            return node_1.equals(nodeName);
        }
    });
    logger.info("--> waiting for green status");
    ensureGreen();
    // spin a bit waiting for the index to exists
    long time = System.currentTimeMillis();
    while ((System.currentTimeMillis() - time) < TimeValue.timeValueSeconds(10).millis()) {
        if (client().admin().indices().prepareExists("test").execute().actionGet().isExists()) {
            break;
        }
    }
    logger.info("--> verify that the dangling index exists");
    assertThat(client().admin().indices().prepareExists("test").execute().actionGet().isExists(), equalTo(true));
    logger.info("--> waiting for green status");
    ensureGreen();
    logger.info("--> verify the doc is there");
    assertThat(client().prepareGet("test", "type1", "1").execute().actionGet().isExists(), equalTo(true));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) RestartCallback(org.elasticsearch.test.InternalTestCluster.RestartCallback)

Aggregations

RestartCallback (org.elasticsearch.test.InternalTestCluster.RestartCallback)7 Settings (org.elasticsearch.common.settings.Settings)5 Path (java.nio.file.Path)2 Client (org.elasticsearch.client.Client)2 NodeEnvironment (org.elasticsearch.env.NodeEnvironment)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)1 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)1 GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)1 RecoveryResponse (org.elasticsearch.action.admin.indices.recovery.RecoveryResponse)1 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 ClusterService (org.elasticsearch.cluster.service.ClusterService)1 Index (org.elasticsearch.index.Index)1 MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)1 ShardId (org.elasticsearch.index.shard.ShardId)1 ShardPath (org.elasticsearch.index.shard.ShardPath)1