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

Try to finish remote sink once #117592

Merged
merged 1 commit into from
Nov 27, 2024
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 @@ -42,6 +42,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

/**
Expand Down Expand Up @@ -292,6 +293,7 @@ static final class TransportRemoteSink implements RemoteSink {
final Executor responseExecutor;

final AtomicLong estimatedPageSizeInBytes = new AtomicLong(0L);
final AtomicBoolean finished = new AtomicBoolean(false);

TransportRemoteSink(
TransportService transportService,
Expand All @@ -311,6 +313,32 @@ static final class TransportRemoteSink implements RemoteSink {

@Override
public void fetchPageAsync(boolean allSourcesFinished, ActionListener<ExchangeResponse> listener) {
if (allSourcesFinished) {
if (finished.compareAndSet(false, true)) {
doFetchPageAsync(true, listener);
} else {
// already finished or promised
listener.onResponse(new ExchangeResponse(blockFactory, null, true));
}
} else {
// already finished
if (finished.get()) {
listener.onResponse(new ExchangeResponse(blockFactory, null, true));
return;
}
doFetchPageAsync(false, ActionListener.wrap(r -> {
if (r.finished()) {
finished.set(true);
}
listener.onResponse(r);
}, e -> {
finished.set(true);
listener.onFailure(e);
}));
}
}

private void doFetchPageAsync(boolean allSourcesFinished, ActionListener<ExchangeResponse> listener) {
final long reservedBytes = allSourcesFinished ? 0 : estimatedPageSizeInBytes.get();
if (reservedBytes > 0) {
// This doesn't fully protect ESQL from OOM, but reduces the likelihood.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,15 @@ public void testConcurrentWithTransportActions() {
ExchangeService exchange1 = new ExchangeService(Settings.EMPTY, threadPool, ESQL_TEST_EXECUTOR, blockFactory());
exchange1.registerTransportHandler(node1);
AbstractSimpleTransportTestCase.connectToNode(node0, node1.getLocalNode());
Set<String> finishingRequests = ConcurrentCollections.newConcurrentSet();
node1.addRequestHandlingBehavior(ExchangeService.EXCHANGE_ACTION_NAME, (handler, request, channel, task) -> {
final ExchangeRequest exchangeRequest = (ExchangeRequest) request;
if (exchangeRequest.sourcesFinished()) {
String exchangeId = exchangeRequest.exchangeId();
assertTrue("tried to finish [" + exchangeId + "] twice", finishingRequests.add(exchangeId));
}
handler.messageReceived(request, channel, task);
});

try (exchange0; exchange1; node0; node1) {
String exchangeId = "exchange";
Expand Down