software.amazon.awssdk.services.dynamodb.streams.DefaultDynamoDbStreamsClient Maven / Gradle / Ivy
Show all versions of dynamodb Show documentation
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services.dynamodb.streams;
import java.util.Collections;
import java.util.List;
import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.RequestOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.client.handler.ClientExecutionParams;
import software.amazon.awssdk.core.client.handler.SyncClientHandler;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.http.HttpResponseHandler;
import software.amazon.awssdk.core.metrics.CoreMetric;
import software.amazon.awssdk.metrics.MetricCollector;
import software.amazon.awssdk.metrics.MetricPublisher;
import software.amazon.awssdk.metrics.NoOpMetricCollector;
import software.amazon.awssdk.protocols.core.ExceptionMetadata;
import software.amazon.awssdk.protocols.json.AwsJsonProtocol;
import software.amazon.awssdk.protocols.json.AwsJsonProtocolFactory;
import software.amazon.awssdk.protocols.json.BaseAwsJsonProtocolFactory;
import software.amazon.awssdk.protocols.json.JsonOperationMetadata;
import software.amazon.awssdk.services.dynamodb.model.DescribeStreamRequest;
import software.amazon.awssdk.services.dynamodb.model.DescribeStreamResponse;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.ExpiredIteratorException;
import software.amazon.awssdk.services.dynamodb.model.GetRecordsRequest;
import software.amazon.awssdk.services.dynamodb.model.GetRecordsResponse;
import software.amazon.awssdk.services.dynamodb.model.GetShardIteratorRequest;
import software.amazon.awssdk.services.dynamodb.model.GetShardIteratorResponse;
import software.amazon.awssdk.services.dynamodb.model.InternalServerErrorException;
import software.amazon.awssdk.services.dynamodb.model.LimitExceededException;
import software.amazon.awssdk.services.dynamodb.model.ListStreamsRequest;
import software.amazon.awssdk.services.dynamodb.model.ListStreamsResponse;
import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException;
import software.amazon.awssdk.services.dynamodb.model.TrimmedDataAccessException;
import software.amazon.awssdk.services.dynamodb.streams.transform.DescribeStreamRequestMarshaller;
import software.amazon.awssdk.services.dynamodb.streams.transform.GetRecordsRequestMarshaller;
import software.amazon.awssdk.services.dynamodb.streams.transform.GetShardIteratorRequestMarshaller;
import software.amazon.awssdk.services.dynamodb.streams.transform.ListStreamsRequestMarshaller;
import software.amazon.awssdk.utils.Logger;
/**
* Internal implementation of {@link DynamoDbStreamsClient}.
*
* @see DynamoDbStreamsClient#builder()
*/
@Generated("software.amazon.awssdk:codegen")
@SdkInternalApi
final class DefaultDynamoDbStreamsClient implements DynamoDbStreamsClient {
private static final Logger log = Logger.loggerFor(DefaultDynamoDbStreamsClient.class);
private final SyncClientHandler clientHandler;
private final AwsJsonProtocolFactory protocolFactory;
private final SdkClientConfiguration clientConfiguration;
protected DefaultDynamoDbStreamsClient(SdkClientConfiguration clientConfiguration) {
this.clientHandler = new AwsSyncClientHandler(clientConfiguration);
this.clientConfiguration = clientConfiguration;
this.protocolFactory = init(AwsJsonProtocolFactory.builder()).build();
}
@Override
public final String serviceName() {
return SERVICE_NAME;
}
/**
*
* Returns information about a stream, including the current status of the stream, its Amazon Resource Name (ARN),
* the composition of its shards, and its corresponding DynamoDB table.
*
*
*
* You can call DescribeStream
at a maximum rate of 10 times per second.
*
*
*
* Each shard in the stream has a SequenceNumberRange
associated with it. If the
* SequenceNumberRange
has a StartingSequenceNumber
but no
* EndingSequenceNumber
, then the shard is still open (able to receive more stream records). If both
* StartingSequenceNumber
and EndingSequenceNumber
are present, then that shard is closed
* and can no longer receive more data.
*
*
* @param describeStreamRequest
* Represents the input of a DescribeStream
operation.
* @return Result of the DescribeStream operation returned by the service.
* @throws ResourceNotFoundException
* The operation tried to access a nonexistent table or index. The resource might not be specified
* correctly, or its status might not be ACTIVE
.
* @throws InternalServerErrorException
* An error occurred on the server side.
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws DynamoDbStreamsException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample DynamoDbStreamsClient.DescribeStream
* @see AWS API Documentation
*/
@Override
public DescribeStreamResponse describeStream(DescribeStreamRequest describeStreamRequest) throws ResourceNotFoundException,
InternalServerErrorException, AwsServiceException, SdkClientException, DynamoDbException {
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();
HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata,
DescribeStreamResponse::builder);
HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);
List metricPublishers = resolveMetricPublishers(clientConfiguration, describeStreamRequest
.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "DynamoDB Streams");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "DescribeStream");
return clientHandler.execute(new ClientExecutionParams()
.withOperationName("DescribeStream").withResponseHandler(responseHandler)
.withErrorResponseHandler(errorResponseHandler).withInput(describeStreamRequest)
.withMetricCollector(apiCallMetricCollector)
.withMarshaller(new DescribeStreamRequestMarshaller(protocolFactory)));
} finally {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
}
}
/**
*
* Retrieves the stream records from a given shard.
*
*
* Specify a shard iterator using the ShardIterator
parameter. The shard iterator specifies the
* position in the shard from which you want to start reading stream records sequentially. If there are no stream
* records available in the portion of the shard that the iterator points to, GetRecords
returns an
* empty list. Note that it might take multiple calls to get to a portion of the shard that contains stream records.
*
*
*
* GetRecords
can retrieve a maximum of 1 MB of data or 1000 stream records, whichever comes first.
*
*
*
* @param getRecordsRequest
* Represents the input of a GetRecords
operation.
* @return Result of the GetRecords operation returned by the service.
* @throws ResourceNotFoundException
* The operation tried to access a nonexistent table or index. The resource might not be specified
* correctly, or its status might not be ACTIVE
.
* @throws LimitExceededException
* There is no limit to the number of daily on-demand backups that can be taken.
*
* Up to 50 simultaneous table operations are allowed per account. These operations include
* CreateTable
, UpdateTable
, DeleteTable
,
* UpdateTimeToLive
, RestoreTableFromBackup
, and
* RestoreTableToPointInTime
.
*
*
* The only exception is when you are creating a table with one or more secondary indexes. You can have up
* to 25 such requests running at a time; however, if the table or index specifications are complex,
* DynamoDB might temporarily reduce the number of concurrent operations.
*
*
* There is a soft account quota of 256 tables.
* @throws InternalServerErrorException
* An error occurred on the server side.
* @throws ExpiredIteratorException
* The shard iterator has expired and can no longer be used to retrieve stream records. A shard iterator
* expires 15 minutes after it is retrieved using the GetShardIterator
action.
* @throws TrimmedDataAccessException
* The operation attempted to read past the oldest stream record in a shard.
*
*
* In DynamoDB Streams, there is a 24 hour limit on data retention. Stream records whose age exceeds this
* limit are subject to removal (trimming) from the stream. You might receive a TrimmedDataAccessException
* if:
*
*
* -
*
* You request a shard iterator with a sequence number older than the trim point (24 hours).
*
*
* -
*
* You obtain a shard iterator, but before you use the iterator in a GetRecords
request, a
* stream record in the shard exceeds the 24 hour period and is trimmed. This causes the iterator to access
* a record that no longer exists.
*
*
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws DynamoDbStreamsException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample DynamoDbStreamsClient.GetRecords
* @see AWS
* API Documentation
*/
@Override
public GetRecordsResponse getRecords(GetRecordsRequest getRecordsRequest) throws ResourceNotFoundException,
LimitExceededException, InternalServerErrorException, ExpiredIteratorException, TrimmedDataAccessException,
AwsServiceException, SdkClientException, DynamoDbException {
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();
HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata,
GetRecordsResponse::builder);
HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);
List metricPublishers = resolveMetricPublishers(clientConfiguration, getRecordsRequest
.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "DynamoDB Streams");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetRecords");
return clientHandler
.execute(new ClientExecutionParams().withOperationName("GetRecords")
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
.withInput(getRecordsRequest).withMetricCollector(apiCallMetricCollector)
.withMarshaller(new GetRecordsRequestMarshaller(protocolFactory)));
} finally {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
}
}
/**
*
* Returns a shard iterator. A shard iterator provides information about how to retrieve the stream records from
* within a shard. Use the shard iterator in a subsequent GetRecords
request to read the stream records
* from the shard.
*
*
*
* A shard iterator expires 15 minutes after it is returned to the requester.
*
*
*
* @param getShardIteratorRequest
* Represents the input of a GetShardIterator
operation.
* @return Result of the GetShardIterator operation returned by the service.
* @throws ResourceNotFoundException
* The operation tried to access a nonexistent table or index. The resource might not be specified
* correctly, or its status might not be ACTIVE
.
* @throws InternalServerErrorException
* An error occurred on the server side.
* @throws TrimmedDataAccessException
* The operation attempted to read past the oldest stream record in a shard.
*
* In DynamoDB Streams, there is a 24 hour limit on data retention. Stream records whose age exceeds this
* limit are subject to removal (trimming) from the stream. You might receive a TrimmedDataAccessException
* if:
*
*
* -
*
* You request a shard iterator with a sequence number older than the trim point (24 hours).
*
*
* -
*
* You obtain a shard iterator, but before you use the iterator in a GetRecords
request, a
* stream record in the shard exceeds the 24 hour period and is trimmed. This causes the iterator to access
* a record that no longer exists.
*
*
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws DynamoDbStreamsException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample DynamoDbStreamsClient.GetShardIterator
* @see AWS API Documentation
*/
@Override
public GetShardIteratorResponse getShardIterator(GetShardIteratorRequest getShardIteratorRequest)
throws ResourceNotFoundException, InternalServerErrorException, TrimmedDataAccessException, AwsServiceException,
SdkClientException, DynamoDbException {
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();
HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata,
GetShardIteratorResponse::builder);
HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);
List metricPublishers = resolveMetricPublishers(clientConfiguration, getShardIteratorRequest
.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "DynamoDB Streams");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetShardIterator");
return clientHandler.execute(new ClientExecutionParams()
.withOperationName("GetShardIterator").withResponseHandler(responseHandler)
.withErrorResponseHandler(errorResponseHandler).withInput(getShardIteratorRequest)
.withMetricCollector(apiCallMetricCollector)
.withMarshaller(new GetShardIteratorRequestMarshaller(protocolFactory)));
} finally {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
}
}
/**
*
* Returns an array of stream ARNs associated with the current account and endpoint. If the TableName
* parameter is present, then ListStreams
will return only the streams ARNs for that table.
*
*
*
* You can call ListStreams
at a maximum rate of 5 times per second.
*
*
*
* @param listStreamsRequest
* Represents the input of a ListStreams
operation.
* @return Result of the ListStreams operation returned by the service.
* @throws ResourceNotFoundException
* The operation tried to access a nonexistent table or index. The resource might not be specified
* correctly, or its status might not be ACTIVE
.
* @throws InternalServerErrorException
* An error occurred on the server side.
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws DynamoDbStreamsException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample DynamoDbStreamsClient.ListStreams
* @see AWS
* API Documentation
*/
@Override
public ListStreamsResponse listStreams(ListStreamsRequest listStreamsRequest) throws ResourceNotFoundException,
InternalServerErrorException, AwsServiceException, SdkClientException, DynamoDbException {
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();
HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata,
ListStreamsResponse::builder);
HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);
List metricPublishers = resolveMetricPublishers(clientConfiguration, listStreamsRequest
.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "DynamoDB Streams");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "ListStreams");
return clientHandler.execute(new ClientExecutionParams()
.withOperationName("ListStreams").withResponseHandler(responseHandler)
.withErrorResponseHandler(errorResponseHandler).withInput(listStreamsRequest)
.withMetricCollector(apiCallMetricCollector)
.withMarshaller(new ListStreamsRequestMarshaller(protocolFactory)));
} finally {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
}
}
private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration,
RequestOverrideConfiguration requestOverrideConfiguration) {
List publishers = null;
if (requestOverrideConfiguration != null) {
publishers = requestOverrideConfiguration.metricPublishers();
}
if (publishers == null || publishers.isEmpty()) {
publishers = clientConfiguration.option(SdkClientOption.METRIC_PUBLISHERS);
}
if (publishers == null) {
publishers = Collections.emptyList();
}
return publishers;
}
private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory,
JsonOperationMetadata operationMetadata) {
return protocolFactory.createErrorResponseHandler(operationMetadata);
}
private > T init(T builder) {
return builder
.clientConfiguration(clientConfiguration)
.defaultServiceExceptionSupplier(DynamoDbException::builder)
.protocol(AwsJsonProtocol.AWS_JSON)
.protocolVersion("1.0")
.registerModeledException(
ExceptionMetadata.builder().errorCode("TrimmedDataAccessException")
.exceptionBuilderSupplier(TrimmedDataAccessException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("ResourceNotFoundException")
.exceptionBuilderSupplier(ResourceNotFoundException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("ExpiredIteratorException")
.exceptionBuilderSupplier(ExpiredIteratorException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("InternalServerError")
.exceptionBuilderSupplier(InternalServerErrorException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("LimitExceededException")
.exceptionBuilderSupplier(LimitExceededException::builder).build());
}
@Override
public void close() {
clientHandler.close();
}
}