Search in sources :

Example 41 with FlowLog

use of com.sequenceiq.flow.domain.FlowLog in project cloudbreak by hortonworks.

the class DatalakeRestoreActions method triggerDatalakeRestore.

@Bean(name = "DATALAKE_TRIGGERING_RESTORE_STATE")
public Action<?, ?> triggerDatalakeRestore() {
    return new AbstractSdxAction<>(DatalakeTriggerRestoreEvent.class) {

        @Override
        protected SdxContext createFlowContext(FlowParameters flowParameters, StateContext<FlowState, FlowEvent> stateContext, DatalakeTriggerRestoreEvent payload) {
            // When SDX is created as part of re-size flow chain, SDX in payload will not have the correct ID.
            Optional<FlowLog> lastFlowLog = flowLogService.getLastFlowLog(flowParameters.getFlowId());
            if (lastFlowLog.isPresent()) {
                SdxContext sdxContext;
                Optional<FlowChainLog> flowChainLog = flowChainLogService.findFirstByFlowChainIdOrderByCreatedDesc(lastFlowLog.get().getFlowChainId());
                if (flowChainLog.isPresent() && flowChainLog.get().getFlowChainType().equals(DatalakeResizeFlowEventChainFactory.class.getSimpleName())) {
                    SdxCluster sdxCluster = sdxService.getByNameInAccount(payload.getUserId(), payload.getSdxName());
                    LOGGER.info("Updating the Sdx-id in context from {} to {}", payload.getResourceId(), sdxCluster.getId());
                    payload.getDrStatus().setSdxClusterId(sdxCluster.getId());
                    sdxContext = SdxContext.from(flowParameters, payload);
                    sdxContext.setSdxId(sdxCluster.getId());
                    return sdxContext;
                }
            }
            return SdxContext.from(flowParameters, payload);
        }

        @Override
        protected void prepareExecution(DatalakeTriggerRestoreEvent payload, Map<Object, Object> variables) {
            super.prepareExecution(payload, variables);
        }

        @Override
        protected void doExecute(SdxContext context, DatalakeTriggerRestoreEvent payload, Map<Object, Object> variables) {
            DatalakeRestoreStatusResponse restoreStatusResponse = sdxBackupRestoreService.triggerDatalakeRestore(context.getSdxId(), payload.getBackupId(), payload.getBackupLocationOverride(), payload.getUserId());
            variables.put(RESTORE_ID, restoreStatusResponse.getRestoreId());
            variables.put(BACKUP_ID, restoreStatusResponse.getBackupId());
            variables.put(OPERATION_ID, restoreStatusResponse.getRestoreId());
            payload.getDrStatus().setOperationId(restoreStatusResponse.getRestoreId());
            if (!restoreStatusResponse.failed()) {
                sendEvent(context, DatalakeDatabaseRestoreStartEvent.from(payload, context.getSdxId(), restoreStatusResponse.getBackupId(), restoreStatusResponse.getRestoreId()));
            } else {
                LOGGER.error("Datalake restore has failed for {} ", context.getSdxId());
                sendEvent(context, DATALAKE_RESTORE_FAILED_EVENT.event(), payload);
            }
        }

        @Override
        protected Object getFailurePayload(DatalakeTriggerRestoreEvent payload, Optional<SdxContext> flowContext, Exception ex) {
            return DatalakeRestoreFailedEvent.from(flowContext, payload, ex);
        }
    };
}
Also used : Optional(java.util.Optional) FlowLog(com.sequenceiq.flow.domain.FlowLog) StateContext(org.springframework.statemachine.StateContext) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) SdxContext(com.sequenceiq.datalake.flow.SdxContext) FlowParameters(com.sequenceiq.flow.core.FlowParameters) AbstractSdxAction(com.sequenceiq.datalake.service.AbstractSdxAction) DatalakeTriggerRestoreEvent(com.sequenceiq.datalake.flow.dr.restore.event.DatalakeTriggerRestoreEvent) SdxCluster(com.sequenceiq.datalake.entity.SdxCluster) FlowChainLog(com.sequenceiq.flow.domain.FlowChainLog) Map(java.util.Map) DatalakeRestoreStatusResponse(com.sequenceiq.cloudbreak.datalakedr.model.DatalakeRestoreStatusResponse) Bean(org.springframework.context.annotation.Bean)

Example 42 with FlowLog

use of com.sequenceiq.flow.domain.FlowLog in project cloudbreak by hortonworks.

the class SdxRetryServiceTest method retryAndRestartFlowIfStackCreationInProgressWasTheLastFailedState.

@Test
public void retryAndRestartFlowIfStackCreationInProgressWasTheLastFailedState() {
    SdxCluster sdxCluster = new SdxCluster();
    sdxCluster.setId(1L);
    sdxCluster.setClusterName("sdxclustername");
    sdxCluster.setAccountId("accountid");
    FlowLog successfulFlowLog = new FlowLog();
    successfulFlowLog.setFlowId("FLOW_ID_1");
    successfulFlowLog.setStateStatus(StateStatus.SUCCESSFUL);
    successfulFlowLog.setNextEvent(SDX_STACK_CREATION_IN_PROGRESS_EVENT.name());
    successfulFlowLog.setCreated(1L);
    successfulFlowLog.setCurrentState(SDX_CREATION_WAIT_RDS_STATE.name());
    successfulFlowLog.setFlowType(ClassValue.of(SdxCreateFlowConfig.class));
    doAnswer(invocation -> {
        ((Consumer<FlowLog>) invocation.getArgument(1)).accept(successfulFlowLog);
        return null;
    }).when(flow2Handler).retryLastFailedFlow(anyLong(), any());
    when(flow2Handler.getFirstRetryableStateLogfromLatestFlow(anyLong())).thenReturn(successfulFlowLog);
    when(regionAwareInternalCrnGenerator.getInternalCrnForServiceAsString()).thenReturn("crn");
    when(regionAwareInternalCrnGeneratorFactory.iam()).thenReturn(regionAwareInternalCrnGenerator);
    sdxRetryService.retrySdx(sdxCluster);
    verify(stackV4Endpoint, times(1)).retry(any(), eq("sdxclustername"), anyString());
}
Also used : Consumer(java.util.function.Consumer) FlowLog(com.sequenceiq.flow.domain.FlowLog) SdxCluster(com.sequenceiq.datalake.entity.SdxCluster) SdxCreateFlowConfig(com.sequenceiq.datalake.flow.create.SdxCreateFlowConfig) Test(org.junit.jupiter.api.Test)

Example 43 with FlowLog

use of com.sequenceiq.flow.domain.FlowLog in project cloudbreak by hortonworks.

the class SdxRetryServiceTest method retryOnBackupRestoreFailure.

@Test
public void retryOnBackupRestoreFailure() {
    SdxCluster sdxCluster = new SdxCluster();
    sdxCluster.setId(1L);
    sdxCluster.setClusterName("sdxclustername");
    FlowLog successfulFlowLog = new FlowLog();
    successfulFlowLog.setFlowId("FLOW_ID_1");
    successfulFlowLog.setStateStatus(StateStatus.SUCCESSFUL);
    successfulFlowLog.setNextEvent(DATALAKE_TRIGGER_RESTORE_EVENT.name());
    successfulFlowLog.setCreated(1L);
    successfulFlowLog.setCurrentState(INIT_STATE.name());
    successfulFlowLog.setFlowType(ClassValue.of(DatalakeRestoreFlowConfig.class));
    when(flow2Handler.getFirstRetryableStateLogfromLatestFlow(anyLong())).thenReturn(successfulFlowLog);
    sdxRetryService.retrySdx(sdxCluster);
    verify(flow2Handler, times(1)).retryLastFailedFlowFromStart(any());
}
Also used : FlowLog(com.sequenceiq.flow.domain.FlowLog) SdxCluster(com.sequenceiq.datalake.entity.SdxCluster) DatalakeRestoreFlowConfig(com.sequenceiq.datalake.flow.dr.restore.DatalakeRestoreFlowConfig) Test(org.junit.jupiter.api.Test)

Example 44 with FlowLog

use of com.sequenceiq.flow.domain.FlowLog in project cloudbreak by hortonworks.

the class SdxRetryServiceTest method retryOnBackupBackupFailure.

@Test
public void retryOnBackupBackupFailure() {
    SdxCluster sdxCluster = new SdxCluster();
    sdxCluster.setId(1L);
    sdxCluster.setClusterName("sdxclustername");
    FlowLog successfulFlowLog = new FlowLog();
    successfulFlowLog.setFlowId("FLOW_ID_1");
    successfulFlowLog.setStateStatus(StateStatus.SUCCESSFUL);
    successfulFlowLog.setNextEvent(DATALAKE_TRIGGER_BACKUP_EVENT.name());
    successfulFlowLog.setCreated(1L);
    successfulFlowLog.setCurrentState(INIT_STATE.name());
    successfulFlowLog.setFlowType(ClassValue.of(DatalakeRestoreFlowConfig.class));
    when(flow2Handler.getFirstRetryableStateLogfromLatestFlow(anyLong())).thenReturn(successfulFlowLog);
    sdxRetryService.retrySdx(sdxCluster);
    verify(flow2Handler, times(1)).retryLastFailedFlowFromStart(any());
}
Also used : FlowLog(com.sequenceiq.flow.domain.FlowLog) SdxCluster(com.sequenceiq.datalake.entity.SdxCluster) DatalakeRestoreFlowConfig(com.sequenceiq.datalake.flow.dr.restore.DatalakeRestoreFlowConfig) Test(org.junit.jupiter.api.Test)

Example 45 with FlowLog

use of com.sequenceiq.flow.domain.FlowLog in project cloudbreak by hortonworks.

the class FlowProgressResponseConverter method convert.

public FlowProgressResponse convert(List<FlowLog> flowLogs, String resourceCrn) {
    FlowProgressResponse response = new FlowProgressResponse();
    if (CollectionUtils.isNotEmpty(flowLogs)) {
        FlowLog lastFlowLog = flowLogs.get(0);
        Optional<String> flowTypeOpt = flowLogs.stream().filter(fl -> fl.getFlowType() != null).findFirst().map(fl -> fl.getFlowType().getClassValue().getName());
        FlowLog firstFlowLog = flowLogs.get(flowLogs.size() - 1);
        response.setFlowId(lastFlowLog.getFlowId());
        response.setFlowChainId(lastFlowLog.getFlowChainId());
        Integer prgoressFromSteps = flowTypeOpt.map(s -> flowProgressHolder.getProgressPercentageForState(s, lastFlowLog.getCurrentState())).orElse(UNKNOWN_PROGRESS_PERCENTAGE);
        response.setProgress(prgoressFromSteps);
        response.setMaxNumberOfTransitions(flowTypeOpt.map(flowProgressHolder::getTransitionsSize).orElse(null));
        List<FlowLog> reversedFlowLogs = flowLogs.stream().sorted(Comparator.comparingLong(FlowLog::getCreated)).collect(Collectors.toList());
        response.setTransitions(createFlowStateTransitions(reversedFlowLogs, firstFlowLog.getCreated()));
        response.setFinalized(lastFlowLog.getFinalized());
        response.setCreated(firstFlowLog.getCreated());
        if (lastFlowLog.getFinalized()) {
            response.setElapsedTimeInSeconds(getRoundedTimeInSeconds(firstFlowLog.getCreated(), lastFlowLog.getCreated()));
            response.setProgress(FINISHED_PERCENTAGE);
        } else {
            response.setElapsedTimeInSeconds(getRoundedTimeInSeconds(firstFlowLog.getCreated(), new Date().getTime()));
        }
        response.setResourceCrn(resourceCrn);
    }
    return response;
}
Also used : FlowLog(com.sequenceiq.flow.domain.FlowLog) FlowProgressResponse(com.sequenceiq.flow.api.model.FlowProgressResponse) FlowStateTransitionResponse(com.sequenceiq.flow.api.model.FlowStateTransitionResponse) Date(java.util.Date) DecimalFormat(java.text.DecimalFormat) Collectors(java.util.stream.Collectors) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) List(java.util.List) Component(org.springframework.stereotype.Component) FlowProgressHolder(com.sequenceiq.flow.core.config.FlowProgressHolder) Optional(java.util.Optional) Comparator(java.util.Comparator) FlowProgressResponse(com.sequenceiq.flow.api.model.FlowProgressResponse) FlowLog(com.sequenceiq.flow.domain.FlowLog) Date(java.util.Date)

Aggregations

FlowLog (com.sequenceiq.flow.domain.FlowLog)92 Test (org.junit.jupiter.api.Test)25 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)21 List (java.util.List)13 FlowConfiguration (com.sequenceiq.flow.core.config.FlowConfiguration)12 TransactionService (com.sequenceiq.cloudbreak.common.service.TransactionService)11 Mockito.times (org.mockito.Mockito.times)11 Mockito.verify (org.mockito.Mockito.verify)11 Mockito.when (org.mockito.Mockito.when)11 HelloWorldFlowConfig (com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig)10 Map (java.util.Map)10 UUID (java.util.UUID)10 Collectors (java.util.stream.Collectors)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)10 Clock (com.sequenceiq.cloudbreak.common.service.Clock)9 FlowIdentifier (com.sequenceiq.flow.api.model.FlowIdentifier)9 FlowRegister (com.sequenceiq.flow.core.FlowRegister)9 SecureRandom (java.security.SecureRandom)9 Random (java.util.Random)9