2424import com .carrotsearch .hppc .cursors .ObjectCursor ;
2525import com .carrotsearch .hppc .cursors .ObjectObjectCursor ;
2626
27+ import org .elasticsearch .Assertions ;
2728import org .elasticsearch .Version ;
2829import org .elasticsearch .action .admin .indices .rollover .RolloverInfo ;
2930import org .elasticsearch .action .support .ActiveShardCount ;
@@ -291,6 +292,7 @@ public Iterator<Setting<Integer>> settings() {
291292
292293 public static final String KEY_IN_SYNC_ALLOCATIONS = "in_sync_allocations" ;
293294 static final String KEY_VERSION = "version" ;
295+ static final String KEY_MAPPING_VERSION = "mapping_version" ;
294296 static final String KEY_ROUTING_NUM_SHARDS = "routing_num_shards" ;
295297 static final String KEY_SETTINGS = "settings" ;
296298 static final String KEY_STATE = "state" ;
@@ -309,6 +311,9 @@ public Iterator<Setting<Integer>> settings() {
309311
310312 private final Index index ;
311313 private final long version ;
314+
315+ private final long mappingVersion ;
316+
312317 private final long [] primaryTerms ;
313318
314319 private final State state ;
@@ -336,7 +341,7 @@ public Iterator<Setting<Integer>> settings() {
336341 private final ActiveShardCount waitForActiveShards ;
337342 private final ImmutableOpenMap <String , RolloverInfo > rolloverInfos ;
338343
339- private IndexMetaData (Index index , long version , long [] primaryTerms , State state , int numberOfShards , int numberOfReplicas , Settings settings ,
344+ private IndexMetaData (Index index , long version , long mappingVersion , long [] primaryTerms , State state , int numberOfShards , int numberOfReplicas , Settings settings ,
340345 ImmutableOpenMap <String , MappingMetaData > mappings , ImmutableOpenMap <String , AliasMetaData > aliases ,
341346 ImmutableOpenMap <String , Custom > customs , ImmutableOpenIntMap <Set <String >> inSyncAllocationIds ,
342347 DiscoveryNodeFilters requireFilters , DiscoveryNodeFilters initialRecoveryFilters , DiscoveryNodeFilters includeFilters , DiscoveryNodeFilters excludeFilters ,
@@ -345,6 +350,8 @@ private IndexMetaData(Index index, long version, long[] primaryTerms, State stat
345350
346351 this .index = index ;
347352 this .version = version ;
353+ assert mappingVersion >= 0 : mappingVersion ;
354+ this .mappingVersion = mappingVersion ;
348355 this .primaryTerms = primaryTerms ;
349356 assert primaryTerms .length == numberOfShards ;
350357 this .state = state ;
@@ -394,6 +401,9 @@ public long getVersion() {
394401 return this .version ;
395402 }
396403
404+ public long getMappingVersion () {
405+ return mappingVersion ;
406+ }
397407
398408 /**
399409 * The term of the current selected primary. This is a non-negative number incremented when
@@ -644,6 +654,7 @@ private static class IndexMetaDataDiff implements Diff<IndexMetaData> {
644654 private final String index ;
645655 private final int routingNumShards ;
646656 private final long version ;
657+ private final long mappingVersion ;
647658 private final long [] primaryTerms ;
648659 private final State state ;
649660 private final Settings settings ;
@@ -656,6 +667,7 @@ private static class IndexMetaDataDiff implements Diff<IndexMetaData> {
656667 IndexMetaDataDiff (IndexMetaData before , IndexMetaData after ) {
657668 index = after .index .getName ();
658669 version = after .version ;
670+ mappingVersion = after .mappingVersion ;
659671 routingNumShards = after .routingNumShards ;
660672 state = after .state ;
661673 settings = after .settings ;
@@ -672,6 +684,11 @@ private static class IndexMetaDataDiff implements Diff<IndexMetaData> {
672684 index = in .readString ();
673685 routingNumShards = in .readInt ();
674686 version = in .readLong ();
687+ if (in .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
688+ mappingVersion = in .readVLong ();
689+ } else {
690+ mappingVersion = 1 ;
691+ }
675692 state = State .fromId (in .readByte ());
676693 settings = Settings .readSettingsFromStream (in );
677694 primaryTerms = in .readVLongArray ();
@@ -707,6 +724,9 @@ public void writeTo(StreamOutput out) throws IOException {
707724 out .writeString (index );
708725 out .writeInt (routingNumShards );
709726 out .writeLong (version );
727+ if (out .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
728+ out .writeVLong (mappingVersion );
729+ }
710730 out .writeByte (state .id );
711731 Settings .writeSettingsToStream (settings , out );
712732 out .writeVLongArray (primaryTerms );
@@ -723,6 +743,7 @@ public void writeTo(StreamOutput out) throws IOException {
723743 public IndexMetaData apply (IndexMetaData part ) {
724744 Builder builder = builder (index );
725745 builder .version (version );
746+ builder .mappingVersion (mappingVersion );
726747 builder .setRoutingNumShards (routingNumShards );
727748 builder .state (state );
728749 builder .settings (settings );
@@ -739,6 +760,11 @@ public IndexMetaData apply(IndexMetaData part) {
739760 public static IndexMetaData readFrom (StreamInput in ) throws IOException {
740761 Builder builder = new Builder (in .readString ());
741762 builder .version (in .readLong ());
763+ if (in .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
764+ builder .mappingVersion (in .readVLong ());
765+ } else {
766+ builder .mappingVersion (1 );
767+ }
742768 builder .setRoutingNumShards (in .readInt ());
743769 builder .state (State .fromId (in .readByte ()));
744770 builder .settings (readSettingsFromStream (in ));
@@ -778,6 +804,9 @@ public static IndexMetaData readFrom(StreamInput in) throws IOException {
778804 public void writeTo (StreamOutput out ) throws IOException {
779805 out .writeString (index .getName ()); // uuid will come as part of settings
780806 out .writeLong (version );
807+ if (out .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
808+ out .writeVLong (mappingVersion );
809+ }
781810 out .writeInt (routingNumShards );
782811 out .writeByte (state .id ());
783812 writeSettingsToStream (settings , out );
@@ -821,6 +850,7 @@ public static class Builder {
821850 private String index ;
822851 private State state = State .OPEN ;
823852 private long version = 1 ;
853+ private long mappingVersion = 1 ;
824854 private long [] primaryTerms = null ;
825855 private Settings settings = Settings .Builder .EMPTY_SETTINGS ;
826856 private final ImmutableOpenMap .Builder <String , MappingMetaData > mappings ;
@@ -843,6 +873,7 @@ public Builder(IndexMetaData indexMetaData) {
843873 this .index = indexMetaData .getIndex ().getName ();
844874 this .state = indexMetaData .state ;
845875 this .version = indexMetaData .version ;
876+ this .mappingVersion = indexMetaData .mappingVersion ;
846877 this .settings = indexMetaData .getSettings ();
847878 this .primaryTerms = indexMetaData .primaryTerms .clone ();
848879 this .mappings = ImmutableOpenMap .builder (indexMetaData .mappings );
@@ -1009,6 +1040,15 @@ public Builder version(long version) {
10091040 return this ;
10101041 }
10111042
1043+ public long mappingVersion () {
1044+ return mappingVersion ;
1045+ }
1046+
1047+ public Builder mappingVersion (final long mappingVersion ) {
1048+ this .mappingVersion = mappingVersion ;
1049+ return this ;
1050+ }
1051+
10121052 /**
10131053 * returns the primary term for the given shard.
10141054 * See {@link IndexMetaData#primaryTerm(int)} for more information.
@@ -1136,7 +1176,7 @@ public IndexMetaData build() {
11361176
11371177 final String uuid = settings .get (SETTING_INDEX_UUID , INDEX_UUID_NA_VALUE );
11381178
1139- return new IndexMetaData (new Index (index , uuid ), version , primaryTerms , state , numberOfShards , numberOfReplicas , tmpSettings , mappings .build (),
1179+ return new IndexMetaData (new Index (index , uuid ), version , mappingVersion , primaryTerms , state , numberOfShards , numberOfReplicas , tmpSettings , mappings .build (),
11401180 tmpAliases .build (), customs .build (), filledInSyncAllocationIds .build (), requireFilters , initialRecoveryFilters , includeFilters , excludeFilters ,
11411181 indexCreatedVersion , indexUpgradedVersion , getRoutingNumShards (), routingPartitionSize , waitForActiveShards , rolloverInfos .build ());
11421182 }
@@ -1145,6 +1185,7 @@ public static void toXContent(IndexMetaData indexMetaData, XContentBuilder build
11451185 builder .startObject (indexMetaData .getIndex ().getName ());
11461186
11471187 builder .field (KEY_VERSION , indexMetaData .getVersion ());
1188+ builder .field (KEY_MAPPING_VERSION , indexMetaData .getMappingVersion ());
11481189 builder .field (KEY_ROUTING_NUM_SHARDS , indexMetaData .getRoutingNumShards ());
11491190 builder .field (KEY_STATE , indexMetaData .getState ().toString ().toLowerCase (Locale .ENGLISH ));
11501191
@@ -1218,6 +1259,7 @@ public static IndexMetaData fromXContent(XContentParser parser) throws IOExcepti
12181259 if (token != XContentParser .Token .START_OBJECT ) {
12191260 throw new IllegalArgumentException ("expected object but got a " + token );
12201261 }
1262+ boolean mappingVersion = false ;
12211263 while ((token = parser .nextToken ()) != XContentParser .Token .END_OBJECT ) {
12221264 if (token == XContentParser .Token .FIELD_NAME ) {
12231265 currentFieldName = parser .currentName ();
@@ -1316,6 +1358,9 @@ public static IndexMetaData fromXContent(XContentParser parser) throws IOExcepti
13161358 builder .state (State .fromString (parser .text ()));
13171359 } else if (KEY_VERSION .equals (currentFieldName )) {
13181360 builder .version (parser .longValue ());
1361+ } else if (KEY_MAPPING_VERSION .equals (currentFieldName )) {
1362+ mappingVersion = true ;
1363+ builder .mappingVersion (parser .longValue ());
13191364 } else if (KEY_ROUTING_NUM_SHARDS .equals (currentFieldName )) {
13201365 builder .setRoutingNumShards (parser .intValue ());
13211366 } else {
@@ -1325,6 +1370,9 @@ public static IndexMetaData fromXContent(XContentParser parser) throws IOExcepti
13251370 throw new IllegalArgumentException ("Unexpected token " + token );
13261371 }
13271372 }
1373+ if (Assertions .ENABLED && Version .indexCreated (builder .settings ).onOrAfter (Version .V_7_0_0_alpha1 )) {
1374+ assert mappingVersion : "mapping version should be present for indices created on or after 7.0.0" ;
1375+ }
13281376 return builder .build ();
13291377 }
13301378 }
0 commit comments