Skip to content

Commit

Permalink
Improve VFS cache deletion (#1337)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck authored Feb 13, 2025
1 parent d2d5123 commit b997cc2
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cli/src/main/java/org/jboss/pnc/build/finder/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public final class Main implements Callable<Void> {
private Long cacheLifespan = ConfigDefaults.CACHE_LIFESPAN;

@Option(names = { "-c", "--config" }, paramLabel = "FILE", description = "Specify configuration file to use.")
private Path configFile = Path.of(ConfigDefaults.CONFIG);
private Path configFile = ConfigDefaults.CONFIG;

@Option(names = { "-d", "--debug" }, description = "Enable debug logging.")
private boolean debug;
Expand Down Expand Up @@ -421,7 +421,7 @@ private BuildConfig setupBuildConfig() throws IOException {

private void initCaches(BuildConfig config) {
GlobalConfigurationChildBuilder globalConfig = new GlobalConfigurationBuilder();
String cacheLocation = Path.of(ConfigDefaults.CACHE_LOCATION).toAbsolutePath().toString();
String cacheLocation = ConfigDefaults.CACHE_LOCATION.toString();

globalConfig.globalState()
.persistentLocation(cacheLocation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package org.jboss.pnc.build.finder.core;

import java.io.File;
import java.net.URL;
import java.nio.file.Path;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
Expand All @@ -25,7 +25,7 @@
import java.util.regex.Pattern;

public abstract class ConfigDefaults {
private static final String USER_HOME = Utils.getUserHome();
private static final Path USER_HOME = Utils.getUserHome();
public static final List<String> ARCHIVE_TYPES = List.of("jar", "xml", "pom", "so", "dll", "dylib");
public static final List<String> ARCHIVE_EXTENSIONS = List.of(
"dll",
Expand All @@ -48,10 +48,10 @@ public abstract class ConfigDefaults {
public static final Set<ChecksumType> CHECKSUM_TYPES = Collections
.unmodifiableSet(EnumSet.allOf(ChecksumType.class));
public static final String CONFIG_FILE = "config.json";
public static final String CONFIG_PATH = USER_HOME + File.separator + ".build-finder";
public static final String CONFIG = CONFIG_PATH + File.separator + CONFIG_FILE;
public static final Path CONFIG_PATH = USER_HOME.resolve(".build-finder");
public static final Path CONFIG = CONFIG_PATH.resolve(CONFIG_FILE);
public static final Boolean DISABLE_CACHE = Boolean.FALSE;
public static final String CACHE_LOCATION = CONFIG_PATH + File.separator + "cache";
public static final Path CACHE_LOCATION = CONFIG_PATH.resolve("cache");
public static final Boolean DISABLE_RECURSION = Boolean.FALSE;
public static final List<Pattern> EXCLUDES = List.of(Pattern.compile("^(?!.*/pom\\.xml$).*/.*\\.xml$"));
public static final URL KOJI_HUB_URL = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,11 @@ public Map<ChecksumType, MultiValuedMap<String, LocalFile>> checksumFiles() thro
}
} finally {
try {
cleanupVfsCache();
boolean clean = Utils.cleanupVfsCache();

if (LOGGER.isInfoEnabled()) {
LOGGER.info("Cleaned up {}: {}", green(Utils.getVfsCache()), clean ? green("yes") : green("no"));
}
} catch (IOException e) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Cleaning up VFS cache failed: {}", red(getMessage(e)));
Expand Down Expand Up @@ -387,23 +391,6 @@ public Map<ChecksumType, MultiValuedMap<String, LocalFile>> checksumFiles() thro
return Collections.unmodifiableMap(map);
}

private static void cleanupVfsCache() throws IOException {
// XXX: <https://issues.apache.org/jira/browse/VFS-634>
String tmpDir = System.getProperty("java.io.tmpdir");

if (tmpDir != null) {
Path vfsCacheDir = Path.of(tmpDir, "vfs_cache").toAbsolutePath();

try (Stream<Path> stream = Files.walk(vfsCacheDir)) {
List<Path> paths = stream.sorted(reverseOrder()).toList();

for (Path path : paths) {
Files.delete(path);
}
}
}
}

private static FileSystemManager createManager() throws FileSystemException {
StandardFileSystemManager sfs = new StandardFileSystemManager();

Expand Down
51 changes: 48 additions & 3 deletions core/src/main/java/org/jboss/pnc/build/finder/core/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.jboss.pnc.build.finder.core;

import static java.util.Comparator.reverseOrder;
import static org.apache.commons.lang3.ThreadUtils.sleep;
import static org.jboss.pnc.build.finder.core.AnsiUtils.boldRed;
import static org.jboss.pnc.build.finder.core.AnsiUtils.boldYellow;
Expand All @@ -24,13 +25,18 @@
import java.io.IOException;
import java.io.InputStream;
import java.math.RoundingMode;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.apache.commons.lang3.SystemProperties;
import org.apache.commons.vfs2.FileContent;
Expand All @@ -56,6 +62,8 @@ public final class Utils {

private static final int NUM_RETRIES = 3;

private static final String VFS_CACHE = "vfs_cache";

static {
PROPERTIES = new Properties();

Expand Down Expand Up @@ -105,7 +113,7 @@ private static String getProperty(String name) {
return PROPERTIES.getProperty(name, "unknown");
}

public static String getUserHome() {
public static Path getUserHome() {
String userHome = SystemProperties.getUserHome();

if (userHome == null || "?".equals(userHome)) {
Expand All @@ -114,10 +122,10 @@ public static String getUserHome() {
"Using java.io.tmpdir {} instead of bogus user.home value {}",
boldRed(javaIoTmpdir),
boldRed(userHome));
return javaIoTmpdir;
return Path.of(javaIoTmpdir);
}

return userHome;
return Path.of(userHome);
}

public static String byteCountToDisplaySize(long bytes) {
Expand Down Expand Up @@ -240,4 +248,41 @@ public static <T> T retry(Supplier<T> supplier) {
LOGGER.error("Retry attempt failed after {} of {} tries", boldRed(numRetries), boldRed(NUM_RETRIES));
throw new IllegalStateException(exception);
}

public static Optional<Path> getVfsCache() throws IOException {
String tmpDir = SystemProperties.getJavaIoTmpdir();

if (tmpDir == null) {
return Optional.empty();
}

Path vfsCacheDir = Path.of(tmpDir, VFS_CACHE).toAbsolutePath();

if (!Files.isDirectory(vfsCacheDir)) {
return Optional.empty();
}

return Optional.of(vfsCacheDir);
}

// XXX: <https://issues.apache.org/jira/browse/VFS-634>
public static boolean cleanupVfsCache() throws IOException {
Path vfsCacheDir = getVfsCache().orElse(null);

if (vfsCacheDir == null) {
return false;
}

try (Stream<Path> stream = Files.walk(vfsCacheDir)) {
List<Path> paths = stream.sorted(reverseOrder()).toList();

for (Path path : paths) {
Files.delete(path);
}

Files.delete(vfsCacheDir);
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static org.jboss.pnc.build.finder.core.Utils.getUserHome;
import static org.jboss.pnc.build.finder.core.Utils.retry;

import java.nio.file.Path;

import org.apache.commons.lang3.SystemProperties;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.ResourceAccessMode;
Expand All @@ -37,9 +39,9 @@ class UtilsTest {
private static final Logger LOGGER = LoggerFactory.getLogger(UtilsTest.class);

private static String userHome() {
String userHome = getUserHome();
Path userHome = getUserHome();
LOGGER.debug("user.home={}", userHome);
return userHome;
return userHome.toString();
}

@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ_WRITE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@

import static java.util.concurrent.Executors.newFixedThreadPool;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.jboss.pnc.build.finder.core.ConfigDefaults.CONFIG;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.commonjava.o11yphant.metrics.DefaultMetricRegistry;
import org.commonjava.o11yphant.metrics.api.MetricRegistry;
import org.commonjava.util.jhttpc.auth.MemoryPasswordManager;
import org.jboss.pnc.build.finder.core.BuildConfig;
import org.jboss.pnc.build.finder.core.ConfigDefaults;
import org.jboss.pnc.build.finder.koji.KojiClientSession;
import org.jboss.pnc.build.finder.pnc.client.PncClientImpl;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -70,7 +69,7 @@ public abstract class AbstractKojiIT {

@BeforeEach
void setup() throws IOException, KojiClientException {
Path configFile = Paths.get(CONFIG);
Path configFile = ConfigDefaults.CONFIG;

if (!Files.isRegularFile(configFile) || !Files.isReadable(configFile)) {
throw new IOException("File not found: " + configFile.toAbsolutePath());
Expand Down

0 comments on commit b997cc2

Please sign in to comment.