LakshSingla commented on code in PR #12874: URL: https://github.com/apache/druid/pull/12874#discussion_r940955912
########## extensions-core/s3-extensions/src/main/java/org/apache/druid/storage/s3/output/RetriableS3OutputStream.java: ########## @@ -0,0 +1,432 @@ +/* + * 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.druid.storage.s3.output; + +import com.amazonaws.AmazonServiceException; +import com.amazonaws.services.s3.model.AbortMultipartUploadRequest; +import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest; +import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest; +import com.amazonaws.services.s3.model.InitiateMultipartUploadResult; +import com.amazonaws.services.s3.model.ObjectMetadata; +import com.amazonaws.services.s3.model.PartETag; +import com.amazonaws.services.s3.model.UploadPartRequest; +import com.amazonaws.services.s3.model.UploadPartResult; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Joiner; +import com.google.common.base.Stopwatch; +import com.google.common.io.CountingOutputStream; +import it.unimi.dsi.fastutil.io.FastBufferedOutputStream; +import org.apache.druid.java.util.common.FileUtils; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.java.util.common.IOE; +import org.apache.druid.java.util.common.RetryUtils; +import org.apache.druid.java.util.common.io.Closer; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.storage.s3.S3Utils; +import org.apache.druid.storage.s3.ServerSideEncryptingAmazonS3; + +import java.io.Closeable; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +/** + * A retriable output stream for s3. How it works is, + * <p> + * 1) When new data is written, it first creates a chunk in local disk. + * 2) New data is written to the local chunk until it is full. + * 3) When the chunk is full, it uploads the chunk to s3 using the multipart upload API. + * Since this happens synchronously, {@link #write(byte[], int, int)} can be blocked until the upload is done. + * The upload can be retries when it fails with transient errors. + * 4) Once the upload succeeds, it creates a new chunk and continue. + * 5) When the stream is closed, it uploads the last chunk and finalize the multipart upload. + * {@link #close()} can be blocked until upload is done. + * <p> + * For compression format support, this output stream supports compression formats if they are <i>concatenatable</i>, + * such as ZIP or GZIP. + * <p> + * This class is not thread-safe. + * <p> + * This class can be moved to the s3 extension as a low-level API, + * whereas it currently provides only high-level APIs such as S3DataSegmentPuller. + */ +public class RetriableS3OutputStream extends OutputStream Review Comment: This class seems to be solving a wider problem of chunking the multipart upload. Can a follow up to this class be that it is generic (chunking part), and then depending on the cloud storage that we are using, their constructor/specific methods can handle the upload part? Or is this something which is specific enough to S3 that this refactoring is not required? ########## core/src/test/java/org/apache/druid/storage/LocalFileStorageConnectorTest.java: ########## @@ -0,0 +1,102 @@ +/* + * 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.druid.storage; + +import org.apache.druid.java.util.common.FileUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.UUID; + +public class LocalFileStorageConnectorTest +{ + private File tempDir = FileUtils.createTempDir(); + private StorageConnector storageConnector = new LocalFileStorageConnectorProvider(tempDir.getPath()).get(); + + public LocalFileStorageConnectorTest() + { + tempDir.deleteOnExit(); + } + + @Test + public void sanityCheck() throws IOException + { + String uuid = UUID.randomUUID().toString(); + + //create file + createFile(uuid); + + // check if file is created + Assert.assertTrue(storageConnector.pathExists(uuid)); + Assert.assertTrue(new File(tempDir.getAbsolutePath() + "/" + uuid).exists()); + + // check contents + checkContents(uuid); + + // delete file + storageConnector.delete(uuid); + Assert.assertFalse(new File(tempDir.getAbsolutePath() + "/" + uuid).exists()); + } + + @Test + public void deleteRecursivelyTest() throws IOException + { + String uuid_base = UUID.randomUUID().toString(); + String uuid1 = uuid_base + "/" + UUID.randomUUID(); + String uuid2 = uuid_base + "/" + UUID.randomUUID(); + + createFile(uuid1); + createFile(uuid2); + + Assert.assertTrue(storageConnector.pathExists(uuid1)); + Assert.assertTrue(storageConnector.pathExists(uuid2)); + + checkContents(uuid1); + checkContents(uuid2); + + File baseFile = new File(tempDir.getAbsolutePath() + "/" + uuid_base); + Assert.assertTrue(baseFile.exists()); + Assert.assertTrue(baseFile.isDirectory()); + Assert.assertEquals(2, baseFile.listFiles().length); + + storageConnector.deleteRecursively(uuid_base); + Assert.assertFalse(baseFile.exists()); + + } + + private void checkContents(String uuid) throws IOException + { + InputStream inputStream = storageConnector.read(uuid); + Assert.assertEquals(1, inputStream.read()); + Assert.assertEquals(0, inputStream.available()); + inputStream.close(); + } + + private void createFile(String uuid) throws IOException Review Comment: Since this method is populating the file with some data as well, should this be called something like `createAndPopulateFile`? ########## extensions-core/s3-extensions/src/main/java/org/apache/druid/storage/s3/output/RetriableS3OutputStream.java: ########## @@ -0,0 +1,432 @@ +/* + * 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.druid.storage.s3.output; + +import com.amazonaws.AmazonServiceException; +import com.amazonaws.services.s3.model.AbortMultipartUploadRequest; +import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest; +import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest; +import com.amazonaws.services.s3.model.InitiateMultipartUploadResult; +import com.amazonaws.services.s3.model.ObjectMetadata; +import com.amazonaws.services.s3.model.PartETag; +import com.amazonaws.services.s3.model.UploadPartRequest; +import com.amazonaws.services.s3.model.UploadPartResult; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Joiner; +import com.google.common.base.Stopwatch; +import com.google.common.io.CountingOutputStream; +import it.unimi.dsi.fastutil.io.FastBufferedOutputStream; +import org.apache.druid.java.util.common.FileUtils; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.java.util.common.IOE; +import org.apache.druid.java.util.common.RetryUtils; +import org.apache.druid.java.util.common.io.Closer; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.storage.s3.S3Utils; +import org.apache.druid.storage.s3.ServerSideEncryptingAmazonS3; + +import java.io.Closeable; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +/** + * A retriable output stream for s3. How it works is, + * <p> + * 1) When new data is written, it first creates a chunk in local disk. + * 2) New data is written to the local chunk until it is full. + * 3) When the chunk is full, it uploads the chunk to s3 using the multipart upload API. + * Since this happens synchronously, {@link #write(byte[], int, int)} can be blocked until the upload is done. + * The upload can be retries when it fails with transient errors. + * 4) Once the upload succeeds, it creates a new chunk and continue. + * 5) When the stream is closed, it uploads the last chunk and finalize the multipart upload. + * {@link #close()} can be blocked until upload is done. + * <p> + * For compression format support, this output stream supports compression formats if they are <i>concatenatable</i>, + * such as ZIP or GZIP. + * <p> + * This class is not thread-safe. + * <p> + * This class can be moved to the s3 extension as a low-level API, + * whereas it currently provides only high-level APIs such as S3DataSegmentPuller. + */ +public class RetriableS3OutputStream extends OutputStream +{ + public static final long S3_MULTIPART_UPLOAD_MIN_PART_SIZE = 5L * 1024 * 1024; + public static final long S3_MULTIPART_UPLOAD_MAX_PART_SIZE = 5L * 1024 * 1024 * 1024L; + + private static final Logger LOG = new Logger(RetriableS3OutputStream.class); + private static final Joiner JOINER = Joiner.on("/").skipNulls(); + private static final int S3_MULTIPART_UPLOAD_MAX_NUM_PARTS = 10_000; + + private final S3OutputConfig config; + private final ServerSideEncryptingAmazonS3 s3; + private final String s3Key; + private final String uploadId; + private final File chunkStorePath; + private final long chunkSize; + + private final List<PartETag> pushResults = new ArrayList<>(); + private final byte[] singularBuffer = new byte[1]; + + // metric + private final Stopwatch pushStopwatch; + + private Chunk currentChunk; + private int nextChunkId = 1; // multipart upload requires partNumber to be in the range between 1 and 10000 + private int numChunksPushed; + /** + * Total size of all chunks. This size is updated whenever the chunk is ready for push, + * not when {@link #write(byte[], int, int)} is called. This is because + * it will be hard to know the increase of chunk size in write() when the chunk is compressed. Review Comment: Shouldn't the chunk size decrease in compression? ########## core/src/main/java/org/apache/druid/storage/StorageConnector.java: ########## @@ -0,0 +1,93 @@ +/* + * 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.druid.storage; + +import org.apache.druid.guice.annotations.UnstableApi; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Low level interface for interacting with different storage providers like S3, GCS, Azure and local file system. + * For adding a new implementation of this interface in your extension extend {@link StorageConnectorProvider}. + * For using the interface in your extension as a consumer, use JsonConfigProvider like: + * 1. JsonConfigProvider.bind(binder, "druid,extension.custom.type", StorageConnectorProvider.class, Custom.class); + * // bind the storage config provider + * 2. binder.bind(Key.get(StorageConnector.class, Custom.class)).toProvider(Key.get(StorageConnectorProvider.class, Custom.class)).in(LazySingleton.class); + * // Use Named annotations to access the storageConnector instance in your custom extension. + * 3. @Custom StorageConnector storageConnector + * <p> + * The final state of this inteface would have + * * 1. Future Non blocking API's + * * 2. Offset based fetch + */ + +@UnstableApi +public interface StorageConnector +{ + + /** + * Check if the relative path exists in the underlying storage layer. + * + * @param path + * @return true if path exists else false. + * @throws IOException + */ + @SuppressWarnings("all") + boolean pathExists(String path) throws IOException; + + /** + * Reads the data present at the relative path from the underlying storage system. + * The caller should take care of closing the stream when done or in case of error. + * + * @param path + * @return InputStream + * @throws IOException if the path is not present or the unable to read the data present on the path. + */ + InputStream read(String path) throws IOException; + + /** + * Open an {@link OutputStream} for writing data to the relative path in the underlying storage system. + * The caller should take care of closing the stream when done or in case of error. + * + * @param path + * @return + * @throws IOException + */ + OutputStream write(String path) throws IOException; + + /** + * Delete file present at the relative path. + * + * @param path + * @throws IOException + */ + @SuppressWarnings("all") + void delete(String path) throws IOException; + + /** + * Delete a directory pointed to by the path and also recursively deletes all files in said directory. + * + * @param path path + * @throws IOException + */ + void deleteRecursively(String path) throws IOException; Review Comment: Is `@SuppressWarnings` required here? ########## core/src/test/java/org/apache/druid/storage/LocalFileStorageConnectorTest.java: ########## @@ -0,0 +1,102 @@ +/* + * 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.druid.storage; + +import org.apache.druid.java.util.common.FileUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.UUID; + +public class LocalFileStorageConnectorTest +{ + private File tempDir = FileUtils.createTempDir(); + private StorageConnector storageConnector = new LocalFileStorageConnectorProvider(tempDir.getPath()).get(); + + public LocalFileStorageConnectorTest() + { + tempDir.deleteOnExit(); + } + + @Test + public void sanityCheck() throws IOException + { + String uuid = UUID.randomUUID().toString(); + + //create file + createFile(uuid); + + // check if file is created + Assert.assertTrue(storageConnector.pathExists(uuid)); + Assert.assertTrue(new File(tempDir.getAbsolutePath() + "/" + uuid).exists()); + + // check contents + checkContents(uuid); + + // delete file + storageConnector.delete(uuid); + Assert.assertFalse(new File(tempDir.getAbsolutePath() + "/" + uuid).exists()); + } + + @Test + public void deleteRecursivelyTest() throws IOException + { + String uuid_base = UUID.randomUUID().toString(); + String uuid1 = uuid_base + "/" + UUID.randomUUID(); Review Comment: I think `Paths.get()` or some other API that handles the filename nuances might be cleaner than manually joining the path. ########## core/src/main/java/org/apache/druid/storage/LocalFileStorageConnector.java: ########## @@ -0,0 +1,79 @@ +/* + * 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.druid.storage; + +import org.apache.druid.java.util.common.FileUtils; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; + +public class LocalFileStorageConnector implements StorageConnector Review Comment: Can you please add Javadocs for this class and its methods, and also the behavior of methods like `read` and `write` on already existing files, or paths that do not exist? -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
