Search in sources :

Example 11 with StopStartUpscaleStartInstancesResult

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());
}
Also used : StopStartUpscaleStartInstancesResult(com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesResult) StopStartUpscaleStartInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesRequest) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Event(reactor.bus.Event) LinkedList(java.util.LinkedList) List(java.util.List) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Test(org.junit.jupiter.api.Test)

Example 12 with StopStartUpscaleStartInstancesResult

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());
}
Also used : StopStartUpscaleStartInstancesResult(com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesResult) StopStartUpscaleStartInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesRequest) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Event(reactor.bus.Event) Test(org.junit.jupiter.api.Test)

Example 13 with StopStartUpscaleStartInstancesResult

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);
    }
}
Also used : Logger(org.slf4j.Logger) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) StopStartUpscaleStartInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesRequest) LoggerFactory(org.slf4j.LoggerFactory) CloudbreakRuntimeException(com.sequenceiq.cloudbreak.service.CloudbreakRuntimeException) Set(java.util.Set) StopStartUpscaleStartInstancesResult(com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesResult) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) EventBus(reactor.bus.EventBus) Collectors(java.util.stream.Collectors) CloudConnector(com.sequenceiq.cloudbreak.cloud.CloudConnector) Inject(javax.inject.Inject) List(java.util.List) Component(org.springframework.stereotype.Component) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Event(reactor.bus.Event) CloudPlatformConnectors(com.sequenceiq.cloudbreak.cloud.init.CloudPlatformConnectors) PollerStoppedException(com.dyngr.exception.PollerStoppedException) Collections(java.util.Collections) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudbreakRuntimeException(com.sequenceiq.cloudbreak.service.CloudbreakRuntimeException) PollerStoppedException(com.dyngr.exception.PollerStoppedException) StopStartUpscaleStartInstancesResult(com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesResult) StopStartUpscaleStartInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesRequest)

Aggregations

StopStartUpscaleStartInstancesResult (com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesResult)13 StopStartUpscaleStartInstancesRequest (com.sequenceiq.cloudbreak.cloud.event.instance.StopStartUpscaleStartInstancesRequest)12 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)12 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)9 Event (reactor.bus.Event)9 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)7 List (java.util.List)6 Test (org.junit.jupiter.api.Test)6 LinkedList (java.util.LinkedList)4 InstanceMetaData (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)3 StopStartUpscaleCommissionViaCMRequest (com.sequenceiq.cloudbreak.reactor.api.event.cluster.StopStartUpscaleCommissionViaCMRequest)3 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)2 InstanceStatus (com.sequenceiq.cloudbreak.cloud.model.InstanceStatus)2 AbstractStopStartUpscaleActions (com.sequenceiq.cloudbreak.core.flow2.cluster.stopstartus.StopStartUpscaleActions.AbstractStopStartUpscaleActions)2 StackFailureEvent (com.sequenceiq.cloudbreak.reactor.api.event.StackFailureEvent)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Bean (org.springframework.context.annotation.Bean)2 PollerStoppedException (com.dyngr.exception.PollerStoppedException)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1