com.amazonaws.services.kinesis.model.ShardIteratorType Java Examples

The following examples show how to use com.amazonaws.services.kinesis.model.ShardIteratorType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StartingPointShardsFinder.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Validates the shards at the given startingPoint. Validity is checked by getting an iterator at
 * the startingPoint and then trying to read some records. This action does not affect the records
 * at all. If the shard is valid then it will get read from exactly the same point and these
 * records will be read again.
 */
private Set<Shard> validateShards(
    SimplifiedKinesisClient kinesis,
    Iterable<Shard> rootShards,
    String streamName,
    StartingPoint startingPoint)
    throws TransientKinesisException {
  Set<Shard> validShards = new HashSet<>();
  ShardIteratorType shardIteratorType =
      ShardIteratorType.fromValue(startingPoint.getPositionName());
  for (Shard shard : rootShards) {
    String shardIterator =
        kinesis.getShardIterator(
            streamName,
            shard.getShardId(),
            shardIteratorType,
            null,
            startingPoint.getTimestamp());
    GetKinesisRecordsResult records =
        kinesis.getRecords(shardIterator, streamName, shard.getShardId());
    if (records.getNextShardIterator() != null || !records.getRecords().isEmpty()) {
      validShards.add(shard);
    }
  }
  return validShards;
}
 
Example #2
Source File: ShardConsumer.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
protected String getShardIteratorForSentinel(SequenceNumber sentinelSequenceNumber) throws InterruptedException {
	String nextShardItr;

	if (sentinelSequenceNumber.equals(SentinelSequenceNumber.SENTINEL_LATEST_SEQUENCE_NUM.get())) {
		// if the shard is already closed, there will be no latest next record to get for this shard
		if (subscribedShard.isClosed()) {
			nextShardItr = null;
		} else {
			nextShardItr = kinesis.getShardIterator(subscribedShard, ShardIteratorType.LATEST.toString(), null);
		}
	} else if (sentinelSequenceNumber.equals(SentinelSequenceNumber.SENTINEL_EARLIEST_SEQUENCE_NUM.get())) {
		nextShardItr = kinesis.getShardIterator(subscribedShard, ShardIteratorType.TRIM_HORIZON.toString(), null);
	} else if (sentinelSequenceNumber.equals(SentinelSequenceNumber.SENTINEL_SHARD_ENDING_SEQUENCE_NUM.get())) {
		nextShardItr = null;
	} else if (sentinelSequenceNumber.equals(SentinelSequenceNumber.SENTINEL_AT_TIMESTAMP_SEQUENCE_NUM.get())) {
		nextShardItr = kinesis.getShardIterator(subscribedShard, ShardIteratorType.AT_TIMESTAMP.toString(), initTimestamp);
	} else {
		throw new RuntimeException("Unknown sentinel type: " + sentinelSequenceNumber);
	}

	return nextShardItr;
}
 
Example #3
Source File: ShardConsumer.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
protected String getShardIteratorForRealSequenceNumber(SequenceNumber sequenceNumber)
		throws Exception {

	// if the last sequence number refers to an aggregated record, we need to clean up any dangling sub-records
	// from the last aggregated record; otherwise, we can simply start iterating from the record right after.

	if (sequenceNumber.isAggregated()) {
		return getShardIteratorForAggregatedSequenceNumber(sequenceNumber);
	} else {
		// the last record was non-aggregated, so we can simply start from the next record
		return kinesis.getShardIterator(
				subscribedShard,
				ShardIteratorType.AFTER_SEQUENCE_NUMBER.toString(),
				sequenceNumber.getSequenceNumber());
	}
}
 
Example #4
Source File: ShardConsumer.java    From flink with Apache License 2.0 6 votes vote down vote up
protected String getShardIteratorForRealSequenceNumber(SequenceNumber sequenceNumber)
		throws Exception {

	// if the last sequence number refers to an aggregated record, we need to clean up any dangling sub-records
	// from the last aggregated record; otherwise, we can simply start iterating from the record right after.

	if (sequenceNumber.isAggregated()) {
		return getShardIteratorForAggregatedSequenceNumber(sequenceNumber);
	} else {
		// the last record was non-aggregated, so we can simply start from the next record
		return kinesis.getShardIterator(
				subscribedShard,
				ShardIteratorType.AFTER_SEQUENCE_NUMBER.toString(),
				sequenceNumber.getSequenceNumber());
	}
}
 
Example #5
Source File: ShardConsumer.java    From flink with Apache License 2.0 6 votes vote down vote up
protected String getShardIteratorForSentinel(SequenceNumber sentinelSequenceNumber) throws InterruptedException {
	String nextShardItr;

	if (sentinelSequenceNumber.equals(SentinelSequenceNumber.SENTINEL_LATEST_SEQUENCE_NUM.get())) {
		// if the shard is already closed, there will be no latest next record to get for this shard
		if (subscribedShard.isClosed()) {
			nextShardItr = null;
		} else {
			nextShardItr = kinesis.getShardIterator(subscribedShard, ShardIteratorType.LATEST.toString(), null);
		}
	} else if (sentinelSequenceNumber.equals(SentinelSequenceNumber.SENTINEL_EARLIEST_SEQUENCE_NUM.get())) {
		nextShardItr = kinesis.getShardIterator(subscribedShard, ShardIteratorType.TRIM_HORIZON.toString(), null);
	} else if (sentinelSequenceNumber.equals(SentinelSequenceNumber.SENTINEL_SHARD_ENDING_SEQUENCE_NUM.get())) {
		nextShardItr = null;
	} else if (sentinelSequenceNumber.equals(SentinelSequenceNumber.SENTINEL_AT_TIMESTAMP_SEQUENCE_NUM.get())) {
		nextShardItr = kinesis.getShardIterator(subscribedShard, ShardIteratorType.AT_TIMESTAMP.toString(), initTimestamp);
	} else {
		throw new RuntimeException("Unknown sentinel type: " + sentinelSequenceNumber);
	}

	return nextShardItr;
}
 
Example #6
Source File: ShardConsumer.java    From flink with Apache License 2.0 6 votes vote down vote up
protected String getShardIteratorForSentinel(SequenceNumber sentinelSequenceNumber) throws InterruptedException {
	String nextShardItr;

	if (sentinelSequenceNumber.equals(SentinelSequenceNumber.SENTINEL_LATEST_SEQUENCE_NUM.get())) {
		// if the shard is already closed, there will be no latest next record to get for this shard
		if (subscribedShard.isClosed()) {
			nextShardItr = null;
		} else {
			nextShardItr = kinesis.getShardIterator(subscribedShard, ShardIteratorType.LATEST.toString(), null);
		}
	} else if (sentinelSequenceNumber.equals(SentinelSequenceNumber.SENTINEL_EARLIEST_SEQUENCE_NUM.get())) {
		nextShardItr = kinesis.getShardIterator(subscribedShard, ShardIteratorType.TRIM_HORIZON.toString(), null);
	} else if (sentinelSequenceNumber.equals(SentinelSequenceNumber.SENTINEL_SHARD_ENDING_SEQUENCE_NUM.get())) {
		nextShardItr = null;
	} else if (sentinelSequenceNumber.equals(SentinelSequenceNumber.SENTINEL_AT_TIMESTAMP_SEQUENCE_NUM.get())) {
		nextShardItr = kinesis.getShardIterator(subscribedShard, ShardIteratorType.AT_TIMESTAMP.toString(), initTimestamp);
	} else {
		throw new RuntimeException("Unknown sentinel type: " + sentinelSequenceNumber);
	}

	return nextShardItr;
}
 
Example #7
Source File: ShardConsumer.java    From flink with Apache License 2.0 6 votes vote down vote up
protected String getShardIteratorForRealSequenceNumber(SequenceNumber sequenceNumber)
		throws Exception {

	// if the last sequence number refers to an aggregated record, we need to clean up any dangling sub-records
	// from the last aggregated record; otherwise, we can simply start iterating from the record right after.

	if (sequenceNumber.isAggregated()) {
		return getShardIteratorForAggregatedSequenceNumber(sequenceNumber);
	} else {
		// the last record was non-aggregated, so we can simply start from the next record
		return kinesis.getShardIterator(
				subscribedShard,
				ShardIteratorType.AFTER_SEQUENCE_NUMBER.toString(),
				sequenceNumber.getSequenceNumber());
	}
}
 
Example #8
Source File: SimplifiedKinesisClientTest.java    From beam with Apache License 2.0 6 votes vote down vote up
private void shouldHandleGetShardIteratorError(
    Exception thrownException, Class<? extends Exception> expectedExceptionClass) {
  GetShardIteratorRequest request =
      new GetShardIteratorRequest()
          .withStreamName(STREAM)
          .withShardId(SHARD_1)
          .withShardIteratorType(ShardIteratorType.LATEST);

  when(kinesis.getShardIterator(request)).thenThrow(thrownException);

  try {
    underTest.getShardIterator(STREAM, SHARD_1, ShardIteratorType.LATEST, null, null);
    failBecauseExceptionWasNotThrown(expectedExceptionClass);
  } catch (Exception e) {
    assertThat(e).isExactlyInstanceOf(expectedExceptionClass);
  } finally {
    reset(kinesis);
  }
}
 
Example #9
Source File: SimplifiedKinesisClientTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnIteratorStartingWithTimestamp() throws Exception {
  Instant timestamp = Instant.now();
  when(kinesis.getShardIterator(
          new GetShardIteratorRequest()
              .withStreamName(STREAM)
              .withShardId(SHARD_1)
              .withShardIteratorType(ShardIteratorType.AT_SEQUENCE_NUMBER)
              .withTimestamp(timestamp.toDate())))
      .thenReturn(new GetShardIteratorResult().withShardIterator(SHARD_ITERATOR));

  String stream =
      underTest.getShardIterator(
          STREAM, SHARD_1, ShardIteratorType.AT_SEQUENCE_NUMBER, null, timestamp);

  assertThat(stream).isEqualTo(SHARD_ITERATOR);
}
 
Example #10
Source File: SimplifiedKinesisClientTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnIteratorStartingWithSequenceNumber() throws Exception {
  when(kinesis.getShardIterator(
          new GetShardIteratorRequest()
              .withStreamName(STREAM)
              .withShardId(SHARD_1)
              .withShardIteratorType(ShardIteratorType.AT_SEQUENCE_NUMBER)
              .withStartingSequenceNumber(SEQUENCE_NUMBER)))
      .thenReturn(new GetShardIteratorResult().withShardIterator(SHARD_ITERATOR));

  String stream =
      underTest.getShardIterator(
          STREAM, SHARD_1, ShardIteratorType.AT_SEQUENCE_NUMBER, SEQUENCE_NUMBER, null);

  assertThat(stream).isEqualTo(SHARD_ITERATOR);
}
 
Example #11
Source File: SimplifiedKinesisClient.java    From beam with Apache License 2.0 6 votes vote down vote up
public String getShardIterator(
    final String streamName,
    final String shardId,
    final ShardIteratorType shardIteratorType,
    final String startingSequenceNumber,
    final Instant timestamp)
    throws TransientKinesisException {
  final Date date = timestamp != null ? timestamp.toDate() : null;
  return wrapExceptions(
      () ->
          kinesis
              .getShardIterator(
                  new GetShardIteratorRequest()
                      .withStreamName(streamName)
                      .withShardId(shardId)
                      .withShardIteratorType(shardIteratorType)
                      .withStartingSequenceNumber(startingSequenceNumber)
                      .withTimestamp(date))
              .getShardIterator());
}
 
Example #12
Source File: KinesisUtil.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
/**
 * Get the records from the particular shard
 * @param streamName Name of the stream from where the records to be accessed
 * @param recordsLimit Number of records to return from shard
 * @param shId Shard Id of the shard
 * @param iteratorType Shard iterator type
 * @param seqNo Record sequence number
 * @return the list of records from the given shard
 * @throws AmazonClientException
 */
public List<Record> getRecords(String streamName, Integer recordsLimit, String shId, ShardIteratorType iteratorType, String seqNo)
  throws AmazonClientException
{
  assert client != null : "Illegal client";
  try {
    // Create the GetShardIteratorRequest instance and sets streamName, shardId and iteratorType to it
    GetShardIteratorRequest iteratorRequest = new GetShardIteratorRequest();
    iteratorRequest.setStreamName(streamName);
    iteratorRequest.setShardId(shId);
    iteratorRequest.setShardIteratorType(iteratorType);

    // If the iteratorType is AFTER_SEQUENCE_NUMBER, set the sequence No to the iteratorRequest
    if (ShardIteratorType.AFTER_SEQUENCE_NUMBER.equals(iteratorType) ||
        ShardIteratorType.AT_SEQUENCE_NUMBER.equals(iteratorType)) {
      iteratorRequest.setStartingSequenceNumber(seqNo);
    }
    // Get the Response from the getShardIterator service method & get the shardIterator from that response
    GetShardIteratorResult iteratorResponse = client.getShardIterator(iteratorRequest);
    // getShardIterator() specifies the position in the shard
    String iterator = iteratorResponse.getShardIterator();

    // Create the GetRecordsRequest instance and set the recordsLimit and iterator
    GetRecordsRequest getRequest = new GetRecordsRequest();
    getRequest.setLimit(recordsLimit);
    getRequest.setShardIterator(iterator);

    // Get the Response from the getRecords service method and get the data records from that response.
    GetRecordsResult getResponse = client.getRecords(getRequest);
    return getResponse.getRecords();
  } catch (AmazonClientException e) {
    throw new RuntimeException(e);
  }
}
 
Example #13
Source File: KinesisConsumer.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns the iterator type of the given shard
 */
public ShardIteratorType getIteratorType(String shardId)
{
  if (shardPosition.containsKey(shardId)) {
    return ShardIteratorType.AFTER_SEQUENCE_NUMBER;
  }
  return initialOffset.equalsIgnoreCase("earliest") ? ShardIteratorType.TRIM_HORIZON : ShardIteratorType.LATEST;
}
 
Example #14
Source File: ShardConsumer.java    From flink with Apache License 2.0 5 votes vote down vote up
protected String getShardIteratorForAggregatedSequenceNumber(SequenceNumber sequenceNumber)
		throws Exception {

	String itrForLastAggregatedRecord =
			kinesis.getShardIterator(
					subscribedShard,
					ShardIteratorType.AT_SEQUENCE_NUMBER.toString(),
					sequenceNumber.getSequenceNumber());

	// get only the last aggregated record
	GetRecordsResult getRecordsResult = getRecords(itrForLastAggregatedRecord, 1);

	List<UserRecord> fetchedRecords = deaggregateRecords(
			getRecordsResult.getRecords(),
			subscribedShard.getShard().getHashKeyRange().getStartingHashKey(),
			subscribedShard.getShard().getHashKeyRange().getEndingHashKey());

	long lastSubSequenceNum = sequenceNumber.getSubSequenceNumber();
	for (UserRecord record : fetchedRecords) {
		// we have found a dangling sub-record if it has a larger subsequence number
		// than our last sequence number; if so, collect the record and update state
		if (record.getSubSequenceNumber() > lastSubSequenceNum) {
			deserializeRecordForCollectionAndUpdateState(record);
		}
	}

	return getRecordsResult.getNextShardIterator();
}
 
Example #15
Source File: AmazonKinesisMock.java    From beam with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #16
Source File: StartingPointShardsFinderTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private void prepareShard(
    Shard shard,
    String nextIterator,
    ShardIteratorType shardIteratorType,
    Instant startTimestamp) {
  try {
    String shardIterator = shardIteratorType + shard.getShardId() + "-current";
    if (shardIteratorType == ShardIteratorType.AT_TIMESTAMP) {
      when(kinesis.getShardIterator(
              STREAM_NAME,
              shard.getShardId(),
              ShardIteratorType.AT_TIMESTAMP,
              null,
              startTimestamp))
          .thenReturn(shardIterator);
    } else {
      when(kinesis.getShardIterator(
              STREAM_NAME, shard.getShardId(), shardIteratorType, null, null))
          .thenReturn(shardIterator);
    }
    GetKinesisRecordsResult result =
        new GetKinesisRecordsResult(
            Collections.<UserRecord>emptyList(),
            nextIterator,
            0,
            STREAM_NAME,
            shard.getShardId());
    when(kinesis.getRecords(shardIterator, STREAM_NAME, shard.getShardId())).thenReturn(result);
  } catch (TransientKinesisException e) {
    throw new RuntimeException(e);
  }
}
 
Example #17
Source File: StartingPointShardsFinderTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private void activeAtTimestamp(Shard shard, Instant startTimestamp) {
  prepareShard(
      shard,
      "timestampIterator-" + shard.getShardId(),
      ShardIteratorType.AT_TIMESTAMP,
      startTimestamp);
}
 
Example #18
Source File: ShardCheckpoint.java    From beam with Apache License 2.0 5 votes vote down vote up
private ShardCheckpoint(
    String streamName,
    String shardId,
    ShardIteratorType shardIteratorType,
    String sequenceNumber,
    Long subSequenceNumber,
    Instant timestamp) {
  this.shardIteratorType = checkNotNull(shardIteratorType, "shardIteratorType");
  this.streamName = checkNotNull(streamName, "streamName");
  this.shardId = checkNotNull(shardId, "shardId");
  if (shardIteratorType == AT_SEQUENCE_NUMBER || shardIteratorType == AFTER_SEQUENCE_NUMBER) {
    checkNotNull(
        sequenceNumber,
        "You must provide sequence number for AT_SEQUENCE_NUMBER" + " or AFTER_SEQUENCE_NUMBER");
  } else {
    checkArgument(
        sequenceNumber == null,
        "Sequence number must be null for LATEST, TRIM_HORIZON or AT_TIMESTAMP");
  }
  if (shardIteratorType == AT_TIMESTAMP) {
    checkNotNull(timestamp, "You must provide timestamp for AT_TIMESTAMP");
  } else {
    checkArgument(
        timestamp == null, "Timestamp must be null for an iterator type other than AT_TIMESTAMP");
  }

  this.subSequenceNumber = subSequenceNumber;
  this.sequenceNumber = sequenceNumber;
  this.timestamp = timestamp;
}
 
Example #19
Source File: ShardCheckpoint.java    From beam with Apache License 2.0 5 votes vote down vote up
public ShardCheckpoint(
    String streamName,
    String shardId,
    ShardIteratorType shardIteratorType,
    String sequenceNumber,
    Long subSequenceNumber) {
  this(streamName, shardId, shardIteratorType, sequenceNumber, subSequenceNumber, null);
}
 
Example #20
Source File: ShardCheckpoint.java    From beam with Apache License 2.0 5 votes vote down vote up
public ShardCheckpoint(String streamName, String shardId, StartingPoint startingPoint) {
  this(
      streamName,
      shardId,
      ShardIteratorType.fromValue(startingPoint.getPositionName()),
      startingPoint.getTimestamp());
}
 
Example #21
Source File: KinesisBinderTests.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
@Override
protected ExtendedConsumerProperties<KinesisConsumerProperties> createConsumerProperties() {
	ExtendedConsumerProperties<KinesisConsumerProperties> kinesisConsumerProperties = new ExtendedConsumerProperties<>(
			new KinesisConsumerProperties());
	// set the default values that would normally be propagated by Spring Cloud Stream
	kinesisConsumerProperties.setInstanceCount(1);
	kinesisConsumerProperties.setInstanceIndex(0);
	kinesisConsumerProperties.getExtension().setShardIteratorType(ShardIteratorType.TRIM_HORIZON.name());
	return kinesisConsumerProperties;
}
 
Example #22
Source File: KinesisBinderTests.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoCreateStreamForNonExistingStream() throws Exception {
	KinesisTestBinder binder = getBinder();
	DirectChannel output = createBindableChannel("output", new BindingProperties());
	ExtendedConsumerProperties<KinesisConsumerProperties> consumerProperties = createConsumerProperties();
	Date testDate = new Date();
	consumerProperties.getExtension().setShardIteratorType(
			ShardIteratorType.AT_TIMESTAMP.name() + ":" + testDate.getTime());
	String testStreamName = "nonexisting" + System.currentTimeMillis();
	Binding<?> binding = binder.bindConsumer(testStreamName, "test", output,
			consumerProperties);
	binding.unbind();

	DescribeStreamResult streamResult = AMAZON_KINESIS.describeStream(testStreamName);
	String createdStreamName = streamResult.getStreamDescription().getStreamName();
	int createdShards = streamResult.getStreamDescription().getShards().size();
	String createdStreamStatus = streamResult.getStreamDescription()
			.getStreamStatus();

	assertThat(createdStreamName).isEqualTo(testStreamName);
	assertThat(createdShards).isEqualTo(consumerProperties.getInstanceCount()
			* consumerProperties.getConcurrency());
	assertThat(createdStreamStatus).isEqualTo(StreamStatus.ACTIVE.toString());

	KinesisShardOffset shardOffset = TestUtils.getPropertyValue(binding,
			"lifecycle.streamInitialSequence", KinesisShardOffset.class);
	assertThat(shardOffset.getIteratorType())
			.isEqualTo(ShardIteratorType.AT_TIMESTAMP);
	assertThat(shardOffset.getTimestamp()).isEqualTo(testDate);
}
 
Example #23
Source File: KinesisProxy.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String getShardIterator(StreamShardHandle shard, String shardIteratorType, @Nullable Object startingMarker) throws InterruptedException {
	GetShardIteratorRequest getShardIteratorRequest = new GetShardIteratorRequest()
		.withStreamName(shard.getStreamName())
		.withShardId(shard.getShard().getShardId())
		.withShardIteratorType(shardIteratorType);

	switch (ShardIteratorType.fromValue(shardIteratorType)) {
		case TRIM_HORIZON:
		case LATEST:
			break;
		case AT_TIMESTAMP:
			if (startingMarker instanceof Date) {
				getShardIteratorRequest.setTimestamp((Date) startingMarker);
			} else {
				throw new IllegalArgumentException("Invalid object given for GetShardIteratorRequest() when ShardIteratorType is AT_TIMESTAMP. Must be a Date object.");
			}
			break;
		case AT_SEQUENCE_NUMBER:
		case AFTER_SEQUENCE_NUMBER:
			if (startingMarker instanceof String) {
				getShardIteratorRequest.setStartingSequenceNumber((String) startingMarker);
			} else {
				throw new IllegalArgumentException("Invalid object given for GetShardIteratorRequest() when ShardIteratorType is AT_SEQUENCE_NUMBER or AFTER_SEQUENCE_NUMBER. Must be a String.");
			}
	}
	return getShardIterator(getShardIteratorRequest);
}
 
Example #24
Source File: ShardConsumer.java    From flink with Apache License 2.0 5 votes vote down vote up
protected String getShardIteratorForAggregatedSequenceNumber(SequenceNumber sequenceNumber)
		throws Exception {

	String itrForLastAggregatedRecord =
			kinesis.getShardIterator(
					subscribedShard,
					ShardIteratorType.AT_SEQUENCE_NUMBER.toString(),
					sequenceNumber.getSequenceNumber());

	// get only the last aggregated record
	GetRecordsResult getRecordsResult = getRecords(itrForLastAggregatedRecord, 1);

	List<UserRecord> fetchedRecords = deaggregateRecords(
			getRecordsResult.getRecords(),
			subscribedShard.getShard().getHashKeyRange().getStartingHashKey(),
			subscribedShard.getShard().getHashKeyRange().getEndingHashKey());

	long lastSubSequenceNum = sequenceNumber.getSubSequenceNumber();
	for (UserRecord record : fetchedRecords) {
		// we have found a dangling sub-record if it has a larger subsequence number
		// than our last sequence number; if so, collect the record and update state
		if (record.getSubSequenceNumber() > lastSubSequenceNum) {
			deserializeRecordForCollectionAndUpdateState(record);
		}
	}

	return getRecordsResult.getNextShardIterator();
}
 
Example #25
Source File: KinesisProxy.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String getShardIterator(StreamShardHandle shard, String shardIteratorType, @Nullable Object startingMarker) throws InterruptedException {
	GetShardIteratorRequest getShardIteratorRequest = new GetShardIteratorRequest()
		.withStreamName(shard.getStreamName())
		.withShardId(shard.getShard().getShardId())
		.withShardIteratorType(shardIteratorType);

	switch (ShardIteratorType.fromValue(shardIteratorType)) {
		case TRIM_HORIZON:
		case LATEST:
			break;
		case AT_TIMESTAMP:
			if (startingMarker instanceof Date) {
				getShardIteratorRequest.setTimestamp((Date) startingMarker);
			} else {
				throw new IllegalArgumentException("Invalid object given for GetShardIteratorRequest() when ShardIteratorType is AT_TIMESTAMP. Must be a Date object.");
			}
			break;
		case AT_SEQUENCE_NUMBER:
		case AFTER_SEQUENCE_NUMBER:
			if (startingMarker instanceof String) {
				getShardIteratorRequest.setStartingSequenceNumber((String) startingMarker);
			} else {
				throw new IllegalArgumentException("Invalid object given for GetShardIteratorRequest() when ShardIteratorType is AT_SEQUENCE_NUMBER or AFTER_SEQUENCE_NUMBER. Must be a String.");
			}
	}
	return getShardIterator(getShardIteratorRequest);
}
 
Example #26
Source File: ShardConsumer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
protected String getShardIteratorForAggregatedSequenceNumber(SequenceNumber sequenceNumber)
		throws Exception {

	String itrForLastAggregatedRecord =
			kinesis.getShardIterator(
					subscribedShard,
					ShardIteratorType.AT_SEQUENCE_NUMBER.toString(),
					sequenceNumber.getSequenceNumber());

	// get only the last aggregated record
	GetRecordsResult getRecordsResult = getRecords(itrForLastAggregatedRecord, 1);

	List<UserRecord> fetchedRecords = deaggregateRecords(
			getRecordsResult.getRecords(),
			subscribedShard.getShard().getHashKeyRange().getStartingHashKey(),
			subscribedShard.getShard().getHashKeyRange().getEndingHashKey());

	long lastSubSequenceNum = sequenceNumber.getSubSequenceNumber();
	for (UserRecord record : fetchedRecords) {
		// we have found a dangling sub-record if it has a larger subsequence number
		// than our last sequence number; if so, collect the record and update state
		if (record.getSubSequenceNumber() > lastSubSequenceNum) {
			deserializeRecordForCollectionAndUpdateState(record);
		}
	}

	return getRecordsResult.getNextShardIterator();
}
 
Example #27
Source File: KinesisProxy.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String getShardIterator(StreamShardHandle shard, String shardIteratorType, @Nullable Object startingMarker) throws InterruptedException {
	GetShardIteratorRequest getShardIteratorRequest = new GetShardIteratorRequest()
		.withStreamName(shard.getStreamName())
		.withShardId(shard.getShard().getShardId())
		.withShardIteratorType(shardIteratorType);

	switch (ShardIteratorType.fromValue(shardIteratorType)) {
		case TRIM_HORIZON:
		case LATEST:
			break;
		case AT_TIMESTAMP:
			if (startingMarker instanceof Date) {
				getShardIteratorRequest.setTimestamp((Date) startingMarker);
			} else {
				throw new IllegalArgumentException("Invalid object given for GetShardIteratorRequest() when ShardIteratorType is AT_TIMESTAMP. Must be a Date object.");
			}
			break;
		case AT_SEQUENCE_NUMBER:
		case AFTER_SEQUENCE_NUMBER:
			if (startingMarker instanceof String) {
				getShardIteratorRequest.setStartingSequenceNumber((String) startingMarker);
			} else {
				throw new IllegalArgumentException("Invalid object given for GetShardIteratorRequest() when ShardIteratorType is AT_SEQUENCE_NUMBER or AFTER_SEQUENCE_NUMBER. Must be a String.");
			}
	}
	return getShardIterator(getShardIteratorRequest);
}
 
Example #28
Source File: ShardCheckpointTest.java    From beam with Apache License 2.0 4 votes vote down vote up
private ShardCheckpoint checkpoint(
    ShardIteratorType iteratorType, String sequenceNumber, Long subSequenceNumber) {
  return new ShardCheckpoint(
      STREAM_NAME, SHARD_ID, iteratorType, sequenceNumber, subSequenceNumber);
}
 
Example #29
Source File: StartingPointShardsFinderTest.java    From beam with Apache License 2.0 4 votes vote down vote up
private void activeAtPoint(Shard shard, ShardIteratorType shardIteratorType) {
  prepareShard(shard, shardIteratorType.toString() + shard.getShardId(), shardIteratorType, null);
}
 
Example #30
Source File: StartingPointShardsFinderTest.java    From beam with Apache License 2.0 4 votes vote down vote up
private void expiredAtPoint(Shard shard, ShardIteratorType shardIteratorType) {
  prepareShard(shard, null, shardIteratorType, null);
}