use of com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesResult in project cloudbreak by hortonworks.
the class StopStartUpscaleStartInstancesHandlerTest method testUnableToCollectInstancesFromCloudPovider.
@Test
void testUnableToCollectInstancesFromCloudPovider() {
List<CloudInstance> stoppedInstancesInHg = generateCloudInstances(3);
List<CloudInstance> allInstancesInHg = generateCloudInstances(10);
List<CloudInstance> startedInstancesWithServicesNotRunning = null;
int numInstancesToStart = 5;
when(instanceConnector.checkWithoutRetry(any(AuthenticatedContext.class), any(List.class))).thenThrow(new RuntimeException("CloudProviderCheckStateError"));
StopStartUpscaleStartInstancesRequest request = new StopStartUpscaleStartInstancesRequest(cloudContext, cloudCredential, cloudStack, "compute", stoppedInstancesInHg, allInstancesInHg, startedInstancesWithServicesNotRunning, numInstancesToStart);
Event event = new Event(request);
underTest.accept(event);
ArgumentCaptor<Event> resultCaptor = ArgumentCaptor.forClass(Event.class);
verify(eventBus).notify(any(Object.class), resultCaptor.capture());
assertEquals(1, resultCaptor.getAllValues().size());
Event resultEvent = resultCaptor.getValue();
assertEquals(StopStartUpscaleStartInstancesResult.class, resultEvent.getData().getClass());
StopStartUpscaleStartInstancesResult result = (StopStartUpscaleStartInstancesResult) resultEvent.getData();
assertEquals("CloudProviderCheckStateError", result.getErrorDetails().getMessage());
assertEquals(0, result.getAffectedInstanceStatuses().size());
assertEquals(EventStatus.FAILED, result.getStatus());
assertEquals("STOPSTARTUPSCALESTARTINSTANCESRESULT_ERROR", result.selector());
}
use of com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesResult in project cloudbreak by hortonworks.
the class StopStartUpscaleStartInstancesHandlerTest method testNegativeInstancesToStart.
@Test
void testNegativeInstancesToStart() {
List<CloudInstance> stoppedInstancesInHg = generateCloudInstances(5);
List<CloudInstance> allInstancesInHg = generateCloudInstances(10);
List<CloudInstance> startedInstancesWithServicesNotRunning = null;
int numInstancesToStart = -1;
StopStartUpscaleStartInstancesRequest request = new StopStartUpscaleStartInstancesRequest(cloudContext, cloudCredential, cloudStack, "compute", stoppedInstancesInHg, allInstancesInHg, startedInstancesWithServicesNotRunning, numInstancesToStart);
Event event = new Event(request);
underTest.accept(event);
ArgumentCaptor<Event> resultCaptor = ArgumentCaptor.forClass(Event.class);
verify(eventBus).notify(any(Object.class), resultCaptor.capture());
verifyNoMoreInteractions(instanceConnector);
assertEquals(1, resultCaptor.getAllValues().size());
Event resultEvent = resultCaptor.getValue();
assertEquals(StopStartUpscaleStartInstancesResult.class, resultEvent.getData().getClass());
StopStartUpscaleStartInstancesResult result = (StopStartUpscaleStartInstancesResult) resultEvent.getData();
assertEquals(0, result.getAffectedInstanceStatuses().size());
}
use of com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesResult in project cloudbreak by hortonworks.
the class StopStartUpscaleStartInstancesHandler method accept.
@Override
public void accept(Event<StopStartUpscaleStartInstancesRequest> event) {
StopStartUpscaleStartInstancesRequest request = event.getData();
LOGGER.info("StopStartUpscaleStartInstancesHandler: {}", event.getData().getResourceId());
int numInstancesToStart = request.getNumInstancesToStart() - request.getStartedInstancesWithServicesNotRunning().size();
if (numInstancesToStart <= 0) {
// TODO CB-15132: This is currently not exercised, since the Flow itself does not send any good information about running instances.
LOGGER.debug("No nodes to start. Start requested for numInstances={}, Running CM instances with services not running={}", request.getNumInstancesToStart(), request.getStartedInstancesWithServicesNotRunning().size());
List<CloudInstance> startedInstancesWithServicesNotRunning = request.getStartedInstancesWithServicesNotRunning();
List<CloudVmInstanceStatus> vmInstanceStatuses = startedInstancesWithServicesNotRunning.stream().map(i -> new CloudVmInstanceStatus(i, InstanceStatus.STARTED)).collect(Collectors.toUnmodifiableList());
StopStartUpscaleStartInstancesResult result = new StopStartUpscaleStartInstancesResult(request.getResourceId(), request, vmInstanceStatuses);
notify(result, event);
return;
}
CloudContext cloudContext = request.getCloudContext();
try {
CloudConnector<?> connector = cloudPlatformConnectors.get(cloudContext.getPlatformVariant());
AuthenticatedContext ac = getAuthenticatedContext(request, cloudContext, connector);
List<CloudInstance> stoppedInstancesInCbHg = request.getStoppedCloudInstancesInHg();
List<CloudVmInstanceStatus> stoppedInstancesOnCloudProvider = Collections.emptyList();
List<CloudInstance> instancesToStart = Collections.emptyList();
if (numInstancesToStart > stoppedInstancesInCbHg.size()) {
// See if we can find additional instances on the cloud-provider which can potentially be STARTED.
stoppedInstancesOnCloudProvider = collectStoppedInstancesFromCloudProvider(connector, ac, request.getAllInstancesInHg());
instancesToStart = getInstancesToStart(stoppedInstancesInCbHg, stoppedInstancesOnCloudProvider, request.getHostGroupName(), numInstancesToStart);
} else {
// Filter based on CB state, but confirm against the cloud provider. It is OK to start fewer instances than requested.
// TODO CB-15132: We could go back to the cloud provider and try finding additional stopped instances.
instancesToStart = stoppedInstancesInCbHg.subList(0, numInstancesToStart);
stoppedInstancesOnCloudProvider = collectStoppedInstancesFromCloudProvider(connector, ac, instancesToStart);
Set<CloudInstance> stoppedInstancesOnCloudProviderSet = stoppedInstancesOnCloudProvider.stream().map(CloudVmInstanceStatus::getCloudInstance).collect(Collectors.toUnmodifiableSet());
instancesToStart = instancesToStart.stream().filter(i -> stoppedInstancesOnCloudProviderSet.contains(i)).collect(Collectors.toList());
}
LOGGER.info("Requested instances to start={}, actual instances being started={}, numInstancesWithServicesNotRunning={}" + ", numStoppedInstanceCountInCbHg={}, numStoppedInstancesOnCloudProvider(subset)={}", request.getNumInstancesToStart(), instancesToStart.size(), request.getStartedInstancesWithServicesNotRunning().size(), stoppedInstancesInCbHg.size(), stoppedInstancesOnCloudProvider.size());
LOGGER.debug("Attempting to start instances. count={}, instanceIds={}", instancesToStart.size(), instancesToStart.stream().map(i -> i.getInstanceId()).collect(Collectors.toList()));
List<CloudVmInstanceStatus> instanceStatuses = Collections.emptyList();
if (instancesToStart.size() > 0) {
instanceStatuses = startInstances(connector, ac, instancesToStart);
LOGGER.info("Started instances. Result: {}", instanceStatuses);
} else {
LOGGER.info("No instances to start");
}
StopStartUpscaleStartInstancesResult result = new StopStartUpscaleStartInstancesResult(request.getResourceId(), request, instanceStatuses);
notify(result, event);
} catch (Exception e) {
// TODO CB-15132: Try propagating specific information in the error, so that a later step can potentially attempt
// to recover from this, or proceed with a reduced set of nodes.
String message = "Failed while attempting to start instances";
LOGGER.error(message);
StopStartUpscaleStartInstancesResult result = new StopStartUpscaleStartInstancesResult(message, e, request.getResourceId(), request);
notify(result, event);
}
}
Aggregations