From cb46e97a046b61a20bc237b9d49fa30d2aef9edf Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Thu, 4 May 2017 10:41:20 -0400 Subject: [PATCH] Fix reschedule async fsync test This commit fixes the reschedule async fsync test in index service tests. This test was passing for the wrong reason. Namely, the test was trying to set translog durability to async, fire off an indexing request, and then assert that the translog eventually got fsynced. The problem here is that in the course of issuing the indexing request, a mapping update is trigger. The mapping update triggers the index settings to be refreshed. Since the test did not issue a cluster state update to change the durability from request to async but instead did this directly through index service, the mapping update flops the durability back to async. This means that when the indexing request executes, an fsync is performed after the request and the assertoin that an fsync is not needed passes but for the wrong reason (in short: the test wanted it to pass because an async fsync fired, but instead it passed because a request async fired). This commit fixes this by going through the index settings API so that a proper cluster state update is triggered and so the mapping update does not flop the durability back to request. --- .../index/IndexServiceTests.java | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/index/IndexServiceTests.java b/core/src/test/java/org/elasticsearch/index/IndexServiceTests.java index 385770426f5..c202db1470e 100644 --- a/core/src/test/java/org/elasticsearch/index/IndexServiceTests.java +++ b/core/src/test/java/org/elasticsearch/index/IndexServiceTests.java @@ -238,31 +238,43 @@ public class IndexServiceTests extends ESSingleNodeTestCase { } public void testRescheduleAsyncFsync() throws Exception { - Settings settings = Settings.builder() - .put(IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING.getKey(), "100ms") // very often :) - .put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.REQUEST) + final Settings settings = Settings.builder() + .put(IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING.getKey(), "100ms") + .put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.REQUEST) .build(); - IndexService indexService = createIndex("test", settings); + final IndexService indexService = createIndex("test", settings); ensureGreen("test"); assertNull(indexService.getFsyncTask()); - IndexMetaData metaData = IndexMetaData.builder(indexService.getMetaData()).settings(Settings.builder().put(indexService.getMetaData().getSettings()).put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.ASYNC)).build(); - indexService.updateMetaData(metaData); - assertNotNull(indexService.getFsyncTask()); - assertTrue(indexService.getRefreshTask().mustReschedule()); - client().prepareIndex("test", "test", "1").setSource("{\"foo\": \"bar\"}", XContentType.JSON).get(); - IndexShard shard = indexService.getShard(0); - assertBusy(() -> { - assertFalse(shard.getTranslog().syncNeeded()); - }); - metaData = IndexMetaData.builder(indexService.getMetaData()).settings(Settings.builder().put(indexService.getMetaData().getSettings()).put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.REQUEST)).build(); - indexService.updateMetaData(metaData); + client() + .admin() + .indices() + .prepareUpdateSettings("test") + .setSettings(Settings.builder().put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.ASYNC)) + .get(); + + assertNotNull(indexService.getFsyncTask()); + assertTrue(indexService.getFsyncTask().mustReschedule()); + client().prepareIndex("test", "test", "1").setSource("{\"foo\": \"bar\"}", XContentType.JSON).get(); + assertNotNull(indexService.getFsyncTask()); + final IndexShard shard = indexService.getShard(0); + assertBusy(() -> assertFalse(shard.getTranslog().syncNeeded())); + + client() + .admin() + .indices() + .prepareUpdateSettings("test") + .setSettings(Settings.builder().put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.REQUEST)) + .get(); assertNull(indexService.getFsyncTask()); - metaData = IndexMetaData.builder(indexService.getMetaData()).settings(Settings.builder().put(indexService.getMetaData().getSettings()).put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.ASYNC)).build(); - indexService.updateMetaData(metaData); + client() + .admin() + .indices() + .prepareUpdateSettings("test") + .setSettings(Settings.builder().put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.ASYNC)) + .get(); assertNotNull(indexService.getFsyncTask()); - } public void testIllegalFsyncInterval() {