Search in sources :

Example 11 with BatchElement

use of com.palantir.atlasdb.autobatch.BatchElement in project atlasdb by palantir.

the class WriteBatchingTransactionServiceTest method repeatedProcessBatchOnlyMakesOneCallWithDuplicatesIfSuccessful.

@Test
public void repeatedProcessBatchOnlyMakesOneCallWithDuplicatesIfSuccessful() {
    KeyAlreadyExistsException exception = new KeyAlreadyExistsException("boo", ImmutableList.of(ENCODING_STRATEGY.encodeStartTimestampAsCell(5L)));
    doNothing().doThrow(exception).when(mockTransactionService).putUnlessExistsMultiple(anyMap());
    int numRequests = 100;
    List<BatchElement<WriteBatchingTransactionService.TimestampPair, Void>> batchedRequest = IntStream.range(0, numRequests).mapToObj(unused -> TestTransactionBatchElement.of(5L, 9L)).collect(Collectors.toList());
    WriteBatchingTransactionService.processBatch(mockTransactionService, batchedRequest);
    AtomicInteger successCount = new AtomicInteger();
    AtomicInteger failureCount = new AtomicInteger();
    getResultsTrackingOutcomes(batchedRequest, successCount, failureCount);
    verify(mockTransactionService, times(1)).putUnlessExistsMultiple(anyMap());
    verify(mockTransactionService, atLeastOnce()).getCellEncodingStrategy();
    // XXX Technically invalid, but valid for a mock transaction service.
    assertThat(successCount).hasValue(1);
    assertThat(failureCount).hasValue(numRequests - 1);
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Output(com.palantir.common.annotation.Output) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) InMemoryKeyValueService(com.palantir.atlasdb.keyvalue.impl.InMemoryKeyValueService) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) Mockito.doThrow(org.mockito.Mockito.doThrow) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ImmutableList(com.google.common.collect.ImmutableList) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Value(org.immutables.value.Value) After(org.junit.After) DisruptorAutobatcher(com.palantir.atlasdb.autobatch.DisruptorAutobatcher) Nullable(javax.annotation.Nullable) Before(org.junit.Before) ImmutableMap(com.google.common.collect.ImmutableMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) V1EncodingStrategy(com.palantir.atlasdb.transaction.encoding.V1EncodingStrategy) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) Mockito.mock(org.mockito.Mockito.mock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) Test(org.junit.Test)

Example 12 with BatchElement

use of com.palantir.atlasdb.autobatch.BatchElement in project atlasdb by palantir.

the class WriteBatchingTransactionServiceTest method throwsExceptionsCorrectlyOnDuplicatedElementsInBatch.

@Test
public void throwsExceptionsCorrectlyOnDuplicatedElementsInBatch() throws InterruptedException {
    EncodingTransactionService encodingTransactionService = SimpleTransactionService.createV1(new InMemoryKeyValueService(true));
    int numRequests = 100;
    List<BatchElement<WriteBatchingTransactionService.TimestampPair, Void>> batchedRequest = IntStream.range(0, numRequests).mapToObj(unused -> TestTransactionBatchElement.of(1L, 5L)).collect(Collectors.toList());
    WriteBatchingTransactionService.processBatch(encodingTransactionService, batchedRequest);
    AtomicInteger successCounter = new AtomicInteger();
    AtomicInteger exceptionCounter = new AtomicInteger();
    for (BatchElement<WriteBatchingTransactionService.TimestampPair, Void> batchElement : batchedRequest) {
        try {
            batchElement.result().get();
            successCounter.incrementAndGet();
        } catch (ExecutionException ex) {
            assertThat(ex).hasCauseInstanceOf(KeyAlreadyExistsException.class).satisfies(executionException -> {
                KeyAlreadyExistsException keyAlreadyExistsException = (KeyAlreadyExistsException) executionException.getCause();
                assertThat(keyAlreadyExistsException.getExistingKeys()).containsExactly(encodingTransactionService.getCellEncodingStrategy().encodeStartTimestampAsCell(1L));
            });
            exceptionCounter.incrementAndGet();
        }
    }
    // XXX Not something reasonable to assume in production (since the one successful call might actually
    // return fail while succeeding on the KVS), but acceptable for In Memory KVS.
    assertThat(successCounter).hasValue(1);
    assertThat(exceptionCounter).hasValue(numRequests - 1);
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Output(com.palantir.common.annotation.Output) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) InMemoryKeyValueService(com.palantir.atlasdb.keyvalue.impl.InMemoryKeyValueService) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) Mockito.doThrow(org.mockito.Mockito.doThrow) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ImmutableList(com.google.common.collect.ImmutableList) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Value(org.immutables.value.Value) After(org.junit.After) DisruptorAutobatcher(com.palantir.atlasdb.autobatch.DisruptorAutobatcher) Nullable(javax.annotation.Nullable) Before(org.junit.Before) ImmutableMap(com.google.common.collect.ImmutableMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) V1EncodingStrategy(com.palantir.atlasdb.transaction.encoding.V1EncodingStrategy) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) Mockito.mock(org.mockito.Mockito.mock) InMemoryKeyValueService(com.palantir.atlasdb.keyvalue.impl.InMemoryKeyValueService) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

BatchElement (com.palantir.atlasdb.autobatch.BatchElement)12 ImmutableList (com.google.common.collect.ImmutableList)11 List (java.util.List)11 Test (org.junit.Test)10 Namespace (com.palantir.atlasdb.timelock.api.Namespace)7 SafeIllegalStateException (com.palantir.logsafe.exceptions.SafeIllegalStateException)7 UUID (java.util.UUID)7 Collectors (java.util.stream.Collectors)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 IntStream (java.util.stream.IntStream)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)6 Mockito.mock (org.mockito.Mockito.mock)6 Mockito.times (org.mockito.Mockito.times)6 Mockito.verify (org.mockito.Mockito.verify)6 Mockito.when (org.mockito.Mockito.when)6 DisruptorAutobatcher (com.palantir.atlasdb.autobatch.DisruptorAutobatcher)5 ConjureStartTransactionsResponse (com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse)5 StartIdentifiedAtlasDbTransactionResponse (com.palantir.lock.v2.StartIdentifiedAtlasDbTransactionResponse)5 ArrayList (java.util.ArrayList)5