You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2019/12/30 16:25:02 UTC

[GitHub] [druid] gianm commented on a change in pull request #9098: S3: Improvements to prefix listing (including fix for an infinite loop)

gianm commented on a change in pull request #9098: S3: Improvements to prefix listing (including fix for an infinite loop)
URL: https://github.com/apache/druid/pull/9098#discussion_r362031593
 
 

 ##########
 File path: extensions-core/s3-extensions/src/main/java/org/apache/druid/storage/s3/LazyObjectSummariesIterator.java
 ##########
 @@ -0,0 +1,162 @@
+/*
+ * 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.model.ListObjectsV2Request;
+import com.amazonaws.services.s3.model.ListObjectsV2Result;
+import com.amazonaws.services.s3.model.S3ObjectSummary;
+import org.apache.druid.java.util.common.RE;
+
+import java.net.URI;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ * Iterator class used by {@link S3Utils#lazyObjectSummaryIterator}.
+ *
+ * Walks a set of prefixes, returning all objects underneath them except for directory placeholders.
+ */
+public class LazyObjectSummariesIterator implements Iterator<S3ObjectSummary>
+{
+  private final ServerSideEncryptingAmazonS3 s3Client;
+  private final Iterator<URI> prefixesIterator;
+  private final int maxListingLength;
+
+  private ListObjectsV2Request request;
+  private ListObjectsV2Result result;
+  private Iterator<S3ObjectSummary> objectSummaryIterator;
+  private S3ObjectSummary currentObjectSummary;
+
+  LazyObjectSummariesIterator(
+      final ServerSideEncryptingAmazonS3 s3Client,
+      final Iterable<URI> prefixes,
+      final int maxListingLength
+  )
+  {
+    this.s3Client = s3Client;
+    this.prefixesIterator = prefixes.iterator();
+    this.maxListingLength = maxListingLength;
+
+    prepareNextRequest();
+    fetchNextBatch();
+    advanceObjectSummary();
+  }
+
+  private void prepareNextRequest()
+  {
+    final URI currentUri = prefixesIterator.next();
+    final String currentBucket = currentUri.getAuthority();
+    final String currentPrefix = S3Utils.extractS3Key(currentUri);
+
+    request = new ListObjectsV2Request()
+        .withBucketName(currentBucket)
+        .withPrefix(currentPrefix)
+        .withMaxKeys(maxListingLength);
+  }
+
+  private void fetchNextBatch()
+  {
+    try {
+      result = S3Utils.retryS3Operation(() -> s3Client.listObjectsV2(request));
+      request.setContinuationToken(result.getNextContinuationToken());
+      objectSummaryIterator = result.getObjectSummaries().iterator();
+    }
+    catch (Exception e) {
+      throw new RE(
+          e,
+          "Failed to get object summaries from S3 bucket[%s], prefix[%s]",
+          request.getBucketName(),
+          request.getPrefix()
+      );
+    }
+  }
+
+  /**
+   * Advance objectSummaryIterator to the next non-placeholder, updating "currentObjectSummary".
+   */
+  private void advanceObjectSummary()
+  {
+    while (objectSummaryIterator.hasNext() || result.isTruncated() || prefixesIterator.hasNext()) {
+      while (objectSummaryIterator.hasNext()) {
+        currentObjectSummary = objectSummaryIterator.next();
+
+        if (!isDirectoryPlaceholder(currentObjectSummary)) {
+          return;
+        }
+      }
+
+      // Exhausted "objectSummaryIterator" without finding a non-placeholder.
+      if (result.isTruncated()) {
+        fetchNextBatch();
+      } else if (prefixesIterator.hasNext()) {
+        prepareNextRequest();
+        fetchNextBatch();
+      }
+    }
+
+    // Truly nothing left to read.
+    currentObjectSummary = null;
+  }
+
+  @Override
+  public boolean hasNext()
+  {
+    return currentObjectSummary != null;
+  }
+
+  @Override
+  public S3ObjectSummary next()
+  {
+    if (currentObjectSummary == null) {
+      throw new NoSuchElementException();
+    }
+
+    final S3ObjectSummary retVal = currentObjectSummary;
+    advanceObjectSummary();
+    return retVal;
+  }
 
 Review comment:
   I'll move it, because I agree with this particular point, but IMO style nits should either be encoded into an automated style checker or written down somewhere centrally agreed-upon. Otherwise they can create friction in the review process. Maybe we can come up with a nice way as a community to handle this.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org