com.netflix.loadbalancer.reactive.ExecutionContext Java Examples

The following examples show how to use com.netflix.loadbalancer.reactive.ExecutionContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BearerHeaderAdder.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartWithServer(ExecutionContext<HttpClientRequest<ByteBuf>> context, ExecutionInfo info) throws AbortExecutionException {
    KeycloakSecurityContext securityContext = KeycloakSecurityContextAssociation.get();
    if (securityContext != null) {
        HttpClientRequest<ByteBuf> request = context.getRequest();
        request.withHeader("Authorization", "Bearer " + securityContext.getTokenString());
        context.put(KeycloakSecurityContextAssociation.class.getName(), securityContext);
    } else {
        KeycloakSecurityContextAssociation.disassociate();
    }
}
 
Example #2
Source File: TestExecutionListener.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Override
public void onExecutionFailed(ExecutionContext<HttpClientRequest<I>> context, Throwable finalException, ExecutionInfo info) {
    checkContext(context);
    checkExecutionInfo(info);
    executionFailedCounter.incrementAndGet();
    finalThrowable = finalException;
}
 
Example #3
Source File: TestExecutionListener.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Override
public void onExecutionSuccess(ExecutionContext<HttpClientRequest<I>> context, HttpClientResponse<O> response, ExecutionInfo info) {
    checkContext(context);
    checkExecutionInfo(info);
    this.response = response;
    executionSuccessCounter.incrementAndGet();
}
 
Example #4
Source File: TestExecutionListener.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Override
public void onExceptionWithServer(ExecutionContext<HttpClientRequest<I>> context, Throwable exception, ExecutionInfo info) {
    checkContext(context);
    checkExecutionInfo(info);
    numAttemptsOnServer.incrementAndGet();
    errors.add(exception);
    exceptionWithServerCounter.incrementAndGet();
}
 
Example #5
Source File: TestExecutionListener.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartWithServer(ExecutionContext<HttpClientRequest<I>> context, ExecutionInfo info) {
    checkContext(context);
    
    if (lastServer == null) {
        lastServer = info.getServer();
    } else if (!lastServer.equals(info.getServer())) {
        lastServer = info.getServer();
        numAttemptsOnServer.set(0);
        numServers.incrementAndGet();
    }
    checkExecutionInfo(info);
    startWithServerCounter.incrementAndGet();
}
 
Example #6
Source File: TestExecutionListener.java    From ribbon with Apache License 2.0 5 votes vote down vote up
private void checkContext(ExecutionContext<HttpClientRequest<I>> context) {
    try {
        assertSame(requestConfig, context.getRequestConfig());
        assertSame(expectedRequest, context.getRequest());
        assertEquals(MY_OBJECT, context.get("MyObject"));
        if (this.context == null) {
            this.context = context;
        } else {
            assertSame(this.context, context);
        }
    } catch (Throwable e) {
        e.printStackTrace();
        checkContext = false;
    }
}
 
Example #7
Source File: LoadBalancingHttpClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
/**
 * Subject an operation to run in the load balancer
 * 
 * @param request
 * @param errorHandler
 * @param requestConfig
 * @param rxClientConfig
 * @return
 */
private Observable<HttpClientResponse<O>> submit(final Server server, final HttpClientRequest<I> request, final RetryHandler errorHandler, final IClientConfig requestConfig, final ClientConfig rxClientConfig) {
    RetryHandler retryHandler = errorHandler;
    if (retryHandler == null) {
        retryHandler = getRequestRetryHandler(request, requestConfig);
    }
    
    final IClientConfig config = requestConfig == null ? DefaultClientConfigImpl.getEmptyConfig() : requestConfig;
    final ExecutionContext<HttpClientRequest<I>> context = new ExecutionContext<HttpClientRequest<I>>(request, config, this.getClientConfig(), retryHandler);
    
    Observable<HttpClientResponse<O>> result = submitToServerInURI(request, config, rxClientConfig, retryHandler, context);
    if (result == null) {
        LoadBalancerCommand<HttpClientResponse<O>> command;
        if (retryHandler != defaultRetryHandler) {
            // need to create new builder instead of the default one
            command = LoadBalancerCommand.<HttpClientResponse<O>>builder()
                    .withExecutionContext(context)
                    .withLoadBalancerContext(lbContext)
                    .withListeners(listeners)
                    .withClientConfig(this.getClientConfig())
                    .withRetryHandler(retryHandler)
                    .withServer(server)
                    .build();
        }
        else {
            command = defaultCommandBuilder;
        }
        
        result = command.submit(requestToOperation(request, getRxClientConfig(config, rxClientConfig)));
    }
    return result;
}
 
Example #8
Source File: BearerHeaderAdder.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
@Override
public void onExecutionFailed(ExecutionContext<HttpClientRequest<ByteBuf>> context, Throwable finalException, ExecutionInfo info) {
    KeycloakSecurityContext securityContext = (KeycloakSecurityContext) context.get(KeycloakSecurityContextAssociation.class.getName());
    if (securityContext != null) {
        KeycloakSecurityContextAssociation.associate(securityContext);
    } else {
        KeycloakSecurityContextAssociation.disassociate();
    }
}
 
Example #9
Source File: BearerHeaderAdder.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
@Override
public void onExecutionSuccess(ExecutionContext<HttpClientRequest<ByteBuf>> context, HttpClientResponse<ByteBuf> response, ExecutionInfo info) {
    KeycloakSecurityContext securityContext = (KeycloakSecurityContext) context.get(KeycloakSecurityContextAssociation.class.getName());
    if (securityContext != null) {
        KeycloakSecurityContextAssociation.associate(securityContext);
    } else {
        KeycloakSecurityContextAssociation.disassociate();
    }
}
 
Example #10
Source File: BearerHeaderAdder.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
@Override
public void onExceptionWithServer(ExecutionContext<HttpClientRequest<ByteBuf>> context, Throwable exception, ExecutionInfo info) {
    KeycloakSecurityContext securityContext = (KeycloakSecurityContext) context.get(KeycloakSecurityContextAssociation.class.getName());
    if (securityContext != null) {
        KeycloakSecurityContextAssociation.associate(securityContext);
    } else {
        KeycloakSecurityContextAssociation.disassociate();
    }
}
 
Example #11
Source File: BearerHeaderAdder.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartWithServer(ExecutionContext<HttpClientRequest<ByteBuf>> context, ExecutionInfo info) throws AbortExecutionException {
    KeycloakSecurityContext securityContext = KeycloakSecurityContextAssociation.get();
    if (securityContext != null) {
        HttpClientRequest<ByteBuf> request = context.getRequest();
        request.withHeader("Authorization", "Bearer " + securityContext.getTokenString());
        context.put(KeycloakSecurityContextAssociation.class.getName(), securityContext);
    } else {
        KeycloakSecurityContextAssociation.disassociate();
    }
}
 
Example #12
Source File: BearerHeaderAdder.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
public void onExecutionFailed(ExecutionContext<HttpClientRequest<ByteBuf>> context, Throwable finalException, ExecutionInfo info) {
    KeycloakSecurityContext securityContext = (KeycloakSecurityContext) context.get(KeycloakSecurityContextAssociation.class.getName());
    if (securityContext != null) {
        KeycloakSecurityContextAssociation.associate(securityContext);
    } else {
        KeycloakSecurityContextAssociation.disassociate();
    }
}
 
Example #13
Source File: BearerHeaderAdder.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
public void onExecutionSuccess(ExecutionContext<HttpClientRequest<ByteBuf>> context, HttpClientResponse<ByteBuf> response, ExecutionInfo info) {
    KeycloakSecurityContext securityContext = (KeycloakSecurityContext) context.get(KeycloakSecurityContextAssociation.class.getName());
    if (securityContext != null) {
        KeycloakSecurityContextAssociation.associate(securityContext);
    } else {
        KeycloakSecurityContextAssociation.disassociate();
    }
}
 
Example #14
Source File: BearerHeaderAdder.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
public void onExceptionWithServer(ExecutionContext<HttpClientRequest<ByteBuf>> context, Throwable exception, ExecutionInfo info) {
    KeycloakSecurityContext securityContext = (KeycloakSecurityContext) context.get(KeycloakSecurityContextAssociation.class.getName());
    if (securityContext != null) {
        KeycloakSecurityContextAssociation.associate(securityContext);
    } else {
        KeycloakSecurityContextAssociation.disassociate();
    }
}
 
Example #15
Source File: BearerHeaderAdder.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
@Override
public void onExecutionStart(ExecutionContext<HttpClientRequest<ByteBuf>> context) throws AbortExecutionException {
}
 
Example #16
Source File: TestExecutionListener.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Override
public void onExecutionStart(ExecutionContext<HttpClientRequest<I>> context) {
    context.put("MyObject", MY_OBJECT);
    checkContext(context);
    executionStartCounter.incrementAndGet();
}
 
Example #17
Source File: BearerHeaderAdder.java    From thorntail with Apache License 2.0 4 votes vote down vote up
@Override
public void onExecutionStart(ExecutionContext<HttpClientRequest<ByteBuf>> context) throws AbortExecutionException {
}
 
Example #18
Source File: TestExecutionListener.java    From ribbon with Apache License 2.0 4 votes vote down vote up
public ExecutionContext<HttpClientRequest<I>> getContext() {
    return this.context;
}
 
Example #19
Source File: ListenerTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testAbortedExecution() {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "100")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server badServer2 = new Server("localhost:34567");
    List<Server> servers = Lists.newArrayList(badServer, badServer2);
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
    TestExecutionListener listener = new TestExecutionListener(request, overrideConfig) {
        @Override
        public void onExecutionStart(ExecutionContext context) {
            throw new AbortExecutionException("exit now");
        }
    };
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> ref = new AtomicReference<Throwable>();
    client.submit(request, null, overrideConfig).subscribe(new Action1<HttpClientResponse<ByteBuf>>() {
        @Override
        public void call(HttpClientResponse<ByteBuf> byteBufHttpClientResponse) {
        }
    }, new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {
            ref.set(throwable);
            latch.countDown();
        }
    });
    try {
        latch.await(500, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertTrue(ref.get() instanceof AbortExecutionException);
}
 
Example #20
Source File: ListenerTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testAbortedExecutionOnServer() {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "100")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server badServer2 = new Server("localhost:34567");
    List<Server> servers = Lists.newArrayList(badServer, badServer2);
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
    TestExecutionListener listener = new TestExecutionListener(request, overrideConfig) {
        @Override
        public void onStartWithServer(ExecutionContext context, ExecutionInfo info) {
            throw new AbortExecutionException("exit now");
        }
    };
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> ref = new AtomicReference<Throwable>();
    client.submit(request, null, overrideConfig).subscribe(new Action1<HttpClientResponse<ByteBuf>>() {
        @Override
        public void call(HttpClientResponse<ByteBuf> byteBufHttpClientResponse) {
        }
    }, new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {
            ref.set(throwable);
            latch.countDown();
        }
    });
    try {
        latch.await(500, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertTrue(ref.get() instanceof AbortExecutionException);
}
 
Example #21
Source File: ProxyEndpoint.java    From zuul with Apache License 2.0 4 votes vote down vote up
@Override
public void operationComplete(final Future<PooledConnection> connectResult) {
    // MUST run this within bindingcontext because RequestExpiryProcessor (and probably other things) depends on ThreadVariables.
    try {
        methodBinding.bind(() -> {

            Integer readTimeout = null;
            Server server = chosenServer.get();

            // The chosen server would be null if the loadbalancer found no available servers.
            if (server != null) {
                if (currentRequestStat != null) {
                    currentRequestStat.server(server);
                }

                // Invoke the ribbon execution listeners (including RequestExpiry).
                final ExecutionContext<?> executionContext = origin.getExecutionContext(zuulRequest);
                IClientConfig requestConfig = executionContext.getRequestConfig();
                try {
                    readTimeout = requestConfig.get(ReadTimeout);

                    origin.onRequestStartWithServer(zuulRequest, server, attemptNum);

                    // As the read-timeout can be overridden in the listeners executed from onRequestStartWithServer() above
                    // check now to see if it was. And if it was, then use that.
                    Object overriddenReadTimeoutObj = requestConfig.get(IClientConfigKey.Keys.ReadTimeout);
                    if (overriddenReadTimeoutObj != null && overriddenReadTimeoutObj instanceof Integer) {
                        int overriddenReadTimeout = (Integer) overriddenReadTimeoutObj;
                        readTimeout = overriddenReadTimeout;
                    }
                }
                catch (Throwable e) {
                    handleError(e);
                    return;
                }
                finally {
                    // Reset the timeout in overriddenConfig back to what it was before, otherwise it will take
                    // preference on subsequent retry attempts in RequestExpiryProcessor.
                    if (originalReadTimeout == null) {
                        requestConfig.setProperty(ReadTimeout, null);
                    }
                    else {
                        requestConfig.setProperty(ReadTimeout, originalReadTimeout);
                    }
                }
            }

            // Handle the connection establishment result.
            if (connectResult.isSuccess()) {
                onOriginConnectSucceeded(connectResult.getNow(), readTimeout);
            } else {
                onOriginConnectFailed(connectResult.cause());
            }
        });
    } catch (Throwable ex) {
        LOG.error("Uncaught error in operationComplete(). Closing the server channel now. {}"
                , ChannelUtils.channelInfoForLogging(channelCtx.channel()), ex);

        unlinkFromOrigin();

        // Fire exception here to ensure that server channel gets closed, so clients don't hang.
        channelCtx.fireExceptionCaught(ex);
    }
}
 
Example #22
Source File: NettyOrigin.java    From zuul with Apache License 2.0 votes vote down vote up
ExecutionContext<?> getExecutionContext(HttpRequestMessage zuulRequest);