-
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
Open
Yavor16
wants to merge
2
commits into
master
Choose a base branch
from
adopt-azure-sdk
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 213 additions & 0 deletions
213
...g/cloudfoundry/multiapps/controller/persistence/services/AzureObjectStoreFileStorage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| 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()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ain/java/org/cloudfoundry/multiapps/controller/persistence/util/ObjectStoreConstants.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.