title: kafka streams hopping windowed aggregation causing multiple windows at timestamp zero
body: <p>Kafka Streams DSL windowed aggregation causing multiple windows.</p>
  
  <pre><code>@StreamListener("input")
      public void process(KStream&lt;String, Data&gt; DataKStream) {
  
          JsonSerde&lt;DataAggregator&gt; DataJsonSerde =
                  new JsonSerde&lt;&gt;(DataAggregator.class);
  
          DataKStream
                  .groupByKey()
                  .windowedBy(TimeWindows.of(60000).advanceBy(30000))
                  .aggregate(
                          DataAggregator::new,
                          (key, Data, aggregator) -&gt; aggregator.add(Data),
                          Materialized.with(Serdes.String(), DataJsonSerde)
                  );
      }
  </code></pre>
  
  <p><strong>DataAggregator.java</strong></p>
  
  <pre><code>public class DataAggregator {
  
      private List&lt;String&gt; dataList = new ArrayList&lt;&gt;();
  
      public DataAggregator add(Data data) {
          dataList.add(data.getId());
          System.out.println(dataList);
          return this;
      }
  
      public List&lt;String&gt; getDataList() {
          return dataList;
      }
  }
  </code></pre>
  
  <p>I am grouping input data based on key, then doing 1 minute window with 30 seconds hop and in aggregator I'm just collecting data and displaying.</p>
  
  <p>I was expecting 1 window at the beginning and after 30 seconds another window. But the actual output is different since beginning itself 2 windows are creating.</p>
  
  <p><strong>Expected:</strong></p>
  
  <pre><code>[1]
  [1, 2]
  [1, 2, 3]
  [1, 2, 3, 4]
  [1, 2, 3, 4, 5]
  [1, 2, 3, 4, 5, 6] // till 30 seconds only one window
  [6] // new window after 30 seconds
  [1, 2, 3, 4, 5, 6, 7]
  [6, 7]
  [1, 2, 3, 4, 5, 6, 7, 8]
  [6, 7, 8]
  </code></pre>
  
  <p><strong>Actual output:</strong></p>
  
  <pre><code>[1]
  [1]
  [1, 2]
  [1, 2]
  [1, 2, 3]
  [1, 2, 3]
  [1, 2, 3, 4]
  [1, 2, 3, 4]
  [1, 2, 3, 4, 5]
  [1, 2, 3, 4, 5]
  [1, 2, 3, 4, 5, 6]
  [1, 2, 3, 4, 5, 6] // duplicate window even before 30 seconds
  [6] // new window after 30 seconds and 1 window from earlier will be dropped
  [1, 2, 3, 4, 5, 6, 7]
  [6, 7]
  </code></pre>
  
  <p>Since I'm creating hoping window of 30 seconds in a 1 minute window. I believe, initially there should be only one window and after 30 seconds another window should create.</p>
  
  <p>Can someone please let me know, is the actual output is expected behavior or I am missing something?</p>
  
  <p><strong>NOTE:</strong> I am getting input data every 4 seconds and expected/actual output is just for representation.</p>
  
  <p><strong>From Kafka Documentation:</strong></p>
  
  <blockquote>
    <p>Hopping time windows are aligned to the epoch, with the lower interval
    bound being inclusive and the upper bound being exclusive. “Aligned to
    the epoch” means that the first window starts at timestamp zero. For
    example, hopping windows with a size of 5000ms and an advance interval
    (“hop”) of 3000ms have predictable window boundaries
    [0;5000),[3000;8000),... — and not [1000;6000),[4000;9000),... or even
    something “random” like [1452;6452),[4452;9452),....</p>
  </blockquote>