-
Notifications
You must be signed in to change notification settings - Fork 9
[ENG-35624]: Add azure storage support for metadata extractor #180
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a155f85
[ENG-35624]: Add azure storage support for metadata extractor
5891e31
Fix code
04a5dc3
Remove unwanted changes
d5d8124
Add comment
9719192
Add comments
df0a506
Remove sonar issue
ef040cb
Add unit tests
8dd1f3f
Fix comment
d938d2e
Fix configs
e716522
Use dfs in endpoint instead of blob
2bedb79
Fix liberary versions
949ae89
Add debugging logs in azure client
5601483
Update dependency to latest stable
948d834
Fix lib
c4819cb
Remove debug loggong
4bf29ce
Fix uri to blob
6242061
Use dfs client
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
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
28 changes: 28 additions & 0 deletions
28
lakeview/src/main/java/ai/onehouse/config/models/common/AzureConfig.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,28 @@ | ||
| package ai.onehouse.config.models.common; | ||
|
|
||
| import java.util.Optional; | ||
| import lombok.Builder; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.NonNull; | ||
| import lombok.extern.jackson.Jacksonized; | ||
|
|
||
| @Builder | ||
| @Getter | ||
| @Jacksonized | ||
| @EqualsAndHashCode | ||
| public class AzureConfig { | ||
| @NonNull private String accountName; | ||
|
|
||
| // Optional authentication methods | ||
| // Option 1: Account Key (for dev/testing, never expires) | ||
| @Builder.Default private Optional<String> accountKey = Optional.empty(); | ||
|
|
||
| // Option 2: Connection String (alternative to account key) | ||
| @Builder.Default private Optional<String> connectionString = Optional.empty(); | ||
|
|
||
| // Option 3: Service Principal | ||
| @Builder.Default private Optional<String> tenantId = Optional.empty(); | ||
| @Builder.Default private Optional<String> clientId = Optional.empty(); | ||
| @Builder.Default private Optional<String> clientSecret = Optional.empty(); | ||
| } | ||
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
233 changes: 233 additions & 0 deletions
233
lakeview/src/main/java/ai/onehouse/storage/AzureAsyncStorageClient.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,233 @@ | ||
| package ai.onehouse.storage; | ||
|
|
||
| import ai.onehouse.exceptions.AccessDeniedException; | ||
| import ai.onehouse.exceptions.NoSuchKeyException; | ||
| import ai.onehouse.exceptions.ObjectStorageClientException; | ||
| import ai.onehouse.exceptions.RateLimitException; | ||
| import ai.onehouse.storage.models.File; | ||
| import ai.onehouse.storage.models.FileStreamData; | ||
| import ai.onehouse.storage.providers.AzureStorageClientProvider; | ||
| import com.azure.core.http.rest.PagedFlux; | ||
| import com.azure.core.http.rest.PagedResponse; | ||
| import com.azure.core.util.BinaryData; | ||
| import com.azure.storage.file.datalake.DataLakeDirectoryAsyncClient; | ||
| import com.azure.storage.file.datalake.DataLakeFileAsyncClient; | ||
| import com.azure.storage.file.datalake.DataLakeFileSystemAsyncClient; | ||
| import com.azure.storage.file.datalake.DataLakeServiceAsyncClient; | ||
| import com.azure.storage.file.datalake.models.DataLakeRequestConditions; | ||
| import com.azure.storage.file.datalake.models.DataLakeStorageException; | ||
| import com.azure.storage.file.datalake.models.ListPathsOptions; | ||
| import com.azure.storage.file.datalake.models.PathItem; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.inject.Inject; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ExecutorService; | ||
| import javax.annotation.Nonnull; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.commons.lang3.tuple.Pair; | ||
|
|
||
| @Slf4j | ||
| public class AzureAsyncStorageClient extends AbstractAsyncStorageClient { | ||
| private final AzureStorageClientProvider azureStorageClientProvider; | ||
|
|
||
| @Inject | ||
| public AzureAsyncStorageClient( | ||
| @Nonnull AzureStorageClientProvider azureStorageClientProvider, | ||
| @Nonnull StorageUtils storageUtils, | ||
| @Nonnull ExecutorService executorService) { | ||
| super(executorService, storageUtils); | ||
| this.azureStorageClientProvider = azureStorageClientProvider; | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Pair<String, List<File>>> fetchObjectsByPage( | ||
| String containerName, String prefix, String continuationToken, String startAfter) { | ||
|
|
||
| log.debug( | ||
| "fetching files in container {} with prefix {} continuationToken {} startAfter {}", | ||
| containerName, | ||
| prefix, | ||
| continuationToken, | ||
| startAfter); | ||
|
|
||
| return CompletableFuture.supplyAsync( | ||
| () -> { | ||
| try { | ||
| DataLakeServiceAsyncClient dataLakeServiceClient = | ||
| azureStorageClientProvider.getAzureAsyncClient(); | ||
| DataLakeFileSystemAsyncClient fileSystemClient = | ||
| dataLakeServiceClient.getFileSystemAsyncClient(containerName); | ||
|
|
||
| ListPathsOptions options = new ListPathsOptions(); | ||
| if (StringUtils.isNotBlank(prefix)) { | ||
| options.setPath(prefix); | ||
| } | ||
|
|
||
| PagedFlux<PathItem> pagedFlux = fileSystemClient.listPaths(options); | ||
|
|
||
| List<File> files = new ArrayList<>(); | ||
| String nextContinuationToken = null; | ||
|
|
||
| // Get single page with continuation token | ||
| try (PagedResponse<PathItem> page = | ||
| StringUtils.isNotBlank(continuationToken) | ||
| ? pagedFlux.byPage(continuationToken).blockFirst() | ||
| : pagedFlux.byPage().blockFirst()) { | ||
|
|
||
| if (page != null) { | ||
| // Process items in the page | ||
| page.getElements() | ||
| .forEach( | ||
| pathItem -> { | ||
| String pathName = pathItem.getName(); | ||
| boolean isDirectory = pathItem.isDirectory(); | ||
| String fileName = pathName.replaceFirst("^" + prefix, ""); | ||
|
|
||
| files.add( | ||
| File.builder() | ||
| .filename(fileName) | ||
| .lastModifiedAt( | ||
| isDirectory | ||
| ? Instant.EPOCH | ||
| : pathItem.getLastModified().toInstant()) | ||
| .isDirectory(isDirectory) | ||
| .build()); | ||
| }); | ||
|
|
||
| // Get continuation token for next page | ||
| nextContinuationToken = page.getContinuationToken(); | ||
| } | ||
| } | ||
|
|
||
| return Pair.of(nextContinuationToken, files); | ||
| } catch (Exception ex) { | ||
| return handleListPathsException(ex, containerName, prefix); | ||
| } | ||
| }, | ||
| executorService); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| CompletableFuture<BinaryData> readBlob(String azureUri) { | ||
| log.debug("Reading Azure Data Lake file: {}", azureUri); | ||
| return CompletableFuture.supplyAsync( | ||
| () -> { | ||
| try { | ||
| DataLakeFileAsyncClient fileClient = getFileClient(azureUri); | ||
| return BinaryData.fromBytes(fileClient.read().blockLast().array()); | ||
| } catch (Exception ex) { | ||
| log.error("Failed to read file", ex); | ||
| throw clientException(ex, "readBlob", azureUri); | ||
| } | ||
| }, | ||
| executorService); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<FileStreamData> streamFileAsync(String azureUri) { | ||
| return readBlob(azureUri) | ||
| .thenApply( | ||
| binaryData -> | ||
| FileStreamData.builder() | ||
| .inputStream(new ByteArrayInputStream(binaryData.toBytes())) | ||
| .fileSize((long) binaryData.toBytes().length) | ||
| .build()); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<byte[]> readFileAsBytes(String azureUri) { | ||
| return readBlob(azureUri).thenApply(BinaryData::toBytes); | ||
| } | ||
|
|
||
| private DataLakeFileAsyncClient getFileClient(String azureUri) { | ||
| String fileSystemName = storageUtils.getBucketNameFromUri(azureUri); | ||
| String filePath = storageUtils.getPathFromUrl(azureUri); | ||
|
|
||
| DataLakeServiceAsyncClient dataLakeServiceClient = azureStorageClientProvider.getAzureAsyncClient(); | ||
| DataLakeFileSystemAsyncClient fileSystemClient = | ||
| dataLakeServiceClient.getFileSystemAsyncClient(fileSystemName); | ||
| return fileSystemClient.getFileAsyncClient(filePath); | ||
| } | ||
|
|
||
| private Pair<String, List<File>> handleListPathsException( | ||
| Exception ex, String containerName, String prefix) { | ||
| // DataLake API returns 404 for non-existent paths, treat as empty directory | ||
| Throwable wrappedException = ex.getCause() != null ? ex.getCause() : ex; | ||
| if (wrappedException instanceof DataLakeStorageException) { | ||
| DataLakeStorageException dlsException = (DataLakeStorageException) wrappedException; | ||
| if ("PathNotFound".equals(dlsException.getErrorCode()) | ||
| || dlsException.getStatusCode() == 404) { | ||
| log.debug( | ||
| "Path not found, returning empty list for container: {}, prefix: {}", | ||
| containerName, | ||
| prefix); | ||
| return Pair.of(null, new ArrayList<>()); | ||
| } | ||
| } | ||
| log.error("Failed to fetch objects by page", ex); | ||
| throw clientException(ex, "fetchObjectsByPage", containerName); | ||
| } | ||
|
|
||
| @Override | ||
| protected RuntimeException clientException(Throwable ex, String operation, String path) { | ||
| Throwable wrappedException = ex.getCause() != null ? ex.getCause() : ex; | ||
|
|
||
| if (wrappedException instanceof DataLakeStorageException) { | ||
| DataLakeStorageException dataLakeException = (DataLakeStorageException) wrappedException; | ||
| String errorCode = dataLakeException.getErrorCode(); | ||
| int statusCode = dataLakeException.getStatusCode(); | ||
|
|
||
| log.error( | ||
| "Error in Azure Data Lake operation: {} on path: {} code: {} status: {} message: {}", | ||
| operation, | ||
| path, | ||
| errorCode, | ||
| statusCode, | ||
| dataLakeException.getMessage()); | ||
|
|
||
| // Map to AccessDeniedException | ||
| if (statusCode == 403 || statusCode == 401) { | ||
| return new AccessDeniedException( | ||
| String.format( | ||
| "AccessDenied for operation: %s on path: %s with message: %s", | ||
| operation, path, dataLakeException.getMessage())); | ||
| } | ||
|
|
||
| // Map to NoSuchKeyException | ||
| if (errorCode != null | ||
| && (errorCode.equals("PathNotFound") | ||
| || errorCode.equals("FilesystemNotFound") | ||
| || statusCode == 404)) { | ||
| return new NoSuchKeyException( | ||
| String.format("NoSuchKey for operation: %s on path: %s", operation, path)); | ||
| } | ||
|
|
||
| // Map to RateLimitException | ||
| if (statusCode == 429 || statusCode == 503) { | ||
| return new RateLimitException( | ||
| String.format("Throttled by Azure for operation: %s on path: %s", operation, path)); | ||
| } | ||
| } else if (wrappedException instanceof AccessDeniedException | ||
| || wrappedException instanceof NoSuchKeyException | ||
| || wrappedException instanceof RateLimitException) { | ||
| return (RuntimeException) wrappedException; | ||
| } | ||
|
|
||
| return new ObjectStorageClientException(ex); | ||
| } | ||
|
|
||
| @Override | ||
| public void refreshClient() { | ||
| azureStorageClientProvider.refreshClient(); | ||
| } | ||
|
|
||
| @Override | ||
| public void initializeClient() { | ||
| azureStorageClientProvider.getAzureAsyncClient(); | ||
| } | ||
| } |
Oops, something went wrong.
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.