use of com.amazonaws.services.autoscaling.model.DetachInstancesResult in project cloudbreak by hortonworks.
the class AwsDownscaleServiceTest method downscaleOrderTest.
@Test
void downscaleOrderTest() {
// We need to invoke detach, terminate and update ASG in this order othervise a strange sporadic concurrency issue can occure on AWS side
// The proper order is:
// amazonASClient.detachInstances(...);
// amazonEC2Client.terminateInstances(...);
// amazonASClient.updateAutoScalingGroup(...);
CloudStack stack = mock(CloudStack.class);
List<CloudResource> resources = List.of(new CloudResource.Builder().name("i-1").type(ResourceType.AWS_INSTANCE).build());
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
List<CloudInstance> cloudInstances = new ArrayList<>();
InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
when(workerTemplate.getGroupName()).thenReturn("worker");
CloudInstance workerInstance1 = new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1");
cloudInstances.add(workerInstance1);
CloudContext context = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
AuthenticatedContext authenticatedContext = new AuthenticatedContext(context, new CloudCredential());
AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
autoScalingGroup.setInstances(List.of(new Instance().withInstanceId("i-worker1")));
describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
when(amazonAutoScalingClient.describeAutoScalingGroups(any())).thenReturn(describeAutoScalingGroupsResult);
when(awsClient.createAutoScalingClient(any(), anyString())).thenReturn(amazonAutoScalingClient);
AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
when(awsClient.createEc2Client(any(), anyString())).thenReturn(amazonEC2Client);
AmazonEC2Waiters amazonEC2Waiters = mock(AmazonEC2Waiters.class);
when(amazonEC2Client.waiters()).thenReturn(amazonEC2Waiters);
Waiter waiter = mock(Waiter.class);
when(amazonEC2Waiters.instanceTerminated()).thenReturn(waiter);
when(amazonAutoScalingClient.detachInstances(any())).thenReturn(new DetachInstancesResult());
when(amazonEC2Client.terminateInstances(any())).thenReturn(new TerminateInstancesResult());
when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
mockDescribeInstances(amazonEC2Client);
// create inOrder object passing any mocks that need to be verified in order
InOrder inOrder = Mockito.inOrder(amazonAutoScalingClient, amazonEC2Client);
underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
// Following will make sure that detach, ivoked before terminate and terminate invoked before update ASG!
inOrder.verify(amazonAutoScalingClient).detachInstances(any());
inOrder.verify(amazonEC2Client).terminateInstances(any());
inOrder.verify(amazonAutoScalingClient).updateAutoScalingGroup(any());
}
use of com.amazonaws.services.autoscaling.model.DetachInstancesResult in project cloudbreak by hortonworks.
the class AwsDownscaleServiceTest method downscaleWithLoadBalancers.
@Test
void downscaleWithLoadBalancers() {
CloudStack stack = mock(CloudStack.class);
List<CloudResource> resources = Collections.emptyList();
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
when(workerTemplate.getGroupName()).thenReturn("worker");
List<CloudInstance> cloudInstances = List.of(new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1"));
CloudLoadBalancer privateLoadBalancer = new CloudLoadBalancer(LoadBalancerType.PRIVATE);
CloudLoadBalancer publicLoadBalancer = new CloudLoadBalancer(LoadBalancerType.PUBLIC);
CloudContext context = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
AuthenticatedContext authenticatedContext = new AuthenticatedContext(context, new CloudCredential());
AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
when(awsClient.createAutoScalingClient(any(), anyString())).thenReturn(amazonAutoScalingClient);
AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
when(awsClient.createEc2Client(any(), anyString())).thenReturn(amazonEC2Client);
AmazonEC2Waiters amazonEC2Waiters = mock(AmazonEC2Waiters.class);
when(amazonEC2Client.waiters()).thenReturn(amazonEC2Waiters);
Waiter waiter = mock(Waiter.class);
when(amazonEC2Waiters.instanceTerminated()).thenReturn(waiter);
when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
when(stack.getLoadBalancers()).thenReturn(List.of(privateLoadBalancer, publicLoadBalancer));
doNothing().when(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
autoScalingGroup.setInstances(List.of(new Instance().withInstanceId("i-worker1")));
describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
ArgumentCaptor<DescribeAutoScalingGroupsRequest> describeAutoScalingGroupsRequest = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
ArgumentCaptor<DetachInstancesRequest> detachInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(DetachInstancesRequest.class);
when(amazonAutoScalingClient.describeAutoScalingGroups(describeAutoScalingGroupsRequest.capture())).thenReturn(describeAutoScalingGroupsResult);
when(amazonAutoScalingClient.detachInstances(detachInstancesRequestArgumentCaptor.capture())).thenReturn(new DetachInstancesResult());
mockDescribeInstances(amazonEC2Client);
underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
}
use of com.amazonaws.services.autoscaling.model.DetachInstancesResult in project cloudbreak by hortonworks.
the class AwsDownscaleServiceTest method downscaleASG.
@Test
void downscaleASG() {
CloudStack stack = mock(CloudStack.class);
List<CloudResource> resources = List.of(new CloudResource.Builder().name("i-1").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-2").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-3").type(ResourceType.AWS_INSTANCE).build());
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
List<CloudInstance> cloudInstances = new ArrayList<>();
InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
when(workerTemplate.getGroupName()).thenReturn("worker");
CloudInstance workerInstance1 = new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1");
CloudInstance workerInstance2 = new CloudInstance("i-worker2", workerTemplate, instanceAuthentication, "subnet-1", "az1");
CloudInstance workerInstance3 = new CloudInstance("i-worker3", workerTemplate, instanceAuthentication, "subnet-1", "az1");
cloudInstances.add(workerInstance1);
cloudInstances.add(workerInstance2);
cloudInstances.add(workerInstance3);
AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
when(awsClient.createAutoScalingClient(any(), anyString())).thenReturn(amazonAutoScalingClient);
AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
when(awsClient.createEc2Client(any(), anyString())).thenReturn(amazonEC2Client);
CloudContext context = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
AuthenticatedContext authenticatedContext = new AuthenticatedContext(context, new CloudCredential());
AmazonEC2Waiters amazonEC2Waiters = mock(AmazonEC2Waiters.class);
when(amazonEC2Client.waiters()).thenReturn(amazonEC2Waiters);
Waiter waiter = mock(Waiter.class);
when(amazonEC2Waiters.instanceTerminated()).thenReturn(waiter);
when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
autoScalingGroup.setInstances(List.of(new Instance().withInstanceId("i-worker1")));
describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
ArgumentCaptor<DescribeAutoScalingGroupsRequest> describeAutoScalingGroupsRequest = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
ArgumentCaptor<DetachInstancesRequest> detachInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(DetachInstancesRequest.class);
when(amazonAutoScalingClient.describeAutoScalingGroups(describeAutoScalingGroupsRequest.capture())).thenReturn(describeAutoScalingGroupsResult);
when(amazonAutoScalingClient.detachInstances(detachInstancesRequestArgumentCaptor.capture())).thenReturn(new DetachInstancesResult());
mockDescribeInstances(amazonEC2Client);
underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
List<DetachInstancesRequest> allValues = detachInstancesRequestArgumentCaptor.getAllValues();
assertThat(allValues.get(0).getInstanceIds(), contains("i-worker1"));
verify(amazonAutoScalingClient, times(1)).detachInstances(any());
verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
assertEquals(describeAutoScalingGroupsRequest.getValue().getAutoScalingGroupNames(), List.of("autoscalegroup-1"));
}
use of com.amazonaws.services.autoscaling.model.DetachInstancesResult in project cloudbreak by hortonworks.
the class AwsDownscaleService method detachInstances.
private void detachInstances(String asGroupName, List<String> instanceIdsForAutoScalingGroup, List<String> instanceIdsToDownscale, AmazonAutoScalingClient amazonASClient) {
try {
List<String> instanceIdsToDetach = instanceIdsForAutoScalingGroup.stream().filter(instanceIdsToDownscale::contains).collect(Collectors.toList());
if (instanceIdsToDownscale.size() != instanceIdsToDetach.size()) {
LOGGER.info("Some instances were already detached. Requesting to detach [{}] from original list [{}].", instanceIdsToDetach, instanceIdsToDownscale);
}
for (int i = 0; i < instanceIdsToDetach.size(); i += MAX_DETACH_INSTANCE_SIZE) {
List<String> idPartition = instanceIdsToDetach.subList(i, i + Math.min(instanceIdsToDetach.size() - i, MAX_DETACH_INSTANCE_SIZE));
DetachInstancesRequest detachInstancesRequest = new DetachInstancesRequest().withAutoScalingGroupName(asGroupName).withInstanceIds(idPartition).withShouldDecrementDesiredCapacity(true);
LOGGER.info("Detach instances from asGroupName: {}, instanceIdsToDetach: {}, detachInstancesRequest: {}", asGroupName, instanceIdsToDetach, detachInstancesRequest);
DetachInstancesResult result = amazonASClient.detachInstances(detachInstancesRequest);
LOGGER.info("Detach instances from asGroupName: {}, instanceIdsToDetach: {}, result: {}", asGroupName, instanceIdsToDetach, result);
}
} catch (AmazonServiceException e) {
LOGGER.info("Detach instances failed: {}", instanceIdsToDownscale, e);
throw e;
}
}
use of com.amazonaws.services.autoscaling.model.DetachInstancesResult in project titus-control-plane by Netflix.
the class AwsInstanceCloudConnector method doDetach.
private Completable doDetach(String instanceGroup, List<String> instanceIds, boolean shrink) {
DetachInstancesRequest request = new DetachInstancesRequest().withAutoScalingGroupName(instanceGroup).withInstanceIds(instanceIds).withShouldDecrementDesiredCapacity(shrink);
Observable<DetachInstancesResult> observable = toObservable(request, autoScalingClient::detachInstancesAsync);
return observable.doOnCompleted(() -> logger.info("Detached instances: {}", instanceIds)).doOnError(e -> logger.warn("Failed to detach instances: {}, due to {}", instanceIds, e.getMessage())).toCompletable();
}
Aggregations