Skip to content

Commit 44a4e4d

Browse files
feat: Change one thread per retry to use a thread pool (#1898)
* 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 Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 8e90767 commit 44a4e4d

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import io.grpc.Status.Code;
4242
import java.io.IOException;
4343
import java.util.Map;
44+
import java.util.concurrent.ExecutorService;
45+
import java.util.concurrent.Executors;
4446
import java.util.concurrent.Phaser;
4547
import javax.annotation.concurrent.GuardedBy;
4648
import org.json.JSONArray;
@@ -193,6 +195,8 @@ static class AppendCompleteCallback implements ApiFutureCallback<AppendRowsRespo
193195

194196
private final DataWriter parent;
195197
private final AppendContext appendContext;
198+
// Prepare a thread pool
199+
static ExecutorService pool = Executors.newFixedThreadPool(50);
196200

197201
public AppendCompleteCallback(DataWriter parent, AppendContext appendContext) {
198202
this.parent = parent;
@@ -213,19 +217,18 @@ public void onFailure(Throwable throwable) {
213217
&& RETRIABLE_ERROR_CODES.contains(status.getCode())) {
214218
appendContext.retryCount++;
215219
// Use a separate thread to avoid potentially blocking while we are in a callback.
216-
new Thread(
217-
() -> {
218-
try {
219-
// Since default stream appends are not ordered, we can simply retry the
220-
// appends.
221-
// Retrying with exclusive streams requires more careful consideration.
222-
this.parent.append(appendContext);
223-
} catch (Exception e) {
224-
// Fall through to return error.
225-
System.out.format("Failed to retry append: %s%n", e);
226-
}
227-
})
228-
.start();
220+
pool.submit(
221+
() -> {
222+
try {
223+
// Since default stream appends are not ordered, we can simply retry the
224+
// appends.
225+
// Retrying with exclusive streams requires more careful consideration.
226+
this.parent.append(appendContext);
227+
} catch (Exception e) {
228+
// Fall through to return error.
229+
System.out.format("Failed to retry append: %s%n", e);
230+
}
231+
});
229232
// Mark the existing attempt as done since it's being retried.
230233
done();
231234
return;
@@ -251,15 +254,14 @@ public void onFailure(Throwable throwable) {
251254
// Retry the remaining valid rows, but using a separate thread to
252255
// avoid potentially blocking while we are in a callback.
253256
if (dataNew.length() > 0) {
254-
new Thread(
255-
() -> {
256-
try {
257-
this.parent.append(new AppendContext(dataNew, 0));
258-
} catch (Exception e2) {
259-
System.out.format("Failed to retry append with filtered rows: %s%n", e2);
260-
}
261-
})
262-
.start();
257+
pool.submit(
258+
() -> {
259+
try {
260+
this.parent.append(new AppendContext(dataNew, 0));
261+
} catch (Exception e2) {
262+
System.out.format("Failed to retry append with filtered rows: %s%n", e2);
263+
}
264+
});
263265
}
264266
return;
265267
}

0 commit comments

Comments
 (0)