spena commented on a change in pull request #10462: URL: https://github.com/apache/kafka/pull/10462#discussion_r614303123
########## File path: streams/src/main/java/org/apache/kafka/streams/kstream/internals/KStreamKStreamJoin.java ########## @@ -93,23 +136,118 @@ public void process(final K key, final V1 value) { } boolean needOuterJoin = outer; + boolean joinFound = false; final long inputRecordTimestamp = context().timestamp(); final long timeFrom = Math.max(0L, inputRecordTimestamp - joinBeforeMs); final long timeTo = Math.max(0L, inputRecordTimestamp + joinAfterMs); + maxObservedStreamTime.advance(inputRecordTimestamp); + try (final WindowStoreIterator<V2> iter = otherWindow.fetch(key, timeFrom, timeTo)) { while (iter.hasNext()) { needOuterJoin = false; + joinFound = true; final KeyValue<Long, V2> otherRecord = iter.next(); + final long otherRecordTimestamp = otherRecord.key; + + // Emit expired records before the joined record to keep time ordering + emitExpiredNonJoinedOuterRecordsExcept(key, otherRecordTimestamp); + context().forward( key, joiner.apply(key, value, otherRecord.value), - To.all().withTimestamp(Math.max(inputRecordTimestamp, otherRecord.key))); + To.all().withTimestamp(Math.max(inputRecordTimestamp, otherRecordTimestamp))); + } + + // Emit all expired records before adding a new non-joined record to the store. Otherwise, + // the put() call will advance the stream time, which causes records out of the retention + // period to be deleted, thus not being emitted later. + if (!joinFound && inputRecordTimestamp == maxObservedStreamTime.get()) { + emitExpiredNonJoinedOuterRecords(); } if (needOuterJoin) { - context().forward(key, joiner.apply(key, value, null)); + // The maxStreamTime contains the max time observed in both sides of the join. + // Having access to the time observed in the other join side fixes the following + // problem: + // + // Say we have a window size of 5 seconds + // 1. A non-joined record wth time T10 is seen in the left-topic (maxLeftStreamTime: 10) + // The record is not processed yet, and is added to the outer-join store + // 2. A non-joined record with time T2 is seen in the right-topic (maxRightStreamTime: 2) + // The record is not processed yet, and is added to the outer-join store + // 3. A joined record with time T11 is seen in the left-topic (maxLeftStreamTime: 11) + // It is time to look at the expired records. T10 and T2 should be emitted, but + // because T2 was late, then it is not fetched by the window store, so it is not processed + // + // See KStreamKStreamLeftJoinTest.testLowerWindowBound() tests + // + // the condition below allows us to process the late record without the need + // to hold it in the temporary outer store + if (!outerJoinWindowStore.isPresent() || timeTo < maxObservedStreamTime.get()) { + context().forward(key, joiner.apply(key, value, null)); + } else { + outerJoinWindowStore.ifPresent(store -> store.put( + KeyAndJoinSide.make(isLeftJoin, key), + LeftOrRightValue.make(isLeftJoin, value), + inputRecordTimestamp)); + } + } + } + } + + private void emitExpiredNonJoinedOuterRecords() { + outerJoinWindowStore.ifPresent(store -> + emitExpiredNonJoinedOuterRecords(store, recordWindowHasClosed)); + } + + private void emitExpiredNonJoinedOuterRecordsExcept(final K key, final long timestamp) { + outerJoinWindowStore.ifPresent(store -> { + final KeyAndJoinSide<K> keyAndJoinSide = KeyAndJoinSide.make(!isLeftJoin, key); + + // Emit all expired records except the just found non-joined key. We need + // to emit all expired records before calling put(), otherwise the internal + // stream time will advance and may cause records out of the retention period to + // be deleted. + emitExpiredNonJoinedOuterRecords(store, + recordWindowHasClosed + .and(k -> !k.key().equals(keyAndJoinSide)) + .and(k -> k.window().start() != timestamp)); + + if (store.fetch(keyAndJoinSide, timestamp) != null) { + // Delete the record. The previous emit call may not have removed this record + // if the record window has not closed. + store.put(keyAndJoinSide, null, timestamp); + } + }); + } + + @SuppressWarnings("unchecked") + private void emitExpiredNonJoinedOuterRecords(final WindowStore<KeyAndJoinSide<K>, LeftOrRightValue> store, final Predicate<Windowed<KeyAndJoinSide<K>>> emitCondition) { + try (final KeyValueIterator<Windowed<KeyAndJoinSide<K>>, LeftOrRightValue> it = store.all()) { + while (it.hasNext()) { + final KeyValue<Windowed<KeyAndJoinSide<K>>, LeftOrRightValue> e = it.next(); + + // Skip next records if the emit condition is false + if (!emitCondition.test(e.key)) { + break; + } + + final K key = e.key.key().getKey(); + final To timestamp = To.all().withTimestamp(e.key.window().start()); + + final R nullJoinedValue; + if (isLeftJoin) { Review comment: Yes. The `ReverseJoiner` does not work well in the shared store scenario. I need cast the value and otherValue based on what join side we are. For instance, I would get an error if I remove the `(V1)` and `(V2)` casts. When using the casts, now I need to specify which value is the right one. In the left-side is `(V1) record.value.getLeftValue()`, in the right-side is `(V2) record.value.getLeftValue()`. ``` nullJoinedValue = joiner.apply(key, (V1) record.value.getLeftValue(), (V2) record.value.getRightValue()); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org