forked from googleapis/java-bigtable-hbase
/
RetryingMutateRowsOperation.java
138 lines (123 loc) · 4.84 KB
/
RetryingMutateRowsOperation.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright 2017 Google Inc. All Rights Reserved.
*
* 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
*
* http://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.grpc.async;
import com.google.api.core.ApiClock;
import com.google.api.core.InternalApi;
import com.google.bigtable.v2.MutateRowsRequest;
import com.google.bigtable.v2.MutateRowsResponse;
import com.google.cloud.bigtable.config.Logger;
import com.google.cloud.bigtable.config.RetryOptions;
import com.google.cloud.bigtable.grpc.BigtableDataClient;
import com.google.cloud.bigtable.grpc.DeadlineGenerator;
import com.google.cloud.bigtable.grpc.async.MutateRowsRequestManager.ProcessingStatus;
import com.google.common.collect.ImmutableMap;
import io.grpc.Metadata;
import io.opencensus.trace.AttributeValue;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
/**
* Performs retries for {@link BigtableDataClient#mutateRows(MutateRowsRequest)} operations.
*
* <p>For internal use only - public for technical reasons.
*/
@InternalApi("For internal usage only")
public class RetryingMutateRowsOperation
extends AbstractRetryingOperation<
MutateRowsRequest, MutateRowsResponse, List<MutateRowsResponse>> {
protected static final Logger LOG = new Logger(RetryingMutateRowsOperation.class);
private static final io.grpc.Status INVALID_RESPONSE =
io.grpc.Status.INTERNAL.withDescription("The server returned an invalid response");
private final MutateRowsRequestManager requestManager;
public RetryingMutateRowsOperation(
RetryOptions retryOptions,
MutateRowsRequest originalRequest,
BigtableAsyncRpc<MutateRowsRequest, MutateRowsResponse> retryableRpc,
DeadlineGenerator deadlineGenerator,
ScheduledExecutorService retryExecutorService,
Metadata originalMetadata,
ApiClock clock) {
super(
retryOptions,
originalRequest,
retryableRpc,
deadlineGenerator,
retryExecutorService,
originalMetadata,
clock);
requestManager = new MutateRowsRequestManager(retryOptions, originalRequest);
operationSpan.addAnnotation(
"MutationCount",
ImmutableMap.of(
"count", AttributeValue.longAttributeValue(originalRequest.getEntriesCount())));
}
@Override
public void onMessage(MutateRowsResponse message) {
try {
requestManager.onMessage(message);
} catch (Exception e) {
setException(e);
}
}
@Override
protected MutateRowsRequest getRetryRequest() {
return requestManager.getRetryRequest();
}
@Override
protected boolean onOK(Metadata trailers) {
ProcessingStatus status = requestManager.onOK();
if (status == ProcessingStatus.INVALID) {
LOG.error("BulkMutateRows was invalid, final state: " + requestManager.getResultString());
// Set an exception.
onError(INVALID_RESPONSE, trailers);
return true;
}
// There was a problem in the data found in onMessage(), so fail the RPC.
if (status == ProcessingStatus.SUCCESS || status == ProcessingStatus.NOT_RETRYABLE) {
// Set the response, with either success, or non-retryable responses.
completionFuture.set(Arrays.asList(requestManager.buildResponse()));
if (status != ProcessingStatus.SUCCESS) {
LOG.error(
"BulkMutateRows partially failed with nonretryable errors, final state: "
+ requestManager.getResultString());
}
return true;
}
// Perform a partial retry, if the backoff policy allows it.
Long nextBackOff = getNextBackoff();
if (nextBackOff == null) {
// Return the response as is, and don't retry;
rpc.getRpcMetrics().markRetriesExhasted();
completionFuture.set(Arrays.asList(requestManager.buildResponse()));
operationSpan.addAnnotation(
"MutationCount",
ImmutableMap.of(
"failureCount",
AttributeValue.longAttributeValue(
requestManager.getRetryRequest().getEntriesCount())));
LOG.error(
"BulkMutateRows exhausted retries, final state: " + requestManager.getResultString());
return true;
}
performRetry(nextBackOff);
operationSpan.addAnnotation(
"MutationCount",
ImmutableMap.of(
"retryCount",
AttributeValue.longAttributeValue(requestManager.getRetryRequest().getEntriesCount())));
return false;
}
}