HADOOP-15085. Output streams closed with IOUtils suppressing write errors. Contributed by Jim Brennan

This commit is contained in:
Jason Lowe 2017-12-14 10:18:08 -06:00
parent 80db744ee5
commit f8af0e2feb
8 changed files with 38 additions and 69 deletions

View File

@ -2121,17 +2121,13 @@ public boolean copy(final Path src, final Path dst, boolean deleteSource,
content.getPath().getName())), deleteSource, overwrite); content.getPath().getName())), deleteSource, overwrite);
} }
} else { } else {
InputStream in=null; EnumSet<CreateFlag> createFlag = overwrite ? EnumSet.of(
OutputStream out = null; CreateFlag.CREATE, CreateFlag.OVERWRITE) :
try { EnumSet.of(CreateFlag.CREATE);
in = open(qSrc); InputStream in = open(qSrc);
EnumSet<CreateFlag> createFlag = overwrite ? EnumSet.of( try (OutputStream out = create(qDst, createFlag)) {
CreateFlag.CREATE, CreateFlag.OVERWRITE) :
EnumSet.of(CreateFlag.CREATE);
out = create(qDst, createFlag);
IOUtils.copyBytes(in, out, conf, true); IOUtils.copyBytes(in, out, conf, true);
} finally { } finally {
IOUtils.closeStream(out);
IOUtils.closeStream(in); IOUtils.closeStream(in);
} }
} }

View File

@ -1279,15 +1279,10 @@ public static String[] createJarWithClassPath(String inputClassPath, Path pwd,
// Write the manifest to output JAR file // Write the manifest to output JAR file
File classPathJar = File.createTempFile("classpath-", ".jar", workingDir); File classPathJar = File.createTempFile("classpath-", ".jar", workingDir);
FileOutputStream fos = null; try (FileOutputStream fos = new FileOutputStream(classPathJar);
BufferedOutputStream bos = null; BufferedOutputStream bos = new BufferedOutputStream(fos)) {
JarOutputStream jos = null; JarOutputStream jos = new JarOutputStream(bos, jarManifest);
try { jos.close();
fos = new FileOutputStream(classPathJar);
bos = new BufferedOutputStream(fos);
jos = new JarOutputStream(bos, jarManifest);
} finally {
IOUtils.cleanupWithLogger(LOG, jos, bos, fos);
} }
String[] jarCp = {classPathJar.getCanonicalPath(), String[] jarCp = {classPathJar.getCanonicalPath(),
unexpandedWildcardClasspath.toString()}; unexpandedWildcardClasspath.toString()};

View File

@ -20,7 +20,6 @@
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Iterator; import java.util.Iterator;
@ -464,10 +463,8 @@ protected void processArguments(LinkedList<PathData> args)
dst.fs.create(dst.path, false).close(); dst.fs.create(dst.path, false).close();
} }
InputStream is = null; FileInputStream is = null;
FSDataOutputStream fos = dst.fs.append(dst.path); try (FSDataOutputStream fos = dst.fs.append(dst.path)) {
try {
if (readStdin) { if (readStdin) {
if (args.size() == 0) { if (args.size() == 0) {
IOUtils.copyBytes(System.in, fos, DEFAULT_IO_LENGTH); IOUtils.copyBytes(System.in, fos, DEFAULT_IO_LENGTH);
@ -488,10 +485,6 @@ protected void processArguments(LinkedList<PathData> args)
if (is != null) { if (is != null) {
IOUtils.closeStream(is); IOUtils.closeStream(is);
} }
if (fos != null) {
IOUtils.closeStream(fos);
}
} }
} }
} }

View File

@ -986,23 +986,22 @@ public static void main(String[] args) throws Exception {
Configuration conf = new Configuration(); Configuration conf = new Configuration();
FileSystem fs = FileSystem.getLocal(conf); FileSystem fs = FileSystem.getLocal(conf);
MapFile.Reader reader = null; MapFile.Reader reader = null;
MapFile.Writer writer = null;
try { try {
reader = new MapFile.Reader(fs, in, conf); reader = new MapFile.Reader(fs, in, conf);
writer =
new MapFile.Writer(conf, fs, out,
reader.getKeyClass().asSubclass(WritableComparable.class),
reader.getValueClass());
WritableComparable<?> key = ReflectionUtils.newInstance( WritableComparable<?> key = ReflectionUtils.newInstance(
reader.getKeyClass().asSubclass(WritableComparable.class), conf); reader.getKeyClass().asSubclass(WritableComparable.class), conf);
Writable value = ReflectionUtils.newInstance(reader.getValueClass() Writable value = ReflectionUtils.newInstance(reader.getValueClass()
.asSubclass(Writable.class), conf); .asSubclass(Writable.class), conf);
while (reader.next(key, value)) // copy all entries try (MapFile.Writer writer = new MapFile.Writer(conf, fs, out,
writer.append(key, value); reader.getKeyClass().asSubclass(WritableComparable.class),
reader.getValueClass())) {
while (reader.next(key, value)) { // copy all entries
writer.append(key, value);
}
}
} finally { } finally {
IOUtils.cleanupWithLogger(LOG, writer, reader); IOUtils.cleanupWithLogger(LOG, reader);
} }
} }
} }

View File

@ -956,28 +956,23 @@ public static void copyFileUnbuffered(File src, File dst) throws IOException {
if (nativeLoaded && Shell.WINDOWS) { if (nativeLoaded && Shell.WINDOWS) {
copyFileUnbuffered0(src.getAbsolutePath(), dst.getAbsolutePath()); copyFileUnbuffered0(src.getAbsolutePath(), dst.getAbsolutePath());
} else { } else {
FileInputStream fis = null; FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = null;
FileChannel input = null; FileChannel input = null;
FileChannel output = null;
try { try {
fis = new FileInputStream(src);
fos = new FileOutputStream(dst);
input = fis.getChannel(); input = fis.getChannel();
output = fos.getChannel(); try (FileOutputStream fos = new FileOutputStream(dst);
long remaining = input.size(); FileChannel output = fos.getChannel()) {
long position = 0; long remaining = input.size();
long transferred = 0; long position = 0;
while (remaining > 0) { long transferred = 0;
transferred = input.transferTo(position, remaining, output); while (remaining > 0) {
remaining -= transferred; transferred = input.transferTo(position, remaining, output);
position += transferred; remaining -= transferred;
position += transferred;
}
} }
} finally { } finally {
IOUtils.cleanupWithLogger(LOG, output); IOUtils.cleanupWithLogger(LOG, input, fis);
IOUtils.cleanupWithLogger(LOG, fos);
IOUtils.cleanupWithLogger(LOG, input);
IOUtils.cleanupWithLogger(LOG, fis);
} }
} }
} }

View File

@ -56,7 +56,6 @@
import org.apache.hadoop.conf.Configuration.IntegerRanges; import org.apache.hadoop.conf.Configuration.IntegerRanges;
import org.apache.hadoop.fs.FileUtil; import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.net.NetUtils; import org.apache.hadoop.net.NetUtils;
import org.apache.hadoop.security.alias.CredentialProvider; import org.apache.hadoop.security.alias.CredentialProvider;
import org.apache.hadoop.security.alias.CredentialProviderFactory; import org.apache.hadoop.security.alias.CredentialProviderFactory;
@ -385,11 +384,9 @@ public void testMultiByteCharacters() throws IOException {
Configuration conf = new Configuration(false); Configuration conf = new Configuration(false);
conf.addResource(new Path(CONFIG_MULTI_BYTE)); conf.addResource(new Path(CONFIG_MULTI_BYTE));
assertEquals(value, conf.get(name)); assertEquals(value, conf.get(name));
FileOutputStream fos = new FileOutputStream(CONFIG_MULTI_BYTE_SAVED); try (FileOutputStream fos =
try { new FileOutputStream(CONFIG_MULTI_BYTE_SAVED)) {
conf.writeXml(fos); conf.writeXml(fos);
} finally {
IOUtils.closeStream(fos);
} }
conf = new Configuration(false); conf = new Configuration(false);

View File

@ -362,11 +362,10 @@ private static Path writeSplitTestFile(FileSystem fs, Random rand,
final Path file = new Path(wd, "test" + codec.getDefaultExtension()); final Path file = new Path(wd, "test" + codec.getDefaultExtension());
final byte[] b = new byte[REC_SIZE]; final byte[] b = new byte[REC_SIZE];
final Base64 b64 = new Base64(0, null); final Base64 b64 = new Base64(0, null);
DataOutputStream fout = null;
Compressor cmp = CodecPool.getCompressor(codec); Compressor cmp = CodecPool.getCompressor(codec);
try { try (DataOutputStream fout =
fout = new DataOutputStream(codec.createOutputStream( new DataOutputStream(codec.createOutputStream(fs.create(file,
fs.create(file, true), cmp)); true), cmp))) {
final DataOutputBuffer dob = new DataOutputBuffer(REC_SIZE * 4 / 3 + 4); final DataOutputBuffer dob = new DataOutputBuffer(REC_SIZE * 4 / 3 + 4);
int seq = 0; int seq = 0;
while (infLen > 0) { while (infLen > 0) {
@ -382,7 +381,6 @@ private static Path writeSplitTestFile(FileSystem fs, Random rand,
} }
LOG.info("Wrote " + seq + " records to " + file); LOG.info("Wrote " + seq + " records to " + file);
} finally { } finally {
IOUtils.cleanupWithLogger(LOG, fout);
CodecPool.returnCompressor(cmp); CodecPool.returnCompressor(cmp);
} }
return file; return file;

View File

@ -105,15 +105,11 @@ public MiniKMS(String kmsConfDir, String log4ConfFile, String keyStore,
private void copyResource(String inputResourceName, File outputFile) throws private void copyResource(String inputResourceName, File outputFile) throws
IOException { IOException {
InputStream is = null; InputStream is = ThreadUtil.getResourceAsStream(inputResourceName);
OutputStream os = null; try (OutputStream os = new FileOutputStream(outputFile)) {
try {
is = ThreadUtil.getResourceAsStream(inputResourceName);
os = new FileOutputStream(outputFile);
IOUtils.copy(is, os); IOUtils.copy(is, os);
} finally { } finally {
IOUtils.closeQuietly(is); IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
} }
} }