MINOR: Remove unnecessary Optional from offsetsToSnapshot (#19613)
CI / build (push) Waiting to run Details

Reviewers: PoAn Yang <payang@apache.org>, Ken Huang
 <s7133700@gmail.com>, TengYao Chi <kitingiao@gmail.com>, Chia-Ping Tsai
 <chia7712@gmail.com>
This commit is contained in:
Jhen-Yung Hsu 2025-05-03 12:58:08 +08:00 committed by GitHub
parent e68781414e
commit 28ad4dd5c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 15 deletions

View File

@ -2416,15 +2416,13 @@ public class UnifiedLog implements AutoCloseable {
Time time,
boolean reloadFromCleanShutdown,
String logPrefix) throws IOException {
List<Optional<Long>> offsetsToSnapshot = new ArrayList<>();
if (segments.nonEmpty()) {
long lastSegmentBaseOffset = segments.lastSegment().get().baseOffset();
Optional<LogSegment> lowerSegment = segments.lowerSegment(lastSegmentBaseOffset);
Optional<Long> nextLatestSegmentBaseOffset = lowerSegment.map(LogSegment::baseOffset);
offsetsToSnapshot.add(nextLatestSegmentBaseOffset);
offsetsToSnapshot.add(Optional.of(lastSegmentBaseOffset));
}
offsetsToSnapshot.add(Optional.of(lastOffset));
List<Long> offsetsToSnapshot = new ArrayList<>();
segments.lastSegment().ifPresent(lastSegment -> {
long lastSegmentBaseOffset = lastSegment.baseOffset();
segments.lowerSegment(lastSegmentBaseOffset).ifPresent(s -> offsetsToSnapshot.add(s.baseOffset()));
offsetsToSnapshot.add(lastSegmentBaseOffset);
});
offsetsToSnapshot.add(lastOffset);
LOG.info("{}Loading producer state till offset {}", logPrefix, lastOffset);
@ -2443,11 +2441,9 @@ public class UnifiedLog implements AutoCloseable {
// To avoid an expensive scan through all the segments, we take empty snapshots from the start of the
// last two segments and the last offset. This should avoid the full scan in the case that the log needs
// truncation.
for (Optional<Long> offset : offsetsToSnapshot) {
if (offset.isPresent()) {
producerStateManager.updateMapEndOffset(offset.get());
producerStateManager.takeSnapshot();
}
for (long offset : offsetsToSnapshot) {
producerStateManager.updateMapEndOffset(offset);
producerStateManager.takeSnapshot();
}
} else {
LOG.info("{}Reloading from producer snapshot and rebuilding producer state from offset {}", logPrefix, lastOffset);
@ -2469,7 +2465,7 @@ public class UnifiedLog implements AutoCloseable {
long startOffset = Utils.max(segment.baseOffset(), producerStateManager.mapEndOffset(), logStartOffset);
producerStateManager.updateMapEndOffset(startOffset);
if (offsetsToSnapshot.contains(Optional.of(segment.baseOffset()))) {
if (offsetsToSnapshot.contains(segment.baseOffset())) {
producerStateManager.takeSnapshot();
}
int maxPosition = segment.size();