williamhyun commented on code in PR #17457: URL: https://github.com/apache/iceberg/pull/17457#discussion_r3865514807
########## core/src/main/java/org/apache/iceberg/io/http/HttpInputFile.java: ########## @@ -0,0 +1,190 @@ +/* + * 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.IOException; +import java.util.Locale; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.iceberg.exceptions.NotFoundException; +import org.apache.iceberg.exceptions.RuntimeIOException; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.io.SeekableInputStream; +import org.apache.iceberg.metrics.MetricsContext; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; + +/** + * An {@link InputFile} backed by an HTTP URL, typically a pre-signed object-store URL that encodes + * auth in its query parameters. + * + * <p>A known content length is returned directly; otherwise it is fetched lazily via a {@code GET + * Range: bytes=0-0} request, which (unlike HEAD) works with pre-signed GET URLs. + */ +class HTTPInputFile implements InputFile { + private static final long UNKNOWN_LENGTH = -1L; + + private final CloseableHttpClient client; + private final String location; + private final String url; + private final MetricsContext metrics; + + private long length; + + HTTPInputFile(CloseableHttpClient client, String location, String url, MetricsContext metrics) { + this(client, location, url, UNKNOWN_LENGTH, metrics); + } + + HTTPInputFile( + CloseableHttpClient client, + String location, + String url, + long length, + MetricsContext metrics) { + Preconditions.checkNotNull(client, "Invalid HTTP client: null"); + Preconditions.checkNotNull(location, "Invalid location: null"); + Preconditions.checkNotNull(url, "Invalid url: null"); + Preconditions.checkNotNull(metrics, "Invalid metrics context: null"); + this.client = client; + this.location = location; + this.url = url; + this.length = length; + this.metrics = metrics; + } + + @Override + public long getLength() { + if (length == UNKNOWN_LENGTH) { + this.length = fetchContentLength(); + } + + return length; + } + + @Override + public SeekableInputStream newStream() { + return new HTTPInputStream(client, location, url, metrics); + } + + @Override + public String location() { + return location; + } + + @Override + public boolean exists() { + try { + HttpGet request = new HttpGet(url); + request.setHeader(HttpHeaders.RANGE, "bytes=0-0"); Review Comment: Hey @xndai, there are a couple things in our model of catalog-vended presigned URLs that limit our use of `HEAD` requests. 1. S3's remote signed header is method-bound 2. The catalog holds the credentials to sign the object and provide it to the client via URL. Hence the catalog-vended URL is method bound to `GET` in our read use case and to do a `HEAD` request on the same object, we would need the catalog to vend a new, separate URL signed for the `HEAD` request. Effectively doubling sign calls, response payload, and introducing additional spec changes for carrying two URLs. -- 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]
