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


##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/WindmillStreamPool.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.function.Supplier;
+import javax.annotation.concurrent.ThreadSafe;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+
+/**
+ * Pool of homogeneous streams to Windmill.
+ *
+ * <p>The pool holds a fixed total number of streams, and keeps each stream 
open for a specified
+ * time to allow for better load-balancing.
+ */
+@ThreadSafe
+public class WindmillStreamPool<StreamT extends WindmillStream> {
+
+  private final Duration streamTimeout;
+  private final Supplier<StreamT> streamSupplier;
+  private final Map<StreamT, StreamData<StreamT>> holds;
+  private final List<@Nullable StreamData<StreamT>> streamPool;
+
+  private WindmillStreamPool(
+      Duration streamTimeout,
+      Supplier<StreamT> streamSupplier,
+      List<@Nullable StreamData<StreamT>> streamPool,
+      Map<StreamT, StreamData<StreamT>> holds) {
+    this.streamPool = streamPool;
+    this.streamTimeout = streamTimeout;
+    this.streamSupplier = streamSupplier;
+    this.holds = holds;
+  }
+
+  public static <StreamT extends WindmillStream> WindmillStreamPool<StreamT> 
create(
+      int numStreams, Duration streamTimeout, Supplier<StreamT> 
streamSupplier) {
+    return new WindmillStreamPool<>(
+        streamTimeout, streamSupplier, newStreamList(numStreams), new 
ConcurrentHashMap<>());

Review Comment:
   I don't think the ConcurrentHashMap is necessary because we are guarding 
holds by synchronizing on this.  Since it is more complex it is more overhead 
in performance and understanding so prefer to just use a hashmap.
   
   You could add GuardedBy annotations to the guarded members. 
   
   



##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/WindmillStreamPool.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.function.Supplier;
+import javax.annotation.concurrent.ThreadSafe;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+
+/**
+ * Pool of homogeneous streams to Windmill.
+ *
+ * <p>The pool holds a fixed total number of streams, and keeps each stream 
open for a specified
+ * time to allow for better load-balancing.
+ */
+@ThreadSafe
+public class WindmillStreamPool<StreamT extends WindmillStream> {
+
+  private final Duration streamTimeout;
+  private final Supplier<StreamT> streamSupplier;
+  private final Map<StreamT, StreamData<StreamT>> holds;
+  private final List<@Nullable StreamData<StreamT>> streamPool;
+
+  private WindmillStreamPool(
+      Duration streamTimeout,
+      Supplier<StreamT> streamSupplier,
+      List<@Nullable StreamData<StreamT>> streamPool,
+      Map<StreamT, StreamData<StreamT>> holds) {
+    this.streamPool = streamPool;
+    this.streamTimeout = streamTimeout;
+    this.streamSupplier = streamSupplier;
+    this.holds = holds;
+  }
+
+  public static <StreamT extends WindmillStream> WindmillStreamPool<StreamT> 
create(
+      int numStreams, Duration streamTimeout, Supplier<StreamT> 
streamSupplier) {
+    return new WindmillStreamPool<>(
+        streamTimeout, streamSupplier, newStreamList(numStreams), new 
ConcurrentHashMap<>());
+  }
+
+  @VisibleForTesting
+  static <StreamT extends WindmillStream> WindmillStreamPool<StreamT> 
forTesting(
+      Duration streamTimeout,
+      Supplier<StreamT> streamSupplier,
+      List<@Nullable StreamData<StreamT>> streamPool,
+      Map<StreamT, StreamData<StreamT>> holds) {
+    return new WindmillStreamPool<>(streamTimeout, streamSupplier, streamPool, 
holds);
+  }
+
+  /**
+   * Creates a new list of streams of the given capacity with all values 
initialized to null. This
+   * is because we randomly load balance across all the streams in the pool.
+   */
+  @VisibleForTesting
+  static <StreamT extends WindmillStream> List<@Nullable StreamData<StreamT>> 
newStreamList(
+      int numStreams) {
+    List<@Nullable StreamData<StreamT>> streamPool = new 
ArrayList<>(numStreams);
+    for (int i = 0; i < numStreams; i++) {
+      streamPool.add(null);
+    }
+    return streamPool;
+  }
+
+  /**
+   * Returns a stream for use that may be cached from a previous call. Each 
call of getStream must
+   * be matched with a call of {@link 
WindmillStreamPool#releaseStream(WindmillStream)}. If the
+   * stream has been cached but has timed out and drained (no longer has any 
holds), the stream will
+   * be closed.
+   */
+  public StreamT getStream() {
+    int index = streamPool.size() == 1 ? 0 : 
ThreadLocalRandom.current().nextInt(streamPool.size());
+    // We will return this stream
+    StreamT resultStream;
+    StreamT closeThisStream = null;
+    try {
+      synchronized (this) {
+        WindmillStreamPool.StreamData<StreamT> existingStreamData = 
streamPool.get(index);
+        // There are 3 possible states that can result from fetching the 
stream from the cache.
+        // 1. Stream doesn't exist create and cache a new one.
+        if (existingStreamData == null) {
+          resultStream = createAndCacheStream(index).stream;
+        }
+        // 2. The stream exists, but has timed out. The timed out stream is 
not returned (a new one
+        // is created and returned here) and evicted from the cache if the 
stream has completely
+        // drained. Every call to getStream(), is matched with a call to 
releaseStream(), so the
+        // stream will eventually drain and be closed either there or here if 
we randomly fetch the
+        // stream again.
+        else if (existingStreamData.hasTimedOut(streamTimeout)) {
+          if (--existingStreamData.holds == 0) {
+            holds.remove(existingStreamData.stream);
+            closeThisStream = existingStreamData.stream;
+          }
+          // Create and cache a new stream at the closed stream's index.
+          resultStream = createAndCacheStream(index).stream;
+        } else {
+          // 3. The stream exists and is in a valid state.
+          existingStreamData.holds++;
+          resultStream = existingStreamData.stream;
+        }
+      }
+      return resultStream;
+    } finally {
+      if (closeThisStream != null) {
+        closeThisStream.close();
+      }
+    }
+  }
+
+  private synchronized WindmillStreamPool.StreamData<StreamT> 
createAndCacheStream(int cacheKey) {
+    WindmillStreamPool.StreamData<StreamT> newStreamData =
+        new WindmillStreamPool.StreamData<>(streamSupplier.get());
+    newStreamData.holds++;
+    streamPool.set(cacheKey, newStreamData);
+    holds.put(newStreamData.stream, newStreamData);
+    return newStreamData;
+  }
+
+  /** Releases a stream that was obtained with {@link 
WindmillStreamPool#getStream()}. */
+  public void releaseStream(StreamT stream) {
+    boolean closeStream = false;
+    synchronized (this) {
+      StreamData<StreamT> streamData = holds.get(stream);
+      // All streams that are created by an instance of a pool will be present.
+      if (streamData == null) {
+        throw new IllegalStateException(
+            "Attempted to release stream that does not exist in this pool. 
Streams not created from this pool "
+                + "cannot be released by it.");

Review Comment:
   or this stream may have been released more times than acquired.



##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/WindmillStreamPool.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.function.Supplier;
+import javax.annotation.concurrent.ThreadSafe;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+
+/**
+ * Pool of homogeneous streams to Windmill.
+ *
+ * <p>The pool holds a fixed total number of streams, and keeps each stream 
open for a specified
+ * time to allow for better load-balancing.
+ */
+@ThreadSafe
+public class WindmillStreamPool<StreamT extends WindmillStream> {
+
+  private final Duration streamTimeout;
+  private final Supplier<StreamT> streamSupplier;
+  private final Map<StreamT, StreamData<StreamT>> holds;
+  private final List<@Nullable StreamData<StreamT>> streamPool;

Review Comment:
   comment that the size never changes?
   



##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/WindmillStreamPool.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.function.Supplier;
+import javax.annotation.concurrent.ThreadSafe;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+
+/**
+ * Pool of homogeneous streams to Windmill.
+ *
+ * <p>The pool holds a fixed total number of streams, and keeps each stream 
open for a specified
+ * time to allow for better load-balancing.
+ */
+@ThreadSafe
+public class WindmillStreamPool<StreamT extends WindmillStream> {
+
+  private final Duration streamTimeout;
+  private final Supplier<StreamT> streamSupplier;
+  private final Map<StreamT, StreamData<StreamT>> holds;
+  private final List<@Nullable StreamData<StreamT>> streamPool;
+
+  private WindmillStreamPool(
+      Duration streamTimeout,
+      Supplier<StreamT> streamSupplier,
+      List<@Nullable StreamData<StreamT>> streamPool,
+      Map<StreamT, StreamData<StreamT>> holds) {
+    this.streamPool = streamPool;
+    this.streamTimeout = streamTimeout;
+    this.streamSupplier = streamSupplier;
+    this.holds = holds;
+  }
+
+  public static <StreamT extends WindmillStream> WindmillStreamPool<StreamT> 
create(
+      int numStreams, Duration streamTimeout, Supplier<StreamT> 
streamSupplier) {
+    return new WindmillStreamPool<>(
+        streamTimeout, streamSupplier, newStreamList(numStreams), new 
ConcurrentHashMap<>());
+  }
+
+  @VisibleForTesting
+  static <StreamT extends WindmillStream> WindmillStreamPool<StreamT> 
forTesting(
+      Duration streamTimeout,
+      Supplier<StreamT> streamSupplier,
+      List<@Nullable StreamData<StreamT>> streamPool,
+      Map<StreamT, StreamData<StreamT>> holds) {
+    return new WindmillStreamPool<>(streamTimeout, streamSupplier, streamPool, 
holds);
+  }
+
+  /**
+   * Creates a new list of streams of the given capacity with all values 
initialized to null. This
+   * is because we randomly load balance across all the streams in the pool.
+   */
+  @VisibleForTesting
+  static <StreamT extends WindmillStream> List<@Nullable StreamData<StreamT>> 
newStreamList(
+      int numStreams) {
+    List<@Nullable StreamData<StreamT>> streamPool = new 
ArrayList<>(numStreams);
+    for (int i = 0; i < numStreams; i++) {
+      streamPool.add(null);
+    }
+    return streamPool;
+  }
+
+  /**
+   * Returns a stream for use that may be cached from a previous call. Each 
call of getStream must
+   * be matched with a call of {@link 
WindmillStreamPool#releaseStream(WindmillStream)}. If the
+   * stream has been cached but has timed out and drained (no longer has any 
holds), the stream will
+   * be closed.
+   */
+  public StreamT getStream() {
+    int index = streamPool.size() == 1 ? 0 : 
ThreadLocalRandom.current().nextInt(streamPool.size());
+    // We will return this stream
+    StreamT resultStream;
+    StreamT closeThisStream = null;
+    try {
+      synchronized (this) {
+        WindmillStreamPool.StreamData<StreamT> existingStreamData = 
streamPool.get(index);
+        // There are 3 possible states that can result from fetching the 
stream from the cache.
+        // 1. Stream doesn't exist create and cache a new one.

Review Comment:
   nit: move the 1. and 2. comments into the if blocks like the 3. comment is, 
I think it will make the if-else tree more apparent.



##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/WindmillStreamPool.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.function.Supplier;
+import javax.annotation.concurrent.ThreadSafe;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+
+/**
+ * Pool of homogeneous streams to Windmill.
+ *
+ * <p>The pool holds a fixed total number of streams, and keeps each stream 
open for a specified
+ * time to allow for better load-balancing.
+ */
+@ThreadSafe
+public class WindmillStreamPool<StreamT extends WindmillStream> {
+
+  private final Duration streamTimeout;
+  private final Supplier<StreamT> streamSupplier;
+  private final Map<StreamT, StreamData<StreamT>> holds;
+  private final List<@Nullable StreamData<StreamT>> streamPool;
+
+  private WindmillStreamPool(
+      Duration streamTimeout,
+      Supplier<StreamT> streamSupplier,
+      List<@Nullable StreamData<StreamT>> streamPool,
+      Map<StreamT, StreamData<StreamT>> holds) {
+    this.streamPool = streamPool;
+    this.streamTimeout = streamTimeout;
+    this.streamSupplier = streamSupplier;
+    this.holds = holds;
+  }
+
+  public static <StreamT extends WindmillStream> WindmillStreamPool<StreamT> 
create(
+      int numStreams, Duration streamTimeout, Supplier<StreamT> 
streamSupplier) {
+    return new WindmillStreamPool<>(
+        streamTimeout, streamSupplier, newStreamList(numStreams), new 
ConcurrentHashMap<>());
+  }
+
+  @VisibleForTesting
+  static <StreamT extends WindmillStream> WindmillStreamPool<StreamT> 
forTesting(
+      Duration streamTimeout,
+      Supplier<StreamT> streamSupplier,
+      List<@Nullable StreamData<StreamT>> streamPool,
+      Map<StreamT, StreamData<StreamT>> holds) {
+    return new WindmillStreamPool<>(streamTimeout, streamSupplier, streamPool, 
holds);
+  }
+
+  /**
+   * Creates a new list of streams of the given capacity with all values 
initialized to null. This
+   * is because we randomly load balance across all the streams in the pool.
+   */
+  @VisibleForTesting
+  static <StreamT extends WindmillStream> List<@Nullable StreamData<StreamT>> 
newStreamList(
+      int numStreams) {
+    List<@Nullable StreamData<StreamT>> streamPool = new 
ArrayList<>(numStreams);
+    for (int i = 0; i < numStreams; i++) {
+      streamPool.add(null);
+    }
+    return streamPool;
+  }
+
+  /**
+   * Returns a stream for use that may be cached from a previous call. Each 
call of getStream must
+   * be matched with a call of {@link 
WindmillStreamPool#releaseStream(WindmillStream)}. If the
+   * stream has been cached but has timed out and drained (no longer has any 
holds), the stream will
+   * be closed.
+   */
+  public StreamT getStream() {
+    int index = streamPool.size() == 1 ? 0 : 
ThreadLocalRandom.current().nextInt(streamPool.size());
+    // We will return this stream
+    StreamT resultStream;
+    StreamT closeThisStream = null;
+    try {
+      synchronized (this) {
+        WindmillStreamPool.StreamData<StreamT> existingStreamData = 
streamPool.get(index);
+        // There are 3 possible states that can result from fetching the 
stream from the cache.
+        // 1. Stream doesn't exist create and cache a new one.
+        if (existingStreamData == null) {
+          resultStream = createAndCacheStream(index).stream;
+        }
+        // 2. The stream exists, but has timed out. The timed out stream is 
not returned (a new one
+        // is created and returned here) and evicted from the cache if the 
stream has completely
+        // drained. Every call to getStream(), is matched with a call to 
releaseStream(), so the
+        // stream will eventually drain and be closed either there or here if 
we randomly fetch the

Review Comment:
   "or here if we randomly fetch the stream again" is confusing to me. Since 
we're removing it from cache it won't be fetched here again.



##########
runners/google-cloud-dataflow-java/worker/src/test/java/org/apache/beam/runners/dataflow/worker/FakeWindmillServer.java:
##########
@@ -485,4 +437,55 @@ public boolean isReady() {
   public void setIsReady(boolean ready) {
     this.isReady = ready;
   }
+
+  static class ResponseQueue<T, U> {

Review Comment:
   Is it preferred style to put inner classes later? Otherwise reduce churn by 
keeping this where it was.



-- 
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