From de68c2a1b04872609444b4949b15423f85349a41 Mon Sep 17 00:00:00 2001 From: Zack Shoylev Date: Wed, 7 Sep 2016 15:23:36 -0500 Subject: [PATCH] Some handles were not properly closed --- .../blobstore/RegionScopedSwiftBlobStore.java | 26 +++++++-------- ...nScopedSwiftBlobStoreParallelLiveTest.java | 33 +++++++++++-------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/blobstore/RegionScopedSwiftBlobStore.java b/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/blobstore/RegionScopedSwiftBlobStore.java index b9630e128b..e902c5a8c6 100644 --- a/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/blobstore/RegionScopedSwiftBlobStore.java +++ b/apis/openstack-swift/src/main/java/org/jclouds/openstack/swift/v1/blobstore/RegionScopedSwiftBlobStore.java @@ -41,6 +41,7 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; +import javax.annotation.Resource; import javax.inject.Inject; import javax.inject.Named; @@ -76,6 +77,7 @@ import org.jclouds.io.ContentMetadata; import org.jclouds.io.Payload; import org.jclouds.io.PayloadSlicer; import org.jclouds.io.payloads.ByteSourcePayload; +import org.jclouds.logging.Logger; import org.jclouds.openstack.swift.v1.SwiftApi; import org.jclouds.openstack.swift.v1.blobstore.functions.ToBlobMetadata; import org.jclouds.openstack.swift.v1.blobstore.functions.ToListContainerOptions; @@ -89,6 +91,7 @@ import org.jclouds.openstack.swift.v1.features.BulkApi; import org.jclouds.openstack.swift.v1.features.ObjectApi; import org.jclouds.openstack.swift.v1.options.UpdateContainerOptions; import org.jclouds.openstack.swift.v1.reference.SwiftHeaders; +import org.jclouds.util.Closeables2; import com.google.common.annotations.Beta; import com.google.common.base.Function; @@ -152,6 +155,9 @@ public class RegionScopedSwiftBlobStore implements BlobStore { protected final PayloadSlicer slicer; protected final ListeningExecutorService userExecutor; + @Resource + protected Logger logger = Logger.NULL; + @Override public Set listAssignableLocations() { return ImmutableSet.of(region); @@ -701,14 +707,11 @@ public class RegionScopedSwiftBlobStore implements BlobStore { Futures.getUnchecked(Futures.allAsList(results)); } catch (IOException e) { - // cleanup, attempt to delete large file - if (raf != null) { - try { - raf.close(); - } catch (IOException e1) {} - } + Closeables2.closeQuietly(raf); destination.delete(); throw new RuntimeException(e); + } finally { + Closeables2.closeQuietly(raf); } } @@ -798,7 +801,7 @@ public class RegionScopedSwiftBlobStore implements BlobStore { long from; for (from = 0; from < contentLength; from = from + partSize) { try { - System.out.println(Thread.currentThread() + " writing to output"); + logger.debug(Thread.currentThread() + " writing to output"); result = results.take(); if (result == null) { output.close(); @@ -807,7 +810,7 @@ public class RegionScopedSwiftBlobStore implements BlobStore { } output.write(result.get()); } catch (Exception e) { - System.out.println(e); + logger.debug(e.toString()); try { // close pipe so client is notified of an exception input.close(); @@ -819,10 +822,7 @@ public class RegionScopedSwiftBlobStore implements BlobStore { } } // Finished writing results to stream - try { - output.close(); - } catch (IOException e) { - } + Closeables2.closeQuietly(output); } }); @@ -866,7 +866,7 @@ public class RegionScopedSwiftBlobStore implements BlobStore { byte[] downloadedBlock = ByteStreams.toByteArray(object.getPayload().openStream()); return downloadedBlock; } catch (IOException e) { - System.out.println(e); + logger.debug(e.toString()); lastException = e; continue; } diff --git a/apis/openstack-swift/src/test/java/org/jclouds/openstack/swift/v1/blobstore/RegionScopedSwiftBlobStoreParallelLiveTest.java b/apis/openstack-swift/src/test/java/org/jclouds/openstack/swift/v1/blobstore/RegionScopedSwiftBlobStoreParallelLiveTest.java index e7a4fed5f1..dd48612838 100644 --- a/apis/openstack-swift/src/test/java/org/jclouds/openstack/swift/v1/blobstore/RegionScopedSwiftBlobStoreParallelLiveTest.java +++ b/apis/openstack-swift/src/test/java/org/jclouds/openstack/swift/v1/blobstore/RegionScopedSwiftBlobStoreParallelLiveTest.java @@ -40,6 +40,7 @@ import org.jclouds.blobstore.BlobStore; import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.integration.internal.BaseBlobStoreIntegrationTest; import org.jclouds.io.payloads.FilePayload; +import org.jclouds.util.Closeables2; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -147,24 +148,28 @@ public class RegionScopedSwiftBlobStoreParallelLiveTest extends BaseBlobStoreInt // Reserve space for performance reasons raf = new RandomAccessFile(file.getAbsoluteFile(), "rw"); - raf.seek(size - 1); - raf.write(0); + try { + raf.seek(size - 1); + raf.write(0); - // Loop through ranges within the file - long from; - long to; - long partSize = 1000000; + // Loop through ranges within the file + long from; + long to; + long partSize = 1000000; - ExecutorService threadPool = Executors.newFixedThreadPool(16); + ExecutorService threadPool = Executors.newFixedThreadPool(16); - for (from = 0; from < size; from = from + partSize) { - to = (from + partSize >= size) ? size - 1 : from + partSize - 1; - RandomFileWriter writer = new RandomFileWriter(raf, from, to); - threadPool.submit(writer); + for (from = 0; from < size; from = from + partSize) { + to = (from + partSize >= size) ? size - 1 : from + partSize - 1; + RandomFileWriter writer = new RandomFileWriter(raf, from, to); + threadPool.submit(writer); + } + + threadPool.shutdown(); + threadPool.awaitTermination(1, TimeUnit.DAYS); + } finally { + Closeables2.closeQuietly(raf); } - - threadPool.shutdown(); - threadPool.awaitTermination(1, TimeUnit.DAYS); } private final class RandomFileWriter implements Runnable {