use of com.sequenceiq.cloudbreak.cloud.exception.RolledbackResourcesException in project cloudbreak by hortonworks.
the class AzureUpscaleService method upscale.
public List<CloudResourceStatus> upscale(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources, AzureStackView azureStackView, AzureClient client, AdjustmentTypeWithThreshold adjustmentTypeWithThreshold) throws QuotaExceededException {
CloudContext cloudContext = ac.getCloudContext();
String stackName = azureUtils.getStackName(cloudContext);
String resourceGroupName = azureResourceGroupMetadataProvider.getResourceGroupName(cloudContext, stack);
List<CloudResource> newInstances = new ArrayList<>();
List<CloudResource> templateResources = new ArrayList<>();
List<CloudResource> osDiskResources = new ArrayList<>();
DateTime preDeploymentTime = DateTime.now();
filterExistingInstances(azureStackView);
try {
List<Group> scaledGroups = cloudResourceHelper.getScaledGroups(stack);
CloudResource armTemplate = getArmTemplate(resources, stackName);
Deployment templateDeployment = azureTemplateDeploymentService.getTemplateDeployment(client, stack, ac, azureStackView, AzureInstanceTemplateOperation.UPSCALE);
LOGGER.info("Created template deployment for upscale: {}", templateDeployment.exportTemplate().template());
templateResources.addAll(azureCloudResourceService.getDeploymentCloudResources(templateDeployment));
newInstances.addAll(azureCloudResourceService.getInstanceCloudResources(stackName, templateResources, scaledGroups, resourceGroupName));
if (!newInstances.isEmpty()) {
osDiskResources.addAll(azureCloudResourceService.getAttachedOsDiskResources(newInstances, resourceGroupName, client));
} else {
LOGGER.warn("Skipping OS disk collection as there was no VM instance found amongst cloud resources for {}!", stackName);
}
azureCloudResourceService.saveCloudResources(resourceNotifier, cloudContext, ListUtils.union(templateResources, osDiskResources));
List<CloudResource> reattachableVolumeSets = getReattachableVolumeSets(resources, newInstances);
List<CloudResource> networkResources = azureCloudResourceService.getNetworkResources(resources);
azureComputeResourceService.buildComputeResourcesForUpscale(ac, stack, scaledGroups, newInstances, reattachableVolumeSets, networkResources, adjustmentTypeWithThreshold);
List<CloudResourceStatus> successfulInstances = newInstances.stream().map(cloudResource -> new CloudResourceStatus(cloudResource, ResourceStatus.CREATED, cloudResource.getParameter(CloudResource.PRIVATE_ID, Long.class))).collect(Collectors.toList());
return ListUtils.union(Collections.singletonList(new CloudResourceStatus(armTemplate, ResourceStatus.IN_PROGRESS)), successfulInstances);
} catch (Retry.ActionFailedException e) {
LOGGER.error("Retry.ActionFailedException happened", e);
rollbackResources(ac, client, stack, cloudContext, resources, preDeploymentTime);
throw azureUtils.convertToCloudConnectorException(e.getCause(), "Stack upscale");
} catch (CloudException e) {
LOGGER.error("CloudException happened", e);
rollbackResources(ac, client, stack, cloudContext, resources, preDeploymentTime);
checkIfQuotaLimitIssued(e);
throw azureUtils.convertToCloudConnectorException(e, "Stack upscale");
} catch (RolledbackResourcesException e) {
LOGGER.error("RolledbackResourcesException happened", e);
rollbackResources(ac, client, stack, cloudContext, resources, preDeploymentTime);
throw new CloudConnectorException(String.format("Could not upscale Azure infrastructure, infrastructure was rolled back with resources: %s, %s", stackName, e.getMessage()), e);
} catch (Exception e) {
LOGGER.error("Exception happened", e);
rollbackResources(ac, client, stack, cloudContext, resources, preDeploymentTime);
throw new CloudConnectorException(String.format("Could not upscale Azure infrastructure, infrastructure was rolled back: %s, %s", stackName, e.getMessage()), e);
}
}
use of com.sequenceiq.cloudbreak.cloud.exception.RolledbackResourcesException in project cloudbreak by hortonworks.
the class CloudFailureHandlerTest method rollbackOnExactDoesNotReachThreshold.
@Test
void rollbackOnExactDoesNotReachThreshold() throws Exception {
ScaleContext scaleContext = new ScaleContext(true, AdjustmentType.EXACT, 4L);
Group group = mock(Group.class);
List<CloudResourceStatus> failuresList = new ArrayList<>();
CloudResource instance2 = newResource("instance-2", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
failuresList.add(new CloudResourceStatus(instance2, ResourceStatus.FAILED, "failed instance", 2L));
CloudResource instance3 = newResource("instance-3", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
failuresList.add(new CloudResourceStatus(instance3, ResourceStatus.FAILED, "failed instance", 3L));
CloudResource instance4 = newResource("instance-4", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
failuresList.add(new CloudResourceStatus(instance4, ResourceStatus.FAILED, "failed instance", 4L));
AuthenticatedContext auth = new AuthenticatedContext(mock(CloudContext.class), mock(CloudCredential.class));
ResourceBuilderContext resourceBuilderContext = mock(ResourceBuilderContext.class);
List<CloudResourceStatus> resourceStatuses = new ArrayList<>(failuresList);
CloudResource volume2 = newResource("vol-2", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(volume2, ResourceStatus.CREATED, 2L));
CloudResource volume4 = newResource("vol-4", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(volume4, ResourceStatus.CREATED, 4L));
CloudResource instance1 = newResource("instance-1", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(instance1, ResourceStatus.CREATED, 1L));
CloudResource volume1 = newResource("vol-1", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(volume1, ResourceStatus.CREATED, 1L));
CloudResource instance5 = newResource("instance-5", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(instance5, ResourceStatus.CREATED, 5L));
CloudResource volume5 = newResource("vol-5", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(volume5, ResourceStatus.CREATED, 5L));
CloudResource instance6 = newResource("instance-6", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(instance6, ResourceStatus.CREATED, 6L));
CloudResource volume6 = newResource("vol-6", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(volume6, ResourceStatus.CREATED, 6L));
ResourceBuilders resourceBuilders = mock(ResourceBuilders.class);
ArrayList<ComputeResourceBuilder<ResourceBuilderContext>> computeResourceBuilders = new ArrayList<>();
ComputeResourceBuilder instanceResourceBuilder = mock(ComputeResourceBuilder.class);
when(instanceResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_INSTANCE);
computeResourceBuilders.add(instanceResourceBuilder);
ComputeResourceBuilder volumeResourceBuilder = mock(ComputeResourceBuilder.class);
when(volumeResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_VOLUMESET);
computeResourceBuilders.add(volumeResourceBuilder);
when(resourceBuilders.compute(any())).thenReturn(computeResourceBuilders);
ArgumentCaptor<ResourceDeletionCallable> callableArgumentCaptor = ArgumentCaptor.forClass(ResourceDeletionCallable.class);
when(resourceBuilderExecutor.submit(callableArgumentCaptor.capture())).thenAnswer(invocation -> ((Callable) invocation.getArgument(0)).call());
RolledbackResourcesException rolledbackResourcesException = assertThrows(RolledbackResourcesException.class, () -> cloudFailureHandler.rollbackIfNecessary(new CloudFailureContext(auth, scaleContext, resourceBuilderContext), failuresList, resourceStatuses, group, resourceBuilders, 6));
assertEquals("Resources are rolled back because successful node count was lower than threshold. 3 nodes are failed. Error reason: failed instance", rolledbackResourcesException.getMessage());
verifyDeleteAll(instanceResourceBuilder, resourceBuilderContext, auth, instance1, instance5, instance6);
verifyDeleteAll(volumeResourceBuilder, resourceBuilderContext, auth, volume1, volume2, volume4, volume5, volume6);
verify(resourceBuilderExecutor, times(11)).submit(any(Callable.class));
}
use of com.sequenceiq.cloudbreak.cloud.exception.RolledbackResourcesException in project cloudbreak by hortonworks.
the class CloudFailureHandlerTest method rollbackOnPercentageDoesNotReachThreshold.
@Test
void rollbackOnPercentageDoesNotReachThreshold() throws Exception {
ScaleContext scaleContext = new ScaleContext(true, AdjustmentType.PERCENTAGE, 60L);
Group group = mock(Group.class);
ArrayList<CloudResourceStatus> failuresList = new ArrayList<>();
CloudResource instance2 = newResource("instance-2", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
failuresList.add(new CloudResourceStatus(instance2, ResourceStatus.FAILED, "failed instance", 2L));
CloudResource instance3 = newResource("instance-3", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
failuresList.add(new CloudResourceStatus(instance3, ResourceStatus.FAILED, "failed instance", 3L));
CloudResource instance4 = newResource("instance-4", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
failuresList.add(new CloudResourceStatus(instance4, ResourceStatus.FAILED, "failed instance", 4L));
AuthenticatedContext auth = new AuthenticatedContext(mock(CloudContext.class), mock(CloudCredential.class));
ResourceBuilderContext resourceBuilderContext = mock(ResourceBuilderContext.class);
ArrayList<CloudResourceStatus> resourceStatuses = new ArrayList<>(failuresList);
CloudResource volume2 = newResource("vol-2", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(volume2, ResourceStatus.CREATED, 2L));
CloudResource volume4 = newResource("vol-4", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(volume4, ResourceStatus.CREATED, 4L));
CloudResource instance1 = newResource("instance-1", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(instance1, ResourceStatus.CREATED, 1L));
CloudResource volume1 = newResource("vol-1", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(volume1, ResourceStatus.CREATED, 1L));
CloudResource instance5 = newResource("instance-5", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(instance5, ResourceStatus.CREATED, 5L));
CloudResource volume5 = newResource("vol-5", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(volume5, ResourceStatus.CREATED, 5L));
CloudResource instance6 = newResource("instance-6", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(instance6, ResourceStatus.CREATED, 6L));
CloudResource volume6 = newResource("vol-6", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
resourceStatuses.add(new CloudResourceStatus(volume6, ResourceStatus.CREATED, 6L));
ResourceBuilders resourceBuilders = mock(ResourceBuilders.class);
ArrayList<ComputeResourceBuilder<ResourceBuilderContext>> computeResourceBuilders = new ArrayList<>();
ComputeResourceBuilder instanceResourceBuilder = mock(ComputeResourceBuilder.class);
when(instanceResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_INSTANCE);
computeResourceBuilders.add(instanceResourceBuilder);
ComputeResourceBuilder volumeResourceBuilder = mock(ComputeResourceBuilder.class);
when(volumeResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_VOLUMESET);
computeResourceBuilders.add(volumeResourceBuilder);
when(resourceBuilders.compute(any())).thenReturn(computeResourceBuilders);
ArgumentCaptor<ResourceDeletionCallable> callableArgumentCaptor = ArgumentCaptor.forClass(ResourceDeletionCallable.class);
when(resourceBuilderExecutor.submit(callableArgumentCaptor.capture())).thenAnswer(invocation -> ((Callable) invocation.getArgument(0)).call());
RolledbackResourcesException rolledbackResourcesException = assertThrows(RolledbackResourcesException.class, () -> cloudFailureHandler.rollbackIfNecessary(new CloudFailureContext(auth, scaleContext, resourceBuilderContext), failuresList, resourceStatuses, group, resourceBuilders, 6));
assertEquals("Resources are rolled back because successful node count was lower than threshold. 3 nodes are failed. Error reason: failed instance", rolledbackResourcesException.getMessage());
verifyDeleteAll(instanceResourceBuilder, resourceBuilderContext, auth, instance1, instance5, instance6);
verifyDeleteAll(volumeResourceBuilder, resourceBuilderContext, auth, volume1, volume2, volume4, volume5, volume6);
verify(resourceBuilderExecutor, times(11)).submit(any(Callable.class));
}
Aggregations