diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/BPServiceActor.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/BPServiceActor.java index ea914024699..0b3eb14dff8 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/BPServiceActor.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/BPServiceActor.java @@ -1241,7 +1241,8 @@ class BPServiceActor implements Runnable { } long getLifelineWaitTime() { - return nextLifelineTime - monotonicNow(); + long waitTime = nextLifelineTime - monotonicNow(); + return waitTime > 0 ? waitTime : 0; } /** diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestBpServiceActorScheduler.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestBpServiceActorScheduler.java index 438be89be92..0bd450bed7e 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestBpServiceActorScheduler.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestBpServiceActorScheduler.java @@ -18,6 +18,7 @@ package org.apache.hadoop.hdfs.server.datanode; +import org.apache.hadoop.util.Time; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.hadoop.hdfs.server.datanode.BPServiceActor.Scheduler; @@ -182,6 +183,18 @@ public class TestBpServiceActorScheduler { } } + @Test + public void testScheduleLifelineScheduleTime() { + Scheduler mockScheduler = spy(new Scheduler( + HEARTBEAT_INTERVAL_MS, LIFELINE_INTERVAL_MS, + BLOCK_REPORT_INTERVAL_MS, OUTLIER_REPORT_INTERVAL_MS)); + long now = Time.monotonicNow(); + mockScheduler.scheduleNextLifeline(now); + long mockMonotonicNow = now + LIFELINE_INTERVAL_MS * 2; + doReturn(mockMonotonicNow).when(mockScheduler).monotonicNow(); + assertTrue(mockScheduler.getLifelineWaitTime() >= 0); + } + @Test public void testOutlierReportScheduling() { for (final long now : getTimestamps()) {