Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -136,14 +136,32 @@ static <Response> ActionListener<Response> wrap(Runnable runnable) {
* Creates a listener that wraps another listener, mapping response values via the given mapping function and passing along
* exceptions to the delegate.
*
* Notice that if the listener onResponse handler fails, the exception will bubble out, whereas if the function fails, the listeners
* onFailure handler will be called. The principle is that the code using this is responsible for the function, whereas the listener
* should do its own exception handling since it is a different component.
*
* @param listener Listener to delegate to
* @param fn Function to apply to listener response
* @param <Response> Response type of the new listener
* @param <T> Response type of the wrapped listener
* @return a listener that maps the received response and then passes it to its delegate listener
*/
static <T, Response> ActionListener<Response> map(ActionListener<T> listener, CheckedFunction<Response, T, Exception> fn) {
return wrap(r -> listener.onResponse(fn.apply(r)), listener::onFailure);
return delegateFailure(listener, (ActionListener<T> delegate, Response response) -> {
T mapped;
try {
mapped = fn.apply(response);
} catch (Exception e) {
delegate.onFailure(e);
return;
}
try {
delegate.onResponse(mapped);
} catch (RuntimeException e) {
assert false : new AssertionError("map: listener.onResponse failed", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

why assert here but not when calling delegate.onFailure(e);?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, added that in ca31964 and tests seems unaffected.

throw e;
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import org.elasticsearch.transport.TransportService;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -306,27 +305,11 @@ public static void registerRequestHandler(TransportService transportService, Sea
(in) -> TransportResponse.Empty.INSTANCE);

transportService.registerRequestHandler(DFS_ACTION_NAME, ThreadPool.Names.SAME, ShardSearchRequest::new,
(request, channel, task) -> {
searchService.executeDfsPhase(request, (SearchShardTask) task, new ActionListener<SearchPhaseResult>() {
@Override
public void onResponse(SearchPhaseResult searchPhaseResult) {
try {
channel.sendResponse(searchPhaseResult);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public void onFailure(Exception e) {
try {
channel.sendResponse(e);
} catch (IOException e1) {
throw new UncheckedIOException(e1);
}
}
});
});
(request, channel, task) ->
searchService.executeDfsPhase(request, (SearchShardTask) task,
new ChannelActionListener<>(channel, DFS_ACTION_NAME, request))
Copy link
Contributor

Choose a reason for hiding this comment

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

ChannelActionListener also has this weird double-sending logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but the code used to propagate the exception out. This seemed to be a left-over from when the ChannelActionListener was introduced. The old map exception handling would ensure onFailure were called on exception. This means this change is effectively a no-op now (with the caveat that onFailure will be called on exceptions like for all other ChannelActionListener usages).

We should notice that DirectTransportChannel will not bubble out exceptions from invoking the TransportResponseHandler. So it seems likely that the primary exceptions bubbled out are related to communicating the response over a wire, in which case invoking onFailure might be desirable (for instance an NPE on serialization).

So I would like to keep using ChannelActionListener here and then deal with ChannelActionListener in a follow-up. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

++

);

TransportActionProxy.registerProxyAction(transportService, DFS_ACTION_NAME, DfsSearchResult::new);

transportService.registerRequestHandler(QUERY_ACTION_NAME, ThreadPool.Names.SAME, ShardSearchRequest::new,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,44 @@ public void testCompleteWith() {
assertThat(onFailureListener.isDone(), equalTo(true));
assertThat(expectThrows(ExecutionException.class, onFailureListener::get).getCause(), instanceOf(IOException.class));
}

/**
* Test that map passes the output of the function to its delegate listener and that exceptions in the function are propagated to the
* onFailure handler. Also verify that exceptions from ActionListener.onResponse does not invoke onFailure, since it is the
* responsibility of the ActionListener implementation (the client of the API) to handle exceptions in onResponse and onFailure.
*/
public void testMap() {
AtomicReference<Exception> exReference = new AtomicReference<>();

ActionListener<String> listener = new ActionListener<>() {
@Override
public void onResponse(String s) {
if (s == null) {
throw new IllegalArgumentException("simulate onResponse exception");
}
}

@Override
public void onFailure(Exception e) {
exReference.set(e);
}
};
ActionListener<Boolean> mapped = ActionListener.map(listener, b -> {
if (b == null) {
return null;
} else if (b) {
throw new IllegalStateException("simulate map function exception");
} else {
return b.toString();
}
});

AssertionError assertionError = expectThrows(AssertionError.class, () -> mapped.onResponse(null));
assertThat(assertionError.getCause().getCause(), instanceOf(IllegalArgumentException.class));
assertNull(exReference.get());
mapped.onResponse(false);
assertNull(exReference.get());
mapped.onResponse(true);
assertThat(exReference.get(), instanceOf(IllegalStateException.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,4 @@ public void testIndexNameInResponse() {

assertEquals("Should have index name in response", "foo", response.index());
}

}