cameronlee314 commented on a change in pull request #1173: SAMZA-2333: [AM 
isolation] Use cytodynamics classloader to launch job coordinator
URL: https://github.com/apache/samza/pull/1173#discussion_r333262292
 
 

 ##########
 File path: 
samza-core/src/main/java/org/apache/samza/classloader/IsolatingClassLoaderFactory.java
 ##########
 @@ -0,0 +1,195 @@
+/*
+ * 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.samza.classloader;
+
+import com.google.common.collect.ImmutableSet;
+import com.linkedin.cytodynamics.matcher.BootstrapClassPredicate;
+import com.linkedin.cytodynamics.matcher.GlobMatcher;
+import com.linkedin.cytodynamics.nucleus.DelegateRelationship;
+import com.linkedin.cytodynamics.nucleus.DelegateRelationshipBuilder;
+import com.linkedin.cytodynamics.nucleus.IsolationLevel;
+import com.linkedin.cytodynamics.nucleus.LoaderBuilder;
+import com.linkedin.cytodynamics.nucleus.OriginRestriction;
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.samza.SamzaException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Use this to build an isolating classloader for running Samza.
+ */
+public class IsolatingClassLoaderFactory {
+  private static final Logger LOG = 
LoggerFactory.getLogger(IsolatingClassLoaderFactory.class);
+
+  public ClassLoader buildClassLoader() {
+    String baseDirectoryPath = System.getProperty("user.dir");
+    File apiLibDirectory =
+        libDirectory(new File(baseDirectoryPath, 
IsolationUtils.APPLICATION_MASTER_API_DIRECTORY));
+    LOG.info("Using API lib directory: {}", apiLibDirectory);
+    File infrastructureLibDirectory =
+        libDirectory(new File(baseDirectoryPath, 
IsolationUtils.APPLICATION_MASTER_INFRASTRUCTURE_DIRECTORY));
+    LOG.info("Using infrastructure lib directory: {}", 
infrastructureLibDirectory);
+    File applicationLibDirectory =
+        libDirectory(new File(baseDirectoryPath, 
IsolationUtils.APPLICATION_MASTER_APPLICATION_DIRECTORY));
+    LOG.info("Using application lib directory: {}", applicationLibDirectory);
+
+    ClassLoader apiClassLoader = buildApiClassLoader(apiLibDirectory);
+    ClassLoader applicationClassLoader =
+        buildApplicationClassLoader(applicationLibDirectory, apiLibDirectory, 
apiClassLoader);
+
+    // the classloader to ultimately return is the one with the infrastructure 
classpath
+    return buildInfrastructureClassLoader(infrastructureLibDirectory, 
apiLibDirectory, apiClassLoader,
+        applicationClassLoader);
+  }
+
+  private static ClassLoader buildApiClassLoader(File apiLibDirectory) {
+    // null parent means to use bootstrap classloader as the parent
+    return new URLClassLoader(getClasspathAsURLs(apiLibDirectory), null);
+  }
+
+  private static ClassLoader buildApplicationClassLoader(File 
applicationLibDirectory, File apiLibDirectory,
+      ClassLoader apiClassLoader) {
+    return LoaderBuilder.anIsolatingLoader()
+        // look in application directory for JARs
+        .withClasspath(getClasspathAsURIs(applicationLibDirectory))
+        // getClasspathAsURIs should already satisfy this, but doing it to be 
safe
+        
.withOriginRestriction(OriginRestriction.denyByDefault().allowingDirectory(applicationLibDirectory,
 false))
+        .withParentRelationship(buildApiParentRelationship(apiLibDirectory, 
apiClassLoader))
+        .build();
+  }
+
+  private static ClassLoader buildInfrastructureClassLoader(File 
infrastructureLibDirectory, File apiLibDirectory,
+      ClassLoader apiClassLoader, ClassLoader applicationClassLoader) {
+    return LoaderBuilder.anIsolatingLoader()
+        // look in application directory for JARs
+        .withClasspath(getClasspathAsURIs(infrastructureLibDirectory))
+        // getClasspathAsURIs should already satisfy this, but doing it to be 
safe
+        
.withOriginRestriction(OriginRestriction.denyByDefault().allowingDirectory(infrastructureLibDirectory,
 false))
+        .withParentRelationship(buildApiParentRelationship(apiLibDirectory, 
apiClassLoader))
+        // need to be able to fall back to application for loading certain 
classes
 
 Review comment:
   Examples would be application-specified pluggable Samza classes and message 
schemas. I'll update the comment.

----------------------------------------------------------------
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