SOLR-12290: We must close ContentStreams because we don't know the source of the inputstream - use a CloseShield to prevent tripping our close assert in SolrDispatchFilter.

This commit is contained in:
markrmiller 2018-05-06 14:25:59 -05:00
parent 0922e58c2c
commit 5fc7251540
4 changed files with 69 additions and 44 deletions

View File

@ -17,6 +17,7 @@
package org.apache.solr.handler; package org.apache.solr.handler;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.math.BigInteger; import java.math.BigInteger;
@ -108,8 +109,9 @@ public class BlobHandler extends RequestHandlerBase implements PluginInfoInitial
for (ContentStream stream : req.getContentStreams()) { for (ContentStream stream : req.getContentStreams()) {
ByteBuffer payload; ByteBuffer payload;
payload = SimplePostTool.inputStreamToByteArray(stream.getStream(), maxSize); try (InputStream is = stream.getStream()) {
payload = SimplePostTool.inputStreamToByteArray(is, maxSize);
}
MessageDigest m = MessageDigest.getInstance("MD5"); MessageDigest m = MessageDigest.getInstance("MD5");
m.update(payload.array(), payload.position(), payload.limit()); m.update(payload.array(), payload.position(), payload.limit());
String md5 = new BigInteger(1, m.digest()).toString(16); String md5 = new BigInteger(1, m.digest()).toString(16);

View File

@ -28,6 +28,7 @@ import org.apache.solr.update.*;
import org.apache.solr.update.processor.UpdateRequestProcessor; import org.apache.solr.update.processor.UpdateRequestProcessor;
import org.apache.solr.internal.csv.CSVStrategy; import org.apache.solr.internal.csv.CSVStrategy;
import org.apache.solr.internal.csv.CSVParser; import org.apache.solr.internal.csv.CSVParser;
import org.apache.commons.io.IOUtils;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.List; import java.util.List;
@ -314,10 +315,11 @@ abstract class CSVLoaderBase extends ContentStreamLoader {
/** load the CSV input */ /** load the CSV input */
@Override @Override
public void load(SolrQueryRequest req, SolrQueryResponse rsp, ContentStream stream, UpdateRequestProcessor processor) public void load(SolrQueryRequest req, SolrQueryResponse rsp, ContentStream stream, UpdateRequestProcessor processor) throws IOException {
throws IOException {
errHeader = "CSVLoader: input=" + stream.getSourceInfo(); errHeader = "CSVLoader: input=" + stream.getSourceInfo();
Reader reader = stream.getReader(); Reader reader = null;
try {
reader = stream.getReader();
if (skipLines>0) { if (skipLines>0) {
if (!(reader instanceof BufferedReader)) { if (!(reader instanceof BufferedReader)) {
reader = new BufferedReader(reader); reader = new BufferedReader(reader);
@ -357,6 +359,11 @@ abstract class CSVLoaderBase extends ContentStreamLoader {
addDoc(line,vals); addDoc(line,vals);
} }
} finally{
if (reader != null) {
IOUtils.closeQuietly(reader);
}
}
} }
/** called for each line of values (document) */ /** called for each line of values (document) */

View File

@ -48,8 +48,20 @@ import org.apache.solr.update.processor.UpdateRequestProcessor;
public class JavabinLoader extends ContentStreamLoader { public class JavabinLoader extends ContentStreamLoader {
@Override @Override
public void load(SolrQueryRequest req, SolrQueryResponse rsp, ContentStream cs, UpdateRequestProcessor processor) throws Exception { public void load(SolrQueryRequest req, SolrQueryResponse rsp, ContentStream stream, UpdateRequestProcessor processor) throws Exception {
InputStream stream = cs.getStream(); InputStream is = null;
try {
is = stream.getStream();
parseAndLoadDocs(req, rsp, is, processor);
} finally {
if(is != null) {
is.close();
}
}
}
private void parseAndLoadDocs(final SolrQueryRequest req, SolrQueryResponse rsp, InputStream stream,
final UpdateRequestProcessor processor) throws IOException {
UpdateRequest update = null; UpdateRequest update = null;
JavaBinUpdateRequestCodec.StreamingUpdateHandler handler = new JavaBinUpdateRequestCodec.StreamingUpdateHandler() { JavaBinUpdateRequestCodec.StreamingUpdateHandler handler = new JavaBinUpdateRequestCodec.StreamingUpdateHandler() {
private AddUpdateCommand addCmd = null; private AddUpdateCommand addCmd = null;

View File

@ -519,7 +519,11 @@ public class SolrRequestParsers
@Override @Override
public InputStream getStream() throws IOException { public InputStream getStream() throws IOException {
return req.getInputStream(); // we explicitly protect this servlet stream from being closed
// so that it does not trip our test assert in our close shield
// in SolrDispatchFilter - we must allow closes from getStream
// due to the other impls of ContentStream
return new CloseShieldInputStream(req.getInputStream());
} }
} }