Skip to content

Commit 5d4324c

Browse files
Use JDK's cleaner to always pool when reading source in SearchHit
WIP, we can make this a little more efficient by not doing redundant wrapping but I'd like a test run first.
1 parent 8bca164 commit 5d4324c

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

server/src/main/java/org/elasticsearch/common/bytes/ReleasableBytesReference.java

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public ReleasableBytesReference retainedSlice(int from, int length) {
8484
return new ReleasableBytesReference(slice, refCounted);
8585
}
8686

87+
public RefCounted getRefCounted() {
88+
return refCounted;
89+
}
90+
8791
@Override
8892
public void close() {
8993
refCounted.decRef();

server/src/main/java/org/elasticsearch/search/SearchHit.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.common.Strings;
1515
import org.elasticsearch.common.bytes.BytesArray;
1616
import org.elasticsearch.common.bytes.BytesReference;
17+
import org.elasticsearch.common.bytes.ReleasableBytesReference;
1718
import org.elasticsearch.common.compress.CompressorFactory;
1819
import org.elasticsearch.common.document.DocumentField;
1920
import org.elasticsearch.common.io.stream.StreamInput;
@@ -210,9 +211,13 @@ public static SearchHit readFrom(StreamInput in, boolean pooled) throws IOExcept
210211
final long version = in.readLong();
211212
final long seqNo = in.readZLong();
212213
final long primaryTerm = in.readVLong();
213-
BytesReference source = pooled ? in.readReleasableBytesReference() : in.readBytesReference();
214+
ReleasableBytesReference source = in.readReleasableBytesReference();
214215
if (source.length() == 0) {
215216
source = null;
217+
} else {
218+
if (pooled == false) {
219+
source = LeakTracker.releaseEventually(source);
220+
}
216221
}
217222
Explanation explanation = null;
218223
if (in.readBoolean()) {

server/src/main/java/org/elasticsearch/transport/LeakTracker.java

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
1313
import org.elasticsearch.common.Randomness;
14+
import org.elasticsearch.common.bytes.ReleasableBytesReference;
1415
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
1516
import org.elasticsearch.common.util.set.Sets;
1617
import org.elasticsearch.core.Assertions;
@@ -63,6 +64,12 @@ public static void setContextHint(String hint) {
6364
contextHint = hint;
6465
}
6566

67+
public static ReleasableBytesReference releaseEventually(ReleasableBytesReference referent) {
68+
final RefCounted refCounted = referent.getRefCounted();
69+
var cleanable = cleaner.register(referent, refCounted::decRef);
70+
return new ReleasableBytesReference(referent, cleanable::clean);
71+
}
72+
6673
public static Releasable wrap(Releasable releasable) {
6774
if (Assertions.ENABLED == false) {
6875
return releasable;

0 commit comments

Comments
 (0)