-
Notifications
You must be signed in to change notification settings - Fork 62
Jhwang/add ability to modify partition #448
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2a1022b
modify partiion - wup
jeffhwang-sq 5d73d87
add modifying partition end points and UI
jeffhwang-sq 3776294
Merge branch 'master' into jhwang/add-ability-to-modify-partition
jeffhwang-sq 64f542d
remove extra files
jeffhwang-sq 622b24b
Add "Cancelled" Button for Paused Backfills (#442)
jeffhwang-sq 583babf
Improve BatchWriteItem retry handling (#449)
notmikedavis d8f791d
Add ApiCallTimeoutException handling to UpdateInPlaceDynamoDbBackfill…
notmikedavis 16837ed
redirect users to backfill page after success
jeffhwang-sq 4dfcf9c
Merge branch 'master' into jhwang/add-ability-to-modify-partition
jeffhwang-sq 67b774c
add test
jeffhwang-sq 0a8d0d6
render the error to users
jeffhwang-sq 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
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
150 changes: 150 additions & 0 deletions
150
service/src/main/kotlin/app/cash/backfila/dashboard/EditPartitionCursorAction.kt
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,150 @@ | ||
package app.cash.backfila.dashboard | ||
|
||
import app.cash.backfila.ui.components.DashboardPageLayout | ||
import app.cash.backfila.ui.pages.BackfillShowAction | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
import kotlinx.html.ButtonType | ||
import kotlinx.html.FormMethod | ||
import kotlinx.html.InputType | ||
import kotlinx.html.a | ||
import kotlinx.html.button | ||
import kotlinx.html.div | ||
import kotlinx.html.form | ||
import kotlinx.html.h1 | ||
import kotlinx.html.input | ||
import kotlinx.html.label | ||
import kotlinx.html.p | ||
import misk.security.authz.Authenticated | ||
import misk.tailwind.Link | ||
import misk.web.Get | ||
import misk.web.PathParam | ||
import misk.web.Response | ||
import misk.web.ResponseBody | ||
import misk.web.ResponseContentType | ||
import misk.web.actions.WebAction | ||
import misk.web.mediatype.MediaTypes | ||
|
||
@Singleton | ||
class EditPartitionCursorAction @Inject constructor( | ||
private val getBackfillStatusAction: GetBackfillStatusAction, | ||
private val dashboardPageLayout: DashboardPageLayout, | ||
) : WebAction { | ||
|
||
@Get(PATH) | ||
@ResponseContentType(MediaTypes.TEXT_HTML) | ||
@Authenticated(capabilities = ["users"]) | ||
fun get( | ||
@PathParam id: Long, | ||
@PathParam partitionName: String, | ||
): Response<ResponseBody> { | ||
val backfill = getBackfillStatusAction.status(id) | ||
|
||
val partition = backfill.partitions.find { it.name == partitionName } | ||
?: throw IllegalArgumentException("Partition not found") | ||
|
||
// Take a snapshot of current cursor for validation | ||
val cursorSnapshot = partition.pkey_cursor | ||
|
||
return Response( | ||
dashboardPageLayout.newBuilder() | ||
.title("Edit Cursor - Partition $partitionName") | ||
.breadcrumbLinks( | ||
Link("Backfill #$id", BackfillShowAction.path(id)), | ||
Link("Edit Cursor", path(id, partitionName)), | ||
) | ||
.buildHtmlResponseBody { | ||
div("space-y-6 max-w-2xl mx-auto py-8") { | ||
h1("text-xl font-semibold") { | ||
+"Edit Cursor for Partition: $partitionName" | ||
} | ||
|
||
div("rounded-md bg-yellow-50 p-4 mb-6") { | ||
div("flex") { | ||
div("flex-shrink-0") { | ||
// Warning icon | ||
div("h-5 w-5 text-yellow-400") { | ||
+"⚠️" | ||
} | ||
} | ||
div("ml-3") { | ||
h1("text-sm font-medium text-yellow-800") { | ||
+"Warning: Editing cursors can be dangerous" | ||
} | ||
div("mt-2 text-sm text-yellow-700") { | ||
p { | ||
+"Make sure you understand the implications of changing the cursor position. Records between the old and new cursor positions may be skipped or processed multiple times." | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
form { | ||
method = FormMethod.get | ||
action = EditPartitionCursorHandlerAction.path(id, partitionName) | ||
|
||
input { | ||
type = InputType.hidden | ||
name = "cursor_snapshot" | ||
value = cursorSnapshot ?: "" | ||
} | ||
|
||
div("space-y-4") { | ||
div { | ||
label("block text-sm font-medium text-gray-700") { | ||
htmlFor = "current_cursor" | ||
+"Current Cursor" | ||
} | ||
div("mt-1") { | ||
input(classes = "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6") { | ||
type = InputType.text | ||
attributes["id"] = "current_cursor" | ||
value = cursorSnapshot ?: "Not started" | ||
disabled = true | ||
} | ||
} | ||
} | ||
|
||
div { | ||
label("block text-sm font-medium text-gray-700") { | ||
htmlFor = "new_cursor" | ||
+"New Cursor" | ||
} | ||
div("mt-1") { | ||
input(classes = "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6") { | ||
type = InputType.text | ||
name = "new_cursor" | ||
attributes["id"] = "new_cursor" | ||
value = cursorSnapshot ?: "" | ||
required = true | ||
} | ||
} | ||
p("mt-2 text-sm text-gray-500") { | ||
+"Enter the new cursor value. This must be a valid UTF-8 string." | ||
} | ||
} | ||
|
||
div("flex justify-end gap-3") { | ||
a(href = BackfillShowAction.path(id), classes = "rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50") { | ||
+"Cancel" | ||
} | ||
button(classes = "rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600") { | ||
type = ButtonType.submit | ||
+"Update Cursor" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
) | ||
} | ||
|
||
companion object { | ||
private const val PATH = "/backfills/{id}/{partitions}/{partitionName}/edit-cursor" | ||
fun path(id: Long, partitionName: String) = PATH | ||
.replace("{id}", id.toString()) | ||
.replace("{partitionName}", partitionName) | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
service/src/main/kotlin/app/cash/backfila/dashboard/EditPartitionCursorHandlerAction.kt
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,91 @@ | ||
package app.cash.backfila.dashboard | ||
|
||
import app.cash.backfila.service.persistence.BackfilaDb | ||
import app.cash.backfila.service.persistence.BackfillState | ||
import app.cash.backfila.service.persistence.RunPartitionQuery | ||
import app.cash.backfila.ui.pages.BackfillShowAction | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
import misk.exceptions.BadRequestException | ||
import misk.hibernate.Query | ||
import misk.hibernate.Transacter | ||
import misk.hibernate.newQuery | ||
import misk.scope.ActionScoped | ||
import misk.security.authz.Authenticated | ||
import misk.web.Get | ||
import misk.web.HttpCall | ||
import misk.web.PathParam | ||
import misk.web.Response | ||
import misk.web.ResponseBody | ||
import misk.web.ResponseContentType | ||
import misk.web.actions.WebAction | ||
import misk.web.mediatype.MediaTypes | ||
import misk.web.toResponseBody | ||
import okhttp3.Headers | ||
import okio.ByteString.Companion.encodeUtf8 | ||
|
||
@Singleton | ||
class EditPartitionCursorHandlerAction @Inject constructor( | ||
private val getBackfillStatusAction: GetBackfillStatusAction, | ||
@BackfilaDb private val transacter: Transacter, | ||
private val queryFactory: Query.Factory, | ||
private val httpCall: ActionScoped<HttpCall>, | ||
) : WebAction { | ||
|
||
@Get(PATH) | ||
@ResponseContentType(MediaTypes.TEXT_HTML) | ||
@Authenticated(capabilities = ["users"]) | ||
fun get( | ||
@PathParam id: Long, | ||
@PathParam partitionName: String, | ||
): Response<ResponseBody> { | ||
val request = httpCall.get().asOkHttpRequest() | ||
val cursorSnapshot = request.url.queryParameter("cursor_snapshot")?.takeIf { it.isNotBlank() } | ||
val newCursor = request.url.queryParameter("new_cursor") | ||
|
||
// Validate UTF-8 | ||
try { | ||
newCursor?.toByteArray(Charsets.UTF_8)?.toString(Charsets.UTF_8) | ||
} catch (e: Exception) { | ||
throw BadRequestException("New cursor must be valid UTF-8") | ||
} | ||
|
||
// Verify backfill state and cursor hasn't changed | ||
val backfill = getBackfillStatusAction.status(id) | ||
if (backfill.state != BackfillState.PAUSED) { | ||
throw BadRequestException("Backfill must be paused to edit cursors") | ||
} | ||
|
||
val partition = backfill.partitions.find { it.name == partitionName } | ||
?: throw BadRequestException("Partition not found") | ||
|
||
if (partition.pkey_cursor != cursorSnapshot) { | ||
throw BadRequestException("Cursor has changed since edit form was loaded") | ||
} | ||
|
||
// Update the cursor | ||
transacter.transaction { session -> | ||
queryFactory.newQuery<RunPartitionQuery>() | ||
.partitionId(partition.id) | ||
.uniqueResult(session) | ||
?.let { partitionRecord -> | ||
partitionRecord.pkey_cursor = newCursor?.encodeUtf8() | ||
session.save(partitionRecord) | ||
} ?: throw BadRequestException("Partition not found") | ||
} | ||
|
||
// Redirect to backfill page | ||
return Response( | ||
body = "go to ${BackfillShowAction.path(id)}".toResponseBody(), | ||
statusCode = 303, | ||
headers = Headers.headersOf("Location", BackfillShowAction.path(id)), | ||
) | ||
} | ||
|
||
companion object { | ||
private const val PATH = "/backfills/{id}/{partitionName}/edit-cursor" | ||
fun path(id: Long, partitionName: String) = PATH | ||
.replace("{id}", id.toString()) | ||
.replace("{partitionName}", partitionName) | ||
} | ||
} |
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
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.