use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener in project riposte by Nike-Inc.
the class OpenChannelLimitHandlerTest method doChannelActive_marks_and_schedules_double_check_timeout_if_too_many_open_channels.
@DataProvider(value = { "0", "1" })
@Test
public void doChannelActive_marks_and_schedules_double_check_timeout_if_too_many_open_channels(int numOpenChannelsGreaterThanMax) throws Exception {
// given
int actualOpenChannels = maxOpenChannelsThreshold + numOpenChannelsGreaterThanMax;
setActualOpenChannels(actualOpenChannels);
// when
PipelineContinuationBehavior result = handler.doChannelActive(ctxMock);
// then
assertThat(result).isEqualTo(CONTINUE);
Pair<Runnable, GenericFutureListener> futureInfoPair = extractDoubleCheckRunnableAndCloseFutureListener();
verify(tooManyOpenConnectionsAttributeMock).set(actualOpenChannels);
verifyDoubleCheckFuture(futureInfoPair.getLeft());
verifyCloseFutureListener(futureInfoPair.getRight());
verify(channelGroupMock, never()).add(channelMock);
}
use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener in project riposte by Nike-Inc.
the class ChannelPipelineFinalizerHandlerTest method finalizeChannelPipeline_should_send_event_to_metricsListener_for_failure_response_and_flush_context.
@Test
public void finalizeChannelPipeline_should_send_event_to_metricsListener_for_failure_response_and_flush_context() throws Exception {
// given
ChannelFuture responseWriterChannelFuture = mock(ChannelFuture.class);
state.setResponseWriterFinalChunkChannelFuture(responseWriterChannelFuture);
HttpProcessingState stateSpy = spy(state);
doReturn(stateSpy).when(stateAttributeMock).get();
ChannelFuture responseWriteFutureResult = mock(ChannelFuture.class);
doReturn(false).when(responseWriteFutureResult).isSuccess();
Assertions.assertThat(stateSpy.isRequestMetricsRecordedOrScheduled()).isFalse();
// when
handler.finalizeChannelPipeline(ctxMock, null, stateSpy, null);
// then
ArgumentCaptor<GenericFutureListener> channelFutureListenerArgumentCaptor = ArgumentCaptor.forClass(GenericFutureListener.class);
verify(responseWriterChannelFuture).addListener(channelFutureListenerArgumentCaptor.capture());
GenericFutureListener futureListener = channelFutureListenerArgumentCaptor.getValue();
assertThat(futureListener, notNullValue());
futureListener.operationComplete(responseWriteFutureResult);
verify(metricsListenerMock).onEvent(ServerMetricsEvent.RESPONSE_WRITE_FAILED, null);
verify(ctxMock).flush();
Assertions.assertThat(stateSpy.isRequestMetricsRecordedOrScheduled()).isTrue();
}
use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener in project riposte by Nike-Inc.
the class ChannelPipelineFinalizerHandlerTest method finalizeChannelPipeline_should_send_event_to_metricsListener_for_successful_response_and_flush_context.
@Test
public void finalizeChannelPipeline_should_send_event_to_metricsListener_for_successful_response_and_flush_context() throws Exception {
// given
ChannelFuture responseWriterChannelFuture = mock(ChannelFuture.class);
state.setResponseWriterFinalChunkChannelFuture(responseWriterChannelFuture);
HttpProcessingState stateSpy = spy(state);
doReturn(stateSpy).when(stateAttributeMock).get();
ChannelFuture responseWriteFutureResult = mock(ChannelFuture.class);
doReturn(true).when(responseWriteFutureResult).isSuccess();
Assertions.assertThat(stateSpy.isRequestMetricsRecordedOrScheduled()).isFalse();
// when
handler.finalizeChannelPipeline(ctxMock, null, stateSpy, null);
// then
ArgumentCaptor<GenericFutureListener> channelFutureListenerArgumentCaptor = ArgumentCaptor.forClass(GenericFutureListener.class);
verify(responseWriterChannelFuture).addListener(channelFutureListenerArgumentCaptor.capture());
GenericFutureListener futureListener = channelFutureListenerArgumentCaptor.getValue();
assertThat(futureListener, notNullValue());
futureListener.operationComplete(responseWriteFutureResult);
verify(metricsListenerMock).onEvent(eq(ServerMetricsEvent.RESPONSE_SENT), any(HttpProcessingState.class));
verify(ctxMock).flush();
Assertions.assertThat(stateSpy.isRequestMetricsRecordedOrScheduled()).isTrue();
}
use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener in project riposte by Nike-Inc.
the class StreamingAsyncHttpClientTest method StreamingChannel_streamChunk_sets_up_task_in_event_loop_to_call_doStreamChunk_and_adds_listener_to_complete_promise.
@Test
public void StreamingChannel_streamChunk_sets_up_task_in_event_loop_to_call_doStreamChunk_and_adds_listener_to_complete_promise() throws Exception {
// given
ChannelFuture doStreamChunkFutureMock = mock(ChannelFuture.class);
doReturn(doStreamChunkFutureMock).when(streamingChannelSpy).doStreamChunk(any(HttpContent.class));
// when
ChannelFuture result = streamingChannelSpy.streamChunk(contentChunkMock);
// then
assertThat(result).isSameAs(streamChunkChannelPromiseMock);
// not yet completed
verifyNoInteractions(streamChunkChannelPromiseMock);
ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class);
verify(eventLoopMock).execute(taskCaptor.capture());
Runnable task = taskCaptor.getValue();
// and given
// not yet called
verify(streamingChannelSpy, never()).doStreamChunk(any(HttpContent.class));
// when
task.run();
// then
verify(streamingChannelSpy).doStreamChunk(contentChunkMock);
ArgumentCaptor<GenericFutureListener> listenerCaptor = ArgumentCaptor.forClass(GenericFutureListener.class);
verify(doStreamChunkFutureMock).addListener(listenerCaptor.capture());
GenericFutureListener listener = listenerCaptor.getValue();
assertThat(listener).isNotNull();
// and when
listener.operationComplete(getFutureForCase(true, false, null));
// then
verify(streamChunkChannelPromiseMock).cancel(true);
verifyNoMoreInteractions(streamChunkChannelPromiseMock);
// and when
listener.operationComplete(getFutureForCase(false, true, null));
// then
verify(streamChunkChannelPromiseMock).setSuccess();
verifyNoMoreInteractions(streamChunkChannelPromiseMock);
// and when
Throwable normalFutureFailure = new RuntimeException("normal future failure");
listener.operationComplete(getFutureForCase(false, false, normalFutureFailure));
// then
verify(streamChunkChannelPromiseMock).setFailure(normalFutureFailure);
verifyNoMoreInteractions(streamChunkChannelPromiseMock);
// and when
reset(streamChunkChannelPromiseMock);
listener.operationComplete(getFutureForCase(false, false, null));
// then
ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
verify(streamChunkChannelPromiseMock).setFailure(throwableCaptor.capture());
assertThat(throwableCaptor.getValue()).hasMessage("Received ChannelFuture that was in an impossible state");
verifyNoMoreInteractions(streamChunkChannelPromiseMock);
}
use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener in project ambry by linkedin.
the class Http2NetworkClient method sendAndPoll.
@Override
public List<ResponseInfo> sendAndPoll(List<RequestInfo> requestsToSend, Set<Integer> requestsToDrop, int pollTimeoutMs) {
List<ResponseInfo> readyResponseInfos = new ArrayList<>();
if (requestsToDrop.size() != 0) {
logger.warn("Number of requestsToDrop: {}", requestsToDrop.size());
http2ClientMetrics.http2RequestsToDropCount.inc(requestsToDrop.size());
for (int correlationId : requestsToDrop) {
Channel streamChannel = correlationIdInFlightToChannelMap.remove(correlationId);
if (streamChannel != null) {
logger.warn("Drop request on streamChannel: {}", streamChannel);
// Drop request just generates a ResponseInfo with TimeoutError to router.
// The stream is still transmitting, but router will ignore ResponseInfo with the same correlationId.
// We need stream reset to cancel the stream in transmitting.
RequestInfo requestInfo = streamChannel.attr(Http2NetworkClient.REQUEST_INFO).get();
if (requestInfo != null) {
readyResponseInfos.add(new ResponseInfo(requestInfo, NetworkClientErrorCode.TimeoutError, null));
}
}
}
}
long sendStartTime = System.currentTimeMillis();
// Send request
http2ClientMetrics.http2ClientSendRate.mark(requestsToSend.size());
for (RequestInfo requestInfo : requestsToSend) {
long streamInitiateTime = System.currentTimeMillis();
long waitingTime = streamInitiateTime - requestInfo.getRequestCreateTime();
http2ClientMetrics.requestToNetworkClientLatencyMs.update(waitingTime);
this.pools.get(InetSocketAddress.createUnresolved(requestInfo.getHost(), requestInfo.getPort().getPort())).acquire().addListener((GenericFutureListener<Future<Channel>>) future -> {
if (future.isSuccess()) {
http2ClientMetrics.http2StreamAcquireTime.update(System.currentTimeMillis() - streamInitiateTime);
long streamAcquiredTime = System.currentTimeMillis();
Channel streamChannel = future.getNow();
correlationIdInFlightToChannelMap.put(requestInfo.getRequest().getCorrelationId(), streamChannel);
streamChannel.attr(REQUEST_INFO).set(requestInfo);
if (!streamChannel.isWritable() || !streamChannel.parent().isWritable()) {
http2ClientMetrics.http2StreamNotWritableCount.inc();
logger.debug("Stream {} {} not writable. BytesBeforeWritable {} {}", streamChannel.hashCode(), streamChannel, streamChannel.bytesBeforeWritable(), streamChannel.parent().bytesBeforeWritable());
}
streamChannel.writeAndFlush(requestInfo.getRequest()).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
long writeAndFlushUsedTime = System.currentTimeMillis() - streamAcquiredTime;
http2ClientMetrics.http2StreamWriteAndFlushTime.update(writeAndFlushUsedTime);
requestInfo.setStreamSendTime(System.currentTimeMillis());
if (writeAndFlushUsedTime > http2ClientConfig.http2WriteAndFlushTimeoutMs) {
logger.debug("WriteAndFlush exceeds http2RequestTimeoutMs {}ms, used time: {}ms, stream channel {}", http2ClientConfig.http2WriteAndFlushTimeoutMs, writeAndFlushUsedTime, streamChannel);
if (http2ClientConfig.http2DropRequestOnWriteAndFlushTimeout) {
RequestInfo requestInfoFromChannelAttr = releaseAndCloseStreamChannel(streamChannel);
if (requestInfoFromChannelAttr != null) {
http2ClientResponseHandler.getResponseInfoQueue().put(new ResponseInfo(requestInfo, NetworkClientErrorCode.NetworkError, null));
}
}
}
} else {
http2ClientMetrics.http2StreamWriteAndFlushErrorCount.inc();
logger.warn("Stream {} {} writeAndFlush fail. Cause: {}", streamChannel.hashCode(), streamChannel, future.cause().toString());
RequestInfo requestInfoFromChannelAttr = releaseAndCloseStreamChannel(streamChannel);
if (requestInfoFromChannelAttr != null) {
http2ClientResponseHandler.getResponseInfoQueue().put(new ResponseInfo(requestInfoFromChannelAttr, NetworkClientErrorCode.NetworkError, null));
}
}
}
});
} else {
logger.error("Couldn't acquire stream channel to {}:{} . Cause:", requestInfo.getHost(), requestInfo.getPort().getPort(), future.cause());
requestInfo.getRequest().release();
http2ClientResponseHandler.getResponseInfoQueue().put(new ResponseInfo(requestInfo, NetworkClientErrorCode.NetworkError, null));
}
});
}
http2ClientMetrics.http2ClientSendTime.update(System.currentTimeMillis() - sendStartTime);
http2ClientResponseHandler.getResponseInfoQueue().poll(readyResponseInfos, pollTimeoutMs);
for (ResponseInfo responseInfo : readyResponseInfos) {
correlationIdInFlightToChannelMap.remove(responseInfo.getRequestInfo().getRequest().getCorrelationId());
}
http2ClientMetrics.http2ClientSendRate.mark(readyResponseInfos.size());
http2ClientMetrics.http2ClientSendAndPollTime.update(System.currentTimeMillis() - sendStartTime);
return readyResponseInfos;
}
Aggregations