azagrebin commented on a change in pull request #8646: [FLINK-12735][network] 
Make shuffle environment implementation independent with IOManager
URL: https://github.com/apache/flink/pull/8646#discussion_r295182426
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/io/disk/FileChannelManager.java
 ##########
 @@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.runtime.io.disk;
+
+import org.apache.flink.runtime.io.disk.iomanager.FileIOChannel.Enumerator;
+import org.apache.flink.runtime.io.disk.iomanager.FileIOChannel.ID;
+import org.apache.flink.util.FileUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Random;
+import java.util.UUID;
+
+/**
+ * The manager used for creating/deleting file channels based on config temp 
dirs.
+ */
+public class FileChannelManager implements AutoCloseable {
+       private static final Logger LOG = 
LoggerFactory.getLogger(FileChannelManager.class);
+
+       /** The temporary directories for files. */
+       private final File[] paths;
+
+       /** A random number generator for the anonymous ChannelIDs. */
+       private final Random random;
+
+       /** The number of the next path to use. */
+       private volatile int nextPath;
+
+       public FileChannelManager(String[] tempDirs) {
+               if (tempDirs == null || tempDirs.length == 0) {
+                       throw new IllegalArgumentException("The temporary 
directories must not be null or empty.");
+               }
+
+               this.random = new Random();
+               this.nextPath = 0;
+
+               this.paths = new File[tempDirs.length];
+               for (int i = 0; i < tempDirs.length; i++) {
+                       File baseDir = new File(tempDirs[i]);
+                       String subfolder = String.format("flink-io-%s", 
UUID.randomUUID().toString());
+                       File storageDir = new File(baseDir, subfolder);
+
+                       if (!storageDir.exists() && !storageDir.mkdirs()) {
+                               throw new RuntimeException(
+                                       "Could not create storage directory for 
FileChannelManager: " + storageDir.getAbsolutePath());
+                       }
+                       paths[i] = storageDir;
+
+                       LOG.info("FileChannelManager uses directory {} for 
spill files.", storageDir.getAbsolutePath());
+               }
+       }
+
+       public ID createChannel() {
+               int num = getNextPathNum();
+               return new ID(paths[num], num, random);
+       }
+
+       public Enumerator createChannelEnumerator() {
+               return new Enumerator(paths, random);
+       }
+
+       private int getNextPathNum() {
+               int next = nextPath;
+               int newNext = next + 1;
+               nextPath = newNext >= paths.length ? 0 : newNext;
+               return next;
+       }
+
+       public File[] getPaths() {
+               return paths;
+       }
+
+       /**
+        * Remove all the temp directories.
+        */
+       @Override
+       public void close() {
+               for (File path : paths) {
+                       try {
+                               if (path != null) {
+                                       if (path.exists()) {
+                                               FileUtils.deleteDirectory(path);
+                                               LOG.info("FileChannelManager 
removed spill file directory {}", path.getAbsolutePath());
+                                       }
+                               }
+                       } catch (IOException e) {
+                               LOG.error("FileChannelManager failed to 
properly clean up temp file directory: " + path, e);
 
 Review comment:
   nit: `... directory: {}", path, e);`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to