Source:
<code>private OffsetsMaps fetchTopicPartitionsMetadata(List<TopicPartition> topicPartitions) { Map<SystemStreamPartition, String> oldestOffsets = new HashMap<>(); Map<SystemStreamPartition, String> newestOffsets = new HashMap<>(); Map<SystemStreamPartition, String> upcomingOffsets = new HashMap<>(); Map<TopicPartition, Long> oldestOffsetsWithLong; Map<TopicPartition, Long> upcomingOffsetsWithLong; oldestOffsetsWithLong = threadSafeKafkaConsumer.execute(consumer -> consumer.beginningOffsets(topicPartitions)); LOG.debug("Kafka-fetched beginningOffsets: {}", oldestOffsetsWithLong); <START> upcomingOffsetsWithLong = threadSafeKafkaConsumer.execute(consumer -> consumer.endOffsets(topicPartitions)); <END> LOG.debug("Kafka-fetched endOffsets: {}", upcomingOffsetsWithLong); oldestOffsetsWithLong.forEach((topicPartition, offset) -> oldestOffsets.put(toSystemStreamPartition(topicPartition), String.valueOf(offset))); upcomingOffsetsWithLong.forEach((topicPartition, offset) -> { upcomingOffsets.put(toSystemStreamPartition(topicPartition), String.valueOf(offset)); if (offset <= 0) { LOG.warn( "Empty Kafka topic partition with upcoming offset {}. Skipping newest offset and setting oldest offset to 0 to consume from beginning", topicPartition, offset); oldestOffsets.put(toSystemStreamPartition(topicPartition), "0"); } else { newestOffsets.put(toSystemStreamPartition(topicPartition), String.valueOf(offset - 1)); } }); return new OffsetsMaps(oldestOffsets, newestOffsets, upcomingOffsets); }</code><technical_language>potential side-effects splitting beginningOffsets endOffsets call synchronized block two? reason change scope of synchronized block pass in previous synchronized block part of lambda function</technical_language>

Target:
private OffsetsMaps fetchTopicPartitionsMetadata(List<TopicPartition> topicPartitions) { Map<SystemStreamPartition, String> oldestOffsets = new HashMap<>(); Map<SystemStreamPartition, String> newestOffsets = new HashMap<>(); Map<SystemStreamPartition, String> upcomingOffsets = new HashMap<>(); final Map<TopicPartition, Long> oldestOffsetsWithLong = new HashMap<>(); final Map<TopicPartition, Long> upcomingOffsetsWithLong = new HashMap<>(); threadSafeKafkaConsumer.execute(consumer -> { Map<TopicPartition, Long> beginningOffsets = consumer.beginningOffsets(topicPartitions); LOG.debug("Beginning offsets for topic-partitions: is {}", topicPartitions, beginningOffsets); oldestOffsetsWithLong.putAll(beginningOffsets); Map<TopicPartition, Long> endOffsets = consumer.endOffsets(topicPartitions); LOG.debug("End offsets for topic-partitions: is {}", topicPartitions, endOffsets); upcomingOffsetsWithLong.putAll(endOffsets); return Optional.empty(); }); oldestOffsetsWithLong.forEach((topicPartition, offset) -> oldestOffsets.put(KafkaUtil.toSystemStreamPartition(systemName, topicPartition), String.valueOf(offset))); upcomingOffsetsWithLong.forEach((topicPartition, offset) -> { upcomingOffsets.put(KafkaUtil.toSystemStreamPartition(systemName, topicPartition), String.valueOf(offset)); if (offset <= 0) { LOG.warn( "Empty Kafka topic partition with upcoming offset {}. Skipping newest offset and setting oldest offset to 0 to consume from beginning", topicPartition, offset); oldestOffsets.put(KafkaUtil.toSystemStreamPartition(systemName, topicPartition), "0"); } else { newestOffsets.put(KafkaUtil.toSystemStreamPartition(systemName, topicPartition), String.valueOf(offset - 1)); } }); return new OffsetsMaps(oldestOffsets, newestOffsets, upcomingOffsets); }