chia7712 commented on code in PR #20913:
URL: https://github.com/apache/kafka/pull/20913#discussion_r3294945914


##########
storage/src/main/java/org/apache/kafka/server/log/remote/storage/RemoteLogManager.java:
##########
@@ -925,18 +927,73 @@ List<EnrichedLogSegment> candidateLogSegments(UnifiedLog 
log, Long fromOffset, L
             List<EnrichedLogSegment> candidateLogSegments = new ArrayList<>();
             List<LogSegment> segments = log.logSegments(fromOffset, 
Long.MAX_VALUE);
             if (!segments.isEmpty()) {
+                long currentTimeMs = time.milliseconds();
+                long totalLogSize = UnifiedLog.sizeInBytes(segments);
+                long cumulativeSize = 0;
                 for (int idx = 1; idx < segments.size(); idx++) {
                     LogSegment previousSeg = segments.get(idx - 1);
                     LogSegment currentSeg = segments.get(idx);
                     if (currentSeg.baseOffset() <= lastStableOffset) {
-                        candidateLogSegments.add(new 
EnrichedLogSegment(previousSeg, currentSeg.baseOffset()));
+                        cumulativeSize += previousSeg.size();
+                        if (isEligibleForUpload(log.config(), previousSeg, 
currentTimeMs, totalLogSize, cumulativeSize)) {
+                            candidateLogSegments.add(new 
EnrichedLogSegment(previousSeg, currentSeg.baseOffset()));
+                        } else {
+                            break;
+                        }
                     }
                 }
                 // Discard the last active segment
             }
             return candidateLogSegments;
         }
 
+        private boolean isEligibleForUpload(LogConfig logConfig, LogSegment 
previousSeg, long currentTimeMs, long totalLogSize, long cumulativeSize) {
+            long copyLagMs = logConfig.remoteCopyLagMs();
+            long copyLagBytes = logConfig.remoteCopyLagBytes();
+            if (logger.isTraceEnabled()) {
+                logger.trace("delayCopy check for segment {}: copyLagMs={}, 
copyLagBytes={}, currentTimeMs={}, totalLogSize={}, cumulativeSize={}, 
sizeLagBytes={}",
+                        previousSeg, copyLagMs, copyLagBytes, currentTimeMs, 
totalLogSize, cumulativeSize, totalLogSize - cumulativeSize);
+            }
+
+            if (copyLagMs == 0 || copyLagBytes == 0) {
+                return true;
+            }
+
+            boolean limitedCopyLagMsCheck =  copyLagMs > 0;
+            boolean limitedCopyLagSizeCheck = copyLagBytes > 0;
+
+            if (limitedCopyLagMsCheck && eligibleUploadByTime(previousSeg, 
currentTimeMs, copyLagMs)) {
+                return true;
+            }
+
+            return limitedCopyLagSizeCheck && 
eligibleUploadBySize(previousSeg, totalLogSize, cumulativeSize, copyLagBytes);
+        }
+
+        private boolean eligibleUploadByTime(LogSegment segment, long 
currentTimeMs, long copyLagMs) {
+            try {
+                long segmentAgeMs = currentTimeMs - segment.largestTimestamp();
+                boolean eligibleUpload = segmentAgeMs < 0 || segmentAgeMs >= 
copyLagMs;

Review Comment:
   If we all hate `-2`, there is another approach: reverse the configuration 
logic. 
   
   For example, we could introduce remote.copy.before.deletion.ms instead of 
remote.copy.lag.ms. If a user sets remote.copy.before.deletion.ms=2hr, it means 
they want to upload log segments 2 hours before they are scheduled to be 
deleted from local storage.
   
   The default value would be -1, which dynamically resolves to 
local.retention.ms. This fully maintains backward compatibility (resulting in 
immediate upload). Setting it to 0 means the user wants to delay the upload as 
much as possible, triggering it right before local deletion.
   
   The only side effect is that this kind of "reverse" or "countdown" time 
calculation is quite rare in the existing Kafka codebase.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to