Source:
private TopicRepository selectCorrectTopicRepo( final NakadiCursor cursor, final Consumer<NakadiCursor> cursorReplacer, final Consumer<NakadiCursor> lastTimelinePosition) throws ServiceUnavailableException { final List<Timeline> eventTimelines = eventTypeTimelines.get(cursor.getEventType()); final ListIterator<Timeline> itTimeline = eventTimelines.listIterator(eventTimelines.size()); Timeline electedTimeline = itTimeline.previous(); while (itTimeline.hasPrevious()) { final Timeline toCheck = itTimeline.previous(); final NakadiCursor latest = toCheck.calculateNakadiLatestPosition(cursor.getPartition()); if (latest == null) { electedTimeline = toCheck; } else if (latest.compareTo(cursor) > 0) { final NakadiCursor firstItem = timelineService.getTopicRepository(toCheck) .loadPartitionStatistics(toCheck, cursor.getPartition()) .get().getFirst(); if (latest.compareTo(firstItem) > 0) { electedTimeline = toCheck; } else { LOG.info("Timeline doesn't have any data inside, skipping", toCheck); } } else { break; } } final TopicRepository result = timelineService.getTopicRepository(electedTimeline); if (electedTimeline.getOrder() != cursor.getTimeline().getOrder()) { final NakadiCursor replacement; if (electedTimeline.isFake() || (cursor.getTimeline().isFake() && electedTimeline.isFirstAfterFake())) { replacement = new NakadiCursor(electedTimeline, cursor.getPartition(), cursor.getOffset()); } else { replacement = getBeforeFirstCursor(result, electedTimeline, cursor.getPartition()); } LOG.info("Replacing cursor from to {}", cursor, replacement); cursorReplacer.accept(replacement); } lastTimelinePosition.accept(electedTimeline.calculateNakadiLatestPosition(cursor.getPartition())); return result; }

Target:
private TopicRepository selectCorrectTopicRepo( final NakadiCursor cursor, final Consumer<NakadiCursor> cursorReplacer, final Consumer<NakadiCursor> lastTimelinePosition) throws ServiceUnavailableException { final List<Timeline> eventTimelines = eventTypeTimelines.get(cursor.getEventType()); final ListIterator<Timeline> itTimeline = eventTimelines.listIterator(eventTimelines.size()); Timeline electedTimeline = itTimeline.previous(); while (itTimeline.hasPrevious()) { final Timeline toCheck = itTimeline.previous(); final NakadiCursor latest = toCheck.calculateNakadiLatestPosition(cursor.getPartition()); if (latest == null) { electedTimeline = toCheck; } else if (latest.compareTo(cursor) > 0) { final NakadiCursor firstItem = timelineService.getTopicRepository(toCheck) .loadPartitionStatistics(toCheck, cursor.getPartition()) .get().getFirst(); if (latest.compareTo(firstItem) >= 0) { electedTimeline = toCheck; } else { LOG.info("Timeline is empty, skipping", toCheck); } } else { break; } } final TopicRepository result = timelineService.getTopicRepository(electedTimeline); if (electedTimeline.getOrder() != cursor.getTimeline().getOrder()) { final NakadiCursor replacement; if (electedTimeline.isFake() || (cursor.getTimeline().isFake() && electedTimeline.isFirstAfterFake())) { replacement = new NakadiCursor(electedTimeline, cursor.getPartition(), cursor.getOffset()); } else { replacement = getBeforeFirstCursor(result, electedTimeline, cursor.getPartition()); } LOG.info("Replacing cursor from to {}", cursor, replacement); cursorReplacer.accept(replacement); } lastTimelinePosition.accept(electedTimeline.calculateNakadiLatestPosition(cursor.getPartition())); return result; }