From c4960ad73603f857a0e85cb03f09cb5a1229e238 Mon Sep 17 00:00:00 2001 From: Nhat Nguyen Date: Tue, 19 Mar 2019 21:27:08 -0400 Subject: [PATCH] Ensure flush happen before closing an index (#40184) If there's an ongoing flush triggered by the translog flush threshold, we may fail to execute a flush because waitIfOngoing is false by default. Relates to #36342 --- .../close/TransportVerifyShardBeforeCloseAction.java | 2 +- .../org/elasticsearch/indices/state/CloseIndexIT.java | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/close/TransportVerifyShardBeforeCloseAction.java b/server/src/main/java/org/elasticsearch/action/admin/indices/close/TransportVerifyShardBeforeCloseAction.java index 13269344a1a..cba01a3e9f8 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/close/TransportVerifyShardBeforeCloseAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/close/TransportVerifyShardBeforeCloseAction.java @@ -108,7 +108,7 @@ public class TransportVerifyShardBeforeCloseAction extends TransportReplicationA throw new IllegalStateException("Index shard " + shardId + " must be blocked by " + request.clusterBlock() + " before closing"); } indexShard.verifyShardBeforeIndexClosing(); - indexShard.flush(new FlushRequest().force(true)); + indexShard.flush(new FlushRequest().force(true).waitIfOngoing(true)); logger.trace("{} shard is ready for closing", shardId); } diff --git a/server/src/test/java/org/elasticsearch/indices/state/CloseIndexIT.java b/server/src/test/java/org/elasticsearch/indices/state/CloseIndexIT.java index 054da8ee7dc..8ee7d430b0a 100644 --- a/server/src/test/java/org/elasticsearch/indices/state/CloseIndexIT.java +++ b/server/src/test/java/org/elasticsearch/indices/state/CloseIndexIT.java @@ -29,7 +29,10 @@ import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaDataIndexStateService; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.ByteSizeUnit; +import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.index.IndexNotFoundException; +import org.elasticsearch.index.IndexSettings; import org.elasticsearch.indices.IndexClosedException; import org.elasticsearch.test.BackgroundIndexer; import org.elasticsearch.test.ESIntegTestCase; @@ -56,6 +59,14 @@ public class CloseIndexIT extends ESIntegTestCase { private static final int MAX_DOCS = 25_000; + @Override + public Settings indexSettings() { + Settings.builder().put(super.indexSettings()) + .put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(), + new ByteSizeValue(randomIntBetween(1, 4096), ByteSizeUnit.KB)); + return super.indexSettings(); + } + public void testCloseMissingIndex() { IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () -> client().admin().indices().prepareClose("test").get()); assertThat(e.getMessage(), is("no such index [test]"));