scwhittle commented on code in PR #27593: URL: https://github.com/apache/beam/pull/27593#discussion_r1285703193
########## runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/WindmillStreamPool.java: ########## @@ -0,0 +1,175 @@ +/* + * 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 WindmillServerStub.WindmillStream> { Review Comment: should the stream interface be moved out too? seems odd to extend an inner class here ########## runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/WindmillStreamPool.java: ########## @@ -0,0 +1,175 @@ +/* + * 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 WindmillServerStub.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 WindmillServerStub.WindmillStream> + WindmillStreamPool<StreamT> create( + int numStreams, Duration streamTimeout, Supplier<StreamT> streamSupplier) { + return new WindmillStreamPool<>( + streamTimeout, streamSupplier, newStreamPool(numStreams), new ConcurrentHashMap<>()); + } + + @VisibleForTesting + static <StreamT extends WindmillServerStub.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 of the streams in the pool. + */ + @VisibleForTesting + static <StreamT extends WindmillServerStub.WindmillStream> + List<@Nullable StreamData<StreamT>> newStreamPool(int numStreams) { Review Comment: nit: this is a confusing method name imo, since it sounds like it should return a StreamPool. How about newStreamList? Or perhaps just remove it and do something like the following at all call-sites new ArrayList(Collections.nCopies(numStreams, null)) ########## runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/WindmillStreamPool.java: ########## @@ -0,0 +1,175 @@ +/* + * 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 WindmillServerStub.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 WindmillServerStub.WindmillStream> + WindmillStreamPool<StreamT> create( + int numStreams, Duration streamTimeout, Supplier<StreamT> streamSupplier) { + return new WindmillStreamPool<>( + streamTimeout, streamSupplier, newStreamPool(numStreams), new ConcurrentHashMap<>()); + } + + @VisibleForTesting + static <StreamT extends WindmillServerStub.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 of the streams in the pool. + */ + @VisibleForTesting + static <StreamT extends WindmillServerStub.WindmillStream> + List<@Nullable StreamData<StreamT>> newStreamPool(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(WindmillServerStub.WindmillStream)}. If the stream has been + * cached but has timed out and 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 and can be closed. The timed out stream is not + // returned (a new one is created and returned here) and evicted from the cache. Every + // call to getStream(), is matched with a call to releaseStream(), so the stream will + // eventually be closed there if not here. + else if (existingStreamData.hasTimedOut(streamTimeout) && --existingStreamData.holds == 0) { Review Comment: this is not quite the same logic as before. Previously once it was timed out, a new stream was created and put into streamPool. Thus the holds on the previous stream would eventually drain as they were released. Now a new stream is put into streamPool only if it has both timed out and has no holds. But if we are always adding holds we may never end up never changing the stream in streamPool. I think you could improve the unit test to catch this. -- 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]
