Search in sources :

Example 1 with GetShardIteratorResult

use of com.amazonaws.services.kinesis.model.GetShardIteratorResult in project kafka-connect-kinesis by jcustenborder.

the class KinesisSourceTask method start.

@Override
public void start(Map<String, String> settings) {
    this.config = new KinesisSourceConnectorConfig(settings);
    this.kinesisClient = this.kinesisClientFactory.create(this.config);
    this.sourcePartition = ImmutableMap.of(RecordConverter.FIELD_SHARD_ID, this.config.kinesisShardId);
    Map<String, Object> lastOffset = this.context.offsetStorageReader().offset(this.sourcePartition);
    GetShardIteratorRequest shardIteratorRequest = new GetShardIteratorRequest().withShardId(this.config.kinesisShardId).withStreamName(this.config.kinesisStreamName);
    if (null != lastOffset && !lastOffset.isEmpty()) {
        String startingSequenceNumber = (String) lastOffset.get(RecordConverter.FIELD_SEQUENCE_NUMBER);
        log.info("start() - Starting iterator after last processed sequence number of '{}'", startingSequenceNumber);
        shardIteratorRequest.setShardIteratorType(ShardIteratorType.AFTER_SEQUENCE_NUMBER);
        shardIteratorRequest.setStartingSequenceNumber(startingSequenceNumber);
    } else {
        log.info("start() - Setting Shard Iterator Type to {} for {}", this.config.kinesisPosition, this.config.kinesisShardId);
        shardIteratorRequest.setShardIteratorType(this.config.kinesisPosition);
    }
    GetShardIteratorResult shardIteratorResult = this.kinesisClient.getShardIterator(shardIteratorRequest);
    log.info("start() - Using Shard Iterator {}", shardIteratorResult.getShardIterator());
    this.recordsRequest = new GetRecordsRequest().withLimit(this.config.kinesisRecordLimit).withShardIterator(shardIteratorResult.getShardIterator());
    this.recordConverter = new RecordConverter(this.config);
}
Also used : GetShardIteratorRequest(com.amazonaws.services.kinesis.model.GetShardIteratorRequest) GetShardIteratorResult(com.amazonaws.services.kinesis.model.GetShardIteratorResult) GetRecordsRequest(com.amazonaws.services.kinesis.model.GetRecordsRequest)

Example 2 with GetShardIteratorResult

use of com.amazonaws.services.kinesis.model.GetShardIteratorResult in project kafka-connect-kinesis by jcustenborder.

the class KinesisSourceTaskTest method sourceOffsets.

@Test
public void sourceOffsets() throws InterruptedException {
    final String SEQUENCE_NUMBER = "asdfasdfddsa";
    Map<String, Object> sourceOffset = ImmutableMap.of(RecordConverter.FIELD_SEQUENCE_NUMBER, SEQUENCE_NUMBER);
    when(this.offsetStorageReader.offset(anyMap())).thenReturn(sourceOffset);
    when(this.kinesisClient.getShardIterator(any())).thenReturn(new GetShardIteratorResult().withShardIterator("dfasdfsadfasdf"));
    this.task.start(settings);
    GetRecordsResult recordsResult = new GetRecordsResult().withNextShardIterator("dsfargadsfasdfasda").withRecords(TestData.record()).withMillisBehindLatest(0L);
    when(this.kinesisClient.getRecords(any())).thenReturn(recordsResult);
    List<SourceRecord> records = this.task.poll();
    assertNotNull(records, "records should not be null.");
    assertFalse(records.isEmpty(), "records should not be empty.");
    verify(this.offsetStorageReader, atLeastOnce()).offset(anyMap());
    GetShardIteratorRequest expectedIteratorRequest = new GetShardIteratorRequest().withShardIteratorType(ShardIteratorType.AFTER_SEQUENCE_NUMBER).withShardId(this.config.kinesisShardId).withStreamName(this.config.kinesisStreamName).withStartingSequenceNumber(SEQUENCE_NUMBER);
    verify(this.kinesisClient, atLeastOnce()).getShardIterator(expectedIteratorRequest);
}
Also used : GetRecordsResult(com.amazonaws.services.kinesis.model.GetRecordsResult) GetShardIteratorResult(com.amazonaws.services.kinesis.model.GetShardIteratorResult) GetShardIteratorRequest(com.amazonaws.services.kinesis.model.GetShardIteratorRequest) SourceRecord(org.apache.kafka.connect.source.SourceRecord) Test(org.junit.jupiter.api.Test)

Example 3 with GetShardIteratorResult

use of com.amazonaws.services.kinesis.model.GetShardIteratorResult in project storm by apache.

the class KinesisConnection method getShardIterator.

String getShardIterator(String stream, String shardId, ShardIteratorType shardIteratorType, String sequenceNumber, Date timestamp) {
    String shardIterator = "";
    try {
        GetShardIteratorRequest getShardIteratorRequest = new GetShardIteratorRequest();
        getShardIteratorRequest.setStreamName(stream);
        getShardIteratorRequest.setShardId(shardId);
        getShardIteratorRequest.setShardIteratorType(shardIteratorType);
        if (shardIteratorType.equals(ShardIteratorType.AFTER_SEQUENCE_NUMBER) || shardIteratorType.equals(ShardIteratorType.AT_SEQUENCE_NUMBER)) {
            getShardIteratorRequest.setStartingSequenceNumber(sequenceNumber);
        } else if (shardIteratorType.equals(ShardIteratorType.AT_TIMESTAMP)) {
            getShardIteratorRequest.setTimestamp(timestamp);
        }
        GetShardIteratorResult getShardIteratorResult = kinesisClient.getShardIterator(getShardIteratorRequest);
        if (getShardIteratorResult != null) {
            shardIterator = getShardIteratorResult.getShardIterator();
        }
    } catch (Exception e) {
        LOG.warn("Exception occured while getting shardIterator for shard " + shardId + " shardIteratorType " + shardIteratorType + " sequence number " + sequenceNumber + " timestamp " + timestamp, e);
    }
    LOG.warn("Returning shardIterator " + shardIterator + " for shardId " + shardId + " shardIteratorType " + shardIteratorType + " sequenceNumber " + sequenceNumber + " timestamp" + timestamp);
    return shardIterator;
}
Also used : GetShardIteratorRequest(com.amazonaws.services.kinesis.model.GetShardIteratorRequest) GetShardIteratorResult(com.amazonaws.services.kinesis.model.GetShardIteratorResult)

Example 4 with GetShardIteratorResult

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

the class KinesisProxy method getShardIterator.

private String getShardIterator(GetShardIteratorRequest getShardIteratorRequest) throws InterruptedException {
    GetShardIteratorResult getShardIteratorResult = null;
    int retryCount = 0;
    while (retryCount <= getShardIteratorMaxRetries && getShardIteratorResult == null) {
        try {
            getShardIteratorResult = kinesisClient.getShardIterator(getShardIteratorRequest);
        } catch (AmazonServiceException ex) {
            if (isRecoverableException(ex)) {
                long backoffMillis = BACKOFF.calculateFullJitterBackoff(getShardIteratorBaseBackoffMillis, getShardIteratorMaxBackoffMillis, getShardIteratorExpConstant, retryCount++);
                LOG.warn("Got recoverable AmazonServiceException. Backing off for " + backoffMillis + " millis (" + ex.getClass().getName() + ": " + ex.getMessage() + ")");
                BACKOFF.sleep(backoffMillis);
            } else {
                throw ex;
            }
        }
    }
    if (getShardIteratorResult == null) {
        throw new RuntimeException("Retries exceeded for getShardIterator operation - all " + getShardIteratorMaxRetries + " retry attempts failed.");
    }
    return getShardIteratorResult.getShardIterator();
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) GetShardIteratorResult(com.amazonaws.services.kinesis.model.GetShardIteratorResult)

Example 5 with GetShardIteratorResult

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

the class AmazonKinesisMock method getShardIterator.

@Override
public GetShardIteratorResult getShardIterator(GetShardIteratorRequest getShardIteratorRequest) {
    ShardIteratorType shardIteratorType = ShardIteratorType.fromValue(getShardIteratorRequest.getShardIteratorType());
    String shardIterator;
    if (shardIteratorType == ShardIteratorType.TRIM_HORIZON) {
        shardIterator = String.format("%s:%s", getShardIteratorRequest.getShardId(), 0);
    } else {
        throw new RuntimeException("Not implemented");
    }
    return new GetShardIteratorResult().withShardIterator(shardIterator);
}
Also used : ShardIteratorType(com.amazonaws.services.kinesis.model.ShardIteratorType) GetShardIteratorResult(com.amazonaws.services.kinesis.model.GetShardIteratorResult)

Aggregations

GetShardIteratorResult (com.amazonaws.services.kinesis.model.GetShardIteratorResult)15 GetShardIteratorRequest (com.amazonaws.services.kinesis.model.GetShardIteratorRequest)9 GetRecordsResult (com.amazonaws.services.kinesis.model.GetRecordsResult)6 GetRecordsRequest (com.amazonaws.services.kinesis.model.GetRecordsRequest)4 SourceRecord (org.apache.kafka.connect.source.SourceRecord)4 Test (org.junit.jupiter.api.Test)4 DescribeStreamRequest (com.amazonaws.services.kinesis.model.DescribeStreamRequest)3 DescribeStreamResult (com.amazonaws.services.kinesis.model.DescribeStreamResult)3 Shard (com.amazonaws.services.kinesis.model.Shard)2 Test (org.junit.Test)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonServiceException (com.amazonaws.AmazonServiceException)1 ProvisionedThroughputExceededException (com.amazonaws.services.kinesis.model.ProvisionedThroughputExceededException)1 ShardIteratorType (com.amazonaws.services.kinesis.model.ShardIteratorType)1 StreamDescription (com.amazonaws.services.kinesis.model.StreamDescription)1 Instant (org.joda.time.Instant)1 Before (org.junit.Before)1