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