Search in sources :

Example 6 with SubscribeToShardRequest

use of software.amazon.awssdk.services.kinesis.model.SubscribeToShardRequest in project flink by apache.

the class FanOutShardSubscriber method openSubscriptionToShard.

/**
 * Calls {@link KinesisProxyV2#subscribeToShard} and waits to acquire a subscription. In the
 * event a non-recoverable error occurs this method will rethrow the exception. Once the
 * subscription is acquired the client signals to the producer that we are ready to receive
 * records.
 *
 * @param startingPosition the position in which to start consuming from
 * @throws FanOutSubscriberException when an exception is propagated from the networking stack
 */
private FanOutShardSubscription openSubscriptionToShard(final StartingPosition startingPosition) throws FanOutSubscriberException, InterruptedException {
    SubscribeToShardRequest request = SubscribeToShardRequest.builder().consumerARN(consumerArn).shardId(shardId).startingPosition(startingPosition).build();
    AtomicReference<Throwable> exception = new AtomicReference<>();
    CountDownLatch waitForSubscriptionLatch = new CountDownLatch(1);
    FanOutShardSubscription subscription = new FanOutShardSubscription(waitForSubscriptionLatch);
    SubscribeToShardResponseHandler responseHandler = SubscribeToShardResponseHandler.builder().onError(e -> {
        // (errors are ignored here once the subscription is open)
        if (waitForSubscriptionLatch.getCount() > 0) {
            exception.set(e);
            waitForSubscriptionLatch.countDown();
        }
    }).subscriber(() -> subscription).build();
    kinesis.subscribeToShard(request, responseHandler);
    boolean subscriptionEstablished = waitForSubscriptionLatch.await(subscribeToShardTimeout.toMillis(), TimeUnit.MILLISECONDS);
    if (!subscriptionEstablished) {
        final String errorMessage = "Timed out acquiring subscription - " + shardId + " (" + consumerArn + ")";
        LOG.error(errorMessage);
        subscription.cancelSubscription();
        handleError(new RecoverableFanOutSubscriberException(new TimeoutException(errorMessage)));
    }
    Throwable throwable = exception.get();
    if (throwable != null) {
        handleError(throwable);
    }
    LOG.debug("Acquired subscription - {} ({})", shardId, consumerArn);
    // Request the first record to kick off consumption
    // Following requests are made by the FanOutShardSubscription on the netty thread
    subscription.requestRecord();
    return subscription;
}
Also used : SubscribeToShardResponseHandler(software.amazon.awssdk.services.kinesis.model.SubscribeToShardResponseHandler) SubscribeToShardRequest(software.amazon.awssdk.services.kinesis.model.SubscribeToShardRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) ReadTimeoutException(io.netty.handler.timeout.ReadTimeoutException)

Aggregations

KinesisAsyncClient (software.amazon.awssdk.services.kinesis.KinesisAsyncClient)5 SubscribeToShardRequest (software.amazon.awssdk.services.kinesis.model.SubscribeToShardRequest)5 CompletableFuture (java.util.concurrent.CompletableFuture)3 SubscribeToShardResponseHandler (software.amazon.awssdk.services.kinesis.model.SubscribeToShardResponseHandler)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Subscriber (org.reactivestreams.Subscriber)2 Subscription (org.reactivestreams.Subscription)2 SdkPublisher (software.amazon.awssdk.core.async.SdkPublisher)2 Region (software.amazon.awssdk.regions.Region)2 ShardIteratorType (software.amazon.awssdk.services.kinesis.model.ShardIteratorType)2 SubscribeToShardEvent (software.amazon.awssdk.services.kinesis.model.SubscribeToShardEvent)2 SubscribeToShardEventStream (software.amazon.awssdk.services.kinesis.model.SubscribeToShardEventStream)2 SubscribeToShardResponse (software.amazon.awssdk.services.kinesis.model.SubscribeToShardResponse)2 ReadTimeoutException (io.netty.handler.timeout.ReadTimeoutException)1 Collections.emptyList (java.util.Collections.emptyList)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 DEREGISTER_STREAM_BACKOFF_BASE (org.apache.flink.streaming.connectors.kinesis.config.ConsumerConfigConstants.DEREGISTER_STREAM_BACKOFF_BASE)1