-
Notifications
You must be signed in to change notification settings - Fork 156
feat(file-service): cleanup stale uncommitted dataset uploads #5643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eugenegujing
wants to merge
4
commits into
apache:main
Choose a base branch
from
eugenegujing:feat/staged-file-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5248032
feat(file-service): cleanup stale uncommitted dataset uploads
eugenegujing 820a0c0
test(file-service): cover cleanup-job lifecycle, skip/error and diff-…
eugenegujing d00460a
Potential fix for pull request finding
eugenegujing 72252c2
fix(file-service): wrap session cleanup in a transaction and treat pe…
eugenegujing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
247 changes: 247 additions & 0 deletions
247
file-service/src/main/scala/org/apache/texera/service/util/StagedFileCleanupJob.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| /* | ||
| * 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.texera.service.util | ||
|
|
||
| import com.typesafe.scalalogging.LazyLogging | ||
| import io.dropwizard.lifecycle.Managed | ||
| import io.lakefs.clients.sdk.ApiException | ||
| import io.lakefs.clients.sdk.model.Diff | ||
| import org.apache.texera.amber.core.storage.util.LakeFSStorageClient | ||
| import org.apache.texera.dao.SqlServer | ||
| import org.apache.texera.dao.jooq.generated.tables.Dataset.DATASET | ||
| import org.apache.texera.dao.jooq.generated.tables.DatasetUploadSession.DATASET_UPLOAD_SESSION | ||
|
|
||
| import java.time.OffsetDateTime | ||
| import java.util.concurrent.{Executors, ScheduledExecutorService, TimeUnit} | ||
| import scala.jdk.CollectionConverters._ | ||
|
|
||
| /** | ||
| * Summary of one cleanup round. | ||
| * | ||
| * @param sessionsDeleted Number of abandoned upload session rows deleted. | ||
| * @param objectsReset Number of staged (uncommitted) objects reset in LakeFS. | ||
| * @param errors Number of failures encountered (each is retried next round). | ||
| */ | ||
| case class CleanupReport(sessionsDeleted: Int, objectsReset: Int, errors: Int) | ||
|
|
||
| /** | ||
| * Periodically cleans up uploaded but uncommitted dataset files: | ||
| * 1. Aborts and deletes abandoned multipart upload sessions older than the retention window. | ||
| * 2. Resets staged (uncommitted) LakeFS objects older than the retention window, skipping | ||
| * objects that belong to still-active upload sessions. | ||
| * | ||
| * @param retentionHours Age (in hours) after which uncommitted uploads are cleaned up. | ||
| * @param intervalMinutes Delay (in minutes) between cleanup rounds. | ||
| */ | ||
| class StagedFileCleanupJob(retentionHours: Int, intervalMinutes: Int) | ||
| extends Managed | ||
| with LazyLogging { | ||
|
|
||
| require(retentionHours > 0, s"retentionHours must be > 0 (got $retentionHours)") | ||
| require(intervalMinutes > 0, s"intervalMinutes must be > 0 (got $intervalMinutes)") | ||
|
|
||
| private var executor: ScheduledExecutorService = _ | ||
|
|
||
| override def start(): Unit = { | ||
| executor = Executors.newSingleThreadScheduledExecutor((runnable: Runnable) => { | ||
| val thread = new Thread(runnable, "staged-file-cleanup") | ||
| thread.setDaemon(true) | ||
| thread | ||
| }) | ||
| executor.scheduleWithFixedDelay( | ||
| () => { | ||
| try { | ||
| runCleanupOnce() | ||
| } catch { | ||
| // An exception must never kill the schedule. | ||
| case t: Throwable => logger.error("Staged file cleanup round failed", t) | ||
| } | ||
| }, | ||
| // Small fixed initial delay so a restart doesn't postpone backlog cleanup by up to a | ||
| // full interval. | ||
| 1L, | ||
| intervalMinutes.toLong, | ||
| TimeUnit.MINUTES | ||
| ) | ||
| } | ||
|
|
||
| override def stop(): Unit = { | ||
| if (executor != null) { | ||
| executor.shutdown() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Runs a single cleanup round. Idempotent: rows/objects already cleaned up are not | ||
| * revisited, and failures are retried on the next round. | ||
| * | ||
| * @param now The reference time used to evaluate the retention window. | ||
| * @return Summary counts for this round. | ||
| */ | ||
| def runCleanupOnce(now: OffsetDateTime = OffsetDateTime.now()): CleanupReport = { | ||
| val cutoff = now.minusHours(retentionHours.toLong) | ||
| var sessionsDeleted = 0 | ||
| var objectsReset = 0 | ||
| var errors = 0 | ||
|
|
||
| val ctx = SqlServer.getInstance().createDSLContext() | ||
|
|
||
| // Map each dataset id to its LakeFS repository name (same mapping DatasetResource uses | ||
| // via dataset.getRepositoryName). | ||
| val repoNameByDid: Map[Integer, String] = ctx | ||
| .select(DATASET.DID, DATASET.REPOSITORY_NAME) | ||
| .from(DATASET) | ||
| .where(DATASET.REPOSITORY_NAME.isNotNull) | ||
| .fetch() | ||
| .asScala | ||
| .map(record => record.get(DATASET.DID) -> record.get(DATASET.REPOSITORY_NAME)) | ||
| .toMap | ||
|
eugenegujing marked this conversation as resolved.
|
||
|
|
||
| // Path 1: abort and delete abandoned multipart upload sessions. | ||
| val expiredSessions = ctx | ||
| .selectFrom(DATASET_UPLOAD_SESSION) | ||
| .where(DATASET_UPLOAD_SESSION.CREATED_AT.lt(cutoff)) | ||
| .fetch() | ||
| .asScala | ||
| .toList | ||
|
|
||
| expiredSessions.foreach { session => | ||
| try { | ||
| // Delete the row and abort the multipart in one transaction, deleting FIRST. LakeFS is | ||
| // external and cannot truly enroll in a DB transaction, but the abort is idempotent | ||
| // (re-aborting an already-aborted upload returns 404, treated as success below), so the | ||
| // only risk is the abort failing AFTER the delete is staged. By staging the delete first | ||
| // and letting a non-404 abort failure roll the whole transaction back, the session row | ||
| // survives and the next round retries — never leaving an orphaned multipart behind. | ||
| SqlServer.withTransaction(ctx) { txn => | ||
| txn | ||
| .deleteFrom(DATASET_UPLOAD_SESSION) | ||
| .where(DATASET_UPLOAD_SESSION.UPLOAD_ID.eq(session.getUploadId)) | ||
| .execute() | ||
| repoNameByDid.get(session.getDid) match { | ||
| case Some(repoName) => | ||
| try { | ||
| LakeFSStorageClient.abortPresignedMultipartUploads( | ||
| repoName, | ||
| session.getFilePath, | ||
| session.getUploadId, | ||
| session.getPhysicalAddress | ||
| ) | ||
| } catch { | ||
| // Already aborted (or never materialized): safe to delete the session row. | ||
| case e: ApiException if e.getCode == 404 => | ||
| logger.debug( | ||
| s"Multipart upload ${session.getUploadId} not found in LakeFS; " + | ||
| "treating as already aborted" | ||
| ) | ||
| } | ||
| case None => | ||
| // Dataset row gone or repository_name is NULL: the multipart lived in that | ||
| // repository's namespace, so there is nothing left to abort. | ||
| logger.debug( | ||
| s"No repository for dataset ${session.getDid}; " + | ||
| s"deleting orphan upload session ${session.getUploadId}" | ||
| ) | ||
| } | ||
| } | ||
| sessionsDeleted += 1 | ||
| } catch { | ||
| case t: Throwable => | ||
| logger.warn( | ||
| s"Failed to clean up upload session ${session.getUploadId} " + | ||
| s"(did=${session.getDid}, path=${session.getFilePath}); will retry next round", | ||
| t | ||
| ) | ||
| errors += 1 | ||
| } | ||
| } | ||
|
|
||
| // File paths of still-active (non-expired) upload sessions, per dataset. Staged objects | ||
| // belonging to an active upload must never be reset. | ||
| val activePathsByDid: Map[Integer, Set[String]] = ctx | ||
| .select(DATASET_UPLOAD_SESSION.DID, DATASET_UPLOAD_SESSION.FILE_PATH) | ||
| .from(DATASET_UPLOAD_SESSION) | ||
| .where(DATASET_UPLOAD_SESSION.CREATED_AT.ge(cutoff)) | ||
| .fetch() | ||
| .asScala | ||
| .groupBy(record => record.get(DATASET_UPLOAD_SESSION.DID)) | ||
| .map { | ||
| case (did, records) => | ||
| did -> records.map(_.get(DATASET_UPLOAD_SESSION.FILE_PATH)).toSet | ||
| } | ||
|
|
||
| // Path 2: reset staged (uncommitted) objects older than the retention window. | ||
| repoNameByDid.foreach { | ||
| case (did, repoName) => | ||
| try { | ||
| val activePaths = activePathsByDid.getOrElse(did, Set.empty) | ||
| val stagedObjects = LakeFSStorageClient.retrieveUncommittedObjects(repoName) | ||
| // diffBranch carries no mtime, so each candidate costs one extra statObject call | ||
| // (N+1). Unavoidable until LakeFS exposes timestamps in the diff API. | ||
| stagedObjects.foreach { diff => | ||
| val path = diff.getPath | ||
| val isObjectWrite = | ||
| diff.getType == Diff.TypeEnum.ADDED || diff.getType == Diff.TypeEnum.CHANGED | ||
| if (!isObjectWrite) { | ||
| // E.g. a staged deletion of a committed file: there is no object behind it and | ||
| // it consumes no storage, so leaving it is correct and cheap. | ||
| logger.debug(s"Skipping staged ${diff.getType} entry '$path' in '$repoName'") | ||
| } else if (!activePaths.contains(path)) { | ||
| try { | ||
| val mtime = LakeFSStorageClient.getStagedObjectMtime(repoName, path) | ||
| if (mtime < cutoff.toEpochSecond) { | ||
| LakeFSStorageClient.resetObjectUploadOrDeletion(repoName, path) | ||
| objectsReset += 1 | ||
| } | ||
| } catch { | ||
| // Concurrently committed/reset, or already cleaned by another round: the | ||
| // object is gone, which is the desired end state for an idempotent job. | ||
| case e: ApiException if e.getCode == 404 => | ||
| logger.debug( | ||
| s"Staged object '$path' not found in repo '$repoName'; " + | ||
| "treating as already cleaned" | ||
| ) | ||
| case t: Throwable => | ||
| logger.warn( | ||
| s"Failed to clean up staged object '$path' in repo '$repoName'", | ||
| t | ||
| ) | ||
| errors += 1 | ||
| } | ||
| } | ||
| } | ||
| } catch { | ||
| // The dataset's LakeFS repository was deleted out-of-band (a supported state): | ||
| // nothing staged to clean up there. | ||
| case e: ApiException if e.getCode == 404 => | ||
| logger.debug(s"Repository '$repoName' not found in LakeFS; skipping") | ||
| case t: Throwable => | ||
| logger.warn(s"Failed to clean up staged objects in repo '$repoName'", t) | ||
| errors += 1 | ||
| } | ||
| } | ||
|
|
||
| logger.info( | ||
| s"Staged file cleanup round finished: sessionsDeleted=$sessionsDeleted, " + | ||
| s"objectsReset=$objectsReset, errors=$errors" | ||
| ) | ||
| CleanupReport(sessionsDeleted, objectsReset, errors) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.