|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigquery.storage.v1.it; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | + |
| 21 | +import com.google.api.gax.rpc.ServerStream; |
| 22 | +import com.google.cloud.ServiceOptions; |
| 23 | +import com.google.cloud.bigquery.storage.v1.BigQueryReadClient; |
| 24 | +import com.google.cloud.bigquery.storage.v1.DataFormat; |
| 25 | +import com.google.cloud.bigquery.storage.v1.ReadRowsRequest; |
| 26 | +import com.google.cloud.bigquery.storage.v1.ReadRowsResponse; |
| 27 | +import com.google.cloud.bigquery.storage.v1.ReadSession; |
| 28 | +import com.google.cloud.bigquery.storage.v1.ReadStream; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.concurrent.Callable; |
| 33 | +import java.util.concurrent.ExecutionException; |
| 34 | +import java.util.concurrent.ExecutorService; |
| 35 | +import java.util.concurrent.Executors; |
| 36 | +import java.util.concurrent.Future; |
| 37 | +import java.util.logging.Logger; |
| 38 | +import org.junit.AfterClass; |
| 39 | +import org.junit.Assume; |
| 40 | +import org.junit.BeforeClass; |
| 41 | +import org.junit.Test; |
| 42 | + |
| 43 | +/** |
| 44 | + * Integration tests for BigQuery Storage API which target long running sessions. These tests can be |
| 45 | + * enabled by setting the system property 'bigquery.storage.enable_long_running_tests' to true. |
| 46 | + */ |
| 47 | +public class ITBigQueryStorageLongRunningTest { |
| 48 | + |
| 49 | + private static final Logger LOG = |
| 50 | + Logger.getLogger(ITBigQueryStorageLongRunningTest.class.getName()); |
| 51 | + |
| 52 | + private static final String LONG_TESTS_ENABLED_PROPERTY = |
| 53 | + "bigquery.storage.enable_long_running_tests"; |
| 54 | + |
| 55 | + private static final String LONG_TESTS_DISABLED_MESSAGE = |
| 56 | + String.format( |
| 57 | + "BigQuery Storage long running tests are not enabled and will be skipped. " |
| 58 | + + "To enable them, set system property '%s' to true.", |
| 59 | + LONG_TESTS_ENABLED_PROPERTY); |
| 60 | + |
| 61 | + private static BigQueryReadClient client; |
| 62 | + private static String parentProjectId; |
| 63 | + |
| 64 | + @BeforeClass |
| 65 | + public static void beforeClass() throws IOException { |
| 66 | + Assume.assumeTrue(LONG_TESTS_DISABLED_MESSAGE, Boolean.getBoolean(LONG_TESTS_ENABLED_PROPERTY)); |
| 67 | + client = BigQueryReadClient.create(); |
| 68 | + parentProjectId = String.format("projects/%s", ServiceOptions.getDefaultProjectId()); |
| 69 | + |
| 70 | + LOG.info( |
| 71 | + String.format( |
| 72 | + "%s tests running with parent project: %s", |
| 73 | + ITBigQueryStorageLongRunningTest.class.getSimpleName(), parentProjectId)); |
| 74 | + } |
| 75 | + |
| 76 | + @AfterClass |
| 77 | + public static void afterClass() { |
| 78 | + if (client != null) { |
| 79 | + client.close(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testLongRunningReadSession() throws InterruptedException, ExecutionException { |
| 85 | + // This test reads a larger table with the goal of doing a simple validation of timeout settings |
| 86 | + // for a longer running session. |
| 87 | + |
| 88 | + String table = |
| 89 | + BigQueryResource.FormatTableResource( |
| 90 | + /* projectId = */ "bigquery-public-data", |
| 91 | + /* datasetId = */ "samples", |
| 92 | + /* tableId = */ "wikipedia"); |
| 93 | + |
| 94 | + ReadSession session = |
| 95 | + client.createReadSession( |
| 96 | + /* parent = */ parentProjectId, |
| 97 | + /* readSession = */ ReadSession.newBuilder() |
| 98 | + .setTable(table) |
| 99 | + .setDataFormat(DataFormat.AVRO) |
| 100 | + .build(), |
| 101 | + /* maxStreamCount = */ 5); |
| 102 | + |
| 103 | + assertEquals( |
| 104 | + String.format( |
| 105 | + "Did not receive expected number of streams for table '%s' CreateReadSession response:%n%s", |
| 106 | + table, session.toString()), |
| 107 | + 5, |
| 108 | + session.getStreamsCount()); |
| 109 | + |
| 110 | + List<Callable<Long>> tasks = new ArrayList<>(session.getStreamsCount()); |
| 111 | + for (final ReadStream stream : session.getStreamsList()) { |
| 112 | + tasks.add( |
| 113 | + new Callable<Long>() { |
| 114 | + @Override |
| 115 | + public Long call() throws Exception { |
| 116 | + return readAllRowsFromStream(stream); |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + ExecutorService executor = Executors.newFixedThreadPool(tasks.size()); |
| 122 | + List<Future<Long>> results = executor.invokeAll(tasks); |
| 123 | + |
| 124 | + long rowCount = 0; |
| 125 | + for (Future<Long> result : results) { |
| 126 | + rowCount += result.get(); |
| 127 | + } |
| 128 | + |
| 129 | + assertEquals(313_797_035, rowCount); |
| 130 | + } |
| 131 | + |
| 132 | + private long readAllRowsFromStream(ReadStream readStream) { |
| 133 | + |
| 134 | + ReadRowsRequest readRowsRequest = |
| 135 | + ReadRowsRequest.newBuilder().setReadStream(readStream.getName()).build(); |
| 136 | + |
| 137 | + long rowCount = 0; |
| 138 | + ServerStream<ReadRowsResponse> serverStream = client.readRowsCallable().call(readRowsRequest); |
| 139 | + for (ReadRowsResponse response : serverStream) { |
| 140 | + rowCount += response.getRowCount(); |
| 141 | + } |
| 142 | + |
| 143 | + LOG.info( |
| 144 | + String.format("Read total of %d rows from stream '%s'.", rowCount, readStream.getName())); |
| 145 | + return rowCount; |
| 146 | + } |
| 147 | +} |
0 commit comments