use of com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold in project cloudbreak by hortonworks.
the class StackUpscaleServiceTest method testBestEffortButFail.
@Test
public void testBestEffortButFail() throws QuotaExceededException {
CloudConnector connector = mock(CloudConnector.class);
ResourceConnector resourceConnector = mock(ResourceConnector.class);
when(connector.resources()).thenReturn(resourceConnector);
when(resourceConnector.upscale(any(), any(), any(), any())).thenThrow(new QuotaExceededException(40, 36, 40, "quota error", new Exception()));
AdjustmentTypeWithThreshold adjustmentTypeWithThreshold = new AdjustmentTypeWithThreshold(AdjustmentType.BEST_EFFORT, 0L);
List<Group> groups = new ArrayList<>();
List<CloudInstance> workerInstances = new ArrayList<>();
workerInstances.add(new CloudInstance("W1", getInstanceTemplate(1L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker1.example.com")));
workerInstances.add(new CloudInstance(null, getInstanceTemplate(2L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker2.example.com")));
workerInstances.add(new CloudInstance(null, getInstanceTemplate(3L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker3.example.com")));
groups.add(new Group("worker", InstanceGroupType.CORE, workerInstances, mock(Security.class), mock(CloudInstance.class), mock(InstanceAuthentication.class), "admin", "ssh", 100, Optional.empty(), null, emptyMap()));
List<CloudInstance> computeInstances = new ArrayList<>();
computeInstances.add(new CloudInstance("C1", getInstanceTemplate(4L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute1.example.com")));
computeInstances.add(new CloudInstance(null, getInstanceTemplate(5L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute2.example.com")));
computeInstances.add(new CloudInstance(null, getInstanceTemplate(6L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute3.example.com")));
groups.add(new Group("compute", InstanceGroupType.CORE, computeInstances, mock(Security.class), mock(CloudInstance.class), mock(InstanceAuthentication.class), "admin", "ssh", 100, Optional.empty(), null, emptyMap()));
CloudStack cloudStack = new CloudStack(groups, mock(Network.class), mock(Image.class), Collections.emptyMap(), Collections.emptyMap(), "template", mock(InstanceAuthentication.class), "username", "publickey", mock(SpiFileSystem.class));
UpscaleStackRequest<UpscaleStackResult> upscaleStackRequest = new UpscaleStackRequest<>(mock(CloudContext.class), mock(CloudCredential.class), cloudStack, new ArrayList<>(), adjustmentTypeWithThreshold);
Assertions.assertThrows(CloudConnectorException.class, () -> stackUpscaleService.upscale(mock(AuthenticatedContext.class), upscaleStackRequest, connector));
verify(flowMessageService, times(1)).fireEventAndLog(upscaleStackRequest.getResourceId(), UPDATE_IN_PROGRESS.name(), STACK_UPSCALE_QUOTA_ISSUE, "quota error");
ArgumentCaptor<CloudStack> cloudStackArgumentCaptor = ArgumentCaptor.forClass(CloudStack.class);
verify(resourceConnector, times(1)).upscale(any(), cloudStackArgumentCaptor.capture(), any(), eq(adjustmentTypeWithThreshold));
}
use of com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold in project cloudbreak by hortonworks.
the class StackUpscaleServiceTest method testBestEffort.
@Test
public void testBestEffort() throws QuotaExceededException {
CloudConnector connector = mock(CloudConnector.class);
ResourceConnector resourceConnector = mock(ResourceConnector.class);
when(connector.resources()).thenReturn(resourceConnector);
when(resourceConnector.upscale(any(), any(), any(), any())).thenThrow(new QuotaExceededException(40, 20, 40, "quota error", new Exception())).thenReturn(Collections.emptyList());
AdjustmentTypeWithThreshold adjustmentTypeWithThreshold = new AdjustmentTypeWithThreshold(AdjustmentType.BEST_EFFORT, 0L);
List<Group> groups = new ArrayList<>();
List<CloudInstance> workerInstances = new ArrayList<>();
workerInstances.add(new CloudInstance("W1", getInstanceTemplate(1L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker1.example.com")));
workerInstances.add(new CloudInstance(null, getInstanceTemplate(2L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker2.example.com")));
workerInstances.add(new CloudInstance(null, getInstanceTemplate(3L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker3.example.com")));
groups.add(new Group("worker", InstanceGroupType.CORE, workerInstances, mock(Security.class), mock(CloudInstance.class), mock(InstanceAuthentication.class), "admin", "ssh", 100, Optional.empty(), null, emptyMap()));
List<CloudInstance> computeInstances = new ArrayList<>();
computeInstances.add(new CloudInstance("C1", getInstanceTemplate(4L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute1.example.com")));
computeInstances.add(new CloudInstance(null, getInstanceTemplate(5L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute2.example.com")));
computeInstances.add(new CloudInstance(null, getInstanceTemplate(6L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute3.example.com")));
groups.add(new Group("compute", InstanceGroupType.CORE, computeInstances, mock(Security.class), mock(CloudInstance.class), mock(InstanceAuthentication.class), "admin", "ssh", 100, Optional.empty(), null, emptyMap()));
CloudStack cloudStack = new CloudStack(groups, mock(Network.class), mock(Image.class), Collections.emptyMap(), Collections.emptyMap(), "template", mock(InstanceAuthentication.class), "username", "publickey", mock(SpiFileSystem.class));
UpscaleStackRequest<UpscaleStackResult> upscaleStackRequest = new UpscaleStackRequest<>(mock(CloudContext.class), mock(CloudCredential.class), cloudStack, new ArrayList<>(), adjustmentTypeWithThreshold);
stackUpscaleService.upscale(mock(AuthenticatedContext.class), upscaleStackRequest, connector);
verify(flowMessageService, times(1)).fireEventAndLog(upscaleStackRequest.getResourceId(), UPDATE_IN_PROGRESS.name(), STACK_UPSCALE_QUOTA_ISSUE, "quota error");
ArgumentCaptor<CloudStack> cloudStackArgumentCaptor = ArgumentCaptor.forClass(CloudStack.class);
verify(resourceConnector, times(2)).upscale(any(), cloudStackArgumentCaptor.capture(), any(), eq(adjustmentTypeWithThreshold));
List<CloudStack> cloudStacks = cloudStackArgumentCaptor.getAllValues();
List<CloudInstance> cloudInstances = cloudStacks.get(1).getGroups().stream().flatMap(group -> group.getInstances().stream()).collect(Collectors.toList());
List<CloudInstance> cloudInstancesWithoutInstanceId = cloudInstances.stream().filter(cloudInstance -> cloudInstance.getInstanceId() == null).collect(Collectors.toList());
assertEquals(4, cloudInstances.size());
assertEquals(2, cloudInstancesWithoutInstanceId.size());
}
use of com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold in project cloudbreak by hortonworks.
the class StackUpscaleServiceTest method testExactWithProvisionableCount.
@Test
public void testExactWithProvisionableCount() throws QuotaExceededException {
CloudConnector connector = mock(CloudConnector.class);
ResourceConnector resourceConnector = mock(ResourceConnector.class);
when(connector.resources()).thenReturn(resourceConnector);
when(resourceConnector.upscale(any(), any(), any(), any())).thenThrow(new QuotaExceededException(40, 20, 40, "quota error", new Exception())).thenReturn(Collections.emptyList());
AdjustmentTypeWithThreshold adjustmentTypeWithThreshold = new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, 2L);
List<Group> groups = new ArrayList<>();
List<CloudInstance> workerInstances = new ArrayList<>();
workerInstances.add(new CloudInstance("W1", getInstanceTemplate(1L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker1.example.com")));
workerInstances.add(new CloudInstance(null, getInstanceTemplate(2L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker2.example.com")));
workerInstances.add(new CloudInstance(null, getInstanceTemplate(3L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker3.example.com")));
groups.add(new Group("worker", InstanceGroupType.CORE, workerInstances, mock(Security.class), mock(CloudInstance.class), mock(InstanceAuthentication.class), "admin", "ssh", 100, Optional.empty(), null, emptyMap()));
List<CloudInstance> computeInstances = new ArrayList<>();
computeInstances.add(new CloudInstance("C1", getInstanceTemplate(4L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute1.example.com")));
computeInstances.add(new CloudInstance(null, getInstanceTemplate(5L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute2.example.com")));
computeInstances.add(new CloudInstance(null, getInstanceTemplate(6L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute3.example.com")));
groups.add(new Group("compute", InstanceGroupType.CORE, computeInstances, mock(Security.class), mock(CloudInstance.class), mock(InstanceAuthentication.class), "admin", "ssh", 100, Optional.empty(), null, emptyMap()));
CloudStack cloudStack = new CloudStack(groups, mock(Network.class), mock(Image.class), Collections.emptyMap(), Collections.emptyMap(), "template", mock(InstanceAuthentication.class), "username", "publickey", mock(SpiFileSystem.class));
UpscaleStackRequest<UpscaleStackResult> upscaleStackRequest = new UpscaleStackRequest<>(mock(CloudContext.class), mock(CloudCredential.class), cloudStack, new ArrayList<>(), adjustmentTypeWithThreshold);
stackUpscaleService.upscale(mock(AuthenticatedContext.class), upscaleStackRequest, connector);
verify(flowMessageService, times(1)).fireEventAndLog(upscaleStackRequest.getResourceId(), UPDATE_IN_PROGRESS.name(), STACK_UPSCALE_QUOTA_ISSUE, "quota error");
ArgumentCaptor<CloudStack> cloudStackArgumentCaptor = ArgumentCaptor.forClass(CloudStack.class);
verify(resourceConnector, times(2)).upscale(any(), cloudStackArgumentCaptor.capture(), any(), eq(adjustmentTypeWithThreshold));
List<CloudStack> cloudStacks = cloudStackArgumentCaptor.getAllValues();
List<CloudInstance> cloudInstances = cloudStacks.get(1).getGroups().stream().flatMap(group -> group.getInstances().stream()).collect(Collectors.toList());
List<CloudInstance> cloudInstancesWithoutInstanceId = cloudInstances.stream().filter(cloudInstance -> cloudInstance.getInstanceId() == null).collect(Collectors.toList());
assertEquals(4, cloudInstances.size());
assertEquals(2, cloudInstancesWithoutInstanceId.size());
}
use of com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold in project cloudbreak by hortonworks.
the class StackUpscaleServiceTest method testPercentageButFailBecauseHigherThanProvisionable.
@Test
public void testPercentageButFailBecauseHigherThanProvisionable() throws QuotaExceededException {
CloudConnector connector = mock(CloudConnector.class);
ResourceConnector resourceConnector = mock(ResourceConnector.class);
when(connector.resources()).thenReturn(resourceConnector);
when(resourceConnector.upscale(any(), any(), any(), any())).thenThrow(new QuotaExceededException(40, 20, 40, "quota error", new Exception()));
AdjustmentTypeWithThreshold adjustmentTypeWithThreshold = new AdjustmentTypeWithThreshold(AdjustmentType.PERCENTAGE, 60L);
List<Group> groups = new ArrayList<>();
List<CloudInstance> workerInstances = new ArrayList<>();
workerInstances.add(new CloudInstance("W1", getInstanceTemplate(1L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker1.example.com")));
workerInstances.add(new CloudInstance(null, getInstanceTemplate(2L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker2.example.com")));
workerInstances.add(new CloudInstance(null, getInstanceTemplate(3L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker3.example.com")));
groups.add(new Group("worker", InstanceGroupType.CORE, workerInstances, mock(Security.class), mock(CloudInstance.class), mock(InstanceAuthentication.class), "admin", "ssh", 100, Optional.empty(), null, emptyMap()));
List<CloudInstance> computeInstances = new ArrayList<>();
computeInstances.add(new CloudInstance("C1", getInstanceTemplate(4L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute1.example.com")));
computeInstances.add(new CloudInstance(null, getInstanceTemplate(5L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute2.example.com")));
computeInstances.add(new CloudInstance(null, getInstanceTemplate(6L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute3.example.com")));
groups.add(new Group("compute", InstanceGroupType.CORE, computeInstances, mock(Security.class), mock(CloudInstance.class), mock(InstanceAuthentication.class), "admin", "ssh", 100, Optional.empty(), null, emptyMap()));
CloudStack cloudStack = new CloudStack(groups, mock(Network.class), mock(Image.class), Collections.emptyMap(), Collections.emptyMap(), "template", mock(InstanceAuthentication.class), "username", "publickey", mock(SpiFileSystem.class));
UpscaleStackRequest<UpscaleStackResult> upscaleStackRequest = new UpscaleStackRequest<>(mock(CloudContext.class), mock(CloudCredential.class), cloudStack, new ArrayList<>(), adjustmentTypeWithThreshold);
Assertions.assertThrows(CloudConnectorException.class, () -> stackUpscaleService.upscale(mock(AuthenticatedContext.class), upscaleStackRequest, connector));
verify(flowMessageService, times(1)).fireEventAndLog(upscaleStackRequest.getResourceId(), UPDATE_IN_PROGRESS.name(), STACK_UPSCALE_QUOTA_ISSUE, "quota error");
ArgumentCaptor<CloudStack> cloudStackArgumentCaptor = ArgumentCaptor.forClass(CloudStack.class);
verify(resourceConnector, times(1)).upscale(any(), cloudStackArgumentCaptor.capture(), any(), eq(adjustmentTypeWithThreshold));
}
use of com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold in project cloudbreak by hortonworks.
the class FreeIpaUpscaleActions method addInstancesAction.
@Bean(name = "UPSCALE_ADD_INSTANCES_STATE")
public Action<?, ?> addInstancesAction() {
return new AbstractUpscaleAction<>(StackEvent.class) {
@Override
protected void doExecute(StackContext context, StackEvent payload, Map<Object, Object> variables) {
Stack stack = context.getStack();
stackUpdater.updateStackStatus(stack.getId(), getInProgressStatus(variables), "Adding instances");
List<CloudInstance> newInstances = buildNewInstances(context.getStack(), getInstanceCountByGroup(variables));
if (newInstances.isEmpty()) {
skipAddingNewInstances(context, stack);
} else {
addNewInstances(context, stack, newInstances);
}
}
private void skipAddingNewInstances(StackContext context, Stack stack) {
List<CloudResourceStatus> list = resourceService.getAllAsCloudResourceStatus(stack.getId());
UpscaleStackRequest<UpscaleStackResult> request = new UpscaleStackRequest<>(context.getCloudContext(), context.getCloudCredential(), context.getCloudStack(), ResourceLists.transform(list), new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, 0L));
UpscaleStackResult result = new UpscaleStackResult(request.getResourceId(), ResourceStatus.CREATED, list);
sendEvent(context, result.selector(), result);
}
private void addNewInstances(StackContext context, Stack stack, List<CloudInstance> newInstances) {
Stack updatedStack = instanceMetaDataService.saveInstanceAndGetUpdatedStack(stack, newInstances);
List<CloudResource> cloudResources = resourceService.findAllByStackId(stack.getId()).stream().map(resource -> resourceConverter.convert(resource)).collect(Collectors.toList());
CloudStack updatedCloudStack = cloudStackConverter.convert(updatedStack);
UpscaleStackRequest<UpscaleStackResult> request = new UpscaleStackRequest<>(context.getCloudContext(), context.getCloudCredential(), updatedCloudStack, cloudResources, new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, (long) newInstances.size()));
sendEvent(context, request.selector(), request);
}
private List<CloudInstance> buildNewInstances(Stack stack, int instanceCountByGroup) {
long privateId = privateIdProvider.getFirstValidPrivateId(stack.getInstanceGroups());
List<CloudInstance> newInstances = new ArrayList<>();
for (InstanceGroup instanceGroup : stack.getInstanceGroups()) {
int remainingInstances = instanceCountByGroup - instanceGroup.getNotDeletedInstanceMetaDataSet().size();
for (long i = 0; i < remainingInstances; ++i) {
newInstances.add(cloudStackConverter.buildInstance(stack, null, instanceGroup, stack.getStackAuthentication(), privateId++, InstanceStatus.CREATE_REQUESTED));
}
}
return newInstances;
}
};
}
Aggregations