StefanRRichter commented on a change in pull request #8442: [FLINK-12483] Support (legacy) SourceFunction as special case in the mailbox model for stream tasks URL: https://github.com/apache/flink/pull/8442#discussion_r285896322
########## File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/mailbox/MailboxImpl.java ########## @@ -0,0 +1,233 @@ +/* + * 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 org.apache.flink.util.Preconditions; + +import javax.annotation.Nonnull; +import javax.annotation.concurrent.GuardedBy; + +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +/** + * Implementation of {@link Mailbox} inspired by {@link java.util.concurrent.ArrayBlockingQueue} and tailored towards + * our use case with multiple writers, single reader and volatile reads instead of lock & read on {@link #count}. + */ +public class MailboxImpl implements Mailbox { + + /** + * The enqueued letters. + */ + @GuardedBy("lock") + private final Runnable[] ringBuffer; + + /** + * Lock for all concurrent ops. + */ + private final ReentrantLock lock; + + /** + * Condition that is triggered when the buffer is no longer empty. + */ + @GuardedBy("lock") + private final Condition notEmpty; + + /** + * Condition that is triggered when the buffer is no longer full. + */ + @GuardedBy("lock") + private final Condition notFull; + + /** + * Index of the ring buffer head. + */ + @GuardedBy("lock") + private int headIndex; + + /** + * Index of the ring buffer tail. + */ + @GuardedBy("lock") + private int tailIndex; + + /** + * Number of letters in the mailbox. + */ + @GuardedBy("lock") + private volatile int count; + + /** + * A mask to wrap around the indexes of the ring buffer. We use this to avoid ifs or modulo ops. + */ + private final int moduloMask; + + public MailboxImpl() { + this(6); // 2^6 = 64 + } + + public MailboxImpl(int capacityPow2) { + final int capacity = 1 << capacityPow2; + Preconditions.checkState(capacity > 0); + this.moduloMask = capacity - 1; + this.ringBuffer = new Runnable[capacity]; + this.lock = new ReentrantLock(); + this.notEmpty = lock.newCondition(); + this.notFull = lock.newCondition(); + } + + @Override + public boolean hasMail() { Review comment: Oh yes, this method has a different semantic. I only introduced it to write to code in a more readable way instead of repeating the check code all over the place. Will think about it and maybe suffix those private methods with `...Internal` ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services