seropian commented on code in PR #2409:
URL: https://github.com/apache/jackrabbit-oak/pull/2409#discussion_r2426527663


##########
oak-blob-cloud-azure/src/main/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/v8/AzureBlobStoreBackendV8.java:
##########
@@ -0,0 +1,1319 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.v8;
+
+import static java.lang.Thread.currentThread;
+import static 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants.AZURE_BLOB_BUFFERED_STREAM_THRESHOLD;
+import static 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants.AZURE_BLOB_DEFAULT_CONCURRENT_REQUEST_COUNT;
+import static 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants.AZURE_BLOB_LAST_MODIFIED_KEY;
+import static 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants.AZURE_BLOB_MAX_ALLOWABLE_UPLOAD_URIS;
+import static 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants.AZURE_BLOB_MAX_BINARY_UPLOAD_SIZE;
+import static 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants.AZURE_BLOB_MAX_CONCURRENT_REQUEST_COUNT;
+import static 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants.AZURE_BLOB_MAX_MULTIPART_UPLOAD_PART_SIZE;
+import static 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants.AZURE_BLOB_MAX_SINGLE_PUT_UPLOAD_SIZE;
+import static 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants.AZURE_BlOB_META_DIR_NAME;
+import static 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants.AZURE_BLOB_META_KEY_PREFIX;
+import static 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants.AZURE_BLOB_MIN_MULTIPART_UPLOAD_PART_SIZE;
+import static 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants.AZURE_BLOB_REF_KEY;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.security.InvalidKeyException;
+import java.time.Instant;
+import java.util.Collection;
+import java.util.EnumSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Queue;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Function;
+
+import org.apache.jackrabbit.oak.commons.time.Stopwatch;
+
+import com.google.common.base.Strings;
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.collect.AbstractIterator;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.microsoft.azure.storage.AccessCondition;
+import com.microsoft.azure.storage.LocationMode;
+import com.microsoft.azure.storage.ResultContinuation;
+import com.microsoft.azure.storage.ResultSegment;
+import com.microsoft.azure.storage.RetryPolicy;
+import com.microsoft.azure.storage.StorageException;
+import com.microsoft.azure.storage.blob.BlobListingDetails;
+import com.microsoft.azure.storage.blob.BlobRequestOptions;
+import com.microsoft.azure.storage.blob.BlockEntry;
+import com.microsoft.azure.storage.blob.BlockListingFilter;
+import com.microsoft.azure.storage.blob.CloudBlob;
+import com.microsoft.azure.storage.blob.CloudBlobContainer;
+import com.microsoft.azure.storage.blob.CloudBlobDirectory;
+import com.microsoft.azure.storage.blob.CloudBlockBlob;
+import com.microsoft.azure.storage.blob.CopyStatus;
+import com.microsoft.azure.storage.blob.ListBlobItem;
+import com.microsoft.azure.storage.blob.SharedAccessBlobHeaders;
+import com.microsoft.azure.storage.blob.SharedAccessBlobPermissions;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.jackrabbit.core.data.DataIdentifier;
+import org.apache.jackrabbit.core.data.DataRecord;
+import org.apache.jackrabbit.core.data.DataStoreException;
+import 
org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AbstractAzureBlobStoreBackend;
+import org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureConstants;
+import org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.Utils;
+import org.apache.jackrabbit.oak.commons.PropertiesUtil;
+import org.apache.jackrabbit.oak.commons.conditions.Validate;
+import 
org.apache.jackrabbit.oak.plugins.blob.datastore.directaccess.DataRecordDownloadOptions;
+import 
org.apache.jackrabbit.oak.plugins.blob.datastore.directaccess.DataRecordUpload;
+import 
org.apache.jackrabbit.oak.plugins.blob.datastore.directaccess.DataRecordUploadException;
+import 
org.apache.jackrabbit.oak.plugins.blob.datastore.directaccess.DataRecordUploadOptions;
+import 
org.apache.jackrabbit.oak.plugins.blob.datastore.directaccess.DataRecordUploadToken;
+import org.apache.jackrabbit.oak.spi.blob.AbstractDataRecord;
+import org.apache.jackrabbit.oak.spi.blob.AbstractSharedBackend;
+import org.apache.jackrabbit.util.Base64;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class AzureBlobStoreBackendV8 extends AbstractAzureBlobStoreBackend {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(AzureBlobStoreBackendV8.class);
+    private static final Logger LOG_STREAMS_DOWNLOAD = 
LoggerFactory.getLogger("oak.datastore.download.streams");
+    private static final Logger LOG_STREAMS_UPLOAD = 
LoggerFactory.getLogger("oak.datastore.upload.streams");
+
+    private Properties properties;
+    private AzureBlobContainerProviderV8 azureBlobContainerProvider;
+    private int concurrentRequestCount = 
AZURE_BLOB_DEFAULT_CONCURRENT_REQUEST_COUNT;
+    private RetryPolicy retryPolicy;
+    private Integer requestTimeout;
+    private int httpDownloadURIExpirySeconds = 0; // disabled by default
+    private int httpUploadURIExpirySeconds = 0; // disabled by default
+    private String uploadDomainOverride = null;
+    private String downloadDomainOverride = null;
+    private boolean createBlobContainer = true;
+    private boolean presignedDownloadURIVerifyExists = true;
+    private boolean enableSecondaryLocation = 
AzureConstants.AZURE_BLOB_ENABLE_SECONDARY_LOCATION_DEFAULT;
+
+    private Cache<String, URI> httpDownloadURICache;
+
+    private byte[] secret;
+
+    public void setProperties(final Properties properties) {
+        this.properties = properties;
+    }
+
+    private final AtomicReference<CloudBlobContainer> azureContainerReference 
= new AtomicReference<>();
+
+    public CloudBlobContainer getAzureContainer() throws DataStoreException {
+        azureContainerReference.compareAndSet(null, 
azureBlobContainerProvider.getBlobContainer(getBlobRequestOptions()));
+        return azureContainerReference.get();
+    }
+
+    @NotNull
+    protected BlobRequestOptions getBlobRequestOptions() {
+        BlobRequestOptions requestOptions = new BlobRequestOptions();
+        if (null != retryPolicy) {
+            requestOptions.setRetryPolicyFactory(retryPolicy);
+        }
+        if (null != requestTimeout) {
+            requestOptions.setTimeoutIntervalInMs(requestTimeout);
+        }
+        requestOptions.setConcurrentRequestCount(concurrentRequestCount);
+        if (enableSecondaryLocation) {
+            
requestOptions.setLocationMode(LocationMode.PRIMARY_THEN_SECONDARY);
+        }
+        return requestOptions;
+    }
+
+    @Override
+    public void init() throws DataStoreException {
+        ClassLoader contextClassLoader = 
Thread.currentThread().getContextClassLoader();
+        Stopwatch watch = Stopwatch.createStarted();
+        try {
+            
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+            LOG.debug("Started backend initialization");
+
+            if (null == properties) {
+                try {
+                    properties = Utils.readConfig(UtilsV8.DEFAULT_CONFIG_FILE);
+                }
+                catch (IOException e) {
+                    throw new DataStoreException("Unable to initialize Azure 
Data Store from " + UtilsV8.DEFAULT_CONFIG_FILE, e);
+                }
+            }
+
+            try {
+                UtilsV8.setProxyIfNeeded(properties);
+                createBlobContainer = PropertiesUtil.toBoolean(
+                        
Strings.emptyToNull(properties.getProperty(AzureConstants.AZURE_CREATE_CONTAINER)),
 true);
+                initAzureDSConfig();
+
+                concurrentRequestCount = PropertiesUtil.toInteger(
+                        
properties.getProperty(AzureConstants.AZURE_BLOB_CONCURRENT_REQUESTS_PER_OPERATION),
+                        AZURE_BLOB_DEFAULT_CONCURRENT_REQUEST_COUNT);
+                if (concurrentRequestCount < 
AZURE_BLOB_DEFAULT_CONCURRENT_REQUEST_COUNT) {
+                    LOG.warn("Invalid setting [{}] for 
concurrentRequestsPerOperation (too low); resetting to {}",
+                            concurrentRequestCount,
+                             AZURE_BLOB_DEFAULT_CONCURRENT_REQUEST_COUNT);
+                    concurrentRequestCount = 
AZURE_BLOB_DEFAULT_CONCURRENT_REQUEST_COUNT;
+                } else if (concurrentRequestCount > 
AZURE_BLOB_MAX_CONCURRENT_REQUEST_COUNT) {
+                    LOG.warn("Invalid setting [{}] for 
concurrentRequestsPerOperation (too high); resetting to {}",
+                            concurrentRequestCount,
+                             AZURE_BLOB_MAX_CONCURRENT_REQUEST_COUNT);
+                    concurrentRequestCount = 
AZURE_BLOB_MAX_CONCURRENT_REQUEST_COUNT;
+                }
+                LOG.info("Using concurrentRequestsPerOperation={}", 
concurrentRequestCount);
+
+                retryPolicy = 
UtilsV8.getRetryPolicy(properties.getProperty(AzureConstants.AZURE_BLOB_MAX_REQUEST_RETRY));
+                if 
(properties.getProperty(AzureConstants.AZURE_BLOB_REQUEST_TIMEOUT) != null) {
+                    requestTimeout = 
PropertiesUtil.toInteger(properties.getProperty(AzureConstants.AZURE_BLOB_REQUEST_TIMEOUT),
 RetryPolicy.DEFAULT_CLIENT_RETRY_COUNT);
+                }
+                presignedDownloadURIVerifyExists = PropertiesUtil.toBoolean(
+                        
Strings.emptyToNull(properties.getProperty(AzureConstants.PRESIGNED_HTTP_DOWNLOAD_URI_VERIFY_EXISTS)),
 true);
+
+                enableSecondaryLocation = PropertiesUtil.toBoolean(
+                        
properties.getProperty(AzureConstants.AZURE_BLOB_ENABLE_SECONDARY_LOCATION_NAME),
+                        
AzureConstants.AZURE_BLOB_ENABLE_SECONDARY_LOCATION_DEFAULT
+                );
+
+                CloudBlobContainer azureContainer = getAzureContainer();
+
+                if (createBlobContainer && !azureContainer.exists()) {
+                    azureContainer.create();
+                    LOG.info("New container created. containerName={}", 
getContainerName());
+                } else {
+                    LOG.info("Reusing existing container. containerName={}", 
getContainerName());
+                }
+                LOG.debug("Backend initialized. duration={}", 
watch.elapsed(TimeUnit.MILLISECONDS));
+
+                // settings pertaining to DataRecordAccessProvider 
functionality
+                String putExpiry = 
properties.getProperty(AzureConstants.PRESIGNED_HTTP_UPLOAD_URI_EXPIRY_SECONDS);
+                if (null != putExpiry) {
+                    
this.setHttpUploadURIExpirySeconds(Integer.parseInt(putExpiry));
+                }
+                String getExpiry = 
properties.getProperty(AzureConstants.PRESIGNED_HTTP_DOWNLOAD_URI_EXPIRY_SECONDS);
+                if (null != getExpiry) {
+                    
this.setHttpDownloadURIExpirySeconds(Integer.parseInt(getExpiry));
+                    String cacheMaxSize = 
properties.getProperty(AzureConstants.PRESIGNED_HTTP_DOWNLOAD_URI_CACHE_MAX_SIZE);
+                    if (null != cacheMaxSize) {
+                        
this.setHttpDownloadURICacheSize(Integer.parseInt(cacheMaxSize));
+                    }
+                    else {
+                        this.setHttpDownloadURICacheSize(0); // default
+                    }
+                }
+                uploadDomainOverride = 
properties.getProperty(AzureConstants.PRESIGNED_HTTP_UPLOAD_URI_DOMAIN_OVERRIDE,
 null);
+                downloadDomainOverride = 
properties.getProperty(AzureConstants.PRESIGNED_HTTP_DOWNLOAD_URI_DOMAIN_OVERRIDE,
 null);
+
+                // Initialize reference key secret
+                boolean createRefSecretOnInit = PropertiesUtil.toBoolean(
+                        
Strings.emptyToNull(properties.getProperty(AzureConstants.AZURE_REF_ON_INIT)), 
true);
+
+                if (createRefSecretOnInit) {
+                    getOrCreateReferenceKey();
+                }
+            }
+            catch (StorageException e) {
+                throw new DataStoreException(e);

Review Comment:
   added message



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to