Himanshu-g81 commented on code in PR #2577:
URL: https://github.com/apache/phoenix/pull/2577#discussion_r3638950572


##########
phoenix-core-server/src/main/java/org/apache/phoenix/replication/reader/ReplicationLogDiscoveryReplay.java:
##########
@@ -202,50 +228,80 @@ protected void processFile(Path path) throws IOException {
   }
 
   /**
-   * Initializes lastRoundProcessed and lastRoundInSync based on HA group 
state. For DEGRADED states
-   * (DEGRADED_STANDBY, DEGRADED_STANDBY_FOR_WRITER): - Sets 
replicationReplayState to DEGRADED -
-   * Initializes lastRoundProcessed from minimum of: in-progress files, new 
files, or current time -
-   * Initializes lastRoundInSync from minimum of: lastSyncStateTimeInMs (from 
HA Store) or minimum
-   * timestamp from IN and IN PROGRESS files - This ensures lastRoundInSync 
represents the last
-   * known good sync point before degradation For SYNC states (STANDBY): - Sets
-   * replicationReplayState to SYNC - Calls parent's 
initializeLastRoundProcessed() to initialize
-   * lastRoundProcessed - Sets lastRoundInSync equal to lastRoundProcessed 
(both are in sync)
+   * Initializes lastRoundProcessed and lastRoundInSync based on the persisted 
HA group state.
+   * <ul>
+   * <li>DEGRADED_STANDBY: sets replicationReplayState to DEGRADED and 
initializes both rounds from
+   * the last known good sync point via {@link 
#initLastRoundsFromLastSyncPoint(HAGroupStoreRecord)}
+   * (lastRoundInSync from the minimum of lastSyncStateTimeInMs and the file 
frontier, so it
+   * represents the last consistent point before degradation).</li>
+   * <li>STANDBY_TO_ACTIVE: a restart while already mid-failover (e.g. after a 
direct
+   * DEGRADED_STANDBY -&gt; STANDBY_TO_ACTIVE transition). Initializes both 
rounds from the last
+   * known good sync point, then sets replicationReplayState to 
SYNCED_RECOVERY only when a rewind
+   * is actually pending (lastRoundInSync behind lastRoundProcessed) so the 
first replay() rewinds
+   * before failover can promote; when the rounds are already equal it 
initializes directly to SYNC
+   * so promotion is not delayed by a round window.</li>
+   * <li>Other states (e.g. STANDBY): sets replicationReplayState to SYNC, 
delegates to the parent
+   * to initialize lastRoundProcessed, and sets lastRoundInSync equal to 
lastRoundProcessed.</li>
+   * </ul>
+   * When the state is STANDBY_TO_ACTIVE, failoverPending is also armed.
    * @throws IOException if there's an error reading HA group state or file 
timestamps
    */
   @Override
   protected void initializeLastRoundProcessed() throws IOException {
     LOG.info("Initializing last round processed for haGroup: {}", haGroupName);
+    // Sample current time BEFORE reading the HA group state, so if the group 
transitions (e.g.
+    // SYNC -> DEGRADED_STANDBY) during init, the starting round still anchors 
to when init began
+    // rather than to a later point after the state read and file scans.
+    long frontierStartTime = EnvironmentEdgeManager.currentTime();
     HAGroupStoreRecord haGroupStoreRecord = getHAGroupRecord();
-    LOG.info("Found HA Group state during initialization as {} for haGroup: 
{}",
-      haGroupStoreRecord.getHAGroupState(), haGroupName);
-    if (
-      
HAGroupStoreRecord.HAGroupState.DEGRADED_STANDBY.equals(haGroupStoreRecord.getHAGroupState())
-    ) {
+    HAGroupStoreRecord.HAGroupState haGroupState = 
haGroupStoreRecord.getHAGroupState();
+    LOG.info("Found HA Group state during initialization as {} for haGroup: 
{}", haGroupState,
+      haGroupName);
+    // Each branch below sets the initial state with 
compareAndSet(NOT_INITIALIZED, ...), not a
+    // plain set: the LOCAL state listeners are subscribed in init() before 
this runs, so a
+    // concurrent LOCAL transition may have already advanced the state off 
NOT_INITIALIZED. When
+    // that happens the CAS no-ops and we deliberately keep the listener's 
value -- a live
+    // transition is more recent (and thus more authoritative) than the record 
snapshot read above.
+    if (HAGroupStoreRecord.HAGroupState.DEGRADED_STANDBY.equals(haGroupState)) 
{

Review Comment:
   Thanks, have updated in latest commit.



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