/
BulkWriterOperation.java
162 lines (141 loc) · 5.62 KB
/
BulkWriterOperation.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* 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
*
* 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.firestore;
import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
import com.google.api.core.SettableApiFuture;
import com.google.common.util.concurrent.MoreExecutors;
import io.grpc.Status;
/**
* Represents a single write for BulkWriter, encapsulating operation dispatch and error handling.
*/
class BulkWriterOperation {
private final SettableApiFuture<WriteResult> operationFuture = SettableApiFuture.create();
private final DocumentReference documentReference;
private final BulkWriter.OperationType operationType;
private final ApiFunction<BulkWriterOperation, Void> scheduleWriteCallback;
private final ApiFunction<WriteResult, ApiFuture<Void>> successListener;
private final ApiFunction<BulkWriterException, ApiFuture<Boolean>> errorListener;
/**
* The default initial backoff time in milliseconds after an error. Set to 1s according to
* https://cloud.google.com/apis/design/errors.
*/
public static final int DEFAULT_BACKOFF_INITIAL_DELAY_MS = 1000;
/** The default maximum backoff time in milliseconds when retrying an operation. */
public static final int DEFAULT_BACKOFF_MAX_DELAY_MS = 60 * 1000;
/** The default factor to increase the backup by after each failed attempt. */
public static final double DEFAULT_BACKOFF_FACTOR = 1.5;
private int failedAttempts = 0;
private Status lastStatus;
private int backoffDuration = 0;
/**
* @param documentReference The document reference being written to.
* @param operationType The type of operation that created this write.
* @param scheduleWriteCallback The callback used to schedule a new write.
* @param successListener The user-provided success handler.
* @param errorListener The user-provided error handler.
*/
BulkWriterOperation(
DocumentReference documentReference,
BulkWriter.OperationType operationType,
ApiFunction<BulkWriterOperation, Void> scheduleWriteCallback,
ApiFunction<WriteResult, ApiFuture<Void>> successListener,
ApiFunction<BulkWriterException, ApiFuture<Boolean>> errorListener) {
this.documentReference = documentReference;
this.operationType = operationType;
this.scheduleWriteCallback = scheduleWriteCallback;
this.successListener = successListener;
this.errorListener = errorListener;
}
/**
* Returns an ApiFuture that resolves when the operation completes (either successfully or failed
* after all retry attempts are exhausted).
*/
public ApiFuture<WriteResult> getFuture() {
return operationFuture;
}
public DocumentReference getDocumentReference() {
return documentReference;
}
public int getBackoffDuration() {
return backoffDuration;
}
/** Callback invoked when an operation attempt fails. */
public ApiFuture<Void> onException(FirestoreException exception) {
++failedAttempts;
final BulkWriterException bulkWriterException =
new BulkWriterException(
exception.getStatus(),
exception.getMessage(),
documentReference,
operationType,
failedAttempts);
final SettableApiFuture<Void> callbackFuture = SettableApiFuture.create();
ApiFutures.addCallback(
this.errorListener.apply(bulkWriterException),
new ApiFutureCallback<Boolean>() {
@Override
public void onFailure(Throwable throwable) {
operationFuture.setException(throwable);
callbackFuture.set(null);
}
@Override
public void onSuccess(Boolean shouldRetry) {
if (shouldRetry) {
lastStatus = bulkWriterException.getStatus();
updateBackoffDuration();
scheduleWriteCallback.apply(BulkWriterOperation.this);
} else {
operationFuture.setException(bulkWriterException);
}
callbackFuture.set(null);
}
},
MoreExecutors.directExecutor());
return callbackFuture;
}
private void updateBackoffDuration() {
if (lastStatus == Status.RESOURCE_EXHAUSTED) {
backoffDuration = DEFAULT_BACKOFF_MAX_DELAY_MS;
} else if (backoffDuration == 0) {
backoffDuration = DEFAULT_BACKOFF_INITIAL_DELAY_MS;
} else {
backoffDuration *= DEFAULT_BACKOFF_FACTOR;
}
}
/** Callback invoked when the operation succeeds. */
public ApiFuture<Void> onSuccess(final WriteResult result) {
final SettableApiFuture<Void> callbackFuture = SettableApiFuture.create();
ApiFutures.addCallback(
this.successListener.apply(result),
new ApiFutureCallback<Void>() {
@Override
public void onFailure(Throwable throwable) {
operationFuture.setException(throwable);
callbackFuture.set(null);
}
@Override
public void onSuccess(Void aVoid) {
operationFuture.set(result);
callbackFuture.set(null);
}
},
MoreExecutors.directExecutor());
return callbackFuture;
}
}