Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve read session expired error message
Browse files Browse the repository at this point in the history
vishalkarve15 committed Oct 28, 2024
1 parent 14fa9b8 commit 0f9dc6e
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import io.grpc.Status;
import io.grpc.Status.Code;
import io.grpc.StatusRuntimeException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -82,6 +83,9 @@ public class BigQueryUtil {
"HTTP/2 error code: INTERNAL_ERROR",
"Connection closed with unknown cause",
"Received unexpected EOS on DATA frame from server");

static final String READ_SESSION_EXPIRED_ERROR_MESSAGE = "session expired at";

private static final String PROJECT_PATTERN = "\\S+";
private static final String DATASET_PATTERN = "\\w+";
// Allow all non-whitespace beside ':' and '.'.
@@ -118,6 +122,19 @@ static boolean isRetryableInternalError(Throwable t) {
return false;
}

public static boolean isReadSessionExpired(Throwable cause) {
return getCausalChain(cause).stream().anyMatch(BigQueryUtil::isReadSessionExpiredInternalError);
}

static boolean isReadSessionExpiredInternalError(Throwable t) {
if (t instanceof StatusRuntimeException) {
StatusRuntimeException statusRuntimeException = (StatusRuntimeException) t;
return statusRuntimeException.getStatus().getCode() == Code.FAILED_PRECONDITION
&& statusRuntimeException.getMessage().contains(READ_SESSION_EXPIRED_ERROR_MESSAGE);
}
return false;
}

static BigQueryException convertToBigQueryException(BigQueryError error) {
return new BigQueryException(UNKNOWN_CODE, error.getMessage(), error);
}
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ private static synchronized void initializeCache(long readSessionCacheDurationMi
if (!initialized) {
READ_SESSION_CACHE =
CacheBuilder.newBuilder()
.expireAfterWrite(readSessionCacheDurationMins, TimeUnit.MINUTES)
.expireAfterWrite(600, TimeUnit.MINUTES)
.maximumSize(1000)
.build();
initialized = true;
Original file line number Diff line number Diff line change
@@ -311,7 +311,19 @@ public void onError(Throwable t) {
newConnection(this, builder);
retries++;
} else {
stopWithError(t);
if (BigQueryUtil.isReadSessionExpired(t)) {
RuntimeException runtimeException =
new RuntimeException(
"Read session expired after 6 hours. Dataframes on BigQuery tables cannot be "
+ "operated on beyond this time-limit. Your options: "
+ "(1) try finishing within 6 hours, "
+ "(2) use Spark dataframe caching, "
+ "(3) read from a newly created BigQuery dataframe.",
t);
stopWithError(runtimeException);
} else {
stopWithError(t);
}
}
}

0 comments on commit 0f9dc6e

Please sign in to comment.