|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.index.mapper; |
| 21 | + |
| 22 | +import com.carrotsearch.hppc.cursors.ObjectCursor; |
| 23 | +import org.elasticsearch.Assertions; |
| 24 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 25 | +import org.elasticsearch.cluster.metadata.MappingMetaData; |
| 26 | +import org.elasticsearch.common.compress.CompressedXContent; |
| 27 | + |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +/** |
| 31 | + * This class exists so that we can disable these assertions in a mixed-version cluster without disabling all assertions in |
| 32 | + * {@link MapperService}. These assertions can not necessarily hold in a mixed-version cluster because older nodes will not be serializing |
| 33 | + * mapping version. |
| 34 | + */ |
| 35 | +// TODO: this indirection can be removed when all nodes in a mixed-version cluster test understand mapping version |
| 36 | +final class MapperServiceAssertions { |
| 37 | + |
| 38 | + private MapperServiceAssertions() { |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Assertions regarding changes in the mapping version. |
| 44 | + * |
| 45 | + * @param currentIndexMetaData the current index metadata |
| 46 | + * @param newIndexMetaData the new index metadata |
| 47 | + * @param updatedEntries the updated document mappers |
| 48 | + */ |
| 49 | + static void assertMappingVersion( |
| 50 | + final IndexMetaData currentIndexMetaData, |
| 51 | + final IndexMetaData newIndexMetaData, |
| 52 | + final Map<String, DocumentMapper> updatedEntries) { |
| 53 | + if (Assertions.ENABLED && currentIndexMetaData != null) { |
| 54 | + if (currentIndexMetaData.getMappingVersion() == newIndexMetaData.getMappingVersion()) { |
| 55 | + // if the mapping version is unchanged, then there should not be any updates and all mappings should be the same |
| 56 | + assert updatedEntries.isEmpty() : updatedEntries; |
| 57 | + for (final ObjectCursor<MappingMetaData> mapping : newIndexMetaData.getMappings().values()) { |
| 58 | + final CompressedXContent currentSource = currentIndexMetaData.mapping(mapping.value.type()).source(); |
| 59 | + final CompressedXContent newSource = mapping.value.source(); |
| 60 | + assert currentSource.equals(newSource) : |
| 61 | + "expected current mapping [" + currentSource + "] for type [" + mapping.value.type() + "] " |
| 62 | + + "to be the same as new mapping [" + newSource + "]"; |
| 63 | + } |
| 64 | + } else { |
| 65 | + // if the mapping version is changed, it should increase, there should be updates, and the mapping should be different |
| 66 | + final long currentMappingVersion = currentIndexMetaData.getMappingVersion(); |
| 67 | + final long newMappingVersion = newIndexMetaData.getMappingVersion(); |
| 68 | + assert currentMappingVersion < newMappingVersion : |
| 69 | + "expected current mapping version [" + currentMappingVersion + "] " |
| 70 | + + "to be less than new mapping version [" + newMappingVersion + "]"; |
| 71 | + assert updatedEntries.isEmpty() == false; |
| 72 | + for (final DocumentMapper documentMapper : updatedEntries.values()) { |
| 73 | + final MappingMetaData currentMapping = currentIndexMetaData.mapping(documentMapper.type()); |
| 74 | + if (currentMapping != null) { |
| 75 | + final CompressedXContent currentSource = currentMapping.source(); |
| 76 | + final CompressedXContent newSource = documentMapper.mappingSource(); |
| 77 | + assert currentSource.equals(newSource) == false : |
| 78 | + "expected current mapping [" + currentSource + "] for type [" + documentMapper.type() + "] " + |
| 79 | + "to be different than new mapping"; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments