Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize resource file writing #1097

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.openvsx.repositories.RepositoryService;
import org.eclipse.openvsx.storage.StorageUtilService;
import org.eclipse.openvsx.util.ErrorResultException;
import org.eclipse.openvsx.util.FileUtil;
import org.eclipse.openvsx.util.NamingUtil;
import org.eclipse.openvsx.util.UrlUtil;
import org.slf4j.Logger;
Expand All @@ -24,9 +25,9 @@
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.stream.Collectors;
import java.util.zip.ZipFile;

Expand Down Expand Up @@ -62,12 +63,7 @@ public Path getWebResource(String namespace, String extension, String targetPlat
return null;
}

Path path;
try {
path = storageUtil.getCachedFile(download);
} catch(IOException e) {
throw new ErrorResultException("Failed to get file for download " + NamingUtil.toLogFormat(download.getExtension()));
}
var path = storageUtil.getCachedFile(download);
if(path == null) {
return null;
}
Expand All @@ -83,11 +79,13 @@ public Path getWebResource(String namespace, String extension, String targetPlat
var fileExtIndex = fileEntry.getName().lastIndexOf('.');
var fileExt = fileExtIndex != -1 ? fileEntry.getName().substring(fileExtIndex) : "";
var file = filesCacheKeyGenerator.generateCachedWebResourcePath(namespace, extension, targetPlatform, version, name, fileExt);
if(!Files.exists(file)) {
FileUtil.writeSync(file, (p) -> {
try (var in = zip.getInputStream(fileEntry)) {
Files.copy(in, file);
Files.copy(in, p);
} catch(IOException e) {
throw new UncheckedIOException(e);
}
}
});

return file;
} else if (browse) {
Expand All @@ -103,22 +101,27 @@ public Path getWebResource(String namespace, String extension, String targetPlat
return null;
}


var file = filesCacheKeyGenerator.generateCachedWebResourcePath(namespace, extension, targetPlatform, version, name, ".unpkg.json");
if(!Files.exists(file)) {
FileUtil.writeSync(file, (p) -> {
var baseUrl = UrlUtil.createApiUrl(UrlUtil.getBaseUrl(), "vscode", "unpkg", namespace, extension, version);
var mapper = new ObjectMapper();
var node = mapper.createArrayNode();
for (var entry : dirEntries) {
node.add(baseUrl + "/" + entry);
}
mapper.writeValue(file.toFile(), node);
}
try {
mapper.writeValue(p.toFile(), node);
} catch(IOException e) {
throw new UncheckedIOException(e);
}
});

return file;
} else {
return null;
}
} catch (IOException e) {
} catch (IOException | UncheckedIOException e) {
throw new ErrorResultException("Failed to read extension files for " + NamingUtil.toLogFormat(download.getExtension()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.openvsx.storage;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
Expand All @@ -24,6 +25,7 @@
import org.eclipse.openvsx.cache.FilesCacheKeyGenerator;
import org.eclipse.openvsx.entities.FileResource;
import org.eclipse.openvsx.entities.Namespace;
import org.eclipse.openvsx.util.FileUtil;
import org.eclipse.openvsx.util.TempFile;
import org.eclipse.openvsx.util.UrlUtil;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -246,19 +248,21 @@ private void copy(String oldObjectKey, String newObjectKey) {

@Override
@Cacheable(value = CACHE_EXTENSION_FILES, keyGenerator = GENERATOR_FILES)
public Path getCachedFile(FileResource resource) throws IOException {
public Path getCachedFile(FileResource resource) {
var objectKey = getObjectKey(resource);
var request = GetObjectRequest.builder()
.bucket(bucket)
.key(objectKey)
.build();

var path = filesCacheKeyGenerator.generateCachedExtensionPath(resource);
if(!Files.exists(path)) {
FileUtil.writeSync(path, (p) -> {
try (var stream = getS3Client().getObject(request)) {
Files.copy(stream, path);
Files.copy(stream, p);
} catch(IOException e) {
throw new UncheckedIOException(e);
}
}
});

return path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.openvsx.cache.FilesCacheKeyGenerator;
import org.eclipse.openvsx.entities.FileResource;
import org.eclipse.openvsx.entities.Namespace;
import org.eclipse.openvsx.util.FileUtil;
import org.eclipse.openvsx.util.TempFile;
import org.eclipse.openvsx.util.UrlUtil;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -32,7 +33,6 @@

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -237,17 +237,16 @@ public void copyNamespaceLogo(Namespace oldNamespace, Namespace newNamespace) {

@Override
@Cacheable(value = CACHE_EXTENSION_FILES, keyGenerator = GENERATOR_FILES)
public Path getCachedFile(FileResource resource) throws IOException {
public Path getCachedFile(FileResource resource) {
var blobName = getBlobName(resource);
if (StringUtils.isEmpty(serviceEndpoint)) {
throw new IllegalStateException(missingEndpointMessage(blobName));
}

var path = filesCacheKeyGenerator.generateCachedExtensionPath(resource);
if(!Files.exists(path)) {
getContainerClient().getBlobClient(blobName).downloadToFile(path.toAbsolutePath().toString());
}

FileUtil.writeSync(path, (p) -> {
getContainerClient().getBlobClient(blobName).downloadToFile(p.toAbsolutePath().toString());
});
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
********************************************************************************/
package org.eclipse.openvsx.storage;

import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.*;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.openvsx.cache.FilesCacheKeyGenerator;
import org.eclipse.openvsx.entities.FileResource;
import org.eclipse.openvsx.entities.Namespace;
import org.eclipse.openvsx.util.FileUtil;
import org.eclipse.openvsx.util.TempFile;
import org.eclipse.openvsx.util.UrlUtil;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -214,16 +212,16 @@ private void copy(String source, String target) {

@Override
@Cacheable(value = CACHE_EXTENSION_FILES, keyGenerator = GENERATOR_FILES)
public Path getCachedFile(FileResource resource) throws IOException {
public Path getCachedFile(FileResource resource) {
if (StringUtils.isEmpty(bucketId)) {
throw new IllegalStateException(missingBucketIdMessage(resource.getName()));
}

var objectId = getObjectId(resource);
var path = filesCacheKeyGenerator.generateCachedExtensionPath(resource);
if(!Files.exists(path)) {
var objectId = getObjectId(resource);
getStorage().downloadTo(BlobId.of(bucketId, objectId), path);
}
FileUtil.writeSync(path, (p) -> {
getStorage().downloadTo(BlobId.of(bucketId, objectId), p);
});

return path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ public interface IStorageService {

void copyNamespaceLogo(Namespace oldNamespace, Namespace newNamespace);

Path getCachedFile(FileResource resource) throws IOException;
Path getCachedFile(FileResource resource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public void copyNamespaceLogo(Namespace oldNamespace, Namespace newNamespace) {
}

@Override
public Path getCachedFile(FileResource resource) throws IOException {
public Path getCachedFile(FileResource resource) {
return switch (resource.getStorageType()) {
case STORAGE_GOOGLE -> googleStorage.getCachedFile(resource);
case STORAGE_AZURE -> azureStorage.getCachedFile(resource);
Expand Down
47 changes: 47 additions & 0 deletions server/src/main/java/org/eclipse/openvsx/util/FileUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** ******************************************************************************
* Copyright (c) 2025 Precies. Software OU and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
* ****************************************************************************** */

package org.eclipse.openvsx.util;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;

public class FileUtil {

private static final Map<Path, Object> LOCKS;

static {
var MAX_SIZE = 100;
LOCKS = Collections.synchronizedMap(new LinkedHashMap<>(MAX_SIZE) {
protected boolean removeEldestEntry(Map.Entry eldest){
return size() > MAX_SIZE;
}
});
}

private FileUtil(){}

/***
* Write to file synchronously, if it doesn't already exist.
* @param path File path to write to
* @param writer Writes to file
*/
public static void writeSync(Path path, Consumer<Path> writer) {
synchronized (LOCKS.computeIfAbsent(path, key -> new Object())) {
if(!Files.exists(path)) {
writer.accept(path);
}
}
}
}