title: Flink: Window does not process data at end of stream
body: <p>I have a stream (kafka msgs being streaming onto a topic) with a flink kafka consumer and I notice an interesting behavior that I'm looking to address.</p>
  
  <p>When the data is being streaming in, if it stops before a window is 'complete', or if the data ends (after a few windows) and does not reach the end of the window, the rest of the pipeline does not trigger.</p>
  
  <p>Example flow:</p>
  
  <pre><code>    env.addSource(kafkaConsumer)
         .flatMap(new TokenMapper())
         .keyBy("word")
         .window(TumblingEventTimeWindows.of(Time.seconds(10L)))
         .reduce(new CountTokens())
         .flatMap(new ConvertToString())
         .addSink(producer);
  </code></pre>
  
  <p>I am using the FlinkKafkaConsumer010 with the env TimeCharacteristic set to EventTime. and consumer.assignTimestampsAndWatermarks(new PeriodicWatermarks())</p>
  
  <pre><code>private static class PeriodicWatermarks implements   AssignerWithPeriodicWatermarks&lt;String&gt;{
  
      private long currentMaxTimestamp;
      private final long maxOutOfOrderness;
  
      public PeriodicWatermarksAuto(long maxOutOfOrderness){
          this.maxOutOfOrderness = maxOutOfOrderness;
      }
  
      @Override
      public Watermark getCurrentWatermark() {
           return new Watermark(currentMaxTimestamp - maxOutOfOrderness);
      }
  
      @Override
      public long extractTimestamp(String t, long l) {
          // this should be the event timestamp
          currentMaxTimestamp = l;
          logger.info("TIMESTAMP: " + l);
          return l;
      }
  }
  </code></pre>
  
  <p>If my window is say 10 seconds, and my data stream only contains 8 seconds of data (and then stops streaming for some period of time), the flatMap->sink does not process until the new later data is streamed in. </p>
  
  <p>Example data stream processing issue: (each x is a piece of data per second
  )</p>
  
  <pre><code>      xxxxxxxx(8secs)------(gap)--(later more data)xxxxx
        ^(not processed)           (until I get here)^
  </code></pre>
  
  <p>Similarly if for example I had 35 seconds worth of streaming data (and again my window is 10 seconds) only 3 windows worth of data trigger, and the remaining 5 seconds worth of data never processes. </p>
  
  <pre><code>     ...xxxxxxxxxx(10secs)xxxxx(5secods)------(gap)--(later more data)xxxxx
           (processed)        ^(not processed)          (until I get here)^
  </code></pre>
  
  <p>finally if my window is 10 seconds and I <em>only</em> have 5 seconds of streamed data the flatmap->sink never happens.</p>
  
  <p>My question is, <strong>is there a way to trigger the windowed data to process if we don't see data after some time?</strong> </p>
  
  <p>If my data was being live-streamed in, I can see there being stretches of no-data, and would not want the last window (of lets say just 5 seconds worth of data) to have to wait some undetermined time until new data comes in, I would want the results for that last window after the window time has passed.</p>
  
  <p>Thinking out loud, this seems to be due to using EventTime instead of ProcessingTime, or, my watermark is not being generated properly for the last window to actually trigger... not sure maybe a bit of both? I would think this would be an issue for anyone, if your stream ends the last bit does not trigger. I would say I could probably send an end-of-stream msg but this does not help if the steam ends because the source breaks up-stream.</p>
  
  <p>EDIT: So I changed to processing time and it does correctly process the data in the last window, so I guess EventTime is the culprit afterall, I'm thinking a custom trigger or proper window watermark might be the answer...</p>
  
  <p>Thanks for the help!</p>