9aman commented on code in PR #20285:
URL: https://github.com/apache/pinot/pull/20285#discussion_r4625654701


##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/realtime/PauselessSegmentCompletionFSM.java:
##########
@@ -93,6 +99,74 @@ public SegmentCompletionProtocol.Response 
extendBuildTime(final String instanceI
     }
   }
 
+  /// Reached only in COMMITTER_UPLOADING (see [#extendBuildTime]). Accepts an 
extension from the committer even after
+  /// the commit deadline or past 
[SegmentCompletionManager#getMaxCommitTimeForAllSegmentsSeconds()]. Fails an
+  /// extension from any other server or for any other offset, without 
aborting the FSM.
+  @Override
+  protected SegmentCompletionProtocol.Response 
committerNotifiedExtendBuildTime(String instanceId,
+      StreamPartitionMsgOffset offset, int extTimeSec, long now) {
+    if (!instanceId.equals(_winner) || offset.compareTo(_winningOffset) != 0) {
+      return fail(instanceId, offset);
+    }
+    logIfPastCommitDeadline(now, instanceId, offset);
+    // After the commit start, the commit deadline only decides which requests 
are logged as late
+    _maxTimeAllowedToCommitMs = now + extTimeSec * 1000L;
+    return SegmentCompletionProtocol.RESP_PROCESSED;
+  }
+
+  /// After the commit start, the commit deadline does not abort the FSM: an 
abort cannot hand the commit to another
+  /// replica, it only makes the commit end of the committer fail. Before the 
commit start, the deadline lets another
+  /// replica take the commit over, as for a blocking table.
+  @Override
+  protected SegmentCompletionProtocol.Response 
abortIfTooLateAndReturnHold(long now, String instanceId,
+      StreamPartitionMsgOffset offset) {
+    if (_state != BlockingSegmentCompletionFSMState.COMMITTER_UPLOADING
+        && _state != BlockingSegmentCompletionFSMState.COMMITTING) {
+      return super.abortIfTooLateAndReturnHold(now, instanceId, offset);
+    }
+    logIfPastCommitDeadline(now, instanceId, offset);
+    return null;
+  }
+
+  private void logIfPastCommitDeadline(long now, String instanceId, 
StreamPartitionMsgOffset offset) {
+    if (now > _maxTimeAllowedToCommitMs) {
+      _logger.info("{}:Ignoring commit deadline after commit start (too late) 
instance={} offset={} now={} start={}",
+          _state, instanceId, offset, now, _startTimeMs);
+    }
+  }
+
+  /// Checks the segment ZK metadata before the commit end moves the uploaded 
segment file to the deep store path of the
+  /// segment: the segment must still be COMMITTING with the end offset of 
this commit, within the max segment
+  /// completion time. The ZK metadata update after the move checks the status 
and the time too, but after the max
+  /// segment completion time the validation manager may already have uploaded 
another replica's copy to that path,
+  /// and the move would overwrite it.
+  @Override
+  protected SegmentCompletionProtocol.Response 
commitSegment(SegmentCompletionProtocol.Request.Params reqParams,
+      CommittingSegmentDescriptor committingSegmentDescriptor) {
+    String segmentName = _segmentName.getSegmentName();
+    StreamPartitionMsgOffset offset = 
_streamPartitionMsgOffsetFactory.create(reqParams.getStreamPartitionMsgOffset());
+    try {
+      SegmentZKMetadata segmentZKMetadata = 
_segmentManager.getSegmentZKMetadata(_realtimeTableName, segmentName, null);
+      String endOffset = segmentZKMetadata.getEndOffset();
+      if (segmentZKMetadata.getStatus() != Status.COMMITTING || endOffset == 
null
+          || 
_streamPartitionMsgOffsetFactory.create(endOffset).compareTo(offset) != 0) {
+        _logger.warn("Rejecting commit end of segment: {} at offset: {}, 
segment ZK status: {}, end offset: {}",
+            segmentName, offset, segmentZKMetadata.getStatus(), endOffset);
+        return SegmentCompletionProtocol.RESP_FAILED;
+      }
+      if 
(_segmentManager.isExceededMaxSegmentCompletionTime(_realtimeTableName, 
segmentName,
+          _segmentManager.getCurrentTimeMs())) {
+        _logger.warn("Rejecting commit end of segment: {} after the max 
segment completion time", segmentName);
+        return SegmentCompletionProtocol.RESP_FAILED;
+      }
+    } catch (Exception e) {
+      _logger.error("Caught exception while checking segment ZK metadata 
before the commit end of segment: {}",
+          segmentName, e);
+      return SegmentCompletionProtocol.RESP_FAILED;
+    }
+    return super.commitSegment(reqParams, committingSegmentDescriptor);

Review Comment:
   Thanks. The race is real, but it is older than this PR:
   - On master, the commit end checks ZK only after the move. So a commit end 
that reaches the move after a repair replaces the repaired copy. This PR 
rejects that case before the move.
   - What remains needs a commit end that passes the check before 
`max.segment.completion.time.millis` and then stalls until the repair moves its 
copy. The scheduled repair runs only in segment-level validation (every 24h by 
default).
   - A full fix must order every writer of the deep store path, also 
`uploadFromServerToDeepstore`, which runs on any controller. A lock on the lead 
controller does not cover it, and PinotFS has no conditional write to fence the 
move. I suggest a separate issue for this.
   - Recovery: 
`uploadFromServerToDeepstore?segmentNames=<segment>&forceMode=true` uploads the 
copy of one replica and writes its CRC to ZK. The commit end logs `should be 
COMMITTING, found: DONE` when the race happens.
   



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/realtime/PauselessSegmentCompletionFSM.java:
##########
@@ -93,6 +99,74 @@ public SegmentCompletionProtocol.Response 
extendBuildTime(final String instanceI
     }
   }
 
+  /// Reached only in COMMITTER_UPLOADING (see [#extendBuildTime]). Accepts an 
extension from the committer even after
+  /// the commit deadline or past 
[SegmentCompletionManager#getMaxCommitTimeForAllSegmentsSeconds()]. Fails an
+  /// extension from any other server or for any other offset, without 
aborting the FSM.
+  @Override
+  protected SegmentCompletionProtocol.Response 
committerNotifiedExtendBuildTime(String instanceId,
+      StreamPartitionMsgOffset offset, int extTimeSec, long now) {
+    if (!instanceId.equals(_winner) || offset.compareTo(_winningOffset) != 0) {
+      return fail(instanceId, offset);
+    }
+    logIfPastCommitDeadline(now, instanceId, offset);
+    // After the commit start, the commit deadline only decides which requests 
are logged as late
+    _maxTimeAllowedToCommitMs = now + extTimeSec * 1000L;
+    return SegmentCompletionProtocol.RESP_PROCESSED;
+  }
+
+  /// After the commit start, the commit deadline does not abort the FSM: an 
abort cannot hand the commit to another
+  /// replica, it only makes the commit end of the committer fail. Before the 
commit start, the deadline lets another
+  /// replica take the commit over, as for a blocking table.
+  @Override
+  protected SegmentCompletionProtocol.Response 
abortIfTooLateAndReturnHold(long now, String instanceId,
+      StreamPartitionMsgOffset offset) {
+    if (_state != BlockingSegmentCompletionFSMState.COMMITTER_UPLOADING
+        && _state != BlockingSegmentCompletionFSMState.COMMITTING) {
+      return super.abortIfTooLateAndReturnHold(now, instanceId, offset);
+    }
+    logIfPastCommitDeadline(now, instanceId, offset);
+    return null;

Review Comment:
   Thanks. Agreed, the cached FSM can return HOLD after a repair. I suggest we 
keep this, because the effect is small:
   - Commit start already set the segment to ONLINE in the IdealState. The 
CONSUMING to ONLINE transition stops the consumer of the non-winner, which ends 
its HOLD loop. With an offset above the end offset, it then downloads the 
segment, as it does after DISCARD.
   - The repair runs only after `max.segment.completion.time.millis`, in 
segment-level validation. So a non-winner sees the difference only if it does 
not process that transition for that long.
   - A retire at the hard deadline brings back the abort loop of master while 
ZK is still COMMITTING: a fresh FSM picks the non-winner as committer, and its 
commit start fails.
   
   If you want it, a follow-up can read the segment ZK metadata before such a 
HOLD and retire the FSM when the segment is DONE.
   



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to