@@ -141,8 +142,67 @@ to use timestamp assignment and watermark generation in the Flink DataStream API
# Event Time and Watermarks
*Note: For a deep introduction to Event Time, please refer also to the paper on the [Dataflow Model](https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/43864.pdf)*
*Note: Flink implements many techniques from the Dataflow Model. For a good introduction to Event Time and, have also a look at these articles*
-[Streaming 101](https://www.oreilly.com/ideas/the-world-beyond-batch-streaming-101) by Tyler Akidau
- The [Dataflow Model paper](https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/43864.pdf)
A stream processor that supports *event time* needs a way to measure the progress of event time.
For example, a window operator that builds hourly windows needs to be notified when event time has reached the
next full hour, such that the operator can close the next window.
*Event Time* can progress independently of *Processing Time* (measures by wall clocks).
For example, in one program, the current *event time* of an operator can trail slightly behind the processing time
(accounting for a delay in receiving the latest elements) and both proceed at the same speed. In another streaming
program, which reads fast-forward through some data already buffered in a Kafka topic (or another message queue), event time
can progress by weeks in seconds.
------
The mechanism in Flink to measure progress in event time are **Watermarks**.
Watermarks flow as part of the data stream and carry a timestamp *t*. A *Watermark(t)* declares that event time has reached time
*t* in that stream, meaning that all events with a timestamps *t' < t* have occurred.
The figure below shows a stream of events with (logical) timestamps, and watermarks flowing inline. The events are in order
(with respect to their timestamp), meaning that watermarks are simply periodic markers in the stream with an in-order timestamp.
<imgsrc="fig/stream_watermark_in_order.svg"alt="A data stream with events (in order) and watermarks"class="center"width="65%"/>
Watermarks are crucial for *out-of-order* streams, as shown in the figure below, where, events do not occur ordered by their timestamp.
Watermarks establish points in the stream where all events up to a certain timestamp have occurred. Once these watermarks reach an
operator, the operator can advance its internal *event time clock* to the value of the watermark.
<imgsrc="fig/stream_watermark_out_of_order.svg"alt="A data stream with events (out of order) and watermarks"class="center"width="65%"/>
## Watermarks in Parallel Streams
Watermarks are generated at source functions, or directly after source functions. Each parallel subtask of a source function usually
generates its watermarks independently. These watermarks define the event time at that particular parallel source.
As the watermarks flow through the streaming program, they advance the event time at the operators where they arrive. Whenever an
operator advances its event time, it generates a new watermark downstream for its successor operators.
Operators that consume multiple input streams (e.g., after a *keyBy(...)* or *partition(...)* function, or a union) track the event time
on each of their input streams. The operator's current event time is the minimum of the input streams' event time. As the input streams
update their event time, so does the operator.
The figure below shows an example of events and watermarks flowing through parallel streams, and operators tracking event time.
<imgsrc="fig/parallel_streams_watermarks.svg"alt="Parallel data streams and operators with events and watermarks"class="center"width="80%"/>
## Late Elements
It is possible that certain elements violate the watermark condition, meaning that even after the *Watermark(t)* has occurred,
more elements with timestamp *t' < t* will occur. In fact, in many real world setups, certain elements can be arbitrarily
delayed, which it is impossible to define a time when all elements of a certain event timestamp have occurred.
Further more, even if the lateness can be bounded, delaying the watermarks by too much is often not desirable, because it delays
the evaluation of the event time windows by too much.
Due to that, some streaming programs will explicitly expect a number of *late* elements. Late elements are elements that
arrive after the system's event time clock (as signaled by the watermarks) has already passed the time of the late element's
d="m 159.32023,167.68379 c 0,-1.67834 1.36892,-3.04726 3.04726,-3.04726 l 12.18905,0 c 1.68771,0 3.04726,1.36892 3.04726,3.04726 l 0,12.18905 c 0,1.68771 -1.35955,3.04726 -3.04726,3.04726 l -12.18905,0 c -1.67834,0 -3.04726,-1.35955 -3.04726,-3.04726 z"
d="m 159.32023,302.70094 c 0,-1.66896 1.36892,-3.03789 3.05664,-3.03789 l 12.18905,0 c 1.68771,0 3.03788,1.36893 3.03788,3.03789 l 0,12.18905 c 0,1.68771 -1.35017,3.05663 -3.03788,3.05663 l -12.18905,0 c -1.68772,0 -3.05664,-1.36892 -3.05664,-3.05663 z"
d="m 302.62593,167.68379 c 0,-1.66896 1.36892,-3.03788 3.05664,-3.03788 l 12.18904,0 c 1.66897,0 3.03789,1.36892 3.03789,3.03788 l 0,12.18905 c 0,1.68771 -1.36892,3.05664 -3.03789,3.05664 l -12.18904,0 c -1.68772,0 -3.05664,-1.36893 -3.05664,-3.05664 z"
d="m 448.57571,176.59117 c 0,-1.66896 1.36892,-3.03788 3.05664,-3.03788 l 12.18905,0 c 1.68771,0 3.03788,1.36892 3.03788,3.03788 l 0,12.18905 c 0,1.68772 -1.35017,3.05664 -3.03788,3.05664 l -12.18905,0 c -1.68772,0 -3.05664,-1.36892 -3.05664,-3.05664 z"
d="m 302.77595,302.70094 c 0,-1.66896 1.36892,-3.03789 3.05664,-3.03789 l 12.18904,0 c 1.68772,0 3.03789,1.36893 3.03789,3.03789 l 0,12.18905 c 0,1.68771 -1.35017,3.05663 -3.03789,3.05663 l -12.18904,0 c -1.68772,0 -3.05664,-1.36892 -3.05664,-3.05663 z"
d="m 454.98903,216.43998 c 0,-1.66896 1.36892,-3.03788 3.05663,-3.03788 l 12.18905,0 c 1.66896,0 3.03789,1.36892 3.03789,3.03788 l 0,12.18905 c 0,1.68772 -1.36893,3.05664 -3.03789,3.05664 l -12.18905,0 c -1.68771,0 -3.05663,-1.36892 -3.05663,-3.05664 z"
d="m 454.98903,334.42997 c 0,-1.68772 1.36892,-3.05664 3.05663,-3.05664 l 12.18905,0 c 1.66896,0 3.03789,1.36892 3.03789,3.05664 l 0,12.18904 c 0,1.68772 -1.36893,3.03789 -3.03789,3.03789 l -12.18905,0 c -1.68771,0 -3.05663,-1.35017 -3.05663,-3.03789 z"
d="m 463.10881,287.54901 c 0,-1.68771 1.36892,-3.05663 3.05663,-3.05663 l 12.18905,0 c 1.68772,0 3.03789,1.36892 3.03789,3.05663 l 0,12.18905 c 0,1.68772 -1.35017,3.03789 -3.03789,3.03789 l -12.18905,0 c -1.68771,0 -3.05663,-1.35017 -3.05663,-3.03789 z"
d="m 509.83974,302.70094 c 0,-1.66896 1.36892,-3.03789 3.05664,-3.03789 l 12.18905,0 c 1.66896,0 3.03788,1.36893 3.03788,3.03789 l 0,12.18905 c 0,1.68771 -1.36892,3.05663 -3.03788,3.05663 l -12.18905,0 c -1.68772,0 -3.05664,-1.36892 -3.05664,-3.05663 z"
d="m 509.83974,167.68379 c 0,-1.66896 1.36892,-3.03788 3.05664,-3.03788 l 12.18905,0 c 1.66896,0 3.03788,1.36892 3.03788,3.03788 l 0,12.18905 c 0,1.68771 -1.36892,3.05664 -3.03788,3.05664 l -12.18905,0 c -1.68772,0 -3.05664,-1.36893 -3.05664,-3.05664 z"
d="m 234.32976,180.73545 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z m 0,3.75048 0,1.87523 -1.87524,0 0,-1.87523 1.87524,0 z m 0,3.75047 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z m 0,3.75048 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z m 0,3.75048 0,1.87523 -1.87524,0 0,-1.87523 1.87524,0 z m 0,3.75047 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z m 0,3.75048 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z m 0,3.75047 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z"
d="m 355.11384,273.31596 1.10639,1.51894 -1.51894,1.10639 -1.10639,-1.51894 1.51894,-1.10639 z m 2.21278,3.03788 1.10639,1.51894 -1.51894,1.1064 -1.10639,-1.51895 1.51894,-1.10639 z m 2.21278,3.01914 1.1064,1.51894 -1.51895,1.10639 -1.10639,-1.51894 1.51894,-1.10639 z m 2.21279,3.03788 1.10639,1.51894 -1.51895,1.10639 -1.10639,-1.51894 1.51895,-1.10639 z m 2.19402,3.03789 1.10639,1.50019 -1.50019,1.10639 -1.10639,-1.51895 1.50019,-1.08763 z m 2.21279,3.01913 1.10639,1.51894 -1.50019,1.10639 -1.1064,-1.51894 1.5002,-1.10639 z m 2.21278,3.03789 0.99387,1.35017 -1.50019,1.10639 -1.01263,-1.35017 1.51895,-1.10639 z"
d="m 374.8226,312.15214 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z m 0,3.75047 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z m 0,3.75048 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z m 0,3.75048 0,1.87523 -1.87524,0 0,-1.87523 1.87524,0 z m 0,3.75047 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z m 0,3.75048 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z m 0,3.75047 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z m 0,3.75048 0,1.87524 -1.87524,0 0,-1.87524 1.87524,0 z"
d="m 414.9902,187.14876 c 0,-1.55644 1.25641,-2.81285 2.81285,-2.81285 l 16.08955,0 c 1.55644,0 2.81285,1.25641 2.81285,2.81285 l 0,11.25143 c 0,1.55645 -1.25641,2.81286 -2.81285,2.81286 l -16.08955,0 c -1.55644,0 -2.81285,-1.25641 -2.81285,-2.81286 z"
d="m 414.9902,187.14876 c 0,-1.55644 1.25641,-2.81285 2.81285,-2.81285 l 16.08955,0 c 1.55644,0 2.81285,1.25641 2.81285,2.81285 l 0,11.25143 c 0,1.55645 -1.25641,2.81286 -2.81285,2.81286 l -16.08955,0 c -1.55644,0 -2.81285,-1.25641 -2.81285,-2.81286 z"
d="m 359.82069,187.14876 c 0,-1.55644 1.25641,-2.81285 2.81286,-2.81285 l 16.08954,0 c 1.55645,0 2.81286,1.25641 2.81286,2.81285 l 0,11.25143 c 0,1.55645 -1.25641,2.81286 -2.81286,2.81286 l -16.08954,0 c -1.55645,0 -2.81286,-1.25641 -2.81286,-2.81286 z"
d="m 359.82069,187.14876 c 0,-1.55644 1.25641,-2.81285 2.81286,-2.81285 l 16.08954,0 c 1.55645,0 2.81286,1.25641 2.81286,2.81285 l 0,11.25143 c 0,1.55645 -1.25641,2.81286 -2.81286,2.81286 l -16.08954,0 c -1.55645,0 -2.81286,-1.25641 -2.81286,-2.81286 z"
d="m 334.03617,219.02781 c 0,-1.55645 1.25641,-2.81285 2.81285,-2.81285 l 16.08955,0 c 1.55644,0 2.81285,1.2564 2.81285,2.81285 l 0,11.25143 c 0,1.55645 -1.25641,2.81286 -2.81285,2.81286 l -16.08955,0 c -1.55644,0 -2.81285,-1.25641 -2.81285,-2.81286 z"
d="m 334.03617,219.02781 c 0,-1.55645 1.25641,-2.81285 2.81285,-2.81285 l 16.08955,0 c 1.55644,0 2.81285,1.2564 2.81285,2.81285 l 0,11.25143 c 0,1.55645 -1.25641,2.81286 -2.81285,2.81286 l -16.08955,0 c -1.55644,0 -2.81285,-1.25641 -2.81285,-2.81286 z"
d="m 402.48236,241.3619 c 0,-1.5377 1.25641,-2.81286 2.81285,-2.81286 l 16.08955,0 c 1.55645,0 2.81286,1.27516 2.81286,2.81286 l 0,11.25143 c 0,1.55644 -1.25641,2.81285 -2.81286,2.81285 l -16.08955,0 c -1.55644,0 -2.81285,-1.25641 -2.81285,-2.81285 z"
d="m 402.48236,241.3619 c 0,-1.5377 1.25641,-2.81286 2.81285,-2.81286 l 16.08955,0 c 1.55645,0 2.81286,1.27516 2.81286,2.81286 l 0,11.25143 c 0,1.55644 -1.25641,2.81285 -2.81286,2.81285 l -16.08955,0 c -1.55644,0 -2.81285,-1.25641 -2.81285,-2.81285 z"
d="m 432.01736,286.06758 c 0,-1.55645 1.25641,-2.81286 2.81286,-2.81286 l 16.08954,0 c 1.55645,0 2.81286,1.25641 2.81286,2.81286 l 0,11.25142 c 0,1.55645 -1.25641,2.81286 -2.81286,2.81286 l -16.08954,0 c -1.55645,0 -2.81286,-1.25641 -2.81286,-2.81286 z"
d="m 432.01736,286.06758 c 0,-1.55645 1.25641,-2.81286 2.81286,-2.81286 l 16.08954,0 c 1.55645,0 2.81286,1.25641 2.81286,2.81286 l 0,11.25142 c 0,1.55645 -1.25641,2.81286 -2.81286,2.81286 l -16.08954,0 c -1.55645,0 -2.81286,-1.25641 -2.81286,-2.81286 z"
d="m 391.69974,321.37831 c 0,-1.55645 1.25641,-2.81286 2.81286,-2.81286 l 16.08954,0 c 1.55645,0 2.81286,1.25641 2.81286,2.81286 l 0,11.25143 c 0,1.55645 -1.25641,2.81286 -2.81286,2.81286 l -16.08954,0 c -1.55645,0 -2.81286,-1.25641 -2.81286,-2.81286 z"
d="m 391.69974,321.37831 c 0,-1.55645 1.25641,-2.81286 2.81286,-2.81286 l 16.08954,0 c 1.55645,0 2.81286,1.25641 2.81286,2.81286 l 0,11.25143 c 0,1.55645 -1.25641,2.81286 -2.81286,2.81286 l -16.08954,0 c -1.55645,0 -2.81286,-1.25641 -2.81286,-2.81286 z"
d="m 325.74761,321.37831 c 0,-1.55645 1.27517,-2.81286 2.81286,-2.81286 l 16.1083,0 c 1.55644,0 2.81285,1.25641 2.81285,2.81286 l 0,11.25143 c 0,1.55645 -1.25641,2.81286 -2.81285,2.81286 l -16.1083,0 c -1.53769,0 -2.81286,-1.25641 -2.81286,-2.81286 z"
d="m 325.74761,321.37831 c 0,-1.55645 1.27517,-2.81286 2.81286,-2.81286 l 16.1083,0 c 1.55644,0 2.81285,1.25641 2.81285,2.81286 l 0,11.25143 c 0,1.55645 -1.25641,2.81286 -2.81285,2.81286 l -16.1083,0 c -1.53769,0 -2.81286,-1.25641 -2.81286,-2.81286 z"
d="m 204.02591,321.37831 c 0,-1.55645 1.25641,-2.81286 2.81285,-2.81286 l 16.08955,0 c 1.55645,0 2.81285,1.25641 2.81285,2.81286 l 0,11.25143 c 0,1.55645 -1.2564,2.81286 -2.81285,2.81286 l -16.08955,0 c -1.55644,0 -2.81285,-1.25641 -2.81285,-2.81286 z"
d="m 204.02591,321.37831 c 0,-1.55645 1.25641,-2.81286 2.81285,-2.81286 l 16.08955,0 c 1.55645,0 2.81285,1.25641 2.81285,2.81286 l 0,11.25143 c 0,1.55645 -1.2564,2.81286 -2.81285,2.81286 l -16.08955,0 c -1.55644,0 -2.81285,-1.25641 -2.81285,-2.81286 z"
d="m 189.79285,187.13939 c 0,-1.55645 1.26579,-2.81286 2.81286,-2.81286 l 16.09892,0 c 1.55644,0 2.81285,1.25641 2.81285,2.81286 l 0,11.25143 c 0,1.55644 -1.25641,2.81285 -2.81285,2.81285 l -16.09892,0 c -1.54707,0 -2.81286,-1.25641 -2.81286,-2.81285 z"
d="m 189.79285,187.13939 c 0,-1.55645 1.26579,-2.81286 2.81286,-2.81286 l 16.09892,0 c 1.55644,0 2.81285,1.25641 2.81285,2.81286 l 0,11.25143 c 0,1.55644 -1.25641,2.81285 -2.81285,2.81285 l -16.09892,0 c -1.54707,0 -2.81286,-1.25641 -2.81286,-2.81285 z"
style="font-size:11.2514286px;font-style:italic;font-weight:normal;text-align:start;text-anchor:start;fill:#000000;font-family:Verdana">at the operator</text>
d="m 371.54093,44.630667 0,7.500953 -1.87524,0 0,-7.500953 1.87524,0 z m 0,13.126667 0,7.500953 -1.87524,0 0,-7.500953 1.87524,0 z m 0,13.126667 0,7.500952 -1.87524,0 0,-7.500952 1.87524,0 z m 0,13.126667 0,2.662838 -1.87524,0 0,-2.662838 1.87524,0 z"
d="m 161.98307,44.621291 0,7.500953 -1.87524,0 0,-7.500953 1.87524,0 z m 0,13.126667 0,7.500952 -1.87524,0 0,-7.500952 1.87524,0 z m 0,13.126667 0,7.500952 -1.87524,0 0,-7.500952 1.87524,0 z m 0,13.126667 0,2.662838 -1.87524,0 0,-2.662838 1.87524,0 z"
style="font-size:13.80175209px;font-style:italic;font-weight:normal;text-align:start;text-anchor:start;fill:#000000;font-family:Verdana">(out of order)</text>
<path
d="m 371.54093,395.76901 0,7.50095 -1.87524,0 0,-7.50095 1.87524,0 z m 0,13.12666 0,7.50095 -1.87524,0 0,-7.50095 1.87524,0 z m 0,13.12667 0,7.50095 -1.87524,0 0,-7.50095 1.87524,0 z m 0,13.12667 0,2.66283 -1.87524,0 0,-2.66283 1.87524,0 z"
d="m 161.98307,395.76901 0,7.50095 -1.87524,0 0,-7.50095 1.87524,0 z m 0,13.12666 0,7.50095 -1.87524,0 0,-7.50095 1.87524,0 z m 0,13.12667 0,7.50095 -1.87524,0 0,-7.50095 1.87524,0 z m 0,13.12667 0,2.66283 -1.87524,0 0,-2.66283 1.87524,0 z"