Skip to content

Commit

Permalink
Fixes alias parsing logic
Browse files Browse the repository at this point in the history
Signed-off-by: Clay Downs <[email protected]>
  • Loading branch information
downsrob committed Apr 15, 2022
1 parent 218eb50 commit 3a510f1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ class ShrinkActionParser : ActionParser() {
TARGET_INDEX_TEMPLATE_FIELD -> targetIndexTemplate = Script.parse(xcp, Script.DEFAULT_TEMPLATE_LANG)
ALIASES_FIELD -> {
if (xcp.currentToken() != XContentParser.Token.VALUE_NULL) {
ensureExpectedToken(XContentParser.Token.START_ARRAY, xcp.currentToken(), xcp)
aliases = mutableListOf()
when (xcp.currentToken()) {
XContentParser.Token.START_OBJECT -> {
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) {
aliases.add(Alias.fromXContent(xcp))
}
}
else -> ensureExpectedToken(XContentParser.Token.START_ARRAY, xcp.currentToken(), xcp)
while (xcp.nextToken() != XContentParser.Token.END_ARRAY) {
ensureExpectedToken(XContentParser.Token.FIELD_NAME, xcp.nextToken(), xcp)
aliases.add(Alias.fromXContent(xcp))
ensureExpectedToken(XContentParser.Token.END_OBJECT, xcp.nextToken(), xcp)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ fun XContentParser.instant(): Instant? {
}

fun XContentBuilder.aliasesField(aliases: List<Alias>): XContentBuilder {
val builder = this.startObject(ShrinkAction.ALIASES_FIELD)
aliases.forEach { it.toXContent(builder, ToXContent.EMPTY_PARAMS) }
return builder.endObject()
val builder = this.startArray(ShrinkAction.ALIASES_FIELD)
aliases.forEach {
builder.startObject()
it.toXContent(builder, ToXContent.EMPTY_PARAMS)
builder.endObject()
}
return builder.endArray()
}

fun XContentBuilder.optionalTimeField(name: String, instant: Instant?): XContentBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
package org.opensearch.indexmanagement.indexstatemanagement.action

import org.apache.logging.log4j.LogManager
import org.opensearch.action.admin.indices.alias.Alias
import org.opensearch.cluster.metadata.IndexMetadata
import org.opensearch.common.settings.Settings
import org.opensearch.common.unit.ByteSizeValue
import org.opensearch.index.query.QueryBuilders
import org.opensearch.indexmanagement.indexstatemanagement.IndexStateManagementRestTestCase
import org.opensearch.indexmanagement.indexstatemanagement.model.Policy
import org.opensearch.indexmanagement.indexstatemanagement.model.State
Expand Down Expand Up @@ -37,7 +39,7 @@ class ShrinkActionIT : IndexStateManagementRestTestCase() {
maxShardSize = null,
percentageOfSourceShards = null,
targetIndexTemplate = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, "{{ctx.index}}$testIndexSuffix", mapOf()),
aliases = null,
aliases = listOf(Alias("test-alias1"), Alias("test-alias2").filter(QueryBuilders.termQuery("foo", "bar")).writeIndex(true)),
forceUnsafe = true,
index = 0
)
Expand Down Expand Up @@ -116,6 +118,8 @@ class ShrinkActionIT : IndexStateManagementRestTestCase() {
getExplainManagedIndexMetaData(indexName).info?.get("message")
)
assertEquals("Write block setting was not reset after successful shrink", "true", getIndexBlocksWriteSetting(indexName))
val aliases = getAlias(targetIndexName, "")
assertTrue("Aliases were not added to shrunken index", aliases.containsKey("test-alias1") && aliases.containsKey("test-alias2"))
}
}

Expand All @@ -130,7 +134,7 @@ class ShrinkActionIT : IndexStateManagementRestTestCase() {
maxShardSize = testMaxShardSize,
percentageOfSourceShards = null,
targetIndexTemplate = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, "{{ctx.index}}$testIndexSuffix", mapOf()),
aliases = null,
aliases = listOf(Alias("max-shard-alias")),
forceUnsafe = true,
index = 0
)
Expand Down Expand Up @@ -206,6 +210,8 @@ class ShrinkActionIT : IndexStateManagementRestTestCase() {
val indexSettings = getIndexSettings(indexName) as Map<String, Map<String, Map<String, Any?>>>
val writeBlock = indexSettings[indexName]!!["settings"]!![IndexMetadata.SETTING_BLOCKS_WRITE] as String?
assertNull("Write block setting was not reset after successful shrink", writeBlock)
val aliases = getAlias(targetIndexName, "")
assertTrue("Alias was not added to shrunken index", aliases.containsKey("max-shard-alias"))
}
}

Expand Down

0 comments on commit 3a510f1

Please sign in to comment.