From 6c95fc90eefa08e44ea4f2f7601ce59d21f0921b Mon Sep 17 00:00:00 2001 From: "Doroszlai, Attila" <6454655+adoroszlai@users.noreply.github.com> Date: Tue, 12 Mar 2024 11:46:24 +0100 Subject: [PATCH] HDDS-10502. Improve generic type of IOUtils.close (#6361) --- .../main/java/org/apache/hadoop/hdds/utils/IOUtils.java | 4 ++-- .../java/org/apache/hadoop/ozone/om/ListIterator.java | 2 +- .../ozone/om/snapshot/TestSnapshotDiffManager.java | 9 ++------- .../java/org/apache/hadoop/ozone/debug/ReadReplicas.java | 3 +-- .../org/apache/hadoop/ozone/freon/DatanodeSimulator.java | 4 ++-- 5 files changed, 8 insertions(+), 14 deletions(-) diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/IOUtils.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/IOUtils.java index 4620a483385..6a234ab5064 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/IOUtils.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/IOUtils.java @@ -67,7 +67,7 @@ public static void close(Logger logger, AutoCloseable... closeables) { * Close each argument, catching exceptions and logging them as error. */ public static void close(Logger logger, - Collection closeables) { + Collection closeables) { if (closeables == null) { return; } @@ -94,7 +94,7 @@ public static void closeQuietly(AutoCloseable... closeables) { /** * Close each argument, swallowing exceptions. */ - public static void closeQuietly(Collection closeables) { + public static void closeQuietly(Collection closeables) { close(null, closeables); } } diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/ListIterator.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/ListIterator.java index 7981222c4c6..86d8352697a 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/ListIterator.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/ListIterator.java @@ -342,7 +342,7 @@ public HeapEntry next() { } public void close() throws IOException { - iterators.forEach(IOUtils::closeQuietly); + IOUtils.closeQuietly(iterators); } } } diff --git a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotDiffManager.java b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotDiffManager.java index 654a89bc335..c0a85c787d5 100644 --- a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotDiffManager.java +++ b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotDiffManager.java @@ -391,13 +391,8 @@ public OmSnapshot load(@Nonnull UUID key) { @AfterEach public void tearDown() { IOUtils.closeQuietly(snapshotDiffManager); - if (columnFamilyHandles != null) { - columnFamilyHandles.forEach(IOUtils::closeQuietly); - } - - IOUtils.closeQuietly(db); - IOUtils.closeQuietly(dbOptions); - IOUtils.closeQuietly(columnFamilyOptions); + IOUtils.closeQuietly(columnFamilyHandles); + IOUtils.closeQuietly(db, dbOptions, columnFamilyOptions); } private OmSnapshot getMockedOmSnapshot(UUID snapshotId) { diff --git a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/ReadReplicas.java b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/ReadReplicas.java index 30f2b4eca1f..ac9fc7854a7 100644 --- a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/ReadReplicas.java +++ b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/ReadReplicas.java @@ -216,8 +216,7 @@ private void downloadReplicasAndCreateManifest( blockJson.add(JSON_PROPERTY_BLOCK_REPLICAS, replicasJson); blocks.add(blockJson); - blockReplicasWithoutChecksum.values() - .forEach(each -> IOUtils.close(LOG, each)); + IOUtils.close(LOG, blockReplicasWithoutChecksum.values()); } } diff --git a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/freon/DatanodeSimulator.java b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/freon/DatanodeSimulator.java index 20800757b1a..2b178ac0aec 100644 --- a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/freon/DatanodeSimulator.java +++ b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/freon/DatanodeSimulator.java @@ -17,7 +17,6 @@ package org.apache.hadoop.ozone.freon; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.RandomUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hdds.DatanodeVersion; @@ -43,6 +42,7 @@ import org.apache.hadoop.hdds.upgrade.HDDSLayoutVersionManager; import org.apache.hadoop.hdds.utils.HAUtils; import org.apache.hadoop.hdds.utils.HddsServerUtil; +import org.apache.hadoop.hdds.utils.IOUtils; import org.apache.hadoop.hdds.utils.LegacyHadoopConfigurationSource; import org.apache.hadoop.io.retry.RetryPolicies; import org.apache.hadoop.io.retry.RetryPolicy; @@ -186,7 +186,7 @@ public Void call() throws Exception { } catch (InterruptedException e) { throw new RuntimeException(e); } - scmClients.values().forEach(IOUtils::closeQuietly); + IOUtils.closeQuietly(scmClients.values()); IOUtils.closeQuietly(reconClient); LOGGER.info("Successfully closed all the used resources"); saveDatanodesToFile();