Skip to content
Open
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
8 changes: 6 additions & 2 deletions multiapps-controller-persistence/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@
<artifactId>aws-s3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.jclouds.provider</groupId>
<artifactId>azureblob</artifactId>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-okhttp</artifactId>
</dependency>
<dependency>
<groupId>org.apache.jclouds</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
requires transitive org.cloudfoundry.multiapps.controller.api;

requires aliyun.sdk.oss;
requires com.azure.core;
requires com.azure.core.http.okhttp;
requires com.azure.storage.blob;
requires com.fasterxml.jackson.annotation;
requires com.fasterxml.jackson.databind;
requires com.google.auth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public final class Messages {

// ERROR log messages:
public static final String UPLOAD_STREAM_FAILED_TO_CLOSE = "Cannot close file upload stream";
public static final String CANNOT_PARSE_CONTAINER_URI_OF_OBJECT_STORE = "Cannot parse container_uri of object store";
public static final String MISSING_CONTAINER_URI_IN_THE_CREDENTIALS = "Missing container url in the credentials";

// WARN log messages:
public static final String COULD_NOT_CLOSE_RESULT_SET = "Could not close result set.";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
package org.cloudfoundry.multiapps.controller.persistence.services;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.azure.core.http.HttpClient;
import com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder;
import com.azure.core.http.policy.ExponentialBackoffOptions;
import com.azure.core.http.policy.RetryOptions;
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobItem;
import com.azure.storage.blob.models.BlobListDetails;
import com.azure.storage.blob.models.BlobRange;
import com.azure.storage.blob.models.BlobStorageException;
import com.azure.storage.blob.models.ListBlobsOptions;
import com.azure.storage.blob.options.BlobParallelUploadOptions;
import org.cloudfoundry.multiapps.controller.persistence.Messages;
import org.cloudfoundry.multiapps.controller.persistence.model.FileEntry;
import org.cloudfoundry.multiapps.controller.persistence.util.ObjectStoreConstants;
import org.cloudfoundry.multiapps.controller.persistence.util.ObjectStoreFilter;
import org.cloudfoundry.multiapps.controller.persistence.util.ObjectStoreMapper;

public class AzureObjectStoreFileStorage implements FileStorage {

private static final String SAS_TOKEN = "sas_token";
private static final String CONTAINER_NAME = "container_name";
private static final String CONTAINER_URI = "container_uri";
private final HttpClient httpClient;
private final BlobContainerClient containerClient;

public AzureObjectStoreFileStorage(Map<String, Object> credentials) {
this.containerClient = createContainerClient(credentials);
this.httpClient = new OkHttpAsyncHttpClientBuilder().build();
}

@Override
public void addFile(FileEntry fileEntry, InputStream content) throws FileStorageException {
BlobClient blobClient = containerClient.getBlobClient(fileEntry.getId());
try {
BlobParallelUploadOptions blobParallelUploadOptions = new BlobParallelUploadOptions(content);
blobParallelUploadOptions.setMetadata(ObjectStoreMapper.createFileEntryMetadata(fileEntry));

blobClient.uploadWithResponse(blobParallelUploadOptions, ObjectStoreConstants.OBJECT_STORE_TOTAL_TIMEOUT_CONFIG_IN_MINUTES,
null);
} catch (BlobStorageException e) {
throw new FileStorageException(e);
}
}

@Override
public List<FileEntry> getFileEntriesWithoutContent(List<FileEntry> fileEntries) throws FileStorageException {
Set<String> existingFiles = getAllEntriesNames();
return fileEntries.stream()
.filter(fileEntry -> !existingFiles.contains(fileEntry.getId()))
.toList();
}

@Override
public void deleteFile(String id, String space) throws FileStorageException {
BlobClient blobClient = containerClient.getBlobClient(id);
try {
blobClient.deleteIfExists();
} catch (BlobStorageException e) {
throw new FileStorageException(e);
}
}

@Override
public void deleteFilesBySpaceIds(List<String> spaceIds) throws FileStorageException {
removeBlobsByFilter(blob -> ObjectStoreFilter.filterBySpaceIds(blob.getMetadata(), spaceIds));
}

@Override
public void deleteFilesBySpaceAndNamespace(String space, String namespace) {
removeBlobsByFilter(blob -> ObjectStoreFilter.filterBySpaceAndNamespace(blob.getMetadata(), space, namespace));
}

@Override
public int deleteFilesModifiedBefore(LocalDateTime modificationTime) throws FileStorageException {
return removeBlobsByFilter(
blob -> ObjectStoreFilter.filterByModificationTime(blob.getMetadata(), blob.getName(), modificationTime));
}

@Override
public <T> T processFileContent(String space, String id, FileContentProcessor<T> fileContentProcessor) throws FileStorageException {
FileEntry fileEntry = ObjectStoreMapper.createFileEntry(space, id);
try (InputStream inputStream = openBlobInputStream(fileEntry)) {
return fileContentProcessor.process(inputStream);
} catch (Exception e) {
throw new FileStorageException(e);
}
}

private InputStream openBlobInputStream(FileEntry fileEntry) throws FileStorageException {
BlobClient blobClient = containerClient.getBlobClient(fileEntry.getId());
try {
return blobClient.openInputStream();
} catch (BlobStorageException e) {
throw new FileStorageException(e);
}
}

@Override
public InputStream openInputStream(String space, String id) throws FileStorageException {
FileEntry fileEntry = ObjectStoreMapper.createFileEntry(space, id);
return openBlobInputStream(fileEntry);
}

@Override
public void testConnection() {
containerClient.getBlobClient("test");
}

@Override
public void deleteFilesByIds(List<String> fileIds) throws FileStorageException {
removeBlobsByFilter(blob -> fileIds.contains(blob.getName()));
}

@Override
public <T> T processArchiveEntryContent(FileContentToProcess fileContentToProcess, FileContentProcessor<T> fileContentProcessor)
throws FileStorageException {
FileEntry fileEntry = ObjectStoreMapper.createFileEntry(fileContentToProcess.getSpaceGuid(), fileContentToProcess.getGuid());
BlobClient blobClient = containerClient.getBlobClient(fileEntry.getId());
long contentSize = fileContentToProcess.getEndOffset() - fileContentToProcess.getStartOffset();
BlobRange blobRange = new BlobRange(fileContentToProcess.getStartOffset(), contentSize);

try {
return fileContentProcessor.process(blobClient.openInputStream(blobRange, null));
} catch (IOException e) {
throw new FileStorageException(e);
}
}

protected BlobContainerClient createContainerClient(Map<String, Object> credentials) {
BlobServiceClient serviceClient = new BlobServiceClientBuilder().endpoint(getContainerUriEndpoint(credentials))
.retryOptions(createRetryOptions())
.httpClient(httpClient)
.sasToken((String) credentials.get(SAS_TOKEN))
.buildClient();

return serviceClient.getBlobContainerClient((String) credentials.get(CONTAINER_NAME));
}

public String getContainerUriEndpoint(Map<String, Object> credentials) {
if (!credentials.containsKey(CONTAINER_URI)) {
throw new IllegalStateException(Messages.MISSING_CONTAINER_URI_IN_THE_CREDENTIALS);
}
try {
URL containerUri = new URL((String) credentials.get(CONTAINER_URI));
return new URL(containerUri.getProtocol(), containerUri.getHost(), containerUri.getPort(), "").toString();
} catch (MalformedURLException e) {
throw new IllegalStateException(Messages.CANNOT_PARSE_CONTAINER_URI_OF_OBJECT_STORE, e);
}
}

private RetryOptions createRetryOptions() {
ExponentialBackoffOptions exponentialBackoffOptions = new ExponentialBackoffOptions().setBaseDelay(
ObjectStoreConstants.OBJECT_STORE_INITIAL_RETRY_DELAY_CONFIG_IN_MILLIS)
.setMaxDelay(
ObjectStoreConstants.OBJECT_STORE_MAX_RETRY_DELAY_CONFIG_IN_SECONDS)
.setMaxRetries(
ObjectStoreConstants.OBJECT_STORE_MAX_ATTEMPTS_CONFIG);

return new RetryOptions(exponentialBackoffOptions);
}

private int removeBlobsByFilter(Predicate<? super BlobItem> filter) {
Set<String> blobNames = getEntryNames(filter);
int deletedBlobsResult = 0;

if (blobNames.isEmpty()) {
return deletedBlobsResult;
}
for (String blobName : blobNames) {
BlobClient blobClient = containerClient.getBlobClient(blobName);
if (blobClient.deleteIfExists()) {
deletedBlobsResult++;
}
}

return deletedBlobsResult;
}

protected Set<String> getEntryNames(Predicate<? super BlobItem> filter) {
BlobListDetails blobListDetails = new BlobListDetails().setRetrieveMetadata(true);
ListBlobsOptions listBlobsOptions = new ListBlobsOptions().setDetails(blobListDetails);

return containerClient.listBlobs(listBlobsOptions, ObjectStoreConstants.OBJECT_STORE_TOTAL_TIMEOUT_CONFIG_IN_MINUTES)
.stream()
.filter(filter)
.map(BlobItem::getName)
.collect(Collectors.toSet());
}

public Set<String> getAllEntriesNames() {
ListBlobsOptions listOptions = new ListBlobsOptions();
return containerClient.listBlobs(listOptions, ObjectStoreConstants.OBJECT_STORE_TOTAL_TIMEOUT_CONFIG_IN_MINUTES)
.stream()
.map(BlobItem::getName)
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.InputStream;
import java.nio.channels.Channels;
import java.text.MessageFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Base64;
Expand All @@ -28,6 +27,7 @@
import com.google.cloud.storage.StorageRetryStrategy;
import org.cloudfoundry.multiapps.controller.persistence.Messages;
import org.cloudfoundry.multiapps.controller.persistence.model.FileEntry;
import org.cloudfoundry.multiapps.controller.persistence.util.ObjectStoreConstants;
import org.cloudfoundry.multiapps.controller.persistence.util.ObjectStoreFilter;
import org.cloudfoundry.multiapps.controller.persistence.util.ObjectStoreMapper;
import org.springframework.http.MediaType;
Expand All @@ -36,13 +36,8 @@ public class GcpObjectStoreFileStorage implements FileStorage {

private final String bucketName;
private final Storage storage;
private static final String BUCKET = "bucket";
private static final int OBJECT_STORE_MAX_ATTEMPTS_CONFIG = 6;
private static final double OBJECT_STORE_RETRY_DELAY_MULTIPLIER_CONFIG = 2.0;
private static final Duration OBJECT_STORE_TOTAL_TIMEOUT_CONFIG_IN_MINUTES = Duration.ofMinutes(10);
private static final Duration OBJECT_STORE_MAX_RETRY_DELAY_CONFIG_IN_SECONDS = Duration.ofSeconds(10);
private static final Duration OBJECT_STORE_INITIAL_RETRY_DELAY_CONFIG_IN_MILLIS = Duration.ofMillis(250);
private static final String BASE_64_ENCODED_PRIVATE_KEY_DATA = "base64EncodedPrivateKeyData";
private static final String BUCKET = "bucket";

public GcpObjectStoreFileStorage(Map<String, Object> credentials) {
this.bucketName = (String) credentials.get(BUCKET);
Expand All @@ -55,11 +50,12 @@ protected Storage createObjectStoreStorage(Map<String, Object> credentials) {
.setStorageRetryStrategy(StorageRetryStrategy.getUniformStorageRetryStrategy())
.setRetrySettings(
RetrySettings.newBuilder()
.setMaxAttempts(OBJECT_STORE_MAX_ATTEMPTS_CONFIG)
.setTotalTimeoutDuration(OBJECT_STORE_TOTAL_TIMEOUT_CONFIG_IN_MINUTES)
.setMaxRetryDelayDuration(OBJECT_STORE_MAX_RETRY_DELAY_CONFIG_IN_SECONDS)
.setInitialRetryDelayDuration(OBJECT_STORE_INITIAL_RETRY_DELAY_CONFIG_IN_MILLIS)
.setRetryDelayMultiplier(OBJECT_STORE_RETRY_DELAY_MULTIPLIER_CONFIG)
.setMaxAttempts(ObjectStoreConstants.OBJECT_STORE_MAX_ATTEMPTS_CONFIG)
.setTotalTimeoutDuration(ObjectStoreConstants.OBJECT_STORE_TOTAL_TIMEOUT_CONFIG_IN_MINUTES)
.setMaxRetryDelayDuration(ObjectStoreConstants.OBJECT_STORE_MAX_RETRY_DELAY_CONFIG_IN_SECONDS)
.setInitialRetryDelayDuration(
ObjectStoreConstants.OBJECT_STORE_INITIAL_RETRY_DELAY_CONFIG_IN_MILLIS)
.setRetryDelayMultiplier(ObjectStoreConstants.OBJECT_STORE_RETRY_DELAY_MULTIPLIER_CONFIG)
.build())
.build()
.getService();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.cloudfoundry.multiapps.controller.persistence.util;

import java.time.Duration;

public class ObjectStoreConstants {

private ObjectStoreConstants() {
}

public static final int OBJECT_STORE_MAX_ATTEMPTS_CONFIG = 6;
public static final double OBJECT_STORE_RETRY_DELAY_MULTIPLIER_CONFIG = 2.0;
public static final Duration OBJECT_STORE_TOTAL_TIMEOUT_CONFIG_IN_MINUTES = Duration.ofMinutes(10);
public static final Duration OBJECT_STORE_MAX_RETRY_DELAY_CONFIG_IN_SECONDS = Duration.ofSeconds(10);
public static final Duration OBJECT_STORE_INITIAL_RETRY_DELAY_CONFIG_IN_MILLIS = Duration.ofMillis(250);
}
Loading