Search in sources :

Example 1 with BatchElement

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

the class GetSuspectedLeaderWithUuid method accept.

@Override
public void accept(List<BatchElement<UUID, Optional<ClientAwareLeaderPinger>>> batchElements) {
    Multimap<UUID, DisruptorFuture<Optional<ClientAwareLeaderPinger>>> uuidsToRequests = batchElements.stream().collect(ImmutableListMultimap.toImmutableListMultimap(BatchElement::argument, BatchElement::result));
    KeyedStream.of(uuidsToRequests.keySet()).filterKeys(cache::containsKey).map(cache::get).forEach((cachedUuid, pingable) -> completeRequest(uuidsToRequests.get(cachedUuid), Optional.of(clientAwareLeaders.get(pingable))));
    Set<UUID> uncachedUuids = uuidsToRequests.keySet().stream().filter(uuid -> !cache.containsKey(uuid)).collect(toSet());
    if (uncachedUuids.isEmpty()) {
        return;
    }
    PaxosResponsesWithRemote<LeaderPingerContext<BatchPingableLeader>, PaxosContainer<UUID>> results = PaxosQuorumChecker.collectUntil(ImmutableList.copyOf(executors.keySet()), pingable -> PaxosContainer.of(pingable.pinger().uuid()), executors, leaderPingResponseWait, state -> state.responses().values().stream().map(PaxosContainer::get).collect(toSet()).containsAll(uncachedUuids), PaxosConstants.CANCEL_REMAINING_CALLS);
    for (Map.Entry<LeaderPingerContext<BatchPingableLeader>, PaxosContainer<UUID>> resultEntries : results.responses().entrySet()) {
        LeaderPingerContext<BatchPingableLeader> pingable = resultEntries.getKey();
        UUID uuid = resultEntries.getValue().get();
        LeaderPingerContext<BatchPingableLeader> oldCachedEntry = cache.putIfAbsent(uuid, pingable);
        throwIfInvalidSetup(oldCachedEntry, pingable, uuid);
        completeRequest(uuidsToRequests.get(uuid), Optional.of(clientAwareLeaders.get(pingable)));
    }
    Set<UUID> missingUuids = Sets.difference(uncachedUuids, results.withoutRemotes().stream().map(PaxosContainer::get).collect(toSet()));
    missingUuids.forEach(missingUuid -> completeRequest(uuidsToRequests.get(missingUuid), Optional.empty()));
}
Also used : DisruptorFuture(com.palantir.atlasdb.autobatch.DisruptorAutobatcher.DisruptorFuture) SafeLoggerFactory(com.palantir.logsafe.logger.SafeLoggerFactory) Throwables(com.palantir.common.base.Throwables) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) PaxosContainer(com.palantir.atlasdb.timelock.paxos.PaxosQuorumCheckingCoalescingFunction.PaxosContainer) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) LeaderPingerContext(com.palantir.paxos.LeaderPingerContext) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) SafeLogger(com.palantir.logsafe.logger.SafeLogger) ImmutableList(com.google.common.collect.ImmutableList) Duration(java.time.Duration) Map(java.util.Map) PaxosQuorumChecker(com.palantir.paxos.PaxosQuorumChecker) Collectors.toSet(java.util.stream.Collectors.toSet) BiMap(com.google.common.collect.BiMap) KeyedStream(com.palantir.common.streams.KeyedStream) CheckedRejectionExecutorService(com.palantir.common.concurrent.CheckedRejectionExecutorService) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) Sets(com.google.common.collect.Sets) Consumer(java.util.function.Consumer) HashBiMap(com.google.common.collect.HashBiMap) List(java.util.List) PaxosResponsesWithRemote(com.palantir.paxos.PaxosResponsesWithRemote) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Optional(java.util.Optional) PaxosConstants(com.palantir.paxos.PaxosConstants) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) LeaderPingerContext(com.palantir.paxos.LeaderPingerContext) PaxosContainer(com.palantir.atlasdb.timelock.paxos.PaxosQuorumCheckingCoalescingFunction.PaxosContainer) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashBiMap(com.google.common.collect.HashBiMap) DisruptorFuture(com.palantir.atlasdb.autobatch.DisruptorAutobatcher.DisruptorFuture)

Example 2 with BatchElement

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

the class WriteBatchingTransactionServiceTest method repeatedProcessBatchOnlyMakesOneCallWithDuplicatesIfFailedAndKnownFailed.

@Test
public void repeatedProcessBatchOnlyMakesOneCallWithDuplicatesIfFailedAndKnownFailed() {
    KeyAlreadyExistsException exception = new KeyAlreadyExistsException("boo", ImmutableList.of(ENCODING_STRATEGY.encodeStartTimestampAsCell(5L)));
    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(0);
    assertThat(failureCount).hasValue(numRequests);
}
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 3 with BatchElement

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

the class WriteBatchingTransactionServiceTest method repeatedProcessBatchFiltersOutPartialSuccesses.

@Test
public void repeatedProcessBatchFiltersOutPartialSuccesses() {
    KeyAlreadyExistsException exception = new KeyAlreadyExistsException("boo", ImmutableList.of(ENCODING_STRATEGY.encodeStartTimestampAsCell(5L)), ImmutableList.of(ENCODING_STRATEGY.encodeStartTimestampAsCell(6L)));
    doThrow(exception).when(mockTransactionService).putUnlessExistsMultiple(anyMap());
    int numFailingRequests = 100;
    List<BatchElement<WriteBatchingTransactionService.TimestampPair, Void>> batchedRequest = IntStream.range(0, numFailingRequests).mapToObj(unused -> TestTransactionBatchElement.of(6L, 9L)).collect(Collectors.toList());
    batchedRequest.add(TestTransactionBatchElement.of(5L, 888L));
    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(numFailingRequests);
}
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 4 with BatchElement

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

the class MultiClientCommitTimestampGetterTest method updatesCacheWhileProcessingResponse.

@Test
public void updatesCacheWhileProcessingResponse() {
    Namespace client = Namespace.of("Kitty");
    List<BatchElement<NamespacedRequest, Long>> batchElements = IntStream.range(0, COMMIT_TS_LIMIT_PER_REQUEST * 2).mapToObj(ind -> batchElementForNamespace(client)).collect(toList());
    setupServiceAndAssertSanityOfResponse(batchElements);
    LockWatchCache cache = lockWatchCacheMap.get(client);
    verify(cache, times(2)).processCommitTimestampsUpdate(any(), any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DisruptorFuture(com.palantir.atlasdb.autobatch.DisruptorAutobatcher.DisruptorFuture) IntStream(java.util.stream.IntStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) LockWatchStateUpdate(com.palantir.lock.watch.LockWatchStateUpdate) HashMap(java.util.HashMap) GetCommitTimestampsRequest(com.palantir.atlasdb.timelock.api.GetCommitTimestampsRequest) Mockito.spy(org.mockito.Mockito.spy) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) ArrayList(java.util.ArrayList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) GetCommitTimestampsResponse(com.palantir.atlasdb.timelock.api.GetCommitTimestampsResponse) LockWatchCache(com.palantir.lock.watch.LockWatchCache) NamespacedRequest(com.palantir.lock.client.MultiClientCommitTimestampGetter.NamespacedRequest) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) KeyedStream(com.palantir.common.streams.KeyedStream) LockToken(com.palantir.lock.v2.LockToken) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) Namespace(com.palantir.atlasdb.timelock.api.Namespace) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) Mockito.never(org.mockito.Mockito.never) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) StreamEx(one.util.streamex.StreamEx) LockWatchCacheImpl(com.palantir.lock.watch.LockWatchCacheImpl) Mockito.mock(org.mockito.Mockito.mock) LockWatchCache(com.palantir.lock.watch.LockWatchCache) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) Namespace(com.palantir.atlasdb.timelock.api.Namespace) Test(org.junit.Test)

Example 5 with BatchElement

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

the class MultiClientCommitTimestampGetterTest method doesNotUpdateCacheIfClientNotServed.

@Test
public void doesNotUpdateCacheIfClientNotServed() {
    Namespace alpha = Namespace.of("alpha" + UUID.randomUUID());
    Namespace beta = Namespace.of("beta" + UUID.randomUUID());
    BatchElement<NamespacedRequest, Long> requestForAlpha = batchElementForNamespace(alpha);
    BatchElement<NamespacedRequest, Long> requestForBeta = batchElementForNamespace(beta);
    List<BatchElement<NamespacedRequest, Long>> allRequests = ImmutableList.of(requestForAlpha, requestForBeta);
    List<BatchElement<NamespacedRequest, Long>> alphaRequestList = ImmutableList.of(requestForAlpha);
    Map<Namespace, GetCommitTimestampsResponse> responseMap = getCommitTimestamps(alphaRequestList);
    when(timelockService.getCommitTimestamps(any())).thenReturn(responseMap).thenThrow(EXCEPTION);
    assertThatThrownBy(() -> consumer.accept(allRequests)).isEqualTo(EXCEPTION);
    // assert requests made by client alpha are served
    assertSanityOfResponse(alphaRequestList, ImmutableMap.of(alpha, ImmutableList.of(responseMap.get(alpha))));
    LockWatchCache alphaCache = lockWatchCacheMap.get(alpha);
    verify(alphaCache).processCommitTimestampsUpdate(any(), any());
    assertThat(requestForBeta.result().isDone()).as("No requests made by client - beta were successful").isFalse();
    LockWatchCache betaCache = lockWatchCacheMap.get(beta);
    verify(betaCache, never()).processCommitTimestampsUpdate(any(), any());
}
Also used : NamespacedRequest(com.palantir.lock.client.MultiClientCommitTimestampGetter.NamespacedRequest) LockWatchCache(com.palantir.lock.watch.LockWatchCache) BatchElement(com.palantir.atlasdb.autobatch.BatchElement) Namespace(com.palantir.atlasdb.timelock.api.Namespace) GetCommitTimestampsResponse(com.palantir.atlasdb.timelock.api.GetCommitTimestampsResponse) 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