/
Subscriber.java
585 lines (532 loc) · 22.5 KB
/
Subscriber.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
* Copyright 2016 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.pubsub.v1;
import com.google.api.core.AbstractApiService;
import com.google.api.core.ApiClock;
import com.google.api.core.ApiFunction;
import com.google.api.core.ApiService;
import com.google.api.core.BetaApi;
import com.google.api.core.CurrentMillisClock;
import com.google.api.core.InternalApi;
import com.google.api.gax.batching.FlowControlSettings;
import com.google.api.gax.batching.FlowController;
import com.google.api.gax.batching.FlowController.LimitExceededBehavior;
import com.google.api.gax.core.BackgroundResource;
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.Distribution;
import com.google.api.gax.core.ExecutorAsBackgroundResource;
import com.google.api.gax.core.ExecutorProvider;
import com.google.api.gax.core.InstantiatingExecutorProvider;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.NoHeaderProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub;
import com.google.cloud.pubsub.v1.stub.SubscriberStub;
import com.google.cloud.pubsub.v1.stub.SubscriberStubSettings;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PubsubMessage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import org.threeten.bp.Duration;
/**
* A Cloud Pub/Sub <a href="https://cloud.google.com/pubsub/docs/subscriber">subscriber</a> that is
* associated with a specific subscription at creation.
*
* <p>A {@link Subscriber} allows you to provide an implementation of a {@link MessageReceiver
* receiver} to which messages are going to be delivered as soon as they are received by the
* subscriber. The delivered messages then can be {@link AckReplyConsumer#ack() acked} or {@link
* AckReplyConsumer#nack() nacked} at will as they get processed by the receiver. Nacking a messages
* implies a later redelivery of such message.
*
* <p>The subscriber handles the ack management, by automatically extending the ack deadline while
* the message is being processed, to then issue the ack or nack of such message when the processing
* is done (see {@link Builder#setMaxAckExtensionPeriod(Duration)}). <strong>Note:</strong> message
* redelivery is still possible.
*
* <p>It also provides customizable options that control:
*
* <ul>
* <li>Ack deadline extension: such as the amount of time ahead to trigger the extension of
* message acknowledgement expiration.
* <li>Flow control: such as the maximum outstanding messages or maximum outstanding bytes to keep
* in memory before the receiver either ack or nack them.
* </ul>
*
* <p>{@link Subscriber} will use the credentials set on the channel, which uses application default
* credentials through {@link GoogleCredentials#getApplicationDefault} by default.
*
* <p>{@code Subscriber} is implemented using <a
* href="http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/util/concurrent/Service.html">Guava's
* Service</a> and provides the same methods. See <a
* href="https://github.com/google/guava/wiki/ServiceExplained">Guava documentation</a> for more
* details.
*/
public class Subscriber extends AbstractApiService {
private static final int THREADS_PER_CHANNEL = 5;
private static final int MAX_INBOUND_MESSAGE_SIZE =
20 * 1024 * 1024; // 20MB API maximum message size.
@InternalApi static final int MAX_ACK_DEADLINE_SECONDS = 600;
@InternalApi static final int MIN_ACK_DEADLINE_SECONDS = 10;
private static final Duration UNARY_TIMEOUT = Duration.ofSeconds(60);
private static final Duration ACK_EXPIRATION_PADDING = Duration.ofSeconds(5);
private static final Logger logger = Logger.getLogger(Subscriber.class.getName());
private final String subscriptionName;
private final FlowControlSettings flowControlSettings;
private final Duration maxAckExtensionPeriod;
// The ExecutorProvider used to generate executors for processing messages.
private final ExecutorProvider executorProvider;
// An instantiation of the SystemExecutorProvider used for processing acks
// and other system actions.
@Nullable private final ScheduledExecutorService alarmsExecutor;
private final Distribution ackLatencyDistribution =
new Distribution(MAX_ACK_DEADLINE_SECONDS + 1);
private SubscriberStub subStub;
private final SubscriberStubSettings subStubSettings;
private final FlowController flowController;
private final int numPullers;
private final MessageReceiver receiver;
private final List<StreamingSubscriberConnection> streamingSubscriberConnections;
private final ApiClock clock;
private final List<BackgroundResource> backgroundResources = new ArrayList<>();
private Subscriber(Builder builder) {
receiver = builder.receiver;
flowControlSettings = builder.flowControlSettings;
subscriptionName = builder.subscriptionName;
maxAckExtensionPeriod = builder.maxAckExtensionPeriod;
clock = builder.clock.isPresent() ? builder.clock.get() : CurrentMillisClock.getDefaultClock();
flowController =
new FlowController(
builder
.flowControlSettings
.toBuilder()
.setLimitExceededBehavior(LimitExceededBehavior.Block)
.build());
this.numPullers = builder.parallelPullCount;
executorProvider = builder.executorProvider;
ExecutorProvider systemExecutorProvider = builder.systemExecutorProvider;
alarmsExecutor = systemExecutorProvider.getExecutor();
if (systemExecutorProvider.shouldAutoClose()) {
backgroundResources.add(new ExecutorAsBackgroundResource((alarmsExecutor)));
}
TransportChannelProvider channelProvider = builder.channelProvider;
if (channelProvider.acceptsPoolSize()) {
channelProvider = channelProvider.withPoolSize(numPullers);
}
try {
this.subStubSettings =
SubscriberStubSettings.newBuilder()
.setExecutorProvider(systemExecutorProvider)
.setCredentialsProvider(builder.credentialsProvider)
.setTransportChannelProvider(channelProvider)
.setHeaderProvider(builder.headerProvider)
.setEndpoint(builder.endpoint)
.applyToAllUnaryMethods(
new ApiFunction<UnaryCallSettings.Builder<?, ?>, Void>() {
@Override
public Void apply(UnaryCallSettings.Builder<?, ?> settingsBuilder) {
settingsBuilder.setSimpleTimeoutNoRetries(UNARY_TIMEOUT);
return null;
}
})
.build();
// TODO(pongad): what about internal header??
} catch (Exception e) {
throw new IllegalStateException(e);
}
streamingSubscriberConnections = new ArrayList<StreamingSubscriberConnection>(numPullers);
// We regularly look up the distribution for a good subscription deadline.
// So we seed the distribution with something reasonable to start with.
// Distribution is percentile-based, so this value will eventually lose importance.
ackLatencyDistribution.record(60);
}
/**
* Constructs a new {@link Builder}.
*
* @param subscription Cloud Pub/Sub subscription to bind the subscriber to
* @param receiver an implementation of {@link MessageReceiver} used to process the received
* messages
*/
public static Builder newBuilder(ProjectSubscriptionName subscription, MessageReceiver receiver) {
return newBuilder(subscription.toString(), receiver);
}
/**
* Constructs a new {@link Builder}.
*
* @param subscription Cloud Pub/Sub subscription to bind the subscriber to
* @param receiver an implementation of {@link MessageReceiver} used to process the received
* messages
*/
public static Builder newBuilder(String subscription, MessageReceiver receiver) {
return new Builder(subscription, receiver);
}
/** Returns the delivery attempt count for a received {@link PubsubMessage} */
public static Integer getDeliveryAttempt(PubsubMessage message) {
if (!message.containsAttributes("googclient_deliveryattempt")) {
return null;
}
return Integer.parseInt(message.getAttributesOrThrow("googclient_deliveryattempt"));
}
/** Subscription which the subscriber is subscribed to. */
public String getSubscriptionNameString() {
return subscriptionName;
}
/** The flow control settings the Subscriber is configured with. */
public FlowControlSettings getFlowControlSettings() {
return flowControlSettings;
}
/**
* Initiates service startup and returns immediately.
*
* <p>Example of receiving a specific number of messages.
*
* <pre>{@code
* Subscriber subscriber = Subscriber.newBuilder(subscription, receiver).build();
* subscriber.addListener(new Subscriber.Listener() {
* public void failed(Subscriber.State from, Throwable failure) {
* // Handle error.
* }
* }, executor);
* subscriber.startAsync();
*
* // Wait for a stop signal.
* // In a server, this might be a signal to stop serving.
* // In this example, the signal is just a dummy Future.
* //
* // By default, Subscriber uses daemon threads (see
* // https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html).
* // Consequently, once other threads have terminated, Subscriber will not stop the JVM from
* // exiting.
* // If the Subscriber should simply run forever, either use the setExecutorProvider method in
* // Subscriber.Builder
* // to use non-daemon threads or run
* // for (;;) {
* // Thread.sleep(Long.MAX_VALUE);
* // }
* // at the end of main() to previent the main thread from exiting.
* done.get();
* subscriber.stopAsync().awaitTerminated();
* }</pre>
*/
@Override
public ApiService startAsync() {
// Override only for the docs.
return super.startAsync();
}
@Override
protected void doStart() {
logger.log(Level.FINE, "Starting subscriber group.");
try {
this.subStub = GrpcSubscriberStub.create(subStubSettings);
} catch (IOException e) {
// doesn't matter what we throw, the Service will just catch it and fail to start.
throw new IllegalStateException(e);
}
// When started, connections submit tasks to the executor.
// These tasks must finish before the connections can declare themselves running.
// If we have a single-thread executor and call startStreamingConnections from the
// same executor, it will deadlock: the thread will be stuck waiting for connections
// to start but cannot start the connections.
// For this reason, we spawn a dedicated thread. Starting subscriber should be rare.
new Thread(
new Runnable() {
@Override
public void run() {
try {
startStreamingConnections();
notifyStarted();
} catch (Throwable t) {
notifyFailed(t);
}
}
})
.start();
}
@Override
protected void doStop() {
new Thread(
new Runnable() {
@Override
public void run() {
try {
// stop connection is no-op if connections haven't been started.
stopAllStreamingConnections();
shutdownBackgroundResources();
subStub.shutdownNow();
notifyStopped();
} catch (Exception e) {
notifyFailed(e);
}
}
})
.start();
}
private void startStreamingConnections() {
synchronized (streamingSubscriberConnections) {
for (int i = 0; i < numPullers; i++) {
final ScheduledExecutorService executor = executorProvider.getExecutor();
if (executorProvider.shouldAutoClose()) {
backgroundResources.add(new ExecutorAsBackgroundResource((executor)));
}
streamingSubscriberConnections.add(
new StreamingSubscriberConnection(
subscriptionName,
receiver,
ACK_EXPIRATION_PADDING,
maxAckExtensionPeriod,
ackLatencyDistribution,
subStub,
i,
flowController,
executor,
alarmsExecutor,
clock));
}
startConnections(
streamingSubscriberConnections,
new Listener() {
@Override
public void failed(State from, Throwable failure) {
// If a connection failed is because of a fatal error, we should fail the
// whole subscriber.
stopAllStreamingConnections();
shutdownBackgroundResources();
try {
notifyFailed(failure);
} catch (IllegalStateException e) {
if (isRunning()) {
throw e;
}
// It could happen that we are shutting down while some channels fail.
}
}
});
}
}
private void stopAllStreamingConnections() {
stopConnections(streamingSubscriberConnections);
}
private void shutdownBackgroundResources() {
for (BackgroundResource resource : backgroundResources) {
resource.shutdown();
}
}
private void startConnections(
List<? extends ApiService> connections, final ApiService.Listener connectionsListener) {
for (ApiService subscriber : connections) {
subscriber.addListener(connectionsListener, alarmsExecutor);
subscriber.startAsync();
}
for (ApiService subscriber : connections) {
subscriber.awaitRunning();
}
}
private void stopConnections(List<? extends ApiService> connections) {
ArrayList<ApiService> liveConnections;
synchronized (connections) {
liveConnections = new ArrayList<ApiService>(connections);
connections.clear();
}
for (ApiService subscriber : liveConnections) {
subscriber.stopAsync();
}
for (ApiService subscriber : liveConnections) {
try {
subscriber.awaitTerminated();
} catch (IllegalStateException e) {
// If the service fails, awaitTerminated will throw an exception.
// However, we could be stopping services because at least one
// has already failed, so we just ignore this exception.
}
}
}
/** Builder of {@link Subscriber Subscribers}. */
public static final class Builder {
private static final Duration DEFAULT_MAX_ACK_EXTENSION_PERIOD = Duration.ofMinutes(60);
private static final ExecutorProvider DEFAULT_EXECUTOR_PROVIDER =
InstantiatingExecutorProvider.newBuilder()
.setExecutorThreadCount(THREADS_PER_CHANNEL)
.build();
private static final AtomicInteger SYSTEM_EXECUTOR_COUNTER = new AtomicInteger();
private String subscriptionName;
private MessageReceiver receiver;
private Duration maxAckExtensionPeriod = DEFAULT_MAX_ACK_EXTENSION_PERIOD;
private FlowControlSettings flowControlSettings =
FlowControlSettings.newBuilder()
.setMaxOutstandingElementCount(1000L)
.setMaxOutstandingRequestBytes(100L * 1024L * 1024L) // 100MB
.build();
private ExecutorProvider executorProvider = DEFAULT_EXECUTOR_PROVIDER;
private ExecutorProvider systemExecutorProvider = null;
private TransportChannelProvider channelProvider =
SubscriptionAdminSettings.defaultGrpcTransportProviderBuilder()
.setMaxInboundMessageSize(MAX_INBOUND_MESSAGE_SIZE)
.setKeepAliveTime(Duration.ofMinutes(5))
.build();
private HeaderProvider headerProvider = new NoHeaderProvider();
private CredentialsProvider credentialsProvider =
SubscriptionAdminSettings.defaultCredentialsProviderBuilder().build();
private Optional<ApiClock> clock = Optional.absent();
private int parallelPullCount = 1;
private String endpoint = SubscriberStubSettings.getDefaultEndpoint();
Builder(String subscriptionName, MessageReceiver receiver) {
this.subscriptionName = subscriptionName;
this.receiver = receiver;
}
/**
* {@code ChannelProvider} to use to create Channels, which must point at Cloud Pub/Sub
* endpoint.
*
* <p>For performance, this client benefits from having multiple channels open at once. Users
* are encouraged to provide instances of {@code ChannelProvider} that creates new channels
* instead of returning pre-initialized ones.
*/
public Builder setChannelProvider(TransportChannelProvider channelProvider) {
this.channelProvider = Preconditions.checkNotNull(channelProvider);
return this;
}
/**
* Sets the static header provider. The header provider will be called during client
* construction only once. The headers returned by the provider will be cached and supplied as
* is for each request issued by the constructed client. Some reserved headers can be overridden
* (e.g. Content-Type) or merged with the default value (e.g. User-Agent) by the underlying
* transport layer.
*
* @param headerProvider the header provider
* @return the builder
*/
@BetaApi
public Builder setHeaderProvider(HeaderProvider headerProvider) {
this.headerProvider = Preconditions.checkNotNull(headerProvider);
return this;
}
/**
* Sets the flow control settings.
*
* <p>In the example below, the {@link Subscriber} will make sure that
*
* <ul>
* <li>there are at most ten thousand outstanding messages, and
* <li>the combined size of outstanding messages does not exceed 1GB.
* </ul>
*
* "Outstanding messages" here means the messages that have already been given to {@link
* MessageReceiver} but not yet {@code acked()} or {@code nacked()}.
*
* <pre>{@code
* FlowControlSettings flowControlSettings =
* FlowControlSettings.newBuilder()
* .setMaxOutstandingElementCount(10_000L)
* .setMaxOutstandingRequestBytes(1_000_000_000L)
* .build();
* Subscriber subscriber =
* Subscriber.newBuilder(subscriptionName, receiver)
* .setFlowControlSettings(flowControlSettings)
* .build();
* }</pre>
*/
public Builder setFlowControlSettings(FlowControlSettings flowControlSettings) {
this.flowControlSettings = Preconditions.checkNotNull(flowControlSettings);
return this;
}
/**
* Set the maximum period a message ack deadline will be extended. Defaults to one hour.
*
* <p>It is recommended to set this value to a reasonable upper bound of the subscriber time to
* process any message. This maximum period avoids messages to be <i>locked</i> by a subscriber
* in cases when the ack reply is lost.
*
* <p>A zero duration effectively disables auto deadline extensions.
*/
public Builder setMaxAckExtensionPeriod(Duration maxAckExtensionPeriod) {
Preconditions.checkArgument(maxAckExtensionPeriod.toMillis() >= 0);
this.maxAckExtensionPeriod = maxAckExtensionPeriod;
return this;
}
/**
* Gives the ability to set a custom executor. {@link ExecutorProvider#getExecutor()} will be
* called {@link Builder#parallelPullCount} times.
*/
public Builder setExecutorProvider(ExecutorProvider executorProvider) {
this.executorProvider = Preconditions.checkNotNull(executorProvider);
return this;
}
/** {@code CredentialsProvider} to use to create Credentials to authenticate calls. */
public Builder setCredentialsProvider(CredentialsProvider credentialsProvider) {
this.credentialsProvider = Preconditions.checkNotNull(credentialsProvider);
return this;
}
/**
* Gives the ability to set a custom executor for managing lease extensions. If none is provided
* a shared one will be used by all {@link Subscriber} instances.
*/
public Builder setSystemExecutorProvider(ExecutorProvider executorProvider) {
this.systemExecutorProvider = Preconditions.checkNotNull(executorProvider);
return this;
}
/** Sets the number of pullers used to pull messages from the subscription. Defaults to one. */
public Builder setParallelPullCount(int parallelPullCount) {
this.parallelPullCount = parallelPullCount;
return this;
}
/** Gives the ability to override the gRPC endpoint. */
public Builder setEndpoint(String endpoint) {
this.endpoint = endpoint;
return this;
}
/** Gives the ability to set a custom clock. */
Builder setClock(ApiClock clock) {
this.clock = Optional.of(clock);
return this;
}
public Subscriber build() {
if (systemExecutorProvider == null) {
ThreadFactory threadFactory =
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("Subscriber-SE-" + SYSTEM_EXECUTOR_COUNTER.incrementAndGet() + "-%d")
.build();
int threadCount = Math.max(6, 2 * parallelPullCount);
final ScheduledExecutorService executor =
Executors.newScheduledThreadPool(threadCount, threadFactory);
systemExecutorProvider =
new ExecutorProvider() {
@Override
public boolean shouldAutoClose() {
return true;
}
@Override
public ScheduledExecutorService getExecutor() {
return executor;
}
};
}
return new Subscriber(this);
}
}
}