software.amazon.awssdk.services.kinesis.model.Record Java Examples

The following examples show how to use software.amazon.awssdk.services.kinesis.model.Record. 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: KinesisDecoderTest.java    From synapse with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildKinesisMessageV2WithCompoundKey() {
    final String json = "{\"_synapse_msg_format\":\"v2\","
            + "\"_synapse_msg_key\":{\"partitionKey\":\"1\",\"compactionKey\":\"2\"},"
            + "\"_synapse_msg_headers\":{\"attr\":\"value\"},"
            + "\"_synapse_msg_payload\":{\"some\":\"payload\"}}";

    final Instant now = Instant.now();
    final Record record = Record.builder()
            .partitionKey("42")
            .data(SdkBytes.fromString(json,UTF_8))
            .approximateArrivalTimestamp(now)
            .sequenceNumber("00001")
            .build();
    final Message<String> message = decoder.apply(new RecordWithShard(
            "some-shard",
            record));
    assertThat(message.getKey(), is(Key.of("1", "2")));
    assertThat(message.getPayload(), is("{\"some\":\"payload\"}"));
    assertThat(message.getHeader().getAsInstant(MSG_ARRIVAL_TS), is(now));
    assertThat(message.getHeader().getShardPosition(), is(Optional.of(fromPosition("some-shard", "00001"))));
    assertThat(message.getHeader().get("attr"), is( "value"));
}
 
Example #2
Source File: ShardShardRecordProcessorCheckpointerTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for
 * {@link ShardRecordProcessorCheckpointer#prepareCheckpoint(Record record)}.
 */
@Test
public final void testPrepareCheckpointRecord() throws Exception {
    ShardRecordProcessorCheckpointer processingCheckpointer =
            new ShardRecordProcessorCheckpointer(shardInfo, checkpoint);
    processingCheckpointer.setInitialCheckpointValue(startingExtendedSequenceNumber);
    ExtendedSequenceNumber extendedSequenceNumber = new ExtendedSequenceNumber("5025");
    Record record = makeRecord("5025");
    processingCheckpointer.largestPermittedCheckpointValue(extendedSequenceNumber);
    PreparedCheckpointer preparedCheckpoint = processingCheckpointer.prepareCheckpoint(record);
    assertThat(checkpoint.getCheckpoint(shardId), equalTo(startingExtendedSequenceNumber));
    assertThat(checkpoint.getCheckpointObject(shardId).checkpoint(), equalTo(startingExtendedSequenceNumber));
    assertThat(preparedCheckpoint.pendingCheckpoint(), equalTo(extendedSequenceNumber));
    assertThat(checkpoint.getCheckpointObject(shardId).pendingCheckpoint(), equalTo(extendedSequenceNumber));

    // Checkpoint using preparedCheckpoint
    preparedCheckpoint.checkpoint();
    assertThat(checkpoint.getCheckpoint(shardId), equalTo(extendedSequenceNumber));
    assertThat(checkpoint.getCheckpointObject(shardId).checkpoint(), equalTo(extendedSequenceNumber));
    assertThat(checkpoint.getCheckpointObject(shardId).pendingCheckpoint(), nullValue());
}
 
Example #3
Source File: RecordDeaggregator.java    From kinesis-aggregation with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private List<KinesisClientRecord> convertType(List<T> inputRecords) throws Exception {
	List<KinesisClientRecord> records = null;

	if (inputRecords.size() > 0 && inputRecords.get(0) instanceof KinesisEventRecord) {
		records = convertToKinesis((List<KinesisEventRecord>) inputRecords);
	} else if (inputRecords.size() > 0 && inputRecords.get(0) instanceof Record) {
		records = new ArrayList<>();
		for (Record rec : (List<Record>) inputRecords) {
			records.add(KinesisClientRecord.fromRecord((Record) rec));
		}
	} else {
		if (inputRecords.size() == 0) {
			return new ArrayList<KinesisClientRecord>();
		} else {
			throw new Exception("Input Types must be Kinesis Event or Model Records");
		}
	}

	return records;
}
 
Example #4
Source File: KinesisShardResponseTest.java    From synapse with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldImplementEqualsAndHashCode() {
    final Instant now = now();
    final GetRecordsResponse response = GetRecordsResponse
            .builder()
            .records(Record.builder()
                    .sequenceNumber("1")
                    .partitionKey("first")
                    .approximateArrivalTimestamp(now)
                    .build())
            .nextShardIterator("nextIter")
            .millisBehindLatest(0L)
            .build();
    final ShardResponse first = kinesisShardResponse(fromPosition("shard", "42"), response);
    final ShardResponse second = kinesisShardResponse(fromPosition("shard", "42"), response);

    assertThat(first.equals(second), is(true));
    assertThat(first.hashCode(), is(second.hashCode()));
}
 
Example #5
Source File: KinesisDecoderTest.java    From synapse with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildMinimalKinesisDeletionMessageV2() {
    final String json = "{\"_synapse_msg_format\":\"v2\"}";

    final Instant now = Instant.now();
    final Record record = Record.builder()
            .partitionKey("42")
            .data(SdkBytes.fromString(json,UTF_8))
            .approximateArrivalTimestamp(now)
            .sequenceNumber("00001")
            .build();
    final Message<String> message = decoder.apply(new RecordWithShard(
            "some-shard",
            record));
    assertThat(message.getKey(), is(Key.of("42")));
    assertThat(message.getPayload(), is(nullValue()));
    assertThat(message.getHeader().getShardPosition(), is(Optional.of(fromPosition("some-shard", "00001"))));
    assertThat(message.getHeader().getAsInstant(MSG_ARRIVAL_TS), is(now));
}
 
Example #6
Source File: KinesisDecoderTest.java    From synapse with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildKinesisDeletionMessageWithoutHeadersV2() {
    final String json = "{\"_synapse_msg_format\":\"v2\","
            + "\"_synapse_msg_headers\":{},"
            + "\"_synapse_msg_payload\":null}";

    final Instant now = Instant.now();
    final Record record = Record.builder()
            .partitionKey("42")
            .data(SdkBytes.fromString(json,UTF_8))
            .approximateArrivalTimestamp(now)
            .sequenceNumber("00001")
            .build();
    final Message<String> message = decoder.apply(new RecordWithShard(
            "some-shard",
            record));
    assertThat(message.getKey(), is(Key.of("42")));
    assertThat(message.getPayload(), is(nullValue()));
    assertThat(message.getHeader().getShardPosition(), is(Optional.of(fromPosition("some-shard", "00001"))));
    assertThat(message.getHeader().getAsInstant(MSG_ARRIVAL_TS), is(now));
}
 
Example #7
Source File: KinesisDecoderTest.java    From synapse with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildKinesisDeletionMessageV2() {
    final String json = "{\n   \"_synapse_msg_format\"  :  \"v2\",     "
            + "\"_synapse_msg_headers\":{\"attr\":\"value\"},"
            + "\"_synapse_msg_payload\":null}";

    final Instant now = Instant.now();
    final Record record = Record.builder()
            .partitionKey("42")
            .data(SdkBytes.fromString(json,UTF_8))
            .approximateArrivalTimestamp(now)
            .sequenceNumber("00001")
            .build();
    final Message<String> message = decoder.apply(new RecordWithShard(
            "some-shard",
            record));
    assertThat(message.getKey(), is(Key.of("42")));
    assertThat(message.getPayload(), is(nullValue()));
    assertThat(message.getHeader().getShardPosition(), is(Optional.of(fromPosition("some-shard", "00001"))));
    assertThat(message.getHeader().get("attr"), is("value"));
    assertThat(message.getHeader().getAsInstant(MSG_ARRIVAL_TS), is(now));
}
 
Example #8
Source File: KinesisDecoderTest.java    From synapse with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildKinesisMessageV2() {
    final String json = "{\"_synapse_msg_format\":\"v2\","
            + "\"_synapse_msg_headers\":{\"attr\":\"value\"},"
            + "\"_synapse_msg_payload\":{\"some\":\"payload\"}}";

    final Instant now = Instant.now();
    final Record record = Record.builder()
            .partitionKey("42")
            .data(SdkBytes.fromString(json,UTF_8))
            .approximateArrivalTimestamp(now)
            .sequenceNumber("00001")
            .build();
    final Message<String> message = decoder.apply(new RecordWithShard(
            "some-shard",
            record));
    assertThat(message.getKey(), is(Key.of("42")));
    assertThat(message.getPayload(), is("{\"some\":\"payload\"}"));
    assertThat(message.getHeader().getAsInstant(MSG_ARRIVAL_TS), is(now));
    assertThat(message.getHeader().getShardPosition(), is(Optional.of(fromPosition("some-shard", "00001"))));
    assertThat(message.getHeader().get("attr"), is("value"));
}
 
Example #9
Source File: KinesisDecoderTest.java    From synapse with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildKinesisMessage() {
    final Instant now = Instant.now();
    final Record record = Record.builder()
            .partitionKey("42")
            .data(SdkBytes.fromString("ßome dätä",UTF_8))
            .approximateArrivalTimestamp(now)
            .sequenceNumber("00001")
            .build();
    final Message<String> message = decoder.apply(new RecordWithShard("some-shard",
            record));
    assertThat(message.getKey(), is(Key.of("42")));
    assertThat(message.getPayload(), is("ßome dätä"));
    assertThat(message.getHeader().getAsInstant(MSG_ARRIVAL_TS), is(now));
    assertThat(message.getHeader().getShardPosition(), is(Optional.of(fromPosition("some-shard", "00001"))));
}
 
Example #10
Source File: ShardShardRecordProcessorCheckpointerTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for
 * {@link ShardRecordProcessorCheckpointer#prepareCheckpoint(Record record)}.
 */
@Test
public final void testPrepareCheckpointSubRecord() throws Exception {
    ShardRecordProcessorCheckpointer processingCheckpointer =
            new ShardRecordProcessorCheckpointer(shardInfo, checkpoint);
    processingCheckpointer.setInitialCheckpointValue(startingExtendedSequenceNumber);
    ExtendedSequenceNumber extendedSequenceNumber = new ExtendedSequenceNumber("5030");
    Record record = makeRecord("5030");
    //UserRecord subRecord = new UserRecord(record);
    processingCheckpointer.largestPermittedCheckpointValue(extendedSequenceNumber);
    PreparedCheckpointer preparedCheckpoint = processingCheckpointer.prepareCheckpoint(record);
    assertThat(checkpoint.getCheckpoint(shardId), equalTo(startingExtendedSequenceNumber));
    assertThat(checkpoint.getCheckpointObject(shardId).checkpoint(), equalTo(startingExtendedSequenceNumber));
    assertThat(preparedCheckpoint.pendingCheckpoint(), equalTo(extendedSequenceNumber));
    assertThat(checkpoint.getCheckpointObject(shardId).pendingCheckpoint(), equalTo(extendedSequenceNumber));

    // Checkpoint using preparedCheckpoint
    preparedCheckpoint.checkpoint();
    assertThat(checkpoint.getCheckpoint(shardId), equalTo(extendedSequenceNumber));
    assertThat(checkpoint.getCheckpointObject(shardId).checkpoint(), equalTo(extendedSequenceNumber));
    assertThat(checkpoint.getCheckpointObject(shardId).pendingCheckpoint(), nullValue());
}
 
Example #11
Source File: KinesisDataFetcherTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void testRestartIterator() throws Exception {
    GetRecordsResponse getRecordsResult = mock(GetRecordsResponse.class);
    GetRecordsResponse restartGetRecordsResponse = makeGetRecordsResponse(null, null).get(anyLong(),
            any(TimeUnit.class));
    Record record = mock(Record.class);
    final String nextShardIterator = "NextShardIterator";
    final String sequenceNumber = "SequenceNumber";

    when(getRecordsResult.records()).thenReturn(Collections.singletonList(record));
    when(getRecordsResult.nextShardIterator()).thenReturn(nextShardIterator);
    when(record.sequenceNumber()).thenReturn(sequenceNumber);

    kinesisDataFetcher.initialize(InitialPositionInStream.LATEST.toString(), INITIAL_POSITION_LATEST);
    assertEquals(getRecordsResult, kinesisDataFetcher.getRecords().accept());

    kinesisDataFetcher.restartIterator();
    assertEquals(restartGetRecordsResponse, kinesisDataFetcher.getRecords().accept());
}
 
Example #12
Source File: DateTimeUnmarshallingTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void cborDisabled_dateUnmarshalling_shouldSucceed() {
    String content = content();
    int length = content.getBytes().length;
    Instant instant =  Instant.ofEpochMilli(1548118964772L);

    stubFor(post(anyUrl())
                .willReturn(aResponse().withStatus(200)
                                       .withHeader("Content-Length", String.valueOf(length))
                                       .withBody(content)));


    List<Record> records = client.getRecords(b -> b.shardIterator("test")).records();

    assertThat(records).isNotEmpty();
    assertThat(records.get(0).approximateArrivalTimestamp()).isEqualTo(instant);
}
 
Example #13
Source File: PrefetchRecordsPublisherTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void testFullCacheByteSize() {
    record = Record.builder().data(createByteBufferWithSize(SIZE_1_MB)).build();

    when(records.size()).thenReturn(500);

    records.add(record);

    getRecordsCache.start(sequenceNumber, initialPosition);

    // Sleep for a few seconds for the cache to fill up.
    sleep(2000);

    verify(getRecordsRetrievalStrategy, times(3)).getRecords(eq(MAX_RECORDS_PER_CALL));
    assertEquals(spyQueue.size(), 3);
}
 
Example #14
Source File: PrefetchRecordsPublisherTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetRecords() {
    record = Record.builder().data(createByteBufferWithSize(SIZE_512_KB)).build();

    when(records.size()).thenReturn(1000);

    final List<KinesisClientRecord> expectedRecords = records.stream()
            .map(KinesisClientRecord::fromRecord).collect(Collectors.toList());

    getRecordsCache.start(sequenceNumber, initialPosition);
    ProcessRecordsInput result = blockUntilRecordsAvailable(() -> evictPublishedEvent(getRecordsCache, "shardId"), 1000L)
            .processRecordsInput();

    assertEquals(expectedRecords, result.records());

    verify(executorService).execute(any());
    verify(getRecordsRetrievalStrategy, atLeast(1)).getRecords(eq(MAX_RECORDS_PER_CALL));
}
 
Example #15
Source File: KinesisDataFetcherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
private void testInitializeAndFetch(final String iteratorType, final String seqNo,
        final InitialPositionInStreamExtended initialPositionInStream) throws Exception {
    final ArgumentCaptor<GetShardIteratorRequest> iteratorCaptor = ArgumentCaptor
            .forClass(GetShardIteratorRequest.class);
    final ArgumentCaptor<GetRecordsRequest> recordsCaptor = ArgumentCaptor.forClass(GetRecordsRequest.class);
    final String iterator = "foo";
    final List<Record> expectedRecords = Collections.emptyList();
    GetShardIteratorRequest expectedIteratorRequest = makeGetShardIteratorRequest(iteratorType);
    if (iteratorType.equals(ShardIteratorType.AT_TIMESTAMP.toString())) {
        expectedIteratorRequest = expectedIteratorRequest.toBuilder()
                .timestamp(initialPositionInStream.getTimestamp().toInstant()).build();
    } else if (iteratorType.equals(ShardIteratorType.AT_SEQUENCE_NUMBER.toString())) {
        expectedIteratorRequest = expectedIteratorRequest.toBuilder().startingSequenceNumber(seqNo).build();
    }
    final GetRecordsRequest expectedRecordsRequest = makeGetRecordsRequest(iterator);

    when(kinesisClient.getShardIterator(iteratorCaptor.capture()))
            .thenReturn(makeGetShardIteratorResonse(iterator));

    when(kinesisClient.getRecords(recordsCaptor.capture()))
            .thenReturn(makeGetRecordsResponse(null, expectedRecords));

    Checkpointer checkpoint = mock(Checkpointer.class);
    when(checkpoint.getCheckpoint(SHARD_ID)).thenReturn(new ExtendedSequenceNumber(seqNo));

    final GetRecordsRetrievalStrategy getRecordsRetrievalStrategy = new SynchronousGetRecordsRetrievalStrategy(
            kinesisDataFetcher);
    kinesisDataFetcher.initialize(seqNo, initialPositionInStream);

    assertEquals(expectedRecords, getRecordsRetrievalStrategy.getRecords(MAX_RECORDS).records());
    verify(kinesisClient, times(1)).getShardIterator(any(GetShardIteratorRequest.class));
    verify(kinesisClient, times(1)).getRecords(any(GetRecordsRequest.class));
}
 
Example #16
Source File: TestDirectDeaggregation.java    From kinesis-aggregation with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
	aggregator = new RecordAggregator();

	recordList = new LinkedList<>();

	// create 10 random records for testing
	for (int i = 0; i < c; i++) {
		// create trackable id
		String id = UUID.randomUUID().toString();

		// create a kinesis model record
		byte[] data = RandomStringUtils.randomAlphabetic(20).getBytes();

		Record r = Record.builder().partitionKey(id)
				.approximateArrivalTimestamp(new Date(System.currentTimeMillis()).toInstant())
				.data(SdkBytes.fromByteArray(data)).build();
		recordList.add(r);

		// add the record to the check set
		checkset.put(id, r);

		// add the record to the aggregated AggRecord // create an aggregated set of
		aggregator.addUserRecord(id, data);
	}

	// get the aggregated data
	aggregated = aggregator.clearAndGet();
	assertEquals("Aggregated Record Count Correct", aggregated.getNumUserRecords(), c);
}
 
Example #17
Source File: PrefetchRecordsPublisherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void testMultipleCacheCalls() {
    int recordsSize = 20;
    record = Record.builder().data(createByteBufferWithSize(1024)).build();

    IntStream.range(0, recordsSize).forEach(i -> records.add(record));
    final List<KinesisClientRecord> expectedRecords = records.stream()
            .map(KinesisClientRecord::fromRecord).collect(Collectors.toList());

    getRecordsCache.start(sequenceNumber, initialPosition);
    ProcessRecordsInput processRecordsInput = evictPublishedEvent(getRecordsCache, "shardId").processRecordsInput();

    verify(executorService).execute(any());
    assertEquals(expectedRecords, processRecordsInput.records());
    assertNotNull(processRecordsInput.cacheEntryTime());
    assertNotNull(processRecordsInput.cacheExitTime());

    sleep(2000);

    ProcessRecordsInput processRecordsInput2 = evictPublishedEvent(getRecordsCache, "shardId").processRecordsInput();
    assertNotEquals(processRecordsInput, processRecordsInput2);
    assertEquals(expectedRecords, processRecordsInput2.records());
    assertNotEquals(processRecordsInput2.timeSpentInCache(), Duration.ZERO);

    assertTrue(spyQueue.size() <= MAX_SIZE);
}
 
Example #18
Source File: SubscribeToShardIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void subscribeToShard_ReceivesAllData() {
    List<SdkBytes> producedData = new ArrayList<>();
    ScheduledExecutorService producer = Executors.newScheduledThreadPool(1);
    // Delay it a bit to allow us to subscribe first
    producer.scheduleAtFixedRate(() -> putRecord().ifPresent(producedData::add), 10, 1, TimeUnit.SECONDS);

    List<SdkBytes> receivedData = new ArrayList<>();
    // Add every event's data to the receivedData list
    Consumer<SubscribeToShardEvent> eventConsumer = s -> receivedData.addAll(
        s.records().stream()
         .map(Record::data)
         .collect(Collectors.toList()));
    asyncClient.subscribeToShard(r -> r.consumerARN(consumerArn)
                                       .shardId(shardId)
                                       .startingPosition(s -> s.type(ShardIteratorType.LATEST)),
                                 SubscribeToShardResponseHandler.builder()
                                                                .onEventStream(p -> p.filter(SubscribeToShardEvent.class)
                                                                                     .subscribe(eventConsumer))
                                                                .onResponse(this::verifyHttpMetadata)
                                                                .build())
               .join();
    producer.shutdown();
    // Make sure we all the data we received was data we published, we may have published more
    // if the producer isn't shutdown immediately after we finish subscribing.
    assertThat(producedData).containsSequence(receivedData);
}
 
Example #19
Source File: ShardShardRecordProcessorCheckpointerTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link ShardRecordProcessorCheckpointer#checkpoint(Record record)}.
 */    
@Test
public final void testCheckpointRecord() throws Exception {
	ShardRecordProcessorCheckpointer processingCheckpointer =
            new ShardRecordProcessorCheckpointer(shardInfo, checkpoint);
	processingCheckpointer.setInitialCheckpointValue(startingExtendedSequenceNumber);
	ExtendedSequenceNumber extendedSequenceNumber = new ExtendedSequenceNumber("5025");
	Record record = makeRecord("5025");
    processingCheckpointer.largestPermittedCheckpointValue(extendedSequenceNumber);
    processingCheckpointer.checkpoint(record);
    assertThat(checkpoint.getCheckpoint(shardId), equalTo(extendedSequenceNumber));
}
 
Example #20
Source File: ShardRecordProcessorCheckpointer.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public synchronized PreparedCheckpointer prepareCheckpoint(Record record)
        throws KinesisClientLibDependencyException, InvalidStateException, ThrottlingException, ShutdownException {
    //
    // TODO: UserRecord Deprecation
    //
    if (record == null) {
        throw new IllegalArgumentException("Could not prepare checkpoint a null record");
    } /*else if (record instanceof UserRecord) {
        return prepareCheckpoint(record.sequenceNumber(), ((UserRecord) record).subSequenceNumber());
    } */ else {
        return prepareCheckpoint(record.sequenceNumber(), 0);
    }
}
 
Example #21
Source File: ShardRecordProcessorCheckpointer.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public synchronized void checkpoint(Record record)
    throws KinesisClientLibDependencyException, InvalidStateException, ThrottlingException, ShutdownException,
    IllegalArgumentException {

    // TODO: UserRecord Deprecation
    if (record == null) {
        throw new IllegalArgumentException("Could not checkpoint a null record");
    } /* else if (record instanceof UserRecord) {
        checkpoint(record.sequenceNumber(), ((UserRecord) record).subSequenceNumber());
    } */ else {
        checkpoint(record.sequenceNumber(), 0);
    }
}
 
Example #22
Source File: ShardShardRecordProcessorCheckpointerTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link ShardRecordProcessorCheckpointer#checkpoint(Record record)}.
 */
@Test
public final void testCheckpointSubRecord() throws Exception {
	ShardRecordProcessorCheckpointer processingCheckpointer =
            new ShardRecordProcessorCheckpointer(shardInfo, checkpoint);
	processingCheckpointer.setInitialCheckpointValue(startingExtendedSequenceNumber);
	ExtendedSequenceNumber extendedSequenceNumber = new ExtendedSequenceNumber("5030");
	Record record = makeRecord("5030");
    //UserRecord subRecord = new UserRecord(record);
	processingCheckpointer.largestPermittedCheckpointValue(extendedSequenceNumber);
    processingCheckpointer.checkpoint(record);
    assertThat(checkpoint.getCheckpoint(shardId), equalTo(extendedSequenceNumber));
}
 
Example #23
Source File: FanOutRecordsPublisherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
public KinesisClientRecordMatcher(Record record) {
    expected = KinesisClientRecord.fromRecord(record);
    partitionKeyMatcher = equalTo(expected.partitionKey());
    sequenceNumberMatcher = equalTo(expected.sequenceNumber());
    approximateArrivalMatcher = equalTo(expected.approximateArrivalTimestamp());
    dataMatcher = equalTo(expected.data());

}
 
Example #24
Source File: TestDirectDeaggregation.java    From kinesis-aggregation with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmpty() throws Exception {
	// invoke deaggregation on the static records, returning a List of UserRecord
	List<KinesisClientRecord> records = deaggregator.deaggregate(new ArrayList<Record>());

	assertEquals("Processed Record Count Correct", records.size(), 0);
	verifyOneToOneMapping(records);
}
 
Example #25
Source File: TestDirectDeaggregation.java    From kinesis-aggregation with Apache License 2.0 5 votes vote down vote up
@Test
public void testAggregatedRecord() throws Exception {
	// create a new KinesisEvent.Record from the aggregated data
	Record r = Record.builder().partitionKey(aggregated.getPartitionKey())
			.approximateArrivalTimestamp(new Date(System.currentTimeMillis()).toInstant())
			.data(SdkBytes.fromByteArray(aggregated.toRecordBytes())).build();

	// deaggregate the record
	List<KinesisClientRecord> userRecords = deaggregator.deaggregate(Arrays.asList(r));

	assertEquals("Deaggregated Count Matches", aggregated.getNumUserRecords(), userRecords.size());
	verifyOneToOneMapping(userRecords);
}
 
Example #26
Source File: TestDirectDeaggregation.java    From kinesis-aggregation with Apache License 2.0 5 votes vote down vote up
private void verifyOneToOneMapping(List<KinesisClientRecord> userRecords) {
	userRecords.stream().forEachOrdered(userRecord -> {
		// get the original checkset record by ID
		Record toCheck = checkset.get(userRecord.partitionKey());

		// confirm that toCheck is not null
		assertNotNull("Found Original CheckSet Record", toCheck);

		// confirm that the data is the same
		assertTrue("Data Correct", userRecord.data().compareTo(toCheck.data().asByteBuffer()) == 0);
	});
}
 
Example #27
Source File: RecordDeaggregator.java    From kinesis-aggregation with Apache License 2.0 5 votes vote down vote up
private Record convertOne(KinesisEventRecord record) {
	KinesisEvent.Record r = record.getKinesis();
	Record out = Record.builder().partitionKey(r.getPartitionKey()).encryptionType(r.getEncryptionType())
			.approximateArrivalTimestamp(r.getApproximateArrivalTimestamp().toInstant())
			.sequenceNumber(r.getSequenceNumber()).data(SdkBytes.fromByteBuffer(r.getData())).build();

	return out;

}
 
Example #28
Source File: KinesisIntegrationTests.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void validateRecord(final Record record, String data) {
    Assert.assertNotNull(record);

    Assert.assertNotNull(record.sequenceNumber());
    new BigInteger(record.sequenceNumber());

    String value = record.data() == null ? null : record.data().asUtf8String();
    Assert.assertEquals(data, value);

    Assert.assertNotNull(record.partitionKey());

    // The timestamp should be relatively recent
    Assert.assertTrue(Duration.between(record.approximateArrivalTimestamp(), Instant.now()).toMinutes() < 5);
}
 
Example #29
Source File: KinesisShardResponseTest.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertRecordsToMessages() {
    final Instant firstArrival = now().minus(1, SECONDS);
    final Instant secondArrival = now();
    final GetRecordsResponse recordsResponse = GetRecordsResponse
            .builder()
            .records(
                    Record.builder()
                            .sequenceNumber("1")
                            .approximateArrivalTimestamp(firstArrival)
                            .partitionKey("first")
                            .data(SdkBytes.fromByteArray("content".getBytes(UTF_8)))
                            .build(),
                    Record.builder()
                            .sequenceNumber("2")
                            .approximateArrivalTimestamp(secondArrival)
                            .partitionKey("second")
                            .data(SdkBytes.fromByteArray("content".getBytes(UTF_8)))
                            .build()
                    )
            .nextShardIterator("nextIter")
            .millisBehindLatest(1L)
            .build();
    final ShardResponse response = kinesisShardResponse(fromPosition("shard", "42"), recordsResponse);

    assertThat(response.getShardName(), is("shard"));
    assertThat(response.getDurationBehind(), is(Duration.ofMillis(1L)));
    assertThat(response.getShardPosition(), is(fromPosition("shard", "42")));
    assertThat(response.getMessages(), contains(
            message("first", Header.of(fromPosition("shard", "1"), ImmutableMap.of(MSG_ARRIVAL_TS.key(), firstArrival.toString())), "content"),
            message("second", Header.of(fromPosition("shard", "2"), ImmutableMap.of(MSG_ARRIVAL_TS.key(), secondArrival.toString())), "content")
    ));
}
 
Example #30
Source File: SubscribeToShardUnmarshallingTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void eventWithRecords_UnmarshalledCorrectly() throws Throwable {
    String data = BinaryUtils.toBase64("foobar".getBytes(StandardCharsets.UTF_8));
    AbortableInputStream content = new MessageWriter()
        .writeInitialResponse(new byte[0])
        .writeEvent("SubscribeToShardEvent",
                    String.format("{\"ContinuationSequenceNumber\": \"1234\","
                                  + "\"MillisBehindLatest\": 0,"
                                  + "\"Records\": [{\"Data\": \"%s\"}]"
                                  + "}", data))
        .toInputStream();
    SubscribeToShardEvent event = SubscribeToShardEvent.builder()
                                                       .continuationSequenceNumber("1234")
                                                       .millisBehindLatest(0L)
                                                       .records(Record.builder()
                                                                      .data(SdkBytes.fromUtf8String("foobar"))
                                                                      .build())
                                                       .build();

    stubResponse(SdkHttpFullResponse.builder()
                                    .statusCode(200)
                                    .content(content)
                                    .build());

    List<SubscribeToShardEventStream> events = subscribeToShard();
    assertThat(events).containsOnly(event);
}