clintropolis commented on code in PR #19560:
URL: https://github.com/apache/druid/pull/19560#discussion_r3365998765


##########
extensions-core/s3-extensions/src/main/java/org/apache/druid/storage/s3/S3SegmentRangeReader.java:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.google.common.base.Preconditions;
+import org.apache.druid.data.input.impl.RetryingInputStream;
+import org.apache.druid.data.input.impl.prefetch.ObjectOpenFunction;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.segment.loading.SegmentRangeReader;
+import software.amazon.awssdk.services.s3.model.GetObjectRequest;
+import software.amazon.awssdk.services.s3.model.S3Exception;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * {@link SegmentRangeReader} backed by S3 HTTP {@code Range} requests. The 
segment is expected to be stored as raw
+ * (unzipped) files under a common key prefix, i.e. the layout produced by 
{@code S3DataSegmentPusher.pushNoZip} where
+ * each segment file is uploaded as {@code keyPrefix + file.getName()}. Each 
{@link #readRange} call resolves the
+ * target object key as {@code keyPrefix + filename} and issues a closed 
byte-range GET
+ * ({@code bytes=offset-(offset+length-1)}).
+ * <p>
+ * The returned stream is wrapped in a {@link RetryingInputStream} with the 
{@link S3Utils#S3RETRY} predicate, the
+ * same retry policy {@link S3DataSegmentPuller} uses for full-segment 
downloads. Segment loading from deep storage
+ * needs retry semantics built into the reader so callers don't each reinvent 
it. The retrying wrapper reopens at the
+ * byte offset where it failed, so a transient mid-stream error becomes a 
fresh range request for the remaining bytes
+ * rather than restarting the whole read.
+ */
+public class S3SegmentRangeReader implements SegmentRangeReader
+{
+  private final ServerSideEncryptingAmazonS3 s3Client;
+  private final String bucket;
+  private final String keyPrefix;
+
+  public S3SegmentRangeReader(ServerSideEncryptingAmazonS3 s3Client, String 
bucket, String keyPrefix)
+  {
+    this.s3Client = Preconditions.checkNotNull(s3Client, "s3Client");
+    this.bucket = Preconditions.checkNotNull(bucket, "bucket");
+    this.keyPrefix = Preconditions.checkNotNull(keyPrefix, "keyPrefix");
+  }
+
+  @Override
+  public InputStream readRange(String filename, long offset, long length) 
throws IOException
+  {
+    Preconditions.checkNotNull(filename, "filename");
+    Preconditions.checkArgument(offset >= 0, "offset must be non-negative, got 
[%s]", offset);
+    Preconditions.checkArgument(length > 0, "length must be positive, got 
[%s]", length);

Review Comment:
   ah, i guess `SegmentFileBuilderV10` technically allows empty add operations 
(though it would be very unlikely). I've added a short-circuit here to return 
an empty string if this does happen (since the actual API wouldn't really allow 
it)



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