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 373c081e75e HDDS-17831. Re-register failing SCM endpoint after 
prolonged heartbeat failures (#12060)
373c081e75e is described below

commit 373c081e75efd540bf9b4f62d31cb6bd35877e2b
Author: Sadanand Shenoy <[email protected]>
AuthorDate: Wed Nov 18 19:24:25 2026 +0530

    HDDS-17831. Re-register failing SCM endpoint after prolonged heartbeat 
failures (#12060)
---
 .../org/apache/hadoop/hdds/HddsConfigKeys.java     |   4 +
 .../common/src/main/resources/ozone-default.xml    |  10 +
 .../common/statemachine/StateContext.java          |  18 ++
 .../states/endpoint/HeartbeatEndpointTask.java     |  33 ++-
 .../TestHeartbeatEndpointTaskRecovery.java         | 226 +++++++++++++++++++++
 5 files changed, 288 insertions(+), 3 deletions(-)

diff --git 
a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/HddsConfigKeys.java 
b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/HddsConfigKeys.java
index aae7259d813..4d66b5e0901 100644
--- 
a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/HddsConfigKeys.java
+++ 
b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/HddsConfigKeys.java
@@ -43,6 +43,10 @@ public final class HddsConfigKeys {
   public static final String 
HDDS_HEARTBEAT_ADDRESS_REFRESH_MISSED_COUNT_THRESHOLD =
       "hdds.heartbeat.address.refresh.missed-count-threshold";
   public static final int 
HDDS_HEARTBEAT_ADDRESS_REFRESH_MISSED_COUNT_THRESHOLD_DEFAULT = 3;
+  /** Missed heartbeats against one SCM before the DN forces endpoint 
re-registration. */
+  public static final String HDDS_HEARTBEAT_RE_REGISTER_MISSED_COUNT_THRESHOLD 
=
+      "hdds.heartbeat.re-register.missed-count-threshold";
+  public static final int 
HDDS_HEARTBEAT_RE_REGISTER_MISSED_COUNT_THRESHOLD_DEFAULT = 100;
   public static final String HDDS_NODE_REPORT_INTERVAL =
       "hdds.node.report.interval";
   public static final String HDDS_NODE_REPORT_INTERVAL_DEFAULT =
diff --git a/hadoop-hdds/common/src/main/resources/ozone-default.xml 
b/hadoop-hdds/common/src/main/resources/ozone-default.xml
index 8b7ea051231..990b8a97ab8 100644
--- a/hadoop-hdds/common/src/main/resources/ozone-default.xml
+++ b/hadoop-hdds/common/src/main/resources/ozone-default.xml
@@ -4113,6 +4113,16 @@
     </description>
   </property>
 
+  <property>
+    <name>hdds.heartbeat.re-register.missed-count-threshold</name>
+    <value>100</value>
+    <tag>OZONE, DATANODE, HA</tag>
+    <description>Consecutive heartbeat failures the DataNode tolerates
+      against one SCM endpoint before forcing that endpoint back to
+      GETVERSION and re-running registration.
+    </description>
+  </property>
+
   <property>
     <name>ozone.directory.deleting.service.interval</name>
     <value>1m</value>
diff --git 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/statemachine/StateContext.java
 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/statemachine/StateContext.java
index ae5352169eb..56839d0a2ba 100644
--- 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/statemachine/StateContext.java
+++ 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/statemachine/StateContext.java
@@ -367,6 +367,24 @@ public void putBackReports(List<Message> reportsToPutBack,
     }
   }
 
+  /**
+   * Clears queued command status reports for one endpoint while preserving all
+   * other pending report types.
+   *
+   * @return number of queued command status report messages removed
+   */
+  public int clearCommandStatusReports(HostAndPort endpoint) {
+    synchronized (incrementalReportsQueue) {
+      List<Message> reports = incrementalReportsQueue.get(endpoint);
+      if (reports == null || reports.isEmpty()) {
+        return 0;
+      }
+      int before = reports.size();
+      reports.removeIf(CommandStatusReportsProto.class::isInstance);
+      return before - reports.size();
+    }
+  }
+
   /**
    * Returns all the available reports from the report queue, or empty list if
    * the queue is empty.
diff --git 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/endpoint/HeartbeatEndpointTask.java
 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/endpoint/HeartbeatEndpointTask.java
index 76358e4755c..6d9ca54b6d3 100644
--- 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/endpoint/HeartbeatEndpointTask.java
+++ 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/endpoint/HeartbeatEndpointTask.java
@@ -23,6 +23,8 @@
 import static 
org.apache.hadoop.hdds.HddsConfigKeys.HDDS_HEARTBEAT_ADDRESS_REFRESH_MISSED_COUNT_THRESHOLD_DEFAULT;
 import static 
org.apache.hadoop.hdds.HddsConfigKeys.HDDS_HEARTBEAT_REPORT_MAX_LIMIT;
 import static 
org.apache.hadoop.hdds.HddsConfigKeys.HDDS_HEARTBEAT_REPORT_MAX_LIMIT_DEFAULT;
+import static 
org.apache.hadoop.hdds.HddsConfigKeys.HDDS_HEARTBEAT_RE_REGISTER_MISSED_COUNT_THRESHOLD;
+import static 
org.apache.hadoop.hdds.HddsConfigKeys.HDDS_HEARTBEAT_RE_REGISTER_MISSED_COUNT_THRESHOLD_DEFAULT;
 import static 
org.apache.hadoop.hdds.HddsConfigKeys.HDDS_PIPELINE_ACTION_MAX_LIMIT;
 import static 
org.apache.hadoop.hdds.HddsConfigKeys.HDDS_PIPELINE_ACTION_MAX_LIMIT_DEFAULT;
 import static 
org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_CLIENT_FAILOVER_RESOLVE_NEEDED_DEFAULT;
@@ -90,7 +92,8 @@ public class HeartbeatEndpointTask
   // Test-only override for the version this datanode advertises to clients; 
null in production.
   private final HDDSVersion testCurrentVersion;
   private final boolean resolveOnFailureEnabled;
-  private final int refreshThreshold;
+  private final int addressRefreshThreshold;
+  private final int reRegisterThreshold;
 
   /**
    * Constructs a SCM heart beat.
@@ -122,8 +125,10 @@ public HeartbeatEndpointTask(EndpointStateMachine 
rpcEndpoint,
 
     this.resolveOnFailureEnabled = 
conf.getBoolean(OZONE_CLIENT_FAILOVER_RESOLVE_NEEDED_KEY,
         OZONE_CLIENT_FAILOVER_RESOLVE_NEEDED_DEFAULT);
-    this.refreshThreshold = Math.max(1, 
conf.getInt(HDDS_HEARTBEAT_ADDRESS_REFRESH_MISSED_COUNT_THRESHOLD,
+    this.addressRefreshThreshold = Math.max(1, 
conf.getInt(HDDS_HEARTBEAT_ADDRESS_REFRESH_MISSED_COUNT_THRESHOLD,
         HDDS_HEARTBEAT_ADDRESS_REFRESH_MISSED_COUNT_THRESHOLD_DEFAULT));
+    this.reRegisterThreshold = Math.max(1, 
conf.getInt(HDDS_HEARTBEAT_RE_REGISTER_MISSED_COUNT_THRESHOLD,
+        HDDS_HEARTBEAT_RE_REGISTER_MISSED_COUNT_THRESHOLD_DEFAULT));
   }
 
   /**
@@ -168,6 +173,7 @@ public EndpointStateMachine.EndPointStates call() throws 
Exception {
       // put back the reports which failed to be sent
       putBackIncrementalReports(requestBuilder);
       rpcEndpoint.logIfNeeded(ex);
+      resetEndpointToGetVersion();
       maybeRefreshScmAddress(ex);
     } finally {
       rpcEndpoint.unlock();
@@ -175,6 +181,27 @@ public EndpointStateMachine.EndPointStates call() throws 
Exception {
     return rpcEndpoint.getState();
   }
 
+  /**
+   * After repeated heartbeat failures, force this endpoint back to
+   * GETVERSION so it re-enters normal registration flow. To mimic DN restart
+   * behavior for this endpoint, queued command status reports are dropped.
+   */
+  private void resetEndpointToGetVersion() {
+    if (rpcEndpoint.isPassive()
+        || rpcEndpoint.getState() != EndPointStates.HEARTBEAT
+        || rpcEndpoint.getMissedCount() < reRegisterThreshold) {
+      return;
+    }
+    int droppedCommandStatusReports =
+        context.clearCommandStatusReports(rpcEndpoint.getAddress());
+    LOG.info("Resetting SCM endpoint {} to GETVERSION after {} consecutive 
heartbeat failures, "
+            + "dropped {} queued command status reports", 
rpcEndpoint.getAddress(),
+        rpcEndpoint.getMissedCount(), droppedCommandStatusReports);
+    rpcEndpoint.setState(EndPointStates.GETVERSION);
+    // trigger immediate GETVERSION
+    context.getParent().setNextHB(Time.monotonicNow());
+  }
+
   /**
    * On a connection-class heartbeat failure past the threshold (and when 
resolve-needed is on),
    * asks the connection manager to re-resolve this SCM peer and rebuild the 
endpoint.
@@ -182,7 +209,7 @@ public EndpointStateMachine.EndPointStates call() throws 
Exception {
   private void maybeRefreshScmAddress(IOException heartbeatFailure) {
     if (!resolveOnFailureEnabled
         || rpcEndpoint.isPassive()
-        || rpcEndpoint.getMissedCount() < refreshThreshold
+        || rpcEndpoint.getMissedCount() < addressRefreshThreshold
         || !ConnectionFailureUtils.isConnectionFailure(heartbeatFailure)) {
       return;
     }
diff --git 
a/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/states/endpoint/TestHeartbeatEndpointTaskRecovery.java
 
b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/states/endpoint/TestHeartbeatEndpointTaskRecovery.java
new file mode 100644
index 00000000000..3af0e038a61
--- /dev/null
+++ 
b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/states/endpoint/TestHeartbeatEndpointTaskRecovery.java
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.ozone.container.common.states.endpoint;
+
+import static 
org.apache.hadoop.hdds.HddsConfigKeys.HDDS_HEARTBEAT_ADDRESS_REFRESH_MISSED_COUNT_THRESHOLD;
+import static 
org.apache.hadoop.hdds.HddsConfigKeys.HDDS_HEARTBEAT_RE_REGISTER_MISSED_COUNT_THRESHOLD;
+import static 
org.apache.hadoop.ozone.container.common.statemachine.EndpointStateMachine.EndPointStates.GETVERSION;
+import static 
org.apache.hadoop.ozone.container.common.statemachine.EndpointStateMachine.EndPointStates.HEARTBEAT;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyLong;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.protobuf.Message;
+import java.io.IOException;
+import java.util.List;
+import java.util.UUID;
+import org.apache.hadoop.hdds.HDDSVersion;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.hdds.protocol.DatanodeDetails;
+import 
org.apache.hadoop.hdds.protocol.proto.StorageContainerDatanodeProtocolProtos.CommandStatus;
+import 
org.apache.hadoop.hdds.protocol.proto.StorageContainerDatanodeProtocolProtos.CommandStatusReportsProto;
+import 
org.apache.hadoop.hdds.protocol.proto.StorageContainerDatanodeProtocolProtos.IncrementalContainerReportProto;
+import 
org.apache.hadoop.hdds.protocol.proto.StorageContainerDatanodeProtocolProtos.SCMCommandProto;
+import org.apache.hadoop.hdds.scm.net.HostAndPort;
+import org.apache.hadoop.hdfs.util.EnumCounters;
+import org.apache.hadoop.ozone.container.common.ContainerTestUtils;
+import 
org.apache.hadoop.ozone.container.common.statemachine.DatanodeStateMachine;
+import 
org.apache.hadoop.ozone.container.common.statemachine.DatanodeStateMachine.DatanodeStates;
+import 
org.apache.hadoop.ozone.container.common.statemachine.EndpointStateMachine;
+import org.apache.hadoop.ozone.container.common.statemachine.StateContext;
+import org.apache.hadoop.ozone.container.upgrade.DatanodeVersionManager;
+import 
org.apache.hadoop.ozone.protocolPB.StorageContainerDatanodeProtocolClientSideTranslatorPB;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests heartbeat failure recovery that resets only the affected endpoint.
+ */
+public class TestHeartbeatEndpointTaskRecovery {
+
+  private static final HostAndPort SCM = new HostAndPort("test-scm-1", 9861);
+  private static final HostAndPort HEALTHY_SCM =
+      new HostAndPort("test-scm-2", 9861);
+
+  @Test
+  public void thresholdFailureResetsEndpointAndClearsCommandStatusQueue() 
throws Exception {
+    OzoneConfiguration conf = new OzoneConfiguration();
+    conf.setInt(HDDS_HEARTBEAT_RE_REGISTER_MISSED_COUNT_THRESHOLD, 1);
+    conf.setInt(HDDS_HEARTBEAT_ADDRESS_REFRESH_MISSED_COUNT_THRESHOLD, 50);
+    DatanodeStateMachine dsm = mockDatanodeStateMachine();
+    StateContext context = new StateContext(conf, DatanodeStates.RUNNING, dsm, 
"");
+    StorageContainerDatanodeProtocolClientSideTranslatorPB proxy = 
failingProxy();
+    try (EndpointStateMachine endpoint = new EndpointStateMachine(SCM, proxy, 
conf, "")) {
+      endpoint.setState(HEARTBEAT);
+      context.addEndpoint(SCM);
+      context.addIncrementalReport(newCommandStatusReport(11L));
+      
context.addIncrementalReport(IncrementalContainerReportProto.getDefaultInstance());
+
+      newHeartbeatTask(endpoint, conf, context).call();
+
+      assertEquals(GETVERSION, endpoint.getState());
+      verify(dsm).setNextHB(anyLong());
+      List<Message> pendingReports = context.getAllAvailableReports(SCM);
+      assertEquals(1, pendingReports.size());
+      assertTrue(pendingReports.get(0) instanceof 
IncrementalContainerReportProto);
+    }
+  }
+
+  @Test
+  public void belowThresholdKeepsHeartbeatAndCommandStatusQueue() throws 
Exception {
+    OzoneConfiguration conf = new OzoneConfiguration();
+    conf.setInt(HDDS_HEARTBEAT_RE_REGISTER_MISSED_COUNT_THRESHOLD, 2);
+    conf.setInt(HDDS_HEARTBEAT_ADDRESS_REFRESH_MISSED_COUNT_THRESHOLD, 50);
+    DatanodeStateMachine dsm = mockDatanodeStateMachine();
+    StateContext context = new StateContext(conf, DatanodeStates.RUNNING, dsm, 
"");
+    StorageContainerDatanodeProtocolClientSideTranslatorPB proxy = 
failingProxy();
+    try (EndpointStateMachine endpoint = new EndpointStateMachine(SCM, proxy, 
conf, "")) {
+      endpoint.setState(HEARTBEAT);
+      context.addEndpoint(SCM);
+      context.addIncrementalReport(newCommandStatusReport(12L));
+      
context.addIncrementalReport(IncrementalContainerReportProto.getDefaultInstance());
+
+      newHeartbeatTask(endpoint, conf, context).call();
+
+      assertEquals(HEARTBEAT, endpoint.getState());
+      verify(dsm, never()).setNextHB(anyLong());
+      List<Message> pendingReports = context.getAllAvailableReports(SCM);
+      assertEquals(2, pendingReports.size());
+      long commandStatusCount = pendingReports.stream()
+          .filter(CommandStatusReportsProto.class::isInstance)
+          .count();
+      assertEquals(1L, commandStatusCount);
+    }
+  }
+
+  @Test
+  public void passiveEndpointDoesNotResetOrClearCommandStatusQueue() throws 
Exception {
+    OzoneConfiguration conf = new OzoneConfiguration();
+    conf.setInt(HDDS_HEARTBEAT_RE_REGISTER_MISSED_COUNT_THRESHOLD, 1);
+    conf.setInt(HDDS_HEARTBEAT_ADDRESS_REFRESH_MISSED_COUNT_THRESHOLD, 50);
+    DatanodeStateMachine dsm = mockDatanodeStateMachine();
+    StateContext context = new StateContext(conf, DatanodeStates.RUNNING, dsm, 
"");
+    StorageContainerDatanodeProtocolClientSideTranslatorPB proxy = 
failingProxy();
+    try (EndpointStateMachine endpoint = new EndpointStateMachine(SCM, proxy, 
conf, "")) {
+      endpoint.setPassive(true);
+      endpoint.setState(HEARTBEAT);
+      context.addEndpoint(SCM);
+      context.addIncrementalReport(newCommandStatusReport(13L));
+
+      newHeartbeatTask(endpoint, conf, context).call();
+
+      assertEquals(HEARTBEAT, endpoint.getState());
+      verify(dsm, never()).setNextHB(anyLong());
+      List<Message> pendingReports = context.getAllAvailableReports(SCM);
+      assertEquals(1, pendingReports.size());
+      assertTrue(pendingReports.get(0) instanceof CommandStatusReportsProto);
+    }
+  }
+
+  @Test
+  public void failingEndpointCleanupDoesNotAffectHealthyEndpoint() throws 
Exception {
+    OzoneConfiguration conf = new OzoneConfiguration();
+    conf.setInt(HDDS_HEARTBEAT_RE_REGISTER_MISSED_COUNT_THRESHOLD, 1);
+    conf.setInt(HDDS_HEARTBEAT_ADDRESS_REFRESH_MISSED_COUNT_THRESHOLD, 50);
+    DatanodeStateMachine dsm = mockDatanodeStateMachine();
+    StateContext context = new StateContext(conf, DatanodeStates.RUNNING, dsm, 
"");
+    StorageContainerDatanodeProtocolClientSideTranslatorPB failingProxy =
+        failingProxy();
+    StorageContainerDatanodeProtocolClientSideTranslatorPB healthyProxy =
+        mock(StorageContainerDatanodeProtocolClientSideTranslatorPB.class);
+
+    try (EndpointStateMachine failingEndpoint =
+             new EndpointStateMachine(SCM, failingProxy, conf, "");
+         EndpointStateMachine healthyEndpoint =
+             new EndpointStateMachine(HEALTHY_SCM, healthyProxy, conf, "")) {
+      failingEndpoint.setState(HEARTBEAT);
+      healthyEndpoint.setState(HEARTBEAT);
+      context.addEndpoint(SCM);
+      context.addEndpoint(HEALTHY_SCM);
+      context.addIncrementalReport(newCommandStatusReport(21L));
+      
context.addIncrementalReport(IncrementalContainerReportProto.getDefaultInstance());
+
+      newHeartbeatTask(failingEndpoint, conf, context).call();
+
+      assertEquals(GETVERSION, failingEndpoint.getState());
+      assertEquals(HEARTBEAT, healthyEndpoint.getState());
+      verify(dsm).setNextHB(anyLong());
+
+      List<Message> failedEndpointReports = 
context.getAllAvailableReports(SCM);
+      assertEquals(1, failedEndpointReports.size());
+      assertTrue(failedEndpointReports.get(0) instanceof 
IncrementalContainerReportProto);
+
+      List<Message> healthyEndpointReports = 
context.getAllAvailableReports(HEALTHY_SCM);
+      assertEquals(2, healthyEndpointReports.size());
+      long healthyCommandStatusCount = healthyEndpointReports.stream()
+          .filter(CommandStatusReportsProto.class::isInstance)
+          .count();
+      long healthyIncrementalContainerCount = healthyEndpointReports.stream()
+          .filter(IncrementalContainerReportProto.class::isInstance)
+          .count();
+      assertEquals(1L, healthyCommandStatusCount);
+      assertEquals(1L, healthyIncrementalContainerCount);
+    }
+  }
+
+  private static HeartbeatEndpointTask newHeartbeatTask(EndpointStateMachine 
endpoint,
+      OzoneConfiguration conf, StateContext context) {
+    return HeartbeatEndpointTask.newBuilder()
+        .setConfig(conf)
+        .setContext(context)
+        .setEndpointStateMachine(endpoint)
+        .build();
+  }
+
+  private static StorageContainerDatanodeProtocolClientSideTranslatorPB 
failingProxy() throws IOException {
+    StorageContainerDatanodeProtocolClientSideTranslatorPB proxy =
+        mock(StorageContainerDatanodeProtocolClientSideTranslatorPB.class);
+    when(proxy.sendHeartbeat(any())).thenThrow(new IOException("scm 
unavailable"));
+    return proxy;
+  }
+
+  private static CommandStatusReportsProto newCommandStatusReport(long 
commandId) {
+    return CommandStatusReportsProto.newBuilder()
+        .addCmdStatus(CommandStatus.newBuilder()
+            .setCmdId(commandId)
+            .setType(SCMCommandProto.Type.deleteBlocksCommand)
+            .setStatus(CommandStatus.Status.EXECUTED)
+            .build())
+        .build();
+  }
+
+  private static DatanodeStateMachine mockDatanodeStateMachine() {
+    DatanodeDetails dn = DatanodeDetails.newBuilder()
+        .setUuid(UUID.randomUUID())
+        .setHostName("localhost")
+        .setIpAddress("127.0.0.1")
+        .setCurrentVersion(HDDSVersion.SOFTWARE_VERSION)
+        .build();
+    DatanodeStateMachine dsm = mock(DatanodeStateMachine.class);
+    when(dsm.getDatanodeDetails()).thenReturn(dn);
+    when(dsm.getQueuedCommandCount())
+        .thenReturn(new EnumCounters<>(SCMCommandProto.Type.class));
+    DatanodeVersionManager versionManager = 
ContainerTestUtils.mockDnVersionManager();
+    when(dsm.getVersionManager()).thenReturn(versionManager);
+    return dsm;
+  }
+}


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

Reply via email to