csudharsanan commented on code in PR #3043:
URL: https://github.com/apache/helix/pull/3043#discussion_r2155593565


##########
helix-core/src/main/java/org/apache/helix/controller/stages/MessageGenerationPhase.java:
##########
@@ -290,7 +327,110 @@ private void generateMessage(final Resource resource, 
final BaseControllerDataPr
     } // end of for-each-partition
   }
 
-  private boolean shouldCreateSTCancellation(Message pendingMessage, String 
desiredState,
+  /**
+   * Calculate the current active replica count based on state model type.
+   * The count includes replicas in top states, secondary top states (where 
applicable),
+   * and ERROR states since helix considers them active.
+   * State model handling:
+   * - Single-top state models: Differentiates between patterns with and 
without secondary top
+   * states
+   * - ONLINE-OFFLINE: Counts ONLINE + ERROR states only
+   * - MASTER-SLAVE-OFFLINE: Counts MASTER + SLAVE + ERROR states
+   * - ONLINE-STANDBY-OFFLINE: Counts ONLINE + STANDBY + ERROR states
+   * - Multi-top state models: Counts only top states + ERROR states
+   * @param currentStateMap Map of instance name to current state for this 
partition, representing
+   *          the actual state of each replica before any pending transitions
+   * @param stateModelDef State model definition containing the state 
hierarchy and transition rules
+   *          used to determine which states are considered active
+   * @return The number of replicas currently in active states, used to 
determine the
+   *         currentActiveReplicaNumber for the partition.
+   */
+  private int calculateCurrentActiveReplicaCount(Map<String, String> 
currentStateMap,
+      StateModelDefinition stateModelDef) {
+    List<String> trueSecondaryTopStates = 
getTrueSecondaryTopStates(stateModelDef);
+    return (int) currentStateMap.values().stream()
+        .filter(state -> stateModelDef.getTopState().contains(state) // Top 
states (MASTER, ONLINE,
+                                                                     // LEADER)
+            || trueSecondaryTopStates.contains(state) // True secondary states 
(SLAVE, STANDBY,
+                                                      // BOOTSTRAP)
+            || HelixDefinedState.ERROR.name().equals(state) // ERROR states 
(still considered
+                                                            // active)
+        // DROPPED and OFFLINE are automatically excluded by 
getTrueSecondaryTopStates()
+        ).count();
+  }
+
+  /**
+   * Get true secondary top states - states that:
+   * 1. Are not the top state itself (avoid double-counting)
+   * 2. Are not non-serving states like OFFLINE and DROPPED.
+   * Reasons for elimination:
+   * - getSecondTopStates() can include the top state itself in some state 
models.
+   * Example - OnlineOfflineWithBootstrap:
+   * topState="ONLINE", getSecondTopStates()=["ONLINE", "BOOTSTRAP"]
+   * After filtering: trueSecondaryTopStates=["BOOTSTRAP"] (removes "ONLINE" 
as it is top state.)
+   * - getSecondTopStates() can also include OFFLINE as a secondary top state 
in some state models.
+   * Example - OnlineOffline:
+   * getSecondTopStates() = ["OFFLINE"] as it transitions to ONLINE.
+   * After filtering: trueSecondaryTopStates=[] (removes "OFFLINE" as it's not 
a serving state).
+   * @param stateModelDef State model definition containing state hierarchy 
information
+   */
+  private List<String> getTrueSecondaryTopStates(StateModelDefinition 
stateModelDef) {
+    return stateModelDef.getSecondTopStates().stream()
+        // Remove top-state duplicates
+        .filter(state -> !stateModelDef.getTopState().equals(state))
+        // Remove non-serving states
+        .filter(state -> !OFFLINE.equals(state) && 
!HelixDefinedState.DROPPED.name().equals(state))
+        .collect(Collectors.toList());
+  }
+
+  /**
+   * Determines if the given current state is considered active based on the 
state model type.
+   * For single-top state models, top, true secondary top, and ERROR states 
are active.
+   * For multi-top state models, top and ERROR states are active.
+   * ERROR state replicas are considered active in HELIX as they do not affect 
availability.
+   * @param currentState The current state to check
+   * @param stateModelDef State model definition containing state hierarchy 
information
+   * @param isSingleTopState Whether this is a single-top state model
+   * @return true if the current state is considered active, false otherwise
+   */
+  private boolean isCurrentlyActive(String currentState, StateModelDefinition 
stateModelDef,
+      boolean isSingleTopState) {
+    // ERROR state is always considered active regardless of state model type
+    if (HelixDefinedState.ERROR.name().equals(currentState)) {
+      return true;
+    }
+    if (isSingleTopState) {
+      return stateModelDef.getTopState().contains(currentState)
+          || getTrueSecondaryTopStates(stateModelDef).contains(currentState);
+    } else {
+      return stateModelDef.getTopState().contains(currentState);
+    }
+  }
+
+  /**
+   * Determines if the given target state is considered active based on the 
state model type.
+   * For single-top state models, both top,true secondary top and ERROR states 
are active.
+   * For multi-top state models, top and ERROR states are active.
+   * @param targetState The target state to check
+   * @param stateModelDef State model definition containing state hierarchy 
information
+   * @param isSingleTopState Whether this is a single-top state model
+   * @return true if the target state is considered active, false otherwise
+   */
+  private boolean isTargetActive(String targetState, StateModelDefinition 
stateModelDef,

Review Comment:
   yes



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