Search in sources :

Example 1 with OptimisticLockingRetryException

use of uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException in project microservice_framework by CJSCommonPlatform.

the class OptimisticLockingRetryExceptionTest method shouldCreateRuntimeException.

@Test
public void shouldCreateRuntimeException() throws Exception {
    final OptimisticLockingRetryException exception = new OptimisticLockingRetryException("Test");
    assertThat(exception, instanceOf(RuntimeException.class));
    assertThat(exception.getMessage(), is("Test"));
}
Also used : OptimisticLockingRetryException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException) Test(org.junit.Test)

Example 2 with OptimisticLockingRetryException

use of uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException in project microservice_framework by CJSCommonPlatform.

the class EventStreamManager method appendNonConsecutively.

/**
 * Store a stream of events without enforcing consecutive version ids. Reduces risk of throwing
 * optimistic lock error. To be use instead of the append method, when it's acceptable to
 * store events with non consecutive version ids
 *
 * @param streamId - id of the stream to append to
 * @param events   the stream of events to store
 * @return the current stream version
 * @throws EventStreamException if an event could not be appended
 */
@Transactional(dontRollbackOn = OptimisticLockingRetryException.class)
public long appendNonConsecutively(final UUID streamId, final Stream<JsonEnvelope> events) throws EventStreamException {
    final List<JsonEnvelope> envelopeList = events.collect(toList());
    long currentVersion = eventRepository.getStreamSize(streamId);
    validateEvents(streamId, envelopeList);
    for (final JsonEnvelope event : envelopeList) {
        boolean appendedSuccessfully = false;
        long retryCount = 0L;
        while (!appendedSuccessfully) {
            try {
                eventAppender.append(event, streamId, ++currentVersion);
                appendedSuccessfully = true;
            } catch (OptimisticLockingRetryException e) {
                retryCount++;
                if (retryCount > maxRetry) {
                    logger.warn("Failed to append to stream {} due to concurrency issues, returning to handler.", streamId);
                    throw e;
                }
                currentVersion = eventRepository.getStreamSize(streamId);
                logger.trace("Retrying appending to stream {}, with version {}", streamId, currentVersion + 1);
            }
        }
    }
    return currentVersion;
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) OptimisticLockingRetryException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException) Transactional(javax.transaction.Transactional)

Example 3 with OptimisticLockingRetryException

use of uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException in project microservice_framework by CJSCommonPlatform.

the class EventStreamManagerTest method shouldLogWarningAfterMaxNumberOfRetriesReached.

@Test
public void shouldLogWarningAfterMaxNumberOfRetriesReached() throws Exception {
    eventStreamManager.maxRetry = 2L;
    final long currentVersion = 6L;
    final long currentVersionAfterException1 = 11L;
    final long currentVersionAfterException2 = 12L;
    when(eventRepository.getStreamSize(STREAM_ID)).thenReturn(currentVersion).thenReturn(currentVersionAfterException1).thenReturn(currentVersionAfterException2);
    final JsonEnvelope event = envelope().with(metadataWithDefaults()).build();
    doThrow(OptimisticLockingRetryException.class).when(eventAppender).append(event, STREAM_ID, currentVersion + 1);
    doThrow(OptimisticLockingRetryException.class).when(eventAppender).append(event, STREAM_ID, currentVersionAfterException1 + 1);
    doThrow(OptimisticLockingRetryException.class).when(eventAppender).append(event, STREAM_ID, currentVersionAfterException2 + 1);
    try {
        eventStreamManager.appendNonConsecutively(STREAM_ID, Stream.of(event));
    } catch (OptimisticLockingRetryException e) {
    }
    verify(logger).warn("Failed to append to stream {} due to concurrency issues, returning to handler.", STREAM_ID);
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) OptimisticLockingRetryException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException) Test(org.junit.Test)

Example 4 with OptimisticLockingRetryException

use of uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException in project microservice_framework by CJSCommonPlatform.

the class RetryInterceptorTest method shouldRetryStraightAwayForThreeAttemptsThenWaitBeforeRetry.

@Test
public void shouldRetryStraightAwayForThreeAttemptsThenWaitBeforeRetry() throws Exception {
    final InterceptorContext currentContext = interceptorContextWithInput(envelope().with(metadataWithRandomUUID("nameABC")).build());
    final InterceptorContext nextInChain = interceptorContextWithInput(mock(JsonEnvelope.class));
    when(interceptorChain.processNext(currentContext)).thenThrow(new OptimisticLockingRetryException("Locking Error")).thenThrow(new OptimisticLockingRetryException("Locking Error")).thenThrow(new OptimisticLockingRetryException("Locking Error")).thenThrow(new OptimisticLockingRetryException("Locking Error")).thenReturn(nextInChain);
    retryInterceptor.maxRetry = "5";
    retryInterceptor.waitTime = "1000";
    retryInterceptor.immediateRetries = "3";
    final long start = currentTimeMillis();
    assertThat(retryInterceptor.process(currentContext, interceptorChain), is(nextInChain));
    final long end = currentTimeMillis();
    final long runTime = end - start;
    assertThat(runTime > 999, is(true));
    assertThat(runTime < 1999, is(true));
}
Also used : InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) OptimisticLockingRetryException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException) Test(org.junit.Test)

Example 5 with OptimisticLockingRetryException

use of uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException in project microservice_framework by CJSCommonPlatform.

the class RetryInterceptorTest method shouldThrowExceptionIfRetryMaxValueIsExceeded.

@Test
public void shouldThrowExceptionIfRetryMaxValueIsExceeded() throws Exception {
    final UUID streamId = UUID.randomUUID();
    final JsonEnvelope envelope = envelope().with(metadataWithRandomUUID("nameABC").withStreamId(streamId)).build();
    final InterceptorContext currentContext = interceptorContextWithInput(envelope);
    when(interceptorChain.processNext(currentContext)).thenThrow(new OptimisticLockingRetryException("Locking Error"));
    retryInterceptor.maxRetry = "1";
    retryInterceptor.waitTime = "500";
    retryInterceptor.immediateRetries = "0";
    expectedException.expect(OptimisticLockingRetryFailedException.class);
    expectedException.expectMessage("Retry count of 1 exceeded for command " + envelope.metadata().asJsonObject());
    retryInterceptor.process(currentContext, interceptorChain);
}
Also used : InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) MetadataBuilderFactory.metadataWithRandomUUID(uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID) OptimisticLockingRetryException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException) Test(org.junit.Test)

Aggregations

OptimisticLockingRetryException (uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException)7 Test (org.junit.Test)5 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)5 InterceptorContext (uk.gov.justice.services.core.interceptor.InterceptorContext)3 UUID (java.util.UUID)1 JsonObject (javax.json.JsonObject)1 Transactional (javax.transaction.Transactional)1 MetadataBuilderFactory.metadataWithRandomUUID (uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID)1