Skip to content

Commit

Permalink
Make ActionRequests that extend UpdateRequest to extend more generic …
Browse files Browse the repository at this point in the history
…ActionRequest

Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Nov 26, 2024
1 parent 5aaf114 commit ae9fa1b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@

package org.opensearch.indexmanagement.transform.action.stop

import org.opensearch.action.ActionRequest
import org.opensearch.action.ActionRequestValidationException
import org.opensearch.action.ValidateActions.addValidationError
import org.opensearch.action.update.UpdateRequest
import org.opensearch.core.common.io.stream.StreamInput
import org.opensearch.core.common.io.stream.StreamOutput
import java.io.IOException

class StopTransformRequest : UpdateRequest {
class StopTransformRequest : ActionRequest {

val id: String
get() = field

@Throws(IOException::class)
constructor(sin: StreamInput) : super(sin)
constructor(sin: StreamInput) : super(sin) {
this.id = sin.readString()
}

constructor(id: String) {
super.id(id)
this.id = id
}

override fun validate(): ActionRequestValidationException? {
var validationException: ActionRequestValidationException? = null
if (super.id().isEmpty()) {
if (this.id.isEmpty()) {
validationException = addValidationError("id is missing", validationException)
}
return validationException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ constructor(
private val log = LogManager.getLogger(javaClass)

override fun doExecute(task: Task, request: StopTransformRequest, actionListener: ActionListener<AcknowledgedResponse>) {
log.debug("Executing StopTransformAction on ${request.id()}")
log.debug("Executing StopTransformAction on ${request.id}")
log.debug(
"User and roles string from thread context: ${client.threadPool().threadContext.getTransient<String>(
ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT,
)}",
)
val getRequest = GetRequest(INDEX_MANAGEMENT_INDEX, request.id())
val getRequest = GetRequest(INDEX_MANAGEMENT_INDEX, request.id)
val user = buildUser(client.threadPool().threadContext)
client.threadPool().threadContext.stashContext().use {
client.get(
Expand Down Expand Up @@ -212,7 +212,8 @@ constructor(

private fun updateTransformJob(transform: Transform, request: StopTransformRequest, actionListener: ActionListener<AcknowledgedResponse>) {
val now = Instant.now().toEpochMilli()
request.index(IndexManagementPlugin.INDEX_MANAGEMENT_INDEX).setIfSeqNo(transform.seqNo).setIfPrimaryTerm(transform.primaryTerm)
val updateReq = UpdateRequest(IndexManagementPlugin.INDEX_MANAGEMENT_INDEX, request.id)
updateReq.setIfSeqNo(transform.seqNo).setIfPrimaryTerm(transform.primaryTerm)
.doc(
mapOf(
Transform.TRANSFORM_TYPE to
Expand All @@ -223,7 +224,7 @@ constructor(
),
)
client.update(
request,
updateReq,
object : ActionListener<UpdateResponse> {
override fun onResponse(response: UpdateResponse) {
actionListener.onResponse(AcknowledgedResponse(response.result == DocWriteResponse.Result.UPDATED))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package org.opensearch.indexmanagement.transform.action

import org.opensearch.action.DocWriteRequest
import org.opensearch.action.support.WriteRequest
import org.opensearch.action.update.UpdateRequest
import org.opensearch.common.io.stream.BytesStreamOutput
import org.opensearch.index.seqno.SequenceNumbers
import org.opensearch.indexmanagement.IndexManagementPlugin.Companion.INDEX_MANAGEMENT_INDEX
Expand Down Expand Up @@ -161,11 +162,11 @@ class RequestTests : OpenSearchTestCase() {

fun `test stop transform request`() {
val id = "some_id"
val req = StopTransformRequest(id).index(INDEX_MANAGEMENT_INDEX)
val req = UpdateRequest(INDEX_MANAGEMENT_INDEX, id)

val out = BytesStreamOutput().apply { req.writeTo(this) }
val streamedReq = StopTransformRequest(buildStreamInputForTransforms(out))

assertEquals(id, streamedReq.id())
assertEquals(id, streamedReq.id)
}
}

0 comments on commit ae9fa1b

Please sign in to comment.