|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.gateway; |
| 20 | + |
| 21 | +import org.elasticsearch.client.Requests; |
| 22 | +import org.elasticsearch.cluster.health.ClusterHealthStatus; |
| 23 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 24 | +import org.elasticsearch.common.settings.Settings; |
| 25 | +import org.elasticsearch.core.internal.io.IOUtils; |
| 26 | +import org.elasticsearch.env.NodeEnvironment; |
| 27 | +import org.elasticsearch.test.ESIntegTestCase; |
| 28 | +import org.elasticsearch.test.InternalTestCluster; |
| 29 | + |
| 30 | +import java.nio.file.Path; |
| 31 | + |
| 32 | +import static org.hamcrest.Matchers.equalTo; |
| 33 | +import static org.hamcrest.Matchers.startsWith; |
| 34 | + |
| 35 | + |
| 36 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 37 | +public class DanglingIndicesIT extends ESIntegTestCase { |
| 38 | + |
| 39 | + public void testDanglingIndices() throws Exception { |
| 40 | + // replicating the case described in https://github.com/elastic/elasticsearch/issues/27073 |
| 41 | + final String indexName = "simple1"; |
| 42 | + |
| 43 | + final InternalTestCluster cluster = internalCluster(); |
| 44 | + String node1 = cluster.startNode(); |
| 45 | + String node2 = cluster.startNode(); |
| 46 | + |
| 47 | + final boolean allocateOnNode2 = randomBoolean(); |
| 48 | + |
| 49 | + createIndex(indexName, |
| 50 | + Settings.builder() |
| 51 | + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) |
| 52 | + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) |
| 53 | + // explicitly specify shard location |
| 54 | + .put("index.routing.allocation.include._name", allocateOnNode2 ? node2 : node1) |
| 55 | + .build()); |
| 56 | + ensureGreen(indexName); |
| 57 | + |
| 58 | + if (randomBoolean()) { |
| 59 | + index(indexName, "_doc", "1", "f", "f"); |
| 60 | + } |
| 61 | + |
| 62 | + final NodeEnvironment env = internalCluster().getInstance(NodeEnvironment.class, node1); |
| 63 | + final Path[] node1DataPaths = env.nodeDataPaths(); |
| 64 | + |
| 65 | + cluster.stopRandomNode(InternalTestCluster.nameFilter(node1)); |
| 66 | + cluster.stopRandomNode(InternalTestCluster.nameFilter(node2)); |
| 67 | + |
| 68 | + // clean up data path on node1 to trigger dangling index being picked up |
| 69 | + IOUtils.rm(node1DataPaths); |
| 70 | + |
| 71 | + // start node that could have dangling index |
| 72 | + node1 = cluster.startNode(); |
| 73 | + try { |
| 74 | + node2 = randomBoolean() ? cluster.startMasterOnlyNode() : cluster.startCoordinatingOnlyNode(Settings.EMPTY); |
| 75 | + assertThat("Node 2 has to fail as it contains shard data", |
| 76 | + allocateOnNode2, equalTo(false)); |
| 77 | + assertThat("index exists but has to be red", |
| 78 | + client().admin().cluster().health(Requests.clusterHealthRequest(indexName)).get().getStatus(), |
| 79 | + equalTo(ClusterHealthStatus.RED)); |
| 80 | + } catch (IllegalStateException e) { |
| 81 | + assertThat(allocateOnNode2, equalTo(true)); |
| 82 | + assertThat(e.getMessage(), startsWith("Non data node cannot have dangling indices")); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments