Skip to content

Commit 0a6f7c9

Browse files
committed
Add assertion
1 parent 41260ef commit 0a6f7c9

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

server/src/main/java/org/elasticsearch/index/IndexService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,8 @@ List<SearchOperationListener> getSearchOperationListener() { // pkg private for
522522
}
523523

524524
@Override
525-
public boolean updateMapping(IndexMetaData indexMetaData) throws IOException {
526-
return mapperService().updateMapping(indexMetaData);
525+
public boolean updateMapping(final IndexMetaData currentIndexMetaData, final IndexMetaData newIndexMetaData) throws IOException {
526+
return mapperService().updateMapping(currentIndexMetaData, newIndexMetaData);
527527
}
528528

529529
private class StoreCloseListener implements Store.OnClose {

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

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.lucene.analysis.Analyzer;
2626
import org.apache.lucene.analysis.DelegatingAnalyzerWrapper;
2727
import org.apache.lucene.index.Term;
28+
import org.elasticsearch.Assertions;
2829
import org.elasticsearch.ElasticsearchGenerationException;
2930
import org.elasticsearch.Version;
3031
import org.elasticsearch.cluster.metadata.IndexMetaData;
@@ -192,8 +193,8 @@ public static Map<String, Object> parseMapping(NamedXContentRegistry xContentReg
192193
/**
193194
* Update mapping by only merging the metadata that is different between received and stored entries
194195
*/
195-
public boolean updateMapping(IndexMetaData indexMetaData) throws IOException {
196-
assert indexMetaData.getIndex().equals(index()) : "index mismatch: expected " + index() + " but was " + indexMetaData.getIndex();
196+
public boolean updateMapping(final IndexMetaData currentIndexMetaData, final IndexMetaData newIndexMetaData) throws IOException {
197+
assert newIndexMetaData.getIndex().equals(index()) : "index mismatch: expected " + index() + " but was " + newIndexMetaData.getIndex();
197198
// go over and add the relevant mappings (or update them)
198199
Set<String> existingMappers = new HashSet<>();
199200
if (mapper != null) {
@@ -205,17 +206,19 @@ public boolean updateMapping(IndexMetaData indexMetaData) throws IOException {
205206
final Map<String, DocumentMapper> updatedEntries;
206207
try {
207208
// only update entries if needed
208-
updatedEntries = internalMerge(indexMetaData, MergeReason.MAPPING_RECOVERY, true);
209+
updatedEntries = internalMerge(newIndexMetaData, MergeReason.MAPPING_RECOVERY, true);
209210
} catch (Exception e) {
210211
logger.warn(() -> new ParameterizedMessage("[{}] failed to apply mappings", index()), e);
211212
throw e;
212213
}
213214

214215
boolean requireRefresh = false;
215216

217+
assertMappingVersion(currentIndexMetaData, newIndexMetaData, updatedEntries);
218+
216219
for (DocumentMapper documentMapper : updatedEntries.values()) {
217220
String mappingType = documentMapper.type();
218-
CompressedXContent incomingMappingSource = indexMetaData.mapping(mappingType).source();
221+
CompressedXContent incomingMappingSource = newIndexMetaData.mapping(mappingType).source();
219222

220223
String op = existingMappers.contains(mappingType) ? "updated" : "added";
221224
if (logger.isDebugEnabled() && incomingMappingSource.compressed().length < 512) {
@@ -240,6 +243,43 @@ public boolean updateMapping(IndexMetaData indexMetaData) throws IOException {
240243
return requireRefresh;
241244
}
242245

246+
private void assertMappingVersion(
247+
final IndexMetaData currentIndexMetaData,
248+
final IndexMetaData newIndexMetaData,
249+
final Map<String, DocumentMapper> updatedEntries) {
250+
if (Assertions.ENABLED && currentIndexMetaData != null) {
251+
if (currentIndexMetaData.getMappingVersion() == newIndexMetaData.getMappingVersion()) {
252+
// if the mapping version is unchanged, then there should not be any updates and all mappings should be the same
253+
assert updatedEntries.isEmpty() : updatedEntries;
254+
for (final ObjectCursor<MappingMetaData> mapping : newIndexMetaData.getMappings().values()) {
255+
final CompressedXContent currentSource = currentIndexMetaData.mapping(mapping.value.type()).source();
256+
final CompressedXContent newSource = mapping.value.source();
257+
assert currentSource.equals(newSource) :
258+
"expected current mapping [" + currentSource + "] for type [" + mapping.value.type() + "] "
259+
+ "to be the same as new mapping [" + newSource + "]";
260+
}
261+
} else {
262+
// if the mapping version is changed, it should increase, there should be updates, and the mapping should be different
263+
final long currentMappingVersion = currentIndexMetaData.getMappingVersion();
264+
final long newMappingVersion = newIndexMetaData.getMappingVersion();
265+
assert currentMappingVersion < newMappingVersion :
266+
"expected current mapping version [" + currentMappingVersion + "] "
267+
+ "to be less than new mapping version [" + newMappingVersion + "]";
268+
assert updatedEntries.isEmpty() == false;
269+
for (final DocumentMapper documentMapper : updatedEntries.values()) {
270+
final MappingMetaData currentMapping = currentIndexMetaData.mapping(documentMapper.type());
271+
if (currentMapping != null) {
272+
final CompressedXContent currentSource = currentMapping.source();
273+
final CompressedXContent newSource = documentMapper.mappingSource();
274+
assert currentSource.equals(newSource) == false :
275+
"expected current mapping [" + currentSource + "] for type [" + documentMapper.type() + "] " +
276+
"to be different than new mapping";
277+
}
278+
}
279+
}
280+
}
281+
}
282+
243283
public void merge(Map<String, Map<String, Object>> mappings, MergeReason reason) {
244284
Map<String, CompressedXContent> mappingSourcesCompressed = new LinkedHashMap<>(mappings.size());
245285
for (Map.Entry<String, Map<String, Object>> entry : mappings.entrySet()) {

server/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ private void createIndices(final ClusterState state) {
456456
AllocatedIndex<? extends Shard> indexService = null;
457457
try {
458458
indexService = indicesService.createIndex(indexMetaData, buildInIndexListener);
459-
if (indexService.updateMapping(indexMetaData) && sendRefreshMapping) {
459+
if (indexService.updateMapping(null, indexMetaData) && sendRefreshMapping) {
460460
nodeMappingRefreshAction.nodeMappingRefresh(state.nodes().getMasterNode(),
461461
new NodeMappingRefreshAction.NodeMappingRefreshRequest(indexMetaData.getIndex().getName(),
462462
indexMetaData.getIndexUUID(), state.nodes().getLocalNodeId())
@@ -490,7 +490,7 @@ private void updateIndices(ClusterChangedEvent event) {
490490
if (ClusterChangedEvent.indexMetaDataChanged(currentIndexMetaData, newIndexMetaData)) {
491491
indexService.updateMetaData(newIndexMetaData);
492492
try {
493-
if (indexService.updateMapping(newIndexMetaData) && sendRefreshMapping) {
493+
if (indexService.updateMapping(currentIndexMetaData, newIndexMetaData) && sendRefreshMapping) {
494494
nodeMappingRefreshAction.nodeMappingRefresh(state.nodes().getMasterNode(),
495495
new NodeMappingRefreshAction.NodeMappingRefreshRequest(newIndexMetaData.getIndex().getName(),
496496
newIndexMetaData.getIndexUUID(), state.nodes().getLocalNodeId())
@@ -778,7 +778,7 @@ public interface AllocatedIndex<T extends Shard> extends Iterable<T>, IndexCompo
778778
/**
779779
* Checks if index requires refresh from master.
780780
*/
781-
boolean updateMapping(IndexMetaData indexMetaData) throws IOException;
781+
boolean updateMapping(IndexMetaData currentIndexMetaData, IndexMetaData newIndexMetaData) throws IOException;
782782

783783
/**
784784
* Returns shard with given id.

server/src/test/java/org/elasticsearch/indices/cluster/AbstractIndicesClusterStateServiceTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public IndexSettings getIndexSettings() {
273273
}
274274

275275
@Override
276-
public boolean updateMapping(IndexMetaData indexMetaData) throws IOException {
276+
public boolean updateMapping(final IndexMetaData currentIndexMetaData, final IndexMetaData newIndexMetaData) throws IOException {
277277
failRandomly();
278278
return false;
279279
}

0 commit comments

Comments
 (0)