Search in sources :

Example 6 with StopStartDownscaleStopInstancesRequest

use of com.sequenceiq.cloudbreak.cloud.event.instance.StopStartDownscaleStopInstancesRequest in project cloudbreak by hortonworks.

the class StopStartDownscaleActionsTest method testStopInstancesActionNoneDecommissioned.

@Test
void testStopInstancesActionNoneDecommissioned() throws Exception {
    // This behaviour is not ideal. Letting 0 stopped flow through the actual state machine instead of short-circuiting it.
    AbstractStopStartDownscaleActions<StopStartDownscaleDecommissionViaCMResult> action = (AbstractStopStartDownscaleActions<StopStartDownscaleDecommissionViaCMResult>) underTest.stopInstancesAction();
    initActionPrivateFields(action);
    List<InstanceMetaData> instancesActionableStarted = generateInstances(10, 100, InstanceStatus.SERVICES_HEALTHY, INSTANCE_GROUP_NAME_ACTIONABLE);
    List<InstanceMetaData> instancesActionableNotStarted = generateInstances(5, 200, InstanceStatus.STOPPED, INSTANCE_GROUP_NAME_ACTIONABLE);
    List<InstanceMetaData> instancesRandomStarted = generateInstances(8, 300, InstanceStatus.SERVICES_HEALTHY, INSTANCE_GROUP_NAME_RANDOM);
    List<InstanceMetaData> instancesRandomNotStarted = generateInstances(3, 400, InstanceStatus.STOPPED, INSTANCE_GROUP_NAME_RANDOM);
    Set<Long> instanceIdsToRemove = instancesActionableStarted.stream().limit(6).map(InstanceMetaData::getId).collect(Collectors.toUnmodifiableSet());
    List<InstanceMetaData> expectedToBeStopped = Collections.emptyList();
    Set<String> decommissionedHostsFqdns = Collections.emptySet();
    List<InstanceMetaData> notDecommissioned = instancesActionableStarted.subList(0, 6);
    List<String> notDecommissionedFqdns = notDecommissioned.stream().map(InstanceMetaData::getDiscoveryFQDN).collect(Collectors.toUnmodifiableList());
    StopStartDownscaleContext stopStartDownscaleContext = createContext(instanceIdsToRemove);
    StopStartDownscaleDecommissionViaCMRequest r = new StopStartDownscaleDecommissionViaCMRequest(1L, INSTANCE_GROUP_NAME_ACTIONABLE, instanceIdsToRemove);
    StopStartDownscaleDecommissionViaCMResult payload = new StopStartDownscaleDecommissionViaCMResult(r, decommissionedHostsFqdns, notDecommissionedFqdns);
    mockStackEtc(instancesActionableStarted, instancesActionableNotStarted, instancesRandomStarted, instancesRandomNotStarted);
    List<CloudInstance> expectedCloudInstances = mockInstanceMetadataToCloudInstanceConverter(expectedToBeStopped);
    when(reactorEventFactory.createEvent(anyMap(), isNotNull())).thenReturn(event);
    new AbstractActionTestSupport<>(action).doExecute(stopStartDownscaleContext, payload, Collections.emptyMap());
    verify(instanceMetaDataToCloudInstanceConverter).convert(eq(expectedToBeStopped), anyString(), any(StackAuthentication.class));
    verify(stopStartDownscaleFlowService).logCouldNotDecommission(eq(STACK_ID), eq(notDecommissionedFqdns));
    verify(stopStartDownscaleFlowService).clusterDownscalingStoppingInstances(eq(STACK_ID), eq(INSTANCE_GROUP_NAME_ACTIONABLE), eq(decommissionedHostsFqdns));
    verifyNoMoreInteractions(stopStartDownscaleFlowService);
    ArgumentCaptor<Object> argumentCaptor = ArgumentCaptor.forClass(Object.class);
    verify(reactorEventFactory).createEvent(anyMap(), argumentCaptor.capture());
    verify(eventBus).notify("STOPSTARTDOWNSCALESTOPINSTANCESREQUEST", event);
    assertThat(argumentCaptor.getValue()).isInstanceOf(StopStartDownscaleStopInstancesRequest.class);
    StopStartDownscaleStopInstancesRequest req = (StopStartDownscaleStopInstancesRequest) argumentCaptor.getValue();
    Assert.assertEquals(expectedCloudInstances, req.getCloudInstancesToStop());
}
Also used : StopStartDownscaleDecommissionViaCMResult(com.sequenceiq.cloudbreak.reactor.api.event.orchestration.StopStartDownscaleDecommissionViaCMResult) StackAuthentication(com.sequenceiq.cloudbreak.domain.StackAuthentication) StopStartDownscaleStopInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.StopStartDownscaleStopInstancesRequest) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) StopStartDownscaleDecommissionViaCMRequest(com.sequenceiq.cloudbreak.reactor.api.event.cluster.StopStartDownscaleDecommissionViaCMRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AbstractStopStartDownscaleActions(com.sequenceiq.cloudbreak.core.flow2.cluster.stopstartds.StopStartDownscaleActions.AbstractStopStartDownscaleActions) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) Test(org.junit.jupiter.api.Test)

Example 7 with StopStartDownscaleStopInstancesRequest

use of com.sequenceiq.cloudbreak.cloud.event.instance.StopStartDownscaleStopInstancesRequest in project cloudbreak by hortonworks.

the class StopStartDownscaleActions method stopInstancesAction.

@Bean(name = "STOPSTART_DOWNSCALE_STOP_INSTANCE_STATE")
public Action<?, ?> stopInstancesAction() {
    return new AbstractStopStartDownscaleActions<>(StopStartDownscaleDecommissionViaCMResult.class) {

        @Override
        protected void doExecute(StopStartDownscaleContext context, StopStartDownscaleDecommissionViaCMResult payload, Map<Object, Object> variables) throws Exception {
            Stack stack = context.getStack();
            if (payload.getNotDecommissionedHostFqdns().size() > 0) {
                stopStartDownscaleFlowService.logCouldNotDecommission(stack.getId(), payload.getNotDecommissionedHostFqdns());
            }
            Set<String> decommissionedFqdns = payload.getDecommissionedHostFqdns();
            stopStartDownscaleFlowService.clusterDownscalingStoppingInstances(stack.getId(), context.getHostGroupName(), decommissionedFqdns);
            Set<Long> instancesToStop = stackService.getPrivateIdsForHostNames(stack.getNotDeletedAndNotZombieInstanceMetaDataList(), payload.getDecommissionedHostFqdns());
            List<InstanceMetaData> instanceMetaDataList = stack.getNotDeletedAndNotZombieInstanceMetaDataList();
            List<InstanceMetaData> instanceMetaDataForHg = instanceMetaDataList.stream().filter(x -> x.getInstanceGroupName().equals(context.getHostGroupName())).collect(Collectors.toList());
            LOGGER.debug("InstanceInfoPreStop. hostGroup={}, allInstanceCount={}, hgInstanceCount={}. AllNotDeletedInstances=[{}]", context.getHostGroupName(), instanceMetaDataList.size(), instanceMetaDataForHg.size(), instanceMetaDataList);
            List<InstanceMetaData> toStopInstanceMetadataList = new LinkedList<>();
            for (InstanceMetaData instanceMetaData : instanceMetaDataForHg) {
                if (instancesToStop.contains(instanceMetaData.getPrivateId())) {
                    toStopInstanceMetadataList.add(instanceMetaData);
                }
            }
            LOGGER.debug("toStopInstanceMetadata: count={}, metadata=[{}]", toStopInstanceMetadataList.size(), toStopInstanceMetadataList);
            List<CloudInstance> cloudInstancesToStop = instanceMetaDataToCloudInstanceConverter.convert(toStopInstanceMetadataList, context.getStack().getEnvironmentCrn(), context.getStack().getStackAuthentication());
            StopStartDownscaleStopInstancesRequest request = new StopStartDownscaleStopInstancesRequest(context.getCloudContext(), context.getCloudCredential(), context.getCloudStack(), cloudInstancesToStop);
            sendEvent(context, request);
        }
    };
}
Also used : Action(org.springframework.statemachine.action.Action) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) AvailabilityZone.availabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone.availabilityZone) LoggerFactory(org.slf4j.LoggerFactory) StackToCloudStackConverter(com.sequenceiq.cloudbreak.converter.spi.StackToCloudStackConverter) Location.location(com.sequenceiq.cloudbreak.cloud.model.Location.location) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) StringUtils(org.apache.commons.lang3.StringUtils) StopStartDownscaleStopInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.StopStartDownscaleStopInstancesRequest) InstanceMetaDataToCloudInstanceConverter(com.sequenceiq.cloudbreak.converter.spi.InstanceMetaDataToCloudInstanceConverter) Map(java.util.Map) Location(com.sequenceiq.cloudbreak.cloud.model.Location) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) AbstractStackAction(com.sequenceiq.cloudbreak.core.flow2.AbstractStackAction) AbstractStackFailureAction(com.sequenceiq.cloudbreak.core.flow2.stack.AbstractStackFailureAction) StackFailureContext(com.sequenceiq.cloudbreak.core.flow2.stack.StackFailureContext) Set(java.util.Set) FlowParameters(com.sequenceiq.flow.core.FlowParameters) Crn(com.sequenceiq.cloudbreak.auth.crn.Crn) Collectors(java.util.stream.Collectors) Configuration(org.springframework.context.annotation.Configuration) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) StopStartDownscaleDecommissionViaCMRequest(com.sequenceiq.cloudbreak.reactor.api.event.cluster.StopStartDownscaleDecommissionViaCMRequest) List(java.util.List) StackUtil(com.sequenceiq.cloudbreak.util.StackUtil) Optional(java.util.Optional) STOPSTART_DOWNSCALE_FAILURE_EVENT(com.sequenceiq.cloudbreak.core.flow2.cluster.stopstartds.StopStartDownscaleEvent.STOPSTART_DOWNSCALE_FAILURE_EVENT) StackService(com.sequenceiq.cloudbreak.service.stack.StackService) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) StopStartDownscaleDecommissionViaCMResult(com.sequenceiq.cloudbreak.reactor.api.event.orchestration.StopStartDownscaleDecommissionViaCMResult) Payload(com.sequenceiq.cloudbreak.common.event.Payload) MDCBuilder(com.sequenceiq.cloudbreak.logger.MDCBuilder) Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) StackFailureEvent(com.sequenceiq.cloudbreak.reactor.api.event.StackFailureEvent) STOPSTART_DOWNSCALE_FINALIZED_EVENT(com.sequenceiq.cloudbreak.core.flow2.cluster.stopstartds.StopStartDownscaleEvent.STOPSTART_DOWNSCALE_FINALIZED_EVENT) StackEvent(com.sequenceiq.cloudbreak.reactor.api.event.StackEvent) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ResourceService(com.sequenceiq.cloudbreak.service.resource.ResourceService) CloudInstanceIdToInstanceMetaDataConverter(com.sequenceiq.cloudbreak.converter.CloudInstanceIdToInstanceMetaDataConverter) ClusterManagerType(com.sequenceiq.cloudbreak.common.type.ClusterManagerType) StateContext(org.springframework.statemachine.StateContext) StopStartDownscaleStopInstancesResult(com.sequenceiq.cloudbreak.cloud.event.instance.StopStartDownscaleStopInstancesResult) LinkedList(java.util.LinkedList) Logger(org.slf4j.Logger) Region.region(com.sequenceiq.cloudbreak.cloud.model.Region.region) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) StopStartDownscaleTriggerEvent(com.sequenceiq.cloudbreak.core.flow2.event.StopStartDownscaleTriggerEvent) Bean(org.springframework.context.annotation.Bean) VisibleForTesting(com.google.common.annotations.VisibleForTesting) StopStartDownscaleDecommissionViaCMResult(com.sequenceiq.cloudbreak.reactor.api.event.orchestration.StopStartDownscaleDecommissionViaCMResult) StopStartDownscaleStopInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.StopStartDownscaleStopInstancesRequest) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) LinkedList(java.util.LinkedList) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) Map(java.util.Map) Bean(org.springframework.context.annotation.Bean)

Aggregations

StopStartDownscaleStopInstancesRequest (com.sequenceiq.cloudbreak.cloud.event.instance.StopStartDownscaleStopInstancesRequest)7 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)6 StopStartDownscaleStopInstancesResult (com.sequenceiq.cloudbreak.cloud.event.instance.StopStartDownscaleStopInstancesResult)4 InstanceMetaData (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)4 StopStartDownscaleDecommissionViaCMRequest (com.sequenceiq.cloudbreak.reactor.api.event.cluster.StopStartDownscaleDecommissionViaCMRequest)4 StopStartDownscaleDecommissionViaCMResult (com.sequenceiq.cloudbreak.reactor.api.event.orchestration.StopStartDownscaleDecommissionViaCMResult)4 Test (org.junit.jupiter.api.Test)4 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)3 AbstractStopStartDownscaleActions (com.sequenceiq.cloudbreak.core.flow2.cluster.stopstartds.StopStartDownscaleActions.AbstractStopStartDownscaleActions)3 StackAuthentication (com.sequenceiq.cloudbreak.domain.StackAuthentication)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 Event (reactor.bus.Event)3 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)2 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Crn (com.sequenceiq.cloudbreak.auth.crn.Crn)1 AvailabilityZone.availabilityZone (com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone.availabilityZone)1 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)1 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)1 InstanceStatus (com.sequenceiq.cloudbreak.cloud.model.InstanceStatus)1