Search in sources :

Example 1 with TerminateExternalDatabaseResult

use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseResult in project cloudbreak by hortonworks.

the class ExternalDatabaseTerminationActionsTest method externalDatabaseCreationFinishedAction.

@Test
void externalDatabaseCreationFinishedAction() {
    TerminateExternalDatabaseResult terminateExternalDatabaseResultPayload = new TerminateExternalDatabaseResult(STACK_ID, EXTERNAL_DATABASE_WAIT_SUCCESS_EVENT.event(), STACK_NAME, STACK_CRN);
    when(stateContext.getMessageHeader(MessageFactory.HEADERS.DATA.name())).thenReturn(terminateExternalDatabaseResultPayload);
    Action<?, ?> action = configureAction(underTest::externalDatabaseTerminationFinishedAction);
    action.execute(stateContext);
    verifyNoMoreInteractions(stackUpdaterService);
    verify(eventBus).notify(selectorArgumentCaptor.capture(), eventArgumentCaptor.capture());
    verify(reactorEventFactory).createEvent(headersArgumentCaptor.capture(), payloadArgumentCaptor.capture());
    verify(metricService).incrementMetricCounter(MetricType.EXTERNAL_DATABASE_TERMINATION_SUCCESSFUL, STACK);
    assertThat(selectorArgumentCaptor.getValue()).isEqualTo("EXTERNAL_DATABASE_TERMINATION_FINISHED_EVENT");
    Object capturedPayload = payloadArgumentCaptor.getValue();
    assertThat(capturedPayload).isInstanceOf(StackEvent.class);
    StackEvent stackEvent = (StackEvent) capturedPayload;
    assertThat(stackEvent.getResourceId()).isEqualTo(STACK_ID);
}
Also used : TerminateExternalDatabaseResult(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseResult) StackEvent(com.sequenceiq.cloudbreak.reactor.api.event.StackEvent) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with TerminateExternalDatabaseResult

use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseResult 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 3 with TerminateExternalDatabaseResult

use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseResult 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)

Example 4 with TerminateExternalDatabaseResult

use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseResult in project cloudbreak by hortonworks.

the class TerminateExternalDatabaseHandlerTest method acceptDbNone.

@Test
void acceptDbNone() {
    when(stackService.getById(anyLong())).thenReturn(buildStack(DatabaseAvailabilityType.NONE));
    TerminateExternalDatabaseRequest request = new TerminateExternalDatabaseRequest(STACK_ID, "selector", "resourceName", "crn", false);
    Event<TerminateExternalDatabaseRequest> event = new Event<>(EVENT_HEADERS, request);
    underTest.accept(event);
    verifyNoMoreInteractions(terminationService);
    verifyNoMoreInteractions(stackUpdaterService);
    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()).isNull();
}
Also used : TerminateExternalDatabaseResult(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseResult) Event(reactor.bus.Event) ResourceEvent(com.sequenceiq.cloudbreak.event.ResourceEvent) TerminateExternalDatabaseRequest(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

TerminateExternalDatabaseResult (com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseResult)4 TerminateExternalDatabaseRequest (com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.TerminateExternalDatabaseRequest)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ResourceEvent (com.sequenceiq.cloudbreak.event.ResourceEvent)2 DetailedEnvironmentResponse (com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse)2 Test (org.junit.jupiter.api.Test)2 Event (reactor.bus.Event)2 PollerException (com.dyngr.exception.PollerException)1 PollerStoppedException (com.dyngr.exception.PollerStoppedException)1 UserBreakException (com.dyngr.exception.UserBreakException)1 DatabaseAvailabilityType (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.database.DatabaseAvailabilityType)1 Selectable (com.sequenceiq.cloudbreak.common.event.Selectable)1 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)1 Cluster (com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster)1 StackEvent (com.sequenceiq.cloudbreak.reactor.api.event.StackEvent)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1