-
Notifications
You must be signed in to change notification settings - Fork 41
Adopt Azure sdk for Objectstore #1781
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
IvanBorislavovDimitrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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)); | ||
|
Check warning on line 160 in multiapps-controller-persistence/src/main/java/org/cloudfoundry/multiapps/controller/persistence/services/AzureObjectStoreFileStorage.java
|
||
| return new URL(containerUri.getProtocol(), containerUri.getHost(), containerUri.getPort(), "").toString(); | ||
|
Check warning on line 161 in multiapps-controller-persistence/src/main/java/org/cloudfoundry/multiapps/controller/persistence/services/AzureObjectStoreFileStorage.java
|
||
| } 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 |
|---|---|---|
| @@ -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); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.