Skip to content

Commit a60f769

Browse files
committed
HADOOP-19131. Wrapped IO
move the unchecking as default methods in CallableRaisingIOE, FunctionRaisingIOE etc makes for a clean and flexible design. some test enhancements. Change-Id: If25b6d0377bc9e4e8d4a6e689692ddfa96b1c756
1 parent 9d9ae33 commit a60f769

File tree

7 files changed

+88
-34
lines changed

7 files changed

+88
-34
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/wrappedio/WrappedStatistics.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818

1919
package org.apache.hadoop.io.wrappedio;
2020

21-
import java.io.IOException;
2221
import java.io.Serializable;
2322
import java.io.UncheckedIOException;
2423
import java.util.HashMap;
2524
import java.util.Map;
26-
import java.util.function.Function;
2725
import javax.annotation.Nullable;
2826

2927
import org.apache.hadoop.classification.InterfaceAudience;
@@ -120,7 +118,7 @@ public static Serializable iostatisticsSnapshot_create() {
120118
* Create a new {@link IOStatisticsSnapshot} instance.
121119
* @param source optional source statistics
122120
* @return an IOStatisticsSnapshot.
123-
* @throws ClassCastException if the {@code source}
121+
* @throws ClassCastException if the {@code source} is not null and not an IOStatistics instance
124122
*/
125123
public static Serializable iostatisticsSnapshot_create(@Nullable Object source) {
126124
return new IOStatisticsSnapshot((IOStatistics) source);
@@ -342,21 +340,18 @@ public static <T> T applyToIOStatisticsSnapshot(
342340
Serializable source,
343341
FunctionRaisingIOE<IOStatisticsSnapshot, T> fun) {
344342

345-
requireIOStatisticsSnapshot(source);
346-
try {
347-
return fun.apply((IOStatisticsSnapshot) source);
348-
} catch (IOException e) {
349-
throw new UncheckedIOException(e);
350-
}
343+
return fun.unchecked(requireIOStatisticsSnapshot(source));
351344
}
352345

353346
/**
354347
* Require the parameter to be an instance of {@link IOStatisticsSnapshot}.
355-
* @param snapshot class to validate
348+
* @param snapshot object to validate
349+
* @return cast value
356350
* @throws IllegalArgumentException if the supplied class is not a snapshot
357351
*/
358-
private static void requireIOStatisticsSnapshot(final Serializable snapshot) {
352+
private static IOStatisticsSnapshot requireIOStatisticsSnapshot(final Serializable snapshot) {
359353
checkArgument(snapshot instanceof IOStatisticsSnapshot,
360354
"Not an IOStatisticsSnapshot %s", snapshot);
355+
return (IOStatisticsSnapshot) snapshot;
361356
}
362357
}

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/functional/BiFunctionRaisingIOE.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.hadoop.util.functional;
2020

2121
import java.io.IOException;
22+
import java.io.UncheckedIOException;
2223

2324
/**
2425
* Function of arity 2 which may raise an IOException.
@@ -37,4 +38,19 @@ public interface BiFunctionRaisingIOE<T, U, R> {
3738
* @throws IOException Any IO failure
3839
*/
3940
R apply(T t, U u) throws IOException;
41+
42+
/**
43+
* Apply unchecked.
44+
* @param t argument
45+
* @param u argument 2
46+
* @return the evaluated function
47+
* @throws UncheckedIOException IOE raised.
48+
*/
49+
default R unchecked(T t, U u) {
50+
try {
51+
return apply(t, u);
52+
} catch (IOException e) {
53+
throw new UncheckedIOException(e);
54+
}
55+
}
4056
}

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/functional/CallableRaisingIOE.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.hadoop.util.functional;
2020

2121
import java.io.IOException;
22+
import java.io.UncheckedIOException;
2223

2324
/**
2425
* This is a callable which only raises an IOException.
@@ -33,4 +34,18 @@ public interface CallableRaisingIOE<R> {
3334
* @throws IOException Any IO failure
3435
*/
3536
R apply() throws IOException;
37+
38+
/**
39+
* Apply unchecked.
40+
* @return the evaluated call
41+
* @throws UncheckedIOException IOE raised.
42+
*/
43+
default R unchecked() {
44+
try {
45+
return apply();
46+
} catch (IOException e) {
47+
throw new UncheckedIOException(e);
48+
}
49+
}
50+
3651
}

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/functional/FunctionRaisingIOE.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.hadoop.util.functional;
2020

2121
import java.io.IOException;
22+
import java.io.UncheckedIOException;
2223

2324
/**
2425
* Function of arity 1 which may raise an IOException.
@@ -35,4 +36,18 @@ public interface FunctionRaisingIOE<T, R> {
3536
* @throws IOException Any IO failure
3637
*/
3738
R apply(T t) throws IOException;
39+
40+
/**
41+
* Apply unchecked.
42+
* @param t argument
43+
* @return the evaluated function
44+
* @throws UncheckedIOException IOE raised.
45+
*/
46+
default R unchecked(T t) {
47+
try {
48+
return apply(t);
49+
} catch (IOException e) {
50+
throw new UncheckedIOException(e);
51+
}
52+
}
3853
}

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/functional/FunctionalIO.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.IOException;
2222
import java.io.UncheckedIOException;
23+
import java.util.function.Function;
2324
import java.util.function.Supplier;
2425

2526
import org.apache.hadoop.classification.InterfaceAudience;
@@ -42,11 +43,7 @@ private FunctionalIO() {
4243
* @throws UncheckedIOException if an IOE was raised.
4344
*/
4445
public static <T> T uncheckIOExceptions(CallableRaisingIOE<T> call) {
45-
try {
46-
return call.apply();
47-
} catch (IOException e) {
48-
throw new UncheckedIOException(e);
49-
}
46+
return call.unchecked();
5047
}
5148

5249
/**
@@ -56,7 +53,7 @@ public static <T> T uncheckIOExceptions(CallableRaisingIOE<T> call) {
5653
* @return a supplier which invokes the call.
5754
*/
5855
public static <T> Supplier<T> toUncheckedIOExceptionSupplier(CallableRaisingIOE<T> call) {
59-
return () -> uncheckIOExceptions(call);
56+
return call::unchecked;
6057
}
6158

6259
/**
@@ -75,4 +72,17 @@ public static <T> T extractIOExceptions(Supplier<T> call) throws IOException {
7572
}
7673
}
7774

75+
76+
/**
77+
* Convert a {@link FunctionRaisingIOE} as a {@link Supplier}.
78+
* @param fun function to wrap
79+
* @param <T> type of result
80+
* @return a new function which invokes the inner function and wraps
81+
* exceptions.
82+
*/
83+
public static <T, R> Function<T, R> toUncheckedFunction(FunctionRaisingIOE<T, R> fun) {
84+
return fun::unchecked;
85+
}
86+
87+
7888
}

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/wrappedio/impl/TestWrappedIO.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
import static org.apache.hadoop.util.functional.Tuples.pair;
5959

6060
/**
61-
* Test wrapped operations.
61+
* Test WrappedIO operations.
6262
* <p>
6363
* This is a contract test; the base class is bonded to the local fs;
6464
* it is possible for other stores to implement themselves.
@@ -291,7 +291,6 @@ public void testFilesystemIOStatistics() throws Throwable {
291291
final Serializable roundTripped = statistics.iostatisticsSnapshot_fromJsonString(
292292
status);
293293

294-
295294
final Path path = methodPath();
296295
statistics.iostatisticsSnapshot_save(roundTripped, fs, path, true);
297296
final Serializable loaded = statistics.iostatisticsSnapshot_load(fs, path);

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/wrappedio/impl/TestWrappedStatistics.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,11 @@ public class TestWrappedStatistics extends AbstractHadoopTestBase {
6666
*/
6767
private final DynamicWrappedStatistics statistics = new DynamicWrappedStatistics();
6868

69-
/**
70-
* temp dir for saving snapshots.
71-
*/
72-
private File tempDir;
73-
7469
/**
7570
* Local FS.
7671
*/
7772
private LocalFileSystem local;
7873

79-
/**
80-
* Temporary file.
81-
*/
82-
private File jsonFile;
83-
8474
/**
8575
* Path to temporary file.
8676
*/
@@ -89,9 +79,10 @@ public class TestWrappedStatistics extends AbstractHadoopTestBase {
8979
@Before
9080
public void setUp() throws Exception {
9181
String testDataDir = new FileSystemTestHelper().getTestRootDir();
92-
tempDir = new File(testDataDir);
82+
File tempDir = new File(testDataDir);
9383
local = FileSystem.getLocal(new Configuration());
94-
jsonFile = new File(tempDir, "snapshot.json");
84+
// Temporary file.
85+
File jsonFile = new File(tempDir, "snapshot.json");
9586
jsonPath = new Path(jsonFile.toURI());
9687
}
9788

@@ -457,8 +448,14 @@ public void testMissingIOStatisticsMethods() throws Throwable {
457448

458449
}
459450

451+
460452
/**
461-
* Bind to an empty class to simulate a runtime where none of the methods were found
453+
* Empty class to bind against and ensure all methods fail to bind.
454+
*/
455+
private static final class StubClass { }
456+
457+
/**
458+
* Bind to {@link StubClass} to simulate a runtime where none of the methods were found
462459
* through reflection, and verify the expected failure semantics.
463460
*/
464461
@Test
@@ -482,10 +479,17 @@ public void testMissingContextMethods() throws Throwable {
482479
missing.iostatisticsContext_setThreadIOStatisticsContext(null));
483480
}
484481

482+
485483
/**
486-
* Empty class to bind against and ensure all methods fail to bind.
484+
* Validate class checks in {@code iostatisticsSnapshot_aggregate()}.
487485
*/
488-
private static final class StubClass { }
486+
@Test
487+
public void testStatisticCasting() throws Throwable {
488+
Serializable iostats = statistics.iostatisticsSnapshot_create(null);
489+
final String wrongType = "wrong type";
490+
intercept(IllegalArgumentException.class, () ->
491+
statistics.iostatisticsSnapshot_aggregate(iostats, wrongType));
492+
}
489493

490494
}
491495

0 commit comments

Comments
 (0)