This is an automated email from the ASF dual-hosted git repository.

adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 0cacefad084 HDDS-17758. Shutdown SCM when RatisServer is down (#12015)
0cacefad084 is described below

commit 0cacefad08428aab94405cb0a297db49e2121b32
Author: Sarveksha Yeshavantha Raju 
<[email protected]>
AuthorDate: Tue Nov 17 20:59:28 2026 +0530

    HDDS-17758. Shutdown SCM when RatisServer is down (#12015)
---
 .../apache/hadoop/hdds/scm/ha/SCMStateMachine.java | 19 +++++++
 .../hadoop/hdds/scm/ha/TestSCMStateMachine.java    | 60 ++++++++++++++++++----
 2 files changed, 70 insertions(+), 9 deletions(-)

diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
index 026f57c169f..3fba33c6126 100644
--- 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
+++ 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
@@ -252,6 +252,25 @@ public void notifyLogFailed(Throwable ex,
     ExitUtils.terminate(1, ex.getMessage(), ex, StateMachine.LOG);
   }
 
+  @Override
+  public void notifyServerShutdown(RaftProtos.RoleInfoProto roleInfo, boolean 
allServer) {
+    // If SCM is stopped, this shutdown originates from scm.stop(); otherwise 
it originates from Ratis.
+    if (!isInitialized || scm.isStopped()) {
+      return;
+    }
+    LOG.error("SCM Ratis server is shut down by Ratis, terminating SCM");
+    // Run on a different thread, so that the Ratis server can finish closing.
+    CompletableFuture.runAsync(() -> {
+      try {
+        close();
+      } catch (IOException e) {
+        String message = "Failed to close the SCM state machine after the 
Ratis server shutdown";
+        LOG.error(message, e);
+        scm.shutDown(message);
+      }
+    });
+  }
+
   @Override
   public void notifyNotLeader(Collection<TransactionContext> pendingEntries) {
     if (!isInitialized) {
diff --git 
a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSCMStateMachine.java
 
b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSCMStateMachine.java
index af8a5ae8049..e897df0836e 100644
--- 
a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSCMStateMachine.java
+++ 
b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSCMStateMachine.java
@@ -43,6 +43,7 @@
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
@@ -80,8 +81,10 @@
 import org.apache.ratis.protocol.Message;
 import org.apache.ratis.protocol.RaftGroupId;
 import org.apache.ratis.protocol.RaftGroupMemberId;
+import org.apache.ratis.protocol.RaftPeer;
 import org.apache.ratis.protocol.RaftPeerId;
 import org.apache.ratis.server.DivisionInfo;
+import org.apache.ratis.server.RaftConfiguration;
 import org.apache.ratis.server.RaftServer;
 import org.apache.ratis.server.protocol.TermIndex;
 import org.apache.ratis.statemachine.TransactionContext;
@@ -106,6 +109,10 @@ public class TestSCMStateMachine {
   private final RaftPeerId followerId = RaftPeerId.valueOf("follower");
   private final RaftPeerId leaderId = RaftPeerId.valueOf("leader");
 
+  private volatile List<RaftPeer> currentPeers = Collections.singletonList(
+      RaftPeer.newBuilder().setId(followerId).build());
+
+  private StorageContainerManager mockScm;
   private SCMMetrics metrics;
   private SCMDatanodeProtocolServer datanodeProtocolServer;
   private SCMSafeModeManager safeModeManager;
@@ -113,7 +120,7 @@ public class TestSCMStateMachine {
 
   @BeforeEach
   void setUp() {
-    StorageContainerManager scm = mock(StorageContainerManager.class);
+    mockScm = mock(StorageContainerManager.class);
     metrics = SCMMetrics.create("scm1");
     SCMHADBTransactionBuffer buffer = mock(SCMHADBTransactionBuffer.class);
     datanodeProtocolServer = mock(SCMDatanodeProtocolServer.class);
@@ -124,13 +131,17 @@ void setUp() {
     SCMRatisServer ratisServer = mock(SCMRatisServer.class);
     RaftServer.Division division = mock(RaftServer.Division.class);
     DivisionInfo divisionInfo = mock(DivisionInfo.class);
-
-    when(scm.getMetrics()).thenReturn(metrics);
-    when(scm.isStopped()).thenAnswer(invocation -> scmStopped.get());
-    when(scm.getScmContext()).thenReturn(scmContext);
-    when(scm.getScmHAManager()).thenReturn(haManager);
-    when(scm.getDatanodeProtocolServer()).thenReturn(datanodeProtocolServer);
-    when(scm.getScmSafeModeManager()).thenReturn(safeModeManager);
+    RaftConfiguration raftConf = mock(RaftConfiguration.class);
+
+    when(mockScm.getMetrics()).thenReturn(metrics);
+    when(mockScm.getScmId()).thenReturn(followerId.toString());
+    when(division.getRaftConf()).thenReturn(raftConf);
+    when(raftConf.getCurrentPeers()).thenAnswer(invocation -> currentPeers);
+    when(mockScm.isStopped()).thenAnswer(invocation -> scmStopped.get());
+    when(mockScm.getScmContext()).thenReturn(scmContext);
+    when(mockScm.getScmHAManager()).thenReturn(haManager);
+    
when(mockScm.getDatanodeProtocolServer()).thenReturn(datanodeProtocolServer);
+    when(mockScm.getScmSafeModeManager()).thenReturn(safeModeManager);
     when(haManager.getRatisServer()).thenReturn(ratisServer);
     when(ratisServer.getDivision()).thenReturn(division);
     when(division.getInfo()).thenReturn(divisionInfo);
@@ -145,7 +156,7 @@ void setUp() {
     when(buffer.getLatestTrxInfo()).thenReturn(
         TransactionInfo.valueOf(TermIndex.valueOf(0, 0)));
 
-    stateMachine = new SCMStateMachine(scm, buffer, RETRY_INTERVAL, 
retryClock::get);
+    stateMachine = new SCMStateMachine(mockScm, buffer, RETRY_INTERVAL, 
retryClock::get);
   }
 
   @AfterEach
@@ -160,6 +171,37 @@ void testRatisEventsRecording() {
     assertTrue(metrics.getRatisEvents().contains("Configuration changed at 
term index"));
   }
 
+  @Test
+  void testNotifyServerShutdownTerminatesScm() {
+    
stateMachine.notifyServerShutdown(RaftProtos.RoleInfoProto.getDefaultInstance(),
 true);
+
+    verify(mockScm, timeout(5000)).shutDown(anyString());
+  }
+
+  @Test
+  void testNotifyServerShutdownExitsCleanlyWhenScmIsDecommissioned() {
+    ExitUtils.disableSystemExit();
+    ExitUtils.clear();
+    try {
+      currentPeers = 
Collections.singletonList(RaftPeer.newBuilder().setId(leaderId).build());
+      
stateMachine.notifyServerShutdown(RaftProtos.RoleInfoProto.getDefaultInstance(),
 true);
+
+      verify(mockScm, timeout(5000)).stop();
+      verify(mockScm, never()).shutDown(anyString());
+      assertThat(ExitUtils.getFirstExitException().getStatus()).isZero();
+    } finally {
+      ExitUtils.clear();
+    }
+  }
+
+  @Test
+  void testNotifyServerShutdownIgnoredWhenScmStopped() {
+    scmStopped.set(true);
+    
stateMachine.notifyServerShutdown(RaftProtos.RoleInfoProto.getDefaultInstance(),
 true);
+
+    verify(mockScm, after(500).never()).shutDown(anyString());
+  }
+
   @Test
   void testRetryStartsDNServerWhenLeaderCommitIndexBecomesAvailable() throws 
Exception {
     LogCapturer executorLogs = LogCapturer.captureLogs(ExecutorHelper.class);


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

Reply via email to