|
6 | 6 | package org.opensearch.indexmanagement.snapshotmanagement.engine |
7 | 7 |
|
8 | 8 | import com.nhaarman.mockitokotlin2.argumentCaptor |
| 9 | +import com.nhaarman.mockitokotlin2.doAnswer |
| 10 | +import com.nhaarman.mockitokotlin2.doReturn |
| 11 | +import com.nhaarman.mockitokotlin2.mock |
9 | 12 | import com.nhaarman.mockitokotlin2.spy |
10 | 13 | import com.nhaarman.mockitokotlin2.times |
11 | 14 | import com.nhaarman.mockitokotlin2.verify |
12 | 15 | import kotlinx.coroutines.runBlocking |
| 16 | +import org.apache.logging.log4j.LogManager |
| 17 | +import org.apache.logging.log4j.Logger |
| 18 | +import org.opensearch.OpenSearchException |
| 19 | +import org.opensearch.action.bulk.BackoffPolicy |
13 | 20 | import org.opensearch.common.unit.TimeValue |
| 21 | +import org.opensearch.core.index.shard.ShardId |
| 22 | +import org.opensearch.index.engine.VersionConflictEngineException |
14 | 23 | import org.opensearch.indexmanagement.MocksTestCase |
| 24 | +import org.opensearch.indexmanagement.opensearchapi.retry |
| 25 | +import org.opensearch.indexmanagement.snapshotmanagement.SnapshotManagementException |
15 | 26 | import org.opensearch.indexmanagement.snapshotmanagement.engine.states.SMState |
16 | 27 | import org.opensearch.indexmanagement.snapshotmanagement.engine.states.creationTransitions |
17 | 28 | import org.opensearch.indexmanagement.snapshotmanagement.engine.states.deletionTransitions |
@@ -230,4 +241,70 @@ open class SMStateMachineTests : MocksTestCase() { |
230 | 241 | assertEquals(1, firstValue.policyPrimaryTerm) |
231 | 242 | } |
232 | 243 | } |
| 244 | + |
| 245 | + fun `test updateMetadata handles VersionConflictEngineException gracefully`() = runBlocking { |
| 246 | + val initialMetadata = randomSMMetadata( |
| 247 | + policySeqNo = 0, |
| 248 | + policyPrimaryTerm = 0, |
| 249 | + ) |
| 250 | + val smPolicy = randomSMPolicy( |
| 251 | + seqNo = 1, |
| 252 | + primaryTerm = 1, |
| 253 | + ) |
| 254 | + val updatedMetadata = randomSMMetadata( |
| 255 | + policySeqNo = 1, |
| 256 | + policyPrimaryTerm = 1, |
| 257 | + ) |
| 258 | + |
| 259 | + val mockBackoffPolicy = mock<BackoffPolicy>() |
| 260 | + val stateMachineSpy = spy(SMStateMachine(client, smPolicy, initialMetadata, settings, threadPool, indicesManager)) |
| 261 | + doReturn(mockBackoffPolicy).`when`(stateMachineSpy).updateMetaDataRetryPolicy |
| 262 | + val logger: Logger = LogManager.getLogger(javaClass) |
| 263 | + |
| 264 | + doAnswer { throw VersionConflictEngineException(ShardId("index", "_na_", 1), "test", "message") } |
| 265 | + .`when`(mockBackoffPolicy) |
| 266 | + .retry(logger) { true } |
| 267 | + |
| 268 | + // Verify VersionConflictEngineException is handled gracefully |
| 269 | + try { |
| 270 | + stateMachineSpy.updateMetadata(updatedMetadata) |
| 271 | + } catch (e: Exception) { |
| 272 | + fail("VersionConflictEngineException should be handled without throwing: ${e.message}") |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + fun `test updateMetadata throws SnapshotManagementException for other exceptions`() = runBlocking { |
| 277 | + val initialMetadata = randomSMMetadata( |
| 278 | + policySeqNo = 0, |
| 279 | + policyPrimaryTerm = 0, |
| 280 | + ) |
| 281 | + val smPolicy = randomSMPolicy( |
| 282 | + seqNo = 1, |
| 283 | + primaryTerm = 1, |
| 284 | + ) |
| 285 | + val updatedMetadata = randomSMMetadata( |
| 286 | + policySeqNo = 1, |
| 287 | + policyPrimaryTerm = 1, |
| 288 | + ) |
| 289 | + |
| 290 | + val mockBackoffPolicy = mock<BackoffPolicy>() |
| 291 | + val stateMachineSpy = spy(SMStateMachine(client, smPolicy, initialMetadata, settings, threadPool, indicesManager)) |
| 292 | + doReturn(mockBackoffPolicy).`when`(stateMachineSpy).updateMetaDataRetryPolicy |
| 293 | + val logger: Logger = LogManager.getLogger(javaClass) |
| 294 | + |
| 295 | + val openSearchException = OpenSearchException("Test exception") |
| 296 | + doAnswer { throw openSearchException } |
| 297 | + .`when`(mockBackoffPolicy) |
| 298 | + .retry(logger) { true } |
| 299 | + |
| 300 | + // Verify OpenSearchException is wrapped in SnapshotManagementException |
| 301 | + val thrownException = assertThrows(SnapshotManagementException::class.java) { |
| 302 | + runBlocking { |
| 303 | + stateMachineSpy.updateMetadata(updatedMetadata) |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + // Verify exception type and cause |
| 308 | + assertTrue(thrownException.cause is OpenSearchException) |
| 309 | + } |
233 | 310 | } |
0 commit comments