Search in sources :

Example 1 with TerminateExternalDatabaseRequest

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);
        }
    };
}
Also used : ExternalDatabaseContext(com.sequenceiq.cloudbreak.core.flow2.externaldatabase.ExternalDatabaseContext) AbstractExternalDatabaseTerminationAction(com.sequenceiq.cloudbreak.core.flow2.externaldatabase.terminate.action.AbstractExternalDatabaseTerminationAction) ExternalDatabaseTerminationEvent(com.sequenceiq.cloudbreak.core.flow2.externaldatabase.terminate.config.ExternalDatabaseTerminationEvent) TerminationEvent(com.sequenceiq.cloudbreak.reactor.api.event.stack.TerminationEvent) Map(java.util.Map) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) TerminateExternalDatabaseRequest(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest) Bean(org.springframework.context.annotation.Bean)

Example 2 with TerminateExternalDatabaseRequest

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);
}
Also used : TerminationEvent(com.sequenceiq.cloudbreak.reactor.api.event.stack.TerminationEvent) TerminateExternalDatabaseRequest(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with TerminateExternalDatabaseRequest

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));
}
Also used : DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) Event(reactor.bus.Event) ResourceEvent(com.sequenceiq.cloudbreak.event.ResourceEvent) DatabaseAvailabilityType(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.database.DatabaseAvailabilityType) TerminateExternalDatabaseRequest(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings)

Example 4 with TerminateExternalDatabaseRequest

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);
}
Also used : TerminateExternalDatabaseResult(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseResult) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) Event(reactor.bus.Event) ResourceEvent(com.sequenceiq.cloudbreak.event.ResourceEvent) TerminateExternalDatabaseRequest(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with TerminateExternalDatabaseRequest

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));
}
Also used : UserBreakException(com.dyngr.exception.UserBreakException) TerminateExternalDatabaseResult(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseResult) Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) PollerException(com.dyngr.exception.PollerException) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) PollerStoppedException(com.dyngr.exception.PollerStoppedException) UserBreakException(com.dyngr.exception.UserBreakException) PollerException(com.dyngr.exception.PollerException) PollerStoppedException(com.dyngr.exception.PollerStoppedException) TerminateExternalDatabaseRequest(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) DatabaseAvailabilityType(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.database.DatabaseAvailabilityType)

Aggregations

TerminateExternalDatabaseRequest (com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 ResourceEvent (com.sequenceiq.cloudbreak.event.ResourceEvent)3 TerminateExternalDatabaseResult (com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseResult)3 DetailedEnvironmentResponse (com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse)3 ValueSource (org.junit.jupiter.params.provider.ValueSource)3 Event (reactor.bus.Event)3 DatabaseAvailabilityType (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.database.DatabaseAvailabilityType)2 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)2 TerminationEvent (com.sequenceiq.cloudbreak.reactor.api.event.stack.TerminationEvent)2 PollerException (com.dyngr.exception.PollerException)1 PollerStoppedException (com.dyngr.exception.PollerStoppedException)1 UserBreakException (com.dyngr.exception.UserBreakException)1 Selectable (com.sequenceiq.cloudbreak.common.event.Selectable)1 ExternalDatabaseContext (com.sequenceiq.cloudbreak.core.flow2.externaldatabase.ExternalDatabaseContext)1 AbstractExternalDatabaseTerminationAction (com.sequenceiq.cloudbreak.core.flow2.externaldatabase.terminate.action.AbstractExternalDatabaseTerminationAction)1 ExternalDatabaseTerminationEvent (com.sequenceiq.cloudbreak.core.flow2.externaldatabase.terminate.config.ExternalDatabaseTerminationEvent)1 Cluster (com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster)1 Map (java.util.Map)1 Test (org.junit.jupiter.api.Test)1