Search in sources :

Example 1 with LimitExceededException

use of com.amazonaws.services.kinesis.model.LimitExceededException in project flink by apache.

the class KinesisProxy method describeStream.

/**
 * Get metainfo for a Kinesis stream, which contains information about which shards this Kinesis
 * stream possess.
 *
 * <p>This method is using a "full jitter" approach described in AWS's article, <a
 * href="https://www.awsarchitectureblog.com/2015/03/backoff.html">"Exponential Backoff and
 * Jitter"</a>. This is necessary because concurrent calls will be made by all parallel
 * subtask's fetcher. This jitter backoff approach will help distribute calls across the
 * fetchers over time.
 *
 * @param streamName the stream to describe
 * @param startShardId which shard to start with for this describe operation
 * @return the result of the describe stream operation
 */
protected DescribeStreamResult describeStream(String streamName, @Nullable String startShardId) throws InterruptedException {
    final DescribeStreamRequest describeStreamRequest = new DescribeStreamRequest();
    describeStreamRequest.setStreamName(streamName);
    describeStreamRequest.setExclusiveStartShardId(startShardId);
    DescribeStreamResult describeStreamResult = null;
    // Call DescribeStream, with full-jitter backoff (if we get LimitExceededException).
    int attemptCount = 0;
    while (describeStreamResult == null) {
        // retry until we get a result
        try {
            describeStreamResult = kinesisClient.describeStream(describeStreamRequest);
        } catch (LimitExceededException le) {
            long backoffMillis = BACKOFF.calculateFullJitterBackoff(describeStreamBaseBackoffMillis, describeStreamMaxBackoffMillis, describeStreamExpConstant, attemptCount++);
            LOG.warn(String.format("Got LimitExceededException when describing stream %s. " + "Backing off for %d millis.", streamName, backoffMillis));
            BACKOFF.sleep(backoffMillis);
        } catch (ResourceNotFoundException re) {
            throw new RuntimeException("Error while getting stream details", re);
        }
    }
    String streamStatus = describeStreamResult.getStreamDescription().getStreamStatus();
    if (!(streamStatus.equals(StreamStatus.ACTIVE.toString()) || streamStatus.equals(StreamStatus.UPDATING.toString()))) {
        if (LOG.isWarnEnabled()) {
            LOG.warn(String.format("The status of stream %s is %s ; result of the current " + "describeStream operation will not contain any shard information.", streamName, streamStatus));
        }
    }
    return describeStreamResult;
}
Also used : LimitExceededException(com.amazonaws.services.kinesis.model.LimitExceededException) DescribeStreamRequest(com.amazonaws.services.kinesis.model.DescribeStreamRequest) DescribeStreamResult(com.amazonaws.services.kinesis.model.DescribeStreamResult) ResourceNotFoundException(com.amazonaws.services.kinesis.model.ResourceNotFoundException)

Example 2 with LimitExceededException

use of com.amazonaws.services.kinesis.model.LimitExceededException in project flink by apache.

the class KinesisProxy method listShards.

/**
 * Get metainfo for a Kinesis stream, which contains information about which shards this Kinesis
 * stream possess.
 *
 * <p>This method is using a "full jitter" approach described in AWS's article, <a
 * href="https://www.awsarchitectureblog.com/2015/03/backoff.html">"Exponential Backoff and
 * Jitter"</a>. This is necessary because concurrent calls will be made by all parallel
 * subtask's fetcher. This jitter backoff approach will help distribute calls across the
 * fetchers over time.
 *
 * @param streamName the stream to describe
 * @param startShardId which shard to start with for this describe operation (earlier shard's
 *     infos will not appear in result)
 * @return the result of the describe stream operation
 */
private ListShardsResult listShards(String streamName, @Nullable String startShardId, @Nullable String startNextToken) throws InterruptedException {
    final ListShardsRequest listShardsRequest = new ListShardsRequest();
    if (startNextToken == null) {
        listShardsRequest.setExclusiveStartShardId(startShardId);
        listShardsRequest.setStreamName(streamName);
    } else {
        // Note the nextToken returned by AWS expires within 300 sec.
        listShardsRequest.setNextToken(startNextToken);
    }
    ListShardsResult listShardsResults = null;
    // Call ListShards, with full-jitter backoff (if we get LimitExceededException).
    int retryCount = 0;
    // are taken up.
    while (retryCount <= listShardsMaxRetries && listShardsResults == null) {
        // retry until we get a result
        try {
            listShardsResults = kinesisClient.listShards(listShardsRequest);
        } catch (LimitExceededException le) {
            long backoffMillis = BACKOFF.calculateFullJitterBackoff(listShardsBaseBackoffMillis, listShardsMaxBackoffMillis, listShardsExpConstant, retryCount++);
            LOG.warn("Got LimitExceededException when listing shards from stream " + streamName + ". Backing off for " + backoffMillis + " millis.");
            BACKOFF.sleep(backoffMillis);
        } catch (ResourceInUseException reInUse) {
            if (LOG.isWarnEnabled()) {
                // List Shards will throw an exception if stream in not in active state. Return
                // and re-use previous state available.
                LOG.info("The stream is currently not in active state. Reusing the older state " + "for the time being");
                break;
            }
        } catch (ResourceNotFoundException reNotFound) {
            throw new RuntimeException("Stream not found. Error while getting shard list.", reNotFound);
        } catch (InvalidArgumentException inArg) {
            throw new RuntimeException("Invalid Arguments to listShards.", inArg);
        } catch (ExpiredNextTokenException expiredToken) {
            LOG.warn("List Shards has an expired token. Reusing the previous state.");
            break;
        } catch (SdkClientException ex) {
            if (retryCount < listShardsMaxRetries && isRecoverableSdkClientException(ex)) {
                long backoffMillis = BACKOFF.calculateFullJitterBackoff(listShardsBaseBackoffMillis, listShardsMaxBackoffMillis, listShardsExpConstant, retryCount++);
                LOG.warn("Got SdkClientException when listing shards from stream {}. Backing off for {} millis.", streamName, backoffMillis);
                BACKOFF.sleep(backoffMillis);
            } else {
                // (otherwise would return null result and keep trying forever)
                throw ex;
            }
        }
    }
    // https://github.com/lyft/kinesalite/pull/4
    if (startShardId != null && listShardsResults != null) {
        List<Shard> shards = listShardsResults.getShards();
        shards.removeIf(shard -> StreamShardHandle.compareShardIds(shard.getShardId(), startShardId) <= 0);
    }
    return listShardsResults;
}
Also used : ListShardsResult(com.amazonaws.services.kinesis.model.ListShardsResult) ListShardsRequest(com.amazonaws.services.kinesis.model.ListShardsRequest) InvalidArgumentException(com.amazonaws.services.kinesis.model.InvalidArgumentException) SdkClientException(com.amazonaws.SdkClientException) ResourceInUseException(com.amazonaws.services.kinesis.model.ResourceInUseException) ExpiredNextTokenException(com.amazonaws.services.kinesis.model.ExpiredNextTokenException) LimitExceededException(com.amazonaws.services.kinesis.model.LimitExceededException) ResourceNotFoundException(com.amazonaws.services.kinesis.model.ResourceNotFoundException) Shard(com.amazonaws.services.kinesis.model.Shard)

Example 3 with LimitExceededException

use of com.amazonaws.services.kinesis.model.LimitExceededException in project beam by apache.

the class AmazonKinesisMock method listShards.

@Override
public ListShardsResult listShards(ListShardsRequest listShardsRequest) {
    if (expectedListShardsLimitExceededException) {
        throw new LimitExceededException("ListShards rate limit exceeded");
    }
    ListShardsResult result = new ListShardsResult();
    List<Shard> shards = IntStream.range(0, shardedData.size()).boxed().map(i -> new Shard().withShardId(Integer.toString(i))).collect(Collectors.toList());
    result.setShards(shards);
    HttpResponse response = new HttpResponse(null, null);
    response.setStatusCode(200);
    result.setSdkHttpMetadata(SdkHttpMetadata.from(response));
    return result;
}
Also used : ListShardsResult(com.amazonaws.services.kinesis.model.ListShardsResult) Shard(com.amazonaws.services.kinesis.model.Shard) AddTagsToStreamResult(com.amazonaws.services.kinesis.model.AddTagsToStreamResult) MergeShardsResult(com.amazonaws.services.kinesis.model.MergeShardsResult) PutRecordsRequest(com.amazonaws.services.kinesis.model.PutRecordsRequest) PutRecordRequest(com.amazonaws.services.kinesis.model.PutRecordRequest) ListTagsForStreamRequest(com.amazonaws.services.kinesis.model.ListTagsForStreamRequest) KinesisProducerConfiguration(com.amazonaws.services.kinesis.producer.KinesisProducerConfiguration) ByteBuffer(java.nio.ByteBuffer) SplitShardRequest(com.amazonaws.services.kinesis.model.SplitShardRequest) GetShardIteratorRequest(com.amazonaws.services.kinesis.model.GetShardIteratorRequest) UpdateShardCountResult(com.amazonaws.services.kinesis.model.UpdateShardCountResult) GetShardIteratorResult(com.amazonaws.services.kinesis.model.GetShardIteratorResult) PutRecordsResult(com.amazonaws.services.kinesis.model.PutRecordsResult) HashCodeBuilder.reflectionHashCode(org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode) DescribeStreamResult(com.amazonaws.services.kinesis.model.DescribeStreamResult) ListStreamConsumersRequest(com.amazonaws.services.kinesis.model.ListStreamConsumersRequest) RemoveTagsFromStreamResult(com.amazonaws.services.kinesis.model.RemoveTagsFromStreamResult) UpdateStreamModeRequest(com.amazonaws.services.kinesis.model.UpdateStreamModeRequest) ListShardsResult(com.amazonaws.services.kinesis.model.ListShardsResult) GetRecordsResult(com.amazonaws.services.kinesis.model.GetRecordsResult) StartStreamEncryptionResult(com.amazonaws.services.kinesis.model.StartStreamEncryptionResult) AmazonKinesis(com.amazonaws.services.kinesis.AmazonKinesis) ShardIteratorType(com.amazonaws.services.kinesis.model.ShardIteratorType) HttpResponse(com.amazonaws.http.HttpResponse) ListStreamsResult(com.amazonaws.services.kinesis.model.ListStreamsResult) AmazonKinesisWaiters(com.amazonaws.services.kinesis.waiters.AmazonKinesisWaiters) Math.min(java.lang.Math.min) SdkHttpMetadata(com.amazonaws.http.SdkHttpMetadata) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) DescribeStreamConsumerResult(com.amazonaws.services.kinesis.model.DescribeStreamConsumerResult) DecreaseStreamRetentionPeriodResult(com.amazonaws.services.kinesis.model.DecreaseStreamRetentionPeriodResult) List(java.util.List) LimitExceededException(com.amazonaws.services.kinesis.model.LimitExceededException) UpdateShardCountRequest(com.amazonaws.services.kinesis.model.UpdateShardCountRequest) IntStream(java.util.stream.IntStream) ListStreamConsumersResult(com.amazonaws.services.kinesis.model.ListStreamConsumersResult) ListStreamsRequest(com.amazonaws.services.kinesis.model.ListStreamsRequest) DescribeStreamSummaryResult(com.amazonaws.services.kinesis.model.DescribeStreamSummaryResult) Record(com.amazonaws.services.kinesis.model.Record) AddTagsToStreamRequest(com.amazonaws.services.kinesis.model.AddTagsToStreamRequest) EnableEnhancedMonitoringRequest(com.amazonaws.services.kinesis.model.EnableEnhancedMonitoringRequest) AmazonCloudWatch(com.amazonaws.services.cloudwatch.AmazonCloudWatch) DisableEnhancedMonitoringRequest(com.amazonaws.services.kinesis.model.DisableEnhancedMonitoringRequest) ListShardsRequest(com.amazonaws.services.kinesis.model.ListShardsRequest) StartStreamEncryptionRequest(com.amazonaws.services.kinesis.model.StartStreamEncryptionRequest) Splitter(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Splitter) EnableEnhancedMonitoringResult(com.amazonaws.services.kinesis.model.EnableEnhancedMonitoringResult) DescribeStreamRequest(com.amazonaws.services.kinesis.model.DescribeStreamRequest) StopStreamEncryptionResult(com.amazonaws.services.kinesis.model.StopStreamEncryptionResult) IKinesisProducer(com.amazonaws.services.kinesis.producer.IKinesisProducer) CreateStreamRequest(com.amazonaws.services.kinesis.model.CreateStreamRequest) DescribeLimitsRequest(com.amazonaws.services.kinesis.model.DescribeLimitsRequest) DeregisterStreamConsumerRequest(com.amazonaws.services.kinesis.model.DeregisterStreamConsumerRequest) CreateStreamResult(com.amazonaws.services.kinesis.model.CreateStreamResult) RemoveTagsFromStreamRequest(com.amazonaws.services.kinesis.model.RemoveTagsFromStreamRequest) StopStreamEncryptionRequest(com.amazonaws.services.kinesis.model.StopStreamEncryptionRequest) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ListTagsForStreamResult(com.amazonaws.services.kinesis.model.ListTagsForStreamResult) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) DecreaseStreamRetentionPeriodRequest(com.amazonaws.services.kinesis.model.DecreaseStreamRetentionPeriodRequest) ResponseMetadata(com.amazonaws.ResponseMetadata) DescribeStreamSummaryRequest(com.amazonaws.services.kinesis.model.DescribeStreamSummaryRequest) PutRecordResult(com.amazonaws.services.kinesis.model.PutRecordResult) UpdateStreamModeResult(com.amazonaws.services.kinesis.model.UpdateStreamModeResult) DeleteStreamResult(com.amazonaws.services.kinesis.model.DeleteStreamResult) IncreaseStreamRetentionPeriodRequest(com.amazonaws.services.kinesis.model.IncreaseStreamRetentionPeriodRequest) MergeShardsRequest(com.amazonaws.services.kinesis.model.MergeShardsRequest) DescribeStreamConsumerRequest(com.amazonaws.services.kinesis.model.DescribeStreamConsumerRequest) Integer.parseInt(java.lang.Integer.parseInt) DeregisterStreamConsumerResult(com.amazonaws.services.kinesis.model.DeregisterStreamConsumerResult) RegisterStreamConsumerResult(com.amazonaws.services.kinesis.model.RegisterStreamConsumerResult) IncreaseStreamRetentionPeriodResult(com.amazonaws.services.kinesis.model.IncreaseStreamRetentionPeriodResult) Mockito(org.mockito.Mockito) Lists.transform(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists.transform) EqualsBuilder(org.apache.commons.lang.builder.EqualsBuilder) Instant(org.joda.time.Instant) GetRecordsRequest(com.amazonaws.services.kinesis.model.GetRecordsRequest) DeleteStreamRequest(com.amazonaws.services.kinesis.model.DeleteStreamRequest) SplitShardResult(com.amazonaws.services.kinesis.model.SplitShardResult) Region(com.amazonaws.regions.Region) DescribeLimitsResult(com.amazonaws.services.kinesis.model.DescribeLimitsResult) DisableEnhancedMonitoringResult(com.amazonaws.services.kinesis.model.DisableEnhancedMonitoringResult) RegisterStreamConsumerRequest(com.amazonaws.services.kinesis.model.RegisterStreamConsumerRequest) HttpResponse(com.amazonaws.http.HttpResponse) LimitExceededException(com.amazonaws.services.kinesis.model.LimitExceededException) Shard(com.amazonaws.services.kinesis.model.Shard)

Example 4 with LimitExceededException

use of com.amazonaws.services.kinesis.model.LimitExceededException in project beam by apache.

the class SimplifiedKinesisClient method describeStreamSummary.

private StreamDescriptionSummary describeStreamSummary(final String streamName) throws IOException, InterruptedException {
    // DescribeStreamSummary has limits that can be hit fairly easily if we are attempting
    // to configure multiple KinesisIO inputs in the same account. Retry up to
    // DESCRIBE_STREAM_SUMMARY_MAX_ATTEMPTS times if we end up hitting that limit.
    // 
    // Only pass the wrapped exception up once that limit is reached. Use FluentBackoff
    // to implement the retry policy.
    FluentBackoff retryBackoff = FluentBackoff.DEFAULT.withMaxRetries(DESCRIBE_STREAM_SUMMARY_MAX_ATTEMPTS).withInitialBackoff(DESCRIBE_STREAM_SUMMARY_INITIAL_BACKOFF);
    BackOff backoff = retryBackoff.backoff();
    Sleeper sleeper = Sleeper.DEFAULT;
    DescribeStreamSummaryRequest request = new DescribeStreamSummaryRequest();
    request.setStreamName(streamName);
    while (true) {
        try {
            return kinesis.describeStreamSummary(request).getStreamDescriptionSummary();
        } catch (LimitExceededException exc) {
            if (!BackOffUtils.next(sleeper, backoff)) {
                throw exc;
            }
        }
    }
}
Also used : FluentBackoff(org.apache.beam.sdk.util.FluentBackoff) DescribeStreamSummaryRequest(com.amazonaws.services.kinesis.model.DescribeStreamSummaryRequest) LimitExceededException(com.amazonaws.services.kinesis.model.LimitExceededException) Sleeper(org.apache.beam.sdk.util.Sleeper) BackOff(org.apache.beam.sdk.util.BackOff)

Example 5 with LimitExceededException

use of com.amazonaws.services.kinesis.model.LimitExceededException in project beam by apache.

the class SimplifiedKinesisClientTest method shouldListAllShardsForTimestampWithRetriedDescribeStreamSummaryCallAfterStreamCreationTimestamp.

@Test
public void shouldListAllShardsForTimestampWithRetriedDescribeStreamSummaryCallAfterStreamCreationTimestamp() throws TransientKinesisException {
    Shard shard1 = new Shard().withShardId(SHARD_1);
    Shard shard2 = new Shard().withShardId(SHARD_2);
    Shard shard3 = new Shard().withShardId(SHARD_3);
    int hoursDifference = 1;
    int retentionPeriodHours = hoursDifference * 3;
    Instant streamCreationTimestamp = CURRENT_TIMESTAMP.minus(Duration.standardHours(retentionPeriodHours));
    Instant startingPointTimestamp = streamCreationTimestamp.plus(Duration.standardHours(hoursDifference));
    when(currentInstantSupplier.get()).thenReturn(CURRENT_TIMESTAMP);
    when(kinesis.describeStreamSummary(new DescribeStreamSummaryRequest().withStreamName(STREAM))).thenThrow(new LimitExceededException("Fake Exception: Limit exceeded")).thenReturn(new DescribeStreamSummaryResult().withStreamDescriptionSummary(new StreamDescriptionSummary().withRetentionPeriodHours(retentionPeriodHours).withStreamCreationTimestamp(streamCreationTimestamp.toDate())));
    ShardFilter shardFilter = new ShardFilter().withType(ShardFilterType.AT_TIMESTAMP).withTimestamp(startingPointTimestamp.toDate());
    when(kinesis.listShards(new ListShardsRequest().withStreamName(STREAM).withShardFilter(shardFilter).withMaxResults(1_000))).thenReturn(new ListShardsResult().withShards(shard1, shard2, shard3).withNextToken(null));
    List<Shard> shards = underTest.listShardsAtPoint(STREAM, new StartingPoint(startingPointTimestamp));
    assertThat(shards).containsOnly(shard1, shard2, shard3);
}
Also used : StreamDescriptionSummary(com.amazonaws.services.kinesis.model.StreamDescriptionSummary) ListShardsResult(com.amazonaws.services.kinesis.model.ListShardsResult) ListShardsRequest(com.amazonaws.services.kinesis.model.ListShardsRequest) Instant(org.joda.time.Instant) DescribeStreamSummaryResult(com.amazonaws.services.kinesis.model.DescribeStreamSummaryResult) DescribeStreamSummaryRequest(com.amazonaws.services.kinesis.model.DescribeStreamSummaryRequest) LimitExceededException(com.amazonaws.services.kinesis.model.LimitExceededException) ShardFilter(com.amazonaws.services.kinesis.model.ShardFilter) Shard(com.amazonaws.services.kinesis.model.Shard) Datapoint(com.amazonaws.services.cloudwatch.model.Datapoint) Test(org.junit.Test)

Aggregations

LimitExceededException (com.amazonaws.services.kinesis.model.LimitExceededException)6 DescribeStreamResult (com.amazonaws.services.kinesis.model.DescribeStreamResult)3 DescribeStreamSummaryRequest (com.amazonaws.services.kinesis.model.DescribeStreamSummaryRequest)3 ListShardsRequest (com.amazonaws.services.kinesis.model.ListShardsRequest)3 ListShardsResult (com.amazonaws.services.kinesis.model.ListShardsResult)3 Shard (com.amazonaws.services.kinesis.model.Shard)3 AmazonKinesis (com.amazonaws.services.kinesis.AmazonKinesis)2 DescribeStreamRequest (com.amazonaws.services.kinesis.model.DescribeStreamRequest)2 DescribeStreamSummaryResult (com.amazonaws.services.kinesis.model.DescribeStreamSummaryResult)2 ResourceNotFoundException (com.amazonaws.services.kinesis.model.ResourceNotFoundException)2 Instant (org.joda.time.Instant)2 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)1 ResponseMetadata (com.amazonaws.ResponseMetadata)1 SdkClientException (com.amazonaws.SdkClientException)1 HttpResponse (com.amazonaws.http.HttpResponse)1 SdkHttpMetadata (com.amazonaws.http.SdkHttpMetadata)1 Region (com.amazonaws.regions.Region)1 AmazonCloudWatch (com.amazonaws.services.cloudwatch.AmazonCloudWatch)1 Datapoint (com.amazonaws.services.cloudwatch.model.Datapoint)1 AddTagsToStreamRequest (com.amazonaws.services.kinesis.model.AddTagsToStreamRequest)1