ArafatKhan2198 commented on code in PR #12218:
URL: https://github.com/apache/ozone/pull/12218#discussion_r4765571130


##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/StorageContainerManager.java:
##########
@@ -2350,7 +2349,10 @@ public boolean doesPeerExist(String scmId) {
         .getGroup().getPeer(RaftPeerId.valueOf(scmId)) != null;
   }
 
-  public void scmHAMetricsUpdate(String leaderId) {
+  /**
+   * @param leaderId the current SCM leader, or null if it is not known.
+   */
+  public synchronized void scmHAMetricsUpdate(String leaderId) {

Review Comment:
   I'm curious how this would behave in a mini-cluster where multiple SCMs 
share the same DefaultMetricsSystem. Since they all use the same source name 
and synchronization is at the instance level, could one SCM end up 
unregistering another SCM's metrics, especially now that followers also update 
them?



##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/hdds/scm/TestStorageContainerManagerHA.java:
##########
@@ -170,14 +172,25 @@ public void testSCMLeadershipMetric() throws IOException, 
InterruptedException {
     assertEquals(0, failedSCMs.get());
 
     // THEN expect only one SCM node (leader) will have 
'scmha_metrics_scmha_leader_state' metric set to 1
-    int leaderCount = 0;
-    for (StorageContainerManager scm : 
cluster.getStorageContainerManagersList()) {
-      if (scm.getScmHAMetrics() != null && 
scm.getScmHAMetrics().getSCMHAMetricsInfoLeaderState() == 1) {
-        leaderCount++;
-        break;
-      }
+    StorageContainerManager oldLeader = cluster.getScmLeader(true);
+    waitForOnlyLeaderMetric(oldLeader);
+
+    // WHEN leadership is transferred to another SCM
+    StorageContainerManager newLeader = inactiveSCMs.get(0);

Review Comment:
   Could there be a scenario where inactiveSCMs.get(0) has already become the 
leader by the time we reach here? Would it be better to explicitly pick an SCM 
other than oldLeader so that we always exercise an actual leadership transfer?



##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/hdds/scm/TestStorageContainerManagerHA.java:
##########
@@ -170,14 +172,25 @@ public void testSCMLeadershipMetric() throws IOException, 
InterruptedException {
     assertEquals(0, failedSCMs.get());
 
     // THEN expect only one SCM node (leader) will have 
'scmha_metrics_scmha_leader_state' metric set to 1
-    int leaderCount = 0;
-    for (StorageContainerManager scm : 
cluster.getStorageContainerManagersList()) {
-      if (scm.getScmHAMetrics() != null && 
scm.getScmHAMetrics().getSCMHAMetricsInfoLeaderState() == 1) {
-        leaderCount++;
-        break;
-      }
+    StorageContainerManager oldLeader = cluster.getScmLeader(true);
+    waitForOnlyLeaderMetric(oldLeader);
+
+    // WHEN leadership is transferred to another SCM
+    StorageContainerManager newLeader = inactiveSCMs.get(0);
+    try (StorageContainerLocationProtocolClientSideTranslatorPB scmClient =
+        cluster.getStorageContainerLocationClient()) {
+      scmClient.transferLeadership(newLeader.getScmId());
     }
-    assertEquals(1, leaderCount);
+    GenericTestUtils.waitFor(newLeader::checkLeader, 100, 30000);
+
+    // THEN the old leader's metric drops to 0 and only the new leader has it 
set to 1
+    waitForOnlyLeaderMetric(newLeader);
+  }
+
+  private void waitForOnlyLeaderMetric(StorageContainerManager leader) throws 
TimeoutException, InterruptedException {
+    GenericTestUtils.waitFor(() -> 
cluster.getStorageContainerManagersList().stream()
+        .allMatch(scm -> scm.getScmHAMetrics() != null
+            && scm.getScmHAMetrics().getSCMHAMetricsInfoLeaderState() == (scm 
== leader ? 1 : 0)), 100, 30000);

Review Comment:
   From what I understand, getSCMHAMetricsInfoLeaderState() returns a value 
that is only updated when getMetrics() is called. Could we end up reading the 
default value of 0 for a follower whose metrics haven't been collected yet? 
Would it be a good idea to explicitly collect the metrics before verifying the 
gauge value?



##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java:
##########
@@ -353,46 +355,38 @@ public void notifyLeaderChanged(RaftGroupMemberId 
groupMemberId,
     }
 
     final boolean isLeader = groupMemberId.getPeerId().equals(newLeaderId);
-
-    if (!isLeader) {
-      // Follower: capture the (possibly new) leader's current committed index
-      // as the fixed catch-up target, then start the datanode protocol server
-      // if we are already caught up with it; otherwise applyTransaction /
-      // notifyTermIndexUpdated start it as catch-up completes. Set it always:
-      // getLeaderCommitIndex() returns -1 when the leader is not known yet,
-      // which isFollowerCaughtUp() treats as uncaptured and re-reads later.
+    final String message;
+
+    if (isLeader) {
+      long currentTerm = 
scm.getScmHAManager().getRatisServer().getDivision().getInfo().getCurrentTerm();
+      message = "current SCM " + scm.getScmId() + " becomes leader of term " + 
currentTerm;
+
+      scm.getScmContext().updateLeaderAndTerm(true, currentTerm);
+      scm.getSequenceIdGen().invalidateBatch();
+
+      DeletedBlockLog deletedBlockLog = 
scm.getScmBlockManager().getDeletedBlockLog();
+      Preconditions.checkArgument(deletedBlockLog instanceof 
DeletedBlockLogImpl);
+      ((DeletedBlockLogImpl) deletedBlockLog).onBecomeLeader();
+      scm.getScmDecommissionManager().onBecomeLeader();

Review Comment:
   I noticed that earlier we were starting the DN server and refreshing safe 
mode before calling onBecomeLeader() on these services. Is there a particular 
reason for reversing that order? Would it be safer to retain the existing 
sequence since this PR is mainly concerned with fixing the metrics?



##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/StorageContainerManager.java:
##########


Review Comment:
   Would it be a good idea to register the metrics once and just update the 
leadership state using a volatile field? Since the node ID is the only tag we 
expose and it doesn't change, I'm wondering if we could avoid recreating the 
metrics object on every leadership change.



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