Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#4483] add error retry to TestFetchFileUtils (#4511)

Naresh-kumar-Thodupunoori7e61d0d21d7f
diff --git a/catalogs/catalog-hive/src/test/java/org/apache/gravitino/catalog/hive/TestFetchFileUtils.java b/catalogs/catalog-hive/src/test/java/org/apache/gravitino/catalog/hive/TestFetchFileUtils.java
index 7c4d153dee..f949f72496 100644
--- a/catalogs/catalog-hive/src/test/java/org/apache/gravitino/catalog/hive/TestFetchFileUtils.java
+++ b/catalogs/catalog-hive/src/test/java/org/apache/gravitino/catalog/hive/TestFetchFileUtils.java
@@ -20,32 +20,74 @@
package org.apache.gravitino.catalog.hive;
import java.io.File;
+import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class TestFetchFileUtils {
+ private static final Logger LOG = LoggerFactory.getLogger(TestFetchFileUtils.class);
+ private static final int MAX_RETRIES = 3;
+ private static final long INITIAL_RETRY_DELAY_MS = 1000;
+
@Test
public void testLinkLocalFile() throws Exception {
-
File srcFile = new File("test");
File destFile = new File("dest");
- srcFile.createNewFile();
- FetchFileUtils.fetchFileFromUri(srcFile.toURI().toString(), destFile, 10, new Configuration());
- Assertions.assertTrue(destFile.exists());
-
- srcFile.delete();
- destFile.delete();
+ try {
+ if (srcFile.createNewFile()) {
+ FetchFileUtils.fetchFileFromUri(
+ srcFile.toURI().toString(), destFile, 10, new Configuration());
+ Assertions.assertTrue(destFile.exists(), "Destination file should exist after linking");
+ } else {
+ Assertions.fail("Failed to create the source file");
+ }
+ } finally {
+ if (!srcFile.delete()) {
+ LOG.warn("Failed to delete source file after test");
+ }
+ if (!destFile.delete()) {
+ LOG.warn("Failed to delete destination file after test");
+ }
+ }
}
@Test
public void testDownloadFromHTTP() throws Exception {
File destFile = new File("dest");
- FetchFileUtils.fetchFileFromUri(
- "https://downloads.apache.org/hadoop/common/KEYS", destFile, 10, new Configuration());
- Assertions.assertTrue(destFile.exists());
- destFile.delete();
+ String fileUrl = "https://downloads.apache.org/hadoop/common/KEYS";
+ Configuration conf = new Configuration();
+
+ boolean success = false;
+ int attempts = 0;
+
+ while (!success && attempts < MAX_RETRIES) {
+ try {
+ LOG.info("Attempting to download file from URL: {} (Attempt {})", fileUrl, attempts + 1);
+ FetchFileUtils.fetchFileFromUri(fileUrl, destFile, 10, conf);
+ success = true;
+ LOG.info("File downloaded successfully on attempt {}", attempts + 1);
+ } catch (IOException e) {
+ attempts++;
+ LOG.error("Download attempt {} failed due to: {}", attempts, e.getMessage(), e);
+ if (attempts < MAX_RETRIES) {
+ long retryDelay = INITIAL_RETRY_DELAY_MS * (1L << (attempts - 1));
+ LOG.warn("Retrying in {}ms", retryDelay);
+ Thread.sleep(retryDelay);
+ } else {
+ throw new AssertionError("Failed to download file after " + MAX_RETRIES + " attempts", e);
+ }
+ }
+ }
+
+ Assertions.assertTrue(destFile.exists(), "File should exist after successful download");
+
+ if (!destFile.delete()) {
+ LOG.warn("Failed to delete destination file after test");
+ }
}
}