Search in sources :

Example 1 with KinesisShardAssigner

use of org.apache.flink.streaming.connectors.kinesis.KinesisShardAssigner in project flink by apache.

the class KinesisDataFetcherTest method testShardToSubtaskMappingWithCustomHashFunction.

// ----------------------------------------------------------------------
// Tests shard distribution with custom hash function
// ----------------------------------------------------------------------
@Test
public void testShardToSubtaskMappingWithCustomHashFunction() throws Exception {
    int totalCountOfSubtasks = 10;
    int shardCount = 3;
    for (int i = 0; i < 2; i++) {
        final int hash = i;
        final KinesisShardAssigner allShardsSingleSubtaskFn = (shard, subtasks) -> hash;
        Map<String, Integer> streamToShardCount = new HashMap<>();
        List<String> fakeStreams = new LinkedList<>();
        fakeStreams.add("fakeStream");
        streamToShardCount.put("fakeStream", shardCount);
        for (int j = 0; j < totalCountOfSubtasks; j++) {
            int subtaskIndex = j;
            // subscribe with default hashing
            final TestableKinesisDataFetcher fetcher = new TestableKinesisDataFetcher(fakeStreams, new TestSourceContext<>(), new Properties(), new KinesisDeserializationSchemaWrapper<>(new SimpleStringSchema()), totalCountOfSubtasks, subtaskIndex, new AtomicReference<>(), new LinkedList<>(), KinesisDataFetcher.createInitialSubscribedStreamsToLastDiscoveredShardsState(fakeStreams), FakeKinesisBehavioursFactory.nonReshardedStreamsBehaviour(streamToShardCount));
            Whitebox.setInternalState(fetcher, "shardAssigner", // override hashing
            allShardsSingleSubtaskFn);
            List<StreamShardHandle> shards = fetcher.discoverNewShardsToSubscribe();
            fetcher.shutdownFetcher();
            String msg = String.format("for hash=%d, subtask=%d", hash, subtaskIndex);
            if (j == i) {
                assertEquals(msg, shardCount, shards.size());
            } else {
                assertEquals(msg, 0, shards.size());
            }
        }
    }
}
Also used : KinesisShardAssigner(org.apache.flink.streaming.connectors.kinesis.KinesisShardAssigner) Shard(com.amazonaws.services.kinesis.model.Shard) Arrays(java.util.Arrays) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) TestableKinesisDataFetcher(org.apache.flink.streaming.connectors.kinesis.testutils.TestableKinesisDataFetcher) StreamShardMetadata(org.apache.flink.streaming.connectors.kinesis.model.StreamShardMetadata) Random(java.util.Random) TestSourceContext(org.apache.flink.streaming.connectors.kinesis.testutils.TestSourceContext) Collections.singletonList(java.util.Collections.singletonList) SimpleStringSchema(org.apache.flink.api.common.serialization.SimpleStringSchema) MutableLong(org.apache.commons.lang3.mutable.MutableLong) Map(java.util.Map) TestLogger(org.apache.flink.util.TestLogger) KinesisProxyInterface(org.apache.flink.streaming.connectors.kinesis.proxy.KinesisProxyInterface) KinesisShardIdGenerator(org.apache.flink.streaming.connectors.kinesis.testutils.KinesisShardIdGenerator) Assert.fail(org.junit.Assert.fail) KinesisShardAssigner(org.apache.flink.streaming.connectors.kinesis.KinesisShardAssigner) StreamShardHandle(org.apache.flink.streaming.connectors.kinesis.model.StreamShardHandle) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) UUID(java.util.UUID) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) Mockito.mock(org.mockito.Mockito.mock) KinesisDeserializationSchema(org.apache.flink.streaming.connectors.kinesis.serialization.KinesisDeserializationSchema) Whitebox(org.powermock.reflect.Whitebox) KinesisDeserializationSchemaWrapper(org.apache.flink.streaming.connectors.kinesis.serialization.KinesisDeserializationSchemaWrapper) HashKeyRange(com.amazonaws.services.kinesis.model.HashKeyRange) Watermark(org.apache.flink.streaming.api.watermark.Watermark) HashMap(java.util.HashMap) RecordPublisher(org.apache.flink.streaming.connectors.kinesis.internals.publisher.RecordPublisher) AtomicReference(java.util.concurrent.atomic.AtomicReference) BoundedOutOfOrdernessTimestampExtractor(org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor) ArrayList(java.util.ArrayList) CheckedThread(org.apache.flink.core.testutils.CheckedThread) SourceFunction(org.apache.flink.streaming.api.functions.source.SourceFunction) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) FlinkKinesisConsumer(org.apache.flink.streaming.connectors.kinesis.FlinkKinesisConsumer) LinkedList(java.util.LinkedList) KinesisStreamShardState(org.apache.flink.streaming.connectors.kinesis.model.KinesisStreamShardState) SequenceNumber(org.apache.flink.streaming.connectors.kinesis.model.SequenceNumber) KinesisProxyV2Interface(org.apache.flink.streaming.connectors.kinesis.proxy.KinesisProxyV2Interface) Time(org.apache.flink.streaming.api.windowing.time.Time) Properties(java.util.Properties) FakeKinesisBehavioursFactory(org.apache.flink.streaming.connectors.kinesis.testutils.FakeKinesisBehavioursFactory) SequenceNumberRange(com.amazonaws.services.kinesis.model.SequenceNumberRange) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AlwaysThrowsDeserializationSchema(org.apache.flink.streaming.connectors.kinesis.testutils.AlwaysThrowsDeserializationSchema) Mockito.when(org.mockito.Mockito.when) TestUtils(org.apache.flink.streaming.connectors.kinesis.testutils.TestUtils) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertNull(org.junit.Assert.assertNull) TestableKinesisDataFetcherForShardConsumerException(org.apache.flink.streaming.connectors.kinesis.testutils.TestableKinesisDataFetcherForShardConsumerException) Assert(org.junit.Assert) Collections(java.util.Collections) SHARD_DISCOVERY_INTERVAL_MILLIS(org.apache.flink.streaming.connectors.kinesis.config.ConsumerConfigConstants.SHARD_DISCOVERY_INTERVAL_MILLIS) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) Properties(java.util.Properties) LinkedList(java.util.LinkedList) StreamShardHandle(org.apache.flink.streaming.connectors.kinesis.model.StreamShardHandle) SimpleStringSchema(org.apache.flink.api.common.serialization.SimpleStringSchema) TestableKinesisDataFetcher(org.apache.flink.streaming.connectors.kinesis.testutils.TestableKinesisDataFetcher) Test(org.junit.Test)

Aggregations

HashKeyRange (com.amazonaws.services.kinesis.model.HashKeyRange)1 SequenceNumberRange (com.amazonaws.services.kinesis.model.SequenceNumberRange)1 Shard (com.amazonaws.services.kinesis.model.Shard)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Random (java.util.Random)1 Set (java.util.Set)1 UUID (java.util.UUID)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Collectors (java.util.stream.Collectors)1