|
| 1 | +/* |
| 2 | + * Copyright © 2024 Apple Inc. and the ServiceTalk project authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.servicetalk.loadbalancer.experimental; |
| 17 | + |
| 18 | +import io.servicetalk.client.api.LoadBalancerFactory; |
| 19 | +import io.servicetalk.http.api.DelegatingSingleAddressHttpClientBuilder; |
| 20 | +import io.servicetalk.http.api.FilterableStreamingHttpLoadBalancedConnection; |
| 21 | +import io.servicetalk.http.api.HttpLoadBalancerFactory; |
| 22 | +import io.servicetalk.http.api.HttpProviders; |
| 23 | +import io.servicetalk.http.api.SingleAddressHttpClientBuilder; |
| 24 | +import io.servicetalk.http.netty.DefaultHttpLoadBalancerFactory; |
| 25 | +import io.servicetalk.loadbalancer.LoadBalancers; |
| 26 | +import io.servicetalk.transport.api.HostAndPort; |
| 27 | + |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +import static java.util.Objects.requireNonNull; |
| 32 | + |
| 33 | +/** |
| 34 | + * A client builder provider that supports enabling the new `DefaultLoadBalancer` in applications via property flags. |
| 35 | + * See the packages README.md for more details. |
| 36 | + */ |
| 37 | +public class DefaultHttpLoadBalancerProvider implements HttpProviders.SingleAddressHttpClientBuilderProvider { |
| 38 | + |
| 39 | + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultHttpLoadBalancerProvider.class); |
| 40 | + |
| 41 | + private final DefaultLoadBalancerProviderConfig config; |
| 42 | + |
| 43 | + public DefaultHttpLoadBalancerProvider() { |
| 44 | + this(DefaultLoadBalancerProviderConfig.INSTANCE); |
| 45 | + } |
| 46 | + |
| 47 | + // exposed for testing |
| 48 | + DefaultHttpLoadBalancerProvider(final DefaultLoadBalancerProviderConfig config) { |
| 49 | + this.config = requireNonNull(config, "config"); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public final <U, R> SingleAddressHttpClientBuilder<U, R> newBuilder(U address, |
| 54 | + SingleAddressHttpClientBuilder<U, R> builder) { |
| 55 | + final String serviceName = clientNameFromAddress(address); |
| 56 | + if (config.enabledForServiceName(serviceName)) { |
| 57 | + try { |
| 58 | + HttpLoadBalancerFactory<R> loadBalancerFactory = DefaultHttpLoadBalancerFactory.Builder.<R>from( |
| 59 | + defaultLoadBalancer(serviceName)).build(); |
| 60 | + builder = builder.loadBalancerFactory(loadBalancerFactory); |
| 61 | + return new LoadBalancerIgnoringBuilder(builder, serviceName); |
| 62 | + } catch (Throwable ex) { |
| 63 | + LOGGER.warn("Failed to enabled DefaultLoadBalancer for client to address {}.", address, ex); |
| 64 | + } |
| 65 | + } |
| 66 | + return builder; |
| 67 | + } |
| 68 | + |
| 69 | + private <R> LoadBalancerFactory<R, FilterableStreamingHttpLoadBalancedConnection> defaultLoadBalancer( |
| 70 | + String serviceName) { |
| 71 | + return LoadBalancers.<R, FilterableStreamingHttpLoadBalancedConnection> |
| 72 | + builder("experimental-load-balancer") |
| 73 | + .loadBalancerObserver(new DefaultLoadBalancerObserver(serviceName)) |
| 74 | + // set up the new features. |
| 75 | + .outlierDetectorConfig(config.outlierDetectorConfig()) |
| 76 | + .loadBalancingPolicy(config.getLoadBalancingPolicy()) |
| 77 | + .build(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Extract the service name from the address object. |
| 82 | + * Note: this is a protected method to allow overriding for custom address types. |
| 83 | + * @param <U> the unresolved type of the address. |
| 84 | + * @param address the address from which to extract the service name. |
| 85 | + * @return the String representation of the provided address. |
| 86 | + */ |
| 87 | + protected <U> String clientNameFromAddress(U address) { |
| 88 | + String serviceName; |
| 89 | + if (address instanceof HostAndPort) { |
| 90 | + serviceName = ((HostAndPort) address).hostName(); |
| 91 | + } else if (address instanceof String) { |
| 92 | + serviceName = (String) address; |
| 93 | + } else { |
| 94 | + LOGGER.warn("Unknown service address type={} was provided, " |
| 95 | + + "default 'toString()' will be used as serviceName", address.getClass()); |
| 96 | + serviceName = address.toString(); |
| 97 | + } |
| 98 | + return serviceName; |
| 99 | + } |
| 100 | + |
| 101 | + private static final class LoadBalancerIgnoringBuilder<U, R> |
| 102 | + extends DelegatingSingleAddressHttpClientBuilder<U, R> { |
| 103 | + |
| 104 | + private final String serviceName; |
| 105 | + |
| 106 | + LoadBalancerIgnoringBuilder(final SingleAddressHttpClientBuilder<U, R> delegate, final String serviceName) { |
| 107 | + super(delegate); |
| 108 | + this.serviceName = serviceName; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public SingleAddressHttpClientBuilder<U, R> loadBalancerFactory( |
| 113 | + HttpLoadBalancerFactory<R> loadBalancerFactory) { |
| 114 | + LOGGER.info("Ignoring http load balancer factory of type {} for client to {} which has " + |
| 115 | + "DefaultLoadBalancer enabled.", loadBalancerFactory.getClass(), serviceName); |
| 116 | + return this; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments