Skip to content

Commit

Permalink
Fix ConvertIndexToRemoteActionIT
Browse files Browse the repository at this point in the history
Signed-off-by: Seung Yeon Joo <[email protected]>
  • Loading branch information
Seung Yeon Joo committed Jan 7, 2025
1 parent 309321e commit 174cd04
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class AttemptRestoreStep(private val action: ConvertIndexToRemoteAction) : Step(

// Proceed with the restore operation
val restoreSnapshotRequest = RestoreSnapshotRequest(repository, snapshotName)
.indices("*")
.indices(indexName)
.storageType(RestoreSnapshotRequest.StorageType.REMOTE_SNAPSHOT)
.renamePattern("^(.*)\$")
.renameReplacement("$1_remote")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,113 @@ package org.opensearch.indexmanagement.indexstatemanagement.action
import org.opensearch.indexmanagement.indexstatemanagement.IndexStateManagementRestTestCase
import org.opensearch.indexmanagement.indexstatemanagement.model.Policy
import org.opensearch.indexmanagement.indexstatemanagement.model.State
import org.opensearch.indexmanagement.indexstatemanagement.model.Transition
import org.opensearch.indexmanagement.indexstatemanagement.randomErrorNotification
import org.opensearch.indexmanagement.indexstatemanagement.step.restore.AttemptRestoreStep
import org.opensearch.indexmanagement.indexstatemanagement.step.restore.WaitForRestoreStep
import org.opensearch.indexmanagement.indexstatemanagement.step.snapshot.AttemptSnapshotStep
import org.opensearch.indexmanagement.indexstatemanagement.step.snapshot.WaitForSnapshotStep
import org.opensearch.indexmanagement.waitFor
import org.opensearch.test.OpenSearchTestCase
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.Locale

class ConvertIndexToRemoteActionIT : IndexStateManagementRestTestCase() {

private val testIndexName = javaClass.simpleName.lowercase(Locale.ROOT)

fun `test basic conversion to remote index`() {
val indexName = "${testIndexName}_index_basic"
val policyID = "${testIndexName}_policy_basic"
fun `test snapshot then convert to remote index`() {
val indexName = "${testIndexName}_index_snapshot_and_convert"
val policyID = "${testIndexName}_policy_snapshot_and_convert"
val repository = "repository"

// Step 1: Create an index and index some data
createIndex(indexName, null)
indexDoc(indexName, "1", """{"field": "value1"}""")

// Step 2: Create a snapshot repository and take a snapshot of the index
createRepository(repository)
val snapshotName = "$indexName-${OpenSearchTestCase.randomAlphaOfLength(10).lowercase()}"
createSnapshot(repository, snapshotName, true)

// Step 3: Assign a policy with ConvertIndexToRemoteAction to the index
val actionConfig = ConvertIndexToRemoteAction(repository, 0)
val states = listOf(State("ConvertToRemote", listOf(actionConfig), listOf()))
val snapshotAction = SnapshotAction(
repository = repository,
snapshot = indexName,
index = 0,
)

val convertAction = ConvertIndexToRemoteAction(
repository = repository,
0,
)

val snapshotState = State(
name = "snapshotState",
actions = listOf(snapshotAction),
transitions = listOf(Transition(stateName = "convertToRemoteState", conditions = null)),
)

val convertToRemoteState = State(
name = "convertToRemoteState",
actions = listOf(convertAction),
transitions = listOf(),
)

val policy = Policy(
id = policyID,
description = "$testIndexName description",
schemaVersion = 1L,
lastUpdatedTime = Instant.now().truncatedTo(ChronoUnit.MILLIS),
errorNotification = randomErrorNotification(),
defaultState = states[0].name,
states = states,
defaultState = snapshotState.name,
states = listOf(snapshotState, convertToRemoteState),
)

createPolicy(policy, policyID)
addPolicyToIndex(indexName, policyID)

val managedIndexConfig = getExistingManagedIndexConfig(indexName)

// Step 4: Trigger the action
updateManagedIndexConfigStartTime(managedIndexConfig)
waitFor { assertEquals(policyID, getExplainManagedIndexMetaData(indexName).policyID) }

// Trigger AttemptRestoreStep
waitFor {
val explainMetaData = getExplainManagedIndexMetaData(indexName)
assertEquals(
"Successfully initialized policy: convertindextoremoteactionit_policy_snapshot_and_convert",
explainMetaData.info?.get("message"),
)
}
updateManagedIndexConfigStartTime(managedIndexConfig)
waitFor {
val explainMetaData = getExplainManagedIndexMetaData(indexName)
assertEquals(
AttemptSnapshotStep.getSuccessMessage(indexName),
explainMetaData.info?.get("message"),
)
}
updateManagedIndexConfigStartTime(managedIndexConfig)
waitFor {
val explainMetaData = getExplainManagedIndexMetaData(indexName)
assertEquals(AttemptRestoreStep.getSuccessMessage(indexName), explainMetaData.info?.get("message"))
assertEquals(
WaitForSnapshotStep.getSuccessMessage(indexName),
explainMetaData.info?.get("message"),
)
}

// Trigger WaitForRestoreStep
updateManagedIndexConfigStartTime(managedIndexConfig)
waitFor {
val explainMetaData = getExplainManagedIndexMetaData(indexName)
assertEquals(WaitForRestoreStep.getSuccessMessage(indexName), explainMetaData.info?.get("message"))
assertEquals(
"Transitioning to convertToRemoteState [index=convertindextoremoteactionit_index_snapshot_and_convert]",
explainMetaData.info?.get("message"),
)
}
updateManagedIndexConfigStartTime(managedIndexConfig)
waitFor {
val explainMetaData = getExplainManagedIndexMetaData(indexName)
assertEquals(
AttemptRestoreStep.getSuccessMessage(indexName),
explainMetaData.info?.get("message"),
)
}

// Step 5: Verify that the remote index exists and contains the expected data
val remoteIndexName = "${indexName}_remote"
waitFor { assertIndexExists(remoteIndexName) }

// Verify that the restored index is a remote index
val isRemote = isIndexRemote(remoteIndexName)
assertTrue("Index $remoteIndexName is not a remote index", isRemote)
}
Expand Down

0 comments on commit 174cd04

Please sign in to comment.