public class WindowManager<T> extends Object implements TriggerHandler
Tracks a window of events and fires WindowLifecycleListener
callbacks on expiry of events or activation of the window due to TriggerPolicy
.
Modifier and Type | Field and Description |
---|---|
protected EvictionPolicy<T,?> |
evictionPolicy |
static int |
EXPIRE_EVENTS_THRESHOLD
Expire old events every EXPIRE_EVENTS_THRESHOLD to keep the window size in check.
|
protected Collection<Event<T>> |
queue |
protected TriggerPolicy<T,?> |
triggerPolicy |
protected WindowLifecycleListener<T> |
windowLifecycleListener |
Constructor and Description |
---|
WindowManager(WindowLifecycleListener<T> lifecycleListener) |
WindowManager(WindowLifecycleListener<T> lifecycleListener,
Collection<Event<T>> queue)
Constructs a
WindowManager . |
Modifier and Type | Method and Description |
---|---|
void |
add(Event<T> windowEvent)
Tracks a window event.
|
void |
add(T event)
Add an event into the window, with
System.currentTimeMillis() as the tracking ts. |
void |
add(T event,
long ts)
Add an event into the window, with the given ts as the tracking ts.
|
protected void |
compactWindow()
expires events that fall out of the window every EXPIRE_EVENTS_THRESHOLD so that the window does not grow too big.
|
long |
getEarliestEventTs(long startTs,
long endTs)
Scans the event queue and returns the next earliest event ts between the startTs and endTs.
|
int |
getEventCount(long referenceTime)
Scans the event queue and returns number of events having timestamp less than or equal to the reference time.
|
List<Long> |
getSlidingCountTimestamps(long startTs,
long endTs,
int slidingCount)
Scans the event queue and returns the list of event ts falling between startTs (exclusive) and endTs (inclusive) at each sliding interval counts.
|
Map<String,Optional<?>> |
getState() |
boolean |
onTrigger()
The callback invoked by the trigger policy.
|
void |
restoreState(Map<String,Optional<?>> state) |
void |
setEvictionPolicy(EvictionPolicy<T,?> evictionPolicy) |
void |
setTriggerPolicy(TriggerPolicy<T,?> triggerPolicy) |
void |
shutdown() |
String |
toString() |
public static final int EXPIRE_EVENTS_THRESHOLD
Expire old events every EXPIRE_EVENTS_THRESHOLD to keep the window size in check.
Note that if the eviction policy is based on watermarks, events will not be evicted until a new watermark would cause them to be considered expired anyway, regardless of this limit
protected final Collection<Event<T>> queue
protected final WindowLifecycleListener<T> windowLifecycleListener
protected EvictionPolicy<T,?> evictionPolicy
protected TriggerPolicy<T,?> triggerPolicy
public WindowManager(WindowLifecycleListener<T> lifecycleListener)
public WindowManager(WindowLifecycleListener<T> lifecycleListener, Collection<Event<T>> queue)
Constructs a WindowManager
.
lifecycleListener
- the WindowLifecycleListener
queue
- a collection where the events in the window can be enqueued. public void setEvictionPolicy(EvictionPolicy<T,?> evictionPolicy)
public void setTriggerPolicy(TriggerPolicy<T,?> triggerPolicy)
public void add(T event)
Add an event into the window, with System.currentTimeMillis()
as the tracking ts.
event
- the event to addpublic void add(T event, long ts)
Add an event into the window, with the given ts as the tracking ts.
event
- the event to trackts
- the timestamppublic void add(Event<T> windowEvent)
Tracks a window event.
windowEvent
- the window event to trackpublic boolean onTrigger()
The callback invoked by the trigger policy.
onTrigger
in interface TriggerHandler
public void shutdown()
protected void compactWindow()
expires events that fall out of the window every EXPIRE_EVENTS_THRESHOLD so that the window does not grow too big.
public long getEarliestEventTs(long startTs, long endTs)
Scans the event queue and returns the next earliest event ts between the startTs and endTs.
startTs
- the start ts (exclusive)endTs
- the end ts (inclusive)public int getEventCount(long referenceTime)
Scans the event queue and returns number of events having timestamp less than or equal to the reference time.
referenceTime
- the reference timestamp in millispublic List<Long> getSlidingCountTimestamps(long startTs, long endTs, int slidingCount)
Scans the event queue and returns the list of event ts falling between startTs (exclusive) and endTs (inclusive) at each sliding interval counts.
startTs
- the start timestamp (exclusive)endTs
- the end timestamp (inclusive)slidingCount
- the sliding interval countCopyright © 2019 The Apache Software Foundation. All rights reserved.