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

fix: catch uncaught exception from append loop and add expoential retry to reconnection #2015

Merged
merged 87 commits into from Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
5a63d95
feat: Split writer into connection worker and wrapper, this is a
GaoleMeng Sep 9, 2022
5a13302
feat: add connection worker pool skeleton, used for multiplexing client
GaoleMeng Sep 13, 2022
0297204
Merge branch 'main' into main
GaoleMeng Sep 14, 2022
8a81ad3
feat: add Load api for connection worker for multiplexing client
GaoleMeng Sep 14, 2022
68fd040
Merge remote-tracking branch 'upstream/main'
GaoleMeng Sep 14, 2022
3106dae
Merge remote-tracking branch 'upstream/main'
GaoleMeng Sep 15, 2022
5bf04e5
Merge branch 'googleapis:main' into main
GaoleMeng Sep 15, 2022
2fc7551
Merge branch 'main' of https://github.com/GaoleMeng/java-bigquerystorage
GaoleMeng Sep 15, 2022
7a6d919
feat: add multiplexing support to connection worker. We will treat every
GaoleMeng Sep 15, 2022
3ba7659
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Sep 16, 2022
f379a78
Updates from OwlBot post-processor
gcf-owl-bot[bot] Sep 16, 2022
9307776
Merge branch 'main' of https://github.com/GaoleMeng/java-bigquerystorage
GaoleMeng Sep 16, 2022
de73013
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Sep 16, 2022
19005a1
feat: port the multiplexing client core algorithm and basic tests
GaoleMeng Sep 19, 2022
c5d14ba
Merge branch 'googleapis:main' into main
GaoleMeng Sep 19, 2022
644360a
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Sep 20, 2022
3099d82
Merge branch 'googleapis:main' into main
GaoleMeng Sep 20, 2022
e707dd6
Merge branch 'googleapis:main' into main
GaoleMeng Sep 20, 2022
9e7a8fa
Merge branch 'main' of https://github.com/GaoleMeng/java-bigquerystorage
GaoleMeng Sep 20, 2022
31f1755
Merge branch 'googleapis:main' into main
GaoleMeng Sep 20, 2022
44c36fc
feat: wire multiplexing connection pool to stream writer
GaoleMeng Sep 20, 2022
87a4036
feat: some fixes for multiplexing client
GaoleMeng Sep 23, 2022
c92ea1b
Merge remote-tracking branch 'upstream/main'
GaoleMeng Sep 23, 2022
019520c
Merge branch 'googleapis:main' into main
GaoleMeng Sep 26, 2022
47893df
feat: fix some todos, and reject the mixed behavior of passed in clie…
GaoleMeng Sep 27, 2022
8bd4e6a
Merge remote-tracking branch 'upstream/main'
GaoleMeng Sep 27, 2022
83409b0
Merge remote-tracking branch 'upstream/main'
GaoleMeng Sep 27, 2022
f7dd72d
Merge branch 'googleapis:main' into main
GaoleMeng Sep 27, 2022
a48399f
Merge branch 'googleapis:main' into main
GaoleMeng Sep 29, 2022
6789bc9
feat: fix the bug that we may peek into the write_stream field but it's
GaoleMeng Sep 29, 2022
46b4e6c
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Sep 29, 2022
dfd4dd9
Merge branch 'googleapis:main' into main
GaoleMeng Sep 29, 2022
d68ae70
feat: fix the bug that we may peek into the write_stream field but it's
GaoleMeng Sep 29, 2022
2983fe9
Merge branch 'main' of https://github.com/GaoleMeng/java-bigquerystorage
GaoleMeng Sep 29, 2022
d406256
Merge branch 'googleapis:main' into main
GaoleMeng Oct 13, 2022
22e9e07
feat: add getInflightWaitSeconds implementation
GaoleMeng Oct 13, 2022
fdb4e1c
Merge branch 'googleapis:main' into main
GaoleMeng Oct 21, 2022
0469474
Merge branch 'googleapis:main' into main
GaoleMeng Nov 2, 2022
d1b7740
feat: Add schema comparision in connection loop to ensure schema upda…
GaoleMeng Nov 3, 2022
e4cd529
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Nov 4, 2022
74ff1c4
Merge branch 'googleapis:main' into main
GaoleMeng Nov 4, 2022
762f49e
feat: add schema update support to multiplexing
GaoleMeng Nov 5, 2022
de456c2
Merge branch 'googleapis:main' into main
GaoleMeng Nov 11, 2022
c2f6edc
Merge branch 'googleapis:main' into main
GaoleMeng Nov 15, 2022
2487227
fix: fix windows build bug: windows Instant resolution is different with
GaoleMeng Nov 15, 2022
084d6d1
fix: fix another failing tests for windows build
GaoleMeng Nov 16, 2022
89c9701
Merge branch 'main' of https://github.com/GaoleMeng/java-bigquerystorage
GaoleMeng Nov 16, 2022
8441518
fix: fix another test failure for Windows build
GaoleMeng Nov 16, 2022
d249add
Merge branch 'googleapis:main' into main
GaoleMeng Nov 30, 2022
83aa7ff
feat: Change new thread for each retry to be a thread pool to avoid
GaoleMeng Nov 30, 2022
92a9c36
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Nov 30, 2022
a713a52
Merge branch 'googleapis:main' into main
GaoleMeng Nov 30, 2022
a042d5c
fix: add back the background executor provider that's accidentally
GaoleMeng Nov 30, 2022
53f4ec8
feat: throw error when use connection pool for explicit stream
GaoleMeng Dec 2, 2022
c494d8b
Merge branch 'googleapis:main' into main
GaoleMeng Dec 20, 2022
14b0c12
fix: Add precision truncation to the passed in value from JSON float and
GaoleMeng Jan 17, 2023
0da0e4b
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jan 17, 2023
33d23ac
Merge branch 'googleapis:main' into main
GaoleMeng Jan 17, 2023
d2ee46e
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jan 17, 2023
be6646e
modify the bom version
GaoleMeng Jan 17, 2023
62d8c41
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jan 17, 2023
adf5f3f
fix deadlockissue in ConnectionWorkerPool
GaoleMeng Jan 18, 2023
c1970ff
Merge branch 'googleapis:main' into main
GaoleMeng Jan 18, 2023
3488df8
fix: fix deadlock issue during close + append for multiplexing
GaoleMeng Jan 19, 2023
6a512e8
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jan 20, 2023
05edc2f
Merge branch 'googleapis:main' into main
GaoleMeng Jan 20, 2023
7d3da74
Merge branch 'googleapis:main' into main
GaoleMeng Jan 20, 2023
ecf6807
Merge branch 'googleapis:main' into main
GaoleMeng Jan 20, 2023
057dab9
Merge branch 'googleapis:main' into main
GaoleMeng Jan 23, 2023
5db46a2
fix: fix one potential root cause of deadlock issue for non-multiplexing
GaoleMeng Jan 23, 2023
32e9d33
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jan 25, 2023
183941a
Merge branch 'googleapis:main' into main
GaoleMeng Jan 25, 2023
f93f89e
Add timeout to inflight queue waiting, and also add some extra log
GaoleMeng Jan 25, 2023
e2ec7e7
Merge branch 'googleapis:main' into main
GaoleMeng Jan 27, 2023
1be6ab4
feat: allow java client lib handle switch table schema for the same s…
GaoleMeng Jan 27, 2023
544f063
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jan 31, 2023
3cf5482
Merge branch 'googleapis:main' into main
GaoleMeng Jan 31, 2023
d9da007
Merge branch 'googleapis:main' into main
GaoleMeng Jan 31, 2023
4626857
Merge branch 'main' of https://github.com/GaoleMeng/java-bigquerystorage
GaoleMeng Jan 31, 2023
146bf95
Merge branch 'googleapis:main' into main
GaoleMeng Feb 4, 2023
dde2c6b
fix: close before retry connection
GaoleMeng Feb 4, 2023
770d08c
fix: close before retry connection
GaoleMeng Feb 4, 2023
700a3e5
Merge branch 'main' of https://github.com/GaoleMeng/java-bigquerystorage
GaoleMeng Feb 6, 2023
2d279ea
Merge branch 'googleapis:main' into main
GaoleMeng Feb 7, 2023
5a74e5b
Merge branch 'googleapis:main' into main
GaoleMeng Feb 8, 2023
bafc1b4
Merge branch 'main' of https://github.com/GaoleMeng/java-bigquerystorage
GaoleMeng Feb 23, 2023
60f7ad3
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -49,20 +49,20 @@ If you are using Maven without BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.8.0')
implementation platform('com.google.cloud:libraries-bom:26.9.0')

implementation 'com.google.cloud:google-cloud-bigquerystorage'
```
If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-bigquerystorage:2.32.0'
implementation 'com.google.cloud:google-cloud-bigquerystorage:2.32.1'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-bigquerystorage" % "2.32.0"
libraryDependencies += "com.google.cloud" % "google-cloud-bigquerystorage" % "2.32.1"
```

## Authentication
Expand Down
Expand Up @@ -198,6 +198,12 @@ class ConnectionWorker implements AutoCloseable {
*/
private final String writerId = UUID.randomUUID().toString();

/*
* Test only exception behavior testing params.
*/
private RuntimeException testOnlyRunTimeExceptionInAppendLoop = null;
private long testOnlyAppendLoopSleepTime = 0;

/** The maximum size of one request. Defined by the API. */
public static long getApiMaxRequestBytes() {
return 10L * 1000L * 1000L; // 10 megabytes (https://en.wikipedia.org/wiki/Megabyte)
Expand Down Expand Up @@ -240,6 +246,25 @@ public void run() {
appendLoop();
}
});
appendThread.setUncaughtExceptionHandler(
(Thread t, Throwable e) -> {
log.warning(
"Exception thrown from append loop, thus stream writer is shutdown due to exception: "
+ e.toString());
e.printStackTrace();
lock.lock();
try {
connectionFinalStatus = e;
// Move all current waiting requests to in flight queue.
while (!this.waitingRequestQueue.isEmpty()) {
AppendRequestAndResponse requestWrapper = this.waitingRequestQueue.pollFirst();
this.inflightRequestQueue.addLast(requestWrapper);
}
} finally {
lock.unlock();
}
cleanupInflightRequests();
});
this.appendThread.start();
}

Expand All @@ -249,6 +274,8 @@ private void resetConnection() {
// It's safe to directly close the previous connection as the in flight messages
// will be picked up by the next connection.
this.streamConnection.close();
Uninterruptibles.sleepUninterruptibly(
calculateSleepTimeMilli(conectionRetryCountWithoutCallback), TimeUnit.MILLISECONDS);
}
this.streamConnection =
new StreamConnection(
Expand Down Expand Up @@ -391,6 +418,22 @@ private void maybeWaitForInflightQuota() {
inflightWaitSec.set((System.currentTimeMillis() - start_time) / 1000);
}

@VisibleForTesting
static long calculateSleepTimeMilli(long retryCount) {
return Math.min((long) Math.pow(2, retryCount), 60000);
}

@VisibleForTesting
void setTestOnlyAppendLoopSleepTime(long testOnlyAppendLoopSleepTime) {
this.testOnlyAppendLoopSleepTime = testOnlyAppendLoopSleepTime;
}

@VisibleForTesting
void setTestOnlyRunTimeExceptionInAppendLoop(
RuntimeException testOnlyRunTimeExceptionInAppendLoop) {
this.testOnlyRunTimeExceptionInAppendLoop = testOnlyRunTimeExceptionInAppendLoop;
}

public long getInflightWaitSeconds() {
return inflightWaitSec.longValue();
}
Expand Down Expand Up @@ -524,6 +567,10 @@ private void appendLoop() {
} finally {
lock.unlock();
}
if (testOnlyRunTimeExceptionInAppendLoop != null) {
Uninterruptibles.sleepUninterruptibly(testOnlyAppendLoopSleepTime, TimeUnit.MILLISECONDS);
throw testOnlyRunTimeExceptionInAppendLoop;
}
resetConnection();
// Set firstRequestInConnection to indicate the next request to be sent should include
// metedata. Reset everytime after reconnection.
Expand Down
Expand Up @@ -38,6 +38,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -351,6 +352,72 @@ public void testAppendButInflightQueueFull() throws Exception {
}
}

@Test
public void testThrowExceptionWhileWithinAppendLoop() throws Exception {
ProtoSchema schema1 = createProtoSchema("foo");
StreamWriter sw1 =
StreamWriter.newBuilder(TEST_STREAM_1, client).setWriterSchema(schema1).build();
ConnectionWorker connectionWorker =
new ConnectionWorker(
TEST_STREAM_1,
createProtoSchema("foo"),
100000,
100000,
Duration.ofSeconds(100),
FlowController.LimitExceededBehavior.Block,
TEST_TRACE_ID,
client.getSettings());
testBigQueryWrite.setResponseSleep(org.threeten.bp.Duration.ofSeconds(1));
ConnectionWorker.setMaxInflightQueueWaitTime(500);

long appendCount = 10;
for (int i = 0; i < appendCount; i++) {
testBigQueryWrite.addResponse(createAppendResponse(i));
}
connectionWorker.setTestOnlyRunTimeExceptionInAppendLoop(
new RuntimeException("Any exception can happen."));
// Sleep 1 second before erroring out.
connectionWorker.setTestOnlyAppendLoopSleepTime(1000L);

// In total insert 5 requests,
List<ApiFuture<AppendRowsResponse>> futures = new ArrayList<>();
for (int i = 0; i < appendCount; i++) {
futures.add(
sendTestMessage(
connectionWorker, sw1, createFooProtoRows(new String[] {String.valueOf(i)}), i));
assertEquals(connectionWorker.getLoad().inFlightRequestsCount(), i + 1);
}

for (int i = 0; i < appendCount; i++) {
int finalI = i;
ExecutionException ex =
assertThrows(
ExecutionException.class,
() -> futures.get(finalI).get().getAppendResult().getOffset().getValue());
assertThat(ex.getCause()).hasMessageThat().contains("Any exception can happen.");
}

// The future append will directly fail.
ExecutionException ex =
assertThrows(
ExecutionException.class,
() ->
sendTestMessage(
connectionWorker,
sw1,
createFooProtoRows(new String[] {String.valueOf(100)}),
100)
.get());
assertThat(ex.getCause()).hasMessageThat().contains("Any exception can happen.");
}

@Test
public void testExponentialBackoff() throws Exception {
assertThat(ConnectionWorker.calculateSleepTimeMilli(0)).isEqualTo(1);
assertThat(ConnectionWorker.calculateSleepTimeMilli(5)).isEqualTo(32);
assertThat(ConnectionWorker.calculateSleepTimeMilli(100)).isEqualTo(60000);
}

private AppendRowsResponse createAppendResponse(long offset) {
return AppendRowsResponse.newBuilder()
.setAppendResult(
Expand Down