List of usage examples for com.amazonaws.services.kinesis.model DescribeStreamRequest withExclusiveStartShardId
public DescribeStreamRequest withExclusiveStartShardId(String exclusiveStartShardId)
The shard ID of the shard to start with.
From source file:org.springframework.cloud.stream.binder.kinesis.provisioning.KinesisStreamProvisioner.java
License:Apache License
private List<Shard> createOrUpdate(String stream, int shards) { List<Shard> shardList = new ArrayList<>(); int describeStreamRetries = 0; String exclusiveStartShardId = null; DescribeStreamRequest describeStreamRequest = new DescribeStreamRequest().withStreamName(stream); while (true) { DescribeStreamResult describeStreamResult = null; try {//from ww w . ja v a 2 s . c om describeStreamRequest.withExclusiveStartShardId(exclusiveStartShardId); describeStreamResult = this.amazonKinesis.describeStream(describeStreamRequest); StreamDescription streamDescription = describeStreamResult.getStreamDescription(); if (StreamStatus.ACTIVE.toString().equals(streamDescription.getStreamStatus())) { shardList.addAll(streamDescription.getShards()); if (streamDescription.getHasMoreShards()) { exclusiveStartShardId = shardList.get(shardList.size() - 1).getShardId(); } else { break; } } } catch (ResourceNotFoundException ex) { if (!this.configurationProperties.isAutoCreateStream()) { throw new ProvisioningException( "The stream [" + stream + "] was not found and auto creation is disabled.", ex); } if (logger.isInfoEnabled()) { logger.info("Stream '" + stream + "' not found. Create one..."); } this.amazonKinesis.createStream(stream, Math.max(this.configurationProperties.getMinShardCount(), shards)); continue; } catch (LimitExceededException ex) { logger.info( "Got LimitExceededException when describing stream [" + stream + "]. " + "Backing off for [" + this.configurationProperties.getDescribeStreamBackoff() + "] millis."); } if (describeStreamResult == null || !StreamStatus.ACTIVE.toString() .equals(describeStreamResult.getStreamDescription().getStreamStatus())) { if (describeStreamRetries++ > this.configurationProperties.getDescribeStreamRetries()) { ResourceNotFoundException resourceNotFoundException = new ResourceNotFoundException( "The stream [" + stream + "] isn't ACTIVE or doesn't exist."); resourceNotFoundException.setServiceName("Kinesis"); throw new ProvisioningException( "Kinesis org.springframework.cloud.stream.binder.kinesis.provisioning error", resourceNotFoundException); } try { Thread.sleep(this.configurationProperties.getDescribeStreamBackoff()); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new ProvisioningException( "The [describeStream] thread for the stream [" + stream + "] has been interrupted.", ex); } } } int effectiveShardCount = Math.max(this.configurationProperties.getMinShardCount(), shards); if ((shardList.size() < effectiveShardCount) && this.configurationProperties.isAutoAddShards()) { return updateShardCount(stream, shardList.size(), effectiveShardCount); } return shardList; }
From source file:org.springframework.cloud.stream.binder.kinesis.provisioning.KinesisStreamProvisioner.java
License:Apache License
private List<Shard> updateShardCount(String streamName, int shardCount, int targetCount) { if (logger.isInfoEnabled()) { logger.info("Stream [" + streamName + "] has [" + shardCount + "] shards compared to a target configuration of [" + targetCount + "], creating shards..."); }/*from ww w .j ava 2 s.c om*/ UpdateShardCountRequest updateShardCountRequest = new UpdateShardCountRequest().withStreamName(streamName) .withTargetShardCount(targetCount).withScalingType(ScalingType.UNIFORM_SCALING); this.amazonKinesis.updateShardCount(updateShardCountRequest); // Wait for stream to become active again after resharding List<Shard> shardList = new ArrayList<>(); int describeStreamRetries = 0; String exclusiveStartShardId = null; DescribeStreamRequest describeStreamRequest = new DescribeStreamRequest().withStreamName(streamName); while (true) { DescribeStreamResult describeStreamResult = null; try { describeStreamRequest.withExclusiveStartShardId(exclusiveStartShardId); describeStreamResult = this.amazonKinesis.describeStream(describeStreamRequest); StreamDescription streamDescription = describeStreamResult.getStreamDescription(); if (StreamStatus.ACTIVE.toString().equals(streamDescription.getStreamStatus())) { shardList.addAll(streamDescription.getShards()); if (streamDescription.getHasMoreShards()) { exclusiveStartShardId = shardList.get(shardList.size() - 1).getShardId(); } else { break; } } } catch (LimitExceededException ex) { logger.info("Got LimitExceededException when describing stream [" + streamName + "]. " + "Backing off for [" + this.configurationProperties.getDescribeStreamBackoff() + "] millis."); } if (describeStreamResult == null || !StreamStatus.ACTIVE.toString() .equals(describeStreamResult.getStreamDescription().getStreamStatus())) { if (describeStreamRetries++ > this.configurationProperties.getDescribeStreamRetries()) { ResourceNotFoundException resourceNotFoundException = new ResourceNotFoundException( "The stream [" + streamName + "] isn't ACTIVE or doesn't exist."); resourceNotFoundException.setServiceName("Kinesis"); throw new ProvisioningException( "Kinesis org.springframework.cloud.stream.binder.kinesis.provisioning error", resourceNotFoundException); } try { Thread.sleep(this.configurationProperties.getDescribeStreamBackoff()); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new ProvisioningException( "The [describeStream] thread for the stream [" + streamName + "] has been interrupted.", ex); } } } return shardList; }