snleee commented on a change in pull request #5116: Add Azure Data Lake Gen2 connector for PinotFS URL: https://github.com/apache/incubator-pinot/pull/5116#discussion_r389157324
########## File path: pinot-plugins/pinot-file-system/pinot-adls/src/main/java/org/apache/pinot/plugin/filesystem/AzureGen2PinotFS.java ########## @@ -0,0 +1,447 @@ +/** + * 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.pinot.plugin.filesystem; + +import com.azure.core.http.rest.PagedIterable; +import com.azure.core.util.Context; +import com.azure.storage.blob.BlobClient; +import com.azure.storage.blob.BlobServiceClient; +import com.azure.storage.blob.BlobServiceClientBuilder; +import com.azure.storage.common.StorageSharedKeyCredential; +import com.azure.storage.common.Utility; +import com.azure.storage.file.datalake.DataLakeFileClient; +import com.azure.storage.file.datalake.DataLakeFileSystemClient; +import com.azure.storage.file.datalake.DataLakeServiceClient; +import com.azure.storage.file.datalake.DataLakeServiceClientBuilder; +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.PathHttpHeaders; +import com.azure.storage.file.datalake.models.PathItem; +import com.azure.storage.file.datalake.models.PathProperties; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLDecoder; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.sql.Timestamp; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.util.Arrays; +import java.util.Map; +import org.apache.commons.configuration.Configuration; +import org.apache.commons.io.FileUtils; +import org.apache.pinot.spi.filesystem.PinotFS; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Azure Data Lake Storage Gen2 implementation for the PinotFS interface. + */ +public class AzureGen2PinotFS extends PinotFS { + private static final Logger LOGGER = LoggerFactory.getLogger(AzureGen2PinotFS.class); + + private static final String ACCOUNT_NAME = "accountName"; + private static final String ACCESS_KEY = "accessKey"; + private static final String FILE_SYSTEM_NAME = "fileSystemName"; + + private static final String HTTPS_URL_PREFIX = "https://"; + private static final String DIRECTORY_DELIMITER = "/"; + + private static final String AZURE_STORAGE_DNS_SUFFIX = ".dfs.core.windows.net"; + private static final String AZURE_BLOB_DNS_SUFFIX = ".blob.core.windows.net"; + private static final String PATH_ALREADY_EXISTS_ERROR_CODE = "PathAlreadyExists"; + private static final String IS_DIRECTORY_KEY = "hdi_isfolder"; + + private static final int NOT_FOUND_STATUS_CODE = 404; + private static final int ALREADY_EXISTS_STATUS_CODE = 409; + + // Azure Data Lake Gen2's block size is 4MB + private static final int BUFFER_SIZE = 4 * 1024 * 1024; + + private DataLakeFileSystemClient _fileSystemClient; + private BlobServiceClient _blobServiceClient; + + @Override + public void init(Configuration config) { + // Azure storage account name + String accountName = config.getString(ACCOUNT_NAME); + String accessKey = config.getString(ACCESS_KEY); + String fileSystemName = config.getString(FILE_SYSTEM_NAME); + String dfsServiceEndpointUrl = HTTPS_URL_PREFIX + accountName + AZURE_STORAGE_DNS_SUFFIX; + String blobServiceEndpointUrl = HTTPS_URL_PREFIX + accountName + AZURE_BLOB_DNS_SUFFIX; + + StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(accountName, accessKey); + + DataLakeServiceClient serviceClient = new DataLakeServiceClientBuilder().credential(sharedKeyCredential) + .endpoint(dfsServiceEndpointUrl) + .buildClient(); + + _blobServiceClient = + new BlobServiceClientBuilder().credential(sharedKeyCredential).endpoint(blobServiceEndpointUrl).buildClient(); + _fileSystemClient = serviceClient.getFileSystemClient(fileSystemName); + LOGGER.info("AzureGen2PinotFS is initialized (accountName={}, fileSystemName={}, dfsServiceEndpointUrl={}, " + + "blobServiceEndpointUrl={})", accountName, fileSystemName, dfsServiceEndpointUrl, blobServiceEndpointUrl); + } + + @Override + public boolean mkdir(URI uri) throws IOException { + LOGGER.info("mkdir is called with uri='{}'", uri); + try { + // By default, create directory call will overwrite if the path already exists. Setting IfNoneMatch = "*" to + // prevent overwrite. https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/create + DataLakeRequestConditions requestConditions = new DataLakeRequestConditions().setIfNoneMatch("*"); + _fileSystemClient.createDirectoryWithResponse(convertUriToAzureStylePath(uri), null, null, null, null, + requestConditions, null, null); + return true; + } catch (DataLakeStorageException e) { + // If the path already exists, doing nothing and return true + if (e.getStatusCode() == ALREADY_EXISTS_STATUS_CODE && e.getErrorCode().equals(PATH_ALREADY_EXISTS_ERROR_CODE)) { + return true; + } + LOGGER.error("Exception thrown while calling mkdir.", e); + throw new IOException(e); + } + } + + @Override + public boolean delete(URI segmentUri, boolean forceDelete) throws IOException { + LOGGER.info("delete is called with segmentUri='{}', forceDelete='{}'", segmentUri, forceDelete); + try { + boolean isDirectory = isDirectory(segmentUri); + if (isDirectory && listFiles(segmentUri, false).length > 0 && !forceDelete) { + return false; + } + + String path = convertUriToAzureStylePath(segmentUri); + if (isDirectory) { + _fileSystemClient.deleteDirectoryWithResponse(path, true, null, null, Context.NONE).getValue(); + } else { + _fileSystemClient.deleteFile(path); + } + return true; + } catch (DataLakeStorageException e) { + throw new IOException(e); + } + } + + @Override + public boolean doMove(URI srcUri, URI dstUri) throws IOException { + LOGGER.info("doMove is called with srcUri='{}', dstUri='{}'", srcUri, dstUri); + try { + // TODO: currently, azure-sdk has a bug in "rename" when the path includes some special characters that gets Review comment: the comment already have the issue linked. As for atomic moves, pinotFS interface doesn't require us to implement `move()` as an atomic operation. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
