Search in sources :

Example 1 with WriteSuccessSummary

use of org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.WriteSuccessSummary in project beam by apache.

the class FirestoreV1FnBatchWriteWithSummaryTest method nonRetryableWriteResultStopsAttempts.

@Test
public void nonRetryableWriteResultStopsAttempts() throws Exception {
    Write write0 = FirestoreProtoHelpers.newWrite(0);
    Write write1 = FirestoreProtoHelpers.newWrite(1).toBuilder().setCurrentDocument(Precondition.newBuilder().setExists(false).build()).build();
    BatchWriteRequest expectedRequest1 = BatchWriteRequest.newBuilder().setDatabase("projects/testing-project/databases/(default)").addWrites(write0).addWrites(write1).build();
    BatchWriteResponse response1 = BatchWriteResponse.newBuilder().addStatus(STATUS_OK).addWriteResults(WriteResult.newBuilder().setUpdateTime(Timestamp.newBuilder().setSeconds(1).build()).build()).addStatus(statusForCode(Code.ALREADY_EXISTS)).addWriteResults(WriteResult.newBuilder().build()).build();
    RpcQosOptions options = rpcQosOptions.toBuilder().withMaxAttempts(1).withBatchMaxCount(2).build();
    when(processContext.element()).thenReturn(write0, write1).thenThrow(new IllegalStateException("too many calls"));
    when(rpcQos.newWriteAttempt(any())).thenReturn(attempt);
    when(attempt.awaitSafeToProceed(any())).thenReturn(true);
    when(attempt.<Write, Element<Write>>newFlushBuffer(any())).thenReturn(newFlushBuffer(options)).thenReturn(newFlushBuffer(options)).thenThrow(new IllegalStateException("too many attempt#newFlushBuffer calls"));
    when(attempt.isCodeRetryable(Code.ALREADY_EXISTS)).thenReturn(false);
    ArgumentCaptor<BatchWriteRequest> requestCaptor1 = ArgumentCaptor.forClass(BatchWriteRequest.class);
    when(callable.call(requestCaptor1.capture())).thenReturn(response1);
    BaseBatchWriteFn<WriteSuccessSummary> fn = new BatchWriteFnWithSummary(clock, ff, options, CounterFactory.DEFAULT);
    fn.setup();
    fn.startBundle(startBundleContext);
    // write0
    fn.processElement(processContext, window);
    try {
        // write1
        fn.processElement(processContext, window);
        fail("expected an exception when trying to apply a write with a failed precondition");
    } catch (FailedWritesException e) {
        List<WriteFailure> writeFailures = e.getWriteFailures();
        assertEquals(1, writeFailures.size());
        WriteFailure failure = writeFailures.get(0);
        assertEquals(Code.ALREADY_EXISTS.getNumber(), failure.getStatus().getCode());
        assertEquals(write1, failure.getWrite());
        assertEquals(WriteResult.getDefaultInstance(), failure.getWriteResult());
    }
    assertEquals(expectedRequest1, requestCaptor1.getValue());
    List<WriteElement> actualWrites = new ArrayList<>(fn.writes);
    Instant flush1Attempt1Begin = Instant.ofEpochMilli(1);
    Instant flush1Attempt1RpcStart = Instant.ofEpochMilli(2);
    Instant flush1Attempt1RpcEnd = Instant.ofEpochMilli(3);
    assertTrue(actualWrites.isEmpty());
    fn.finishBundle(finishBundleContext);
    verify(attempt, times(1)).newFlushBuffer(flush1Attempt1Begin);
    verify(attempt, times(1)).recordRequestStart(flush1Attempt1RpcStart, 2);
    verify(attempt, times(1)).recordWriteCounts(flush1Attempt1RpcEnd, 1, 1);
    verify(attempt, never()).completeSuccess();
    verify(callable, times(1)).call(any());
    verifyNoMoreInteractions(callable);
}
Also used : Write(com.google.firestore.v1.Write) FailedWritesException(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.FailedWritesException) Element(org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcWriteAttempt.Element) WriteElement(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement) Instant(org.joda.time.Instant) BatchWriteResponse(com.google.firestore.v1.BatchWriteResponse) WriteFailure(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.WriteFailure) ArrayList(java.util.ArrayList) WriteElement(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement) BatchWriteRequest(com.google.firestore.v1.BatchWriteRequest) ArrayList(java.util.ArrayList) List(java.util.List) BatchWriteFnWithSummary(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.BatchWriteFnWithSummary) WriteSuccessSummary(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.WriteSuccessSummary) Test(org.junit.Test)

Aggregations

BatchWriteRequest (com.google.firestore.v1.BatchWriteRequest)1 BatchWriteResponse (com.google.firestore.v1.BatchWriteResponse)1 Write (com.google.firestore.v1.Write)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FailedWritesException (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.FailedWritesException)1 WriteFailure (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.WriteFailure)1 WriteSuccessSummary (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.WriteSuccessSummary)1 BatchWriteFnWithSummary (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.BatchWriteFnWithSummary)1 WriteElement (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement)1 Element (org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcWriteAttempt.Element)1 Instant (org.joda.time.Instant)1 Test (org.junit.Test)1