software.amazon.kinesis.retrieval.KinesisClientRecord Java Examples

The following examples show how to use software.amazon.kinesis.retrieval.KinesisClientRecord. 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: TestLambdaDeaggregation.java    From kinesis-aggregation with Apache License 2.0 6 votes vote down vote up
@Test
public void testAggregatedRecord() throws Exception {
	// create a new KinesisEvent.Record from the aggregated data
	KinesisEvent.Record r = new KinesisEvent.Record();
	r.setPartitionKey(aggregated.getPartitionKey());
	r.setApproximateArrivalTimestamp(new Date(System.currentTimeMillis()));
	r.setData(ByteBuffer.wrap(aggregated.toRecordBytes()));
	r.setKinesisSchemaVersion("1.0");
	KinesisEventRecord ker = new KinesisEventRecord();
	ker.setKinesis(r);

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

	assertEquals("Deaggregated Count Matches", aggregated.getNumUserRecords(), userRecords.size());
	verifyOneToOneMapping(userRecords);
}
 
Example #2
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 #3
Source File: StreamingShardRecordProcessorTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
private void phases(Answer<StatusMessage> answer) throws InterruptedException, ExecutionException {
    /*
     * Return a status message for each call
     * Plan is:
     * initialize
     * processRecords
     * processRecords
     * shutdown
     */
    when(messageFuture.get()).thenAnswer(answer);
    when(messageReader.getNextMessageFromSTDOUT()).thenReturn(messageFuture);

    List<KinesisClientRecord> testRecords = Collections.emptyList();

    recordProcessor.initialize(InitializationInput.builder().shardId(shardId).build());
    recordProcessor.processRecords(ProcessRecordsInput.builder().records(testRecords)
            .checkpointer(unimplementedCheckpointer).build());
    recordProcessor.processRecords(ProcessRecordsInput.builder().records(testRecords)
            .checkpointer(unimplementedCheckpointer).build());
    recordProcessor.leaseLost(LeaseLostInput.builder().build());
}
 
Example #4
Source File: ProcessTaskTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonAggregatedKinesisRecord() {
    final String sqn = new BigInteger(128, new Random()).toString();
    final String pk = UUID.randomUUID().toString();
    final Date ts = new Date(System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(4, TimeUnit.HOURS));
    final KinesisClientRecord r = makeKinesisClientRecord(pk, sqn, ts.toInstant());

    ShardRecordProcessorOutcome outcome = testWithRecord(r);

    assertEquals(1, outcome.getProcessRecordsCall().records().size());

    KinesisClientRecord pr = outcome.getProcessRecordsCall().records().get(0);
    assertEquals(pk, pr.partitionKey());
    assertEquals(ts.toInstant(), pr.approximateArrivalTimestamp());
    byte[] b = pr.data().array();
    assertThat(b, equalTo(TEST_DATA));

    assertEquals(sqn, outcome.getCheckpointCall().sequenceNumber());
    assertEquals(0, outcome.getCheckpointCall().subSequenceNumber());
}
 
Example #5
Source File: ProcessTaskTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeaggregatesRecordWithNoArrivalTimestamp() {
    final String sqn = new BigInteger(128, new Random()).toString();
    final String pk = UUID.randomUUID().toString();

    KinesisClientRecord record = KinesisClientRecord.builder().partitionKey("-").data(generateAggregatedRecord(pk))
            .sequenceNumber(sqn).build();

    processTask = makeProcessTask(processRecordsInput);
    ShardRecordProcessorOutcome outcome = testWithRecord(record);

    List<KinesisClientRecord> actualRecords = outcome.getProcessRecordsCall().records();

    assertEquals(3, actualRecords.size());
    for (KinesisClientRecord actualRecord : actualRecords) {
        assertThat(actualRecord.partitionKey(), equalTo(pk));
        assertThat(actualRecord.approximateArrivalTimestamp(), nullValue());
    }
}
 
Example #6
Source File: EchoHandler.java    From kinesis-aggregation with Apache License 2.0 6 votes vote down vote up
@Override
public Void handleRequest(KinesisEvent event, Context context) {
	LambdaLogger logger = context.getLogger();

	// extract the records from the event
	List<KinesisEventRecord> records = event.getRecords();

	logger.log(String.format("Recieved %s Raw Records", records.size()));

	try {
		// now deaggregate the message contents
		List<KinesisClientRecord> deaggregated = new RecordDeaggregator<KinesisEventRecord>().deaggregate(records);
		logger.log(String.format("Received %s Deaggregated User Records", deaggregated.size()));

		deaggregated.stream().forEachOrdered(rec -> {
			logger.log(rec.partitionKey());
		});
	} catch (Exception e) {
		logger.log(e.getMessage());
	}

	return null;
}
 
Example #7
Source File: ProcessTaskTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testLargestPermittedCheckpointValue() {
    // Some sequence number value from previous processRecords call to mock.
    final BigInteger previousCheckpointSqn = new BigInteger(128, new Random());

    // Values for this processRecords call.
    final int numberOfRecords = 104;
    // Start these batch of records's sequence number that is greater than previous checkpoint value.
    final BigInteger startingSqn = previousCheckpointSqn.add(BigInteger.valueOf(10));
    final List<KinesisClientRecord> records = generateConsecutiveRecords(numberOfRecords, "-", ByteBuffer.wrap(TEST_DATA),
            new Date(), startingSqn);

    processTask = makeProcessTask(processRecordsInput);
    ShardRecordProcessorOutcome outcome = testWithRecords(records,
            new ExtendedSequenceNumber(previousCheckpointSqn.toString()),
            new ExtendedSequenceNumber(previousCheckpointSqn.toString()));

    final ExtendedSequenceNumber expectedLargestPermittedEsqn = new ExtendedSequenceNumber(
            startingSqn.add(BigInteger.valueOf(numberOfRecords - 1)).toString());
    assertEquals(expectedLargestPermittedEsqn, outcome.getCheckpointCall());
}
 
Example #8
Source File: ProcessTask.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
/**
 * Dispatches a batch of records to the record processor, and handles any fallout from that.
 *
 * @param input
 *            the result of the last call to Kinesis
 * @param records
 *            the records to be dispatched. It's possible the records have been adjusted by KPL deaggregation.
 */
private void callProcessRecords(ProcessRecordsInput input, List<KinesisClientRecord> records) {
    log.debug("Calling application processRecords() with {} records from {}", records.size(),
            shardInfo.shardId());

    final ProcessRecordsInput processRecordsInput = ProcessRecordsInput.builder().records(records).cacheExitTime(input.cacheExitTime()).cacheEntryTime(input.cacheEntryTime())
            .checkpointer(recordProcessorCheckpointer).millisBehindLatest(input.millisBehindLatest()).build();

    final MetricsScope scope = MetricsUtil.createMetricsWithOperation(metricsFactory, PROCESS_TASK_OPERATION);
    MetricsUtil.addShardId(scope, shardInfo.shardId());
    final long startTime = System.currentTimeMillis();
    try {
        shardRecordProcessor.processRecords(processRecordsInput);
    } catch (Exception e) {
        log.error("ShardId {}: Application processRecords() threw an exception when processing shard ",
                shardInfo.shardId(), e);
        log.error("ShardId {}: Skipping over the following data records: {}", shardInfo.shardId(), records);
    } finally {
        MetricsUtil.addLatency(scope, RECORD_PROCESSOR_PROCESS_RECORDS_METRIC, startTime, MetricsLevel.SUMMARY);
        MetricsUtil.endScope(scope);
    }
}
 
Example #9
Source File: ProcessTaskTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
private KinesisClientRecord createAndRegisterAggregatedRecord(BigInteger sequenceNumber,
        AggregatedRecord.Builder aggregatedRecord, int i, Instant approximateArrivalTime) {
    byte[] dataArray = new byte[1024];
    ThreadLocalRandom.current().nextBytes(dataArray);
    ByteBuffer data = ByteBuffer.wrap(dataArray);

    KinesisClientRecord expectedRecord = KinesisClientRecord.builder().partitionKey("p-" + i)
            .sequenceNumber(sequenceNumber.toString()).approximateArrivalTimestamp(approximateArrivalTime)
            .data(data).subSequenceNumber(i).aggregated(true).build();

    Messages.Record kplRecord = Messages.Record.newBuilder().setData(ByteString.copyFrom(dataArray))
            .setPartitionKeyIndex(i).build();
    aggregatedRecord.addPartitionKeyTable(expectedRecord.partitionKey()).addRecords(kplRecord);

    return expectedRecord;
}
 
Example #10
Source File: KinesisRecordProcessor.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void processRecords(ProcessRecordsInput processRecordsInput) {

    log.info("Processing " + processRecordsInput.records().size() + " records from " + kinesisShardId);

    for (KinesisClientRecord record : processRecordsInput.records()) {
        try {
            queue.put(new KinesisRecord(record));
        } catch (InterruptedException e) {
            log.warn("unable to create KinesisRecord ", e);
        }
    }

    // Checkpoint once every checkpoint interval.
    if (System.nanoTime() > nextCheckpointTimeInNanos) {
        checkpoint(processRecordsInput.checkpointer());
        nextCheckpointTimeInNanos = System.nanoTime() + checkpointInterval;
    }
}
 
Example #11
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 #12
Source File: ProcessTask.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
/**
 * Scans a list of records to filter out records up to and including the most recent checkpoint value and to get the
 * greatest extended sequence number from the retained records. Also emits metrics about the records.
 *
 * @param scope
 *            metrics scope to emit metrics into
 * @param records
 *            list of records to scan and change in-place as needed
 * @param lastCheckpointValue
 *            the most recent checkpoint value
 * @param lastLargestPermittedCheckpointValue
 *            previous largest permitted checkpoint value
 * @return the largest extended sequence number among the retained records
 */
private ExtendedSequenceNumber filterAndGetMaxExtendedSequenceNumber(final MetricsScope scope,
                                                                     final List<KinesisClientRecord> records,
                                                                     final ExtendedSequenceNumber lastCheckpointValue,
                                                                     final ExtendedSequenceNumber lastLargestPermittedCheckpointValue) {
    ExtendedSequenceNumber largestExtendedSequenceNumber = lastLargestPermittedCheckpointValue;
    ListIterator<KinesisClientRecord> recordIterator = records.listIterator();
    while (recordIterator.hasNext()) {
        KinesisClientRecord record = recordIterator.next();
        ExtendedSequenceNumber extendedSequenceNumber = new ExtendedSequenceNumber(record.sequenceNumber(),
                record.subSequenceNumber());

        if (extendedSequenceNumber.compareTo(lastCheckpointValue) <= 0) {
            recordIterator.remove();
            log.debug("removing record with ESN {} because the ESN is <= checkpoint ({})", extendedSequenceNumber,
                    lastCheckpointValue);
            continue;
        }

        if (largestExtendedSequenceNumber == null
                || largestExtendedSequenceNumber.compareTo(extendedSequenceNumber) < 0) {
            largestExtendedSequenceNumber = extendedSequenceNumber;
        }

        scope.addData(DATA_BYTES_PROCESSED_METRIC, record.data().limit(), StandardUnit.BYTES,
                MetricsLevel.SUMMARY);
    }
    return largestExtendedSequenceNumber;
}
 
Example #13
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 #14
Source File: FanOutRecordsPublisherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean matchesSafely(KinesisClientRecord item, Description mismatchDescription) {
    boolean matches = matchAndDescribe(partitionKeyMatcher, item.partitionKey(), "partitionKey",
            mismatchDescription);
    matches &= matchAndDescribe(sequenceNumberMatcher, item.sequenceNumber(), "sequenceNumber",
            mismatchDescription);
    matches &= matchAndDescribe(approximateArrivalMatcher, item.approximateArrivalTimestamp(),
            "approximateArrivalTimestamp", mismatchDescription);
    matches &= matchAndDescribe(dataMatcher, item.data(), "data", mismatchDescription);
    return matches;
}
 
Example #15
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 #16
Source File: TestLambdaDeaggregation.java    From kinesis-aggregation with Apache License 2.0 5 votes vote down vote up
@Test
public void testList() throws Exception {
	// invoke deaggregation on the static records, returning a List of UserRecord
	List<KinesisClientRecord> records = deaggregator.deaggregate(recordList);

	assertEquals("Processed Record Count Correct", records.size(), recordList.size());
	verifyOneToOneMapping(records);
}
 
Example #17
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 #18
Source File: TestLambdaDeaggregation.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
		KinesisEventRecord 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.getKinesis().getData()) == 0);
	});
}
 
Example #19
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 #20
Source File: ProcessTaskTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeaggregatesRecord() {
    final String sqn = new BigInteger(128, new Random()).toString();
    final String pk = UUID.randomUUID().toString();
    final Instant ts = Instant.now().minus(4, ChronoUnit.HOURS);
    KinesisClientRecord record = KinesisClientRecord.builder().partitionKey("-").data(generateAggregatedRecord(pk))
            .sequenceNumber(sqn).approximateArrivalTimestamp(ts).build();

    processTask = makeProcessTask(processRecordsInput);
    ShardRecordProcessorOutcome outcome = testWithRecord(record);

    List<KinesisClientRecord> actualRecords = outcome.getProcessRecordsCall().records();

    assertEquals(3, actualRecords.size());
    for (KinesisClientRecord pr : actualRecords) {
        assertThat(pr, instanceOf(KinesisClientRecord.class));
        assertEquals(pk, pr.partitionKey());
        assertEquals(ts, pr.approximateArrivalTimestamp());

        byte[] actualData = new byte[pr.data().limit()];
        pr.data().get(actualData);
        assertThat(actualData, equalTo(TEST_DATA));
    }

    assertEquals(sqn, outcome.getCheckpointCall().sequenceNumber());
    assertEquals(actualRecords.size() - 1, outcome.getCheckpointCall().subSequenceNumber());
}
 
Example #21
Source File: ProcessTaskTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterBasedOnLastCheckpointValue() {
    // Explanation of setup:
    // * Assume in previous processRecord call, user got 3 sub-records that all belonged to one
    // Kinesis record. So sequence number was X, and sub-sequence numbers were 0, 1, 2.
    // * 2nd sub-record was checkpointed (extended sequnce number X.1).
    // * Worker crashed and restarted. So now DDB has checkpoint value of X.1.
    // Test:
    // * Now in the subsequent processRecords call, KCL should filter out X.0 and X.1.
    BigInteger previousCheckpointSqn = new BigInteger(128, new Random());
    long previousCheckpointSsqn = 1;

    // Values for this processRecords call.
    String startingSqn = previousCheckpointSqn.toString();
    String pk = UUID.randomUUID().toString();
    KinesisClientRecord record = KinesisClientRecord.builder().partitionKey("-").data(generateAggregatedRecord(pk))
            .sequenceNumber(startingSqn).build();

    processTask = makeProcessTask(processRecordsInput);
    ShardRecordProcessorOutcome outcome = testWithRecords(Collections.singletonList(record),
            new ExtendedSequenceNumber(previousCheckpointSqn.toString(), previousCheckpointSsqn),
            new ExtendedSequenceNumber(previousCheckpointSqn.toString(), previousCheckpointSsqn));

    List<KinesisClientRecord> actualRecords = outcome.getProcessRecordsCall().records();

    // First two records should be dropped - and only 1 remaining records should be there.
    assertThat(actualRecords.size(), equalTo(1));

    // Verify user record's extended sequence number and other fields.
    KinesisClientRecord actualRecord = actualRecords.get(0);
    assertThat(actualRecord.partitionKey(), equalTo(pk));
    assertThat(actualRecord.sequenceNumber(), equalTo(startingSqn));
    assertThat(actualRecord.subSequenceNumber(), equalTo(previousCheckpointSsqn + 1));
    assertThat(actualRecord.approximateArrivalTimestamp(), nullValue());

    // Expected largest permitted sequence number will be last sub-record sequence number.
    final ExtendedSequenceNumber expectedLargestPermittedEsqn = new ExtendedSequenceNumber(
            previousCheckpointSqn.toString(), 2L);
    assertEquals(expectedLargestPermittedEsqn, outcome.getCheckpointCall());
}
 
Example #22
Source File: ProcessTaskTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
private ShardRecordProcessorOutcome testWithRecords(List<KinesisClientRecord> records, ExtendedSequenceNumber lastCheckpointValue,
                                                    ExtendedSequenceNumber largestPermittedCheckpointValue, AggregatorUtil aggregatorUtil) {
    when(processRecordsInput.records()).thenReturn(records);
    return testWithRecords(
            makeProcessTask(processRecordsInput, aggregatorUtil, skipShardSyncAtWorkerInitializationIfLeasesExist),
            lastCheckpointValue, largestPermittedCheckpointValue);
}
 
Example #23
Source File: ProcessTaskTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
private static List<KinesisClientRecord> generateConsecutiveRecords(int numberOfRecords, String partitionKey, ByteBuffer data,
        Date arrivalTimestamp, BigInteger startSequenceNumber) {
    List<KinesisClientRecord> records = new ArrayList<>();
    for (int i = 0; i < numberOfRecords; ++i) {
        String seqNum = startSequenceNumber.add(BigInteger.valueOf(i)).toString();
        KinesisClientRecord record = KinesisClientRecord.builder().partitionKey(partitionKey).data(data)
                .sequenceNumber(seqNum).approximateArrivalTimestamp(arrivalTimestamp.toInstant()).build();
        records.add(record);
    }
    return records;
}
 
Example #24
Source File: ShardConsumerSubscriberTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
private void addUniqueItem(int id) {
    RecordsRetrieved r = mock(RecordsRetrieved.class, "Record-" + id);
    ProcessRecordsInput input = ProcessRecordsInput.builder().cacheEntryTime(Instant.now())
            .records(Collections.singletonList(KinesisClientRecord.builder().partitionKey("Record-" + id).build()))
            .build();
    when(r.processRecordsInput()).thenReturn(input);
    recordsPublisher.add(new ResponseItem(r));
}
 
Example #25
Source File: ShardConsumerSubscriberTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
private ProcessRecordsInput addTerminalMarker(int id) {
    RecordsRetrieved terminalResponse = mock(RecordsRetrieved.class, TERMINAL_MARKER + "-" + id);
    ProcessRecordsInput terminalInput = ProcessRecordsInput.builder()
            .records(Collections
                    .singletonList(KinesisClientRecord.builder().partitionKey(TERMINAL_MARKER + "-" + id).build()))
            .cacheEntryTime(Instant.now()).build();
    when(terminalResponse.processRecordsInput()).thenReturn(terminalInput);
    recordsPublisher.add(new ResponseItem(terminalResponse));

    return terminalInput;
}
 
Example #26
Source File: TestStreamlet.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Override
public void processRecords(ProcessRecordsInput input) {
    List<KinesisClientRecord> dataRecords = input.records();
    RecordProcessorCheckpointer checkpointer = input.checkpointer();
    if ((dataRecords != null) && (!dataRecords.isEmpty())) {
        for (KinesisClientRecord record : dataRecords) {
            log.debug("Processing record: {}", record);
            String seqNum = record.sequenceNumber();
            if (!processedSeqNums.contains(seqNum)) {
                records.add(record);
                processedSeqNums.add(seqNum);
            }
        }
    }
    if (dataRecords.isEmpty()) {
        numProcessRecordsCallsWithEmptyRecordList++;
    }
    try {
        checkpointer.checkpoint();
    } catch (ThrottlingException | ShutdownException
            | KinesisClientLibDependencyException | InvalidStateException e) {
        // Continue processing records and checkpoint next time if we get a transient error.
        // Don't checkpoint if the processor has been shutdown.
        log.debug("Caught exception while checkpointing: ", e);
    }

    if (sem != null) {
        sem.release(dataRecords.size());
    }
}
 
Example #27
Source File: JsonFriendlyRecord.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
public static JsonFriendlyRecord fromKinesisClientRecord(@NonNull final KinesisClientRecord record) {
    byte[] data;
    if (record.data() == null) {
        data = null;
    } else if (record.data().hasArray()) {
        data = record.data().array();
    } else {
        data = new byte[record.data().limit()];
        record.data().get(data);
    }
    Long approximateArrival = record.approximateArrivalTimestamp() == null ? null
            : record.approximateArrivalTimestamp().toEpochMilli();
    return new JsonFriendlyRecord(data, record.partitionKey(), record.sequenceNumber(),
            approximateArrival, record.subSequenceNumber());
}
 
Example #28
Source File: ProcessRecordsMessage.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience constructor.
 * 
 * @param processRecordsInput
 *            the process records input to be sent to the child
 */
public ProcessRecordsMessage(ProcessRecordsInput processRecordsInput) {
    this.millisBehindLatest = processRecordsInput.millisBehindLatest();
    List<JsonFriendlyRecord> recordMessages = new ArrayList<>();
    for (KinesisClientRecord record : processRecordsInput.records()) {
        recordMessages.add(JsonFriendlyRecord.fromKinesisClientRecord(record));
    }
    this.setRecords(recordMessages);
}
 
Example #29
Source File: JsonFriendlyRecordTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
private RecordMatcher(KinesisClientRecord expected) {
    this.matchers = Arrays.asList(
            new FieldMatcher<>("approximateArrivalTimestamp",
                    equalTo(expected.approximateArrivalTimestamp().toEpochMilli()),
                    JsonFriendlyRecord::getApproximateArrivalTimestamp),
            new FieldMatcher<>("partitionKey", expected::partitionKey, JsonFriendlyRecord::getPartitionKey),
            new FieldMatcher<>("sequenceNumber", expected::sequenceNumber,
                    JsonFriendlyRecord::getSequenceNumber),
            new FieldMatcher<>("subSequenceNumber", expected::subSequenceNumber,
                    JsonFriendlyRecord::getSubSequenceNumber),
            new FieldMatcher<>("data", dataEquivalentTo(expected.data()), JsonFriendlyRecord::getData));

    this.expected = expected;
}
 
Example #30
Source File: MessageWriterTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void writeProcessRecordsMessageTest() throws IOException, InterruptedException, ExecutionException {
    List<KinesisClientRecord> records = Arrays.asList(
            KinesisClientRecord.builder().data(ByteBuffer.wrap("kitten".getBytes())).partitionKey("some cats")
                    .sequenceNumber("357234807854789057805").build(),
            KinesisClientRecord.builder().build()
    );
    Future<Boolean> future = this.messageWriter.writeProcessRecordsMessage(ProcessRecordsInput.builder().records(records).build());
    future.get();

    verify(this.stream, Mockito.atLeastOnce()).write(Mockito.any(byte[].class), Mockito.anyInt(),
            Mockito.anyInt());
    verify(this.stream, Mockito.atLeastOnce()).flush();
}