SOLR-14169: Fix 20 Resource Leak warnings in SolrJ's apache/solr/common

This commit is contained in:
Tomas Fernandez Lobbe 2020-01-08 11:16:32 -08:00
parent a17c486424
commit a9beeb1d1e
7 changed files with 75 additions and 55 deletions

View File

@ -224,6 +224,8 @@ Other Changes
* SOLR-13778: Solrj client will retry requests on SSLException with a suppressed SocketException
(very likely a hard-closed socket connection).
* SOLR-14169: Fix 20 Resource Leak warnings in SolrJ's apache/solr/common (Andras Salamon via Tomás Fernández Löbbe)
================== 8.4.0 ==================
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.

View File

@ -94,8 +94,8 @@ public class ZkNodeProps implements JSONWriter.Writable {
public static ZkNodeProps load(byte[] bytes) {
Map<String, Object> props = null;
if (bytes[0] == 2) {
try {
props = (Map<String, Object>) new JavaBinCodec().unmarshal(bytes);
try (JavaBinCodec jbc = new JavaBinCodec()) {
props = (Map<String, Object>) jbc.unmarshal(bytes);
} catch (IOException e) {
throw new RuntimeException("Unable to parse javabin content");
}

View File

@ -203,10 +203,10 @@ public class Utils {
}
public static Writer writeJson(Object o, Writer writer, boolean indent) throws IOException {
new SolrJSONWriter(writer)
.setIndent(indent)
.writeObj(o)
.close();
try (SolrJSONWriter jsonWriter = new SolrJSONWriter(writer)) {
jsonWriter.setIndent(indent)
.writeObj(o);
}
return writer;
}

View File

@ -47,7 +47,7 @@ public class ContentStreamTest extends SolrTestCaseJ4 {
public void testFileStream() throws IOException {
File file = new File(createTempDir().toFile(), "README");
try (InputStream is = new SolrResourceLoader().openResource("solrj/README");
try (SolrResourceLoader srl = new SolrResourceLoader(); InputStream is = srl.openResource("solrj/README");
FileOutputStream os = new FileOutputStream(file)) {
assertNotNull(is);
IOUtils.copy(is, os);
@ -70,7 +70,7 @@ public class ContentStreamTest extends SolrTestCaseJ4 {
public void testFileStreamGZIP() throws IOException {
File file = new File(createTempDir().toFile(), "README.gz");
try (InputStream is = new SolrResourceLoader().openResource("solrj/README");
try (SolrResourceLoader srl = new SolrResourceLoader(); InputStream is = srl.openResource("solrj/README");
FileOutputStream os = new FileOutputStream(file);
GZIPOutputStream zos = new GZIPOutputStream(os)) {
IOUtils.copy(is, zos);
@ -95,7 +95,7 @@ public class ContentStreamTest extends SolrTestCaseJ4 {
public void testURLStream() throws IOException {
File file = new File(createTempDir().toFile(), "README");
try (InputStream is = new SolrResourceLoader().openResource("solrj/README");
try (SolrResourceLoader srl = new SolrResourceLoader(); InputStream is = srl.openResource("solrj/README");
FileOutputStream os = new FileOutputStream(file)) {
IOUtils.copy(is, os);
}
@ -124,7 +124,7 @@ public class ContentStreamTest extends SolrTestCaseJ4 {
public void testURLStreamGZIP() throws IOException {
File file = new File(createTempDir().toFile(), "README.gz");
try (InputStream is = new SolrResourceLoader().openResource("solrj/README");
try (SolrResourceLoader srl = new SolrResourceLoader(); InputStream is = srl.openResource("solrj/README");
FileOutputStream os = new FileOutputStream(file);
GZIPOutputStream zos = new GZIPOutputStream(os)) {
IOUtils.copy(is, zos);
@ -149,7 +149,7 @@ public class ContentStreamTest extends SolrTestCaseJ4 {
public void testURLStreamCSVGZIPExtention() throws IOException {
File file = new File(createTempDir().toFile(), "README.CSV.gz");
try (InputStream is = new SolrResourceLoader().openResource("solrj/README");
try (SolrResourceLoader srl = new SolrResourceLoader(); InputStream is = srl.openResource("solrj/README");
FileOutputStream os = new FileOutputStream(file);
GZIPOutputStream zos = new GZIPOutputStream(os)) {
IOUtils.copy(is, zos);
@ -174,7 +174,7 @@ public class ContentStreamTest extends SolrTestCaseJ4 {
public void testURLStreamJSONGZIPExtention() throws IOException {
File file = new File(createTempDir().toFile(), "README.json.gzip");
try (InputStream is = new SolrResourceLoader().openResource("solrj/README");
try (SolrResourceLoader srl = new SolrResourceLoader(); InputStream is = srl.openResource("solrj/README");
FileOutputStream os = new FileOutputStream(file);
GZIPOutputStream zos = new GZIPOutputStream(os)) {
IOUtils.copy(is, zos);

View File

@ -40,32 +40,33 @@ import static org.apache.solr.common.util.Utils.NEW_LINKED_HASHMAP_FUN;
public class TestFastJavabinDecoder extends SolrTestCaseJ4 {
public void testTagRead() throws Exception {
BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
FastOutputStream faos = FastOutputStream.wrap(baos);
JavaBinCodec codec = new JavaBinCodec(faos, null);
codec.writeVal(10);
codec.writeVal(100);
codec.writeVal("Hello!");
try (JavaBinCodec codec = new JavaBinCodec(faos, null)) {
codec.writeVal(10);
codec.writeVal(100);
codec.writeVal("Hello!");
}
faos.flushBuffer();
faos.close();
FastInputStream fis = new FastInputStream(null, baos.getbuf(), 0, baos.size());
FastJavaBinDecoder.StreamCodec scodec = new FastJavaBinDecoder.StreamCodec(fis);
scodec.start();
Tag tag = scodec.getTag();
assertEquals(Tag._SINT, tag);
assertEquals(10, scodec.readSmallInt(scodec.dis));
tag = scodec.getTag();
assertEquals(Tag._SINT, tag);
assertEquals(100, scodec.readSmallInt(scodec.dis));
tag = scodec.getTag();
assertEquals(Tag._STR, tag);
assertEquals("Hello!", scodec.readStr(fis));
try (FastJavaBinDecoder.StreamCodec scodec = new FastJavaBinDecoder.StreamCodec(fis)) {
scodec.start();
Tag tag = scodec.getTag();
assertEquals(Tag._SINT, tag);
assertEquals(10, scodec.readSmallInt(scodec.dis));
tag = scodec.getTag();
assertEquals(Tag._SINT, tag);
assertEquals(100, scodec.readSmallInt(scodec.dis));
tag = scodec.getTag();
assertEquals(Tag._STR, tag);
assertEquals("Hello!", scodec.readStr(fis));
}
}
public void testSimple() throws IOException {
@ -78,10 +79,14 @@ public class TestFastJavabinDecoder extends SolrTestCaseJ4 {
Map m = (Map) Utils.fromJSONString(sampleObj);
BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
new JavaBinCodec().marshal(m, baos);
Map m2 = (Map) new JavaBinCodec().unmarshal(new FastInputStream(null, baos.getbuf(), 0, baos.size()));
try (JavaBinCodec jbc = new JavaBinCodec()) {
jbc.marshal(m, baos);
}
Map m2;
try (JavaBinCodec jbc = new JavaBinCodec()) {
m2 = (Map) jbc.unmarshal(new FastInputStream(null, baos.getbuf(), 0, baos.size()));
}
LinkedHashMap fastMap = (LinkedHashMap) new FastJavaBinDecoder()
.withInputStream(new FastInputStream(null, baos.getbuf(), 0, baos.size()))
.decode(FastJavaBinDecoder.getEntryListener());
@ -113,7 +118,6 @@ public class TestFastJavabinDecoder extends SolrTestCaseJ4 {
((Map) m2.get("mapk")).remove("k2");
assertEquals(Utils.writeJson(m2, new StringWriter(), true).toString(),
Utils.writeJson(newMap, new StringWriter(), true).toString());
}
public void testFastJavabinStreamingDecoder() throws IOException {
@ -121,8 +125,13 @@ public class TestFastJavabinDecoder extends SolrTestCaseJ4 {
try (InputStream is = getClass().getResourceAsStream("/solrj/javabin_sample.bin")) {
IOUtils.copy(is, baos);
}
SimpleOrderedMap o = (SimpleOrderedMap) new JavaBinCodec().unmarshal(baos.toByteArray());
SolrDocumentList list = (SolrDocumentList) o.get("response");
SolrDocumentList list;
try (JavaBinCodec jbc = new JavaBinCodec()) {
SimpleOrderedMap o = (SimpleOrderedMap) jbc.unmarshal(baos.toByteArray());
list = (SolrDocumentList) o.get("response");
}
System.out.println(" " + list.getNumFound() + " , " + list.getStart() + " , " + list.getMaxScore());
class Pojo {
long _idx;
@ -172,10 +181,7 @@ public class TestFastJavabinDecoder extends SolrTestCaseJ4 {
assertEquals((Float) doc.get("price"), pojo.price, 0.001);
}
});
parser.processResponse(new FastInputStream(null, baos.getbuf(), 0, baos.size()), null);
}
public void testParsingWithChildDocs() throws IOException {
@ -195,7 +201,9 @@ public class TestFastJavabinDecoder extends SolrTestCaseJ4 {
orderedMap.add("response", sdocs);
BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
new JavaBinCodec().marshal(orderedMap, baos);
try (JavaBinCodec jbc = new JavaBinCodec()) {
jbc.marshal(orderedMap, baos);
}
boolean[] useListener = new boolean[1];
useListener[0] = true;

View File

@ -42,10 +42,9 @@ public class TestSolrJsonWriter extends SolrTestCaseJ4 {
.add("v632"));
});
new SolrJSONWriter(writer)
.setIndent(true)
.writeObj(map)
.close();
try (SolrJSONWriter jsonWriter = new SolrJSONWriter(writer)) {
jsonWriter.setIndent(true).writeObj(map);
}
Object o = Utils.fromJSONString(writer.toString());
assertEquals("v1", Utils.getObjectByPath(o, true, "k1"));
assertEquals(1l, Utils.getObjectByPath(o, true, "k2"));

View File

@ -35,9 +35,10 @@ public class Utf8CharSequenceTest extends SolrTestCaseJ4 {
ByteArrayUtf8CharSequence utf8 = new ByteArrayUtf8CharSequence(sb.toString());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[256];
FastOutputStream fos = new FastOutputStream(baos, buf, 0);
fos.writeUtf8CharSeq(utf8);
fos.flush();
try (FastOutputStream fos = new FastOutputStream(baos, buf, 0)) {
fos.writeUtf8CharSeq(utf8);
fos.flush();
}
byte[] result = baos.toByteArray();
ByteArrayUtf8CharSequence utf81 = new ByteArrayUtf8CharSequence(result, 0, result.length);
assertTrue(utf81.equals(utf8));
@ -50,13 +51,17 @@ public class Utf8CharSequenceTest extends SolrTestCaseJ4 {
Map m0 = new HashMap();
m0.put("str", utf8);
baos.reset();
new JavaBinCodec().marshal(m0, baos);
try (JavaBinCodec jbc = new JavaBinCodec()) {
jbc.marshal(m0, baos);
}
result = baos.toByteArray();
Map m1 = (Map) new JavaBinCodec()
.setReadStringAsCharSeq(true)
.unmarshal(new ByteArrayInputStream(result));
utf81 = (ByteArrayUtf8CharSequence) m1.get("str");
assertTrue(utf81.equals(utf8));
try (JavaBinCodec jbc = new JavaBinCodec()) {
Map m1 = (Map) jbc
.setReadStringAsCharSeq(true)
.unmarshal(new ByteArrayInputStream(result));
utf81 = (ByteArrayUtf8CharSequence) m1.get("str");
assertTrue(utf81.equals(utf8));
}
}
public void testUnMarshal() throws IOException {
@ -78,16 +83,22 @@ public class Utf8CharSequenceTest extends SolrTestCaseJ4 {
nl.add("key_long", sb.toString());
nl.add("key5", "5" + str);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new JavaBinCodec().marshal(nl, baos);
try (JavaBinCodec jbc = new JavaBinCodec()) {
jbc.marshal(nl, baos);
}
byte[] bytes = baos.toByteArray();
NamedList nl1 = (NamedList) new JavaBinCodec()
.setReadStringAsCharSeq(true)
.unmarshal(new ByteArrayInputStream( bytes, 0, bytes.length));
NamedList nl1;
try (JavaBinCodec jbc = new JavaBinCodec()) {
nl1 = (NamedList) jbc
.setReadStringAsCharSeq(true)
.unmarshal(new ByteArrayInputStream(bytes, 0, bytes.length));
}
byte[] buf = ((ByteArrayUtf8CharSequence) nl1.getVal(0)).getBuf();
ByteArrayUtf8CharSequence valLong = (ByteArrayUtf8CharSequence) nl1.get("key_long");
assertFalse(valLong.getBuf() == buf);
for (int i = 1; i < 6; i++) {
ByteArrayUtf8CharSequence val = (ByteArrayUtf8CharSequence) nl1.get("key" + i);
assertEquals(buf, val.getBuf());