Class SlidingWindow<E,R,Q>
- java.lang.Object
-
- org.pipecraft.infra.monitoring.sliding.SlidingWindow<E,R,Q>
-
- Type Parameters:
E- The type of an event to be reportedR- The type of the record stored per time slot. Must be thread safeQ- The type of query results
- Direct Known Subclasses:
AverageSlidingWindow,EventRateSlidingWindow
public abstract class SlidingWindow<E,R,Q> extends Object
An abstract sliding window. A sliding window object is notified with events as they occur, and supports querying the recently collected data. The data aggregation and the query semantics are implementation dependent.- Author:
- Eyal Schneider
-
-
Constructor Summary
Constructors Constructor Description SlidingWindow(int slotsCount, int slotTime, TimeUnit slotTimeUnit, ScheduledExecutorService ex)Constructor
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voidclearRecord(R record)clears a given record by resetting its data.protected RgetRecord(int pos)intgetSlotsCount()intgetSlotTime()TimeUnitgetSlotTimeUnit()longgetWindowTimeSpanMs()voidnewEvent(E event)Reports a new eventprotected abstract voidnewEvent(E event, R rec)Reports a new event to be appended into a given time slot recordprotected abstract RnewRecord()protected abstract R[]newRecordArray(int size)Qquery()Qquery(int lookback)protected abstract Qquery(R[] records)voidshutdown()Terminates the sliding window.
-
-
-
Constructor Detail
-
SlidingWindow
public SlidingWindow(int slotsCount, int slotTime, TimeUnit slotTimeUnit, ScheduledExecutorService ex)Constructor- Parameters:
slotsCount- The number of time slots in the sliding windowslotTime- The time duration represented by one slotslotTimeUnit- The time unit of slotTimeex- The executor to run the time-pulse task on.
-
-
Method Detail
-
newEvent
public void newEvent(E event)
Reports a new event- Parameters:
event- The event
-
newEvent
protected abstract void newEvent(E event, R rec)
Reports a new event to be appended into a given time slot record- Parameters:
event- The eventrec- The record to be updated
-
newRecord
protected abstract R newRecord()
- Returns:
- A new, empty record. Called only during the construction of the sliding window.
-
newRecordArray
protected abstract R[] newRecordArray(int size)
- Parameters:
size- The required size- Returns:
- A new, empty record array of the given size.
-
clearRecord
protected abstract void clearRecord(R record)
clears a given record by resetting its data. It is up to the implementation to decide how to deal with data consistency in case of a concurrent query that inspects that record. It is highly unlikely that an event is registered in the record during this operation.
-
query
public Q query()
- Returns:
- The query result, relevant for the complete time period covered by the sliding window.
-
query
public Q query(int lookback)
- Parameters:
lookback- The number of time slots to look back on (must be between 1 and the number of slots defined in the constructor)- Returns:
- The query result, relevant for the time period covered by the given lookback.
-
query
protected abstract Q query(R[] records)
- Parameters:
records- the array of records to inspect. Ordered from oldest to newest.- Returns:
- The query result, relevant for the time period covered by the given lookback.
-
shutdown
public void shutdown()
Terminates the sliding window. Note: subsequent queries may return undefined results!
-
getSlotTime
public int getSlotTime()
- Returns:
- The time duration represented by a single slot
-
getSlotTimeUnit
public TimeUnit getSlotTimeUnit()
- Returns:
- The time unit by which getSlotTime() is measured
-
getWindowTimeSpanMs
public long getWindowTimeSpanMs()
- Returns:
- The total time covered by this sliding window, in milliseconds
-
getSlotsCount
public int getSlotsCount()
- Returns:
- The number of slots in this sliding window
-
getRecord
protected R getRecord(int pos)
- Parameters:
pos- A position in the cyclic array- Returns:
- The record in the given position
-
-