use of com.google.api.gax.rpc.FailedPreconditionException in project beam by apache.
the class BigQueryIOStorageReadTest method testStreamSourceSplitAtFractionFailsWhenParentIsPastSplitPointArrow.
@Test
public void testStreamSourceSplitAtFractionFailsWhenParentIsPastSplitPointArrow() throws Exception {
List<String> names = Arrays.asList("A", "B", "C", "D", "E", "F");
List<Long> values = Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L);
List<ReadRowsResponse> parentResponses = Lists.newArrayList(createResponseArrow(ARROW_SCHEMA, names.subList(0, 2), values.subList(0, 2), 0.0, 0.25), createResponseArrow(ARROW_SCHEMA, names.subList(2, 3), values.subList(2, 3), 0.25, 0.5), createResponseArrow(ARROW_SCHEMA, names.subList(3, 5), values.subList(3, 5), 0.5, 0.75));
StorageClient fakeStorageClient = mock(StorageClient.class);
when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream("parentStream").build(), "")).thenReturn(new FakeBigQueryServerStream<>(parentResponses));
// Mocks the split call. A response without a primary_stream and remainder_stream means
// that the split is not possible.
// Mocks the split call.
when(fakeStorageClient.splitReadStream(SplitReadStreamRequest.newBuilder().setName("parentStream").setFraction(0.5f).build())).thenReturn(SplitReadStreamResponse.newBuilder().setPrimaryStream(ReadStream.newBuilder().setName("primaryStream")).setRemainderStream(ReadStream.newBuilder().setName("remainderStream")).build());
// Mocks the ReadRows calls expected on the primary and residual streams.
when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream("primaryStream").setOffset(2).build(), "")).thenThrow(new FailedPreconditionException("Given row offset is invalid for stream.", new StatusRuntimeException(Status.FAILED_PRECONDITION), GrpcStatusCode.of(Code.FAILED_PRECONDITION), /* retryable = */
false));
BigQueryStorageStreamSource<TableRow> streamSource = BigQueryStorageStreamSource.create(ReadSession.newBuilder().setName("readSession").setArrowSchema(ArrowSchema.newBuilder().setSerializedSchema(serializeArrowSchema(ARROW_SCHEMA)).build()).setDataFormat(DataFormat.ARROW).build(), ReadStream.newBuilder().setName("parentStream").build(), TABLE_SCHEMA, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withStorageClient(fakeStorageClient));
// Read a few records from the parent stream and ensure that records are returned in the
// prescribed order.
BoundedReader<TableRow> parent = streamSource.createReader(options);
assertTrue(parent.start());
assertEquals("A", parent.getCurrent().get("name"));
assertTrue(parent.advance());
assertEquals("B", parent.getCurrent().get("name"));
assertNull(parent.splitAtFraction(0.5));
// Verify that the parent source still works okay even after an unsuccessful split attempt.
assertTrue(parent.advance());
assertEquals("C", parent.getCurrent().get("name"));
assertTrue(parent.advance());
assertEquals("D", parent.getCurrent().get("name"));
assertTrue(parent.advance());
assertEquals("E", parent.getCurrent().get("name"));
assertFalse(parent.advance());
}
use of com.google.api.gax.rpc.FailedPreconditionException in project beam by apache.
the class BigQueryIOStorageReadTest method testStreamSourceSplitAtFractionFailsWhenParentIsPastSplitPoint.
@Test
public void testStreamSourceSplitAtFractionFailsWhenParentIsPastSplitPoint() throws Exception {
List<ReadRowsResponse> parentResponses = Lists.newArrayList(createResponse(AVRO_SCHEMA, Lists.newArrayList(createRecord("A", 1, AVRO_SCHEMA), createRecord("B", 2, AVRO_SCHEMA)), 0.0, 0.25), createResponse(AVRO_SCHEMA, Lists.newArrayList(createRecord("C", 3, AVRO_SCHEMA)), 0.25, 0.50), createResponse(AVRO_SCHEMA, Lists.newArrayList(createRecord("D", 4, AVRO_SCHEMA), createRecord("E", 5, AVRO_SCHEMA)), 0.5, 0.75));
StorageClient fakeStorageClient = mock(StorageClient.class);
when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream("parentStream").build(), "")).thenReturn(new FakeBigQueryServerStream<>(parentResponses));
// Mocks the split call. A response without a primary_stream and remainder_stream means
// that the split is not possible.
// Mocks the split call.
when(fakeStorageClient.splitReadStream(SplitReadStreamRequest.newBuilder().setName("parentStream").setFraction(0.5f).build())).thenReturn(SplitReadStreamResponse.newBuilder().setPrimaryStream(ReadStream.newBuilder().setName("primaryStream")).setRemainderStream(ReadStream.newBuilder().setName("remainderStream")).build());
// Mocks the ReadRows calls expected on the primary and residual streams.
when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream("primaryStream").setOffset(2).build(), "")).thenThrow(new FailedPreconditionException("Given row offset is invalid for stream.", new StatusRuntimeException(Status.FAILED_PRECONDITION), GrpcStatusCode.of(Code.FAILED_PRECONDITION), /* retryable = */
false));
BigQueryStorageStreamSource<TableRow> streamSource = BigQueryStorageStreamSource.create(ReadSession.newBuilder().setName("readSession").setAvroSchema(AvroSchema.newBuilder().setSchema(AVRO_SCHEMA_STRING)).build(), ReadStream.newBuilder().setName("parentStream").build(), TABLE_SCHEMA, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withStorageClient(fakeStorageClient));
// Read a few records from the parent stream and ensure that records are returned in the
// prescribed order.
BoundedReader<TableRow> parent = streamSource.createReader(options);
assertTrue(parent.start());
assertEquals("A", parent.getCurrent().get("name"));
assertTrue(parent.advance());
assertEquals("B", parent.getCurrent().get("name"));
assertNull(parent.splitAtFraction(0.5));
// Verify that the parent source still works okay even after an unsuccessful split attempt.
assertTrue(parent.advance());
assertEquals("C", parent.getCurrent().get("name"));
assertTrue(parent.advance());
assertEquals("D", parent.getCurrent().get("name"));
assertTrue(parent.advance());
assertEquals("E", parent.getCurrent().get("name"));
assertFalse(parent.advance());
}
Aggregations