dlmarion commented on code in PR #35:
URL: 
https://github.com/apache/accumulo-classloaders/pull/35#discussion_r2636349574


##########
modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/util/LocalStore.java:
##########
@@ -0,0 +1,219 @@
+/*
+ * 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.util;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.nio.file.StandardCopyOption.ATOMIC_MOVE;
+import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
+import static java.util.Objects.requireNonNull;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UncheckedIOException;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.FileAlreadyExistsException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Pattern;
+
+import org.apache.accumulo.classloader.lcc.Constants;
+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 LocalStore {
+  private static final Logger LOG = LoggerFactory.getLogger(LocalStore.class);
+  private static final String PID = 
Long.toString(ProcessHandle.current().pid());
+
+  private final Path contextsDir;
+  private final Path resourcesDir;
+
+  public LocalStore(final Path baseDir) throws IOException {
+    this.contextsDir = 
requireNonNull(baseDir).toAbsolutePath().resolve("contexts");
+    this.resourcesDir = baseDir.resolve("resources");
+    Files.createDirectories(contextsDir);
+    Files.createDirectories(resourcesDir);
+  }
+
+  Path contextsDir() {
+    return contextsDir;
+  }
+
+  Path resourcesDir() {
+    return resourcesDir;
+  }
+
+  // pattern to match regular files that have at least one non-dot character 
preceding a dot and a
+  // non-zero suffix; these files can be easily converted so the local store 
retains the original
+  // file name extension, while non-matching files will not attempt to retain 
the original file name
+  // extension, and will instead just append the checksum to the original file 
name
+  private static Pattern fileNamesWithExtensionPattern = 
Pattern.compile("^(.*[^.].*)[.]([^.]+)$");
+
+  static String localName(String remoteFileName, String checksum) {
+    requireNonNull(remoteFileName);
+    requireNonNull(checksum);
+    var matcher = fileNamesWithExtensionPattern.matcher(remoteFileName);
+    if (matcher.matches()) {
+      return String.format("%s-%s.%s", matcher.group(1), checksum, 
matcher.group(2));
+    }
+    return String.format("%s-%s", remoteFileName, checksum);
+  }
+
+  static String tempName(String baseName) {
+    return "." + requireNonNull(baseName) + ".tmp_" + PID;
+  }
+
+  public URLClassLoaderParams storeContextResources(final ContextDefinition 
contextDefinition)
+      throws IOException, ContextClassLoaderException, InterruptedException, 
URISyntaxException {
+    requireNonNull(contextDefinition, "definition must be supplied");
+    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();
+    final String contextName = contextDefinition.getContextName();
+    final Set<Path> localFiles = new LinkedHashSet<>();
+    try {
+      storeContextDefinition(contextDefinition);
+      Retry retry = retryFactory.createRetry();
+      boolean successful = false;
+      while (!successful) {
+        localFiles.clear();
+        for (Resource resource : contextDefinition.getResources()) {
+          Path path = storeResource(resource);
+          if (path == null) {
+            LOG.debug("Skipped resource {} while another process or thread is 
downloading it",
+                resource.getLocation());
+            continue;
+          }
+          localFiles.add(path);
+          LOG.trace("Added resource {} to classpath", path);
+        }
+        successful = localFiles.size() == 
contextDefinition.getResources().size();
+        if (!successful) {
+          retry.logRetry(LOG, "Unable to store all resources for context " + 
contextName);
+          retry.waitForNextAttempt(LOG, "Store resources for context " + 
contextName);
+          retry.useRetry();
+        }
+        retry.logCompletion(LOG,
+            "Resources for context " + contextName + " cached locally as " + 
localFiles);
+      }
+
+    } catch (IOException | InterruptedException | RuntimeException e) {
+      LOG.error("Error initializing context: " + 
contextDefinition.getContextName(), e);

Review Comment:
   That's likely.



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

Reply via email to