Skip to content

Commit 8c8b1dc

Browse files
committed
Fix index with unknown setting test
This commit fixes the test of an index with an unknown setting. The problem here is that we were manipulating the index state on disk, but a cluster state update could arrive between us manipulating the index state on disk and us restarting the node, leading to the index state that we just intentionally broke being fixed. As such, after restart, the index state would not be in the state that we expected it to be in and the test would fail. To address this, we hook into the restart and break the index state immediately before the node is started again. Relates elastic#26995
1 parent d1acb76 commit 8c8b1dc

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

core/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.action.admin.indices.create;
2121

22+
import com.carrotsearch.hppc.cursors.ObjectCursor;
2223
import org.elasticsearch.ElasticsearchException;
2324
import org.elasticsearch.action.ActionListener;
2425
import org.elasticsearch.action.UnavailableShardsException;
@@ -30,6 +31,7 @@
3031
import org.elasticsearch.cluster.ClusterState;
3132
import org.elasticsearch.cluster.metadata.IndexMetaData;
3233
import org.elasticsearch.cluster.metadata.MetaData;
34+
import org.elasticsearch.cluster.node.DiscoveryNode;
3335
import org.elasticsearch.common.collect.ImmutableOpenMap;
3436
import org.elasticsearch.common.settings.Settings;
3537
import org.elasticsearch.common.unit.TimeValue;
@@ -39,8 +41,11 @@
3941
import org.elasticsearch.test.ESIntegTestCase;
4042
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
4143
import org.elasticsearch.test.ESIntegTestCase.Scope;
44+
import org.elasticsearch.test.InternalTestCluster;
4245

4346
import java.util.HashMap;
47+
import java.util.HashSet;
48+
import java.util.Set;
4449
import java.util.concurrent.CountDownLatch;
4550
import java.util.concurrent.atomic.AtomicInteger;
4651
import java.util.function.BiFunction;
@@ -355,17 +360,29 @@ public void testIndexWithUnknownSetting() throws Exception {
355360
client().admin().indices().prepareCreate("test").setSettings(settings).get();
356361
ensureGreen("test");
357362
final ClusterState state = client().admin().cluster().prepareState().get().getState();
358-
final IndexMetaData metaData = state.getMetaData().index("test");
359-
for (final NodeEnvironment services : internalCluster().getInstances(NodeEnvironment.class)) {
360-
final IndexMetaData brokenMetaData =
361-
IndexMetaData
362-
.builder(metaData)
363-
.settings(Settings.builder().put(metaData.getSettings()).put("index.foo", "true"))
364-
.build();
365-
// so evil
366-
IndexMetaData.FORMAT.write(brokenMetaData, services.indexPaths(brokenMetaData.getIndex()));
363+
364+
final Set<String> dataOrMasterNodeNames = new HashSet<>();
365+
for (final ObjectCursor<DiscoveryNode> node : state.nodes().getMasterAndDataNodes().values()) {
366+
assertTrue(dataOrMasterNodeNames.add(node.value.getName()));
367367
}
368-
internalCluster().fullRestart();
368+
369+
final IndexMetaData metaData = state.getMetaData().index("test");
370+
internalCluster().fullRestart(new InternalTestCluster.RestartCallback() {
371+
@Override
372+
public Settings onNodeStopped(String nodeName) throws Exception {
373+
if (dataOrMasterNodeNames.contains(nodeName)) {
374+
final NodeEnvironment nodeEnvironment = internalCluster().getInstance(NodeEnvironment.class, nodeName);
375+
final IndexMetaData brokenMetaData =
376+
IndexMetaData
377+
.builder(metaData)
378+
.settings(Settings.builder().put(metaData.getSettings()).put("index.foo", true))
379+
.build();
380+
// so evil
381+
IndexMetaData.FORMAT.write(brokenMetaData, nodeEnvironment.indexPaths(brokenMetaData.getIndex()));
382+
}
383+
return Settings.EMPTY;
384+
}
385+
});
369386
ensureGreen(metaData.getIndex().getName()); // we have to wait for the index to show up in the metadata or we will fail in a race
370387
final ClusterState stateAfterRestart = client().admin().cluster().prepareState().get().getState();
371388

0 commit comments

Comments
 (0)