You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "kinolaev (via GitHub)" <gi...@apache.org> on 2026/03/27 18:12:22 UTC

[PR] AWS: handle premature connection close [iceberg]

kinolaev opened a new pull request, #15792:
URL: https://github.com/apache/iceberg/pull/15792

   During vectorized Parquet reads, `S3InputStream` opens an unbounded HTTP range request (`bytes=pos-`) and reads one row group eagerly into memory. While Spark processes that in-memory row group (which can take several minutes for large batches), the client stops reading from S3. The TCP receive buffer fills up, and S3 eventually tears down the stalled connection.
   
   When the next row group read begins, the connection is already dead and Apache HTTP client throws `ConnectionClosedException: Premature end of Content-Length delimited message body (expected: x; received: y)` (when using [apache http client](https://github.com/apache/httpcomponents-core/blob/rel/v5.4.2/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ContentLengthInputStream.java#L176-L178)). This only affects files with multiple row groups (typically >128 MB).
   
   The existing retry policy handles `SSLException`, `SocketTimeoutException`, and `SocketException`, but not this case. This PR extends the retry predicate to reopen the stream at the saved position when this specific exception is encountered, while leaving all other `ConnectionClosedException` variants (e.g. from `abort()`) unaffected.
   
   Fixes https://github.com/apache/iceberg/issues/9674 and https://github.com/apache/iceberg/issues/9679.
   


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on code in PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#discussion_r3083247455


##########
aws/src/test/java/org/apache/iceberg/aws/s3/TestS3InputStream.java:
##########
@@ -62,4 +66,36 @@ void testReadTailClosesTheStream() throws IOException {
 
     verify(inputStream).close();
   }
+
+  @Test
+  void testReadRetriesOnPrematureConnectionClose() throws IOException {

Review Comment:
   I added a MockServer and real S3 calls to the test in c3c43875c8a0ad94ef5ded0970bd76a346d8b952.
   
   I've also found that UrlConnectionHttpClient returns -1 from `read` when the connection is closed prematurely instead of throwing an exception. That is why I added `handleResultIf` to the retry policy that checks if `read` returns -1 before the expected end of the stream. The PR now covers both clients' behavior.
   
   Additionally, I've tested both clients with minio in `aws/src/integration/java/org/apache/iceberg/aws/s3/TestS3InputStream.java`:
   ```java
   public class TestS3InputStream {
     // ...
     @Test
     public void testReadRetriesOnPrematureConnectionCloseWithApacheHttpClient() throws Exception {
       testReadRetriesOnPrematureConnectionClose(s3, "apache");
     }
   
     public void testReadRetriesOnPrematureConnectionCloseWithUrlConnectionHttpClient() throws Exception {
       testReadRetriesOnPrematureConnectionClose(s3urlconnection, "urlconnection");
     }
   
     private void testReadRetriesOnPrematureConnectionClose(S3Client s3, String filename) throws Exception {
       S3URI uri = new S3URI("s3://bucket/path/to/read-retries-on-premature-connection-close-%s.dat".formatted(filename));
       byte[] data = randomData(16 * 1024 * 1024);
       writeS3Data(uri, data);
       S3InputStream stream = newInputStream(s3, uri);
   
       int blockSize = data.length / 2;
       byte[] buf1 = new byte[blockSize];
       assertThat(stream.readNBytes(buf1, 0, blockSize)).isEqualTo(blockSize);
       assertThat(buf1).isEqualTo(Arrays.copyOfRange(data, 0, blockSize));
       Thread.sleep(32000);
       byte[] buf2 = new byte[blockSize];
       assertThat(stream.readNBytes(buf2, 0, blockSize)).isEqualTo(blockSize);
       assertThat(buf2).isEqualTo(Arrays.copyOfRange(data, blockSize, data.length));
       // RetryPolicy.builder().onRetry(e -> { attemptCount = e.getAttemptCount(); })
       assertThat(stream.attemptCount).isEqualTo(1);
     }
   }
   ```
   I didn't commit these tests because on my laptop they only succeed when the delay between the read calls is greater than 30s. Otherwise there are no retries (`attemptCount=0`). I'm not sure this specific delay is consistent enough across different environments to reliably reproduce the behavior and I didn't want to increase the total execution time.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "singhpk234 (via GitHub)" <gi...@apache.org>.
singhpk234 commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-4237815098

   Thats really good to know @kinolaev !


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "danielcweeks (via GitHub)" <gi...@apache.org>.
danielcweeks commented on code in PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#discussion_r3119972600


##########
aws/src/main/java/org/apache/iceberg/aws/s3/S3InputStream.java:
##########
@@ -55,12 +57,32 @@ class S3InputStream extends SeekableInputStream implements RangeReadable {
   private static final List<Class<? extends Throwable>> RETRYABLE_EXCEPTIONS =
       ImmutableList.of(SSLException.class, SocketTimeoutException.class, SocketException.class);
 
+  private boolean isPrematureConnectionCloseApacheHttpClient(Throwable ex) {

Review Comment:
   This is a strange enough workaround that I think we should include some docs on exactly what we're doing here.  I can't imagine someone figuring this out looking at the code in the future.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-4680917505

   The PR is not stale. It is not critical for Parquet in version 1.11.0 because, after PR #13997, the Parquet reader calls S3InputStream.readFully instead of S3InputStream.openStream. However, it is still useful for 1.10.x, where the rewrite_data_files procedure often gets stuck on files with several row groups.
   
   It might also be useful for formats other than Parquet if they call S3InputStream.openStream and pause reading for longer than a server timeout.
   
   All comments have been addressed. The PR is now waiting for a committer.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on code in PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#discussion_r3083247455


##########
aws/src/test/java/org/apache/iceberg/aws/s3/TestS3InputStream.java:
##########
@@ -62,4 +66,36 @@ void testReadTailClosesTheStream() throws IOException {
 
     verify(inputStream).close();
   }
+
+  @Test
+  void testReadRetriesOnPrematureConnectionClose() throws IOException {

Review Comment:
   I added a MockServer and real S3 calls to the test in c3c43875c8a0ad94ef5ded0970bd76a346d8b952.
   
   I've also found that UrlConnectionHttpClient returns -1 from `read` when the connection is closed prematurely instead of throwing an exception. That is why I added `handleResultIf` to the retry policy that checks if `read` returns -1 before the expected end of the stream. The PR now covers both clients' behavior.
   
   Additionally, I've tested both clients with minio in `aws/src/integration/java/org/apache/iceberg/aws/s3/TestS3InputStream.java`:
   ```java
   public class TestS3InputStream {
     // ...
     @Test
     public void testReadRetriesOnPrematureConnectionCloseWithApacheHttpClient() throws Exception {
       testReadRetriesOnPrematureConnectionClose(s3, "apache");
     }
   
     public void testReadRetriesOnPrematureConnectionCloseWithUrlConnectionHttpClient() throws Exception {
       testReadRetriesOnPrematureConnectionClose(s3urlconnection, "urlconnection");
     }
   
     private void testReadRetriesOnPrematureConnectionClose(S3Client s3, String filename) throws Exception {
       S3URI uri = new S3URI("s3://bucket/path/to/read-retries-on-premature-connection-close-%s.dat".formatted(filename));
       byte[] data = randomData(16 * 1024 * 1024);
       writeS3Data(uri, data);
       S3InputStream stream = newInputStream(s3, uri);
   
       int blockSize = data.length / 2;
       byte[] buf1 = new byte[blockSize];
       assertThat(stream.readNBytes(buf1, 0, blockSize)).isEqualTo(blockSize);
       assertThat(buf1).isEqualTo(Arrays.copyOfRange(data, 0, blockSize));
       Thread.sleep(32000);
       byte[] buf2 = new byte[blockSize];
       assertThat(stream.readNBytes(buf2, 0, blockSize)).isEqualTo(blockSize);
       assertThat(buf2).isEqualTo(Arrays.copyOfRange(data, blockSize, data.length));
       // RetryPolicy.builder().onRetry(e -> { attemptCount = e.getAttemptCount(); })
       assertThat(stream.attemptCount).isEqualTo(1);
     }
   }
   ```
   I didn't commit these tests because on my laptop they only succeed when the delay between the read calls is greater than 30s. Otherwise there are no retries (`attemptCount=0`). I'm not sure this specific delay is consistent enough across different environments to reliably reproduce the behavior and I didn't want to increase the total execution time.
   
   So, if there are any cases where multiple read calls are separated by a delay, the PR fixes them.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "singhpk234 (via GitHub)" <gi...@apache.org>.
singhpk234 commented on code in PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#discussion_r3006931047


##########
aws/src/main/java/org/apache/iceberg/aws/s3/S3InputStream.java:
##########
@@ -55,6 +55,12 @@ class S3InputStream extends SeekableInputStream implements RangeReadable {
   private static final List<Class<? extends Throwable>> RETRYABLE_EXCEPTIONS =
       ImmutableList.of(SSLException.class, SocketTimeoutException.class, SocketException.class);
 
+  private static boolean isPrematureConnectionClose(Throwable ex) {
+    return ex.getClass().getSimpleName().equals("ConnectionClosedException")
+        && ex.getMessage() != null
+        && ex.getMessage().startsWith("Premature end of Content-Length delimited message body");

Review Comment:
   is there a better way to identify this rather than inspecting on exception message (it looks to fragile to me)



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on code in PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#discussion_r3083247455


##########
aws/src/test/java/org/apache/iceberg/aws/s3/TestS3InputStream.java:
##########
@@ -62,4 +66,36 @@ void testReadTailClosesTheStream() throws IOException {
 
     verify(inputStream).close();
   }
+
+  @Test
+  void testReadRetriesOnPrematureConnectionClose() throws IOException {

Review Comment:
   I added a MockServer and real S3 calls to the test in c3c43875c8a0ad94ef5ded0970bd76a346d8b952.
   
   I've also found that UrlConnectionHttpClient returns -1 from `read` when the connection is closed prematurely instead of throwing an exception. That is why I added `handleResultIf` to the retry policy that checks if `read` returns -1 before the expected end of the stream. The PR now covers both clients' behavior.
   
   Additionally, I've tested both clients with minio in `aws/src/integration/java/org/apache/iceberg/aws/s3/TestS3InputStream.java`:
   ```java
   public class TestS3InputStream {
     // ...
     @Test
     public void testReadRetriesOnPrematureConnectionCloseWithApacheHttpClient() throws Exception {
       testReadRetriesOnPrematureConnectionClose(s3, "apache");
     }
   
     public void testReadRetriesOnPrematureConnectionCloseWithUrlConnectionHttpClient() throws Exception {
       testReadRetriesOnPrematureConnectionClose(s3urlconnection, "urlconnection");
     }
   
     private void testReadRetriesOnPrematureConnectionClose(S3Client s3, String filename) throws Exception {
       S3URI uri = new S3URI("s3://bucket/path/to/read-retries-on-premature-connection-close-%s.dat".formatted(filename));
       byte[] data = randomData(16 * 1024 * 1024);
       writeS3Data(uri, data);
       S3InputStream stream = newInputStream(s3, uri);
   
       int blockSize = data.length / 2;
       byte[] buf1 = new byte[blockSize];
       assertThat(stream.readNBytes(buf1, 0, blockSize)).isEqualTo(blockSize);
       assertThat(buf1).isEqualTo(Arrays.copyOfRange(data, 0, blockSize));
       Thread.sleep(32000);
       byte[] buf2 = new byte[blockSize];
       assertThat(stream.readNBytes(buf2, 0, blockSize)).isEqualTo(blockSize);
       assertThat(buf2).isEqualTo(Arrays.copyOfRange(data, blockSize, data.length));
       // RetryPolicy.builder().onRetry(e -> { attemptCount = e.getAttemptCount(); })
       assertThat(stream.attemptCount).isEqualTo(1);
     }
   }
   ```
   I didn't commit these tests because on my laptop they only succeed when the delay between the read calls is greater than 30s. I'm not sure this specific delay is consistent enough across different environments to reliably reproduce the behavior and I didn't want to increase the total execution time.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "livelace (via GitHub)" <gi...@apache.org>.
livelace commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-4311379816

   Any chance this PR appears in 1.10.2 ?


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-5584967248

   The branch has been rebased to resolve the conflict.
   
   @devilatwork, what versions of Iceberg and Spark are you using, and what data file format? If you're on Iceberg 1.10 and Parquet, have you tried Iceberg 1.11?
   Are you hitting this issue during compaction or in another scenario?


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-4156048718

   RangeReadable is already implemented for S3InputStream. The problem might be already solved in the main branch by the PR https://github.com/apache/iceberg/pull/13997.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-4953431110

   This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "tomerr90 (via GitHub) via issues" <is...@iceberg.apache.org>.
tomerr90 commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-7128496153

   We ran into the same issue while the Committer was trying to commit in Flink


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-4155661890

   > I think this stems from the fact that row groups are read sequentially, i had a pr a while back to prefetch the row groups and this would have potentially saved us in this scenario.
   > PR : https://github.com/apache/iceberg/pull/7279/changes
   
   If I understand it right, prefetching a row group wouldn't help. This problem is caused by an unbounded HTTP range request (`bytes=pos-`) and long row group processing time (time between `advance()` calls). I think, the reader should either continue reading a file while processing the first row group or make bounded range requests (with `S3InputStream.readFully`) for each row group. But unfortunately I didn't find a way to implement it.
   
   > do you know the case like your, reads ever success ? because if a fresh connection is established and we reprocess this task again from the begining we will still get into same situation ?
   
   I encountered this issue only while executing the rewrite_data_files procedure. This PR resolves it, the procedure no longer fails. We don't reprocess the task again from the beginning, we just open a new connection instead of already closed one to read the next row group.
   
   PS: @danielcweeks , this time I've double checked that the problem actually happens in my production environment before opening the PR)


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-4956021878

   The PR is not stale.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "devilatwork (via GitHub)" <gi...@apache.org>.
devilatwork commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-5536963062

   I am hitting this issue. Is there an ETA on when can this fix be made available?


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on code in PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#discussion_r3009987068


##########
aws/src/main/java/org/apache/iceberg/aws/s3/S3InputStream.java:
##########
@@ -55,6 +55,12 @@ class S3InputStream extends SeekableInputStream implements RangeReadable {
   private static final List<Class<? extends Throwable>> RETRYABLE_EXCEPTIONS =
       ImmutableList.of(SSLException.class, SocketTimeoutException.class, SocketException.class);
 
+  private static boolean isPrematureConnectionClose(Throwable ex) {
+    return ex.getClass().getSimpleName().equals("ConnectionClosedException")
+        && ex.getMessage() != null
+        && ex.getMessage().startsWith("Premature end of Content-Length delimited message body");

Review Comment:
   Yes, it definitely looks fragile and it most probably doesn't work for urlconnection client, but I couldn't find a better way. ConnectionClosedException is already identified by its simple name [here](https://github.com/kinolaev/iceberg/blob/e4d88c9ebe5b461b12dc60c1d6704c79d39853e9/aws/src/main/java/org/apache/iceberg/aws/s3/S3InputStream.java#L276-L280). There are many different cases of [ConnectionClosedException](https://github.com/search?q=repo%3Aapache%2Fhttpcomponents-core+new+ConnectionClosedException&type=code) in apache http client that are distinguished only by their error messages. And I doubt that we should add all of them to the retry policy.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "danielcweeks (via GitHub)" <gi...@apache.org>.
danielcweeks commented on code in PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#discussion_r3075445721


##########
aws/src/test/java/org/apache/iceberg/aws/s3/TestS3InputStream.java:
##########
@@ -62,4 +66,36 @@ void testReadTailClosesTheStream() throws IOException {
 
     verify(inputStream).close();
   }
+
+  @Test
+  void testReadRetriesOnPrematureConnectionClose() throws IOException {

Review Comment:
   This doesn't feel like a great way to test this.  We're just throwing what we expect to see.  Can we update this to use the apache client so that it actually throws?  This would also insulate us from a client change to the messaging if the upstream library were to change.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-4237744418

   After backporting PR #13997 to version 1.10.1, I can see bounded range requests in my local debug logs. Additionally, I haven't seen any `Premature end...` or `Unclosed input stream` errors in production for 9 days. It looks like the problem doesn't exist in the main branch anymore.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-4424709136

   Hello @singhpk234 and @danielcweeks , I'm still ready to continue working on this PR if you plan to merge it.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on code in PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#discussion_r3137736905


##########
aws/src/main/java/org/apache/iceberg/aws/s3/S3InputStream.java:
##########
@@ -55,12 +57,32 @@ class S3InputStream extends SeekableInputStream implements RangeReadable {
   private static final List<Class<? extends Throwable>> RETRYABLE_EXCEPTIONS =
       ImmutableList.of(SSLException.class, SocketTimeoutException.class, SocketException.class);
 
+  private boolean isPrematureConnectionCloseApacheHttpClient(Throwable ex) {

Review Comment:
   I added comments to both predicates in commit a34ad534bc303967bb88b91c68d74a52a93078b1.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on code in PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#discussion_r3083247455


##########
aws/src/test/java/org/apache/iceberg/aws/s3/TestS3InputStream.java:
##########
@@ -62,4 +66,36 @@ void testReadTailClosesTheStream() throws IOException {
 
     verify(inputStream).close();
   }
+
+  @Test
+  void testReadRetriesOnPrematureConnectionClose() throws IOException {

Review Comment:
   I added a MockServer and real S3 calls to the test in c3c43875c8a0ad94ef5ded0970bd76a346d8b952.
   
   I've also found that UrlConnectionHttpClient returns -1 from `read` when the connection is closed prematurely instead of throwing an exception. That is why I added `handleResultIf` to the retry policy that checks if `read` returns -1 before the expected end of the stream. The PR now covers both clients' behavior.
   
   Additionally, I've tested both clients with minio in `aws/src/integration/java/org/apache/iceberg/aws/s3/TestS3InputStream.java`:
   ```java
   public class TestS3InputStream {
     // ...
     @Test
     public void testReadRetriesOnPrematureConnectionCloseWithApacheHttpClient() throws Exception {
       testReadRetriesOnPrematureConnectionClose(s3, "apache");
     }
   
     @Test
     public void testReadRetriesOnPrematureConnectionCloseWithUrlConnectionHttpClient() throws Exception {
       testReadRetriesOnPrematureConnectionClose(s3urlconnection, "urlconnection");
     }
   
     private void testReadRetriesOnPrematureConnectionClose(S3Client s3, String filename) throws Exception {
       S3URI uri = new S3URI("s3://bucket/path/to/read-retries-on-premature-connection-close-%s.dat".formatted(filename));
       byte[] data = randomData(16 * 1024 * 1024);
       writeS3Data(uri, data);
       S3InputStream stream = newInputStream(s3, uri);
   
       int blockSize = data.length / 2;
       byte[] buf1 = new byte[blockSize];
       assertThat(stream.readNBytes(buf1, 0, blockSize)).isEqualTo(blockSize);
       assertThat(buf1).isEqualTo(Arrays.copyOfRange(data, 0, blockSize));
       Thread.sleep(32000);
       byte[] buf2 = new byte[blockSize];
       assertThat(stream.readNBytes(buf2, 0, blockSize)).isEqualTo(blockSize);
       assertThat(buf2).isEqualTo(Arrays.copyOfRange(data, blockSize, data.length));
       // RetryPolicy.builder().onRetry(e -> { attemptCount = e.getAttemptCount(); })
       assertThat(stream.attemptCount).isEqualTo(1);
     }
   }
   ```
   I didn't commit these tests because on my laptop they only succeed when the delay between the read calls is greater than 30s. Otherwise there are no retries (`attemptCount=0`). I'm not sure this specific delay is consistent enough across different environments to reliably reproduce the behavior and I didn't want to increase the total execution time.
   
   So, if there are any cases where multiple read calls are separated by a delay, the PR fixes them.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-4155840302

   > I think, the reader should either continue reading a file while processing the first row group or make bounded range requests (with S3InputStream.readFully) for each row group.
   
   There is a [vectored reading io in ParquetFileReader](https://github.com/apache/parquet-java/blob/apache-parquet-1.16.0/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileReader.java#L1272-L1281) that might help to make several bounded range requests instead of one unbounded. I guess we only need to implementing RangeReadable for S3InputStream. I'll try it later this week.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-4676153812

   This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] AWS: handle premature connection close [iceberg]

Posted by "kinolaev (via GitHub)" <gi...@apache.org>.
kinolaev commented on PR #15792:
URL: https://github.com/apache/iceberg/pull/15792#issuecomment-5303123591

   `httpcore5` uses a locale-invariant format for the error message:
   ```
   Premature end of Content-Length delimited message body (expected: %d; received: %d)
   ```
   instead of the format used in `httpcore` (v4):
   ```
   Premature end of Content-Length delimited message body (expected: %,d; received: %,d)
   ```
   
   AWS SDK support for `httpcore5` has been [GA since 2.41.0](https://github.com/aws/aws-sdk-java-v2/blob/master/changelogs/2.41.x-CHANGELOG.md#2410-2025-12-30) and [the preferred default since 2.41.11](https://github.com/aws/aws-sdk-java-v2/blob/master/changelogs/2.41.x-CHANGELOG.md#24111-2026-01-20).
   
   I added support for the `httpcore5` format in 3f061208d5df467c9c9fccd4add651c1b3dc77fd.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org