You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@accumulo.apache.org by "dlmarion (via GitHub)" <gi...@apache.org> on 2023/02/13 14:18:14 UTC

[GitHub] [accumulo] dlmarion commented on a diff in pull request #3136: Remove deprecated AccumuloVFSClassLoader

dlmarion commented on code in PR #3136:
URL: https://github.com/apache/accumulo/pull/3136#discussion_r1104510866


##########
core/src/main/java/org/apache/accumulo/core/classloader/URLContextClassLoaderFactory.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.classloader;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Arrays;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Collectors;
+
+import org.apache.accumulo.core.spi.common.ContextClassLoaderFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+
+/**
+ * The default implementation of ContextClassLoaderFactory. This classloader returns a
+ * URLClassLoader based on the given context value which is a CSV list of URLs. For example,
+ * file://path/one/jar1.jar,file://path/two/jar2.jar
+ */
+public class URLContextClassLoaderFactory implements ContextClassLoaderFactory {
+
+  private static final AtomicBoolean isInstantiated = new AtomicBoolean(false);
+  private static final Logger LOG = LoggerFactory.getLogger(URLContextClassLoaderFactory.class);
+  private static final String className = URLContextClassLoaderFactory.class.getName();
+
+  // Cache the class loaders for re-use
+  // WeakReferences are used so that the class loaders can be cleaned up when no longer needed
+  // Classes that are loaded contain a reference to the class loader used to load them
+  // so the class loader will be garbage collected when no more classes are loaded that reference it
+  private final Cache<String,URLClassLoader> classloaders =
+      CacheBuilder.newBuilder().weakValues().build();
+
+  public URLContextClassLoaderFactory() {
+    if (!isInstantiated.compareAndSet(false, true)) {
+      throw new IllegalStateException("Can only instantiate " + className + " once");
+    }
+  }
+
+  @Override
+  public ClassLoader getClassLoader(String context) {
+    if (context == null) {
+      throw new IllegalArgumentException("Unknown context");
+    }
+
+    try {
+      return classloaders.get(context, () -> {

Review Comment:
   What happens with the Caffeine cache entry when the ClassLoader is garbage collected and the WeakReference is cleared? Are both the key and value removed? Is a new ClassLoader created after the WeakReference is cleared out? Just curious if there is a possibility of null being returned here.



##########
start/src/main/java/org/apache/accumulo/start/Main.java:
##########
@@ -38,20 +37,17 @@ public class Main {
 
   private static final Logger log = LoggerFactory.getLogger(Main.class);
   private static ClassLoader classLoader;
-  private static Class<?> vfsClassLoader;
   private static Map<String,KeywordExecutable> servicesMap;
 
   public static void main(final String[] args) throws Exception {
     // Preload classes that cause a deadlock between the ServiceLoader and the DFSClient when
     // using the VFSClassLoader with jars in HDFS.
     ClassLoader loader = getClassLoader();
-    Class<?> confClass = null;
+    Class<?> confClass;
     try {
-      @SuppressWarnings("deprecation")
-      var deprecatedConfClass = org.apache.accumulo.start.classloader.AccumuloClassLoader
-          .getClassLoader().loadClass("org.apache.hadoop.conf.Configuration");
-      confClass = deprecatedConfClass;
-      Object conf = null;
+      confClass =
+          ClassLoader.getSystemClassLoader().loadClass("org.apache.hadoop.conf.Configuration");
+      Object conf;

Review Comment:
   Going back and looking at ACCUMULO-4341 and ACCUMULO-3923, it looks like this was added to get around an issue, that's been resolved, in VFS not loading resource files in jars correctly. I think this can be removed. If you test this with the new VFS classloader and we run into an issue, then we can put it back.



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

To unsubscribe, e-mail: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org