Search in sources :

Example 16 with ListShardsResult

use of com.amazonaws.services.kinesis.model.ListShardsResult in project flink by apache.

the class KinesisProxyTest method testGetShardListWithNewShardsOnSecondRun.

@Test
public void testGetShardListWithNewShardsOnSecondRun() throws Exception {
    // given
    List<String> shardIds = Arrays.asList(KinesisShardIdGenerator.generateFromShardOrder(0), KinesisShardIdGenerator.generateFromShardOrder(1));
    String fakeStreamName = "fake-stream";
    List<Shard> shards = shardIds.stream().map(shardId -> new Shard().withShardId(shardId)).collect(Collectors.toList());
    AmazonKinesis mockClient = mock(AmazonKinesis.class);
    KinesisProxy kinesisProxy = getProxy(mockClient);
    ListShardsResult responseFirst = new ListShardsResult().withShards(shards).withNextToken(null);
    doReturn(responseFirst).when(mockClient).listShards(argThat(initialListShardsRequestMatcher()));
    HashMap<String, String> streamHashMap = createInitialSubscribedStreamsToLastDiscoveredShardsState(Collections.singletonList(fakeStreamName));
    // when
    GetShardListResult shardListResult = kinesisProxy.getShardList(streamHashMap);
    // then
    Assert.assertTrue(shardListResult.hasRetrievedShards());
    Set<String> expectedStreams = new HashSet<>();
    expectedStreams.add(fakeStreamName);
    Assert.assertEquals(shardListResult.getStreamsWithRetrievedShards(), expectedStreams);
    List<StreamShardHandle> actualShardList = shardListResult.getRetrievedShardListOfStream(fakeStreamName);
    Assert.assertThat(actualShardList, hasSize(2));
    List<StreamShardHandle> expectedStreamShard = IntStream.range(0, actualShardList.size()).mapToObj(i -> new StreamShardHandle(fakeStreamName, new Shard().withShardId(KinesisShardIdGenerator.generateFromShardOrder(i)))).collect(Collectors.toList());
    Assert.assertThat(actualShardList, containsInAnyOrder(expectedStreamShard.toArray(new StreamShardHandle[actualShardList.size()])));
    // given new shards
    ListShardsResult responseSecond = new ListShardsResult().withShards(new Shard().withShardId(KinesisShardIdGenerator.generateFromShardOrder(2))).withNextToken(null);
    doReturn(responseSecond).when(mockClient).listShards(argThat(initialListShardsRequestMatcher()));
    // when new shards
    GetShardListResult newShardListResult = kinesisProxy.getShardList(streamHashMap);
    // then new shards
    Assert.assertTrue(newShardListResult.hasRetrievedShards());
    Assert.assertEquals(newShardListResult.getStreamsWithRetrievedShards(), expectedStreams);
    List<StreamShardHandle> newActualShardList = newShardListResult.getRetrievedShardListOfStream(fakeStreamName);
    Assert.assertThat(newActualShardList, hasSize(1));
    List<StreamShardHandle> newExpectedStreamShard = Collections.singletonList(new StreamShardHandle(fakeStreamName, new Shard().withShardId(KinesisShardIdGenerator.generateFromShardOrder(2))));
    Assert.assertThat(newActualShardList, containsInAnyOrder(newExpectedStreamShard.toArray(new StreamShardHandle[newActualShardList.size()])));
}
Also used : Shard(com.amazonaws.services.kinesis.model.Shard) Arrays(java.util.Arrays) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ConsumerConfigConstants(org.apache.flink.streaming.connectors.kinesis.config.ConsumerConfigConstants) IsIterableContainingInAnyOrder.containsInAnyOrder(org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder) ClientConfigurationFactory(com.amazonaws.ClientConfigurationFactory) InetAddress(java.net.InetAddress) MockitoHamcrest.argThat(org.mockito.hamcrest.MockitoHamcrest.argThat) KinesisShardIdGenerator(org.apache.flink.streaming.connectors.kinesis.testutils.KinesisShardIdGenerator) Mockito.doReturn(org.mockito.Mockito.doReturn) ListShardsResult(com.amazonaws.services.kinesis.model.ListShardsResult) GetRecordsResult(com.amazonaws.services.kinesis.model.GetRecordsResult) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonKinesis(com.amazonaws.services.kinesis.AmazonKinesis) StreamShardHandle(org.apache.flink.streaming.connectors.kinesis.model.StreamShardHandle) Set(java.util.Set) Collectors(java.util.stream.Collectors) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) Matchers.any(org.mockito.Matchers.any) AWSConfigConstants(org.apache.flink.streaming.connectors.kinesis.config.AWSConfigConstants) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) Whitebox(org.powermock.reflect.Whitebox) ListShardsRequest(com.amazonaws.services.kinesis.model.ListShardsRequest) ProvisionedThroughputExceededException(com.amazonaws.services.kinesis.model.ProvisionedThroughputExceededException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Description(org.hamcrest.Description) Properties(java.util.Properties) Assert.assertTrue(org.junit.Assert.assertTrue) ErrorType(com.amazonaws.AmazonServiceException.ErrorType) Test(org.junit.Test) UnknownHostException(java.net.UnknownHostException) Mockito(org.mockito.Mockito) SdkClientException(com.amazonaws.SdkClientException) ClientConfiguration(com.amazonaws.ClientConfiguration) AmazonKinesisClient(com.amazonaws.services.kinesis.AmazonKinesisClient) AWSUtil(org.apache.flink.streaming.connectors.kinesis.util.AWSUtil) AmazonKinesisException(com.amazonaws.services.kinesis.model.AmazonKinesisException) Assert(org.junit.Assert) ExpiredIteratorException(com.amazonaws.services.kinesis.model.ExpiredIteratorException) HttpHost(org.apache.http.HttpHost) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ListShardsResult(com.amazonaws.services.kinesis.model.ListShardsResult) StreamShardHandle(org.apache.flink.streaming.connectors.kinesis.model.StreamShardHandle) Shard(com.amazonaws.services.kinesis.model.Shard) AmazonKinesis(com.amazonaws.services.kinesis.AmazonKinesis) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with ListShardsResult

use of com.amazonaws.services.kinesis.model.ListShardsResult in project beam by apache.

the class SimplifiedKinesisClientTest method shouldListAllShardsForExclusiveStartShardId.

@Test
public void shouldListAllShardsForExclusiveStartShardId() throws Exception {
    Shard shard1 = new Shard().withShardId(SHARD_1);
    Shard shard2 = new Shard().withShardId(SHARD_2);
    Shard shard3 = new Shard().withShardId(SHARD_3);
    String exclusiveStartShardId = "exclusiveStartShardId";
    when(kinesis.listShards(new ListShardsRequest().withStreamName(STREAM).withMaxResults(1_000).withShardFilter(new ShardFilter().withType(ShardFilterType.AFTER_SHARD_ID).withShardId(exclusiveStartShardId)))).thenReturn(new ListShardsResult().withShards(shard1, shard2, shard3).withNextToken(null));
    List<Shard> shards = underTest.listShardsFollowingClosedShard(STREAM, exclusiveStartShardId);
    assertThat(shards).containsOnly(shard1, shard2, shard3);
}
Also used : ListShardsResult(com.amazonaws.services.kinesis.model.ListShardsResult) ListShardsRequest(com.amazonaws.services.kinesis.model.ListShardsRequest) ShardFilter(com.amazonaws.services.kinesis.model.ShardFilter) Shard(com.amazonaws.services.kinesis.model.Shard) Test(org.junit.Test)

Example 18 with ListShardsResult

use of com.amazonaws.services.kinesis.model.ListShardsResult in project beam by apache.

the class SimplifiedKinesisClientTest method shouldListAllShardsForTrimHorizonWithPagedResults.

@Test
public void shouldListAllShardsForTrimHorizonWithPagedResults() throws Exception {
    Shard shard1 = new Shard().withShardId(SHARD_1);
    Shard shard2 = new Shard().withShardId(SHARD_2);
    Shard shard3 = new Shard().withShardId(SHARD_3);
    ShardFilter shardFilter = new ShardFilter().withType(ShardFilterType.AT_TRIM_HORIZON);
    String nextListShardsToken = "testNextToken";
    when(kinesis.listShards(new ListShardsRequest().withStreamName(STREAM).withShardFilter(shardFilter).withMaxResults(1_000))).thenReturn(new ListShardsResult().withShards(shard1, shard2).withNextToken(nextListShardsToken));
    when(kinesis.listShards(new ListShardsRequest().withMaxResults(1_000).withShardFilter(shardFilter).withNextToken(nextListShardsToken))).thenReturn(new ListShardsResult().withShards(shard3).withNextToken(null));
    List<Shard> shards = underTest.listShardsAtPoint(STREAM, new StartingPoint(InitialPositionInStream.TRIM_HORIZON));
    assertThat(shards).containsOnly(shard1, shard2, shard3);
}
Also used : ListShardsResult(com.amazonaws.services.kinesis.model.ListShardsResult) ListShardsRequest(com.amazonaws.services.kinesis.model.ListShardsRequest) ShardFilter(com.amazonaws.services.kinesis.model.ShardFilter) Shard(com.amazonaws.services.kinesis.model.Shard) Test(org.junit.Test)

Example 19 with ListShardsResult

use of com.amazonaws.services.kinesis.model.ListShardsResult in project hazelcast by hazelcast.

the class RangeMonitor method run.

public void run() {
    long currentTimeMs = System.currentTimeMillis();
    if (listShardsResult == null) {
        initShardListing(currentTimeMs);
    } else {
        if (listShardsResult.isDone()) {
            ListShardsResult result;
            try {
                result = KinesisUtil.readResult(listShardsResult);
            } catch (SdkClientException e) {
                dealWithListShardsFailure(e);
                return;
            } catch (Throwable t) {
                throw rethrow(t);
            } finally {
                listShardsResult = null;
            }
            listShardRetryTracker.reset();
            checkForNewShards(currentTimeMs, result);
            nextToken = result.getNextToken();
            if (nextToken == null) {
                checkForExpiredShards(currentTimeMs);
            }
        }
    }
}
Also used : ListShardsResult(com.amazonaws.services.kinesis.model.ListShardsResult) SdkClientException(com.amazonaws.SdkClientException)

Example 20 with ListShardsResult

use of com.amazonaws.services.kinesis.model.ListShardsResult in project hazelcast by hazelcast.

the class RangeMonitor method checkForNewShards.

private void checkForNewShards(long currentTimeMs, ListShardsResult result) {
    Set<Shard> shards = result.getShards().stream().filter(shard -> KinesisUtil.shardBelongsToRange(shard, memberHashRange)).collect(Collectors.toSet());
    Map<Shard, Integer> newShards = shardTracker.markDetections(shards, currentTimeMs);
    if (!newShards.isEmpty()) {
        logger.info("New shards detected: " + newShards.keySet().stream().map(Shard::getShardId).collect(joining(", ")));
        for (Map.Entry<Shard, Integer> e : newShards.entrySet()) {
            Shard shard = e.getKey();
            int owner = e.getValue();
            shardQueues[owner].addAdded(shard);
        }
    }
}
Also used : ListShardsResult(com.amazonaws.services.kinesis.model.ListShardsResult) Shard(com.amazonaws.services.kinesis.model.Shard) ListShardsRequest(com.amazonaws.services.kinesis.model.ListShardsRequest) RetryTracker(com.hazelcast.jet.kinesis.impl.RetryTracker) Set(java.util.Set) AbstractShardWorker(com.hazelcast.jet.kinesis.impl.AbstractShardWorker) KinesisUtil(com.hazelcast.jet.kinesis.impl.KinesisUtil) Collectors(java.util.stream.Collectors) AmazonKinesisAsync(com.amazonaws.services.kinesis.AmazonKinesisAsync) ShardFilterType(com.amazonaws.services.kinesis.model.ShardFilterType) Collectors.joining(java.util.stream.Collectors.joining) SdkClientException(com.amazonaws.SdkClientException) Future(java.util.concurrent.Future) ILogger(com.hazelcast.logging.ILogger) ExceptionUtil.rethrow(com.hazelcast.jet.impl.util.ExceptionUtil.rethrow) RandomizedRateTracker(com.hazelcast.jet.kinesis.impl.RandomizedRateTracker) Map(java.util.Map) BigInteger(java.math.BigInteger) RetryStrategy(com.hazelcast.jet.retry.RetryStrategy) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ShardFilter(com.amazonaws.services.kinesis.model.ShardFilter) BigInteger(java.math.BigInteger) Shard(com.amazonaws.services.kinesis.model.Shard) Map(java.util.Map)

Aggregations

ListShardsResult (com.amazonaws.services.kinesis.model.ListShardsResult)21 Shard (com.amazonaws.services.kinesis.model.Shard)19 ListShardsRequest (com.amazonaws.services.kinesis.model.ListShardsRequest)18 Test (org.junit.Test)12 ShardFilter (com.amazonaws.services.kinesis.model.ShardFilter)9 SdkClientException (com.amazonaws.SdkClientException)6 DescribeStreamSummaryRequest (com.amazonaws.services.kinesis.model.DescribeStreamSummaryRequest)5 DescribeStreamSummaryResult (com.amazonaws.services.kinesis.model.DescribeStreamSummaryResult)5 Instant (org.joda.time.Instant)5 AmazonKinesis (com.amazonaws.services.kinesis.AmazonKinesis)4 StreamDescriptionSummary (com.amazonaws.services.kinesis.model.StreamDescriptionSummary)4 Datapoint (com.amazonaws.services.cloudwatch.model.Datapoint)3 AmazonKinesisClient (com.amazonaws.services.kinesis.AmazonKinesisClient)3 AmazonKinesisException (com.amazonaws.services.kinesis.model.AmazonKinesisException)3 LimitExceededException (com.amazonaws.services.kinesis.model.LimitExceededException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2