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"));
}
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;
}
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);
}
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));
}
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);
}
Aggregations