Class TaskMailboxImpl
- java.lang.Object
-
- org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl
-
- All Implemented Interfaces:
TaskMailbox
@ThreadSafe public class TaskMailboxImpl extends Object implements TaskMailbox
Implementation ofTaskMailboxin aBlockingQueuefashion and tailored towards our use case with multiple writers and single reader.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailbox
TaskMailbox.MailboxClosedException, TaskMailbox.State
-
-
Field Summary
-
Fields inherited from interface org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailbox
MAX_PRIORITY, MIN_PRIORITY
-
-
Constructor Summary
Constructors Constructor Description TaskMailboxImpl()TaskMailboxImpl(Thread taskMailboxThread)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description List<Mail>close()Close the mailbox.booleancreateBatch()Creates a batch of mails that can be taken withTaskMailbox.tryTakeFromBatch().List<Mail>drain()Drains the mailbox and returns all mails that were still enqueued.TaskMailbox.StategetState()Returns the current state of the mailbox as defined by the lifecycle enumTaskMailbox.State.booleanhasMail()Returnstrueif the mailbox contains mail.booleanisMailboxThread()Check if the current thread is the mailbox thread.voidput(Mail mail)Enqueues the given mail to the mailbox and blocks until there is capacity for a successful put.voidquiesce()Quiesce the mailbox.voidrunExclusively(Runnable runnable)Runs the given code exclusively on this mailbox.intsize()Returns the current number of mails in this mailbox.Mailtake(int priority)This method returns the oldest mail from the mailbox (head of queue) or blocks until a mail is available.Optional<Mail>tryTake(int priority)Returns an optional with either the oldest mail from the mailbox (head of queue) if the mailbox is not empty or an empty optional otherwise.Optional<Mail>tryTakeFromBatch()Returns an optional with either the oldest mail from the batch (head of queue) if the batch is not empty or an empty optional otherwise.
-
-
-
Constructor Detail
-
TaskMailboxImpl
public TaskMailboxImpl(@Nonnull Thread taskMailboxThread)
-
TaskMailboxImpl
@VisibleForTesting public TaskMailboxImpl()
-
-
Method Detail
-
isMailboxThread
public boolean isMailboxThread()
Description copied from interface:TaskMailboxCheck if the current thread is the mailbox thread.Read operations will fail if they are called from another thread.
- Specified by:
isMailboxThreadin interfaceTaskMailbox- Returns:
- only true if called from the mailbox thread.
-
hasMail
public boolean hasMail()
Description copied from interface:TaskMailboxReturnstrueif the mailbox contains mail.Must be called from the mailbox thread (
TaskMailbox.isMailboxThread().- Specified by:
hasMailin interfaceTaskMailbox
-
size
public int size()
Description copied from interface:TaskMailboxReturns the current number of mails in this mailbox. (This includes mails in the batch not processed yet.)- Specified by:
sizein interfaceTaskMailbox- Returns:
- number of mails in the mailbox.
-
tryTake
public Optional<Mail> tryTake(int priority)
Description copied from interface:TaskMailboxReturns an optional with either the oldest mail from the mailbox (head of queue) if the mailbox is not empty or an empty optional otherwise.Must be called from the mailbox thread (
TaskMailbox.isMailboxThread().- Specified by:
tryTakein interfaceTaskMailbox- Returns:
- an optional with either the oldest mail from the mailbox (head of queue) if the mailbox is not empty or an empty optional otherwise.
-
take
@Nonnull public Mail take(int priority) throws InterruptedException, IllegalStateException
Description copied from interface:TaskMailboxThis method returns the oldest mail from the mailbox (head of queue) or blocks until a mail is available.Must be called from the mailbox thread (
TaskMailbox.isMailboxThread().- Specified by:
takein interfaceTaskMailbox- Returns:
- the oldest mail from the mailbox (head of queue).
- Throws:
InterruptedException- on interruption.IllegalStateException- if mailbox is already closed.
-
createBatch
public boolean createBatch()
Description copied from interface:TaskMailboxCreates a batch of mails that can be taken withTaskMailbox.tryTakeFromBatch(). The batch does not affectTaskMailbox.tryTake(int)andTaskMailbox.take(int); that is, they return the same mails even if no batch had been created.The default batch is empty. Thus, this method must be invoked once before
TaskMailbox.tryTakeFromBatch().If a batch is not completely consumed by
TaskMailbox.tryTakeFromBatch(), its elements are carried over to the new batch.Must be called from the mailbox thread (
TaskMailbox.isMailboxThread().- Specified by:
createBatchin interfaceTaskMailbox- Returns:
- 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.
-
tryTakeFromBatch
public Optional<Mail> tryTakeFromBatch()
Description copied from interface:TaskMailboxReturns an optional with either the oldest mail from the batch (head of queue) if the batch is not empty or an empty optional otherwise.Must be called from the mailbox thread (
TaskMailbox.isMailboxThread().Note that there is no blocking
takeFromBatchas batches can only be created and consumed from the mailbox thread.- Specified by:
tryTakeFromBatchin interfaceTaskMailbox- Returns:
- an optional with either the oldest mail from the batch (head of queue) if the batch is not empty or an empty optional otherwise.
-
put
public void put(@Nonnull Mail mail)Description copied from interface:TaskMailboxEnqueues the given mail to the mailbox and blocks until there is capacity for a successful put. The Mail with (MailboxExecutor.MailOptions.isUrgent()will be put in the head.Mails can be added from any thread.
- Specified by:
putin interfaceTaskMailbox- Parameters:
mail- the mail to enqueue.
-
drain
public List<Mail> drain()
Description copied from interface:TaskMailboxDrains the mailbox and returns all mails that were still enqueued.- Specified by:
drainin interfaceTaskMailbox- Returns:
- list with all mails that where enqueued in the mailbox.
-
quiesce
public void quiesce()
Description copied from interface:TaskMailboxQuiesce the mailbox. In this state, the mailbox supports only take operations and all pending and future put operations will throwTaskMailbox.MailboxClosedException.- Specified by:
quiescein interfaceTaskMailbox
-
close
@Nonnull public List<Mail> close()
Description copied from interface:TaskMailboxClose the mailbox. In this state, all pending and future put operations and all pending and future take operations will throwTaskMailbox.MailboxClosedException. Returns all mails that were still enqueued.- Specified by:
closein interfaceTaskMailbox- Returns:
- list with all mails that where enqueued in the mailbox at the time of closing.
-
getState
@Nonnull public TaskMailbox.State getState()
Description copied from interface:TaskMailboxReturns the current state of the mailbox as defined by the lifecycle enumTaskMailbox.State.- Specified by:
getStatein interfaceTaskMailbox- Returns:
- the current state of the mailbox.
-
runExclusively
public void runExclusively(Runnable runnable)
Description copied from interface:TaskMailboxRuns the given code exclusively on this mailbox. No synchronized operations can be run concurrently to the given runnable (e.g.,TaskMailbox.put(Mail)or modifying lifecycle methods).Use this methods when you want to atomically execute code that uses different methods (e.g., check for state and then put message if open).
- Specified by:
runExclusivelyin interfaceTaskMailbox- Parameters:
runnable- the runnable to execute
-
-