Skip to content

Commit

Permalink
Fix a race condition in Derived Field parsing from search request
Browse files Browse the repository at this point in the history
Signed-off-by: Rishabh Maurya <rishabhmaurya05@gmail.com>
  • Loading branch information
rishabhmaurya committed Jun 21, 2024
1 parent f5dbbb0 commit cf64569
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());

Check warning on line 243 in server/src/main/java/org/opensearch/index/mapper/DefaultDerivedFieldResolver.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/mapper/DefaultDerivedFieldResolver.java#L242-L243

Added lines #L242 - L243 were not covered by tests
for (Object itemValue : listValue) {
copy.add(deepCopy(itemValue));
}
return copy;

Check warning on line 247 in server/src/main/java/org/opensearch/index/mapper/DefaultDerivedFieldResolver.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/mapper/DefaultDerivedFieldResolver.java#L245-L247

Added lines #L245 - L247 were not covered by tests
} else if (value instanceof byte[]) {
byte[] bytes = (byte[]) value;
return Arrays.copyOf(bytes, bytes.length);

Check warning on line 250 in server/src/main/java/org/opensearch/index/mapper/DefaultDerivedFieldResolver.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/mapper/DefaultDerivedFieldResolver.java#L249-L250

Added lines #L249 - L250 were not covered by tests
} else {
return value;
}
}
}

0 comments on commit cf64569

Please sign in to comment.