From d8a52f886aef174bf01147e81728de6b2f09f4c3 Mon Sep 17 00:00:00 2001 From: Gaole Meng Date: Thu, 7 Mar 2024 12:08:37 -0800 Subject: [PATCH] fix: fix WriteToDefaultStream example code to close the client properly (#2433) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: fix channel not shut down properly exception. Client being created has to be properly closed, otherwise during garbage collection an error will be reported showing channel not shutdown properly * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot --- .../bigquerystorage/WriteToDefaultStream.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStream.java b/samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStream.java index 6c35f06018..4a89d6274e 100644 --- a/samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStream.java +++ b/samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStream.java @@ -138,6 +138,8 @@ private static class DataWriter { private static final int MAX_RECREATE_COUNT = 3; + private BigQueryWriteClient client; + // Track the number of in-flight requests to wait for all responses before shutting down. private final Phaser inflightRequestCount = new Phaser(1); private final Object lock = new Object(); @@ -163,12 +165,16 @@ public void initialize(TableName parentTable) .setMaxRetryDelay(Duration.ofMinutes(1)) .build(); + // Initialize client without settings, internally within stream writer a new client will be + // created with full settings. + client = BigQueryWriteClient.create(); + // Use the JSON stream writer to send records in JSON format. Specify the table name to write // to the default stream. // For more information about JsonStreamWriter, see: // https://googleapis.dev/java/google-cloud-bigquerystorage/latest/com/google/cloud/bigquery/storage/v1/JsonStreamWriter.html streamWriter = - JsonStreamWriter.newBuilder(parentTable.toString(), BigQueryWriteClient.create()) + JsonStreamWriter.newBuilder(parentTable.toString(), client) .setExecutorProvider( FixedExecutorProvider.create(Executors.newScheduledThreadPool(100))) .setChannelProvider( @@ -193,10 +199,7 @@ public void append(AppendContext appendContext) if (!streamWriter.isUserClosed() && streamWriter.isClosed() && recreateCount.getAndIncrement() < MAX_RECREATE_COUNT) { - streamWriter = - JsonStreamWriter.newBuilder( - streamWriter.getStreamName(), BigQueryWriteClient.create()) - .build(); + streamWriter = JsonStreamWriter.newBuilder(streamWriter.getStreamName(), client).build(); this.error = null; } // If earlier appends have failed, we need to reset before continuing. @@ -217,6 +220,7 @@ public void cleanup() { // Wait for all in-flight requests to complete. inflightRequestCount.arriveAndAwaitAdvance(); + client.close(); // Close the connection to the server. streamWriter.close();