Skip to content

Commit bd89556

Browse files
feat: throw error when using connection pool for explicit stream (#1903)
* feat: Split writer into connection worker and wrapper, this is a prerequisite for multiplexing client * feat: add connection worker pool skeleton, used for multiplexing client * feat: add Load api for connection worker for multiplexing client * feat: add multiplexing support to connection worker. We will treat every new stream name as a switch of destinationt * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * feat: port the multiplexing client core algorithm and basic tests also fixed a tiny bug inside fake bigquery write impl for getting thre response from offset * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * feat: wire multiplexing connection pool to stream writer * feat: some fixes for multiplexing client * feat: fix some todos, and reject the mixed behavior of passed in client or not * feat: fix the bug that we may peek into the write_stream field but it's possible the proto schema does not contain this field * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * feat: fix the bug that we may peek into the write_stream field but it's possible the proto schema does not contain this field * feat: add getInflightWaitSeconds implementation * feat: Add schema comparision in connection loop to ensure schema update for the same stream name can be notified * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * feat: add schema update support to multiplexing * fix: fix windows build bug: windows Instant resolution is different with linux * fix: fix another failing tests for windows build * fix: fix another test failure for Windows build * feat: Change new thread for each retry to be a thread pool to avoid create/tear down too much threads if lots of retries happens * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: add back the background executor provider that's accidentally removed * feat: throw error when use connection pool for explicit stream Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 3c26596 commit bd89556

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/StreamWriter.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public class StreamWriter implements AutoCloseable {
4747
private static final Logger log = Logger.getLogger(StreamWriter.class.getName());
4848

4949
private static String datasetsMatching = "projects/[^/]+/datasets/[^/]+/";
50-
private static Pattern streamPattern = Pattern.compile(datasetsMatching);
50+
private static Pattern streamPatternDatasets = Pattern.compile(datasetsMatching);
51+
52+
private static String defaultStreamMatching = "/_default";
53+
private static Pattern streamPatternDefaultStream = Pattern.compile(defaultStreamMatching);
5154

5255
// Cache of location info for a given dataset.
5356
private static Map<String, String> projectAndDatasetToLocation = new ConcurrentHashMap<>();
@@ -195,6 +198,14 @@ private StreamWriter(Builder builder) throws IOException {
195198
getBigQueryWriteClient(builder),
196199
ownsBigQueryWriteClient));
197200
} else {
201+
if (!isDefaultStream(streamName)) {
202+
log.warning(
203+
"Connection pool is only allowed in default stream! However received "
204+
+ builder.streamName);
205+
throw new IllegalArgumentException(
206+
"Trying to enable connection pool in non-default stream.");
207+
}
208+
198209
BigQueryWriteClient client = getBigQueryWriteClient(builder);
199210
String location = builder.location;
200211
if (location == null || location.isEmpty()) {
@@ -264,7 +275,7 @@ private StreamWriter(Builder builder) throws IOException {
264275

265276
@VisibleForTesting
266277
static String extractDatasetAndProjectName(String streamName) {
267-
Matcher streamMatcher = streamPattern.matcher(streamName);
278+
Matcher streamMatcher = streamPatternDatasets.matcher(streamName);
268279
if (streamMatcher.find()) {
269280
return streamMatcher.group();
270281
} else {
@@ -273,6 +284,12 @@ static String extractDatasetAndProjectName(String streamName) {
273284
}
274285
}
275286

287+
@VisibleForTesting
288+
static boolean isDefaultStream(String streamName) {
289+
Matcher streamMatcher = streamPatternDefaultStream.matcher(streamName);
290+
return streamMatcher.find();
291+
}
292+
276293
private BigQueryWriteClient getBigQueryWriteClient(Builder builder) throws IOException {
277294
if (builder.client == null) {
278295
BigQueryWriteSettings stubSettings =

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonStreamWriterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@
6363
@RunWith(JUnit4.class)
6464
public class JsonStreamWriterTest {
6565
private static final Logger LOG = Logger.getLogger(JsonStreamWriterTest.class.getName());
66-
private static final String TEST_STREAM = "projects/p/datasets/d/tables/t/streams/s";
67-
private static final String TEST_STREAM_2 = "projects/p/datasets/d2/tables/t2/streams/s2";
66+
private static final String TEST_STREAM = "projects/p/datasets/d/tables/t/streams/_default";
67+
private static final String TEST_STREAM_2 = "projects/p/datasets/d2/tables/t2/streams/_default";
6868
private static final String TEST_TABLE = "projects/p/datasets/d/tables/t";
6969
private static final ExecutorProvider SINGLE_THREAD_EXECUTOR =
7070
InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build();

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/StreamWriterTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@
6262
@RunWith(JUnit4.class)
6363
public class StreamWriterTest {
6464
private static final Logger log = Logger.getLogger(StreamWriterTest.class.getName());
65-
private static final String TEST_STREAM_1 = "projects/p/datasets/d1/tables/t1/streams/s1";
66-
private static final String TEST_STREAM_2 = "projects/p/datasets/d2/tables/t2/streams/s2";
65+
private static final String TEST_STREAM_1 = "projects/p/datasets/d1/tables/t1/streams/_default";
66+
private static final String TEST_STREAM_2 = "projects/p/datasets/d2/tables/t2/streams/_default";
67+
private static final String TEST_STREAM_SHORTEN = "projects/p/datasets/d2/tables/t2/_default";
68+
private static final String EXPLICIT_STEAM = "projects/p/datasets/d1/tables/t1/streams/s1";
6769
private static final String TEST_TRACE_ID = "DATAFLOW:job_id";
6870
private FakeScheduledExecutorService fakeExecutor;
6971
private FakeBigQueryWrite testBigQueryWrite;
@@ -366,6 +368,31 @@ public void run() throws Throwable {
366368
});
367369
}
368370

371+
@Test
372+
public void testEnableConnectionPoolOnExplicitStream() throws Exception {
373+
IllegalArgumentException ex =
374+
assertThrows(
375+
IllegalArgumentException.class,
376+
new ThrowingRunnable() {
377+
@Override
378+
public void run() throws Throwable {
379+
StreamWriter.newBuilder(EXPLICIT_STEAM, client)
380+
.setEnableConnectionPool(true)
381+
.build();
382+
}
383+
});
384+
assertTrue(ex.getMessage().contains("Trying to enable connection pool in non-default stream."));
385+
}
386+
387+
@Test
388+
public void testShortenStreamNameAllowed() throws Exception {
389+
// no exception is thrown.
390+
StreamWriter.newBuilder(TEST_STREAM_SHORTEN, client)
391+
.setEnableConnectionPool(true)
392+
.setLocation("us")
393+
.build();
394+
}
395+
369396
@Test
370397
public void testAppendSuccessAndConnectionError() throws Exception {
371398
StreamWriter writer = getTestStreamWriter();

0 commit comments

Comments
 (0)