|
19 | 19 | import com.google.api.core.InternalApi; |
20 | 20 | import com.google.api.gax.core.BackgroundResource; |
21 | 21 | import com.google.api.gax.core.CredentialsProvider; |
| 22 | +import com.google.api.gax.core.ExecutorAsBackgroundResource; |
22 | 23 | import com.google.api.gax.core.FixedCredentialsProvider; |
23 | 24 | import com.google.api.gax.core.FixedExecutorProvider; |
24 | 25 | import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; |
|
35 | 36 | import com.google.cloud.bigtable.data.v2.stub.metrics.NoopMetricsProvider; |
36 | 37 | import com.google.cloud.bigtable.gaxx.grpc.BigtableTransportChannelProvider; |
37 | 38 | import com.google.cloud.bigtable.gaxx.grpc.ChannelPrimer; |
| 39 | +import com.google.common.collect.ImmutableList; |
38 | 40 | import io.grpc.ManagedChannelBuilder; |
39 | 41 | import io.grpc.opentelemetry.GrpcOpenTelemetry; |
40 | 42 | import io.opentelemetry.api.OpenTelemetry; |
@@ -77,9 +79,15 @@ public static BigtableClientContext create(EnhancedBigtableStubSettings settings |
77 | 79 |
|
78 | 80 | String universeDomain = settings.getUniverseDomain(); |
79 | 81 |
|
| 82 | + boolean canAutoCloseExecutor = true; |
80 | 83 | ScheduledExecutorService backgroundExecutor = |
81 | 84 | settings.getBackgroundExecutorProvider().getExecutor(); |
82 | | - |
| 85 | + if (settings.getBackgroundExecutorProvider() instanceof FixedExecutorProvider) { |
| 86 | + // if the background executor in the settings is already a FixedExecutorProvider, |
| 87 | + // we can't assume that we can autoclose it and the life cycle should be managed |
| 88 | + // by the application |
| 89 | + canAutoCloseExecutor = false; |
| 90 | + } |
83 | 91 | FixedExecutorProvider executorProvider = FixedExecutorProvider.create(backgroundExecutor); |
84 | 92 | builder.setBackgroundExecutorProvider(executorProvider); |
85 | 93 |
|
@@ -154,6 +162,17 @@ public static BigtableClientContext create(EnhancedBigtableStubSettings settings |
154 | 162 | } |
155 | 163 |
|
156 | 164 | ClientContext clientContext = ClientContext.create(builder.build()); |
| 165 | + if (canAutoCloseExecutor) { |
| 166 | + // Since we converted background executor to a FixedExecutorProvider, we need |
| 167 | + // to add it back to the background resources, so it will be closed when we close the |
| 168 | + // client context. |
| 169 | + ImmutableList<BackgroundResource> backgroundResources = |
| 170 | + ImmutableList.<BackgroundResource>builder() |
| 171 | + .addAll(clientContext.getBackgroundResources()) |
| 172 | + .add(new ExecutorAsBackgroundResource(backgroundExecutor)) |
| 173 | + .build(); |
| 174 | + clientContext = clientContext.toBuilder().setBackgroundResources(backgroundResources).build(); |
| 175 | + } |
157 | 176 | if (channelPoolMetricsTracer != null) { |
158 | 177 | channelPoolMetricsTracer.start(clientContext.getExecutor()); |
159 | 178 | } |
@@ -208,6 +227,11 @@ public ClientContext getClientContext() { |
208 | 227 | return this.clientContext; |
209 | 228 | } |
210 | 229 |
|
| 230 | + public BigtableClientContext setClientContext(ClientContext clientContext) { |
| 231 | + return new BigtableClientContext( |
| 232 | + clientContext, this.openTelemetry, this.internalOpenTelemetry, this.metricsProvider); |
| 233 | + } |
| 234 | + |
211 | 235 | public void close() throws Exception { |
212 | 236 | for (BackgroundResource resource : clientContext.getBackgroundResources()) { |
213 | 237 | resource.close(); |
|
0 commit comments