n3nash commented on a change in pull request #2359: URL: https://github.com/apache/hudi/pull/2359#discussion_r569154530
########## File path: hudi-client/hudi-client-common/src/test/java/org/apache/hudi/client/heartbeat/TestHoodieHeartbeatClient.java ########## @@ -0,0 +1,145 @@ +/* + * 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.hudi.client.heartbeat; + +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.Path; +import org.apache.hudi.common.testutils.HoodieCommonTestHarness; +import org.apache.hudi.common.util.HeartbeatUtils; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.OutputStream; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TestHoodieHeartbeatClient extends HoodieCommonTestHarness { + + private static final Logger LOG = LogManager.getLogger(TestHoodieHeartbeatClient.class); + + private static String instantTime1 = "100"; + private static String instantTime2 = "101"; + private static Long heartBeatInterval = 1000L; + private static int numTolerableMisses = 1; + + @BeforeEach + public void init() throws IOException { + initMetaClient(); + } + + @Test + public void testStartHeartbeat() throws IOException { + HoodieHeartbeatClient hoodieHeartbeatClient = + new HoodieHeartbeatClient(metaClient.getFs(), metaClient.getBasePath(), heartBeatInterval, numTolerableMisses); + hoodieHeartbeatClient.start(instantTime1); + FileStatus [] fs = metaClient.getFs().listStatus(new Path(hoodieHeartbeatClient.getHeartbeatFolderPath())); + assertTrue(fs.length == 1); + assertTrue(fs[0].getPath().toString().contains(instantTime1)); + } + + @Test + public void testStopHeartbeat() { + HoodieHeartbeatClient hoodieHeartbeatClient = + new HoodieHeartbeatClient(metaClient.getFs(), metaClient.getBasePath(), 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); + } + + @Test + public void testIsHeartbeatExpired() throws IOException { + HoodieHeartbeatClient hoodieHeartbeatClient = + new HoodieHeartbeatClient(metaClient.getFs(), metaClient.getBasePath(), heartBeatInterval, numTolerableMisses); + hoodieHeartbeatClient.start(instantTime1); + hoodieHeartbeatClient.stop(instantTime1); + assertFalse(hoodieHeartbeatClient.isHeartbeatExpired(instantTime1)); + } + + @Test + public void testNumHeartbeatsGenerated() { + Long heartBeatInterval = 5000L; + HoodieHeartbeatClient hoodieHeartbeatClient = + new HoodieHeartbeatClient(metaClient.getFs(), metaClient.getBasePath(), heartBeatInterval, numTolerableMisses); + hoodieHeartbeatClient.start("100"); + await().atMost(5, SECONDS).until(() -> hoodieHeartbeatClient.getHeartbeat(instantTime1).getNumHeartbeats() >= 1); + } + + @Test + public void testDeleteWrongHeartbeat() throws IOException { + HoodieHeartbeatClient hoodieHeartbeatClient = + new HoodieHeartbeatClient(metaClient.getFs(), metaClient.getBasePath(), heartBeatInterval, numTolerableMisses); + hoodieHeartbeatClient.start(instantTime1); + hoodieHeartbeatClient.stop(instantTime1); + assertFalse(HeartbeatUtils.deleteHeartbeatFile(metaClient.getFs(), basePath, instantTime2)); + } + + @Disabled + public void testConcurrentReadsAndWritesToSameFile() throws InterruptedException { + Thread th1 = new Thread(() -> { + try { + while (true) { + OutputStream outputStream = metaClient.getFs().create(new Path(metaClient.getBasePath() + "/test"), true); + outputStream.write("testing".getBytes()); + outputStream.close(); + } + } catch (Exception e) { + LOG.info("Caught Exception th1"); + e.printStackTrace(); + } + }); + + Thread th2 = new Thread(() -> { + try { + while (true) { + FileStatus fs = metaClient.getFs().getFileStatus(new Path(metaClient.getBasePath() + "/test")); + } + } catch (Exception e) { + LOG.info("Caught Exception th2"); + e.printStackTrace(); + } + }); + + Thread th3 = new Thread(() -> { + try { + while (true) { + String heartbeat = org.apache.commons.io.IOUtils.toString(metaClient.getFs().open(new Path(metaClient.getBasePath() + "/test"))); + } + } catch (Exception e) { + LOG.info("Caught Exception th3"); + e.printStackTrace(); + } + }); + + th1.start(); + Thread.sleep(100); + th2.start(); + th3.start(); + + // To simulate the issues of race conditions when creating & reading file, simply do th1.join; th2.join; th3.join; Review comment: I left the test disabled since I was just using this for testing. Have enabled the test now and added the join back. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
