Samrat002 commented on code in PR #28012: URL: https://github.com/apache/flink/pull/28012#discussion_r3137942554
########## flink-filesystems/flink-s3-fs-native/src/test/java/org/apache/flink/fs/s3native/NativeS3InputStreamTest.java: ########## @@ -0,0 +1,259 @@ +/* + * 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.flink.fs.s3native; + +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.core.ResponseInputStream; +import software.amazon.awssdk.http.Abortable; +import software.amazon.awssdk.http.AbortableInputStream; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.GetObjectRequest; +import software.amazon.awssdk.services.s3.model.GetObjectResponse; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.time.Duration; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** Tests for {@link NativeS3InputStream}. */ +class NativeS3InputStreamTest { + + private static final String BUCKET = "test-bucket"; + private static final String KEY = "test-key"; + + private static final byte[] DATA; + + static { + DATA = new byte[256]; + for (int i = 0; i < DATA.length; i++) { + DATA[i] = (byte) i; + } + } + + private static class TrackingInputStream extends InputStream implements Abortable { + private final ByteArrayInputStream delegate; + private final AtomicBoolean aborted = new AtomicBoolean(); + + TrackingInputStream(byte[] data, int offset) { + this.delegate = new ByteArrayInputStream(data, offset, data.length - offset); + } + + TrackingInputStream(byte[] data) { + this(data, 0); + } + + @Override + public int read() { + return delegate.read(); + } + + @Override + public int read(byte[] b, int off, int len) { + return delegate.read(b, off, len); + } + + @Override + public void abort() { + aborted.set(true); + } + + @Override + public void close() throws IOException { + delegate.close(); + } + + boolean wasAborted() { + return aborted.get(); + } + } + + /** {@link S3Client} stub. */ + private static final class StubS3Client implements S3Client { + private final byte[] data; + private final AtomicInteger getObjectCalls = new AtomicInteger(); + private volatile TrackingInputStream lastStream; + + StubS3Client(byte[] data) { + this.data = data; + } + + @Override + public ResponseInputStream<GetObjectResponse> getObject(GetObjectRequest request) { + getObjectCalls.incrementAndGet(); + int offset = 0; + String range = request.range(); + if (range != null && range.startsWith("bytes=")) { + offset = Integer.parseInt(range.substring(6, range.indexOf('-'))); + } + TrackingInputStream tracking = new TrackingInputStream(data, offset); + lastStream = tracking; + AbortableInputStream abortable = AbortableInputStream.create(tracking, tracking); + return new ResponseInputStream<>( + GetObjectResponse.builder().build(), abortable, Duration.ZERO); + } + + @Override + public String serviceName() { + return "s3"; + } + + @Override + public void close() {} + + TrackingInputStream lastStream() { + return lastStream; + } + + int getObjectCalls() { + return getObjectCalls.get(); + } + } + + @Test + void closeAbortsUnderlyingStream() throws Exception { + StubS3Client client = new StubS3Client(DATA); + try (NativeS3InputStream in = new NativeS3InputStream(client, BUCKET, KEY, DATA.length)) { + assertThat(in.read()).isEqualTo(0); + assertThat(in.getPos()).isEqualTo(1); + } + assertThat(client.lastStream().wasAborted()).isTrue(); + } + + @Test + void seekAbortsOldStreamAndOpensNew() throws Exception { Review Comment: Valid concern. Every seek() currently aborts and reopens a new S3 stream via a range request, which involves a new TLS handshake. For patterns like list state restore with many small forward seeks (e.g., a few bytes forward due to rescaling), this would be very expensive. Hadoop's S3AInputStream handles this by draining forward within a configurable threshold (fs.s3a.readahead.range, default 64KB) instead of reopening. drain-forward for small seeks, abort+reopen for large seeks is an optimisation on top of the current change. This patch focuses only on preventing the unbounded drain path. I'll file a JIRA for the seek threshold optimisation. -- 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]
