From eea72a6d8652dac931b42fb075740753bad219ea Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Mon, 30 Nov 2015 18:24:03 -0500 Subject: [PATCH] Preserve existing mappings on batch mapping updates This commit addresses an issues introduced in #14899 to apply mapping updates in batches. The issue is that an existing mapping for a type could be lost if that type came in a batch that already contained a mapping update for another type on the same index. The underlying issue was that the existing mapping would not be merged in because the merging logic was only tripped once per index, rather than for all types seeing updates for each index. Resolving this issue is simply a matter of ensuring that all existing types seeing updates are merged in. Closes #15129 --- .../cluster/metadata/MetaDataMappingService.java | 14 +++++++++----- .../mapping/UpdateMappingIntegrationIT.java | 1 - 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java index fb6ed1f0753..d19a087faa6 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java @@ -221,9 +221,8 @@ public class MetaDataMappingService extends AbstractComponent { class PutMappingExecutor implements ClusterStateTaskExecutor { @Override public BatchResult execute(ClusterState currentState, List tasks) throws Exception { - List indicesToClose = new ArrayList<>(); + Set indicesToClose = new HashSet<>(); BatchResult.Builder builder = BatchResult.builder(); - Map executionResults = new HashMap<>(); try { // precreate incoming indices; for (PutMappingClusterStateUpdateRequest request : tasks) { @@ -231,10 +230,15 @@ public class MetaDataMappingService extends AbstractComponent { for (String index : request.indices()) { if (currentState.metaData().hasIndex(index)) { // if we don't have the index, we will throw exceptions later; - if (indicesService.hasIndex(index) == false) { + if (indicesService.hasIndex(index) == false || indicesToClose.contains(index)) { final IndexMetaData indexMetaData = currentState.metaData().index(index); - IndexService indexService = indicesService.createIndex(nodeServicesProvider, indexMetaData, Collections.EMPTY_LIST); - indicesToClose.add(indexMetaData.getIndex()); + IndexService indexService; + if (indicesService.hasIndex(index) == false) { + indexService = indicesService.createIndex(nodeServicesProvider, indexMetaData, Collections.EMPTY_LIST); + indicesToClose.add(index); + } else { + indexService = indicesService.indexService(index); + } // make sure to add custom default mapping if exists if (indexMetaData.getMappings().containsKey(MapperService.DEFAULT_MAPPING)) { indexService.mapperService().merge(MapperService.DEFAULT_MAPPING, indexMetaData.getMappings().get(MapperService.DEFAULT_MAPPING).source(), false, request.updateAllTypes()); diff --git a/core/src/test/java/org/elasticsearch/indices/mapping/UpdateMappingIntegrationIT.java b/core/src/test/java/org/elasticsearch/indices/mapping/UpdateMappingIntegrationIT.java index 75d4a70320e..68902fd22f8 100644 --- a/core/src/test/java/org/elasticsearch/indices/mapping/UpdateMappingIntegrationIT.java +++ b/core/src/test/java/org/elasticsearch/indices/mapping/UpdateMappingIntegrationIT.java @@ -51,7 +51,6 @@ import static org.hamcrest.Matchers.*; @ClusterScope(randomDynamicTemplates = false) public class UpdateMappingIntegrationIT extends ESIntegTestCase { - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/15129") public void testDynamicUpdates() throws Exception { client().admin().indices().prepareCreate("test") .setSettings(