singhpk234 commented on code in PR #17457:
URL: https://github.com/apache/iceberg/pull/17457#discussion_r3696073044


##########
core/src/main/java/org/apache/iceberg/io/http/HTTPInputStream.java:
##########
@@ -0,0 +1,248 @@
+/*
+ * 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.iceberg.io.http;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+import java.util.Arrays;
+import java.util.Locale;
+import javax.net.ssl.SSLException;
+import org.apache.hc.client5.http.classic.methods.HttpGet;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.iceberg.exceptions.NotFoundException;
+import org.apache.iceberg.io.RangeReadable;
+import org.apache.iceberg.io.SeekableInputStream;
+import 
org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Joiner;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A {@link SeekableInputStream} that reads an HTTP URL via range GETs, for 
pre-signed object-store
+ * URLs that need no object-store credentials on the reader.
+ *
+ * <p>Sequential reads are served from a fixed-size in-memory chunk buffer, 
each chunk fetched with
+ * a single range GET fully consumed within the response handler so 
connections return to the pool.
+ * Positional reads ({@link #readFully}, {@link #readTail}) each issue their 
own range GET.
+ *
+ * <p>Transient socket/TLS errors and 5xx responses are retried up to {@value 
#MAX_RETRIES} times;
+ * any other unexpected status (e.g. an expired pre-signed URL) fails 
immediately.
+ */
+class HTTPInputStream extends SeekableInputStream implements RangeReadable {
+  private static final Logger LOG = 
LoggerFactory.getLogger(HTTPInputStream.class);
+
+  @VisibleForTesting static final int CHUNK_SIZE = 4 * 1024 * 1024; // 4 MB
+  private static final int MAX_RETRIES = 3;
+
+  private final StackTraceElement[] createStack;
+  private final CloseableHttpClient client;
+  private final String location;
+  private final String url;
+
+  /** Cached chunk buffer. {@code bufferFileStart} is the file offset of 
{@code buffer[0]}. */
+  private byte[] buffer;
+
+  private long bufferFileStart = -1L;
+  private int bufferLimit = 0;
+
+  private long next = 0;
+  private boolean closed = false;

Review Comment:
   lets integrate the metrics too, we need a counter to track bytes read ... 
like done in S3InputStream ? 
   
   check this : https://github.com/apache/iceberg/pull/17236/changes



##########
core/src/main/java/org/apache/iceberg/io/http/HttpUrlSupport.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.iceberg.io.http;
+
+import java.io.Serializable;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.core5.io.CloseMode;
+import org.apache.iceberg.io.InputFile;
+
+/**
+ * A helper that a {@link org.apache.iceberg.io.FileIO} can delegate to for 
reading a file directly
+ * over HTTP(S) instead of its normal, credentialed read path.
+ *
+ * <p>Intended for catalogs that vend a pre-signed object-store URL directly 
as a file's location
+ * (e.g. a scan task's {@code file-path}). The location is used unchanged as 
the fetch URL, so {@link
+ * InputFile#location()} equals the location passed to {@link 
#newInputFile(String)}.
+ */
+public class HttpUrlSupport implements Serializable {
+
+  private transient volatile CloseableHttpClient httpClient;
+
+  /** Returns {@code true} if {@code location} is an HTTP(S) URL. */
+  public static boolean isHttpUrl(String location) {
+    return location != null
+        && (location.regionMatches(true, 0, "https://";, 0, 8)
+            || location.regionMatches(true, 0, "http://";, 0, 7));

Review Comment:
   i wonder if there is existing utils to extract scheme ? if yes can we use 
that ?



##########
core/src/main/java/org/apache/iceberg/io/http/HttpUrlSupport.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.iceberg.io.http;
+
+import java.io.Serializable;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.core5.io.CloseMode;
+import org.apache.iceberg.io.InputFile;
+
+/**
+ * A helper that a {@link org.apache.iceberg.io.FileIO} can delegate to for 
reading a file directly
+ * over HTTP(S) instead of its normal, credentialed read path.
+ *
+ * <p>Intended for catalogs that vend a pre-signed object-store URL directly 
as a file's location
+ * (e.g. a scan task's {@code file-path}). The location is used unchanged as 
the fetch URL, so {@link
+ * InputFile#location()} equals the location passed to {@link 
#newInputFile(String)}.
+ */
+public class HttpUrlSupport implements Serializable {
+
+  private transient volatile CloseableHttpClient httpClient;
+
+  /** Returns {@code true} if {@code location} is an HTTP(S) URL. */
+  public static boolean isHttpUrl(String location) {
+    return location != null
+        && (location.regionMatches(true, 0, "https://";, 0, 8)
+            || location.regionMatches(true, 0, "http://";, 0, 7));
+  }
+
+  /**
+   * Returns an {@link InputFile} that reads {@code location} directly over 
HTTP(S).
+   *
+   * @param location an HTTP(S) URL; see {@link #isHttpUrl(String)}
+   */
+  public InputFile newInputFile(String location) {
+    return new HTTPInputFile(httpClient(), location, location);
+  }
+
+  /**
+   * Returns an {@link InputFile} of the given {@code length} that reads 
{@code location} directly
+   * over HTTP(S).
+   *
+   * @param location an HTTP(S) URL; see {@link #isHttpUrl(String)}
+   */
+  public InputFile newInputFile(String location, long length) {
+    return new HTTPInputFile(httpClient(), location, location, length);
+  }
+
+  public void close() {
+    synchronized (this) {
+      if (httpClient != null) {
+        httpClient.close(CloseMode.GRACEFUL);
+        httpClient = null;
+      }
+    }
+  }
+
+  private CloseableHttpClient httpClient() {
+    if (httpClient == null) {
+      synchronized (this) {
+        if (httpClient == null) {
+          this.httpClient = HttpClients.custom().useSystemProperties().build();
+        }
+      }
+    }

Review Comment:
   follow-up : we would likely want to have these configurable rather than 
relying on SDK default or system props ?
   



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