peter-toth commented on code in PR #711:
URL: 
https://github.com/apache/spark-kubernetes-operator/pull/711#discussion_r3427085231


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/AppInitStep.java:
##########
@@ -66,17 +70,30 @@ public ReconcileProgress reconcile(
     if (app.getStatus().getPreviousAttemptSummary() != null) {
       Instant lastTransitionTime = 
Instant.parse(currentState.getLastTransitionTime());
       ApplicationAttemptSummary attemptSummary = 
app.getStatus().getPreviousAttemptSummary();
-      ApplicationState lastState = attemptSummary.getStateTransitionHistory()
-          .get(attemptSummary.getStateTransitionHistory().lastKey());
-      Instant restartTime =
-          lastTransitionTime.plusMillis(
-              app.getSpec()
-                  .getApplicationTolerations()
-                  .getRestartConfig()
-                  
.getEffectiveRestartBackoffMillis(lastState.getCurrentStateSummary()));
-      Instant now = Instant.now();
-      if (restartTime.isAfter(now)) {
-        return ReconcileProgress.completeAndRequeueAfter(Duration.between(now, 
restartTime));
+      SortedMap<Long, ApplicationState> attemptHistory = 
attemptSummary.getStateTransitionHistory();
+      final ApplicationState lastState;
+      if (attemptHistory != null && !attemptHistory.isEmpty()) {
+        lastState = attemptHistory.get(attemptHistory.lastKey());
+      } else {
+        // Non-trim mode: previousAttemptSummary carries null 
stateTransitionHistory.
+        // Fall back to the main history and pick the state entered just 
before the current
+        // initializing state (which is the stopping state that triggered the 
restart).
+        NavigableMap<Long, ApplicationState> mainNav =
+            (NavigableMap<Long, ApplicationState>) 
app.getStatus().getStateTransitionHistory();
+        Map.Entry<Long, ApplicationState> prevEntry = 
mainNav.lowerEntry(mainNav.lastKey());

Review Comment:
   Nit: This "entry immediately before the last main-history entry" computation 
already exists in `AppCleanUpStep.getLastObservedStateBeforeTermination` 
(`AppCleanUpStep.java:300-309`) — same unchecked `NavigableMap` cast, same 
`lowerEntry`. The two will drift over time. Consider promoting a single guarded 
helper onto `ApplicationStatus` and calling it from both sites:
   
   ```java
   /** State entered immediately before the current (last) one, or the current 
state if none. */
   public ApplicationState getStateBeforeCurrent() {
     NavigableMap<Long, ApplicationState> nav =
         (NavigableMap<Long, ApplicationState>) stateTransitionHistory;
     Map.Entry<Long, ApplicationState> prev = nav.lowerEntry(nav.lastKey());
     return prev != null ? prev.getValue() : nav.lastEntry().getValue();
   }
   ```
   
   Then this branch becomes `lastState = 
app.getStatus().getStateBeforeCurrent();`. As a bonus it hardens the 
`AppCleanUpStep` copy, which dereferences `lowerEntry(...).getValue()` with no 
null guard. Counter-argument: it couples two reconcile steps to one status 
method — but they're already coupled by the duplicated logic, just implicitly.



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