iceberg: rewrite_data_files procedure fails with Premature end of Content-Length when using S3 client

Apache Iceberg version

1.4.3 (latest release)

Query engine

Spark

Please describe the bug šŸž

I wrote this code in Scala:

HiveCatalog catalog = new HiveCatalog();
catalog.setConf(spark.sparkContext().hadoopConfiguration());

Map<String, String> properties = new HashMap<>();
properties.put("warehouse", "s3a://obs-zdp-warehouse-stage-mz/");
properties.put("uri", "thrift://******");

catalog.initialize("hive", properties);

String tableName = args[0];
String[] schemaAndTable = tableName.split("\\.");
TableIdentifier tableId = TableIdentifier.of(schemaAndTable);

Table table = catalog.loadTable(tableId);

SparkActions.get()
        .rewriteDataFiles(table)
        .option("partial-progress.enabled", "true")
        .execute();

And this code in SQL:

spark.sql("CALL iceberg_catalog.system.rewrite_data_files(table => 'stage.test_partitioned', options => map('partial-progress.enabled', 'true'))");

They are supposed to do the same thing, they even have the absolute same parameters.

Yet, the Scala one works fine, but the SQL one constantly throws this exception:

2024-02-07T15:02:29,094 WARN [task-result-getter-1] org.apache.spark.scheduler.TaskSetManager - Lost task 0.0 in stage 9.0 (TID 33) (10.16.37.118 executor 21): org.apache.iceberg.exceptions.RuntimeIOException: org.apache.iceberg.aws.shaded.org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 236,207,208; received: 142,033,440)
	at org.apache.iceberg.parquet.ParquetReader$FileIterator.advance(ParquetReader.java:149)
	at org.apache.iceberg.parquet.ParquetReader$FileIterator.next(ParquetReader.java:126)
	at org.apache.iceberg.io.FilterIterator.advance(FilterIterator.java:65)
	at org.apache.iceberg.io.FilterIterator.hasNext(FilterIterator.java:49)
	at org.apache.iceberg.spark.source.BaseReader.next(BaseReader.java:140)
	at org.apache.spark.sql.execution.datasources.v2.PartitionIterator.hasNext(DataSourceRDD.scala:120)
	at org.apache.spark.sql.execution.datasources.v2.MetricsIterator.hasNext(DataSourceRDD.scala:158)
	at org.apache.spark.sql.execution.datasources.v2.DataSourceRDD$$anon$1.$anonfun$hasNext$1(DataSourceRDD.scala:63)
	at org.apache.spark.sql.execution.datasources.v2.DataSourceRDD$$anon$1.$anonfun$hasNext$1$adapted(DataSourceRDD.scala:63)
	at scala.Option.exists(Option.scala:376)
	at org.apache.spark.sql.execution.datasources.v2.DataSourceRDD$$anon$1.hasNext(DataSourceRDD.scala:63)
	at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
	at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:460)
	at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIteratorForCodegenStage1.processNext(Unknown Source)
	at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
	at org.apache.spark.sql.execution.WholeStageCodegenEvaluatorFactory$WholeStageCodegenPartitionEvaluator$$anon$1.hasNext(WholeStageCodegenEvaluatorFactory.scala:43)
	at org.apache.spark.sql.execution.datasources.v2.WritingSparkTask.$anonfun$run$1(WriteToDataSourceV2Exec.scala:441)
	at org.apache.spark.util.Utils$.tryWithSafeFinallyAndFailureCallbacks(Utils.scala:1397)
	at org.apache.spark.sql.execution.datasources.v2.WritingSparkTask.run(WriteToDataSourceV2Exec.scala:486)
	at org.apache.spark.sql.execution.datasources.v2.WritingSparkTask.run$(WriteToDataSourceV2Exec.scala:425)
	at org.apache.spark.sql.execution.datasources.v2.DataWritingSparkTask$.run(WriteToDataSourceV2Exec.scala:491)
	at org.apache.spark.sql.execution.datasources.v2.V2TableWriteExec.$anonfun$writeWithV2$2(WriteToDataSourceV2Exec.scala:388)
	at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:93)
	at org.apache.spark.TaskContext.runTaskWithListeners(TaskContext.scala:161)
	at org.apache.spark.scheduler.Task.run(Task.scala:141)
	at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$4(Executor.scala:620)
	at org.apache.spark.util.SparkErrorUtils.tryWithSafeFinally(SparkErrorUtils.scala:64)
	at org.apache.spark.util.SparkErrorUtils.tryWithSafeFinally$(SparkErrorUtils.scala:61)
	at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:94)
	at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:623)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.apache.iceberg.aws.shaded.org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 297,221,738; received: 143,086,296)
	at org.apache.iceberg.aws.shaded.org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:178)
	at org.apache.iceberg.aws.shaded.org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135)
	at java.base/java.io.FilterInputStream.read(FilterInputStream.java:133)
	at java.base/java.io.FilterInputStream.read(FilterInputStream.java:133)
	at software.amazon.awssdk.core.io.SdkFilterInputStream.read(SdkFilterInputStream.java:66)
	at org.apache.iceberg.aws.s3.S3InputStream.read(S3InputStream.java:109)
	at org.apache.iceberg.shaded.org.apache.parquet.io.DelegatingSeekableInputStream.readFully(DelegatingSeekableInputStream.java:102)
	at org.apache.iceberg.shaded.org.apache.parquet.io.DelegatingSeekableInputStream.readFullyHeapBuffer(DelegatingSeekableInputStream.java:127)
	at org.apache.iceberg.shaded.org.apache.parquet.io.DelegatingSeekableInputStream.readFully(DelegatingSeekableInputStream.java:91)
	at org.apache.iceberg.shaded.org.apache.parquet.hadoop.ParquetFileReader$ConsecutivePartList.readAll(ParquetFileReader.java:1850)
	at org.apache.iceberg.shaded.org.apache.parquet.hadoop.ParquetFileReader.internalReadRowGroup(ParquetFileReader.java:990)
	at org.apache.iceberg.shaded.org.apache.parquet.hadoop.ParquetFileReader.readNextRowGroup(ParquetFileReader.java:940)
	at org.apache.iceberg.parquet.ParquetReader$FileIterator.advance(ParquetReader.java:147)
	... 32 more

I run this code on a table with 1800 small files around 200kb each.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 37 (11 by maintainers)

Most upvoted comments

For future reference the difference between Procedure and Action here was down the the Catalog being configured differently in both contexts. For Action HadoopFileIO was running and for Procedure S3FileIO was in use. When the action was switched to also use S3FileiO the same error replicated. So the underlying issue here is with S3FileIO.

This issue still persists. I’m seeing it with the latest iceberg (1.8.1). Spark-sql or Scala DSL doesn’t seem to matter. S3FileIO or HadoopFileIO also doesn’t seem to matter. I have the same setup with a ton of small files from a streaming source. So maybe there’s something there. Could this be a throttling issue? We’re using Ceph instead of S3.

I am experiencing the same issues, using the same setup as paulpaul1076. Thanks to this discussion I also tried changing from S3FileIO to the default and so far it seems to be working well.

Thanks for helping in narrowing down @RussellSpitzer šŸ‘ We still need to figureout the solution to this problem. But I am not sure how to reproduce locally with small data / testcase.

@nastra unfortunately this doesn’t seem to be the only reason for the content-length exception. We now discovered that it still fails, even though I stopped using the direct streaming writer and switched to foreachBatch.

In one of our test tables today I got this exception:

org.apache.iceberg.aws.shaded.org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 223,998,698; received: 143,802,368)

I looked for files in the files meta table whose file_size_in_bytes is 223,998,698, and found nothing, where could it even get this number?

And once again, Spark actions work from Java/Scala DSL, but don’t work from Spark SQL.

My guess would be maybe the 223 MB is the uncompressed size, whereas the 143 MB is the compressed size. But this is just a wild guess and I don’t know why that would happen, since you mentioned you don’t have any files being that large.

@nastra so, I deployed rest catalog locally, registered this table in it and now Spark SQL’s rewrite_data_files works. We will have to deploy it in production and do a more extensive test, but I believe you were right, hive catalog is buggy and we should use rest catalog.

Discussed in slack that this is due to iceberg’s streaming writer not writing unique file names, this PR should fix this: https://github.com/apache/iceberg/pull/9255, waiting for iceberg 1.5.0, then I will test and write an update. Alternatively, there’s a similar issue here: https://github.com/apache/iceberg/issues/8953 OP says that his problem went away when he switched to using foreachBatch.

The problem happens when using direct streaming writer, when you restart your streaming job, it may create files with names that have been used before, that’s why files get overwritten and there are multiple entries of the same file in iceberg metadata.

Looks like iceberg-aws-bundle doesn’t have this class:

Exception in thread "main" java.lang.NoClassDefFoundError: software/amazon/awssdk/http/urlconnection/UrlConnectionHttpClient$Builder

Should I just add the awsask to classpath also? I thought that iceberg-aws-bundle was supposed to include all that.

Ah yeah this is a bug unfortunately and we need to add this to the aws-bundle, which I’ve done in https://github.com/apache/iceberg/pull/9685. In the meantime you’d have to either add the AWS SDK or just the url-connection-client dependency

I just need to set a spark option like this, right:

spark.sql.catalog.my_catalog.http-client.type=urlconnection

?