/
PublisherImpl.java
executable file
·303 lines (277 loc) · 11 KB
/
PublisherImpl.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Copyright 2020 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.pubsublite.internal.wire;
import static com.google.cloud.pubsublite.internal.CheckedApiPreconditions.checkState;
import static com.google.cloud.pubsublite.internal.wire.ApiServiceUtils.backgroundResourceAsApiService;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.core.ApiService;
import com.google.api.core.SettableApiFuture;
import com.google.api.gax.batching.BatchingSettings;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.cloud.pubsublite.Constants;
import com.google.cloud.pubsublite.Message;
import com.google.cloud.pubsublite.Offset;
import com.google.cloud.pubsublite.internal.CheckedApiException;
import com.google.cloud.pubsublite.internal.CloseableMonitor;
import com.google.cloud.pubsublite.internal.ProxyService;
import com.google.cloud.pubsublite.internal.Publisher;
import com.google.cloud.pubsublite.internal.wire.SerialBatcher.UnbatchedMessage;
import com.google.cloud.pubsublite.proto.InitialPublishRequest;
import com.google.cloud.pubsublite.proto.PubSubMessage;
import com.google.cloud.pubsublite.proto.PublishRequest;
import com.google.cloud.pubsublite.proto.PublishResponse;
import com.google.cloud.pubsublite.v1.PublisherServiceClient;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.Monitor;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.concurrent.GuardedBy;
public final class PublisherImpl extends ProxyService
implements Publisher<Offset>, RetryingConnectionObserver<Offset> {
@GuardedBy("monitor.monitor")
private final RetryingConnection<BatchPublisher> connection;
private final ScheduledExecutorService executorService;
private final BatchingSettings batchingSettings;
private Future<?> alarmFuture;
private final CloseableMonitor monitor = new CloseableMonitor();
private final Monitor.Guard noneInFlight =
new Monitor.Guard(monitor.monitor) {
@Override
public boolean isSatisfied() {
return batchesInFlight.isEmpty() || shutdown;
}
};
@GuardedBy("monitor.monitor")
private boolean shutdown = false;
@GuardedBy("monitor.monitor")
private Optional<Offset> lastSentOffset = Optional.empty();
@GuardedBy("monitor.monitor")
private final SerialBatcher batcher;
private static class InFlightBatch {
final List<PubSubMessage> messages;
final List<SettableApiFuture<Offset>> messageFutures;
InFlightBatch(Collection<UnbatchedMessage> toBatch) {
messages = toBatch.stream().map(UnbatchedMessage::message).collect(Collectors.toList());
messageFutures = toBatch.stream().map(UnbatchedMessage::future).collect(Collectors.toList());
}
}
// An ordered list of batches in flight.
@GuardedBy("monitor.monitor")
private final Queue<InFlightBatch> batchesInFlight = new ArrayDeque<>();
@VisibleForTesting
PublisherImpl(
StreamFactory<PublishRequest, PublishResponse> streamFactory,
BatchPublisherFactory publisherFactory,
InitialPublishRequest initialRequest,
BatchingSettings batchingSettings)
throws ApiException {
Preconditions.checkNotNull(batchingSettings.getDelayThreshold());
Preconditions.checkNotNull(batchingSettings.getRequestByteThreshold());
Preconditions.checkNotNull(batchingSettings.getElementCountThreshold());
this.connection =
new RetryingConnectionImpl<>(
streamFactory,
publisherFactory,
PublishRequest.newBuilder().setInitialRequest(initialRequest).build(),
this);
this.batcher =
new SerialBatcher(
batchingSettings.getRequestByteThreshold(),
batchingSettings.getElementCountThreshold());
this.executorService = Executors.newSingleThreadScheduledExecutor();
this.batchingSettings = batchingSettings;
addServices(connection);
}
public PublisherImpl(
PublisherServiceClient client,
InitialPublishRequest initialRequest,
BatchingSettings batchingSettings)
throws ApiException {
this(
responseStream -> client.publishCallable().splitCall(responseStream),
new BatchPublisherImpl.Factory(),
initialRequest,
batchingSettings);
addServices(backgroundResourceAsApiService(client));
}
@GuardedBy("monitor.monitor")
private void rebatchForRestart() {
Queue<UnbatchedMessage> messages = new ArrayDeque<>();
for (InFlightBatch batch : batchesInFlight) {
for (int i = 0; i < batch.messages.size(); ++i) {
messages.add(UnbatchedMessage.of(batch.messages.get(i), batch.messageFutures.get(i)));
}
}
long size = 0;
int count = 0;
Queue<UnbatchedMessage> currentBatch = new ArrayDeque<>();
batchesInFlight.clear();
for (UnbatchedMessage message : messages) {
long messageSize = message.message().getSerializedSize();
if (size + messageSize > Constants.MAX_PUBLISH_BATCH_BYTES
|| count + 1 > Constants.MAX_PUBLISH_BATCH_COUNT) {
if (!currentBatch.isEmpty()) {
batchesInFlight.add(new InFlightBatch(currentBatch));
currentBatch = new ArrayDeque<>();
count = 0;
size = 0;
}
}
currentBatch.add(message);
size = size + messageSize;
count += 1;
}
if (!currentBatch.isEmpty()) {
batchesInFlight.add(new InFlightBatch(currentBatch));
}
}
@Override
public void triggerReinitialize() {
try (CloseableMonitor.Hold h = monitor.enter()) {
connection.reinitialize();
rebatchForRestart();
Collection<InFlightBatch> batches = batchesInFlight;
connection.modifyConnection(
connectionOr -> {
if (!connectionOr.isPresent()) return;
batches.forEach(batch -> connectionOr.get().publish(batch.messages));
});
} catch (CheckedApiException e) {
onPermanentError(e);
}
}
@Override
protected void handlePermanentError(CheckedApiException error) {
try (CloseableMonitor.Hold h = monitor.enter()) {
shutdown = true;
terminateOutstandingPublishes(error);
}
}
@Override
protected void start() {
try (CloseableMonitor.Hold h = monitor.enter()) {
// After initialize, the stream can have an error and try to cancel the future, but the
// future can call into the stream, so this needs to be created under lock.
this.alarmFuture =
this.executorService.scheduleWithFixedDelay(
this::flushToStream,
batchingSettings.getDelayThreshold().toNanos(),
batchingSettings.getDelayThreshold().toNanos(),
TimeUnit.NANOSECONDS);
}
}
@Override
protected void stop() {
alarmFuture.cancel(false /* mayInterruptIfRunning */);
executorService.shutdown();
flush(); // Flush any outstanding messages that were batched.
try (CloseableMonitor.Hold h = monitor.enter()) {
shutdown = true;
}
flush(); // Flush again in case messages were added since shutdown was set.
}
@GuardedBy("monitor.monitor")
private void processBatch(Collection<UnbatchedMessage> batch) throws CheckedApiException {
if (batch.isEmpty()) return;
InFlightBatch inFlightBatch = new InFlightBatch(batch);
batchesInFlight.add(inFlightBatch);
connection.modifyConnection(
connectionOr -> {
checkState(connectionOr.isPresent(), "Published after the stream shut down.");
connectionOr.get().publish(inFlightBatch.messages);
});
}
@GuardedBy("monitor.monitor")
private void terminateOutstandingPublishes(CheckedApiException e) {
batchesInFlight.forEach(
batch -> batch.messageFutures.forEach(future -> future.setException(e)));
batcher.flush().forEach(m -> m.future().setException(e));
batchesInFlight.clear();
}
@Override
public ApiFuture<Offset> publish(Message message) {
PubSubMessage proto = message.toProto();
try (CloseableMonitor.Hold h = monitor.enter()) {
ApiService.State currentState = state();
checkState(
currentState == ApiService.State.RUNNING,
String.format("Cannot publish when Publisher state is %s.", currentState.name()));
checkState(!shutdown, "Published after the stream shut down.");
ApiFuture<Offset> messageFuture = batcher.add(proto);
if (batcher.shouldFlush()) {
processBatch(batcher.flush());
}
return messageFuture;
} catch (CheckedApiException e) {
onPermanentError(e);
return ApiFutures.immediateFailedFuture(e);
}
}
@Override
public void cancelOutstandingPublishes() {
try (CloseableMonitor.Hold h = monitor.enter()) {
terminateOutstandingPublishes(
new CheckedApiException("Cancelled by client.", Code.CANCELLED));
}
}
@VisibleForTesting
void flushToStream() {
try (CloseableMonitor.Hold h = monitor.enter()) {
if (shutdown) return;
processBatch(batcher.flush());
} catch (CheckedApiException e) {
onPermanentError(e);
}
}
// Flushable implementation
@Override
public void flush() {
flushToStream();
try (CloseableMonitor.Hold h = monitor.enterWhenUninterruptibly(noneInFlight)) {}
}
@Override
public void onClientResponse(Offset value) throws CheckedApiException {
try (CloseableMonitor.Hold h = monitor.enter()) {
checkState(
!batchesInFlight.isEmpty(), "Received publish response with no batches in flight.");
if (lastSentOffset.isPresent() && lastSentOffset.get().value() >= value.value()) {
throw new CheckedApiException(
String.format(
"Received publish response with offset %s that is inconsistent with previous"
+ " responses max %s",
value, lastSentOffset.get()),
Code.FAILED_PRECONDITION);
}
InFlightBatch batch = batchesInFlight.remove();
lastSentOffset = Optional.of(Offset.of(value.value() + batch.messages.size() - 1));
for (int i = 0; i < batch.messageFutures.size(); i++) {
Offset offset = Offset.of(value.value() + i);
batch.messageFutures.get(i).set(offset);
}
}
}
}