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");
}
});
}
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));
}
Aggregations