org.apache.flink.streaming.connectors.kinesis.model.StreamShardHandle Java Examples

The following examples show how to use org.apache.flink.streaming.connectors.kinesis.model.StreamShardHandle. 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: FakeKinesisBehavioursFactory.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public GetShardListResult getShardList(Map<String, String> streamNamesWithLastSeenShardIds) {
	GetShardListResult result = new GetShardListResult();
	for (Map.Entry<String, List<StreamShardHandle>> streamsWithShards : streamsWithListOfShards.entrySet()) {
		String streamName = streamsWithShards.getKey();
		for (StreamShardHandle shard : streamsWithShards.getValue()) {
			if (streamNamesWithLastSeenShardIds.get(streamName) == null) {
				result.addRetrievedShardToStream(streamName, shard);
			} else {
				if (compareShardIds(
					shard.getShard().getShardId(), streamNamesWithLastSeenShardIds.get(streamName)) > 0) {
					result.addRetrievedShardToStream(streamName, shard);
				}
			}
		}
	}
	return result;
}
 
Example #2
Source File: FakeKinesisBehavioursFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public GetShardListResult getShardList(Map<String, String> streamNamesWithLastSeenShardIds) {
	GetShardListResult result = new GetShardListResult();
	for (Map.Entry<String, List<StreamShardHandle>> streamsWithShards : streamsWithListOfShards.entrySet()) {
		String streamName = streamsWithShards.getKey();
		for (StreamShardHandle shard : streamsWithShards.getValue()) {
			if (streamNamesWithLastSeenShardIds.get(streamName) == null) {
				result.addRetrievedShardToStream(streamName, shard);
			} else {
				if (StreamShardHandle.compareShardIds(
					shard.getShard().getShardId(), streamNamesWithLastSeenShardIds.get(streamName)) > 0) {
					result.addRetrievedShardToStream(streamName, shard);
				}
			}
		}
	}
	return result;
}
 
Example #3
Source File: FakeKinesisBehavioursFactory.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public GetShardListResult getShardList(Map<String, String> streamNamesWithLastSeenShardIds) {
	GetShardListResult result = new GetShardListResult();
	for (Map.Entry<String, List<StreamShardHandle>> streamsWithShards : streamsWithListOfShards.entrySet()) {
		String streamName = streamsWithShards.getKey();
		for (StreamShardHandle shard : streamsWithShards.getValue()) {
			if (streamNamesWithLastSeenShardIds.get(streamName) == null) {
				result.addRetrievedShardToStream(streamName, shard);
			} else {
				if (StreamShardHandle.compareShardIds(
					shard.getShard().getShardId(), streamNamesWithLastSeenShardIds.get(streamName)) > 0) {
					result.addRetrievedShardToStream(streamName, shard);
				}
			}
		}
	}
	return result;
}
 
Example #4
Source File: DynamoDBStreamsDataFetcher.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new DynamoDB streams shard consumer.
 *
 * @param subscribedShardStateIndex the state index of the shard this consumer is subscribed to
 * @param handle stream handle
 * @param lastSeqNum last sequence number
 * @param shardMetricsReporter the reporter to report metrics to
 * @return
 */
@Override
protected ShardConsumer<T> createShardConsumer(
	Integer subscribedShardStateIndex,
	StreamShardHandle handle,
	SequenceNumber lastSeqNum,
	ShardMetricsReporter shardMetricsReporter,
	KinesisDeserializationSchema<T> shardDeserializer) {

	return new ShardConsumer(
		this,
		subscribedShardStateIndex,
		handle,
		lastSeqNum,
		DynamoDBStreamsProxy.create(getConsumerConfiguration()),
		shardMetricsReporter,
		shardDeserializer);
}
 
Example #5
Source File: FakeKinesisBehavioursFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
public static KinesisProxyInterface noShardsFoundForRequestedStreamsBehaviour() {

		return new KinesisProxyInterface() {
			@Override
			public GetShardListResult getShardList(Map<String, String> streamNamesWithLastSeenShardIds) {
				return new GetShardListResult(); // not setting any retrieved shards for result
			}

			@Override
			public String getShardIterator(StreamShardHandle shard, String shardIteratorType, Object startingMarker) {
				return null;
			}

			@Override
			public GetRecordsResult getRecords(String shardIterator, int maxRecordsToGet) {
				return null;
			}
		};

	}
 
Example #6
Source File: DynamoDBStreamsProxy.java    From flink with Apache License 2.0 6 votes vote down vote up
private List<StreamShardHandle> getShardsOfStream(
		String streamName,
		@Nullable String lastSeenShardId)
		throws InterruptedException {
	List<StreamShardHandle> shardsOfStream = new ArrayList<>();

	DescribeStreamResult describeStreamResult;
	do {
		describeStreamResult = describeStream(streamName, lastSeenShardId);
		List<Shard> shards = describeStreamResult.getStreamDescription().getShards();
		for (Shard shard : shards) {
			shardsOfStream.add(new StreamShardHandle(streamName, shard));
		}

		if (shards.size() != 0) {
			lastSeenShardId = shards.get(shards.size() - 1).getShardId();
		}
	} while (describeStreamResult.getStreamDescription().isHasMoreShards());

	return shardsOfStream;
}
 
Example #7
Source File: KinesisPubsubClient.java    From flink with Apache License 2.0 6 votes vote down vote up
public List<String> readAllMessages(String streamName) throws Exception {
	KinesisProxyInterface kinesisProxy = KinesisProxy.create(properties);
	Map<String, String> streamNamesWithLastSeenShardIds = new HashMap<>();
	streamNamesWithLastSeenShardIds.put(streamName, null);

	GetShardListResult shardListResult = kinesisProxy.getShardList(streamNamesWithLastSeenShardIds);
	int maxRecordsToFetch = 10;

	List<String> messages = new ArrayList<>();
	// retrieve records from all shards
	for (StreamShardHandle ssh : shardListResult.getRetrievedShardListOfStream(streamName)) {
		String shardIterator = kinesisProxy.getShardIterator(ssh, "TRIM_HORIZON", null);
		GetRecordsResult getRecordsResult = kinesisProxy.getRecords(shardIterator, maxRecordsToFetch);
		List<Record> aggregatedRecords = getRecordsResult.getRecords();
		for (Record record : aggregatedRecords) {
			messages.add(new String(record.getData().array()));
		}
	}
	return messages;
}
 
Example #8
Source File: FakeKinesisBehavioursFactory.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public BlockingQueueKinesis(Map<String, List<BlockingQueue<String>>> streamsToShardCount) {
	for (Map.Entry<String, List<BlockingQueue<String>>> streamToShardQueues : streamsToShardCount.entrySet()) {
		String streamName = streamToShardQueues.getKey();
		int shardCount = streamToShardQueues.getValue().size();

		if (shardCount == 0) {
			// don't do anything
		} else {
			List<StreamShardHandle> shardsOfStream = new ArrayList<>(shardCount);
			for (int i = 0; i < shardCount; i++) {
				StreamShardHandle shardHandle = new StreamShardHandle(
					streamName,
					new Shard().withShardId(KinesisShardIdGenerator.generateFromShardOrder(i))
						.withSequenceNumberRange(new SequenceNumberRange().withStartingSequenceNumber("0"))
						.withHashKeyRange(new HashKeyRange().withStartingHashKey("0").withEndingHashKey("0")));
				shardsOfStream.add(shardHandle);
				shardIteratorToQueueMap.put(getShardIterator(shardHandle), streamToShardQueues.getValue().get(i));
			}
			streamsWithListOfShards.put(streamName, shardsOfStream);
		}
	}
}
 
Example #9
Source File: KinesisDataFetcher.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * A utility function that does the following:
 *
 * <p>1. Find new shards for each stream that we haven't seen before
 * 2. For each new shard, determine whether this consumer subtask should subscribe to them;
 * 	  if yes, it is added to the returned list of shards
 * 3. Update the subscribedStreamsToLastDiscoveredShardIds state so that we won't get shards
 *    that we have already seen before the next time this function is called
 */
public List<StreamShardHandle> discoverNewShardsToSubscribe() throws InterruptedException {

	List<StreamShardHandle> newShardsToSubscribe = new LinkedList<>();

	GetShardListResult shardListResult = kinesis.getShardList(subscribedStreamsToLastDiscoveredShardIds);
	if (shardListResult.hasRetrievedShards()) {
		Set<String> streamsWithNewShards = shardListResult.getStreamsWithRetrievedShards();

		for (String stream : streamsWithNewShards) {
			List<StreamShardHandle> newShardsOfStream = shardListResult.getRetrievedShardListOfStream(stream);
			for (StreamShardHandle newShard : newShardsOfStream) {
				int hashCode = shardAssigner.assign(newShard, totalNumberOfConsumerSubtasks);
				if (isThisSubtaskShouldSubscribeTo(hashCode, totalNumberOfConsumerSubtasks, indexOfThisConsumerSubtask)) {
					newShardsToSubscribe.add(newShard);
				}
			}

			advanceLastDiscoveredShardOfStream(
				stream, shardListResult.getLastSeenShardOfStream(stream).getShard().getShardId());
		}
	}

	return newShardsToSubscribe;
}
 
Example #10
Source File: KinesisProxy.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private List<StreamShardHandle> getShardsOfStream(String streamName, @Nullable String lastSeenShardId) throws InterruptedException {
	List<StreamShardHandle> shardsOfStream = new ArrayList<>();

	// List Shards returns just the first 1000 shard entries. In order to read the entire stream,
	// we need to use the returned nextToken to get additional shards.
	ListShardsResult listShardsResult;
	String startShardToken = null;
	do {
		listShardsResult = listShards(streamName, lastSeenShardId, startShardToken);
		if (listShardsResult == null) {
			// In case we have exceptions while retrieving all shards, ensure that incomplete shard list is not returned.
			// Hence clearing the incomplete shard list before returning it.
			shardsOfStream.clear();
			return shardsOfStream;
		}
		List<Shard> shards = listShardsResult.getShards();
		for (Shard shard : shards) {
			shardsOfStream.add(new StreamShardHandle(streamName, shard));
		}
		startShardToken = listShardsResult.getNextToken();
	} while (startShardToken != null);

	return shardsOfStream;
}
 
Example #11
Source File: KinesisPubsubClient.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> readAllMessages(String streamName) throws Exception {
	KinesisProxyInterface kinesisProxy = KinesisProxy.create(properties);
	Map<String, String> streamNamesWithLastSeenShardIds = new HashMap<>();
	streamNamesWithLastSeenShardIds.put(streamName, null);

	GetShardListResult shardListResult = kinesisProxy.getShardList(streamNamesWithLastSeenShardIds);
	int maxRecordsToFetch = 10;

	List<String> messages = new ArrayList<>();
	// retrieve records from all shards
	for (StreamShardHandle ssh : shardListResult.getRetrievedShardListOfStream(streamName)) {
		String shardIterator = kinesisProxy.getShardIterator(ssh, "TRIM_HORIZON", null);
		GetRecordsResult getRecordsResult = kinesisProxy.getRecords(shardIterator, maxRecordsToFetch);
		List<Record> aggregatedRecords = getRecordsResult.getRecords();
		for (Record record : aggregatedRecords) {
			messages.add(new String(record.getData().array()));
		}
	}
	return messages;
}
 
Example #12
Source File: DynamoDBStreamsProxy.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private List<StreamShardHandle> getShardsOfStream(
		String streamName,
		@Nullable String lastSeenShardId)
		throws InterruptedException {
	List<StreamShardHandle> shardsOfStream = new ArrayList<>();

	DescribeStreamResult describeStreamResult;
	do {
		describeStreamResult = describeStream(streamName, lastSeenShardId);
		List<Shard> shards = describeStreamResult.getStreamDescription().getShards();
		for (Shard shard : shards) {
			shardsOfStream.add(new StreamShardHandle(streamName, shard));
		}

		if (shards.size() != 0) {
			lastSeenShardId = shards.get(shards.size() - 1).getShardId();
		}
	} while (describeStreamResult.getStreamDescription().isHasMoreShards());

	return shardsOfStream;
}
 
Example #13
Source File: FakeKinesisBehavioursFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
public BlockingQueueKinesis(Map<String, List<BlockingQueue<String>>> streamsToShardCount) {
	for (Map.Entry<String, List<BlockingQueue<String>>> streamToShardQueues : streamsToShardCount.entrySet()) {
		String streamName = streamToShardQueues.getKey();
		int shardCount = streamToShardQueues.getValue().size();

		if (shardCount == 0) {
			// don't do anything
		} else {
			List<StreamShardHandle> shardsOfStream = new ArrayList<>(shardCount);
			for (int i = 0; i < shardCount; i++) {
				StreamShardHandle shardHandle = new StreamShardHandle(
					streamName,
					new Shard().withShardId(KinesisShardIdGenerator.generateFromShardOrder(i))
						.withSequenceNumberRange(new SequenceNumberRange().withStartingSequenceNumber("0"))
						.withHashKeyRange(new HashKeyRange().withStartingHashKey("0").withEndingHashKey("0")));
				shardsOfStream.add(shardHandle);
				shardIteratorToQueueMap.put(getShardIterator(shardHandle), streamToShardQueues.getValue().get(i));
			}
			streamsWithListOfShards.put(streamName, shardsOfStream);
		}
	}
}
 
Example #14
Source File: FakeKinesisBehavioursFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public GetShardListResult getShardList(Map<String, String> streamNamesWithLastSeenShardIds) {
	GetShardListResult result = new GetShardListResult();
	for (Map.Entry<String, List<StreamShardHandle>> streamsWithShards : streamsWithListOfShards.entrySet()) {
		String streamName = streamsWithShards.getKey();
		for (StreamShardHandle shard : streamsWithShards.getValue()) {
			if (streamNamesWithLastSeenShardIds.get(streamName) == null) {
				result.addRetrievedShardToStream(streamName, shard);
			} else {
				if (compareShardIds(
					shard.getShard().getShardId(), streamNamesWithLastSeenShardIds.get(streamName)) > 0) {
					result.addRetrievedShardToStream(streamName, shard);
				}
			}
		}
	}
	return result;
}
 
Example #15
Source File: KinesisDataFetcher.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * A utility function that does the following:
 *
 * <p>1. Find new shards for each stream that we haven't seen before
 * 2. For each new shard, determine whether this consumer subtask should subscribe to them;
 * 	  if yes, it is added to the returned list of shards
 * 3. Update the subscribedStreamsToLastDiscoveredShardIds state so that we won't get shards
 *    that we have already seen before the next time this function is called
 */
public List<StreamShardHandle> discoverNewShardsToSubscribe() throws InterruptedException {

	List<StreamShardHandle> newShardsToSubscribe = new LinkedList<>();

	GetShardListResult shardListResult = kinesis.getShardList(subscribedStreamsToLastDiscoveredShardIds);
	if (shardListResult.hasRetrievedShards()) {
		Set<String> streamsWithNewShards = shardListResult.getStreamsWithRetrievedShards();

		for (String stream : streamsWithNewShards) {
			List<StreamShardHandle> newShardsOfStream = shardListResult.getRetrievedShardListOfStream(stream);
			for (StreamShardHandle newShard : newShardsOfStream) {
				int hashCode = shardAssigner.assign(newShard, totalNumberOfConsumerSubtasks);
				if (isThisSubtaskShouldSubscribeTo(hashCode, totalNumberOfConsumerSubtasks, indexOfThisConsumerSubtask)) {
					newShardsToSubscribe.add(newShard);
				}
			}

			advanceLastDiscoveredShardOfStream(
				stream, shardListResult.getLastSeenShardOfStream(stream).getShard().getShardId());
		}
	}

	return newShardsToSubscribe;
}
 
Example #16
Source File: KinesisProxy.java    From flink with Apache License 2.0 6 votes vote down vote up
private List<StreamShardHandle> getShardsOfStream(String streamName, @Nullable String lastSeenShardId) throws InterruptedException {
	List<StreamShardHandle> shardsOfStream = new ArrayList<>();

	// List Shards returns just the first 1000 shard entries. In order to read the entire stream,
	// we need to use the returned nextToken to get additional shards.
	ListShardsResult listShardsResult;
	String startShardToken = null;
	do {
		listShardsResult = listShards(streamName, lastSeenShardId, startShardToken);
		if (listShardsResult == null) {
			// In case we have exceptions while retrieving all shards, ensure that incomplete shard list is not returned.
			// Hence clearing the incomplete shard list before returning it.
			shardsOfStream.clear();
			return shardsOfStream;
		}
		List<Shard> shards = listShardsResult.getShards();
		for (Shard shard : shards) {
			shardsOfStream.add(new StreamShardHandle(streamName, shard));
		}
		startShardToken = listShardsResult.getNextToken();
	} while (startShardToken != null);

	return shardsOfStream;
}
 
Example #17
Source File: FakeKinesisBehavioursFactory.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public static KinesisProxyInterface noShardsFoundForRequestedStreamsBehaviour() {

		return new KinesisProxyInterface() {
			@Override
			public GetShardListResult getShardList(Map<String, String> streamNamesWithLastSeenShardIds) {
				return new GetShardListResult(); // not setting any retrieved shards for result
			}

			@Override
			public String getShardIterator(StreamShardHandle shard, String shardIteratorType, Object startingMarker) {
				return null;
			}

			@Override
			public GetRecordsResult getRecords(String shardIterator, int maxRecordsToGet) {
				return null;
			}
		};

	}
 
Example #18
Source File: FlinkKinesisConsumerMigrationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
public TestFetcher(
		List<String> streams,
		SourceFunction.SourceContext<T> sourceContext,
		RuntimeContext runtimeContext,
		Properties configProps,
		KinesisDeserializationSchema<T> deserializationSchema,
		HashMap<StreamShardMetadata, SequenceNumber> testStateSnapshot,
		List<StreamShardHandle> testInitialDiscoveryShards) {

	super(streams, sourceContext, runtimeContext, configProps, deserializationSchema, DEFAULT_SHARD_ASSIGNER, null, null);

	this.testStateSnapshot = testStateSnapshot;
	this.testInitialDiscoveryShards = testInitialDiscoveryShards;
}
 
Example #19
Source File: FakeKinesisBehavioursFactory.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public String getShardIterator(StreamShardHandle shard, String shardIteratorType, Object startingMarker) {
	if (!expiredOnceAlready) {
		// for the first call, just return the iterator of the first batch of records
		return "0";
	} else {
		// fake the iterator refresh when this is called again after getRecords throws expired iterator
		// exception on the orderOfCallToExpire attempt
		expiredIteratorRefreshed = true;
		return String.valueOf(orderOfCallToExpire - 1);
	}
}
 
Example #20
Source File: GetShardListResult.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public StreamShardHandle getLastSeenShardOfStream(String stream) {
	if (!streamsToRetrievedShardList.containsKey(stream)) {
		return null;
	} else {
		return streamsToRetrievedShardList.get(stream).getLast();
	}
}
 
Example #21
Source File: ShardConsumerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static StreamShardHandle getMockStreamShard(String streamName, int shardId) {
	return new StreamShardHandle(
		streamName,
		new Shard()
			.withShardId(KinesisShardIdGenerator.generateFromShardOrder(shardId))
			.withHashKeyRange(
				new HashKeyRange()
					.withStartingHashKey("0")
					.withEndingHashKey(new BigInteger(StringUtils.repeat("FF", 16), 16).toString())));
}
 
Example #22
Source File: KinesisDataFetcherTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testStreamShardMetadataAndHandleConversion() {
	String streamName = "fakeStream1";
	String shardId = "shard-000001";
	String parentShardId = "shard-000002";
	String adjacentParentShardId = "shard-000003";
	String startingHashKey = "key-000001";
	String endingHashKey = "key-000010";
	String startingSequenceNumber = "seq-0000021";
	String endingSequenceNumber = "seq-00000031";

	StreamShardMetadata kinesisStreamShard = new StreamShardMetadata();
	kinesisStreamShard.setStreamName(streamName);
	kinesisStreamShard.setShardId(shardId);
	kinesisStreamShard.setParentShardId(parentShardId);
	kinesisStreamShard.setAdjacentParentShardId(adjacentParentShardId);
	kinesisStreamShard.setStartingHashKey(startingHashKey);
	kinesisStreamShard.setEndingHashKey(endingHashKey);
	kinesisStreamShard.setStartingSequenceNumber(startingSequenceNumber);
	kinesisStreamShard.setEndingSequenceNumber(endingSequenceNumber);

	Shard shard = new Shard()
		.withShardId(shardId)
		.withParentShardId(parentShardId)
		.withAdjacentParentShardId(adjacentParentShardId)
		.withHashKeyRange(new HashKeyRange()
			.withStartingHashKey(startingHashKey)
			.withEndingHashKey(endingHashKey))
		.withSequenceNumberRange(new SequenceNumberRange()
			.withStartingSequenceNumber(startingSequenceNumber)
			.withEndingSequenceNumber(endingSequenceNumber));
	StreamShardHandle streamShardHandle = new StreamShardHandle(streamName, shard);

	assertEquals(kinesisStreamShard, KinesisDataFetcher.convertToStreamShardMetadata(streamShardHandle));
	assertEquals(streamShardHandle, KinesisDataFetcher.convertToStreamShardHandle(kinesisStreamShard));
}
 
Example #23
Source File: KinesisDataFetcher.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new shard consumer.
 * Override this method to customize shard consumer behavior in subclasses.
 * @param subscribedShardStateIndex the state index of the shard this consumer is subscribed to
 * @param subscribedShard the shard this consumer is subscribed to
 * @param lastSequenceNum the sequence number in the shard to start consuming
 * @param shardMetricsReporter the reporter to report metrics to
 * @return shard consumer
 */
protected ShardConsumer createShardConsumer(
	Integer subscribedShardStateIndex,
	StreamShardHandle subscribedShard,
	SequenceNumber lastSequenceNum,
	ShardMetricsReporter shardMetricsReporter) {
	return new ShardConsumer<>(
		this,
		subscribedShardStateIndex,
		subscribedShard,
		lastSequenceNum,
		this.kinesisProxyFactory.create(configProps),
		shardMetricsReporter);
}
 
Example #24
Source File: FakeKinesisBehavioursFactory.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public String getShardIterator(StreamShardHandle shard, String shardIteratorType, Object startingMarker) {
	if (!expiredOnceAlready) {
		// for the first call, just return the iterator of the first batch of records
		return "0";
	} else {
		// fake the iterator refresh when this is called again after getRecords throws expired iterator
		// exception on the orderOfCallToExpire attempt
		expiredIteratorRefreshed = true;
		return String.valueOf(orderOfCallToExpire - 1);
	}
}
 
Example #25
Source File: FakeKinesisBehavioursFactory.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public String getShardIterator(StreamShardHandle shard, String shardIteratorType,
		Object startingMarker) {
	// this will be called only one time per ShardConsumer;
	// so, simply return the iterator of the first batch of records
	return "0";
}
 
Example #26
Source File: GetShardListResult.java    From flink with Apache License 2.0 5 votes vote down vote up
public void addRetrievedShardsToStream(String stream, List<StreamShardHandle> retrievedShards) {
	if (retrievedShards.size() != 0) {
		if (!streamsToRetrievedShardList.containsKey(stream)) {
			streamsToRetrievedShardList.put(stream, new LinkedList<StreamShardHandle>());
		}
		streamsToRetrievedShardList.get(stream).addAll(retrievedShards);
	}
}
 
Example #27
Source File: GetShardListResult.java    From flink with Apache License 2.0 5 votes vote down vote up
public void addRetrievedShardsToStream(String stream, List<StreamShardHandle> retrievedShards) {
	if (retrievedShards.size() != 0) {
		if (!streamsToRetrievedShardList.containsKey(stream)) {
			streamsToRetrievedShardList.put(stream, new LinkedList<StreamShardHandle>());
		}
		streamsToRetrievedShardList.get(stream).addAll(retrievedShards);
	}
}
 
Example #28
Source File: GetShardListResult.java    From flink with Apache License 2.0 5 votes vote down vote up
public StreamShardHandle getLastSeenShardOfStream(String stream) {
	if (!streamsToRetrievedShardList.containsKey(stream)) {
		return null;
	} else {
		return streamsToRetrievedShardList.get(stream).getLast();
	}
}
 
Example #29
Source File: FlinkKinesisConsumerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private HashMap<StreamShardHandle, SequenceNumber> getFakeRestoredStore(String streamName) {
	HashMap<StreamShardHandle, SequenceNumber> fakeRestoredState = new HashMap<>();

	if (streamName.equals("fakeStream1") || streamName.equals("all")) {
		fakeRestoredState.put(
			new StreamShardHandle("fakeStream1",
				new Shard().withShardId(KinesisShardIdGenerator.generateFromShardOrder(0))),
			new SequenceNumber(UUID.randomUUID().toString()));
		fakeRestoredState.put(
			new StreamShardHandle("fakeStream1",
				new Shard().withShardId(KinesisShardIdGenerator.generateFromShardOrder(1))),
			new SequenceNumber(UUID.randomUUID().toString()));
		fakeRestoredState.put(
			new StreamShardHandle("fakeStream1",
				new Shard().withShardId(KinesisShardIdGenerator.generateFromShardOrder(2))),
			new SequenceNumber(UUID.randomUUID().toString()));
	}

	if (streamName.equals("fakeStream2") || streamName.equals("all")) {
		fakeRestoredState.put(
			new StreamShardHandle("fakeStream2",
				new Shard().withShardId(KinesisShardIdGenerator.generateFromShardOrder(0))),
			new SequenceNumber(UUID.randomUUID().toString()));
		fakeRestoredState.put(
			new StreamShardHandle("fakeStream2",
				new Shard().withShardId(KinesisShardIdGenerator.generateFromShardOrder(1))),
			new SequenceNumber(UUID.randomUUID().toString()));
	}

	return fakeRestoredState;
}
 
Example #30
Source File: KinesisDataFetcherTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testStreamShardMetadataAndHandleConversion() {
	String streamName = "fakeStream1";
	String shardId = "shard-000001";
	String parentShardId = "shard-000002";
	String adjacentParentShardId = "shard-000003";
	String startingHashKey = "key-000001";
	String endingHashKey = "key-000010";
	String startingSequenceNumber = "seq-0000021";
	String endingSequenceNumber = "seq-00000031";

	StreamShardMetadata kinesisStreamShard = new StreamShardMetadata();
	kinesisStreamShard.setStreamName(streamName);
	kinesisStreamShard.setShardId(shardId);
	kinesisStreamShard.setParentShardId(parentShardId);
	kinesisStreamShard.setAdjacentParentShardId(adjacentParentShardId);
	kinesisStreamShard.setStartingHashKey(startingHashKey);
	kinesisStreamShard.setEndingHashKey(endingHashKey);
	kinesisStreamShard.setStartingSequenceNumber(startingSequenceNumber);
	kinesisStreamShard.setEndingSequenceNumber(endingSequenceNumber);

	Shard shard = new Shard()
		.withShardId(shardId)
		.withParentShardId(parentShardId)
		.withAdjacentParentShardId(adjacentParentShardId)
		.withHashKeyRange(new HashKeyRange()
			.withStartingHashKey(startingHashKey)
			.withEndingHashKey(endingHashKey))
		.withSequenceNumberRange(new SequenceNumberRange()
			.withStartingSequenceNumber(startingSequenceNumber)
			.withEndingSequenceNumber(endingSequenceNumber));
	StreamShardHandle streamShardHandle = new StreamShardHandle(streamName, shard);

	assertEquals(kinesisStreamShard, KinesisDataFetcher.convertToStreamShardMetadata(streamShardHandle));
	assertEquals(streamShardHandle, KinesisDataFetcher.convertToStreamShardHandle(kinesisStreamShard));
}