Skip to content

Commit

Permalink
Handle NPE in isRollupIndex (#855)
Browse files Browse the repository at this point in the history
* Handle NPE in isRollupIndex

`metadata.index()` can return `null`, so handle that case by returning
`false`.

Signed-off-by: Bryce Lampe <[email protected]>

* unit test

Signed-off-by: Bryce Lampe <[email protected]>

---------

Signed-off-by: Bryce Lampe <[email protected]>
Co-authored-by: bowenlan-amzn <[email protected]>
  • Loading branch information
blampe and bowenlan-amzn authored Jul 22, 2023
1 parent 6610f82 commit 05bc0d5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ const val DATE_FIELD_EPOCH_MILLIS_FORMAT = "epoch_millis"

@Suppress("ReturnCount")
fun isRollupIndex(index: String, clusterState: ClusterState): Boolean {
if (RollupSettings.ROLLUP_INDEX.get(clusterState.metadata.index(index).settings)) {
val idx = clusterState.metadata.index(index)
if (idx == null) {
return false
} else if (RollupSettings.ROLLUP_INDEX.get(idx.settings)) {
return true
} else if (LegacyOpenDistroRollupSettings.ROLLUP_INDEX.get(clusterState.metadata.index(index).settings)) {
} else if (LegacyOpenDistroRollupSettings.ROLLUP_INDEX.get(idx.settings)) {
return true
}
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

package org.opensearch.indexmanagement.rollup.util

import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import org.opensearch.cluster.ClusterState
import org.opensearch.cluster.metadata.Metadata
import org.opensearch.index.query.BoolQueryBuilder
import org.opensearch.index.query.ConstantScoreQueryBuilder
import org.opensearch.index.query.DisMaxQueryBuilder
Expand Down Expand Up @@ -230,4 +235,16 @@ class RollupUtilsTests : OpenSearchTestCase() {
assertEquals("Rewritten aggregation builder is not the correct type", aggBuilder.type, rewrittenAgg.type)
}
}

fun `test isRollupIndex`() {
val indexName = "missing index"
val clusterState: ClusterState = mock()
val metadata: Metadata = mock()

whenever(clusterState.metadata).doReturn(metadata)
whenever(metadata.index(indexName)).doReturn(null)

val actual = isRollupIndex(indexName, clusterState)
assertFalse(actual)
}
}

0 comments on commit 05bc0d5

Please sign in to comment.