dlmarion commented on code in PR #30: URL: https://github.com/apache/accumulo-classloaders/pull/30#discussion_r2543156066
########## modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/LocalCachingContext.java: ########## @@ -0,0 +1,236 @@ +/* + * 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.classloader.lcc; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.accumulo.classloader.lcc.cache.CacheUtils; +import org.apache.accumulo.classloader.lcc.cache.CacheUtils.LockInfo; +import org.apache.accumulo.classloader.lcc.definition.ContextDefinition; +import org.apache.accumulo.classloader.lcc.definition.Resource; +import org.apache.accumulo.classloader.lcc.resolvers.FileResolver; +import org.apache.accumulo.core.spi.common.ContextClassLoaderFactory.ContextClassLoaderException; +import org.apache.accumulo.core.util.Retry; +import org.apache.accumulo.core.util.Retry.RetryFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class LocalCachingContext { + + private static class ClassPathElement { + private final FileResolver resolver; + private final URL localCachedCopyLocation; + private final String localCachedCopyDigest; + + public ClassPathElement(FileResolver resolver, URL localCachedCopy, + String localCachedCopyDigest) { + this.resolver = Objects.requireNonNull(resolver, "resolver must be supplied"); + this.localCachedCopyLocation = + Objects.requireNonNull(localCachedCopy, "local cached copy location must be supplied"); + this.localCachedCopyDigest = + Objects.requireNonNull(localCachedCopyDigest, "local cached copy md5 must be supplied"); + } + + public URL getLocalCachedCopyLocation() { + return localCachedCopyLocation; + } + + @Override + public int hashCode() { + return Objects.hash(localCachedCopyDigest, localCachedCopyLocation, resolver); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ClassPathElement other = (ClassPathElement) obj; + return Objects.equals(localCachedCopyDigest, other.localCachedCopyDigest) + && Objects.equals(localCachedCopyLocation, other.localCachedCopyLocation) + && Objects.equals(resolver, other.resolver); + } + + @Override + public String toString() { + StringBuilder buf = new StringBuilder(); + buf.append("source: ").append(resolver.getURL()); + buf.append(", cached copy:").append(localCachedCopyLocation); + return buf.toString(); + } + } + + private static final Logger LOG = LoggerFactory.getLogger(LocalCachingContext.class); + + private final Path contextCacheDir; + private final String contextName; + private final Set<ClassPathElement> elements = new HashSet<>(); + private final AtomicBoolean elementsChanged = new AtomicBoolean(true); + private final AtomicReference<URLClassLoader> classloader = new AtomicReference<>(); + private final AtomicReference<ContextDefinition> definition = new AtomicReference<>(); + private final RetryFactory retryFactory = Retry.builder().infiniteRetries() + .retryAfter(1, TimeUnit.SECONDS).incrementBy(1, TimeUnit.SECONDS).maxWait(5, TimeUnit.MINUTES) + .backOffFactor(2).logInterval(1, TimeUnit.SECONDS).createFactory(); + + public LocalCachingContext(ContextDefinition contextDefinition) + throws IOException, ContextClassLoaderException { + this.definition.set(Objects.requireNonNull(contextDefinition, "definition must be supplied")); + this.contextName = this.definition.get().getContextName(); + this.contextCacheDir = CacheUtils.createOrGetContextCacheDir(contextName); + } + + public ContextDefinition getDefinition() { + return definition.get(); + } + + private ClassPathElement cacheResource(final Resource resource) throws Exception { + final FileResolver source = FileResolver.resolve(resource.getURL()); + final Path cacheLocation = + contextCacheDir.resolve(source.getFileName() + "_" + resource.getChecksum()); + final File cacheFile = cacheLocation.toFile(); + if (!Files.exists(cacheLocation)) { + Retry retry = retryFactory.createRetry(); + boolean successful = false; + while (!successful) { + LOG.trace("Caching resource {} at {}", source.getURL(), cacheFile.getAbsolutePath()); + try (InputStream is = source.getInputStream()) { + Files.copy(is, cacheLocation, StandardCopyOption.REPLACE_EXISTING); + successful = true; + retry.logCompletion(LOG, "Resource " + source.getURL() + " cached locally"); + } catch (IOException e) { + LOG.error("Error copying resource from {}. Retrying...", source.getURL(), e); + retry.logRetry(LOG, "Unable to cache resource " + source.getURL()); + retry.waitForNextAttempt(LOG, "Cache resource " + source.getURL()); + } finally { + retry.useRetry(); + } + } + final String checksum = Constants.getChecksummer().digestAsHex(cacheFile); + if (!resource.getChecksum().equals(checksum)) { + LOG.error("Checksum {} for resource {} does not match checksum in context definition {}", + checksum, source.getURL(), resource.getChecksum()); + throw new IllegalStateException("Checksum " + checksum + " for resource " + source.getURL() + + " does not match checksum in context definition " + resource.getChecksum()); + } + return new ClassPathElement(source, cacheFile.toURI().toURL(), checksum); + } else { + // File exists, return new ClassPathElement based on existing file + LOG.trace("Resource {} is already cached at {}", source.getURL(), + cacheFile.getAbsolutePath()); + return new ClassPathElement(source, cacheFile.toURI().toURL(), resource.getChecksum()); + } + } + + private void cacheResources(final ContextDefinition def) throws Exception { + synchronized (elements) { + for (Resource updatedResource : def.getResources()) { + ClassPathElement cpe = cacheResource(updatedResource); + elements.add(cpe); + LOG.trace("Added element {} to classpath", cpe); + } + elementsChanged.set(true); + } + } + + public void initialize() { + try { + synchronized (elements) { + final LockInfo lockInfo = CacheUtils.lockContextCacheDir(contextCacheDir); + if (lockInfo == null) { + // something else is updating this directory + return; + } + try { + cacheResources(definition.get()); + } finally { + lockInfo.unlock(); + } + } + } catch (Exception e) { + LOG.error("Error initializing context: " + contextName, e); + } + } + + public void update(final ContextDefinition update) { + Objects.requireNonNull(update, "definition must be supplied"); + if (definition.get().getResources().equals(update.getResources())) { + return; + } + synchronized (elements) { + try { + final LockInfo lockInfo = CacheUtils.lockContextCacheDir(contextCacheDir); + if (lockInfo == null) { + // something else is updating this directory + return; + } + try { + elements.clear(); + cacheResources(update); + this.definition.set(update); + } finally { + lockInfo.unlock(); + } + } catch (Exception e) { + LOG.error("Error updating context: " + contextName, e); + } + } + } + + public ClassLoader getClassloader() { + synchronized (elements) { Review Comment: Updated in d4e39bb ########## modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/LocalCachingContextClassLoaderFactory.java: ########## @@ -0,0 +1,157 @@ +/* + * 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.classloader.lcc; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Arrays; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.accumulo.classloader.lcc.cache.CacheUtils; +import org.apache.accumulo.classloader.lcc.definition.ContextDefinition; +import org.apache.accumulo.classloader.lcc.definition.Resource; +import org.apache.accumulo.classloader.lcc.resolvers.FileResolver; +import org.apache.accumulo.core.spi.common.ContextClassLoaderFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; + +/** + * A ContextClassLoaderFactory implementation that creates and maintains a ClassLoader for a named + * context. This factory expects the parameter passed to {@link #getClassLoader(String)} to be the + * URL of a json formatted {@link ContextDefinition} file. The file contains an interval at which + * this class should monitor the file for changes and a list of {@link Resource} objects. Each + * resource is defined by a URL to the file and an expected MD5 hash value. + * <p> + * The URLs supplied for the context definition file and for the resources can use one of the + * following protocols: file://, http://, or hdfs://. + * <p> + * As this class processes the ContextDefinition it fetches the contents of the resource from the + * resource URL and caches it in a directory on the local filesystem. This class uses the value of + * the system property {@link Constants#CACHE_DIR_PROPERTY} as the root directory and creates a + * sub-directory for each context name. Each context cache directory contains a lock file and a copy + * of each fetched resource that is named using the following format: fileName_checksum. + * <p> + * The lock file prevents processes from manipulating the contexts of the context cache directory + * concurrently, which enables the cache directories to be shared among multiple processes on the + * host. + * <p> + * Note that because the cache directory is shared among multiple processes, and one process can't + * know what the other processes are doing, this class cannot clean up the shared cache directory. + * It is left to the user to remove unused context cache directories and unused old files within a + * context cache directory. + */ +public class LocalCachingContextClassLoaderFactory implements ContextClassLoaderFactory { + + private static final Logger LOG = + LoggerFactory.getLogger(LocalCachingContextClassLoaderFactory.class); + + private final Cache<String,LocalCachingContext> contexts = + Caffeine.newBuilder().weakValues().build(); + + private ContextDefinition parseContextDefinition(URL url) throws ContextClassLoaderException { + LOG.trace("Retrieving context definition file from {}", url); + FileResolver resolver = FileResolver.resolve(url); + try { + try (InputStream is = resolver.getInputStream()) { + ContextDefinition def = + Constants.GSON.fromJson(new InputStreamReader(is, UTF_8), ContextDefinition.class); + if (def == null) { + throw new ContextClassLoaderException( + "ContextDefinition null for context definition file: " + resolver.getURL()); + } + return def; + } + } catch (IOException e) { + throw new ContextClassLoaderException( + "Error reading context definition file: " + resolver.getURL(), e); + } + } + + private void monitorContext(final String contextLocation, int interval) { + Constants.EXECUTOR.schedule(() -> { + final LocalCachingContext classLoader = contexts.getIfPresent(contextLocation); + if (classLoader == null) { + // context has been removed from the map, no need to check for update + return; + } + final ContextDefinition currentDef = classLoader.getDefinition(); + try { + final URL contextManifest = new URL(contextLocation); + final ContextDefinition update = parseContextDefinition(contextManifest); + if (!Arrays.equals(currentDef.getChecksum(), update.getChecksum())) { + LOG.debug("Context definition for {} has changed", currentDef.getContextName()); + classLoader.update(update); + } else { + LOG.debug("Context definition for {} has not changed", currentDef.getContextName()); Review Comment: Updated in d4e39bb ########## modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/LocalCachingContext.java: ########## @@ -0,0 +1,236 @@ +/* + * 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.classloader.lcc; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.accumulo.classloader.lcc.cache.CacheUtils; +import org.apache.accumulo.classloader.lcc.cache.CacheUtils.LockInfo; +import org.apache.accumulo.classloader.lcc.definition.ContextDefinition; +import org.apache.accumulo.classloader.lcc.definition.Resource; +import org.apache.accumulo.classloader.lcc.resolvers.FileResolver; +import org.apache.accumulo.core.spi.common.ContextClassLoaderFactory.ContextClassLoaderException; +import org.apache.accumulo.core.util.Retry; +import org.apache.accumulo.core.util.Retry.RetryFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class LocalCachingContext { + + private static class ClassPathElement { + private final FileResolver resolver; + private final URL localCachedCopyLocation; + private final String localCachedCopyDigest; + + public ClassPathElement(FileResolver resolver, URL localCachedCopy, + String localCachedCopyDigest) { + this.resolver = Objects.requireNonNull(resolver, "resolver must be supplied"); + this.localCachedCopyLocation = + Objects.requireNonNull(localCachedCopy, "local cached copy location must be supplied"); + this.localCachedCopyDigest = + Objects.requireNonNull(localCachedCopyDigest, "local cached copy md5 must be supplied"); + } + + public URL getLocalCachedCopyLocation() { + return localCachedCopyLocation; + } + + @Override + public int hashCode() { + return Objects.hash(localCachedCopyDigest, localCachedCopyLocation, resolver); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ClassPathElement other = (ClassPathElement) obj; + return Objects.equals(localCachedCopyDigest, other.localCachedCopyDigest) + && Objects.equals(localCachedCopyLocation, other.localCachedCopyLocation) + && Objects.equals(resolver, other.resolver); + } + + @Override + public String toString() { + StringBuilder buf = new StringBuilder(); + buf.append("source: ").append(resolver.getURL()); + buf.append(", cached copy:").append(localCachedCopyLocation); + return buf.toString(); + } + } + + private static final Logger LOG = LoggerFactory.getLogger(LocalCachingContext.class); + + private final Path contextCacheDir; + private final String contextName; + private final Set<ClassPathElement> elements = new HashSet<>(); + private final AtomicBoolean elementsChanged = new AtomicBoolean(true); + private final AtomicReference<URLClassLoader> classloader = new AtomicReference<>(); + private final AtomicReference<ContextDefinition> definition = new AtomicReference<>(); + private final RetryFactory retryFactory = Retry.builder().infiniteRetries() + .retryAfter(1, TimeUnit.SECONDS).incrementBy(1, TimeUnit.SECONDS).maxWait(5, TimeUnit.MINUTES) + .backOffFactor(2).logInterval(1, TimeUnit.SECONDS).createFactory(); + + public LocalCachingContext(ContextDefinition contextDefinition) + throws IOException, ContextClassLoaderException { + this.definition.set(Objects.requireNonNull(contextDefinition, "definition must be supplied")); + this.contextName = this.definition.get().getContextName(); + this.contextCacheDir = CacheUtils.createOrGetContextCacheDir(contextName); + } + + public ContextDefinition getDefinition() { + return definition.get(); + } + + private ClassPathElement cacheResource(final Resource resource) throws Exception { + final FileResolver source = FileResolver.resolve(resource.getURL()); + final Path cacheLocation = + contextCacheDir.resolve(source.getFileName() + "_" + resource.getChecksum()); + final File cacheFile = cacheLocation.toFile(); + if (!Files.exists(cacheLocation)) { + Retry retry = retryFactory.createRetry(); Review Comment: Updated in d4e39bb ########## modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/LocalCachingContext.java: ########## @@ -0,0 +1,236 @@ +/* + * 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.classloader.lcc; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.accumulo.classloader.lcc.cache.CacheUtils; +import org.apache.accumulo.classloader.lcc.cache.CacheUtils.LockInfo; +import org.apache.accumulo.classloader.lcc.definition.ContextDefinition; +import org.apache.accumulo.classloader.lcc.definition.Resource; +import org.apache.accumulo.classloader.lcc.resolvers.FileResolver; +import org.apache.accumulo.core.spi.common.ContextClassLoaderFactory.ContextClassLoaderException; +import org.apache.accumulo.core.util.Retry; +import org.apache.accumulo.core.util.Retry.RetryFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class LocalCachingContext { + + private static class ClassPathElement { + private final FileResolver resolver; + private final URL localCachedCopyLocation; + private final String localCachedCopyDigest; + + public ClassPathElement(FileResolver resolver, URL localCachedCopy, + String localCachedCopyDigest) { + this.resolver = Objects.requireNonNull(resolver, "resolver must be supplied"); + this.localCachedCopyLocation = + Objects.requireNonNull(localCachedCopy, "local cached copy location must be supplied"); + this.localCachedCopyDigest = + Objects.requireNonNull(localCachedCopyDigest, "local cached copy md5 must be supplied"); + } + + public URL getLocalCachedCopyLocation() { + return localCachedCopyLocation; + } + + @Override + public int hashCode() { + return Objects.hash(localCachedCopyDigest, localCachedCopyLocation, resolver); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ClassPathElement other = (ClassPathElement) obj; + return Objects.equals(localCachedCopyDigest, other.localCachedCopyDigest) + && Objects.equals(localCachedCopyLocation, other.localCachedCopyLocation) + && Objects.equals(resolver, other.resolver); + } + + @Override + public String toString() { + StringBuilder buf = new StringBuilder(); + buf.append("source: ").append(resolver.getURL()); + buf.append(", cached copy:").append(localCachedCopyLocation); + return buf.toString(); + } + } + + private static final Logger LOG = LoggerFactory.getLogger(LocalCachingContext.class); + + private final Path contextCacheDir; + private final String contextName; + private final Set<ClassPathElement> elements = new HashSet<>(); + private final AtomicBoolean elementsChanged = new AtomicBoolean(true); + private final AtomicReference<URLClassLoader> classloader = new AtomicReference<>(); + private final AtomicReference<ContextDefinition> definition = new AtomicReference<>(); + private final RetryFactory retryFactory = Retry.builder().infiniteRetries() + .retryAfter(1, TimeUnit.SECONDS).incrementBy(1, TimeUnit.SECONDS).maxWait(5, TimeUnit.MINUTES) + .backOffFactor(2).logInterval(1, TimeUnit.SECONDS).createFactory(); + + public LocalCachingContext(ContextDefinition contextDefinition) + throws IOException, ContextClassLoaderException { + this.definition.set(Objects.requireNonNull(contextDefinition, "definition must be supplied")); + this.contextName = this.definition.get().getContextName(); + this.contextCacheDir = CacheUtils.createOrGetContextCacheDir(contextName); + } + + public ContextDefinition getDefinition() { + return definition.get(); + } + + private ClassPathElement cacheResource(final Resource resource) throws Exception { + final FileResolver source = FileResolver.resolve(resource.getURL()); + final Path cacheLocation = + contextCacheDir.resolve(source.getFileName() + "_" + resource.getChecksum()); + final File cacheFile = cacheLocation.toFile(); + if (!Files.exists(cacheLocation)) { + Retry retry = retryFactory.createRetry(); + boolean successful = false; + while (!successful) { + LOG.trace("Caching resource {} at {}", source.getURL(), cacheFile.getAbsolutePath()); + try (InputStream is = source.getInputStream()) { + Files.copy(is, cacheLocation, StandardCopyOption.REPLACE_EXISTING); + successful = true; + retry.logCompletion(LOG, "Resource " + source.getURL() + " cached locally"); Review Comment: Updated in d4e39bb -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
