Search in sources :

Example 1 with CircuitBreakerDelegate

use of com.nike.fastbreak.CircuitBreakerDelegate in project riposte by Nike-Inc.

the class AsyncHttpClientHelper method getCircuitBreaker.

protected Optional<CircuitBreaker<Response>> getCircuitBreaker(RequestBuilderWrapper requestBuilderWrapper) {
    if (requestBuilderWrapper.disableCircuitBreaker)
        return Optional.empty();
    //      custom one is not specified.
    if (requestBuilderWrapper.customCircuitBreaker.isPresent())
        return requestBuilderWrapper.customCircuitBreaker;
    // No custom circuit breaker. Use the default for the given request's host.
    Uri uri = Uri.create(requestBuilderWrapper.url);
    String host = uri.getHost();
    EventLoop nettyEventLoop = requestBuilderWrapper.getCtx() == null ? null : requestBuilderWrapper.getCtx().channel().eventLoop();
    CircuitBreaker<Integer> defaultStatusCodeCircuitBreaker = getDefaultHttpStatusCodeCircuitBreakerForKey(host, Optional.ofNullable(nettyEventLoop), Optional.ofNullable(nettyEventLoop));
    return Optional.of(new CircuitBreakerDelegate<>(defaultStatusCodeCircuitBreaker, response -> (response == null ? null : response.getStatusCode())));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) Span(com.nike.wingtips.Span) CircuitBreakerDelegate(com.nike.fastbreak.CircuitBreakerDelegate) NameResolver(com.ning.http.client.NameResolver) ManualModeTask(com.nike.fastbreak.CircuitBreaker.ManualModeTask) LoggerFactory(org.slf4j.LoggerFactory) TraceHeaders(com.nike.wingtips.TraceHeaders) Tracer(com.nike.wingtips.Tracer) CompletableFuture(java.util.concurrent.CompletableFuture) Deque(java.util.Deque) CircuitBreakerOpenException(com.nike.fastbreak.exception.CircuitBreakerOpenException) ConcurrentMap(java.util.concurrent.ConcurrentMap) InetAddress(java.net.InetAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) Response(com.ning.http.client.Response) Logger(org.slf4j.Logger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HttpMethod(io.netty.handler.codec.http.HttpMethod) CircuitBreakerForHttpStatusCode.getDefaultHttpStatusCodeCircuitBreakerForKey(com.nike.fastbreak.CircuitBreakerForHttpStatusCode.getDefaultHttpStatusCodeCircuitBreakerForKey) EventLoop(io.netty.channel.EventLoop) UnknownHostException(java.net.UnknownHostException) TimeUnit(java.util.concurrent.TimeUnit) ChannelAttributes(com.nike.riposte.server.channelpipeline.ChannelAttributes) CircuitBreaker(com.nike.fastbreak.CircuitBreaker) MDC(org.slf4j.MDC) Optional(java.util.Optional) Uri(com.ning.http.client.uri.Uri) EventLoop(io.netty.channel.EventLoop) Uri(com.ning.http.client.uri.Uri)

Example 2 with CircuitBreakerDelegate

use of com.nike.fastbreak.CircuitBreakerDelegate in project riposte by Nike-Inc.

the class ProxyRouterEndpointExecutionHandler method getCircuitBreaker.

protected Optional<CircuitBreaker<HttpResponse>> getCircuitBreaker(DownstreamRequestFirstChunkInfo downstreamReqFirstChunkInfo, ChannelHandlerContext ctx) {
    if (downstreamReqFirstChunkInfo == null || downstreamReqFirstChunkInfo.disableCircuitBreaker)
        return Optional.empty();
    //      custom one is not specified.
    if (downstreamReqFirstChunkInfo.customCircuitBreaker.isPresent())
        return downstreamReqFirstChunkInfo.customCircuitBreaker;
    // No custom circuit breaker. Use the default for the given request's host.
    EventLoop nettyEventLoop = ctx.channel().eventLoop();
    CircuitBreaker<Integer> defaultStatusCodeCircuitBreaker = getDefaultHttpStatusCodeCircuitBreakerForKey(downstreamReqFirstChunkInfo.host, Optional.ofNullable(nettyEventLoop), Optional.ofNullable(nettyEventLoop));
    return Optional.of(new CircuitBreakerDelegate<>(defaultStatusCodeCircuitBreaker, httpResponse -> (httpResponse == null ? null : httpResponse.getStatus().code())));
}
Also used : Span(com.nike.wingtips.Span) LoggerFactory(org.slf4j.LoggerFactory) ResponseInfo(com.nike.riposte.server.http.ResponseInfo) HttpObject(io.netty.handler.codec.http.HttpObject) ProxyRouterEndpoint(com.nike.riposte.server.http.ProxyRouterEndpoint) Map(java.util.Map) HttpRequest(io.netty.handler.codec.http.HttpRequest) CompletionException(java.util.concurrent.CompletionException) EventLoop(io.netty.channel.EventLoop) DownstreamRequestFirstChunkInfo(com.nike.riposte.server.http.ProxyRouterEndpoint.DownstreamRequestFirstChunkInfo) BaseInboundHandlerWithTracingAndMdcSupport(com.nike.riposte.server.handler.base.BaseInboundHandlerWithTracingAndMdcSupport) Endpoint(com.nike.riposte.server.http.Endpoint) HttpUtils(com.nike.riposte.util.HttpUtils) StreamingChannel(com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient.StreamingChannel) ChannelAttributes(com.nike.riposte.server.channelpipeline.ChannelAttributes) CircuitBreaker(com.nike.fastbreak.CircuitBreaker) RiposteInternalRequestInfo(com.nike.riposte.server.http.impl.RiposteInternalRequestInfo) Optional(java.util.Optional) HttpResponse(io.netty.handler.codec.http.HttpResponse) StreamingCallback(com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient.StreamingCallback) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) EventExecutor(io.netty.util.concurrent.EventExecutor) RequestInfo(com.nike.riposte.server.http.RequestInfo) CircuitBreakerDelegate(com.nike.fastbreak.CircuitBreakerDelegate) ManualModeTask(com.nike.fastbreak.CircuitBreaker.ManualModeTask) CompletableFuture(java.util.concurrent.CompletableFuture) StreamingAsyncHttpClient(com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient) PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) Deque(java.util.Deque) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) AsyncNettyHelper(com.nike.riposte.util.AsyncNettyHelper) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpContent(io.netty.handler.codec.http.HttpContent) Attribute(io.netty.util.Attribute) OutboundMessageSendHeadersChunkFromResponseInfo(com.nike.riposte.server.channelpipeline.message.OutboundMessageSendHeadersChunkFromResponseInfo) Logger(org.slf4j.Logger) Executor(java.util.concurrent.Executor) AsyncNettyHelper.executeOnlyIfChannelIsActive(com.nike.riposte.util.AsyncNettyHelper.executeOnlyIfChannelIsActive) CircuitBreakerForHttpStatusCode.getDefaultHttpStatusCodeCircuitBreakerForKey(com.nike.fastbreak.CircuitBreakerForHttpStatusCode.getDefaultHttpStatusCodeCircuitBreakerForKey) AsyncNettyHelper.functionWithTracingAndMdc(com.nike.riposte.util.AsyncNettyHelper.functionWithTracingAndMdc) ChannelFuture(io.netty.channel.ChannelFuture) ExecutionException(java.util.concurrent.ExecutionException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) WrapperException(com.nike.backstopper.exception.WrapperException) OutboundMessageSendContentChunk(com.nike.riposte.server.channelpipeline.message.OutboundMessageSendContentChunk) AsyncNettyHelper.runnableWithTracingAndMdc(com.nike.riposte.util.AsyncNettyHelper.runnableWithTracingAndMdc) LastOutboundMessageSendLastContentChunk(com.nike.riposte.server.channelpipeline.message.LastOutboundMessageSendLastContentChunk) Pair(com.nike.internal.util.Pair) ProxyRouterProcessingState(com.nike.riposte.server.http.ProxyRouterProcessingState) EventLoop(io.netty.channel.EventLoop)

Example 3 with CircuitBreakerDelegate

use of com.nike.fastbreak.CircuitBreakerDelegate in project riposte by Nike-Inc.

the class AsyncHttpClientHelperTest method getCircuitBreaker_returns_CircuitBreakerDelegate_wrapping_default_CircuitBreakerForHttpStatusCode_using_host_as_the_key.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void getCircuitBreaker_returns_CircuitBreakerDelegate_wrapping_default_CircuitBreakerForHttpStatusCode_using_host_as_the_key(boolean useNettyEventLoop) {
    // given
    String host = UUID.randomUUID().toString();
    String url = "http://" + host + "/some/path";
    String method = "GET";
    AsyncHttpClient.BoundRequestBuilder reqMock = mock(AsyncHttpClient.BoundRequestBuilder.class);
    Optional<CircuitBreaker<Response>> customCb = Optional.empty();
    RequestBuilderWrapper rbw = new RequestBuilderWrapper(url, method, reqMock, customCb, false);
    if (useNettyEventLoop)
        rbw.setCtx(ctxMock);
    // when
    Optional<CircuitBreaker<Response>> result = helperSpy.getCircuitBreaker(rbw);
    // then
    assertThat(result).isPresent();
    assertThat(result.get()).isInstanceOf(CircuitBreakerDelegate.class);
    CircuitBreakerDelegate<Response, Integer> wrapper = (CircuitBreakerDelegate) result.get();
    CircuitBreaker<Integer> delegate = (CircuitBreaker<Integer>) Whitebox.getInternalState(wrapper, "delegate");
    Function<Response, Integer> eventConverter = (Function<Response, Integer>) Whitebox.getInternalState(wrapper, "eventConverter");
    assertThat(delegate).isSameAs(CircuitBreakerForHttpStatusCode.getDefaultHttpStatusCodeCircuitBreakerForKey(host));
    Response responseMock = mock(Response.class);
    doReturn(42).when(responseMock).getStatusCode();
    assertThat(eventConverter.apply(responseMock)).isEqualTo(42);
    assertThat(eventConverter.apply(null)).isNull();
    if (useNettyEventLoop) {
        assertThat(Whitebox.getInternalState(delegate, "scheduler")).isEqualTo(eventLoopMock);
        assertThat(Whitebox.getInternalState(delegate, "stateChangeNotificationExecutor")).isEqualTo(eventLoopMock);
    } else {
        assertThat(Whitebox.getInternalState(delegate, "scheduler")).isNotEqualTo(eventLoopMock);
        assertThat(Whitebox.getInternalState(delegate, "stateChangeNotificationExecutor")).isNotEqualTo(eventLoopMock);
    }
}
Also used : CircuitBreaker(com.nike.fastbreak.CircuitBreaker) Matchers.anyString(org.mockito.Matchers.anyString) Response(com.ning.http.client.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(java.util.function.Function) CircuitBreakerDelegate(com.nike.fastbreak.CircuitBreakerDelegate) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Aggregations

CircuitBreaker (com.nike.fastbreak.CircuitBreaker)3 CircuitBreakerDelegate (com.nike.fastbreak.CircuitBreakerDelegate)3 ManualModeTask (com.nike.fastbreak.CircuitBreaker.ManualModeTask)2 CircuitBreakerForHttpStatusCode.getDefaultHttpStatusCodeCircuitBreakerForKey (com.nike.fastbreak.CircuitBreakerForHttpStatusCode.getDefaultHttpStatusCodeCircuitBreakerForKey)2 ChannelAttributes (com.nike.riposte.server.channelpipeline.ChannelAttributes)2 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)2 Span (com.nike.wingtips.Span)2 AsyncHttpClient (com.ning.http.client.AsyncHttpClient)2 Response (com.ning.http.client.Response)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 EventLoop (io.netty.channel.EventLoop)2 WrapperException (com.nike.backstopper.exception.WrapperException)1 CircuitBreakerOpenException (com.nike.fastbreak.exception.CircuitBreakerOpenException)1 Pair (com.nike.internal.util.Pair)1 StreamingAsyncHttpClient (com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient)1 StreamingCallback (com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient.StreamingCallback)1 StreamingChannel (com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient.StreamingChannel)1 LastOutboundMessageSendLastContentChunk (com.nike.riposte.server.channelpipeline.message.LastOutboundMessageSendLastContentChunk)1 OutboundMessageSendContentChunk (com.nike.riposte.server.channelpipeline.message.OutboundMessageSendContentChunk)1 OutboundMessageSendHeadersChunkFromResponseInfo (com.nike.riposte.server.channelpipeline.message.OutboundMessageSendHeadersChunkFromResponseInfo)1