Search in sources :

Example 1 with Shard

use of com.amazonaws.services.dynamodbv2.model.Shard in project camel by apache.

the class ShardIteratorHandler method getShardIterator.

String getShardIterator(String resumeFromSequenceNumber) {
    ShardIteratorType iteratorType = getEndpoint().getIteratorType();
    String sequenceNumber = getEndpoint().getSequenceNumber();
    if (resumeFromSequenceNumber != null) {
        // Reset things as we're in an error condition.
        currentShard = null;
        currentShardIterator = null;
        iteratorType = ShardIteratorType.AFTER_SEQUENCE_NUMBER;
        sequenceNumber = resumeFromSequenceNumber;
    }
    // either return a cached one or get a new one via a GetShardIterator request.
    if (currentShardIterator == null) {
        ListStreamsResult streamsListResult = getClient().listStreams(new ListStreamsRequest().withTableName(getEndpoint().getTableName()));
        // XXX assumes there is only one stream
        final String streamArn = streamsListResult.getStreams().get(0).getStreamArn();
        DescribeStreamResult streamDescriptionResult = getClient().describeStream(new DescribeStreamRequest().withStreamArn(streamArn));
        shardList.addAll(streamDescriptionResult.getStreamDescription().getShards());
        LOG.trace("Current shard is: {} (in {})", currentShard, shardList);
        if (currentShard == null) {
            currentShard = resolveNewShard(iteratorType, resumeFromSequenceNumber);
        } else {
            currentShard = shardList.nextAfter(currentShard);
        }
        shardList.removeOlderThan(currentShard);
        LOG.trace("Next shard is: {} (in {})", currentShard, shardList);
        GetShardIteratorResult result = getClient().getShardIterator(buildGetShardIteratorRequest(streamArn, iteratorType, sequenceNumber));
        currentShardIterator = result.getShardIterator();
    }
    LOG.trace("Shard Iterator is: {}", currentShardIterator);
    return currentShardIterator;
}
Also used : ShardIteratorType(com.amazonaws.services.dynamodbv2.model.ShardIteratorType) ListStreamsResult(com.amazonaws.services.dynamodbv2.model.ListStreamsResult) GetShardIteratorResult(com.amazonaws.services.dynamodbv2.model.GetShardIteratorResult) ListStreamsRequest(com.amazonaws.services.dynamodbv2.model.ListStreamsRequest) DescribeStreamResult(com.amazonaws.services.dynamodbv2.model.DescribeStreamResult) DescribeStreamRequest(com.amazonaws.services.dynamodbv2.model.DescribeStreamRequest)

Example 2 with Shard

use of com.amazonaws.services.dynamodbv2.model.Shard in project camel by apache.

the class ShardListTest method nextWithNullReturnsFirstKnownShard.

@Test
public void nextWithNullReturnsFirstKnownShard() throws Exception {
    Shard first = new Shard().withShardId("first_shard");
    Shard second = new Shard().withParentShardId("first_shard").withShardId("second_shard");
    ShardList shards = new ShardList();
    shards.add(first);
    shards.add(second);
    assertThat(shards.nextAfter(first), is(second));
}
Also used : Shard(com.amazonaws.services.dynamodbv2.model.Shard) Test(org.junit.Test)

Example 3 with Shard

use of com.amazonaws.services.dynamodbv2.model.Shard in project camel by apache.

the class ShardListTest method createShards.

static List<Shard> createShards(String initialParent, String... shardIds) {
    String previous = initialParent;
    List<Shard> result = new ArrayList<>();
    for (String s : shardIds) {
        result.add(new Shard().withShardId(s).withParentShardId(previous));
        previous = s;
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Shard(com.amazonaws.services.dynamodbv2.model.Shard)

Example 4 with Shard

use of com.amazonaws.services.dynamodbv2.model.Shard in project camel by apache.

the class DdbStreamConsumer method poll.

@Override
protected int poll() throws Exception {
    GetRecordsResult result;
    try {
        GetRecordsRequest req = new GetRecordsRequest().withShardIterator(shardIteratorHandler.getShardIterator(null)).withLimit(getEndpoint().getMaxResultsPerRequest());
        result = getClient().getRecords(req);
    } catch (ExpiredIteratorException e) {
        LOG.warn("Expired Shard Iterator, attempting to resume from " + lastSeenSequenceNumber, e);
        GetRecordsRequest req = new GetRecordsRequest().withShardIterator(shardIteratorHandler.getShardIterator(lastSeenSequenceNumber)).withLimit(getEndpoint().getMaxResultsPerRequest());
        result = getClient().getRecords(req);
    }
    List<Record> records = result.getRecords();
    Queue<Exchange> exchanges = createExchanges(records, lastSeenSequenceNumber);
    int processedExchangeCount = processBatch(CastUtils.cast(exchanges));
    shardIteratorHandler.updateShardIterator(result.getNextShardIterator());
    if (!records.isEmpty()) {
        lastSeenSequenceNumber = records.get(records.size() - 1).getDynamodb().getSequenceNumber();
    }
    return processedExchangeCount;
}
Also used : Exchange(org.apache.camel.Exchange) GetRecordsResult(com.amazonaws.services.dynamodbv2.model.GetRecordsResult) Record(com.amazonaws.services.dynamodbv2.model.Record) ExpiredIteratorException(com.amazonaws.services.dynamodbv2.model.ExpiredIteratorException) GetRecordsRequest(com.amazonaws.services.dynamodbv2.model.GetRecordsRequest)

Example 5 with Shard

use of com.amazonaws.services.dynamodbv2.model.Shard in project camel by apache.

the class ShardList method removeOlderThan.

/**
     * Removes shards that are older than the provided shard. Does not remove
     * the provided shard.
     *
     * @param removeBefore
     */
void removeOlderThan(Shard removeBefore) {
    String current = removeBefore.getParentShardId();
    int removedShards = 0;
    while (current != null) {
        Shard s = shards.remove(current);
        if (s == null) {
            current = null;
        } else {
            removedShards++;
            current = s.getParentShardId();
        }
    }
    log.trace("removed {} shards from the store, new size is {}", removedShards, shards.size());
}
Also used : Shard(com.amazonaws.services.dynamodbv2.model.Shard)

Aggregations

Shard (com.amazonaws.services.dynamodbv2.model.Shard)8 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 DescribeStreamRequest (com.amazonaws.services.dynamodbv2.model.DescribeStreamRequest)2 DescribeStreamResult (com.amazonaws.services.dynamodbv2.model.DescribeStreamResult)2 GetRecordsRequest (com.amazonaws.services.dynamodbv2.model.GetRecordsRequest)2 GetRecordsResult (com.amazonaws.services.dynamodbv2.model.GetRecordsResult)2 GetShardIteratorResult (com.amazonaws.services.dynamodbv2.model.GetShardIteratorResult)2 Record (com.amazonaws.services.dynamodbv2.model.Record)2 AmazonClientException (com.amazonaws.AmazonClientException)1 DefaultAWSCredentialsProviderChain (com.amazonaws.auth.DefaultAWSCredentialsProviderChain)1 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)1 AmazonDynamoDBStreams (com.amazonaws.services.dynamodbv2.AmazonDynamoDBStreams)1 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)1 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)1 AttributeValueUpdate (com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate)1 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)1 DescribeTableResult (com.amazonaws.services.dynamodbv2.model.DescribeTableResult)1 ExpiredIteratorException (com.amazonaws.services.dynamodbv2.model.ExpiredIteratorException)1 GetShardIteratorRequest (com.amazonaws.services.dynamodbv2.model.GetShardIteratorRequest)1