xndai commented on code in PR #17457: URL: https://github.com/apache/iceberg/pull/17457#discussion_r3805930931
########## 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: why don't we just use HEAD call here? ########## core/src/main/java/org/apache/iceberg/io/http/HttpInputStream.java: ########## @@ -0,0 +1,298 @@ +/* + * 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 java.util.concurrent.atomic.AtomicReference; +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.io.entity.EntityUtils; +import org.apache.iceberg.exceptions.ForbiddenException; +import org.apache.iceberg.exceptions.NotFoundException; +import org.apache.iceberg.io.FileIOMetricsContext; +import org.apache.iceberg.io.RangeReadable; +import org.apache.iceberg.io.SeekableInputStream; +import org.apache.iceberg.metrics.Counter; +import org.apache.iceberg.metrics.MetricsContext; +import org.apache.iceberg.metrics.MetricsContext.Unit; +import org.apache.iceberg.relocated.com.google.common.base.Joiner; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.Tasks; +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 retryable HTTP responses (throttling and transient server + * errors; see {@link HttpStatusCategory}) are retried with exponential backoff up to {@value + * #MAX_RETRIES} times, so a throttled or briefly unavailable endpoint is not hammered. A missing + * location ({@code 404}) surfaces as {@link NotFoundException} and a forbidden response ({@code + * 403}, e.g. an expired pre-signed URL) as {@link ForbiddenException}; both are terminal, as is any + * other non-retryable status. Status codes are classified in one place by {@link + * HttpStatusCategory}. + */ +class HttpInputStream extends SeekableInputStream implements RangeReadable { + private static final Logger LOG = LoggerFactory.getLogger(HttpInputStream.class); + + private static final int MAX_RETRIES = 3; + private static final int MIN_RETRY_WAIT_MS = 100; + private static final int MAX_RETRY_WAIT_MS = 5_000; + private static final int MAX_RETRY_DURATION_MS = 30_000; + private static final double RETRY_SCALE_FACTOR = 2.0; Review Comment: can we make these configurable? -- 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]
