Repository: flink
Updated Branches:
  refs/heads/master 0ccd1fd5b -> e766dba36


[FLINK-1482] Add shutdown hooks to delete blob storage directories


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/e766dba3
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/e766dba3
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/e766dba3

Branch: refs/heads/master
Commit: e766dba3653bb942ff66bd9edc999237a77c2ea2
Parents: 0ccd1fd
Author: Ufuk Celebi <[email protected]>
Authored: Thu Feb 5 17:38:35 2015 +0100
Committer: Ufuk Celebi <[email protected]>
Committed: Thu Feb 5 18:20:02 2015 +0100

----------------------------------------------------------------------
 .../apache/flink/runtime/blob/BlobCache.java    |  3 +++
 .../apache/flink/runtime/blob/BlobServer.java   |  3 +++
 .../apache/flink/runtime/blob/BlobUtils.java    | 23 +++++++++++++++++++-
 3 files changed, 28 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/e766dba3/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobCache.java
----------------------------------------------------------------------
diff --git 
a/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobCache.java 
b/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobCache.java
index 0f57ea3..ef6d9e7 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobCache.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobCache.java
@@ -55,6 +55,9 @@ public final class BlobCache implements BlobService {
                this.storageDir = BlobUtils.initStorageDirectory();
 
                LOG.info("Created BLOB cache storage directory " + storageDir);
+
+               // Add shutdown hook to delete storage directory
+               BlobUtils.addDeleteDirectoryShutdownHook(storageDir);
        }
 
        /**

http://git-wip-us.apache.org/repos/asf/flink/blob/e766dba3/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobServer.java
----------------------------------------------------------------------
diff --git 
a/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobServer.java 
b/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobServer.java
index e47a178..43e2d15 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobServer.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobServer.java
@@ -113,6 +113,9 @@ public final class BlobServer extends Thread implements 
BlobService{
                        this.storageDir = BlobUtils.initStorageDirectory();
 
                        LOG.info("Created BLOB server storage directory " + 
storageDir);
+
+                       // Add shutdown hook to delete storage directory
+                       BlobUtils.addDeleteDirectoryShutdownHook(storageDir);
                }
                catch (IOException e) {
                        throw new IOException("Could not create BlobServer with 
random port.", e);

http://git-wip-us.apache.org/repos/asf/flink/blob/e766dba3/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobUtils.java
----------------------------------------------------------------------
diff --git 
a/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobUtils.java 
b/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobUtils.java
index dec574d..2b604d1 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobUtils.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobUtils.java
@@ -31,6 +31,8 @@ import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.UUID;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 public class BlobUtils {
        /**
         * Algorithm to be used for calculating the BLOB keys.
@@ -52,7 +54,6 @@ public class BlobUtils {
         */
        static final Charset DEFAULT_CHARSET = Charset.forName("utf-8");
 
-
        /**
         * Creates a storage directory for a blob service.
         *
@@ -192,4 +193,24 @@ public class BlobUtils {
                        throw new RuntimeException(e);
                }
        }
+
+       /**
+        * Adds a shutdown hook to the JVM to delete the given directory.
+        */
+       static void addDeleteDirectoryShutdownHook(final File dir) {
+               checkNotNull(dir);
+
+               // Add shutdown hook to delete directory
+               Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
+                       @Override
+                       public void run() {
+                               try {
+                                       FileUtils.deleteDirectory(dir);
+                               }
+                               catch (IOException e) {
+                                       throw new RuntimeException("Error 
deleting directory " + dir + " during JVM shutdown: " + e.getMessage(), e);
+                               }
+                       }
+               }));
+       }
 }

Reply via email to