in genie-agent/src/main/java/com/netflix/genie/agent/execution/services/impl/FetchingCacheServiceImpl.java [134:231]
private void lookupOrDownload(
final URI sourceFileUri,
final File destinationFile
) throws DownloadException, IOException {
final String uriString = sourceFileUri.toASCIIString();
log.debug("Lookup: {}", uriString);
// Unique id to store the resource on local disk
final String resourceCacheId = getResourceCacheId(sourceFileUri);
// Get a handle to the resource
final Resource resource;
try {
resource = resourceLoader.getResource(uriString);
} catch (Throwable t) {
log.error(
"Failed to retrieve resource: {}, {} - {}",
uriString,
t.getClass().getSimpleName(),
t.getMessage()
);
throw t;
}
if (!resource.exists()) {
throw new DownloadException("Resource not found: " + uriString);
}
final long resourceLastModified = resource.lastModified();
//Handle to resourceCacheId/version
final File cacheResourceVersionDir = getCacheResourceVersionDir(
resourceCacheId,
resourceLastModified
);
//Create the resource version dir in cache if it does not exist
createDirectoryStructureIfNotExists(cacheResourceVersionDir);
try (
CloseableLock lock = fileLockFactory.getLock(
touchCacheResourceVersionLockFile(
resourceCacheId,
resourceLastModified
)
)
) {
//Critical section begin
lock.lock();
//Handle to the resource cached locally
final File cachedResourceVersionDataFile = getCacheResourceVersionDataFile(
resourceCacheId,
resourceLastModified
);
if (!cachedResourceVersionDataFile.exists()) {
log.debug(
"Cache miss: {} (id: {})",
uriString,
resourceCacheId
);
// Download the resource into the download file in cache
// resourceCacheId/version/data.tmp
final File cachedResourceVersionDownloadFile = getCacheResourceVersionDownloadFile(
resourceCacheId,
resourceLastModified
);
try (
InputStream in = resource.getInputStream();
OutputStream out = new FileOutputStream(cachedResourceVersionDownloadFile)
) {
FileCopyUtils.copy(in, out);
Files.move(cachedResourceVersionDownloadFile, cachedResourceVersionDataFile);
}
} else {
log.debug(
"Cache hit: {} (id: {})",
uriString,
resourceCacheId
);
}
//Copy from cache data file resourceCacheId/version/DATA_FILE_NAME to targetFile
Files.copy(cachedResourceVersionDataFile, destinationFile);
//Critical section end
} catch (LockException e) {
throw new DownloadException("Error downloading dependency: " + uriString, e);
}
//Clean up any older versions
cleanUpTaskExecutor.execute(
new CleanupOlderVersionsTask(resourceCacheId, resourceLastModified)
);
}