Skip to content

Commit 42377c7

Browse files
authored
Shortcut mapping update if the incoming mapping version is the same as the current mapping version (#59517)
Currently, when we apply a cluster state change to a shard on a non-master node, we check to see if the mappings need to be updated by comparing the decompressed serialized mappings from the update against the serialized version of the shard's existing mappings. However, we already have a much simpler way of checking this, by comparing mapping versions on the index metadata of the old and new states. This commit adds a shortcut to MapperService.updateMappings() that compares these mapping versions, and ignores the merge if they are equal.
1 parent 77f442f commit 42377c7

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

server/src/main/java/org/elasticsearch/index/mapper/MapperService.java

+19-19
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,14 @@ public boolean updateMapping(final IndexMetadata currentIndexMetadata, final Ind
183183
assert newIndexMetadata.getIndex().equals(index()) : "index mismatch: expected " + index()
184184
+ " but was " + newIndexMetadata.getIndex();
185185

186+
if (currentIndexMetadata != null && currentIndexMetadata.getMappingVersion() == newIndexMetadata.getMappingVersion()) {
187+
assertMappingVersion(currentIndexMetadata, newIndexMetadata, this.mapper);
188+
return false;
189+
}
190+
186191
final DocumentMapper updatedMapper;
187192
try {
188-
// only update entries if needed
189-
updatedMapper = internalMerge(newIndexMetadata, MergeReason.MAPPING_RECOVERY, true);
193+
updatedMapper = internalMerge(newIndexMetadata, MergeReason.MAPPING_RECOVERY);
190194
} catch (Exception e) {
191195
logger.warn(() -> new ParameterizedMessage("[{}] failed to apply mappings", index()), e);
192196
throw e;
@@ -230,7 +234,7 @@ public boolean updateMapping(final IndexMetadata currentIndexMetadata, final Ind
230234
private void assertMappingVersion(
231235
final IndexMetadata currentIndexMetadata,
232236
final IndexMetadata newIndexMetadata,
233-
final DocumentMapper updatedMapper) {
237+
final DocumentMapper updatedMapper) throws IOException {
234238
if (Assertions.ENABLED && currentIndexMetadata != null) {
235239
if (currentIndexMetadata.getMappingVersion() == newIndexMetadata.getMappingVersion()) {
236240
// if the mapping version is unchanged, then there should not be any updates and all mappings should be the same
@@ -241,17 +245,21 @@ private void assertMappingVersion(
241245
final CompressedXContent currentSource = currentIndexMetadata.mapping().source();
242246
final CompressedXContent newSource = mapping.source();
243247
assert currentSource.equals(newSource) :
244-
"expected current mapping [" + currentSource + "] for type [" + mapping.type() + "] "
245-
+ "to be the same as new mapping [" + newSource + "]";
248+
"expected current mapping [" + currentSource + "] for type [" + mapping.type() + "] "
249+
+ "to be the same as new mapping [" + newSource + "]";
250+
final CompressedXContent mapperSource = new CompressedXContent(Strings.toString(mapper));
251+
assert currentSource.equals(mapperSource) :
252+
"expected current mapping [" + currentSource + "] for type [" + mapping.type() + "] "
253+
+ "to be the same as new mapping [" + mapperSource + "]";
246254
}
247255

248256
} else {
249-
// if the mapping version is changed, it should increase, there should be updates, and the mapping should be different
257+
// the mapping version should increase, there should be updates, and the mapping should be different
250258
final long currentMappingVersion = currentIndexMetadata.getMappingVersion();
251259
final long newMappingVersion = newIndexMetadata.getMappingVersion();
252260
assert currentMappingVersion < newMappingVersion :
253-
"expected current mapping version [" + currentMappingVersion + "] "
254-
+ "to be less than new mapping version [" + newMappingVersion + "]";
261+
"expected current mapping version [" + currentMappingVersion + "] "
262+
+ "to be less than new mapping version [" + newMappingVersion + "]";
255263
assert updatedMapper != null;
256264
final MappingMetadata currentMapping = currentIndexMetadata.mapping();
257265
if (currentMapping != null) {
@@ -270,26 +278,18 @@ public void merge(String type, Map<String, Object> mappings, MergeReason reason)
270278
}
271279

272280
public void merge(IndexMetadata indexMetadata, MergeReason reason) {
273-
internalMerge(indexMetadata, reason, false);
281+
internalMerge(indexMetadata, reason);
274282
}
275283

276284
public DocumentMapper merge(String type, CompressedXContent mappingSource, MergeReason reason) {
277285
return internalMerge(type, mappingSource, reason);
278286
}
279287

280-
private synchronized DocumentMapper internalMerge(IndexMetadata indexMetadata,
281-
MergeReason reason, boolean onlyUpdateIfNeeded) {
288+
private synchronized DocumentMapper internalMerge(IndexMetadata indexMetadata, MergeReason reason) {
282289
assert reason != MergeReason.MAPPING_UPDATE_PREFLIGHT;
283290
MappingMetadata mappingMetadata = indexMetadata.mapping();
284291
if (mappingMetadata != null) {
285-
if (onlyUpdateIfNeeded) {
286-
DocumentMapper existingMapper = documentMapper();
287-
if (existingMapper == null || mappingMetadata.source().equals(existingMapper.mappingSource()) == false) {
288-
return internalMerge(mappingMetadata.type(), mappingMetadata.source(), reason);
289-
}
290-
} else {
291-
return internalMerge(mappingMetadata.type(), mappingMetadata.source(), reason);
292-
}
292+
return internalMerge(mappingMetadata.type(), mappingMetadata.source(), reason);
293293
}
294294
return null;
295295
}

0 commit comments

Comments
 (0)