Skip to content

Commit

Permalink
Not changing mapping type of snapshot name
Browse files Browse the repository at this point in the history
  • Loading branch information
thalurur committed Jun 21, 2021
1 parent de21d80 commit b25a9fd
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ import org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken
import org.opensearch.indexmanagement.indexstatemanagement.action.Action
import org.opensearch.indexmanagement.indexstatemanagement.action.SnapshotAction
import org.opensearch.indexmanagement.indexstatemanagement.model.ManagedIndexMetaData
import org.opensearch.script.Script
import org.opensearch.script.ScriptService
import java.io.IOException

data class SnapshotActionConfig(
val repository: String,
val snapshot: Script,
val snapshot: String,
val index: Int
) : ToXContentObject, ActionConfig(ActionType.SNAPSHOT, index) {

Expand All @@ -72,15 +71,15 @@ data class SnapshotActionConfig(
@Throws(IOException::class)
constructor(sin: StreamInput) : this(
repository = sin.readString(),
snapshot = Script(sin),
snapshot = sin.readString(),
index = sin.readInt()
)

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
super.writeTo(out)
out.writeString(repository)
snapshot.writeTo(out)
out.writeString(snapshot)
out.writeInt(index)
}

Expand All @@ -93,7 +92,7 @@ data class SnapshotActionConfig(
@Throws(IOException::class)
fun parse(xcp: XContentParser, index: Int): SnapshotActionConfig {
var repository: String? = null
var snapshot: Script? = null
var snapshot: String? = null

ensureExpectedToken(Token.START_OBJECT, xcp.currentToken(), xcp)
while (xcp.nextToken() != Token.END_OBJECT) {
Expand All @@ -102,7 +101,7 @@ data class SnapshotActionConfig(

when (fieldName) {
REPOSITORY_FIELD -> repository = xcp.text()
SNAPSHOT_FIELD -> snapshot = Script.parse(xcp, Script.DEFAULT_TEMPLATE_LANG)
SNAPSHOT_FIELD -> snapshot = xcp.text()
else -> throw IllegalArgumentException("Invalid field: [$fieldName] found in SnapshotActionConfig.")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class AttemptSnapshotStep(
.format(DateTimeFormatter.ofPattern("uuuu.MM.dd-HH:mm:ss.SSS", Locale.ROOT))
)

snapshotName = compileTemplate(config.snapshot, managedIndexMetaData, indexName).plus(snapshotNamePrefix)
val snapshotScript = Script(config.snapshot)
snapshotName = compileTemplate(snapshotScript, managedIndexMetaData, indexName).plus(snapshotNamePrefix)

val createSnapshotRequest = CreateSnapshotRequest()
.userMetadata(mapOf("snapshot_created" to "Open Distro for Elasticsearch Index Management"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fun randomTemplateScript(
): Script = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, source, params)

fun randomSnapshotActionConfig(repository: String = "repo", snapshot: String = "sp"): SnapshotActionConfig {
return SnapshotActionConfig(repository, Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, snapshot, emptyMap()), index = 0)
return SnapshotActionConfig(repository, snapshot, index = 0)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ import org.opensearch.indexmanagement.indexstatemanagement.settings.ManagedIndex
import org.opensearch.indexmanagement.indexstatemanagement.step.snapshot.AttemptSnapshotStep
import org.opensearch.indexmanagement.indexstatemanagement.step.snapshot.WaitForSnapshotStep
import org.opensearch.indexmanagement.waitFor
import org.opensearch.script.Script
import org.opensearch.script.ScriptType
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.Locale
Expand All @@ -49,8 +47,7 @@ class SnapshotActionIT : IndexStateManagementRestTestCase() {
val indexName = "${testIndexName}_index_basic"
val policyID = "${testIndexName}_policy_basic"
val repository = "repository"
val snapshotText = "snapshot"
val snapshot = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, snapshotText, emptyMap())
val snapshot = "snapshot"
val actionConfig = SnapshotActionConfig(repository, snapshot, 0)
val states = listOf(
State("Snapshot", listOf(actionConfig), listOf())
Expand Down Expand Up @@ -80,16 +77,15 @@ class SnapshotActionIT : IndexStateManagementRestTestCase() {
// Need to wait two cycles for wait for snapshot step
updateManagedIndexConfigStartTime(managedIndexConfig)

waitFor { assertSnapshotExists(repository, snapshotText) }
waitFor { assertSnapshotFinishedWithSuccess(repository, snapshotText) }
waitFor { assertSnapshotExists(repository, "snapshot") }
waitFor { assertSnapshotFinishedWithSuccess(repository, "snapshot") }
}

fun `test basic with templated snapshot name`() {
val indexName = "${testIndexName}_index_basic"
val policyID = "${testIndexName}_policy_basic"
val repository = "repository"
val snapshot = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, "{{ctx.index}}", emptyMap())
val actionConfig = SnapshotActionConfig(repository, snapshot, 0)
val actionConfig = SnapshotActionConfig(repository, "{{ctx.index}}", 0)
val states = listOf(
State("Snapshot", listOf(actionConfig), listOf())
)
Expand Down Expand Up @@ -126,8 +122,7 @@ class SnapshotActionIT : IndexStateManagementRestTestCase() {
val indexName = "${testIndexName}_index_basic"
val policyID = "${testIndexName}_policy_basic"
val repository = "repository"
val snapshot = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, "{{ctx.someField}}", emptyMap())
val actionConfig = SnapshotActionConfig(repository, snapshot, 0)
val actionConfig = SnapshotActionConfig(repository, "{{ctx.someField}}", 0)
val states = listOf(
State("Snapshot", listOf(actionConfig), listOf())
)
Expand Down Expand Up @@ -164,8 +159,7 @@ class SnapshotActionIT : IndexStateManagementRestTestCase() {
val indexName = "${testIndexName}_index_success"
val policyID = "${testIndexName}_policy_success"
val repository = "repository"
val snapshotText = "snapshot_success_test"
val snapshot = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, snapshotText, emptyMap())
val snapshot = "snapshot_success_test"
val actionConfig = SnapshotActionConfig(repository, snapshot, 0)
val states = listOf(
State("Snapshot", listOf(actionConfig), listOf())
Expand Down Expand Up @@ -202,20 +196,19 @@ class SnapshotActionIT : IndexStateManagementRestTestCase() {
// verify we set snapshotName in action properties
waitFor {
assert(
getExplainManagedIndexMetaData(indexName).actionMetaData?.actionProperties?.snapshotName?.contains(snapshotText) == true
getExplainManagedIndexMetaData(indexName).actionMetaData?.actionProperties?.snapshotName?.contains(snapshot) == true
)
}

waitFor { assertSnapshotExists(repository, snapshotText) }
waitFor { assertSnapshotFinishedWithSuccess(repository, snapshotText) }
waitFor { assertSnapshotExists(repository, snapshot) }
waitFor { assertSnapshotFinishedWithSuccess(repository, snapshot) }
}

fun `test failed wait for snapshot step`() {
val indexName = "${testIndexName}_index_failed"
val policyID = "${testIndexName}_policy_failed"
val repository = "repository"
val snapshotText = "snapshot_failed_test"
val snapshot = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, snapshotText, emptyMap())
val snapshot = "snapshot_failed_test"
val actionConfig = SnapshotActionConfig(repository, snapshot, 0)
val states = listOf(
State("Snapshot", listOf(actionConfig), listOf())
Expand Down Expand Up @@ -246,8 +239,8 @@ class SnapshotActionIT : IndexStateManagementRestTestCase() {
waitFor { assertEquals(AttemptSnapshotStep.getSuccessMessage(indexName), getExplainManagedIndexMetaData(indexName).info?.get("message")) }

// Confirm successful snapshot creation
waitFor { assertSnapshotExists(repository, snapshotText) }
waitFor { assertSnapshotFinishedWithSuccess(repository, snapshotText) }
waitFor { assertSnapshotExists(repository, snapshot) }
waitFor { assertSnapshotFinishedWithSuccess(repository, snapshot) }

// Delete the snapshot so wait for step will fail with missing snapshot exception
val snapshotName = getExplainManagedIndexMetaData(indexName).actionMetaData?.actionProperties?.snapshotName
Expand All @@ -269,8 +262,7 @@ class SnapshotActionIT : IndexStateManagementRestTestCase() {
val indexName = "${testIndexName}_index_blocked"
val policyID = "${testIndexName}_policy_basic"
val repository = "hello-world"
val snapshot = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, "snapshot", emptyMap())
val actionConfig = SnapshotActionConfig(repository, snapshot, 0)
val actionConfig = SnapshotActionConfig(repository, "snapshot", 0)
val states = listOf(
State("Snapshot", listOf(actionConfig), listOf())
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package org.opensearch.indexmanagement.indexstatemanagement.step
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doAnswer
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -50,7 +51,7 @@ class AttemptSnapshotStepTests : OpenSearchTestCase() {
@Before
fun settings() {
whenever(clusterService.clusterSettings).doReturn(ClusterSettings(Settings.EMPTY, setOf(SNAPSHOT_DENY_LIST)))
whenever(scriptService.compile(config.snapshot, TemplateScript.CONTEXT)).doReturn(MockTemplateScript.Factory("snapshot-name"))
whenever(scriptService.compile(any(), eq(TemplateScript.CONTEXT))).doReturn(MockTemplateScript.Factory("snapshot-name"))
}

fun `test snapshot response when block`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ import org.opensearch.indexmanagement.indexstatemanagement.model.action.Snapshot
import org.opensearch.indexmanagement.indexstatemanagement.model.managedindexmetadata.ActionMetaData
import org.opensearch.indexmanagement.indexstatemanagement.model.managedindexmetadata.ActionProperties
import org.opensearch.indexmanagement.indexstatemanagement.step.snapshot.WaitForSnapshotStep
import org.opensearch.script.Script
import org.opensearch.script.ScriptType
import org.opensearch.snapshots.Snapshot
import org.opensearch.snapshots.SnapshotId
import org.opensearch.test.OpenSearchTestCase
Expand All @@ -55,7 +53,7 @@ import org.opensearch.transport.RemoteTransportException
class WaitForSnapshotStepTests : OpenSearchTestCase() {

private val clusterService: ClusterService = mock()
val snapshot = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, "snapshot-name", emptyMap())
val snapshot = "snapshot-name"

fun `test snapshot missing snapshot name in action properties`() {
val exception = IllegalArgumentException("not used")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@
"type": "keyword"
},
"snapshot": {
"type": "object",
"enabled": false
"type": "keyword"
},
"include_global_state": {
"type": "boolean"
Expand Down

0 comments on commit b25a9fd

Please sign in to comment.