Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class RolloverAction(
val minAge: TimeValue?,
val minPrimaryShardSize: ByteSizeValue?,
val copyAlias: Boolean = false,
val preventEmptyRollover: Boolean = false,
index: Int,
) : Action(name, index) {
init {
Expand Down Expand Up @@ -48,6 +49,7 @@ class RolloverAction(
if (minAge != null) builder.field(MIN_INDEX_AGE_FIELD, minAge.stringRep)
if (minPrimaryShardSize != null) builder.field(MIN_PRIMARY_SHARD_SIZE_FIELD, minPrimaryShardSize.stringRep)
builder.field(COPY_ALIAS_FIELD, copyAlias)
if (preventEmptyRollover) builder.field(PREVENT_EMPTY_ROLLOVER_FIELD, preventEmptyRollover)
builder.endObject()
}

Expand All @@ -57,6 +59,7 @@ class RolloverAction(
out.writeOptionalTimeValue(minAge)
out.writeOptionalWriteable(minPrimaryShardSize)
out.writeBoolean(copyAlias)
out.writeBoolean(preventEmptyRollover)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not have a version check here for backward compatibility?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh right, thanks for catching this, let me make the change

out.writeInt(actionIndex)
}

Expand All @@ -67,5 +70,6 @@ class RolloverAction(
const val MIN_INDEX_AGE_FIELD = "min_index_age"
const val MIN_PRIMARY_SHARD_SIZE_FIELD = "min_primary_shard_size"
const val COPY_ALIAS_FIELD = "copy_alias"
const val PREVENT_EMPTY_ROLLOVER_FIELD = "prevent_empty_rollover"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class RolloverActionParser : ActionParser() {
val minAge = sin.readOptionalTimeValue()
val minPrimaryShardSize = sin.readOptionalWriteable(::ByteSizeValue)
val copyAlias = sin.readBoolean()
val preventEmptyRollover = sin.readBoolean()
val index = sin.readInt()

return RolloverAction(minSize, minDocs, minAge, minPrimaryShardSize, copyAlias, index)
return RolloverAction(minSize, minDocs, minAge, minPrimaryShardSize, copyAlias, preventEmptyRollover, index)
}

override fun fromXContent(xcp: XContentParser, index: Int): Action {
Expand All @@ -31,6 +32,7 @@ class RolloverActionParser : ActionParser() {
var minAge: TimeValue? = null
var minPrimaryShardSize: ByteSizeValue? = null
var copyAlias = false
var preventEmptyRollover = false

ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp)
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) {
Expand All @@ -54,11 +56,13 @@ class RolloverActionParser : ActionParser() {

RolloverAction.COPY_ALIAS_FIELD -> copyAlias = xcp.booleanValue()

RolloverAction.PREVENT_EMPTY_ROLLOVER_FIELD -> preventEmptyRollover = xcp.booleanValue()

else -> throw IllegalArgumentException("Invalid field: [$fieldName] found in RolloverAction.")
}
}

return RolloverAction(minSize, minDocs, minAge, minPrimaryShardSize, copyAlias, index)
return RolloverAction(minSize, minDocs, minAge, minPrimaryShardSize, copyAlias, preventEmptyRollover, index)
}

override fun getActionType(): String = RolloverAction.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ class AttemptRolloverStep(private val action: RolloverAction) : Step(name) {
// If statsResponse is null we already updated failed info from getIndexStatsOrUpdateInfo and can return early
statsResponse ?: return this

// Check for empty index if prevent_empty_rollover is enabled
if (action.preventEmptyRollover) {
val numDocs = statsResponse.primaries.docs?.count ?: 0
if (numDocs == 0L) {
stepStatus = StepStatus.CONDITION_NOT_MET
info = mapOf(
"message" to getPreventedEmptyRolloverMessage(indexName),
"conditions" to mapOf(
"prevent_empty_rollover" to mapOf(
"condition" to "index must have at least 1 document",
"current" to 0,
),
),
)
logger.info("$indexName rollover prevented: index is empty (0 documents)")
return this
}
}

val indexCreationDate = clusterService.state().metadata().index(indexName).creationDate
val indexAgeTimeValue =
if (indexCreationDate == -1L) {
Expand Down Expand Up @@ -426,5 +445,8 @@ class AttemptRolloverStep(private val action: RolloverAction) : Step(name) {

fun getCopyAliasRolledOverIndexNotFoundMessage(index: String?) =
"Successfully rolled over [index=$index] but ISM cannot find rolled over index from metadata to copy aliases to, please manually copy"

fun getPreventedEmptyRolloverMessage(index: String) =
"Rollover prevented: index is empty (0 documents) [index=$index]"
}
}
5 changes: 4 additions & 1 deletion src/main/resources/mappings/opendistro-ism-config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"_meta" : {
"schema_version": 24
"schema_version": 25
},
"dynamic": "strict",
"properties": {
Expand Down Expand Up @@ -245,6 +245,9 @@
},
"copy_alias": {
"type": "boolean"
},
"prevent_empty_rollover": {
"type": "boolean"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import javax.management.remote.JMXConnectorFactory
import javax.management.remote.JMXServiceURL

abstract class IndexManagementRestTestCase : ODFERestTestCase() {
val configSchemaVersion = 24
val configSchemaVersion = 25
val historySchemaVersion = 7

// Having issues with tests leaking into other tests and mappings being incorrect and they are not caught by any pending task wait check as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ fun randomRolloverActionConfig(
minDocs = minDocs,
minAge = minAge,
minPrimaryShardSize = minPrimaryShardSize,
preventEmptyRollover = false,
index = 0,
)

Expand Down
Loading
Loading