afedulov commented on code in PR #20757: URL: https://github.com/apache/flink/pull/20757#discussion_r965724678
########## flink-core/src/main/java/org/apache/flink/api/connector/source/lib/util/RateLimitedSourceReader.java: ########## @@ -0,0 +1,90 @@ +/* + * 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.flink.api.connector.source.lib.util; + +import org.apache.flink.annotation.Experimental; +import org.apache.flink.api.connector.source.ReaderOutput; +import org.apache.flink.api.connector.source.SourceReader; +import org.apache.flink.api.connector.source.SourceSplit; +import org.apache.flink.core.io.InputStatus; + +import java.util.List; +import java.util.concurrent.CompletableFuture; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** Wraps the actual {@link SourceReader} and rate limits its data emission. */ +@Experimental +public class RateLimitedSourceReader<E, SplitT extends SourceSplit> + implements SourceReader<E, SplitT> { + + private final SourceReader<E, SplitT> sourceReader; + private final RateLimiter rateLimiter; + + /** + * Instantiates a new rate-limited source reader. + * + * @param sourceReader The actual source reader. + * @param rateLimiter The rate limiter. + */ + public RateLimitedSourceReader(SourceReader<E, SplitT> sourceReader, RateLimiter rateLimiter) { + checkNotNull(sourceReader); + checkNotNull(rateLimiter); + this.sourceReader = sourceReader; + this.rateLimiter = rateLimiter; + } + + // ------------------------------------------------------------------------ + + @Override + public void start() { + sourceReader.start(); + } + + @Override + public InputStatus pollNext(ReaderOutput<E> output) throws Exception { + rateLimiter.acquire(); Review Comment: Thanks for the sketch. A couple of thoughts: - Can't we use [tryAcquire()](https://guava.dev/releases/19.0/api/docs/com/google/common/util/concurrent/RateLimiter.html#tryAcquire()) for such implementation directly? - Let's say we set the count to Long.MAX_VALUE. The call to `sourceReader.pollNext(output)` is in the sketch is unrestricted and happens in a busy loop. The underlying sourceReader is going to keep internally calling `output.collect(iterator.next())` without any limitations. Won't it blow up the underlying queues/futures? - If the capacity is not an issue, if I am not mistaken, this will still cause the number of cores equal to the configured parallelism to go into a 100% load state even when configured to run with, say, 10 records per second. That might be surprising for users who just want to run a demo/quick test locally. Do I get it wrong? ########## flink-core/src/main/java/org/apache/flink/api/connector/source/lib/util/RateLimitedSourceReader.java: ########## @@ -0,0 +1,90 @@ +/* + * 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.flink.api.connector.source.lib.util; + +import org.apache.flink.annotation.Experimental; +import org.apache.flink.api.connector.source.ReaderOutput; +import org.apache.flink.api.connector.source.SourceReader; +import org.apache.flink.api.connector.source.SourceSplit; +import org.apache.flink.core.io.InputStatus; + +import java.util.List; +import java.util.concurrent.CompletableFuture; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** Wraps the actual {@link SourceReader} and rate limits its data emission. */ +@Experimental +public class RateLimitedSourceReader<E, SplitT extends SourceSplit> + implements SourceReader<E, SplitT> { + + private final SourceReader<E, SplitT> sourceReader; + private final RateLimiter rateLimiter; + + /** + * Instantiates a new rate-limited source reader. + * + * @param sourceReader The actual source reader. + * @param rateLimiter The rate limiter. + */ + public RateLimitedSourceReader(SourceReader<E, SplitT> sourceReader, RateLimiter rateLimiter) { + checkNotNull(sourceReader); + checkNotNull(rateLimiter); + this.sourceReader = sourceReader; + this.rateLimiter = rateLimiter; + } + + // ------------------------------------------------------------------------ + + @Override + public void start() { + sourceReader.start(); + } + + @Override + public InputStatus pollNext(ReaderOutput<E> output) throws Exception { + rateLimiter.acquire(); Review Comment: Thanks for the sketch. A couple of thoughts: - Can't we use [tryAcquire()](https://guava.dev/releases/19.0/api/docs/com/google/common/util/concurrent/RateLimiter.html#tryAcquire()) for such implementation directly? - Let's say we set the count to Long.MAX_VALUE. The call to `sourceReader.pollNext(output)` in the sketch is unrestricted and happens in a busy loop. The underlying sourceReader is going to keep internally calling `output.collect(iterator.next())` without any limitations. Won't it blow up the underlying queues/futures? - If the capacity is not an issue, if I am not mistaken, this will still cause the number of cores equal to the configured parallelism to go into a 100% load state even when configured to run with, say, 10 records per second. That might be surprising for users who just want to run a demo/quick test locally. Do I get it wrong? -- 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]
