Search in sources :

Example 11 with Write

use of com.google.firestore.v1.Write in project beam by apache.

the class DatastoreV1Test method testWriteDisplayData.

@Test
public void testWriteDisplayData() {
    Write write = DatastoreIO.v1().write().withProjectId(PROJECT_ID);
    DisplayData displayData = DisplayData.from(write);
    assertThat(displayData, hasDisplayItem("projectId", PROJECT_ID));
}
Also used : Write(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Write) DisplayData(org.apache.beam.sdk.transforms.display.DisplayData) Test(org.junit.Test)

Example 12 with Write

use of com.google.firestore.v1.Write in project beam by apache.

the class DatastoreV1Test method testBuildWrite.

/**
 * Test building a Write using builder methods.
 */
@Test
public void testBuildWrite() throws Exception {
    DatastoreV1.Write write = DatastoreIO.v1().write().withProjectId(PROJECT_ID);
    assertEquals(PROJECT_ID, write.getProjectId());
}
Also used : Write(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Write) Test(org.junit.Test)

Example 13 with Write

use of com.google.firestore.v1.Write in project beam by apache.

the class FirestoreV1IT method batchWrite_partialFailureOutputsToDeadLetterQueue.

@Test
public void batchWrite_partialFailureOutputsToDeadLetterQueue() throws InterruptedException, ExecutionException, TimeoutException {
    String collectionId = "a";
    String docId = helper.docId();
    Write validWrite = Write.newBuilder().setUpdate(Document.newBuilder().setName(docPath(helper.getBaseDocumentPath(), collectionId, docId)).putFields("foo", Value.newBuilder().setStringValue(docId).build())).build();
    long millis = System.currentTimeMillis();
    Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000).setNanos((int) ((millis % 1000) * 1000000)).build();
    String docId2 = helper.docId();
    helper.getBaseDocument().collection(collectionId).document(docId2).create(ImmutableMap.of("foo", "baz")).get(10, TimeUnit.SECONDS);
    // this will fail because we're setting a updateTime precondition to before it was created
    Write conditionalUpdate = Write.newBuilder().setUpdate(Document.newBuilder().setName(docPath(helper.getBaseDocumentPath(), collectionId, docId2)).putFields("foo", Value.newBuilder().setStringValue(docId).build())).setCurrentDocument(Precondition.newBuilder().setUpdateTime(timestamp)).build();
    List<Write> writes = newArrayList(validWrite, conditionalUpdate);
    RpcQosOptions options = BaseFirestoreIT.rpcQosOptions.toBuilder().withBatchMaxCount(2).build();
    PCollection<WriteFailure> writeFailurePCollection = testPipeline.apply(Create.of(writes)).apply(FirestoreIO.v1().write().batchWrite().withDeadLetterQueue().withRpcQosOptions(options).build());
    PAssert.that(writeFailurePCollection).satisfies((writeFailures) -> {
        Iterator<WriteFailure> iterator = writeFailures.iterator();
        assertTrue(iterator.hasNext());
        WriteFailure failure = iterator.next();
        assertEquals(Code.FAILED_PRECONDITION, Code.forNumber(failure.getStatus().getCode()));
        assertNotNull(failure.getWriteResult());
        assertFalse(failure.getWriteResult().hasUpdateTime());
        assertEquals(conditionalUpdate, failure.getWrite());
        assertFalse(iterator.hasNext());
        return null;
    });
    testPipeline.run(this.options);
    ApiFuture<QuerySnapshot> actualDocsQuery = helper.getBaseDocument().collection(collectionId).orderBy("__name__").get();
    QuerySnapshot querySnapshot = actualDocsQuery.get(10, TimeUnit.SECONDS);
    List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();
    List<KV<String, String>> actualDocumentIds = documents.stream().map(doc -> KV.of(doc.getId(), doc.getString("foo"))).collect(Collectors.toList());
    List<KV<String, String>> expected = newArrayList(KV.of(docId, docId), KV.of(docId2, "baz"));
    assertEquals(expected, actualDocumentIds);
}
Also used : Write(com.google.firestore.v1.Write) KV(org.apache.beam.sdk.values.KV) RunQueryRequest(com.google.firestore.v1.RunQueryRequest) FieldReference(com.google.firestore.v1.StructuredQuery.FieldReference) Write(com.google.firestore.v1.Write) Direction(com.google.firestore.v1.StructuredQuery.Direction) TimeoutException(java.util.concurrent.TimeoutException) Operator(com.google.firestore.v1.StructuredQuery.FieldFilter.Operator) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Timestamp(com.google.protobuf.Timestamp) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) BatchGetDocumentsRequest(com.google.firestore.v1.BatchGetDocumentsRequest) PTransform(org.apache.beam.sdk.transforms.PTransform) Lists.newArrayList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists.newArrayList) Value(com.google.firestore.v1.Value) Create(org.apache.beam.sdk.transforms.Create) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) WriteFailure(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.WriteFailure) ListCollectionIdsRequest(com.google.firestore.v1.ListCollectionIdsRequest) Code(com.google.rpc.Code) DoFn(org.apache.beam.sdk.transforms.DoFn) Precondition(com.google.firestore.v1.Precondition) StructuredQuery(com.google.firestore.v1.StructuredQuery) Iterator(java.util.Iterator) PAssert(org.apache.beam.sdk.testing.PAssert) Assert.assertNotNull(org.junit.Assert.assertNotNull) Filter(com.google.firestore.v1.StructuredQuery.Filter) Document(com.google.firestore.v1.Document) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PCollection(org.apache.beam.sdk.values.PCollection) Collectors(java.util.stream.Collectors) FirestoreIO(org.apache.beam.sdk.io.gcp.firestore.FirestoreIO) FieldFilter(com.google.firestore.v1.StructuredQuery.FieldFilter) ApiFuture(com.google.api.core.ApiFuture) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ListDocumentsRequest(com.google.firestore.v1.ListDocumentsRequest) List(java.util.List) ParDo(org.apache.beam.sdk.transforms.ParDo) Assert.assertFalse(org.junit.Assert.assertFalse) RpcQosOptions(org.apache.beam.sdk.io.gcp.firestore.RpcQosOptions) Order(com.google.firestore.v1.StructuredQuery.Order) CollectionSelector(com.google.firestore.v1.StructuredQuery.CollectionSelector) Assert.assertEquals(org.junit.Assert.assertEquals) PartitionQueryRequest(com.google.firestore.v1.PartitionQueryRequest) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) WriteFailure(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.WriteFailure) KV(org.apache.beam.sdk.values.KV) Timestamp(com.google.protobuf.Timestamp) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) RpcQosOptions(org.apache.beam.sdk.io.gcp.firestore.RpcQosOptions) Test(org.junit.Test)

Example 14 with Write

use of com.google.firestore.v1.Write in project beam by apache.

the class BaseFirestoreV1WriteFnTest method queuedWritesMaintainPriorityIfNotFlushed.

@Test
public final void queuedWritesMaintainPriorityIfNotFlushed() throws Exception {
    RpcQosOptions options = rpcQosOptions.toBuilder().withMaxAttempts(1).build();
    Write write0 = newWrite(0);
    Write write1 = newWrite(1);
    Write write2 = newWrite(2);
    Write write3 = newWrite(3);
    Write write4 = newWrite(4);
    Instant write4Start = Instant.ofEpochMilli(4);
    when(processContext.element()).thenReturn(write0, write1, write2, write3, write4).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())).thenAnswer(invocation -> newFlushBuffer(options));
    FnT fn = getFn(clock, ff, options, CounterFactory.DEFAULT, DistributionFactory.DEFAULT);
    fn.setup();
    fn.startBundle(startBundleContext);
    // write0
    fn.processElement(processContext, window);
    // write1
    fn.processElement(processContext, window);
    // write2
    fn.processElement(processContext, window);
    // write3
    fn.processElement(processContext, window);
    // write4
    fn.processElement(processContext, window);
    List<WriteElement> expectedWrites = newArrayList(new WriteElement(0, write0, window), new WriteElement(1, write1, window), new WriteElement(2, write2, window), new WriteElement(3, write3, window), new WriteElement(4, write4, window));
    List<WriteElement> actualWrites = new ArrayList<>(fn.writes);
    assertEquals(expectedWrites, actualWrites);
    assertEquals(5, fn.queueNextEntryPriority);
    verify(attempt, times(1)).newFlushBuffer(write4Start);
    verifyNoMoreInteractions(callable);
}
Also used : Write(com.google.firestore.v1.Write) FirestoreProtoHelpers.newWrite(org.apache.beam.sdk.io.gcp.firestore.FirestoreProtoHelpers.newWrite) Instant(org.joda.time.Instant) Element(org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcWriteAttempt.Element) WriteElement(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement) WriteElement(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement) Lists.newArrayList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 15 with Write

use of com.google.firestore.v1.Write in project beam by apache.

the class BaseFirestoreV1WriteFnTest method endToEnd_awaitSafeToProceed_falseIsTerminalForAttempt.

@Test
public final void endToEnd_awaitSafeToProceed_falseIsTerminalForAttempt() throws Exception {
    RpcQosOptions options = rpcQosOptions.toBuilder().withBatchMaxCount(2).build();
    Instant rpc1Start = Instant.ofEpochMilli(3);
    Instant rpc1End = Instant.ofEpochMilli(4);
    ArgumentCaptor<BatchWriteRequest> requestCaptor = ArgumentCaptor.forClass(BatchWriteRequest.class);
    Write write = newWrite();
    BatchWriteRequest expectedRequest = BatchWriteRequest.newBuilder().setDatabase("projects/testing-project/databases/(default)").addWrites(write).build();
    BatchWriteResponse response = BatchWriteResponse.newBuilder().addStatus(STATUS_OK).build();
    when(processContext.element()).thenReturn(write);
    // process element attempt 1
    when(attempt.awaitSafeToProceed(any())).thenReturn(false).thenThrow(new IllegalStateException("too many attempt1#awaitSafeToProceed"));
    // process element attempt 2
    when(attempt2.awaitSafeToProceed(any())).thenReturn(true).thenThrow(new IllegalStateException("too many attempt2#awaitSafeToProceed"));
    when(attempt2.<Write, Element<Write>>newFlushBuffer(any())).thenAnswer(invocation -> newFlushBuffer(options));
    // finish bundle attempt
    RpcQos.RpcWriteAttempt finishBundleAttempt = mock(RpcWriteAttempt.class);
    when(finishBundleAttempt.awaitSafeToProceed(any())).thenReturn(true, true).thenThrow(new IllegalStateException("too many finishBundleAttempt#awaitSafeToProceed"));
    when(finishBundleAttempt.<Write, Element<Write>>newFlushBuffer(any())).thenAnswer(invocation -> newFlushBuffer(options));
    when(rpcQos.newWriteAttempt(any())).thenReturn(attempt, attempt2, finishBundleAttempt);
    when(callable.call(requestCaptor.capture())).thenReturn(response);
    FnT fn = getFn(clock, ff, options, CounterFactory.DEFAULT, DistributionFactory.DEFAULT);
    runFunction(fn);
    assertEquals(expectedRequest, requestCaptor.getValue());
    verify(attempt, times(1)).awaitSafeToProceed(any());
    verifyNoMoreInteractions(attempt);
    verify(attempt2, times(1)).awaitSafeToProceed(any());
    verify(attempt2, times(1)).newFlushBuffer(any());
    verifyNoMoreInteractions(attempt2);
    verify(finishBundleAttempt, times(1)).recordRequestStart(rpc1Start, 1);
    verify(finishBundleAttempt, times(1)).recordWriteCounts(rpc1End, 1, 0);
    verify(finishBundleAttempt, times(1)).completeSuccess();
    verify(finishBundleAttempt, never()).checkCanRetry(any(), any());
}
Also used : Write(com.google.firestore.v1.Write) FirestoreProtoHelpers.newWrite(org.apache.beam.sdk.io.gcp.firestore.FirestoreProtoHelpers.newWrite) Instant(org.joda.time.Instant) Element(org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcWriteAttempt.Element) WriteElement(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement) BatchWriteRequest(com.google.firestore.v1.BatchWriteRequest) BatchWriteResponse(com.google.firestore.v1.BatchWriteResponse) RpcWriteAttempt(org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcWriteAttempt) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)19 Write (com.google.firestore.v1.Write)16 WriteElement (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement)11 Element (org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcWriteAttempt.Element)11 FirestoreProtoHelpers.newWrite (org.apache.beam.sdk.io.gcp.firestore.FirestoreProtoHelpers.newWrite)9 Instant (org.joda.time.Instant)9 BatchWriteRequest (com.google.firestore.v1.BatchWriteRequest)8 BatchWriteResponse (com.google.firestore.v1.BatchWriteResponse)8 ArrayList (java.util.ArrayList)5 Write (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Write)5 WriteFailure (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.WriteFailure)5 ApiException (com.google.api.gax.rpc.ApiException)3 Value (com.google.firestore.v1.Value)3 List (java.util.List)3 Lists.newArrayList (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists.newArrayList)3 ArrayValue (com.google.firestore.v1.ArrayValue)2 Document (com.google.firestore.v1.Document)2 ByteString (com.google.protobuf.ByteString)2 Code (com.google.rpc.Code)2 IOException (java.io.IOException)2