-
Notifications
You must be signed in to change notification settings - Fork 51
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
Adding a way to delete backfill runs #207
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package app.cash.backfila.dashboard | |
import app.cash.backfila.service.SlackHelper | ||
import app.cash.backfila.service.persistence.BackfilaDb | ||
import app.cash.backfila.service.persistence.BackfillState | ||
import app.cash.backfila.service.persistence.BackfillState.CANCELLED | ||
import app.cash.backfila.service.persistence.BackfillState.PAUSED | ||
import app.cash.backfila.service.persistence.BackfillState.RUNNING | ||
import app.cash.backfila.service.persistence.DbBackfillRun | ||
|
@@ -25,7 +26,10 @@ class BackfillStateToggler @Inject constructor( | |
val requiredCurrentState = when (desiredState) { | ||
PAUSED -> RUNNING | ||
RUNNING -> PAUSED | ||
else -> throw IllegalArgumentException("can only toggle to RUNNING or PAUSED") | ||
CANCELLED -> PAUSED | ||
else -> throw IllegalArgumentException( | ||
"can only toggle between RUNNING and PAUSED or cancel a PAUSED run. " | ||
) | ||
} | ||
|
||
transacter.transaction { session -> | ||
|
@@ -46,22 +50,27 @@ class BackfillStateToggler @Inject constructor( | |
} | ||
run.setState(session, queryFactory, desiredState) | ||
|
||
val startedOrStopped = if (desiredState == RUNNING) "started" else "stopped" | ||
val action = when (desiredState) { | ||
PAUSED -> "stopped" | ||
RUNNING -> "started" | ||
CANCELLED -> "cancelled" | ||
else -> desiredState.name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not reachable right? throw exception instead? |
||
} | ||
session.save( | ||
DbEventLog( | ||
run.id, | ||
partition_id = null, | ||
user = caller.principal, | ||
type = DbEventLog.Type.STATE_CHANGE, | ||
message = "backfill $startedOrStopped" | ||
message = "backfill $action" | ||
) | ||
) | ||
} | ||
|
||
if (desiredState == RUNNING) { | ||
slackHelper.runStarted(Id(id), caller.principal) | ||
} else { | ||
slackHelper.runPaused(Id(id), caller.principal) | ||
when (desiredState) { | ||
RUNNING -> slackHelper.runStarted(Id(id), caller.principal) | ||
PAUSED -> slackHelper.runPaused(Id(id), caller.principal) | ||
CANCELLED -> slackHelper.runCancelled(Id(id), caller.principal) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you really want to notify? I wouldn't think anyone cares |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package app.cash.backfila.dashboard | ||
|
||
import app.cash.backfila.service.persistence.BackfillState | ||
import javax.inject.Inject | ||
import misk.MiskCaller | ||
import misk.scope.ActionScoped | ||
import misk.security.authz.Authenticated | ||
import misk.web.PathParam | ||
import misk.web.Post | ||
import misk.web.RequestBody | ||
import misk.web.RequestContentType | ||
import misk.web.ResponseContentType | ||
import misk.web.actions.WebAction | ||
import misk.web.mediatype.MediaTypes | ||
import wisp.logging.getLogger | ||
|
||
class CancelBackfillRequest | ||
class CancelBackfillResponse | ||
|
||
class CancelBackfillAction @Inject constructor( | ||
private val caller: @JvmSuppressWildcards ActionScoped<MiskCaller?>, | ||
private val backfillStateToggler: BackfillStateToggler | ||
) : WebAction { | ||
|
||
@Post("/backfills/{id}/cancel") | ||
@RequestContentType(MediaTypes.APPLICATION_JSON) | ||
@ResponseContentType(MediaTypes.APPLICATION_JSON) | ||
// TODO allow any user | ||
@Authenticated(capabilities = ["users"]) | ||
fun cancel( | ||
@PathParam id: Long, | ||
@RequestBody request: CancelBackfillRequest | ||
): CancelBackfillResponse { | ||
// TODO check user has permissions for this service with access api | ||
logger.info { "Canceling backfill $id by ${caller.get()?.user}" } | ||
backfillStateToggler.toggleRunningState(id, caller.get()!!, BackfillState.CANCELLED) | ||
return CancelBackfillResponse() | ||
} | ||
|
||
companion object { | ||
private val logger = getLogger<CancelBackfillAction>() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package app.cash.backfila.service.persistence | ||
|
||
enum class BackfillPartitionState { | ||
PAUSED, // A resumable backfill partition that is not currently meant to be running | ||
RUNNING, // A backfill partition that is allowed to run. | ||
COMPLETE, // A completed partition, this is a final non-resumable state. | ||
STALE, // A partition that is no longer relevant, this is a final non-resumable state (possibly a split partition) | ||
CANCELLED; // A partition that has been manually cancelled, this is a final non-resumable state. | ||
|
||
companion object { | ||
val FINAL_STATES = setOf(STALE, CANCELLED, COMPLETE) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
package app.cash.backfila.service.persistence | ||
|
||
enum class BackfillState { | ||
PAUSED, | ||
RUNNING, | ||
COMPLETE | ||
PAUSED, // A resumable backfill that is not currently meant to be running | ||
RUNNING, // A backfill that is allowed to run. | ||
COMPLETE, // A completed backfill, this is a final non-resumable state. | ||
CANCELLED; // A backfill that has been manually cancelled, this is a final non-resumable state. | ||
|
||
companion object { | ||
val FINAL_STATES = setOf(CANCELLED, COMPLETE) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. however, either is fine |
||
|
||
/** | ||
* When the Backfill state changes modify the underlying partitions to these corresponding states. | ||
*/ | ||
fun BackfillState.getPartitionState() = when (this) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
PAUSED -> BackfillPartitionState.PAUSED | ||
RUNNING -> BackfillPartitionState.RUNNING | ||
CANCELLED -> BackfillPartitionState.CANCELLED | ||
COMPLETE -> BackfillPartitionState.COMPLETE | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package app.cash.backfila.service.persistence | ||
|
||
import app.cash.backfila.service.persistence.BackfillState.Companion.getPartitionState | ||
import com.google.common.base.Preconditions.checkState | ||
import java.time.Instant | ||
import javax.persistence.Column | ||
|
@@ -130,18 +131,18 @@ class DbBackfillRun() : DbUnsharded<DbBackfillRun>, DbTimestampedEntity { | |
.list(session) | ||
|
||
fun setState(session: Session, queryFactory: Query.Factory, state: BackfillState) { | ||
// State can't be changed after being completed. | ||
checkState(this.state != BackfillState.COMPLETE) | ||
// Backfills in final states cannot change. | ||
checkState(!BackfillState.FINAL_STATES.contains(this.state)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could instead do
|
||
this.state = state | ||
// Set the state of all the partitions that are not complete | ||
val query = session.hibernateSession.createQuery( | ||
"update DbRunPartition " + | ||
"set run_state = :newState, version = version + 1 " + | ||
"where backfill_run_id = :runId and run_state <> :completed" | ||
"set partition_state = :newPartitionState, version = version + 1 " + | ||
"where backfill_run_id = :runId and partition_state not in ( :finalPartitionStates )" | ||
) | ||
query.setParameter("runId", id) | ||
query.setParameter("newState", state) | ||
query.setParameter("completed", BackfillState.COMPLETE) | ||
query.setParameter("newPartitionState", state.getPartitionState()) | ||
query.setParameterList("finalPartitionStates", BackfillPartitionState.FINAL_STATES) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool this works well. I was worried it would harder to read |
||
query.executeUpdate() | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why extension instead of a method that takes it in?