@Experimental(value=TRIGGER) public abstract class Trigger extends Object implements Serializable
Triggers control when the elements for a specific key and window are output. As elements
arrive, they are put into one or more windows by a Window transform and its associated
WindowFn, and then passed to the associated Trigger to determine if the
Windows contents should be output.
See GroupByKey and Window
for more information about how grouping with windows works.
The elements that are assigned to a window since the last time it was fired (or since the window was created) are placed into the current window pane. Triggers are evaluated against the elements as they are added. When the root trigger fires, the elements in the current pane will be output. When the root trigger finishes (indicating it will never fire again), the window is closed and any new elements assigned to that window are discarded.
Several predefined Triggers are provided:
AfterWatermark for firing when the watermark passes a timestamp determined from
either the end of the window or the arrival of the first element in a pane.
AfterProcessingTime for firing after some amount of processing time has elapsed
(typically since the first element in a pane).
AfterPane for firing off a property of the elements in the current pane, such as
the number of elements that have been assigned to the current pane.
In addition, Triggers can be combined in a variety of ways:
Repeatedly.forever(org.apache.beam.sdk.transforms.windowing.Trigger) to create a trigger that executes forever. Any time its
argument finishes it gets reset and starts over. Can be combined with
orFinally(org.apache.beam.sdk.transforms.windowing.Trigger.OnceTrigger) to specify a condition that causes the repetition to stop.
AfterEach.inOrder(org.apache.beam.sdk.transforms.windowing.Trigger...) to execute each trigger in sequence, firing each (and every)
time that a trigger fires, and advancing to the next trigger in the sequence when it finishes.
AfterFirst.of(org.apache.beam.sdk.transforms.windowing.Trigger.OnceTrigger...) to create a trigger that fires after at least one of its arguments
fires. An AfterFirst trigger finishes after it fires once.
AfterAll.of(org.apache.beam.sdk.transforms.windowing.Trigger.OnceTrigger...) to create a trigger that fires after all least one of its arguments
have fired at least once. An AfterAll trigger finishes after it fires once.
Each trigger tree is instantiated per-key and per-window. Every trigger in the tree is in one of the following states:
Once finished, a trigger cannot return itself back to an earlier state, however a composite trigger could reset its sub-triggers.
Triggers should not build up any state internally since they may be recreated between invocations of the callbacks. All important values should be persisted using state before the callback returns.
| Modifier and Type | Class and Description |
|---|---|
static interface |
Trigger.MergingTriggerInfo
Interact with properties of the trigger being executed, with extensions to deal with the
merging windows.
|
static class |
Trigger.OnceTrigger
|
class |
Trigger.OnElementContext
Extended
Trigger.TriggerContext containing information accessible to the onElement(org.apache.beam.sdk.transforms.windowing.Trigger.OnElementContext)
operational hook. |
class |
Trigger.OnMergeContext
Extended
Trigger.TriggerContext containing information accessible to the onMerge(org.apache.beam.sdk.transforms.windowing.Trigger.OnMergeContext)
operational hook. |
class |
Trigger.TriggerContext
Information accessible to all operational hooks in this
Trigger. |
static interface |
Trigger.TriggerInfo
Interface for accessing information about the trigger being executed and other triggers in the
same tree.
|
| Modifier and Type | Field and Description |
|---|---|
protected List<Trigger> |
subTriggers |
| Modifier | Constructor and Description |
|---|---|
protected |
Trigger(List<Trigger> subTriggers) |
| Modifier and Type | Method and Description |
|---|---|
void |
clear(Trigger.TriggerContext c)
Clear any state associated with this trigger in the given window.
|
boolean |
equals(Object obj) |
Trigger |
getContinuationTrigger()
Return a trigger to use after a
GroupByKey to preserve the
intention of this trigger. |
protected abstract Trigger |
getContinuationTrigger(List<Trigger> continuationTriggers)
Return the
getContinuationTrigger() of this Trigger. |
abstract Instant |
getWatermarkThatGuaranteesFiring(BoundedWindow window)
Returns a bound in watermark time by which this trigger would have fired at least once
for a given window had there been input data.
|
int |
hashCode() |
boolean |
isCompatible(Trigger other)
Returns whether this performs the same triggering as the given
Trigger. |
abstract void |
onElement(Trigger.OnElementContext c)
Called every time an element is incorporated into a window.
|
abstract void |
onFire(Trigger.TriggerContext context)
Adjusts the state of the trigger to be ready for the next pane.
|
abstract void |
onMerge(Trigger.OnMergeContext c)
Called immediately after windows have been merged.
|
Trigger |
orFinally(Trigger.OnceTrigger until)
Specify an ending condition for this trigger.
|
void |
prefetchOnElement(org.apache.beam.sdk.util.state.StateAccessor<?> state)
Called to allow the trigger to prefetch any state it will likely need to read from during
an
onElement(org.apache.beam.sdk.transforms.windowing.Trigger.OnElementContext) call. |
void |
prefetchOnFire(org.apache.beam.sdk.util.state.StateAccessor<?> state)
Called to allow the trigger to prefetch any state it will likely need to read from during
an
onFire(org.apache.beam.sdk.transforms.windowing.Trigger.TriggerContext) call. |
void |
prefetchOnMerge(org.apache.beam.sdk.util.state.MergingStateAccessor<?,?> state)
Called to allow the trigger to prefetch any state it will likely need to read from during
an
onMerge(org.apache.beam.sdk.transforms.windowing.Trigger.OnMergeContext) call. |
void |
prefetchShouldFire(org.apache.beam.sdk.util.state.StateAccessor<?> state)
Called to allow the trigger to prefetch any state it will likely need to read from during
an
shouldFire(org.apache.beam.sdk.transforms.windowing.Trigger.TriggerContext) call. |
abstract boolean |
shouldFire(Trigger.TriggerContext context)
Returns
true if the current state of the trigger indicates that its condition
is satisfied and it is ready to fire. |
Iterable<Trigger> |
subTriggers() |
String |
toString() |
public abstract void onElement(Trigger.OnElementContext c) throws Exception
Exceptionpublic abstract void onMerge(Trigger.OnMergeContext c) throws Exception
Leaf triggers should update their state by inspecting their status and any state
in the merging windows. Composite triggers should update their state by calling
ExecutableTrigger.invokeOnMerge(org.apache.beam.sdk.transforms.windowing.Trigger.OnMergeContext) on their sub-triggers, and applying appropriate logic.
A trigger such as AfterWatermark.pastEndOfWindow() may no longer be finished;
it is the responsibility of the trigger itself to record this fact. It is forbidden for
a trigger to become finished due to onMerge(org.apache.beam.sdk.transforms.windowing.Trigger.OnMergeContext), as it has not yet fired the pending
elements that led to it being ready to fire.
The implementation does not need to clear out any state associated with the old windows.
Exceptionpublic abstract boolean shouldFire(Trigger.TriggerContext context) throws Exception
true if the current state of the trigger indicates that its condition
is satisfied and it is ready to fire.Exceptionpublic abstract void onFire(Trigger.TriggerContext context) throws Exception
Repeatedly trigger will reset its inner trigger, since it has fired.
If the trigger is finished, it is the responsibility of the trigger itself to
record that fact via the context.
Exceptionpublic void prefetchOnElement(org.apache.beam.sdk.util.state.StateAccessor<?> state)
onElement(org.apache.beam.sdk.transforms.windowing.Trigger.OnElementContext) call.public void prefetchOnMerge(org.apache.beam.sdk.util.state.MergingStateAccessor<?,?> state)
onMerge(org.apache.beam.sdk.transforms.windowing.Trigger.OnMergeContext) call.public void prefetchShouldFire(org.apache.beam.sdk.util.state.StateAccessor<?> state)
shouldFire(org.apache.beam.sdk.transforms.windowing.Trigger.TriggerContext) call.public void prefetchOnFire(org.apache.beam.sdk.util.state.StateAccessor<?> state)
onFire(org.apache.beam.sdk.transforms.windowing.Trigger.TriggerContext) call.public void clear(Trigger.TriggerContext c) throws Exception
This is called after a trigger has indicated it will never fire again. The trigger system keeps enough information to know that the trigger is finished, so this trigger should clear all of its state.
Exceptionpublic Trigger getContinuationTrigger()
GroupByKey to preserve the
intention of this trigger. Specifically, triggers that are time based
and intended to provide speculative results should continue providing
speculative results. Triggers that fire once (or multiple times) should
continue firing once (or multiple times).protected abstract Trigger getContinuationTrigger(List<Trigger> continuationTriggers)
getContinuationTrigger() of this Trigger. For convenience, this
is provided the continuation trigger of each of the sub-triggers.public abstract Instant getWatermarkThatGuaranteesFiring(BoundedWindow window)
For triggers that do not fire based on the watermark advancing, returns
BoundedWindow.TIMESTAMP_MAX_VALUE.
This estimate is used to determine that there are no elements in a side-input window, which causes the default value to be used instead.
public boolean isCompatible(Trigger other)
Trigger.public Trigger orFinally(Trigger.OnceTrigger until)
until fires then the combination
fires.
The expression t1.orFinally(t2) fires every time t1 fires, and finishes
as soon as either t1 finishes or t2 fires, in which case it fires one last time
for t2. Both t1 and t2 are executed in parallel. This means that
t1 may have fired since t2 started, so not all of the elements that t2
has seen are necessarily in the current pane.
For example the final firing of the following trigger may only have 1 element:
Repeatedly.forever(AfterPane.elementCountAtLeast(2))
.orFinally(AfterPane.elementCountAtLeast(5))
Note that if t1 is Trigger.OnceTrigger, then t1.orFinally(t2) is the same
as AfterFirst.of(t1, t2).