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());
}
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);
}
};
}
Aggregations