Skip to content

Commit 17f27cc

Browse files
javannadavidkyle
authored andcommitted
Ensure MapperService#getAllMetaFields elements order is deterministic (#36739)
MapperService#getAllMetaFields returns an array, which is created out of an `ObjectHashSet`. Such set does not guarantee deterministic hash ordering. The array returned by its toArray may be sorted differently at each run. This caused some repeatability issues in our tests (see #29080) as we pick random fields from the array of possible metadata fields, but that won't be repeatable if the input array is sorted differently at every run. Once setting the tests seed, hppc picks that up and the sorting is deterministic, but failures don't repeat with the seed that gets printed out originally (as a seed was not originally set). See also https://issues.carrot2.org/projects/HPPC/issues/HPPC-173. With this commit, we simply create a static sorted array that is used for `getAllMetaFields`. The change is in production code but really affects only testing as the only production usage of this method was to iterate through all values when parsing fields in the high-level REST client code. Anyways, this seems like a good change as returning an array would imply that it's deterministically sorted.
1 parent 997703f commit 17f27cc

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ public enum MergeReason {
109109

110110
//TODO this needs to be cleaned up: _timestamp and _ttl are not supported anymore, _field_names, _seq_no, _version and _source are
111111
//also missing, not sure if on purpose. See IndicesModule#getMetadataMappers
112-
private static ObjectHashSet<String> META_FIELDS = ObjectHashSet.from(
113-
"_id", "_type", "_routing", "_index",
114-
"_size", "_timestamp", "_ttl", IgnoredFieldMapper.NAME
115-
);
112+
private static final String[] SORTED_META_FIELDS = new String[]{
113+
"_id", IgnoredFieldMapper.NAME, "_index", "_routing", "_size", "_timestamp", "_ttl", "_type"
114+
};
115+
116+
private static final ObjectHashSet<String> META_FIELDS = ObjectHashSet.from(SORTED_META_FIELDS);
116117

117118
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(MapperService.class));
118119

@@ -762,7 +763,7 @@ public static boolean isMetadataField(String fieldName) {
762763
}
763764

764765
public static String[] getAllMetaFields() {
765-
return META_FIELDS.toArray(String.class);
766+
return Arrays.copyOf(SORTED_META_FIELDS, SORTED_META_FIELDS.length);
766767
}
767768

768769
/** An analyzer wrapper that can lookup fields within the index mappings */
@@ -789,5 +790,4 @@ protected Analyzer getWrappedAnalyzer(String fieldName) {
789790
return defaultAnalyzer;
790791
}
791792
}
792-
793793
}

0 commit comments

Comments
 (0)