Skip to content

Commit 8eda934

Browse files
feat: Add sample about processing permanent writer failure (#2057)
* feat: Add sample about writer permanently failed * . * . * . * . * . * . * . * . * . * . * 🦉 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 <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 1e9a8ca commit 8eda934

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStream.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.io.IOException;
4141
import java.util.Map;
4242
import java.util.concurrent.Phaser;
43+
import java.util.concurrent.atomic.AtomicInteger;
4344
import javax.annotation.concurrent.GuardedBy;
4445
import org.json.JSONArray;
4546
import org.json.JSONObject;
@@ -123,6 +124,7 @@ private static class AppendContext {
123124
private static class DataWriter {
124125

125126
private static final int MAX_RETRY_COUNT = 3;
127+
private static final int MAX_RECREATE_COUNT = 3;
126128
private static final ImmutableList<Code> RETRIABLE_ERROR_CODES =
127129
ImmutableList.of(
128130
Code.INTERNAL,
@@ -140,6 +142,8 @@ private static class DataWriter {
140142
@GuardedBy("lock")
141143
private RuntimeException error = null;
142144

145+
private AtomicInteger recreateCount = new AtomicInteger(0);
146+
143147
public void initialize(TableName parentTable)
144148
throws DescriptorValidationException, IOException, InterruptedException {
145149
// Use the JSON stream writer to send records in JSON format. Specify the table name to write
@@ -151,8 +155,17 @@ public void initialize(TableName parentTable)
151155
}
152156

153157
public void append(AppendContext appendContext)
154-
throws DescriptorValidationException, IOException {
158+
throws DescriptorValidationException, IOException, InterruptedException {
155159
synchronized (this.lock) {
160+
if (!streamWriter.isUserClosed()
161+
&& streamWriter.isClosed()
162+
&& recreateCount.getAndIncrement() < MAX_RECREATE_COUNT) {
163+
streamWriter =
164+
JsonStreamWriter.newBuilder(
165+
streamWriter.getStreamName(), BigQueryWriteClient.create())
166+
.build();
167+
this.error = null;
168+
}
156169
// If earlier appends have failed, we need to reset before continuing.
157170
if (this.error != null) {
158171
throw this.error;
@@ -194,6 +207,7 @@ public AppendCompleteCallback(DataWriter parent, AppendContext appendContext) {
194207

195208
public void onSuccess(AppendRowsResponse response) {
196209
System.out.format("Append success\n");
210+
this.parent.recreateCount.set(0);
197211
done();
198212
}
199213

@@ -241,6 +255,8 @@ public void onFailure(Throwable throwable) {
241255
throw new RuntimeException(e);
242256
} catch (IOException e) {
243257
throw new RuntimeException(e);
258+
} catch (InterruptedException e) {
259+
throw new RuntimeException(e);
244260
}
245261
}
246262
// Mark the existing attempt as done since we got a response for it

0 commit comments

Comments
 (0)