Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Aggregations Directly from Pooled Buffers #72309

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private MergeResult partialReduce(QuerySearchResult[] toConsume,
aggsList.add(lastMerge.reducedAggs);
}
for (QuerySearchResult result : toConsume) {
aggsList.add(result.consumeAggs().expand());
aggsList.add(result.consumeAggs());
}
newAggs = InternalAggregations.topLevelReduce(aggsList, aggReduceContextBuilder.forPartialReduction());
} else {
Expand Down Expand Up @@ -310,6 +310,7 @@ public void consume(QuerySearchResult result, Runnable next) {
try {
addEstimateAndMaybeBreak(aggsSize);
} catch (Exception exc) {
result.releaseAggs();
onMergeFailure(exc);
next.run();
return;
Expand Down Expand Up @@ -458,7 +459,7 @@ public synchronized List<InternalAggregations> consumeAggs() {
aggsList.add(mergeResult.reducedAggs);
}
for (QuerySearchResult result : buffer) {
aggsList.add(result.consumeAggs().expand());
aggsList.add(result.consumeAggs());
}
return aggsList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
package org.elasticsearch.common.io.stream;

import org.elasticsearch.Version;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.bytes.ReleasableBytesReference;
import org.elasticsearch.common.lease.Releasable;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand All @@ -32,7 +33,7 @@
* to force their buffering in serialized format by calling
* {@link #asSerialized(Reader, NamedWriteableRegistry)}.
*/
public abstract class DelayableWriteable<T extends Writeable> implements Writeable {
public abstract class DelayableWriteable<T extends Writeable> implements Writeable, Releasable {
/**
* Build a {@linkplain DelayableWriteable} that wraps an existing object
* but is serialized so that deserializing it can be delayed.
Expand All @@ -46,7 +47,7 @@ public static <T extends Writeable> DelayableWriteable<T> referencing(T referenc
* when {@link #expand()} is called.
*/
public static <T extends Writeable> DelayableWriteable<T> delayed(Writeable.Reader<T> reader, StreamInput in) throws IOException {
return new Serialized<>(reader, in.getVersion(), in.namedWriteableRegistry(), in.readBytesReference());
return new Serialized<>(reader, in.getVersion(), in.namedWriteableRegistry(), in.readReleasableBytesReference());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would literally create O(100M) sized byte arrays in a recent user issue, completely locking up GC on the coordinating node.

}

private DelayableWriteable() {}
Expand Down Expand Up @@ -98,7 +99,8 @@ public Serialized<T> asSerialized(Reader<T> reader, NamedWriteableRegistry regis
} catch (IOException e) {
throw new RuntimeException("unexpected error writing writeable to buffer", e);
}
return new Serialized<>(reader, Version.CURRENT, registry, buffer.bytes());
// TODO: this path is currently not used in production code, if it ever is this should start using pooled buffers
return new Serialized<>(reader, Version.CURRENT, registry, ReleasableBytesReference.wrap(buffer.bytes()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

production mode activated!

}

@Override
Expand All @@ -118,19 +120,25 @@ private BytesStreamOutput writeToBuffer(Version version) throws IOException {
return buffer;
}
}

@Override
public void close() {
//noop
}
}

/**
* A {@link Writeable} stored in serialized form.
* A {@link Writeable} stored in serialized form backed by a {@link ReleasableBytesReference}. Once an instance is no longer used its
* backing memory must be manually released by invoking {@link #close()} on it.
*/
public static class Serialized<T extends Writeable> extends DelayableWriteable<T> {
private final Writeable.Reader<T> reader;
private final Version serializedAtVersion;
private final NamedWriteableRegistry registry;
private final BytesReference serialized;
private final ReleasableBytesReference serialized;

private Serialized(Writeable.Reader<T> reader, Version serializedAtVersion,
NamedWriteableRegistry registry, BytesReference serialized) {
private Serialized(Writeable.Reader<T> reader, Version serializedAtVersion, NamedWriteableRegistry registry,
ReleasableBytesReference serialized) {
this.reader = reader;
this.serializedAtVersion = serializedAtVersion;
this.registry = registry;
Expand Down Expand Up @@ -186,6 +194,11 @@ public long getSerializedSize() {
// We're already serialized
return serialized.length();
}

@Override
public void close() {
serialized.close();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,23 @@ public boolean hasAggs() {
* Returns and nulls out the aggregation for this search results. This allows to free up memory once the aggregation is consumed.
* @throws IllegalStateException if the aggregations have already been consumed.
*/
public DelayableWriteable<InternalAggregations> consumeAggs() {
public InternalAggregations consumeAggs() {
if (aggregations == null) {
throw new IllegalStateException("aggs already consumed");
}
DelayableWriteable<InternalAggregations> aggs = aggregations;
aggregations = null;
return aggs;
try {
return aggregations.expand();
} finally {
aggregations.close();
aggregations = null;
}
}

public void releaseAggs() {
if (aggregations != null) {
aggregations.close();
aggregations = null;
}
}

public void aggregations(InternalAggregations aggregations) {
Expand Down Expand Up @@ -233,8 +243,9 @@ public void consumeAll() {
if (hasConsumedTopDocs() == false) {
consumeTopDocs();
}
if (hasAggs()) {
consumeAggs();
if (aggregations != null) {
aggregations.close();
aggregations = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public void testSerialization() throws Exception {
assertEquals(querySearchResult.size(), deserialized.size());
assertEquals(querySearchResult.hasAggs(), deserialized.hasAggs());
if (deserialized.hasAggs()) {
Aggregations aggs = querySearchResult.consumeAggs().expand();
Aggregations deserializedAggs = deserialized.consumeAggs().expand();
Aggregations aggs = querySearchResult.consumeAggs();
Aggregations deserializedAggs = deserialized.consumeAggs();
assertEquals(aggs.asList(), deserializedAggs.asList());
}
assertEquals(querySearchResult.terminatedEarly(), deserialized.terminatedEarly());
Expand Down