Skip to content

Commit f99a068

Browse files
authored
[spark] Close the commit and release the cached RDD in remove_unexisting_files (#9706)
1 parent ff8670e commit f99a068

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/procedure/SparkRemoveUnexistingFiles.scala

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import org.apache.paimon.table.FileStoreTable
2727
import org.apache.paimon.table.sink.{CommitMessage, CommitMessageImpl, CommitMessageSerializer}
2828

2929
import org.apache.spark.internal.Logging
30-
import org.apache.spark.rdd.RDD
3130
import org.apache.spark.sql.{PaimonSparkSession, SparkSession}
3231
import org.apache.spark.sql.catalyst.SQLConfHelper
3332

@@ -44,10 +43,10 @@ case class SparkRemoveUnexistingFiles(
4443
extends SQLConfHelper
4544
with Logging {
4645

47-
private def buildRDD(): RDD[String] = {
46+
private def execute(): Array[String] = {
4847
val binaryPartitions = table.newScan().listPartitions()
4948
if (binaryPartitions.isEmpty) {
50-
return spark.sparkContext.emptyRDD[String]
49+
return Array.empty[String]
5150
}
5251

5352
val realParallelism = Math.min(binaryPartitions.size(), parallelism)
@@ -86,25 +85,30 @@ case class SparkRemoveUnexistingFiles(
8685
.repartition(1)
8786
.cache()
8887

89-
if (!dryRun) {
90-
pathAndMessage.foreachPartition {
91-
iter =>
92-
{
93-
val serializer = new CommitMessageSerializer()
94-
val messages = new util.ArrayList[CommitMessage]()
95-
iter.foreach {
96-
case (_, bytes) => messages.add(serializer.deserialize(serializer.getVersion, bytes))
88+
try {
89+
if (!dryRun) {
90+
pathAndMessage.foreachPartition {
91+
iter =>
92+
{
93+
val serializer = new CommitMessageSerializer()
94+
val messages = new util.ArrayList[CommitMessage]()
95+
iter.foreach {
96+
case (_, bytes) =>
97+
messages.add(serializer.deserialize(serializer.getVersion, bytes))
98+
}
99+
val commit = table.newCommit(UUID.randomUUID().toString)
100+
try {
101+
commit.commit(messages)
102+
} finally {
103+
commit.close()
104+
}
97105
}
98-
val commit = table.newCommit(UUID.randomUUID().toString)
99-
commit.commit(Long.MaxValue, messages)
100-
}
106+
}
101107
}
108+
pathAndMessage.flatMap { case (paths, _) => paths }.collect()
109+
} finally {
110+
pathAndMessage.unpersist()
102111
}
103-
104-
pathAndMessage.mapPartitions(
105-
iter => {
106-
iter.flatMap { case (paths, _) => paths }
107-
})
108112
}
109113
}
110114

@@ -129,6 +133,6 @@ object SparkRemoveUnexistingFiles extends SQLConfHelper {
129133
table.isInstanceOf[FileStoreTable],
130134
s"Only FileStoreTable supports remove-unexsiting-files action. The table type is '${table.getClass.getName}'.")
131135
val fileStoreTable = table.asInstanceOf[FileStoreTable]
132-
SparkRemoveUnexistingFiles(fileStoreTable, dryRun, parallelism, spark).buildRDD().collect()
136+
SparkRemoveUnexistingFiles(fileStoreTable, dryRun, parallelism, spark).execute()
133137
}
134138
}

0 commit comments

Comments
 (0)