danielcweeks commented on code in PR #7318: URL: https://github.com/apache/iceberg/pull/7318#discussion_r1162974747
########## aws/src/main/java/org/apache/iceberg/aws/s3/S3AsyncInputStream.java: ########## @@ -0,0 +1,223 @@ +/* + * 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.aws.s3; + +import org.apache.iceberg.aws.AwsProperties; +import org.apache.iceberg.exceptions.NotFoundException; +import org.apache.iceberg.io.FileIOMetricsContext; +import org.apache.iceberg.io.IOUtil; +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.relocated.com.google.common.io.ByteStreams; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.core.async.AsyncResponseTransformer; +import software.amazon.awssdk.services.s3.S3AsyncClient; +import software.amazon.awssdk.services.s3.model.GetObjectRequest; +import software.amazon.awssdk.services.s3.model.NoSuchKeyException; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; + +class S3AsyncInputStream extends SeekableInputStream implements RangeReadable { + private static final Logger LOG = LoggerFactory.getLogger(S3AsyncInputStream.class); + + private final StackTraceElement[] createStack; + private final S3AsyncClient s3; + private final S3URI location; + private final AwsProperties awsProperties; + + private InputStream stream; + private long pos = 0; + private long next = 0; + private boolean closed = false; + + private final Counter readBytes; + private final Counter readOperations; + + private int skipSize = 1024 * 1024; + + S3AsyncInputStream(S3AsyncClient s3, S3URI location) { + this(s3, location, new AwsProperties(), MetricsContext.nullMetrics()); + } + + S3AsyncInputStream(S3AsyncClient s3, S3URI location, AwsProperties awsProperties, MetricsContext metrics) { + this.s3 = s3; + this.location = location; + this.awsProperties = awsProperties; + + this.readBytes = metrics.counter(FileIOMetricsContext.READ_BYTES, Unit.BYTES); + this.readOperations = metrics.counter(FileIOMetricsContext.READ_OPERATIONS); + + this.createStack = Thread.currentThread().getStackTrace(); + } + + @Override + public long getPos() { + return next; + } + + @Override + public void seek(long newPos) { + checkNotClosed(); + Preconditions.checkArgument(newPos >= 0, "position is negative: %s", newPos); + + // this allows a seek beyond the end of the stream but the next read will fail + next = newPos; + } + + @Override + public int read() throws IOException { + checkNotClosed(); + positionStream(); + + pos += 1; + next += 1; + readBytes.increment(); + readOperations.increment(); + + return stream.read(); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + checkNotClosed(); + positionStream(); + + int bytesRead = stream.read(b, off, len); + pos += bytesRead; + next += bytesRead; + readBytes.increment(bytesRead); + readOperations.increment(); + + return bytesRead; + } + + + @Override + public void readFully(long position, byte[] buffer, int offset, int length) throws IOException { + Preconditions.checkPositionIndexes(offset, offset + length, buffer.length); + + String range = String.format("bytes=%s-%s", position, position + length - 1); Review Comment: I'm quite concerned about this behavior since it appears that it would dramatically increase the number of requests. We'll probably need to look closely at how we perform reads in the async case. -- 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]
