diff --git a/hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/heartbeat/HoodieHeartbeatClient.java b/hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/heartbeat/HoodieHeartbeatClient.java index 44f323b2e05f..0ada2a384fbd 100644 --- a/hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/heartbeat/HoodieHeartbeatClient.java +++ b/hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/heartbeat/HoodieHeartbeatClient.java @@ -184,13 +184,14 @@ public void start(String instantTime) { * @param instantTime The instant time for the heartbeat. * @throws HoodieException */ - public void stop(String instantTime) throws HoodieException { - Heartbeat heartbeat = instantToHeartbeatMap.get(instantTime); + public Heartbeat stop(String instantTime) throws HoodieException { + Heartbeat heartbeat = instantToHeartbeatMap.remove(instantTime); if (isHeartbeatStarted(heartbeat)) { stopHeartbeatTimer(heartbeat); HeartbeatUtils.deleteHeartbeatFile(storage, basePath, instantTime); LOG.info("Deleted heartbeat file for instant " + instantTime); } + return heartbeat; } /** diff --git a/hudi-client/hudi-client-common/src/test/java/org/apache/hudi/client/heartbeat/TestHoodieHeartbeatClient.java b/hudi-client/hudi-client-common/src/test/java/org/apache/hudi/client/heartbeat/TestHoodieHeartbeatClient.java index 7778fa1c677e..c7ea5fa87bbd 100644 --- a/hudi-client/hudi-client-common/src/test/java/org/apache/hudi/client/heartbeat/TestHoodieHeartbeatClient.java +++ b/hudi-client/hudi-client-common/src/test/java/org/apache/hudi/client/heartbeat/TestHoodieHeartbeatClient.java @@ -30,7 +30,9 @@ import static java.util.concurrent.TimeUnit.SECONDS; import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; public class TestHoodieHeartbeatClient extends HoodieCommonTestHarness { @@ -64,10 +66,10 @@ public void testStopHeartbeat() { new HoodieHeartbeatClient(metaClient.getStorage(), metaClient.getBasePath().toString(), heartBeatInterval, numTolerableMisses); hoodieHeartbeatClient.start(instantTime1); - hoodieHeartbeatClient.stop(instantTime1); - await().atMost(5, SECONDS).until(() -> hoodieHeartbeatClient.getHeartbeat(instantTime1).getNumHeartbeats() > 0); - Integer numHeartBeats = hoodieHeartbeatClient.getHeartbeat(instantTime1).getNumHeartbeats(); - assertTrue(numHeartBeats == 1); + HoodieHeartbeatClient.Heartbeat heartbeat = hoodieHeartbeatClient.stop(instantTime1); + await().atMost(5, SECONDS).until(() -> heartbeat.getNumHeartbeats() > 0); + assertEquals(1, (int) heartbeat.getNumHeartbeats()); + assertNull(hoodieHeartbeatClient.getHeartbeat(instantTime1), "Heartbeat should be removed from client cache after explicit stop"); } @Test @@ -77,7 +79,7 @@ public void testIsHeartbeatExpired() throws IOException { heartBeatInterval, numTolerableMisses); hoodieHeartbeatClient.start(instantTime1); hoodieHeartbeatClient.stop(instantTime1); - assertFalse(hoodieHeartbeatClient.isHeartbeatExpired(instantTime1)); + assertTrue(hoodieHeartbeatClient.isHeartbeatExpired(instantTime1), "The explicit stopped instant is deemed expiry for heartbeats"); } @Test @@ -91,7 +93,7 @@ public void testNumHeartbeatsGenerated() { } @Test - public void testDeleteWrongHeartbeat() throws IOException { + public void testDeleteWrongHeartbeat() { HoodieHeartbeatClient hoodieHeartbeatClient = new HoodieHeartbeatClient(metaClient.getStorage(), metaClient.getBasePath().toString(), heartBeatInterval, numTolerableMisses);