Skip to content

Commit

Permalink
Fix a race condition in Derived Field parsing from search request (op…
Browse files Browse the repository at this point in the history
…ensearch-project#14445)

Signed-off-by: Rishabh Maurya <[email protected]>
  • Loading branch information
rishabhmaurya authored and harshavamsi committed Jul 12, 2024
1 parent f804405 commit fa860fe
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.opensearch.script.Script;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -189,9 +191,10 @@ private void initDerivedFieldTypes(Map<String, Object> derivedFieldsObject, List

private Map<String, DerivedFieldType> getAllDerivedFieldTypeFromObject(Map<String, Object> derivedFieldObject) {
Map<String, DerivedFieldType> derivedFieldTypes = new HashMap<>();
// deep copy of derivedFieldObject is required as DocumentMapperParser modifies the map
DocumentMapper documentMapper = queryShardContext.getMapperService()
.documentMapperParser()
.parse(DerivedFieldMapper.CONTENT_TYPE, derivedFieldObject);
.parse(DerivedFieldMapper.CONTENT_TYPE, (Map) deepCopy(derivedFieldObject));
if (documentMapper != null && documentMapper.mappers() != null) {
for (Mapper mapper : documentMapper.mappers()) {
if (mapper instanceof DerivedFieldMapper) {
Expand Down Expand Up @@ -226,4 +229,27 @@ private DerivedFieldType resolveUsingMappings(String name) {
}
return null;
}

private static Object deepCopy(Object value) {
if (value instanceof Map) {
Map<?, ?> mapValue = (Map<?, ?>) value;
Map<Object, Object> copy = new HashMap<>(mapValue.size());
for (Map.Entry<?, ?> entry : mapValue.entrySet()) {
copy.put(entry.getKey(), deepCopy(entry.getValue()));
}
return copy;
} else if (value instanceof List) {
List<?> listValue = (List<?>) value;
List<Object> copy = new ArrayList<>(listValue.size());
for (Object itemValue : listValue) {
copy.add(deepCopy(itemValue));
}
return copy;
} else if (value instanceof byte[]) {
byte[] bytes = (byte[]) value;
return Arrays.copyOf(bytes, bytes.length);
} else {
return value;
}
}
}

0 comments on commit fa860fe

Please sign in to comment.