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

The following examples show how to use software.amazon.awssdk.services.kinesis.model.ListShardsRequest. 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: ListShards.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void listKinShards(KinesisClient kinesisClient, String name) {

        try {
        ListShardsRequest request = ListShardsRequest.builder()
                .streamName(name)
                .build();

            ListShardsResponse response = kinesisClient.listShards(request);
            System.out.println(request.streamName() + " has " + response.shards());

        } catch (KinesisException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        System.out.println("Done");
    }
 
Example #2
Source File: StreamScalingUtils.java    From amazon-kinesis-scaling-utils with Apache License 2.0 6 votes vote down vote up
public static Shard getShard(final KinesisClient kinesisClient, final String streamName, final String shardIdStart)
		throws Exception {
	LOG.debug(String.format("Getting Shard %s for Stream %s", shardIdStart, streamName));

	KinesisOperation describe = new KinesisOperation() {
		public Object run(KinesisClient client) {
			// reduce the shardIdStart by 1 as the API uses it as an exclusive start key not
			// a filter
			String shardIdToQuery = new BigDecimal(shardIdStart).subtract(new BigDecimal("1")).toString();
			ListShardsRequest req = ListShardsRequest.builder().streamName(streamName)
					.exclusiveStartShardId(shardIdToQuery).build();
			ListShardsResponse result = client.listShards(req);

			return result.shards().get(0);
		}
	};
	return (Shard) doOperation(kinesisClient, describe, streamName, DESCRIBE_RETRIES, false);
}
 
Example #3
Source File: KinesisShardDetectorTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetShardNewShardForceRefresh() {
    final String shardId = String.format(SHARD_ID, 5);
    final List<Shard> shards = new ArrayList<>(createShardList());
    shards.add(Shard.builder().shardId(shardId).build());

    final CompletableFuture<ListShardsResponse> future = CompletableFuture
            .completedFuture(ListShardsResponse.builder().shards(shards).build());

    shardDetector.cachedShardMap(createShardList());

    when(client.listShards(any(ListShardsRequest.class))).thenReturn(future);

    final List<Shard> responses = IntStream.range(0, MAX_CACHE_MISSES_BEFORE_RELOAD + 1)
            .mapToObj(x -> shardDetector.shard(shardId)).collect(Collectors.toList());

    IntStream.range(0, MAX_CACHE_MISSES_BEFORE_RELOAD).forEach(x -> {
        assertThat(responses.get(x), nullValue());
    });

    assertThat(responses.get(MAX_CACHE_MISSES_BEFORE_RELOAD), equalTo(Shard.builder().shardId(shardId).build()));
    verify(client).listShards(any(ListShardsRequest.class));
}
 
Example #4
Source File: KinesisShardDetectorTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetShardNonExistentShardForceRefresh() {
    final String shardId = String.format(SHARD_ID, 5);
    final CompletableFuture<ListShardsResponse> future = CompletableFuture
            .completedFuture(ListShardsResponse.builder().shards(createShardList()).build());

    shardDetector.cachedShardMap(createShardList());

    when(client.listShards(any(ListShardsRequest.class))).thenReturn(future);

    final List<Shard> responses = IntStream.range(0, MAX_CACHE_MISSES_BEFORE_RELOAD + 1)
            .mapToObj(x -> shardDetector.shard(shardId)).collect(Collectors.toList());

    responses.forEach(response -> assertThat(response, nullValue()));
    assertThat(shardDetector.cacheMisses().get(), equalTo(0));
    verify(client).listShards(any(ListShardsRequest.class));
}
 
Example #5
Source File: StreamScalingUtils.java    From amazon-kinesis-scaling-utils with Apache License 2.0 5 votes vote down vote up
public static List<Shard> listShards(final KinesisClient kinesisClient, final String streamName,
		final String shardIdStart) throws Exception {
	LOG.debug(String.format("Listing Stream %s from Shard %s", streamName, shardIdStart));

	KinesisOperation describe = new KinesisOperation() {
		public Object run(KinesisClient client) {
			ListShardsRequest.Builder builder = ListShardsRequest.builder().streamName(streamName);
			ListShardsRequest req = null;
			boolean hasMoreResults = true;
			List<Shard> shards = new ArrayList<>();

			while (hasMoreResults) {
				if (shardIdStart != null && (req != null && req.nextToken() == null)) {
					builder.exclusiveStartShardId(shardIdStart);
				}
				ListShardsResponse result = client.listShards(builder.build());
				shards.addAll(result.shards());

				if (result.nextToken() == null) {
					hasMoreResults = false;
				} else {
					req = ListShardsRequest.builder().nextToken(result.nextToken()).build();
				}

			}
			return shards;
		}
	};
	return (List<Shard>) doOperation(kinesisClient, describe, streamName, DESCRIBE_RETRIES, false);
}
 
Example #6
Source File: KinesisShardDetectorTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testListShardsNullResponse() {
    final CompletableFuture<ListShardsResponse> future = CompletableFuture.completedFuture(null);

    when(client.listShards(any(ListShardsRequest.class))).thenReturn(future);

    try {
        shardDetector.listShards();
    } finally {
        verify(client, times(MAX_LIST_SHARDS_RETRY_ATTEMPTS))
                .listShards(any(ListShardsRequest.class));
    }
}
 
Example #7
Source File: KinesisShardDetectorTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testListShardsResouceInUse() {
    final CompletableFuture<ListShardsResponse> future = CompletableFuture.supplyAsync(() -> {
        throw ResourceInUseException.builder().build();
    });

    when(client.listShards(any(ListShardsRequest.class))).thenReturn(future);

    final List<Shard> shards = shardDetector.listShards();

    assertThat(shards, nullValue());
    verify(client).listShards(any(ListShardsRequest.class));

}
 
Example #8
Source File: KinesisShardDetectorTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test(expected = LimitExceededException.class)
public void testListShardsThrottled() {
    final CompletableFuture<ListShardsResponse> future = CompletableFuture.supplyAsync(() -> {
        throw LimitExceededException.builder().build();
    });

    when(client.listShards(any(ListShardsRequest.class))).thenReturn(future);

    try {
        shardDetector.listShards();
    } finally {
        verify(client, times(MAX_LIST_SHARDS_RETRY_ATTEMPTS))
                .listShards(any(ListShardsRequest.class));
    }
}
 
Example #9
Source File: KinesisShardDetectorTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test(expected = ResourceNotFoundException.class)
public void testListShardsResourceNotFound() {
    final CompletableFuture<ListShardsResponse> future = CompletableFuture.supplyAsync(() -> {
        throw ResourceNotFoundException.builder().build();
    });

    when(client.listShards(any(ListShardsRequest.class))).thenReturn(future);

    try {
        shardDetector.listShards();
    } finally {
        verify(client).listShards(any(ListShardsRequest.class));
    }
}
 
Example #10
Source File: KinesisShardDetectorTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetShard() {
    final String shardId = String.format(SHARD_ID, 1);

    shardDetector.cachedShardMap(createShardList());

    final Shard shard = shardDetector.shard(shardId);

    assertThat(shard, equalTo(Shard.builder().shardId(shardId).build()));
    verify(client, never()).listShards(any(ListShardsRequest.class));
}
 
Example #11
Source File: KinesisShardDetectorTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetShardEmptyCache() {
    final String shardId = String.format(SHARD_ID, 1);
    final CompletableFuture<ListShardsResponse> future = CompletableFuture
            .completedFuture(ListShardsResponse.builder().shards(createShardList()).build());

    when(client.listShards(any(ListShardsRequest.class))).thenReturn(future);

    final Shard shard = shardDetector.shard(shardId);

    assertThat(shard, equalTo(Shard.builder().shardId(shardId).build()));
    verify(client).listShards(any(ListShardsRequest.class));
}
 
Example #12
Source File: KinesisShardDetectorTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetShardNonExistentShard() {
    final String shardId = String.format(SHARD_ID, 5);

    shardDetector.cachedShardMap(createShardList());

    final Shard shard = shardDetector.shard(shardId);

    assertThat(shard, nullValue());
    assertThat(shardDetector.cacheMisses().get(), equalTo(1));
    verify(client, never()).listShards(any(ListShardsRequest.class));
}
 
Example #13
Source File: KinesisRequestsBuilder.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
public static ListShardsRequest.Builder listShardsRequestBuilder() {
    return appendUserAgent(ListShardsRequest.builder());
}
 
Example #14
Source File: KinesisShardDetectorTest.java    From amazon-kinesis-client with Apache License 2.0 3 votes vote down vote up
@Test
public void testListShardsTimesOut() throws Exception {
    expectedExceptionRule.expect(RuntimeException.class);
    expectedExceptionRule.expectCause(isA(TimeoutException.class));

    when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenThrow(new TimeoutException("Timeout"));

    when(client.listShards(any(ListShardsRequest.class))).thenReturn(mockFuture);

    shardDetector.listShards();

}