Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle when writer is closed #18

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,21 @@ protected void writeWithRetry(String traceId, Rows<A> rows, int retryCount) thro
switch (status.getCode()) {
case INTERNAL:
case CANCELLED:
case ABORTED: {
logger.warn(createLogMessage.apply("Recoverable error. Retrying.., "), error);
try {
Thread.sleep(clientProvider.writeSettings().getRetryPause().toMillis());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

if (retryCount > 0) {
writeWithRetry(errorTraceId, errorRows, retryCount - 1);
} else {
throw error;
}
break;
case UNAVAILABLE:
case ABORTED: {
if (retryCount > 0) {
writeWithRetry(errorTraceId, errorRows, retryCount - 1);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ public BigQueryJsonBufferedSinkWriter(Sink.InitContext sinkInitContext, RowValue
protected ApiFuture<AppendRowsResponse> append(String traceId, Rows<A> rows) {
var rowArray = new JSONArray();
rows.getData().forEach(row -> rowArray.put(new JSONObject(new String(rowSerializer.serialize(row)))));
var writer = streamWriter(traceId, rows.getStream(), rows.getTable());

if (writer.isClosed() || writer.isUserClosed()) {
recreateAllStreamWriters(traceId, rows.getStream(), rows.getTable());
writer = streamWriter(traceId, rows.getStream(), rows.getTable());
}

try {
return streamWriter(traceId, rows.getStream(), rows.getTable()).append(rowArray, rows.getOffset());
return writer.append(rowArray, rows.getOffset());
} catch (Throwable t) {
return ApiFutures.immediateFailedFuture(t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ protected ApiFuture<AppendRowsResponse> append(String traceId, Rows<A> rows) {
Optional.ofNullable(metrics.get(rows.getStream())).ifPresent(s -> s.updateSize(size));
var writer = streamWriter(traceId, rows.getStream(), rows.getTable());

if (writer.isClosed() || writer.isUserClosed()) {
logger.warn("Trace-id {}, StreamWrite is closed. Recreating stream for {}", traceId, rows.getStream());
recreateAllStreamWriters(traceId, rows.getStream(), rows.getTable());
writer = streamWriter(traceId, rows.getStream(), rows.getTable());
}

logger.trace("Trace-id {}, Writing rows stream {} to steamWriter for {} writer id {}", traceId, rows.getStream(), writer.getStreamName(), writer.getWriterId());

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.storage.v1.AppendRowsResponse;
import com.google.cloud.bigquery.storage.v1.JsonStreamWriter;
import com.google.protobuf.Descriptors;
Expand All @@ -28,6 +29,13 @@ protected ApiFuture<AppendRowsResponse> append(String traceId, Rows<A> rows) {
var rowArray = new JSONArray();
rows.getData().forEach(row -> rowArray.put(new JSONObject(new String(rowSerializer.serialize(row)))));
var writer = streamWriter(traceId, rows.getStream(), rows.getTable());

if (writer.isClosed() || writer.isUserClosed()) {
logger.warn("Trace-id {}, StreamWrite is closed. Recreating stream for {}", traceId, rows.getStream());
recreateAllStreamWriters(traceId, rows.getStream(), rows.getTable());
writer = streamWriter(traceId, rows.getStream(), rows.getTable());
}

logger.trace("Trace-id {}, Writing rows stream {} to steamWriter for {} writer id {}", traceId, rows.getStream(), writer.getStreamName(), writer.getWriterId());

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ protected ApiFuture<AppendRowsResponse> append(String traceId, Rows<A> rows) {

var writer = streamWriter(traceId, rows.getStream(), rows.getTable());

if (writer.isClosed() || writer.isUserClosed()) {
logger.warn("Trace-id {}, StreamWrite is closed. Recreating stream for {}", traceId, rows.getStream());
recreateAllStreamWriters(traceId, rows.getStream(), rows.getTable());
writer = streamWriter(traceId, rows.getStream(), rows.getTable());
}

logger.trace("Trace-id {}, Writing rows stream {} to steamWriter for {} writer id {}", traceId, rows.getStream(), writer.getStreamName(), writer.getWriterId());
try {
return writer.append(prows);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ public void onFailure(Throwable t) {
var status = Status.fromThrowable(t);
switch (status.getCode()) {
case INTERNAL:
case ABORTED:
case CANCELLED:
case FAILED_PRECONDITION:
case DEADLINE_EXCEEDED:
doPauseBeforeRetry();
retryWrite(t, retryCount - 1);
break;
case ABORTED:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about buffered stream sink writer ?

case UNAVAILABLE: {
this.parent.recreateAllStreamWriters(traceId, rows.getStream(), rows.getTable());
retryWrite(t, retryCount - 1);
Expand Down
Loading