lryan | 56e307f | 2014-12-05 13:25:08 -0800 | [diff] [blame] | 1 | /* |
Carl Mastrangelo | 3bfd630 | 2017-05-31 13:29:01 -0700 | [diff] [blame] | 2 | * Copyright 2014, gRPC Authors All rights reserved. |
lryan | 56e307f | 2014-12-05 13:25:08 -0800 | [diff] [blame] | 3 | * |
Carl Mastrangelo | 3bfd630 | 2017-05-31 13:29:01 -0700 | [diff] [blame] | 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 |
Carl Mastrangelo | 166108a | 2017-06-01 14:28:37 -0700 | [diff] [blame] | 7 | * |
Carl Mastrangelo | 3bfd630 | 2017-05-31 13:29:01 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Carl Mastrangelo | 166108a | 2017-06-01 14:28:37 -0700 | [diff] [blame] | 9 | * |
Carl Mastrangelo | 3bfd630 | 2017-05-31 13:29:01 -0700 | [diff] [blame] | 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. |
lryan | 56e307f | 2014-12-05 13:25:08 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 17 | package io.grpc.internal; |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 18 | |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkArgument; |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 20 | |
Kun Zhang | 631a9d5 | 2016-06-02 16:47:36 -0700 | [diff] [blame] | 21 | import com.google.common.annotations.VisibleForTesting; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 22 | import com.google.common.base.Preconditions; |
buchgr | 602473d | 2015-11-11 12:53:08 +0100 | [diff] [blame] | 23 | import com.google.common.util.concurrent.MoreExecutors; |
Kun Zhang | cce8eac | 2017-01-05 10:48:13 -0800 | [diff] [blame] | 24 | import com.google.instrumentation.stats.Stats; |
| 25 | import com.google.instrumentation.stats.StatsContextFactory; |
Kun Zhang | 49bde54 | 2017-04-25 13:53:29 -0700 | [diff] [blame] | 26 | import com.google.instrumentation.trace.Tracing; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 27 | import io.grpc.Attributes; |
| 28 | import io.grpc.ClientInterceptor; |
ZHANG Dapeng | 7d048af | 2017-05-02 16:21:43 -0700 | [diff] [blame] | 29 | import io.grpc.ClientStreamTracer; |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 30 | import io.grpc.CompressorRegistry; |
| 31 | import io.grpc.DecompressorRegistry; |
Kun Zhang | 418d52d | 2017-03-22 18:29:31 -0700 | [diff] [blame] | 32 | import io.grpc.EquivalentAddressGroup; |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 33 | import io.grpc.LoadBalancer; |
Kun Zhang | d17a7b5 | 2017-01-10 15:30:12 -0800 | [diff] [blame] | 34 | import io.grpc.ManagedChannel; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 35 | import io.grpc.ManagedChannelBuilder; |
| 36 | import io.grpc.NameResolver; |
Eric Anderson | faa0ad7 | 2017-06-07 12:18:27 -0700 | [diff] [blame] | 37 | import io.grpc.NameResolverProvider; |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 38 | import io.grpc.PickFirstBalancerFactory; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 39 | import java.net.SocketAddress; |
| 40 | import java.net.URI; |
Kun Zhang | 631a9d5 | 2016-06-02 16:47:36 -0700 | [diff] [blame] | 41 | import java.net.URISyntaxException; |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 42 | import java.util.ArrayList; |
| 43 | import java.util.Arrays; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 44 | import java.util.Collections; |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 45 | import java.util.List; |
Louis Ryan | 6a782a0 | 2015-09-03 14:27:48 -0700 | [diff] [blame] | 46 | import java.util.concurrent.Executor; |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 47 | import java.util.concurrent.TimeUnit; |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 48 | import javax.annotation.Nullable; |
| 49 | |
| 50 | /** |
| 51 | * The base class for channel builders. |
nathanmittler | 0304b3d | 2014-10-24 13:39:13 -0700 | [diff] [blame] | 52 | * |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 53 | * @param <T> The concrete type of this builder. |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 54 | */ |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 55 | public abstract class AbstractManagedChannelImplBuilder |
| 56 | <T extends AbstractManagedChannelImplBuilder<T>> extends ManagedChannelBuilder<T> { |
Kun Zhang | 1624715 | 2015-12-08 17:52:05 -0800 | [diff] [blame] | 57 | private static final String DIRECT_ADDRESS_SCHEME = "directaddress"; |
Eric Anderson | aeeebb7 | 2014-12-19 16:41:03 -0800 | [diff] [blame] | 58 | |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 59 | /** |
| 60 | * An idle timeout larger than this would disable idle mode. |
| 61 | */ |
| 62 | @VisibleForTesting |
| 63 | static final long IDLE_MODE_MAX_TIMEOUT_DAYS = 30; |
| 64 | |
| 65 | /** |
Kun Zhang | d74091f | 2016-09-16 22:34:27 -0700 | [diff] [blame] | 66 | * The default idle timeout. |
| 67 | */ |
| 68 | @VisibleForTesting |
| 69 | static final long IDLE_MODE_DEFAULT_TIMEOUT_MILLIS = TimeUnit.MINUTES.toMillis(30); |
| 70 | |
| 71 | /** |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 72 | * An idle timeout smaller than this would be capped to it. |
| 73 | */ |
| 74 | @VisibleForTesting |
| 75 | static final long IDLE_MODE_MIN_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(1); |
| 76 | |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 77 | private static final ObjectPool<? extends Executor> DEFAULT_EXECUTOR_POOL = |
| 78 | SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR); |
| 79 | |
| 80 | private static final NameResolver.Factory DEFAULT_NAME_RESOLVER_FACTORY = |
| 81 | NameResolverProvider.asFactory(); |
| 82 | |
| 83 | private static final LoadBalancer.Factory DEFAULT_LOAD_BALANCER_FACTORY = |
| 84 | PickFirstBalancerFactory.getInstance(); |
| 85 | |
| 86 | private static final DecompressorRegistry DEFAULT_DECOMPRESSOR_REGISTRY = |
| 87 | DecompressorRegistry.getDefaultInstance(); |
| 88 | |
| 89 | private static final CompressorRegistry DEFAULT_COMPRESSOR_REGISTRY = |
| 90 | CompressorRegistry.getDefaultInstance(); |
| 91 | |
| 92 | ObjectPool<? extends Executor> executorPool = DEFAULT_EXECUTOR_POOL; |
Kun Zhang | d17a7b5 | 2017-01-10 15:30:12 -0800 | [diff] [blame] | 93 | |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 94 | private final List<ClientInterceptor> interceptors = new ArrayList<ClientInterceptor>(); |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 95 | |
zpencer | 726c23b | 2017-06-16 18:54:47 -0700 | [diff] [blame] | 96 | // Access via getter, which may perform authority override as needed |
| 97 | private NameResolver.Factory nameResolverFactory = DEFAULT_NAME_RESOLVER_FACTORY; |
| 98 | |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 99 | final String target; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 100 | |
| 101 | @Nullable |
| 102 | private final SocketAddress directServerAddress; |
| 103 | |
nmittler | 8c1d38a | 2015-06-01 08:31:00 -0700 | [diff] [blame] | 104 | @Nullable |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 105 | String userAgent; |
nmittler | 8c1d38a | 2015-06-01 08:31:00 -0700 | [diff] [blame] | 106 | |
zpencer | 726c23b | 2017-06-16 18:54:47 -0700 | [diff] [blame] | 107 | @VisibleForTesting |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 108 | @Nullable |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 109 | String authorityOverride; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 110 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 111 | |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 112 | LoadBalancer.Factory loadBalancerFactory = DEFAULT_LOAD_BALANCER_FACTORY; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 113 | |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 114 | DecompressorRegistry decompressorRegistry = DEFAULT_DECOMPRESSOR_REGISTRY; |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 115 | |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 116 | CompressorRegistry compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY; |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 117 | |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 118 | long idleTimeoutMillis = IDLE_MODE_DEFAULT_TIMEOUT_MILLIS; |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 119 | |
Carl Mastrangelo | fdd062c | 2016-10-26 16:55:41 -0700 | [diff] [blame] | 120 | private int maxInboundMessageSize = GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE; |
| 121 | |
Kun Zhang | be74e97 | 2017-04-26 10:50:55 -0700 | [diff] [blame] | 122 | private boolean enableStatsTagPropagation; |
| 123 | private boolean enableTracing; |
| 124 | |
zpencer | 347eb09 | 2017-05-17 17:12:45 -0700 | [diff] [blame] | 125 | /** |
| 126 | * Sets the maximum message size allowed for a single gRPC frame. If an inbound messages |
| 127 | * larger than this limit is received it will not be processed and the RPC will fail with |
| 128 | * RESOURCE_EXHAUSTED. |
| 129 | */ |
Carl Mastrangelo | fdd062c | 2016-10-26 16:55:41 -0700 | [diff] [blame] | 130 | // Can be overriden by subclasses. |
| 131 | @Override |
| 132 | public T maxInboundMessageSize(int max) { |
| 133 | checkArgument(max >= 0, "negative max"); |
| 134 | maxInboundMessageSize = max; |
| 135 | return thisT(); |
| 136 | } |
| 137 | |
| 138 | protected final int maxInboundMessageSize() { |
| 139 | return maxInboundMessageSize; |
| 140 | } |
| 141 | |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 142 | @Nullable |
Kun Zhang | cce8eac | 2017-01-05 10:48:13 -0800 | [diff] [blame] | 143 | private StatsContextFactory statsFactory; |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 144 | |
Kun Zhang | efac679 | 2015-10-22 14:59:44 -0700 | [diff] [blame] | 145 | protected AbstractManagedChannelImplBuilder(String target) { |
Carl Mastrangelo | 1285477 | 2016-08-12 14:52:00 -0700 | [diff] [blame] | 146 | this.target = Preconditions.checkNotNull(target, "target"); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 147 | this.directServerAddress = null; |
| 148 | } |
| 149 | |
Kun Zhang | 631a9d5 | 2016-06-02 16:47:36 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Returns a target string for the SocketAddress. It is only used as a placeholder, because |
| 152 | * DirectAddressNameResolverFactory will not actually try to use it. However, it must be a valid |
| 153 | * URI. |
| 154 | */ |
| 155 | @VisibleForTesting |
| 156 | static String makeTargetStringForDirectAddress(SocketAddress address) { |
| 157 | try { |
| 158 | return new URI(DIRECT_ADDRESS_SCHEME, "", "/" + address, null).toString(); |
| 159 | } catch (URISyntaxException e) { |
| 160 | // It should not happen. |
| 161 | throw new RuntimeException(e); |
| 162 | } |
| 163 | } |
| 164 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 165 | protected AbstractManagedChannelImplBuilder(SocketAddress directServerAddress, String authority) { |
Kun Zhang | 631a9d5 | 2016-06-02 16:47:36 -0700 | [diff] [blame] | 166 | this.target = makeTargetStringForDirectAddress(directServerAddress); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 167 | this.directServerAddress = directServerAddress; |
| 168 | this.nameResolverFactory = new DirectAddressNameResolverFactory(directServerAddress, authority); |
| 169 | } |
| 170 | |
Eric Anderson | 6122daf | 2015-09-03 12:14:30 -0700 | [diff] [blame] | 171 | @Override |
buchgr | 602473d | 2015-11-11 12:53:08 +0100 | [diff] [blame] | 172 | public final T directExecutor() { |
| 173 | return executor(MoreExecutors.directExecutor()); |
| 174 | } |
| 175 | |
| 176 | @Override |
Louis Ryan | 6a782a0 | 2015-09-03 14:27:48 -0700 | [diff] [blame] | 177 | public final T executor(Executor executor) { |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 178 | if (executor != null) { |
| 179 | this.executorPool = new FixedObjectPool<Executor>(executor); |
| 180 | } else { |
| 181 | this.executorPool = DEFAULT_EXECUTOR_POOL; |
| 182 | } |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 183 | return thisT(); |
| 184 | } |
| 185 | |
Eric Anderson | 6122daf | 2015-09-03 12:14:30 -0700 | [diff] [blame] | 186 | @Override |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 187 | public final T intercept(List<ClientInterceptor> interceptors) { |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 188 | this.interceptors.addAll(interceptors); |
| 189 | return thisT(); |
| 190 | } |
| 191 | |
Eric Anderson | 6122daf | 2015-09-03 12:14:30 -0700 | [diff] [blame] | 192 | @Override |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 193 | public final T intercept(ClientInterceptor... interceptors) { |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 194 | return intercept(Arrays.asList(interceptors)); |
| 195 | } |
| 196 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 197 | @Override |
| 198 | public final T nameResolverFactory(NameResolver.Factory resolverFactory) { |
| 199 | Preconditions.checkState(directServerAddress == null, |
| 200 | "directServerAddress is set (%s), which forbids the use of NameResolverFactory", |
| 201 | directServerAddress); |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 202 | if (resolverFactory != null) { |
| 203 | this.nameResolverFactory = resolverFactory; |
| 204 | } else { |
| 205 | this.nameResolverFactory = DEFAULT_NAME_RESOLVER_FACTORY; |
| 206 | } |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 207 | return thisT(); |
| 208 | } |
| 209 | |
| 210 | @Override |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 211 | public final T loadBalancerFactory(LoadBalancer.Factory loadBalancerFactory) { |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 212 | Preconditions.checkState(directServerAddress == null, |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 213 | "directServerAddress is set (%s), which forbids the use of LoadBalancer.Factory", |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 214 | directServerAddress); |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 215 | if (loadBalancerFactory != null) { |
| 216 | this.loadBalancerFactory = loadBalancerFactory; |
| 217 | } else { |
| 218 | this.loadBalancerFactory = DEFAULT_LOAD_BALANCER_FACTORY; |
| 219 | } |
Kun Zhang | d17a7b5 | 2017-01-10 15:30:12 -0800 | [diff] [blame] | 220 | return thisT(); |
| 221 | } |
| 222 | |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 223 | @Override |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 224 | public final T decompressorRegistry(DecompressorRegistry registry) { |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 225 | if (registry != null) { |
| 226 | this.decompressorRegistry = registry; |
| 227 | } else { |
| 228 | this.decompressorRegistry = DEFAULT_DECOMPRESSOR_REGISTRY; |
| 229 | } |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 230 | return thisT(); |
| 231 | } |
| 232 | |
| 233 | @Override |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 234 | public final T compressorRegistry(CompressorRegistry registry) { |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 235 | if (registry != null) { |
| 236 | this.compressorRegistry = registry; |
| 237 | } else { |
| 238 | this.compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY; |
| 239 | } |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 240 | return thisT(); |
| 241 | } |
| 242 | |
Eric Anderson | 6122daf | 2015-09-03 12:14:30 -0700 | [diff] [blame] | 243 | @Override |
Carl Mastrangelo | 1cc76d8 | 2016-05-24 16:29:26 -0700 | [diff] [blame] | 244 | public final T userAgent(@Nullable String userAgent) { |
nmittler | 8c1d38a | 2015-06-01 08:31:00 -0700 | [diff] [blame] | 245 | this.userAgent = userAgent; |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 246 | return thisT(); |
nmittler | 8c1d38a | 2015-06-01 08:31:00 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Eric Anderson | 6122daf | 2015-09-03 12:14:30 -0700 | [diff] [blame] | 249 | @Override |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 250 | public final T overrideAuthority(String authority) { |
| 251 | this.authorityOverride = checkAuthority(authority); |
| 252 | return thisT(); |
| 253 | } |
| 254 | |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 255 | @Override |
| 256 | public final T idleTimeout(long value, TimeUnit unit) { |
| 257 | checkArgument(value > 0, "idle timeout is %s, but must be positive", value); |
| 258 | // We convert to the largest unit to avoid overflow |
| 259 | if (unit.toDays(value) >= IDLE_MODE_MAX_TIMEOUT_DAYS) { |
| 260 | // This disables idle mode |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 261 | this.idleTimeoutMillis = ManagedChannelImpl.IDLE_TIMEOUT_MILLIS_DISABLE; |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 262 | } else { |
| 263 | this.idleTimeoutMillis = Math.max(unit.toMillis(value), IDLE_MODE_MIN_TIMEOUT_MILLIS); |
| 264 | } |
| 265 | return thisT(); |
| 266 | } |
| 267 | |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 268 | /** |
Kun Zhang | 737cd16 | 2017-01-20 17:20:44 -0800 | [diff] [blame] | 269 | * Override the default stats implementation. |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 270 | */ |
| 271 | @VisibleForTesting |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 272 | protected final T statsContextFactory(StatsContextFactory statsFactory) { |
Kun Zhang | cce8eac | 2017-01-05 10:48:13 -0800 | [diff] [blame] | 273 | this.statsFactory = statsFactory; |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 274 | return thisT(); |
| 275 | } |
| 276 | |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 277 | /** |
| 278 | * Indicates whether this transport will record stats with {@link ClientStreamTracer}. |
| 279 | * |
| 280 | * <p>By default it returns {@code true}. If the transport doesn't record stats, it may override |
| 281 | * this method to return {@code false} so that the builder won't install the Census interceptor. |
| 282 | * |
| 283 | * <p>If it returns true when it shouldn't be, Census will receive incomplete stats. |
| 284 | */ |
| 285 | protected boolean recordsStats() { |
| 286 | return true; |
| 287 | } |
| 288 | |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 289 | @VisibleForTesting |
| 290 | final long getIdleTimeoutMillis() { |
| 291 | return idleTimeoutMillis; |
| 292 | } |
| 293 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 294 | /** |
| 295 | * Verifies the authority is valid. This method exists as an escape hatch for putting in an |
| 296 | * authority that is valid, but would fail the default validation provided by this |
| 297 | * implementation. |
| 298 | */ |
| 299 | protected String checkAuthority(String authority) { |
| 300 | return GrpcUtil.checkAuthority(authority); |
| 301 | } |
| 302 | |
Kun Zhang | be74e97 | 2017-04-26 10:50:55 -0700 | [diff] [blame] | 303 | /** |
| 304 | * Set it to true to propagate the stats tags on the wire. This will be deleted assuming always |
| 305 | * enabled once the instrumentation-java wire format is stabilized. |
| 306 | */ |
| 307 | @Deprecated |
| 308 | public void setEnableStatsTagPropagation(boolean enabled) { |
| 309 | this.enableStatsTagPropagation = enabled; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Set it to true to record traces and propagate tracing information on the wire. This will be |
| 314 | * deleted assuming always enabled once the instrumentation-java wire format is stabilized. |
| 315 | */ |
| 316 | @Deprecated |
| 317 | public void setEnableTracing(boolean enabled) { |
| 318 | this.enableTracing = enabled; |
| 319 | } |
| 320 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 321 | @Override |
Kun Zhang | d17a7b5 | 2017-01-10 15:30:12 -0800 | [diff] [blame] | 322 | public ManagedChannel build() { |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 323 | return new ManagedChannelImpl( |
| 324 | this, |
| 325 | buildTransportFactory(), |
| 326 | // TODO(carl-mastrangelo): Allow clients to pass this in |
| 327 | new ExponentialBackoffPolicy.Provider(), |
| 328 | SharedResourcePool.forResource(GrpcUtil.TIMER_SERVICE), |
| 329 | SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR), |
| 330 | GrpcUtil.STOPWATCH_SUPPLIER, |
| 331 | getEffectiveInterceptors()); |
| 332 | } |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 333 | |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 334 | private List<ClientInterceptor> getEffectiveInterceptors() { |
Kun Zhang | 49bde54 | 2017-04-25 13:53:29 -0700 | [diff] [blame] | 335 | List<ClientInterceptor> effectiveInterceptors = |
| 336 | new ArrayList<ClientInterceptor>(this.interceptors); |
Kun Zhang | be74e97 | 2017-04-26 10:50:55 -0700 | [diff] [blame] | 337 | if (recordsStats()) { |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 338 | StatsContextFactory statsCtxFactory = |
| 339 | this.statsFactory != null ? this.statsFactory : Stats.getStatsContextFactory(); |
| 340 | if (statsCtxFactory != null) { |
Kun Zhang | 49bde54 | 2017-04-25 13:53:29 -0700 | [diff] [blame] | 341 | CensusStatsModule censusStats = |
Kun Zhang | be74e97 | 2017-04-26 10:50:55 -0700 | [diff] [blame] | 342 | new CensusStatsModule( |
| 343 | statsCtxFactory, GrpcUtil.STOPWATCH_SUPPLIER, enableStatsTagPropagation); |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 344 | // First interceptor runs last (see ClientInterceptors.intercept()), so that no |
| 345 | // other interceptor can override the tracer factory we set in CallOptions. |
Kun Zhang | 49bde54 | 2017-04-25 13:53:29 -0700 | [diff] [blame] | 346 | effectiveInterceptors.add(0, censusStats.getClientInterceptor()); |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
Kun Zhang | be74e97 | 2017-04-26 10:50:55 -0700 | [diff] [blame] | 349 | if (enableTracing) { |
Kun Zhang | 49bde54 | 2017-04-25 13:53:29 -0700 | [diff] [blame] | 350 | CensusTracingModule censusTracing = |
| 351 | new CensusTracingModule(Tracing.getTracer(), Tracing.getBinaryPropagationHandler()); |
| 352 | effectiveInterceptors.add(0, censusTracing.getClientInterceptor()); |
| 353 | } |
Eric Anderson | 0a8d761 | 2017-05-23 11:08:34 -0700 | [diff] [blame] | 354 | return effectiveInterceptors; |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 358 | * Subclasses should override this method to provide the {@link ClientTransportFactory} |
| 359 | * appropriate for this channel. This method is meant for Transport implementors and should not |
| 360 | * be used by normal users. |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 361 | */ |
nmittler | 777e928 | 2015-08-19 10:01:52 -0700 | [diff] [blame] | 362 | protected abstract ClientTransportFactory buildTransportFactory(); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 363 | |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 364 | /** |
| 365 | * Subclasses can override this method to provide additional parameters to {@link |
| 366 | * NameResolver.Factory#newNameResolver}. The default implementation returns {@link |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 367 | * Attributes#EMPTY}. |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 368 | */ |
| 369 | protected Attributes getNameResolverParams() { |
| 370 | return Attributes.EMPTY; |
| 371 | } |
| 372 | |
zpencer | 726c23b | 2017-06-16 18:54:47 -0700 | [diff] [blame] | 373 | /** |
| 374 | * Returns a {@link NameResolver.Factory} for the channel. |
| 375 | */ |
| 376 | NameResolver.Factory getNameResolverFactory() { |
| 377 | if (authorityOverride == null) { |
| 378 | return nameResolverFactory; |
| 379 | } else { |
| 380 | return new OverrideAuthorityNameResolverFactory(nameResolverFactory, authorityOverride); |
| 381 | } |
| 382 | } |
| 383 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 384 | private static class DirectAddressNameResolverFactory extends NameResolver.Factory { |
| 385 | final SocketAddress address; |
| 386 | final String authority; |
| 387 | |
| 388 | DirectAddressNameResolverFactory(SocketAddress address, String authority) { |
| 389 | this.address = address; |
| 390 | this.authority = authority; |
| 391 | } |
| 392 | |
| 393 | @Override |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 394 | public NameResolver newNameResolver(URI notUsedUri, Attributes params) { |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 395 | return new NameResolver() { |
| 396 | @Override |
| 397 | public String getServiceAuthority() { |
| 398 | return authority; |
| 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public void start(final Listener listener) { |
Kun Zhang | 418d52d | 2017-03-22 18:29:31 -0700 | [diff] [blame] | 403 | listener.onAddresses( |
| 404 | Collections.singletonList(new EquivalentAddressGroup(address)), |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 405 | Attributes.EMPTY); |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public void shutdown() {} |
| 410 | }; |
| 411 | } |
Kun Zhang | 1624715 | 2015-12-08 17:52:05 -0800 | [diff] [blame] | 412 | |
| 413 | @Override |
| 414 | public String getDefaultScheme() { |
| 415 | return DIRECT_ADDRESS_SCHEME; |
| 416 | } |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 417 | } |
Carl Mastrangelo | 8493498 | 2017-01-11 12:46:47 -0800 | [diff] [blame] | 418 | |
| 419 | /** |
| 420 | * Returns the correctly typed version of the builder. |
| 421 | */ |
| 422 | private T thisT() { |
| 423 | @SuppressWarnings("unchecked") |
| 424 | T thisT = (T) this; |
| 425 | return thisT; |
| 426 | } |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 427 | } |