Source:
<code>private void initialize(final RestoringTasks active) { if (!restoreConsumer.subscription().isEmpty()) { throw new StreamsException("Restore consumer should not be subscribed to any topics (" + restoreConsumer.subscription() + ")"); } refreshChangelogInfo(); final Set<TopicPartition> initializable = new HashSet<>(); for (final TopicPartition topicPartition : needsInitializing) { if (hasPartition(topicPartition)) { initializable.add(topicPartition); } } final Map<TopicPartition, Long> queriedEndOffsets; try { queriedEndOffsets = restoreConsumer.endOffsets(initializable); } catch (final TimeoutException e) { log.debug("Could not fetch end offset for {}; will fall back to partition by partition fetching", initializable); return; } queriedEndOffsets.forEach((tp, endOffset) -> { if (endOffset != null) { final StateRestorer restorer = stateRestorers.get(tp); final long offsetLimit = restorer.offsetLimit(); <START> endOffsets.put(tp, Math.min(endOffset, offsetLimit)); <END> } else { log.info("End offset cannot be found form the returned metadata; removing this partition from the current run loop"); initializable.remove(tp); } }); final Iterator<TopicPartition> iter = initializable.iterator(); while (iter.hasNext()) { final TopicPartition topicPartition = iter.next(); final Long endOffset = endOffsets.get(topicPartition); final StateRestorer restorer = stateRestorers.get(topicPartition); if (restorer.checkpoint() >= endOffset) { restorer.setRestoredOffset(restorer.checkpoint()); iter.remove(); completedRestorers.add(topicPartition); } else if (restorer.offsetLimit() == 0 || endOffset == 0) { restorer.setRestoredOffset(0); iter.remove(); completedRestorers.add(topicPartition); } else { restorer.setEndingOffset(endOffset); } needsInitializing.remove(topicPartition); } if (!initializable.isEmpty()) { startRestoration(initializable, active); } }</code><technical_language>endOffsets is for topic-partition end-offsets -- however, new semantics is -- rename restoreOffset (hope find a name) for avoid confusion? queriedEndOffsets endOffsets is accurate aligns other code</technical_language>

Target:
private void initialize(final RestoringTasks active) { if (!restoreConsumer.subscription().isEmpty()) { throw new StreamsException("Restore consumer should not be subscribed to any topics (" + restoreConsumer.subscription() + ")"); } refreshChangelogInfo(); final Set<TopicPartition> initializable = new HashSet<>(); for (final TopicPartition topicPartition : needsInitializing) { if (hasPartition(topicPartition)) { initializable.add(topicPartition); } } final Map<TopicPartition, Long> endOffsets; try { endOffsets = restoreConsumer.endOffsets(initializable); } catch (final TimeoutException e) { log.debug("Could not fetch end offset for {}; will fall back to partition by partition fetching", initializable); return; } endOffsets.forEach((partition, endOffset) -> { if (endOffset != null) { final StateRestorer restorer = stateRestorers.get(partition); final long offsetLimit = restorer.offsetLimit(); restoreToOffsets.put(partition, Math.min(endOffset, offsetLimit)); } else { log.info("End offset cannot be found form the returned metadata; removing this partition from the current run loop"); initializable.remove(partition); } }); final Iterator<TopicPartition> iter = initializable.iterator(); while (iter.hasNext()) { final TopicPartition topicPartition = iter.next(); final Long restoreOffset = restoreToOffsets.get(topicPartition); final StateRestorer restorer = stateRestorers.get(topicPartition); if (restorer.checkpoint() >= restoreOffset) { restorer.setRestoredOffset(restorer.checkpoint()); iter.remove(); completedRestorers.add(topicPartition); } else if (restoreOffset == 0) { restorer.setRestoredOffset(0); iter.remove(); completedRestorers.add(topicPartition); } else { restorer.setEndingOffset(restoreOffset); } needsInitializing.remove(topicPartition); } if (!initializable.isEmpty()) { startRestoration(initializable, active); } }