Apache Solr open-source search software
SOLR-18237: Do not copy data before noggit parsing (#4415)
Pierre Salagnac2c8cb3235e16
diff --git a/changelog/unreleased/SOLR-18237-no-buffer.yml b/changelog/unreleased/SOLR-18237-no-buffer.ymlnew file mode 100644index 00000000000..e3e9482ebdc--- /dev/null+++ b/changelog/unreleased/SOLR-18237-no-buffer.yml@@ -0,0 +1,7 @@+title: Avoid OOMs when deserializing collection states by not copying full data for UTF8 to Java string conversion.+type: fixed+authors:+ - name: Pierre Salagnac+links:+ - name: SOLR-18237+ url: https://issues.apache.org/jira/browse/SOLR-18237diff --git a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java b/solr/solrj/src/java/org/apache/solr/common/util/Utils.javaindex 82acf966bfb..982d706590d 100644--- a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java+++ b/solr/solrj/src/java/org/apache/solr/common/util/Utils.java@@ -83,12 +83,9 @@ import org.noggit.JSONParser;import org.noggit.JSONWriter;import org.noggit.ObjectBuilder;import org.slf4j.Logger;-import org.slf4j.LoggerFactory;public class Utils {- private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());-public static final Random RANDOM;static {@@ -297,16 +294,10 @@ public class Utils {if (utf8 == null || utf8.length == 0 || length == 0) {return Map.of();}- // convert directly from bytes to chars- // and parse directly from that instead of going through- // intermediate strings or readers- CharArr chars = new CharArr();- ByteUtils.UTF8toUTF16(utf8, offset, length, chars);- JSONParser parser = new JSONParser(chars.getArray(), chars.getStart(), chars.length());- parser.setFlags(- parser.getFlags()- | JSONParser.ALLOW_MISSING_COLON_COMMA_BEFORE_OBJECT- | JSONParser.OPTIONAL_OUTER_BRACES);+ // convert from bytes to chars on-the-fly and parse directly+ // from that instead of going through intermediate buffers+ Reader reader = new InputStreamReader(new ByteArrayInputStream(utf8, offset, length), UTF_8);+ JSONParser parser = getJSONParser(reader);try {return fun.apply(parser).getValStrict();} catch (IOException e) {