/
PointReadTimeoutCallable.java
86 lines (78 loc) · 2.91 KB
/
PointReadTimeoutCallable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.data.v2.stub.readrows;
import com.google.api.core.InternalApi;
import com.google.api.gax.rpc.ApiCallContext;
import com.google.api.gax.rpc.ResponseObserver;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.bigtable.v2.ReadRowsRequest;
import javax.annotation.Nullable;
import org.threeten.bp.Duration;
/**
* Specialization of ReadRows streams for point reads.
*
* <p>Under normal circumstances, the ReadRows RPC can't make any assumptions about deadlines. In
* general case the end user can be issuing a full table scan. However, when dealing with point
* reads, the client can make assumptions and promote the per row timeout to be a per attempt
* timeout.
*
* <p>This callable will check if the request is a point read and promote the timeout to be a
* deadline.
*/
@InternalApi
public class PointReadTimeoutCallable<RespT>
extends ServerStreamingCallable<ReadRowsRequest, RespT> {
private final ServerStreamingCallable<ReadRowsRequest, RespT> inner;
public PointReadTimeoutCallable(ServerStreamingCallable<ReadRowsRequest, RespT> inner) {
this.inner = inner;
}
@Override
public void call(ReadRowsRequest request, ResponseObserver<RespT> observer, ApiCallContext ctx) {
if (isPointRead(request)) {
Duration effectiveTimeout = getEffectivePointReadTimeout(ctx);
if (effectiveTimeout != null) {
ctx = ctx.withTimeout(effectiveTimeout);
}
}
inner.call(request, observer, ctx);
}
private boolean isPointRead(ReadRowsRequest request) {
if (request.getRowsLimit() == 1) {
return true;
}
if (!request.getRows().getRowRangesList().isEmpty()) {
return false;
}
return request.getRows().getRowKeysCount() == 1;
}
/**
* Extracts the effective timeout for a point read.
*
* <p>The effective time is the minimum of a streamWaitTimeout and a user set attempt timeout.
*/
@Nullable
private Duration getEffectivePointReadTimeout(ApiCallContext ctx) {
Duration streamWaitTimeout = ctx.getStreamWaitTimeout();
Duration attemptTimeout = ctx.getTimeout();
if (streamWaitTimeout == null) {
return attemptTimeout;
}
if (attemptTimeout == null) {
return streamWaitTimeout;
}
return (attemptTimeout.compareTo(streamWaitTimeout) <= 0) ? attemptTimeout : streamWaitTimeout;
}
}