-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'OTA-2116' into 'master'
OTA-2116: Delete an existing static delta with a background process removing s3 objects marked for deletion Closes OTA-2116 See merge request torizon-platform/treehub!117
- Loading branch information
Showing
15 changed files
with
513 additions
and
3 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
src/main/resources/db/migration/treehub/V19__static_deltas_status_change.sql
This file contains 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,4 @@ | ||
ALTER TABLE `static_deltas` | ||
CHANGE `status` `status` | ||
ENUM('uploading', 'available', 'deleted') NOT NULL | ||
; |
This file contains 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 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
75 changes: 75 additions & 0 deletions
75
src/main/scala/com/advancedtelematic/treehub/daemon/DeletedDeltaCleanupActor.scala
This file contains 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,75 @@ | ||
package com.advancedtelematic.treehub.daemon | ||
|
||
import akka.actor.{Actor, ActorLogging, Props} | ||
import akka.actor.Status.Failure | ||
import akka.pattern.{BackoffOpts, BackoffSupervisor} | ||
import com.advancedtelematic.data.DataType.StaticDeltaMeta | ||
import com.advancedtelematic.treehub.daemon.DeletedDeltaCleanupActor.{Done, Tick, defaultTickInterval} | ||
import com.advancedtelematic.treehub.db.StaticDeltaMetaRepositorySupport | ||
import com.advancedtelematic.treehub.object_store.BlobStore | ||
import slick.jdbc.MySQLProfile.api.* | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
import scala.concurrent.duration.{DurationInt, FiniteDuration} | ||
|
||
object DeletedDeltaCleanupActor { | ||
case object Tick | ||
case class Done(count: Long, failedCount: Long) | ||
|
||
private val defaultTickInterval = 10.minutes | ||
|
||
def props(storage: BlobStore, tickInterval: FiniteDuration = defaultTickInterval, autoStart: Boolean = false)(implicit db: Database, ec: ExecutionContext) = | ||
Props(new DeletedDeltaCleanupActor(storage, tickInterval, autoStart)) | ||
|
||
def withBackOff(blobStore: BlobStore, tickInterval: FiniteDuration = defaultTickInterval, autoStart: Boolean = false)(implicit db: Database, ec: ExecutionContext) = | ||
BackoffSupervisor.props(BackoffOpts.onFailure(props(blobStore, tickInterval, autoStart), "deleted-deltas-worker", 10.minutes, 1.hour, 0.25)) | ||
} | ||
|
||
class DeletedDeltaCleanupActor(storage: BlobStore, tickInterval: FiniteDuration = defaultTickInterval, autoStart: Boolean = false)(implicit val db: Database, ec: ExecutionContext) extends Actor | ||
with StaticDeltaMetaRepositorySupport | ||
with ActorLogging { | ||
|
||
import akka.pattern.pipe | ||
|
||
import scala.async.Async.* | ||
|
||
override def preStart(): Unit = { | ||
if(autoStart) | ||
self ! Tick | ||
} | ||
|
||
def cleanupDelta(delta: StaticDeltaMeta): Future[Boolean] = | ||
storage.deleteObjects(delta.namespace, delta.id.asPrefixedPath) | ||
.flatMap { _ => | ||
staticDeltaMetaRepository.delete(delta.namespace, delta.id) | ||
.map { _ => true } | ||
} | ||
.recover { case ex => | ||
log.error(ex, s"Failed to delete delta ${delta.id}") | ||
false | ||
} | ||
|
||
def processAll: Future[Done] = async { | ||
val deltas = await { staticDeltaMetaRepository.findByStatus(StaticDeltaMeta.Status.Deleted) } | ||
|
||
val failedCount = await { Future.sequence(deltas.map(cleanupDelta)) }.count(_ == false) | ||
|
||
Done(deltas.length, failedCount) | ||
} | ||
|
||
override def receive: Receive = { | ||
case Done(count, failedCount) => | ||
if(count > 0) | ||
log.info(s"Batch done, processed ${count - failedCount} objects successfully - with $failedCount failures") | ||
else | ||
log.debug("Tick, scheduling next execution") | ||
|
||
context.system.scheduler.scheduleOnce(tickInterval, self, Tick) | ||
|
||
case Failure(ex) => | ||
throw ex | ||
|
||
case Tick => | ||
processAll.pipeTo(sender()) | ||
} | ||
} |
This file contains 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 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 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 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 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 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
Oops, something went wrong.