This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-9145-12999929f1ce305a7f09b80f19f702ad2bc59c90 in repository https://gitbox.apache.org/repos/asf/texera.git
commit 8fbd4f2267d6cf61fd76f9b00ea192572b50702b Author: Tanishq Gandhi <[email protected]> AuthorDate: Mon Oct 26 18:59:56 2026 +0000 fix(file-service, workflow-core): delete the temp files a version zip download leaves behind (#9145) ### What changes were proposed in this PR? The LakeFS SDK's `getObject().execute()` saves each object to a temp file and returns it. Two callers never deleted it: - `ResourceUploadService.versionZipResponse` (dataset and model `versionZip`) copied each file into the zip and left it in the temp directory, so every download left a full copy of the version on the file service's disk. - `LakeFSFileDocument.asInputStream`'s fallback, used when the presigned download fails, opened the temp file as a stream and left it behind. The zip now deletes each temp file right after copying it (in a `finally`), and the fallback opens it with `DELETE_ON_CLOSE`, so it's removed when the stream is closed. Three downloads of a version holding three 1 MB files, with the file service's temp dir pointed at an empty folder: ``` before after download 1 3 files, 3.0M 0 files download 2 6 files, 6.0M 0 files download 3 9 files, 9.0M 0 files ``` The zip's contents are unchanged (byte-for-byte identical files). ### Any related issues, documentation, discussions? Closes #9144 ### How was this PR tested? Added a case to `DatasetResourceSpec` (writing a version zip leaves no SDK temp files) and one to `LakeFSFileDocumentSpec` (reading through the lakeFS fallback and closing the stream leaves none). Both fail without the fix. ``` sbt "WorkflowCore/testOnly *LakeFSFileDocumentSpec" "FileService/testOnly *DatasetResourceSpec *ModelResourceSpec" # Tests: succeeded 22 / 181, failed 0 ``` Also checked against a local stack with the file service's temp dir pointed at an empty folder (table above). ### Was this PR authored or co-authored using generative AI tooling? Generated-by: Claude Code (Claude Opus 5.5) --- .../amber/core/storage/model/LakeFSFileDocument.scala | 5 +++-- .../core/storage/model/LakeFSFileDocumentSpec.scala | 16 ++++++++++++++++ .../service/resource/ResourceUploadService.scala | 13 +++++++++---- .../texera/service/resource/DatasetResourceSpec.scala | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/common/workflow-core/src/main/scala/org/apache/texera/amber/core/storage/model/LakeFSFileDocument.scala b/common/workflow-core/src/main/scala/org/apache/texera/amber/core/storage/model/LakeFSFileDocument.scala index 55fab33ce2..b43768e43c 100644 --- a/common/workflow-core/src/main/scala/org/apache/texera/amber/core/storage/model/LakeFSFileDocument.scala +++ b/common/workflow-core/src/main/scala/org/apache/texera/amber/core/storage/model/LakeFSFileDocument.scala @@ -28,7 +28,7 @@ import org.apache.texera.amber.core.storage.util.LakeFSStorageClient import java.io.{File, FileOutputStream, InputStream} import java.net._ import java.nio.charset.StandardCharsets -import java.nio.file.{Files, Path, Paths} +import java.nio.file.{Files, Path, Paths, StandardOpenOption} import scala.jdk.CollectionConverters.IteratorHasAsScala object LakeFSFileDocument { @@ -113,7 +113,8 @@ private[storage] class LakeFSFileDocument(uri: URI, val resourceType: ResourceTy getVersionHash(), getFileRelativePath() ) - Files.newInputStream(file.toPath) + // The SDK saves the object to a temp file; delete it once the caller closes the stream. + Files.newInputStream(file.toPath, StandardOpenOption.DELETE_ON_CLOSE) } if (userJwtToken.isEmpty) { diff --git a/common/workflow-core/src/test/scala/org/apache/texera/amber/core/storage/model/LakeFSFileDocumentSpec.scala b/common/workflow-core/src/test/scala/org/apache/texera/amber/core/storage/model/LakeFSFileDocumentSpec.scala index 6cdbb0acbb..bd9c87573c 100644 --- a/common/workflow-core/src/test/scala/org/apache/texera/amber/core/storage/model/LakeFSFileDocumentSpec.scala +++ b/common/workflow-core/src/test/scala/org/apache/texera/amber/core/storage/model/LakeFSFileDocumentSpec.scala @@ -191,6 +191,13 @@ class LakeFSFileDocumentSpec private def nestedRelativePath: String = Paths.get(nestedSegments.head, nestedSegments.tail: _*).toString + /** Temp files the lakeFS SDK writes for a direct download, by name. */ + private def sdkTempFiles(): Set[String] = + Option(new java.io.File(System.getProperty("java.io.tmpdir")).list()) + .map(_.toSet) + .getOrElse(Set.empty[String]) + .filter(_.startsWith("download-")) + private def readFully(doc: LakeFSFileDocument): String = { val stream = doc.asInputStream() try new String(stream.readAllBytes(), StandardCharsets.UTF_8) @@ -388,6 +395,15 @@ class LakeFSFileDocumentSpec hits.map(_.path) shouldEqual List(statPath, presignedPath, objectPath) } + it should "delete the temp file a direct lakeFS download writes once the stream is closed" in { + failing = Set(statPath) + val before = sdkTempFiles() + + readFully(servedDoc()) shouldBe fallbackContent + + (sdkTempFiles() -- before) shouldBe empty + } + it should "name the whole nested relative path on both hops, not the file's last segment" in { failing = Set(statPath) diff --git a/file-service/src/main/scala/org/apache/texera/service/resource/ResourceUploadService.scala b/file-service/src/main/scala/org/apache/texera/service/resource/ResourceUploadService.scala index f95a0f92f4..8adf4f6795 100644 --- a/file-service/src/main/scala/org/apache/texera/service/resource/ResourceUploadService.scala +++ b/file-service/src/main/scala/org/apache/texera/service/resource/ResourceUploadService.scala @@ -61,7 +61,7 @@ import software.amazon.awssdk.services.s3.model.UploadPartResponse import java.io.{InputStream, OutputStream} import java.net.URLDecoder import java.nio.charset.StandardCharsets -import java.nio.file.{Files, Paths} +import java.nio.file.Files import java.util.zip.{ZipEntry, ZipOutputStream} import java.sql.SQLException import java.time.OffsetDateTime @@ -386,9 +386,14 @@ object ResourceUploadService { val file = withLakeFSErrorHandling(s"downloading file '$filePath' for the zip") { LakeFSStorageClient.getFileFromRepo(repositoryName, versionHash, filePath) } - zipOut.putNextEntry(new ZipEntry(filePath)) - Files.copy(Paths.get(file.toURI), zipOut) - zipOut.closeEntry() + // The SDK saves each object to a temp file; drop it once it is in the zip. + try { + zipOut.putNextEntry(new ZipEntry(filePath)) + Files.copy(file.toPath, zipOut) + zipOut.closeEntry() + } finally { + Files.deleteIfExists(file.toPath) + } } } finally { zipOut.close() diff --git a/file-service/src/test/scala/org/apache/texera/service/resource/DatasetResourceSpec.scala b/file-service/src/test/scala/org/apache/texera/service/resource/DatasetResourceSpec.scala index 9361e46a70..0024ec354c 100644 --- a/file-service/src/test/scala/org/apache/texera/service/resource/DatasetResourceSpec.scala +++ b/file-service/src/test/scala/org/apache/texera/service/resource/DatasetResourceSpec.scala @@ -993,6 +993,13 @@ class DatasetResourceSpec assertStatus(ex, 403) } + /** Temp files the lakeFS SDK writes for a direct download, by name. */ + private def sdkTempFiles(): Set[String] = + Option(new java.io.File(System.getProperty("java.io.tmpdir")).list()) + .map(_.toSet) + .getOrElse(Set.empty[String]) + .filter(_.startsWith("download-")) + "getDatasetVersionZip" should "zip all files of a dataset version" in { val version = testDatasetVersion val resp = @@ -1003,6 +1010,17 @@ class DatasetResourceSpec out.size() should be > 0 } + it should "leave none of the downloaded files behind in the temp directory" in { + val version = testDatasetVersion + val before = sdkTempFiles() + + val resp = + datasetResource.getDatasetVersionZip(baseDataset.getDid, version.getDvid, null, sessionUser) + resp.getEntity.asInstanceOf[StreamingOutput].write(new ByteArrayOutputStream()) + + (sdkTempFiles() -- before) shouldBe empty + } + "getPresignedUrl" should "generate a presigned URL for an existing file" in { testDatasetVersion val resp = datasetResource.getPresignedUrl(
