如何增加Firestore SDK查询流的超时

时间:2019-10-28 07:31:43

标签: java google-cloud-firestore

我正在尝试基于GCP Firestore构建应用程序,因此我正在使用google-cloud-firestore库。我想通过Query的流API来流查询结果,但在约60秒后得到StatusRuntimeException。似乎操作已超时。我在哪里可以增加此超时时间?

我最后要做的是使用Flux构建流,该流根据查询从Firestore中流出大量数据。

我只是想通过FirestoreOptions找到增加超时的可能性,但没有找到可行的解决方案。我看到的是,在流初始化期间某处已设置了60s的rpcTimeout。但是我不太确定这是正确的选择。另外我也找不到设置它的位置。

我正在使用的方法:

com.google.cloud.firestore.Query
Query.stream(@Nonnull final ApiStreamObserver<DocumentSnapshot> responseObserver)

约60秒后出现异常:

com.google.api.gax.rpc.UnavailableException: io.grpc.StatusRuntimeException: UNAVAILABLE: The datastore operation timed out, or the data was temporarily unavailable.
    at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:69)
    at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:72)
    at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:60)
...

已编辑

仅发现,通常可以覆盖超时的默认设置,但不能覆盖流方法。如FirestoreSettings中的相应方法所述:

/**
* Applies the given settings updater function to all of the unary API methods in this service.
*
* <p>Note: This method does not support applying settings to streaming methods.
*/

真令人讨厌,因为我无意基于基于游标的分页来创建流。

1 个答案:

答案 0 :(得分:0)

我发现控制超时的唯一方法是:

public List<QueryDocumentSnapshot> getAllDocs() {
    try {
        ApiFuture<QuerySnapshot> querySnapshot = db().collection("MyCollection").get();
        return querySnapshot.get(5, TimeUnit.MINUTES).getDocuments();
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        e.printStackTrace();
    }
    return null;
}