use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest in project cloudbreak by hortonworks.
the class ExternalDatabaseTerminationActions method externalDatabaseTermination.
@Bean(name = "WAIT_FOR_EXTERNAL_DATABASE_TERMINATION_STATE")
public Action<?, ?> externalDatabaseTermination() {
return new AbstractExternalDatabaseTerminationAction<>(TerminationEvent.class) {
@Override
protected void doExecute(ExternalDatabaseContext context, TerminationEvent payload, Map<Object, Object> variables) {
Stack stack = context.getStack();
TerminateExternalDatabaseRequest request = new TerminateExternalDatabaseRequest(stack.getId(), "TerminateExternalDatabaseRequest", stack.getName(), stack.getResourceCrn(), payload.getTerminationType().isForced());
sendEvent(context, request);
}
};
}
use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest in project cloudbreak by hortonworks.
the class ExternalDatabaseTerminationActionsTest method externalDatabaseTermination.
@ParameterizedTest
@ValueSource(booleans = { false, true })
void externalDatabaseTermination(boolean forced) {
TerminationEvent terminationEventPayload = new TerminationEvent(ACTION_PAYLOAD_SELECTOR, STACK_ID, forced ? TerminationType.FORCED : TerminationType.REGULAR);
when(stateContext.getMessageHeader(MessageFactory.HEADERS.DATA.name())).thenReturn(terminationEventPayload);
Action<?, ?> action = configureAction(underTest::externalDatabaseTermination);
action.execute(stateContext);
verifyNoMoreInteractions(stackUpdaterService);
verify(eventBus).notify(selectorArgumentCaptor.capture(), eventArgumentCaptor.capture());
verify(reactorEventFactory).createEvent(headersArgumentCaptor.capture(), payloadArgumentCaptor.capture());
assertThat(selectorArgumentCaptor.getValue()).isEqualTo("TerminateExternalDatabaseRequest");
Object rawPayload = payloadArgumentCaptor.getValue();
assertThat(rawPayload).isInstanceOf(TerminateExternalDatabaseRequest.class);
TerminateExternalDatabaseRequest request = (TerminateExternalDatabaseRequest) rawPayload;
assertThat(request.isForced()).isEqualTo(forced);
}
use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest in project cloudbreak by hortonworks.
the class TerminateExternalDatabaseHandlerTest method acceptCatchErrors.
@ParameterizedTest
@ValueSource(classes = { UserBreakException.class, PollerStoppedException.class, PollerException.class, Exception.class })
@MockitoSettings(strictness = Strictness.LENIENT)
void acceptCatchErrors(Class<? extends Exception> exceptionClass) {
doAnswer(a -> {
throw exceptionClass.getDeclaredConstructor().newInstance();
}).when(terminationService).terminateDatabase(any(), any(DatabaseAvailabilityType.class), any(), anyBoolean());
DetailedEnvironmentResponse environment = new DetailedEnvironmentResponse();
environment.setCloudPlatform("cloudplatform");
when(environmentClientService.getByCrn(anyString())).thenReturn(environment);
when(stackService.getById(anyLong())).thenReturn(buildStack(DatabaseAvailabilityType.HA));
TerminateExternalDatabaseRequest request = new TerminateExternalDatabaseRequest(STACK_ID, "selector", "resourceName", "crn", true);
Event<TerminateExternalDatabaseRequest> event = new Event<>(EVENT_HEADERS, request);
underTest.accept(event);
verify(terminationService).terminateDatabase(any(), eq(DatabaseAvailabilityType.HA), eq(environment), eq(true));
verify(stackUpdaterService).updateStatus(eq(STACK_ID), eq(DetailedStackStatus.EXTERNAL_DATABASE_DELETION_IN_PROGRESS), eq(ResourceEvent.CLUSTER_EXTERNAL_DATABASE_DELETION_STARTED), eq("External database deletion in progress"));
verify(stackUpdaterService, never()).updateStatus(eq(STACK_ID), eq(DetailedStackStatus.AVAILABLE), eq(ResourceEvent.CLUSTER_EXTERNAL_DATABASE_CREATION_FINISHED), anyString());
verify(eventBus).notify(eq("TerminateExternalDatabaseFailed"), any(Event.class));
}
use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest in project cloudbreak by hortonworks.
the class TerminateExternalDatabaseHandlerTest method accept.
@ParameterizedTest
@ValueSource(booleans = { false, true })
void accept(boolean needHA) {
DetailedEnvironmentResponse environment = new DetailedEnvironmentResponse();
environment.setCloudPlatform("cloudplatform");
when(environmentClientService.getByCrn(anyString())).thenReturn(environment);
doAnswer(a -> {
Cluster cluster = a.getArgument(0);
cluster.setDatabaseServerCrn(DATABASE_CRN);
return null;
}).when(terminationService).terminateDatabase(any(), any(), any(), anyBoolean());
when(stackService.getById(anyLong())).thenReturn(buildStack(DatabaseAvailabilityType.HA));
TerminateExternalDatabaseRequest request = new TerminateExternalDatabaseRequest(STACK_ID, "selector", "resourceName", "crn", needHA);
Event<TerminateExternalDatabaseRequest> event = new Event<>(EVENT_HEADERS, request);
underTest.accept(event);
verify(terminationService).terminateDatabase(any(), eq(DatabaseAvailabilityType.HA), eq(environment), eq(needHA));
verify(stackUpdaterService).updateStatus(eq(STACK_ID), eq(DetailedStackStatus.EXTERNAL_DATABASE_DELETION_IN_PROGRESS), eq(ResourceEvent.CLUSTER_EXTERNAL_DATABASE_DELETION_STARTED), eq("External database deletion in progress"));
verify(stackUpdaterService).updateStatus(eq(STACK_ID), eq(DetailedStackStatus.AVAILABLE), eq(ResourceEvent.CLUSTER_EXTERNAL_DATABASE_DELETION_FINISHED), eq("External database deletion finished"));
ArgumentCaptor<Event<TerminateExternalDatabaseResult>> eventCaptor = ArgumentCaptor.forClass(Event.class);
verify(eventBus).notify(eq("TerminateExternalDatabaseResult"), eventCaptor.capture());
Event<TerminateExternalDatabaseResult> value = eventCaptor.getValue();
assertThat(value.getHeaders()).isEqualTo(EVENT_HEADERS);
assertThat(value.getData().getResourceCrn()).isEqualTo(DATABASE_CRN);
}
use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest in project cloudbreak by hortonworks.
the class TerminateExternalDatabaseHandler method accept.
@Override
public void accept(Event<TerminateExternalDatabaseRequest> terminateExternalDatabaseRequest) {
LOGGER.debug("In TerminateExternalDatabaseHandler.accept");
TerminateExternalDatabaseRequest request = terminateExternalDatabaseRequest.getData();
Stack stack = stackService.getById(request.getResourceId());
DatabaseAvailabilityType externalDatabase = ObjectUtils.defaultIfNull(stack.getExternalDatabaseCreationType(), DatabaseAvailabilityType.NONE);
LOGGER.debug("External database: {} for stack {}", externalDatabase.name(), stack.getName());
Selectable result;
try {
if (externalDatabase.isEmbedded()) {
LOGGER.info("External database for stack {} is not requested. Termination is not necessary.", stack.getName());
result = new TerminateExternalDatabaseResult(stack.getId(), EXTERNAL_DATABASE_WAIT_TERMINATION_SUCCESS_EVENT.event(), stack.getName(), null);
} else {
LOGGER.debug("Updating stack {} status from {} to {}", stack.getName(), stack.getStatus().name(), DetailedStackStatus.EXTERNAL_DATABASE_DELETION_IN_PROGRESS.name());
stackUpdaterService.updateStatus(stack.getId(), DetailedStackStatus.EXTERNAL_DATABASE_DELETION_IN_PROGRESS, ResourceEvent.CLUSTER_EXTERNAL_DATABASE_DELETION_STARTED, "External database deletion in progress");
LOGGER.debug("Getting environment CRN for stack {}", stack.getName());
DetailedEnvironmentResponse environment = environmentClientService.getByCrn(stack.getEnvironmentCrn());
terminationService.terminateDatabase(stack.getCluster(), externalDatabase, environment, request.isForced());
LOGGER.debug("Updating stack {} status from {} to {}", stack.getName(), stack.getStatus().name(), DetailedStackStatus.AVAILABLE.name());
stackUpdaterService.updateStatus(stack.getId(), DetailedStackStatus.AVAILABLE, ResourceEvent.CLUSTER_EXTERNAL_DATABASE_DELETION_FINISHED, "External database deletion finished");
result = new TerminateExternalDatabaseResult(stack.getId(), EXTERNAL_DATABASE_WAIT_TERMINATION_SUCCESS_EVENT.event(), stack.getName(), stack.getCluster().getDatabaseServerCrn());
}
} catch (UserBreakException e) {
LOGGER.info("Database termination polling exited before timeout. Cause: ", e);
result = terminateFailedEvent(stack, e);
} catch (PollerStoppedException e) {
LOGGER.info(String.format("Database termination poller stopped for stack: %s", stack.getName()), e);
result = terminateFailedEvent(stack, e);
} catch (PollerException e) {
LOGGER.info(String.format("Database termination polling failed for stack: %s", stack.getName()), e);
result = terminateFailedEvent(stack, e);
} catch (Exception e) {
LOGGER.error(String.format("Exception during DB termination for stack/cluster: %s", stack.getName()), e);
result = terminateFailedEvent(stack, e);
}
LOGGER.debug("Sending reactor notification for selector {}", result.selector());
eventBus.notify(result.selector(), new Event<>(terminateExternalDatabaseRequest.getHeaders(), result));
}
Aggregations