Skip to content

Commit

Permalink
feat: Add sample about writer permanently failed
Browse files Browse the repository at this point in the history
  • Loading branch information
yirutang committed Mar 22, 2023
1 parent a989ac6 commit f8fd07b
Showing 1 changed file with 15 additions and 0 deletions.
Expand Up @@ -39,6 +39,7 @@
import io.grpc.Status.Code;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.Phaser;
import javax.annotation.concurrent.GuardedBy;
import org.json.JSONArray;
Expand Down Expand Up @@ -123,6 +124,7 @@ private static class AppendContext {
private static class DataWriter {

private static final int MAX_RETRY_COUNT = 3;
private static final int MAX_RECREATE_COUNT = 3;
private static final ImmutableList<Code> RETRIABLE_ERROR_CODES =
ImmutableList.of(
Code.INTERNAL,
Expand All @@ -140,6 +142,8 @@ private static class DataWriter {
@GuardedBy("lock")
private RuntimeException error = null;

private AtomicInteger recreateCount = new AtomicInteger(0);

public void initialize(TableName parentTable)
throws DescriptorValidationException, IOException, InterruptedException {
// Use the JSON stream writer to send records in JSON format. Specify the table name to write
Expand All @@ -153,6 +157,16 @@ public void initialize(TableName parentTable)
public void append(AppendContext appendContext)
throws DescriptorValidationException, IOException {
synchronized (this.lock) {
if (streamWriter.isUserClosed()) {
throw new RuntimeException("Writer is closed by user.");
}
// If stream writer is premenantly failed, try recreate a stream writer.
if (streamWriter.isClosed() && recreateCount.recreateCount() <= MAX_RECREATE_COUNT) {
streamWriter =
JsonStreamWriter.newBuilder(parentTable.toString(),
BigQueryWriteClient.create()).build();
this.error = null;
}
// If earlier appends have failed, we need to reset before continuing.
if (this.error != null) {
throw this.error;
Expand Down Expand Up @@ -194,6 +208,7 @@ public AppendCompleteCallback(DataWriter parent, AppendContext appendContext) {

public void onSuccess(AppendRowsResponse response) {
System.out.format("Append success\n");
this.parent.recreateCount.set(0);
done();
}

Expand Down

0 comments on commit f8fd07b

Please sign in to comment.