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