zhuzhurk commented on a change in pull request #9715: [FLINK-13993][runtime] 
Using FlinkUserCodeClassLoaders to load the user class in the perjob mode
URL: https://github.com/apache/flink/pull/9715#discussion_r329317918
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/entrypoint/component/AbstractUserClassPathJobGraphRetriever.java
 ##########
 @@ -0,0 +1,109 @@
+/*
+ * 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.entrypoint.component;
+
+import org.apache.flink.core.fs.Path;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.nio.file.FileVisitOption;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ *  Abstract class for the JobGraphRetriever, which wants to get classpath 
user's code depends on.
+ */
+
+public abstract class AbstractUserClassPathJobGraphRetriever implements 
JobGraphRetriever {
+
+       private final Logger log = LoggerFactory.getLogger(getClass());
+
+       public static final String DEFAULT_JOB_DIR = "job";
+
+       /** The directory contains all the jars, which user code depends on. */
+       @Nullable
+       private final String jobDir;
+
+       private List<URL> userClassPaths;
+
+       public AbstractUserClassPathJobGraphRetriever(String jobDir) {
+               this.jobDir = jobDir;
+       }
+
+       public List<URL> getUserClassPaths() throws IOException {
+               if (userClassPaths == null) {
+                       userClassPaths = scanJarsInJobClassDir(jobDir);
+               }
+               return userClassPaths;
+       }
+
+       private List<URL> scanJarsInJobClassDir(String dir) throws IOException {
+
+               if (dir == null) {
+                       return Collections.emptyList();
+               }
+
+               final File dirFile = new File(new Path(dir).toString());
+               final List<URL> jarURLs = new LinkedList<>();
+
+               if (!dirFile.exists()) {
+                       log.warn("the job dir " + dirFile + " dose not 
exists.");
+                       return Collections.emptyList();
+               }
+               if (!dirFile.isDirectory()) {
+                       log.warn("the job dir " + dirFile + " is not a 
directory.");
+                       return Collections.emptyList();
+               }
+
+               Files.walkFileTree(dirFile.toPath(),
 
 Review comment:
   I'm not sure whether there can be cycles in the file tree in all our cases.
   If there is a cycle, it will cause `FileSystemLoopException` and the 
`SimpleFileVisitor` will throw it directly and the job may fail.
   If we want to deal with it, we need to override 
`FileVisitResult#visitFileFailed(..)`.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to