|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +package org.opensearch.dataprepper.plugins.kinesis.source.apihandler; |
| 12 | + |
| 13 | +import lombok.NonNull; |
| 14 | +import lombok.extern.slf4j.Slf4j; |
| 15 | +import org.opensearch.dataprepper.plugins.kinesis.source.exceptions.KinesisConsumerNotFoundException; |
| 16 | +import software.amazon.awssdk.arns.Arn; |
| 17 | +import software.amazon.awssdk.services.kinesis.KinesisAsyncClient; |
| 18 | +import software.amazon.awssdk.services.kinesis.model.DescribeStreamConsumerRequest; |
| 19 | +import software.amazon.awssdk.services.kinesis.model.DescribeStreamConsumerResponse; |
| 20 | +import software.amazon.awssdk.services.kinesis.model.DescribeStreamSummaryRequest; |
| 21 | +import software.amazon.awssdk.services.kinesis.model.DescribeStreamSummaryResponse; |
| 22 | +import software.amazon.awssdk.services.kinesis.model.KinesisException; |
| 23 | +import software.amazon.awssdk.services.kinesis.model.StreamDescriptionSummary; |
| 24 | +import software.amazon.kinesis.common.StreamIdentifier; |
| 25 | + |
| 26 | +import java.util.Objects; |
| 27 | +import java.util.concurrent.CompletionException; |
| 28 | + |
| 29 | +@Slf4j |
| 30 | +public class KinesisClientApiHandler { |
| 31 | + private static final String COLON = ":"; |
| 32 | + |
| 33 | + private final KinesisAsyncClient kinesisClient; |
| 34 | + private final KinesisClientApiRetryHandler kinesisClientApiRetryHandler; |
| 35 | + |
| 36 | + public KinesisClientApiHandler(@NonNull final KinesisAsyncClient kinesisClient, @NonNull final KinesisClientApiRetryHandler kinesisClientApiRetryHandler) { |
| 37 | + this.kinesisClient = kinesisClient; |
| 38 | + this.kinesisClientApiRetryHandler = kinesisClientApiRetryHandler; |
| 39 | + } |
| 40 | + |
| 41 | + public StreamIdentifier getStreamIdentifier(final String streamNameOrArn) { |
| 42 | + if (isArn(streamNameOrArn)) { |
| 43 | + return getStreamIdentifierFromArn(streamNameOrArn); |
| 44 | + } |
| 45 | + return getStreamIdentifierFromName(streamNameOrArn); |
| 46 | + } |
| 47 | + |
| 48 | + public String getConsumerArnForStream(final String streamArn, final String consumerName) { |
| 49 | + if (Objects.isNull(streamArn) || streamArn.trim().isEmpty()) { |
| 50 | + throw new IllegalArgumentException("Stream ARN cannot be null or empty"); |
| 51 | + } |
| 52 | + if (Objects.isNull(consumerName) || consumerName.trim().isEmpty()) { |
| 53 | + throw new IllegalArgumentException("Consumer name cannot be null or empty"); |
| 54 | + } |
| 55 | + DescribeStreamConsumerResponse response = describeStreamConsumer(streamArn, consumerName); |
| 56 | + if (Objects.isNull(response)) { |
| 57 | + throw new KinesisConsumerNotFoundException( |
| 58 | + String.format("Kinesis stream consumer not found for %s", consumerName)); |
| 59 | + } |
| 60 | + return response.consumerDescription().consumerARN(); |
| 61 | + } |
| 62 | + |
| 63 | + private boolean isArn(final String streamNameOrArn) { |
| 64 | + try { |
| 65 | + Arn.fromString(streamNameOrArn); |
| 66 | + return true; |
| 67 | + } catch (IllegalArgumentException e) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private StreamIdentifier getStreamIdentifierFromArn(final String streamArnString) { |
| 73 | + Arn streamArn = Arn.fromString(streamArnString); |
| 74 | + String streamName = streamArn.resource().resource(); |
| 75 | + DescribeStreamSummaryResponse response = getStreamDescriptionSummary( |
| 76 | + buildStreamSummaryRequest(streamName, streamArnString)); |
| 77 | + return StreamIdentifier.multiStreamInstance( |
| 78 | + streamArn, |
| 79 | + response.streamDescriptionSummary().streamCreationTimestamp().getEpochSecond()); |
| 80 | + } |
| 81 | + |
| 82 | + private StreamIdentifier getStreamIdentifierFromName(final String streamName) { |
| 83 | + DescribeStreamSummaryResponse response = getStreamDescriptionSummary( |
| 84 | + buildStreamSummaryRequest(streamName, null)); |
| 85 | + StreamDescriptionSummary summary = response.streamDescriptionSummary(); |
| 86 | + return StreamIdentifier.multiStreamInstance(createStreamIdentifierString(summary)); |
| 87 | + } |
| 88 | + |
| 89 | + private DescribeStreamSummaryRequest buildStreamSummaryRequest(String streamName, String streamArn) { |
| 90 | + return DescribeStreamSummaryRequest.builder() |
| 91 | + .streamName(streamName) |
| 92 | + .streamARN(streamArn) |
| 93 | + .build(); |
| 94 | + } |
| 95 | + |
| 96 | + private String createStreamIdentifierString(StreamDescriptionSummary summary) { |
| 97 | + String accountId = Arn.fromString(summary.streamARN()).accountId().get(); |
| 98 | + long creationEpochSecond = summary.streamCreationTimestamp().getEpochSecond(); |
| 99 | + return String.join(COLON, accountId, summary.streamName(), |
| 100 | + String.valueOf(creationEpochSecond)); |
| 101 | + } |
| 102 | + |
| 103 | + private DescribeStreamSummaryResponse getStreamDescriptionSummary( |
| 104 | + DescribeStreamSummaryRequest request) { |
| 105 | + return kinesisClientApiRetryHandler.executeWithRetry( |
| 106 | + "getStreamDescriptionSummary", |
| 107 | + () -> kinesisClient.describeStreamSummary(request).join(), |
| 108 | + (ex, attempt) -> handleStreamSummaryException((CompletionException) ex, request.streamName()) |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + private void handleStreamSummaryException(CompletionException ex, String streamName) { |
| 113 | + Throwable cause = ex.getCause(); |
| 114 | + if (cause instanceof KinesisException || cause instanceof com.amazonaws.SdkClientException) { |
| 115 | + log.error("AWS error while describing stream summary for stream {}: {}", |
| 116 | + streamName, ex.getMessage()); |
| 117 | + } else { |
| 118 | + log.error("Unexpected error while describing stream summary for stream {}", |
| 119 | + streamName, ex); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private DescribeStreamConsumerResponse describeStreamConsumer( |
| 124 | + final String streamArn, final String consumerName) { |
| 125 | + DescribeStreamConsumerRequest request = DescribeStreamConsumerRequest.builder() |
| 126 | + .streamARN(streamArn) |
| 127 | + .consumerName(consumerName) |
| 128 | + .build(); |
| 129 | + |
| 130 | + return kinesisClientApiRetryHandler.executeWithRetry( |
| 131 | + "describeStreamConsumer", |
| 132 | + () -> kinesisClient.describeStreamConsumer(request).join(), |
| 133 | + (ex, attempt) -> handleConsumerException((CompletionException) ex, streamArn, attempt) |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + private void handleConsumerException(CompletionException ex, String streamArn, int attempt) { |
| 138 | + Throwable cause = ex.getCause(); |
| 139 | + if (cause instanceof KinesisException || cause instanceof com.amazonaws.SdkClientException) { |
| 140 | + log.error("AWS error while describing stream consumer for stream {}: {}. Attempt {}.", |
| 141 | + streamArn, ex.getMessage(), attempt + 1); |
| 142 | + } else { |
| 143 | + log.error("Unexpected error while describing stream consumer for stream {}. Attempt {} of {}.", |
| 144 | + streamArn, attempt + 1, ex); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
0 commit comments