scwhittle commented on code in PR #30233:
URL: https://github.com/apache/beam/pull/30233#discussion_r1482980836


##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/client/grpc/stubs/IsolationChannel.java:
##########
@@ -0,0 +1,246 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.runners.dataflow.worker.windmill.client.grpc.stubs;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Supplier;
+import java.util.logging.Logger;
+import javax.annotation.concurrent.GuardedBy;
+import org.apache.beam.sdk.annotations.Internal;
+import org.apache.beam.vendor.grpc.v1p60p1.io.grpc.CallOptions;
+import org.apache.beam.vendor.grpc.v1p60p1.io.grpc.ClientCall;
+import 
org.apache.beam.vendor.grpc.v1p60p1.io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
+import 
org.apache.beam.vendor.grpc.v1p60p1.io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
+import org.apache.beam.vendor.grpc.v1p60p1.io.grpc.ManagedChannel;
+import org.apache.beam.vendor.grpc.v1p60p1.io.grpc.Metadata;
+import org.apache.beam.vendor.grpc.v1p60p1.io.grpc.MethodDescriptor;
+import org.apache.beam.vendor.grpc.v1p60p1.io.grpc.Status;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions;
+
+/**
+ * A {@link ManagedChannel} that creates a dynamic # of cached channels to the 
same endpoint such
+ * that each active rpc has its own channel.
+ */
+@Internal
+class IsolationChannel extends ManagedChannel {
+  static final Logger LOG = Logger.getLogger(IsolationChannel.class.getName());
+
+  private final Supplier<ManagedChannel> channelFactory;
+
+  @GuardedBy("this")
+  private final List<ManagedChannel> channelCache;
+
+  @GuardedBy("this")
+  private final Set<ManagedChannel> usedChannels;
+
+  @GuardedBy("this")
+  private boolean shutdownStarted = false;
+
+  private final String authority;
+
+  // Expected that supplier returns channels to the same endpoint with the 
same authority.
+  public static IsolationChannel create(Supplier<ManagedChannel> 
channelFactory) {
+    return new IsolationChannel(channelFactory);
+  }
+
+  @VisibleForTesting
+  IsolationChannel(Supplier<ManagedChannel> channelFactory) {
+    this.channelFactory = channelFactory;
+    this.channelCache = new ArrayList<>();
+    ManagedChannel channel = channelFactory.get();
+    this.authority = channel.authority();
+    this.channelCache.add(channel);
+    this.usedChannels = new HashSet<>();
+  }
+
+  @Override
+  public String authority() {
+    return authority;
+  }
+
+  /** Pick an unused channel from the set or create one and then create a 
{@link ClientCall}. */
+  @Override
+  public <ReqT, RespT> ClientCall<ReqT, RespT> newCall(
+      MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions) 
{
+    ManagedChannel channel = getChannel();
+    return new ReleasingClientCall<>(this, channel, 
channel.newCall(methodDescriptor, callOptions));
+  }
+
+  private synchronized ManagedChannel getChannel() {
+    if (!channelCache.isEmpty()) {
+      ManagedChannel result = channelCache.remove(channelCache.size() - 1);
+      usedChannels.add(result);
+      return result;
+    }
+    if (shutdownStarted) {
+      // Avoid creating a new channel if we're shutting down and just use an 
existing one
+      // that has already started shutting down.
+      Preconditions.checkArgument(!usedChannels.isEmpty());
+      return usedChannels.iterator().next();
+    }
+
+    ManagedChannel result = channelFactory.get();
+    usedChannels.add(result);
+    return result;
+  }
+
+  @Override
+  public synchronized ManagedChannel shutdown() {

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to