AHeise commented on a change in pull request #10009: [FLINK-14304] Avoid task starvation with mailbox URL: https://github.com/apache/flink/pull/10009#discussion_r342590128
########## File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/mailbox/BatchReadableMailbox.java ########## @@ -0,0 +1,61 @@ +/* + * 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.streaming.runtime.tasks.mailbox; + +import java.util.Optional; + +/** + * Extends {@link ReadableMailbox} to support taking messages in batches. A batch is a local view on the mailbox that + * does not contain simultaneously added mails similar to iterators of copy-on-write collections. + * + * <p>A batch serves two purposes: it reduces synchronization if more than one mail is processable at the time of the + * creation of a batch. Furthermore, it allows to divide the work of a mailbox in smaller logical chunks, such that + * the task threads cannot be blocked by a mail that enqueues itself and thus provides input starvation. + * + * <p>All methods can only be invoked by the mailbox thread, which is passed upon construction. To verify that the + * current thread is allowed to take any mail, use {@link #isMailboxThread()}, but all methods will perform the check + * themselves. + * + * <p>Note that there is no blocking takeFromBatch as batches can only be created and consumed from the mailbox thread. + */ +public interface BatchReadableMailbox extends ReadableMailbox { + + /** + * Creates a batch of mails that can be taken with {@link #tryTakeFromBatch()}. The batch does not affect any + * method from {@link ReadableMailbox}. + * + * <p>The default batch is empty. Thus, this method must be invoked once before {@link #tryTakeFromBatch()}. + * + * <p>If a batch is not completely consumed by {@link #tryTakeFromBatch()}, its elements are carried over to the + * new batch. + * + * @return true if there is at least one element in the batch; that is, if there is any mail at all at the time + * of the invocation. + */ + boolean createBatch(); Review comment: Same as above: Calling createBatch every time you call `tryTake` or `take` would not solve task starvation at all. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
