github-advanced-security[bot] commented on code in PR #17430:
URL: https://github.com/apache/druid/pull/17430#discussion_r1821883514


##########
extensions-core/s3-extensions/src/main/java/org/apache/druid/storage/s3/S3DataSegmentTransferUtility.java:
##########
@@ -0,0 +1,237 @@
+package org.apache.druid.storage.s3;
+
+import com.amazonaws.AmazonServiceException;
+import com.amazonaws.services.s3.model.CopyObjectRequest;
+import com.amazonaws.services.s3.model.ListObjectsV2Request;
+import com.amazonaws.services.s3.model.ListObjectsV2Result;
+import com.amazonaws.services.s3.model.S3ObjectSummary;
+import com.amazonaws.services.s3.model.StorageClass;
+import com.google.common.base.Predicate;
+import com.google.common.base.Supplier;
+import com.google.common.base.Throwables;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+import org.apache.druid.java.util.common.IOE;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.MapUtils;
+import org.apache.druid.java.util.common.RetryUtils;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.segment.loading.DataSegmentPusher;
+import org.apache.druid.segment.loading.SegmentLoadingException;
+import org.apache.druid.timeline.DataSegment;
+
+import java.io.IOException;
+import java.util.Map;
+
+public abstract class S3DataSegmentTransferUtility
+{
+  private final Supplier<ServerSideEncryptingAmazonS3> s3ClientSupplier;
+  private final S3DataSegmentPusherConfig config;
+
+  public S3DataSegmentTransferUtility(
+      S3DataSegmentPusherConfig config,
+      Supplier<ServerSideEncryptingAmazonS3> s3ClientSupplier
+  )
+  {
+    this.config = config;
+    this.s3ClientSupplier = s3ClientSupplier;
+  }
+
+  protected DataSegment transfer(DataSegment segment, Map<String, Object> 
targetLoadSpec, boolean deleteSource) throws SegmentLoadingException
+  {
+    try {
+      Map<String, Object> loadSpec = segment.getLoadSpec();
+      String s3Bucket = MapUtils.getString(loadSpec, "bucket");
+      String s3Path = MapUtils.getString(loadSpec, "key");
+
+      final String targetS3Bucket = MapUtils.getString(targetLoadSpec, 
"bucket");
+      final String targetS3BaseKey = MapUtils.getString(targetLoadSpec, 
"baseKey");
+
+      final String targetS3Path = S3Utils.constructSegmentPath(
+          targetS3BaseKey,
+          DataSegmentPusher.getDefaultStorageDir(segment, false)
+      );
+
+      if (targetS3Bucket.isEmpty()) {
+        throw new SegmentLoadingException("Target S3 bucket is not specified");
+      }
+      if (targetS3Path.isEmpty()) {
+        throw new SegmentLoadingException("Target S3 baseKey is not 
specified");
+      }
+
+      safeTransfer(s3Bucket, s3Path, targetS3Bucket, targetS3Path, 
deleteSource);
+
+      return segment.withLoadSpec(
+          ImmutableMap.<String, Object>builder()
+                      .putAll(
+                          Maps.filterKeys(
+                              loadSpec,
+                              new Predicate<String>()
+                              {
+                                @Override
+                                public boolean apply(String input)
+                                {
+                                  return !("bucket".equals(input) || 
"key".equals(input));
+                                }
+                              }
+                          )
+                      )
+                      .put("bucket", targetS3Bucket)
+                      .put("key", targetS3Path)
+                      .build()
+      );
+    }
+    catch (AmazonServiceException e) {
+      throw new SegmentLoadingException(e, "Unable to transfer segment[%s]: 
[%s]", segment.getId(), e);
+    }
+  }
+
+  private void safeTransfer(
+      final String s3Bucket,
+      final String s3Path,
+      final String targetS3Bucket,
+      final String targetS3Path,
+      final boolean deleteSource
+  ) throws SegmentLoadingException
+  {
+    try {
+      S3Utils.retryS3Operation(
+          () -> {
+            final String copyMsg = StringUtils.format(
+                "[s3://%s/%s] to [s3://%s/%s]",
+                s3Bucket,
+                s3Path,
+                targetS3Bucket,
+                targetS3Path
+            );
+            try {
+              selfCheckingTransfer(s3Bucket, targetS3Bucket, s3Path, 
targetS3Path, copyMsg, deleteSource);
+              return null;
+            }
+            catch (AmazonServiceException | IOException | 
SegmentLoadingException e) {
+              log().info(e, "Error while trying to transfer " + copyMsg);
+              throw e;
+            }
+          }
+      );
+    }
+    catch (Exception e) {
+      Throwables.propagateIfInstanceOf(e, AmazonServiceException.class);

Review Comment:
   ## Deprecated method or constructor invocation
   
   Invoking [Throwables.propagateIfInstanceOf](1) should be avoided because it 
has been deprecated.
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/8441)



##########
extensions-core/s3-extensions/src/main/java/org/apache/druid/storage/s3/S3DataSegmentTransferUtility.java:
##########
@@ -0,0 +1,237 @@
+package org.apache.druid.storage.s3;
+
+import com.amazonaws.AmazonServiceException;
+import com.amazonaws.services.s3.model.CopyObjectRequest;
+import com.amazonaws.services.s3.model.ListObjectsV2Request;
+import com.amazonaws.services.s3.model.ListObjectsV2Result;
+import com.amazonaws.services.s3.model.S3ObjectSummary;
+import com.amazonaws.services.s3.model.StorageClass;
+import com.google.common.base.Predicate;
+import com.google.common.base.Supplier;
+import com.google.common.base.Throwables;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+import org.apache.druid.java.util.common.IOE;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.MapUtils;
+import org.apache.druid.java.util.common.RetryUtils;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.segment.loading.DataSegmentPusher;
+import org.apache.druid.segment.loading.SegmentLoadingException;
+import org.apache.druid.timeline.DataSegment;
+
+import java.io.IOException;
+import java.util.Map;
+
+public abstract class S3DataSegmentTransferUtility
+{
+  private final Supplier<ServerSideEncryptingAmazonS3> s3ClientSupplier;
+  private final S3DataSegmentPusherConfig config;
+
+  public S3DataSegmentTransferUtility(
+      S3DataSegmentPusherConfig config,
+      Supplier<ServerSideEncryptingAmazonS3> s3ClientSupplier
+  )
+  {
+    this.config = config;
+    this.s3ClientSupplier = s3ClientSupplier;
+  }
+
+  protected DataSegment transfer(DataSegment segment, Map<String, Object> 
targetLoadSpec, boolean deleteSource) throws SegmentLoadingException
+  {
+    try {
+      Map<String, Object> loadSpec = segment.getLoadSpec();
+      String s3Bucket = MapUtils.getString(loadSpec, "bucket");
+      String s3Path = MapUtils.getString(loadSpec, "key");
+
+      final String targetS3Bucket = MapUtils.getString(targetLoadSpec, 
"bucket");
+      final String targetS3BaseKey = MapUtils.getString(targetLoadSpec, 
"baseKey");
+
+      final String targetS3Path = S3Utils.constructSegmentPath(
+          targetS3BaseKey,
+          DataSegmentPusher.getDefaultStorageDir(segment, false)
+      );
+
+      if (targetS3Bucket.isEmpty()) {
+        throw new SegmentLoadingException("Target S3 bucket is not specified");
+      }
+      if (targetS3Path.isEmpty()) {
+        throw new SegmentLoadingException("Target S3 baseKey is not 
specified");
+      }
+
+      safeTransfer(s3Bucket, s3Path, targetS3Bucket, targetS3Path, 
deleteSource);
+
+      return segment.withLoadSpec(
+          ImmutableMap.<String, Object>builder()
+                      .putAll(
+                          Maps.filterKeys(
+                              loadSpec,
+                              new Predicate<String>()
+                              {
+                                @Override
+                                public boolean apply(String input)
+                                {
+                                  return !("bucket".equals(input) || 
"key".equals(input));
+                                }
+                              }
+                          )
+                      )
+                      .put("bucket", targetS3Bucket)
+                      .put("key", targetS3Path)
+                      .build()
+      );
+    }
+    catch (AmazonServiceException e) {
+      throw new SegmentLoadingException(e, "Unable to transfer segment[%s]: 
[%s]", segment.getId(), e);
+    }
+  }
+
+  private void safeTransfer(
+      final String s3Bucket,
+      final String s3Path,
+      final String targetS3Bucket,
+      final String targetS3Path,
+      final boolean deleteSource
+  ) throws SegmentLoadingException
+  {
+    try {
+      S3Utils.retryS3Operation(
+          () -> {
+            final String copyMsg = StringUtils.format(
+                "[s3://%s/%s] to [s3://%s/%s]",
+                s3Bucket,
+                s3Path,
+                targetS3Bucket,
+                targetS3Path
+            );
+            try {
+              selfCheckingTransfer(s3Bucket, targetS3Bucket, s3Path, 
targetS3Path, copyMsg, deleteSource);
+              return null;
+            }
+            catch (AmazonServiceException | IOException | 
SegmentLoadingException e) {
+              log().info(e, "Error while trying to transfer " + copyMsg);
+              throw e;
+            }
+          }
+      );
+    }
+    catch (Exception e) {
+      Throwables.propagateIfInstanceOf(e, AmazonServiceException.class);
+      Throwables.propagateIfInstanceOf(e, SegmentLoadingException.class);

Review Comment:
   ## Deprecated method or constructor invocation
   
   Invoking [Throwables.propagateIfInstanceOf](1) should be avoided because it 
has been deprecated.
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/8442)



##########
extensions-core/s3-extensions/src/test/java/org/apache/druid/storage/s3/S3DataSegmentCopierTest.java:
##########
@@ -0,0 +1,290 @@
+/*
+ * 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;
+
+import com.amazonaws.services.s3.AmazonS3Client;
+import com.amazonaws.services.s3.model.AccessControlList;
+import com.amazonaws.services.s3.model.AmazonS3Exception;
+import com.amazonaws.services.s3.model.CanonicalGrantee;
+import com.amazonaws.services.s3.model.CopyObjectRequest;
+import com.amazonaws.services.s3.model.CopyObjectResult;
+import com.amazonaws.services.s3.model.Grant;
+import com.amazonaws.services.s3.model.ListObjectsV2Request;
+import com.amazonaws.services.s3.model.ListObjectsV2Result;
+import com.amazonaws.services.s3.model.Owner;
+import com.amazonaws.services.s3.model.Permission;
+import com.amazonaws.services.s3.model.PutObjectResult;
+import com.amazonaws.services.s3.model.S3ObjectSummary;
+import com.amazonaws.services.s3.model.StorageClass;
+import com.google.common.base.Suppliers;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.MapUtils;
+import org.apache.druid.segment.loading.SegmentLoadingException;
+import org.apache.druid.timeline.DataSegment;
+import org.apache.druid.timeline.partition.NoneShardSpec;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class S3DataSegmentCopierTest
+{
+  private static final DataSegment SOURCE_SEGMENT = new DataSegment(
+      "test",
+      Intervals.of("2013-01-01/2013-01-02"),
+      "1",
+      ImmutableMap.of(
+          "key",
+          
"baseKey/test/2013-01-01T00:00:00.000Z_2013-01-02T00:00:00.000Z/1/0/index.zip",
+          "bucket",
+          "main"
+      ),
+      ImmutableList.of("dim1", "dim1"),
+      ImmutableList.of("metric1", "metric2"),
+      NoneShardSpec.instance(),
+      0,
+      1
+  );
+
+  @Test
+  public void testCopy() throws Exception
+  {
+    MockAmazonS3Client mockS3Client = new MockAmazonS3Client();
+    S3DataSegmentCopier copier = new S3DataSegmentCopier(
+        Suppliers.ofInstance(mockS3Client),
+        new S3DataSegmentPusherConfig()
+    );
+
+    mockS3Client.putObject(
+        "main",
+        
"baseKey/test/2013-01-01T00:00:00.000Z_2013-01-02T00:00:00.000Z/1/0/index.zip"
+    );
+
+    DataSegment copiedSegment = copier.copy(
+        SOURCE_SEGMENT,
+        ImmutableMap.of("baseKey", "targetBaseKey", "bucket", "archive")
+    );
+
+    Map<String, Object> targetLoadSpec = copiedSegment.getLoadSpec();
+    Assert.assertEquals(
+        
"targetBaseKey/test/2013-01-01T00:00:00.000Z_2013-01-02T00:00:00.000Z/1/0/index.zip",
+        MapUtils.getString(targetLoadSpec, "key")
+    );
+    Assert.assertEquals("archive", MapUtils.getString(targetLoadSpec, 
"bucket"));
+    Assert.assertTrue(mockS3Client.didCopy());
+  }
+
+  @Test
+  public void testCopyNoop() throws Exception
+  {
+    MockAmazonS3Client mockS3Client = new MockAmazonS3Client();
+    S3DataSegmentCopier copier = new S3DataSegmentCopier(
+        Suppliers.ofInstance(mockS3Client),
+        new S3DataSegmentPusherConfig()
+    );
+
+    mockS3Client.putObject(
+        "archive",
+        
"targetBaseKey/test/2013-01-01T00:00:00.000Z_2013-01-02T00:00:00.000Z/1/0/index.zip"
+    );
+
+    DataSegment copiedSegment = copier.copy(
+        SOURCE_SEGMENT,
+        ImmutableMap.of("baseKey", "targetBaseKey", "bucket", "archive")
+    );
+
+    Map<String, Object> targetLoadSpec = copiedSegment.getLoadSpec();
+
+    Assert.assertEquals(
+        
"targetBaseKey/test/2013-01-01T00:00:00.000Z_2013-01-02T00:00:00.000Z/1/0/index.zip",
+        MapUtils.getString(targetLoadSpec, "key")
+    );
+    Assert.assertEquals("archive", MapUtils.getString(targetLoadSpec, 
"bucket"));
+    Assert.assertFalse(mockS3Client.didCopy());
+  }
+
+  @Test(expected = SegmentLoadingException.class)
+  public void testCopyException() throws Exception
+  {
+    MockAmazonS3Client mockS3Client = new MockAmazonS3Client();
+    S3DataSegmentCopier copier = new S3DataSegmentCopier(
+        Suppliers.ofInstance(mockS3Client),
+        new S3DataSegmentPusherConfig()
+    );
+
+    copier.copy(
+        SOURCE_SEGMENT,
+        ImmutableMap.of("baseKey", "targetBaseKey", "bucket", "archive")
+    );
+  }
+
+  @Test
+  public void testIgnoresGoneButAlreadyCopied() throws Exception
+  {
+    MockAmazonS3Client mockS3Client = new MockAmazonS3Client();
+    S3DataSegmentCopier copier = new S3DataSegmentCopier(
+        Suppliers.ofInstance(mockS3Client),
+        new S3DataSegmentPusherConfig()
+    );
+    copier.copy(new DataSegment(
+        "test",
+        Intervals.of("2013-01-01/2013-01-02"),
+        "1",
+        ImmutableMap.of(
+            "key",
+            
"baseKey/test/2013-01-01T00:00:00.000Z_2013-01-02T00:00:00.000Z/1/0/index.zip",
+            "bucket",
+            "DOES NOT EXIST"
+        ),
+        ImmutableList.of("dim1", "dim1"),
+        ImmutableList.of("metric1", "metric2"),
+        NoneShardSpec.instance(),
+        0,
+        1
+    ), ImmutableMap.of("bucket", "DOES NOT EXIST", "baseKey", "baseKey"));
+  }
+
+  @Test(expected = SegmentLoadingException.class)
+  public void testFailsToCopyMissing() throws Exception
+  {
+    MockAmazonS3Client mockS3Client = new MockAmazonS3Client();
+    S3DataSegmentCopier copier = new S3DataSegmentCopier(
+        Suppliers.ofInstance(mockS3Client),
+        new S3DataSegmentPusherConfig()
+    );
+    copier.copy(new DataSegment(
+        "test",
+        Intervals.of("2013-01-01/2013-01-02"),
+        "1",
+        ImmutableMap.of(
+            "key",
+            
"baseKey/test/2013-01-01T00:00:00.000Z_2013-01-02T00:00:00.000Z/1/0/index.zip",
+            "bucket",
+            "DOES NOT EXIST"
+        ),
+        ImmutableList.of("dim1", "dim1"),
+        ImmutableList.of("metric1", "metric2"),
+        NoneShardSpec.instance(),
+        0,
+        1
+    ), ImmutableMap.of("bucket", "DOES NOT EXIST", "baseKey", "baseKey2"));
+  }
+
+  private static class MockAmazonS3Client extends ServerSideEncryptingAmazonS3
+  {
+    Map<String, Set<String>> storage = new HashMap<>();
+    boolean copied = false;
+    boolean deletedOld = false;
+
+    private MockAmazonS3Client()
+    {
+      super(new AmazonS3Client(), new NoopServerSideEncryption());

Review Comment:
   ## Deprecated method or constructor invocation
   
   Invoking [AmazonS3Client.AmazonS3Client](1) should be avoided because it has 
been deprecated.
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/8443)



-- 
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]

Reply via email to