Search in sources :

Example 1 with FlowConfiguration

use of com.sequenceiq.flow.core.config.FlowConfiguration in project cloudbreak by hortonworks.

the class Flow2HandlerTest method testRestartFlow.

@Test
public void testRestartFlow() throws TransactionExecutionException {
    FlowLog flowLog = createFlowLog(FLOW_CHAIN_ID);
    Payload payload = new TestPayload(STACK_ID);
    flowLog.setPayloadType(ClassValue.of(TestPayload.class));
    flowLog.setPayload(JsonWriter.objectToJson(payload));
    when(flowLogService.findFirstByFlowIdOrderByCreatedDesc(FLOW_ID)).thenReturn(Optional.of(flowLog));
    HelloWorldFlowConfig helloWorldFlowConfig = new HelloWorldFlowConfig();
    ReflectionTestUtils.setField(helloWorldFlowConfig, "defaultRestartAction", defaultRestartAction);
    setUpFlowConfigCreateFlow(helloWorldFlowConfig);
    List<FlowConfiguration<?>> flowConfigs = Lists.newArrayList(helloWorldFlowConfig);
    ReflectionTestUtils.setField(underTest, "flowConfigs", flowConfigs);
    underTest.restartFlow(FLOW_ID);
    ArgumentCaptor<Payload> payloadCaptor = ArgumentCaptor.forClass(Payload.class);
    ArgumentCaptor<FlowParameters> flowParamsCaptor = ArgumentCaptor.forClass(FlowParameters.class);
    verify(flowChainHandler, times(1)).restoreFlowChain(FLOW_CHAIN_ID);
    verify(flowLogService, never()).terminate(STACK_ID, FLOW_ID);
    verify(defaultRestartAction, times(1)).restart(flowParamsCaptor.capture(), eq(FLOW_CHAIN_ID), eq(NEXT_EVENT), payloadCaptor.capture());
    Payload captorValue = payloadCaptor.getValue();
    assertEquals(STACK_ID, captorValue.getResourceId());
    FlowParameters flowParameters = flowParamsCaptor.getValue();
    assertEquals(FLOW_ID, flowParameters.getFlowId());
    assertEquals(FLOW_TRIGGER_USERCRN, flowParameters.getFlowTriggerUserCrn());
}
Also used : FlowConfiguration(com.sequenceiq.flow.core.config.FlowConfiguration) FlowLog(com.sequenceiq.flow.domain.FlowLog) Payload(com.sequenceiq.cloudbreak.common.event.Payload) HelloWorldFlowConfig(com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig) Test(org.junit.jupiter.api.Test)

Example 2 with FlowConfiguration

use of com.sequenceiq.flow.core.config.FlowConfiguration in project cloudbreak by hortonworks.

the class Flow2HandlerTest method testRestartFlowNoRestartAction.

@Test
public void testRestartFlowNoRestartAction() throws TransactionExecutionException {
    FlowLog flowLog = createFlowLog(FLOW_CHAIN_ID);
    Payload payload = new TestPayload(STACK_ID);
    flowLog.setPayloadType(ClassValue.of(TestPayload.class));
    flowLog.setPayload(JsonWriter.objectToJson(payload));
    when(flowLogService.findFirstByFlowIdOrderByCreatedDesc(FLOW_ID)).thenReturn(Optional.of(flowLog));
    HelloWorldFlowConfig helloWorldFlowConfig = new HelloWorldFlowConfig();
    setUpFlowConfigCreateFlow(helloWorldFlowConfig);
    List<FlowConfiguration<?>> flowConfigs = Lists.newArrayList(helloWorldFlowConfig);
    ReflectionTestUtils.setField(underTest, "flowConfigs", flowConfigs);
    underTest.restartFlow(FLOW_ID);
    verify(flowChainHandler, times(1)).restoreFlowChain(FLOW_CHAIN_ID);
    verify(flowLogService, times(1)).terminate(STACK_ID, FLOW_ID);
    verify(defaultRestartAction, never()).restart(any(), any(), any(), any());
}
Also used : FlowConfiguration(com.sequenceiq.flow.core.config.FlowConfiguration) FlowLog(com.sequenceiq.flow.domain.FlowLog) Payload(com.sequenceiq.cloudbreak.common.event.Payload) HelloWorldFlowConfig(com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig) Test(org.junit.jupiter.api.Test)

Example 3 with FlowConfiguration

use of com.sequenceiq.flow.core.config.FlowConfiguration in project cloudbreak by hortonworks.

the class Flow2Handler method restartFlow.

public void restartFlow(FlowLog flowLog) {
    if (notSupportedFlowType(flowLog)) {
        try {
            LOGGER.error("Flow type or payload is not present on the classpath anymore. Terminating the flow {}.", flowLog);
            flowLogService.terminate(flowLog.getResourceId(), flowLog.getFlowId());
            return;
        } catch (TransactionExecutionException e) {
            throw new TransactionRuntimeExecutionException(e);
        }
    }
    if (flowLog.getFlowType() != null) {
        Optional<FlowConfiguration<?>> flowConfig = flowConfigs.stream().filter(fc -> flowLog.isFlowType(fc.getClass())).findFirst();
        try {
            String flowChainType = flowChainLogService.getFlowChainType(flowLog.getFlowChainId());
            Payload payload = (Payload) JsonReader.jsonToJava(flowLog.getPayload());
            Flow flow = flowConfig.get().createFlow(flowLog.getFlowId(), flowLog.getFlowChainId(), payload.getResourceId(), flowChainType);
            runningFlows.put(flow, flowLog.getFlowChainId());
            flowStatCache.put(flow.getFlowId(), flowLog.getFlowChainId(), payload.getResourceId(), flowConfig.get().getFlowOperationType().name(), flow.getFlowConfigClass(), true);
            if (flowLog.getFlowChainId() != null) {
                flowChainHandler.restoreFlowChain(flowLog.getFlowChainId());
            }
            Map<Object, Object> variables = (Map<Object, Object>) JsonReader.jsonToJava(flowLog.getVariables());
            flow.initialize(flowLog.getCurrentState(), variables);
            RestartAction restartAction = flowConfig.get().getRestartAction(flowLog.getNextEvent());
            if (restartAction != null) {
                LOGGER.debug("Restarting flow with id: '{}', flow chain id: '{}', flow type: '{}', restart action: '{}'", flow.getFlowId(), flowLog.getFlowChainId(), flowLog.getFlowType().getClassValue().getSimpleName(), restartAction.getClass().getSimpleName());
                Span span = tracer.buildSpan(flowLog.getCurrentState()).ignoreActiveSpan().start();
                restartAction.restart(new FlowParameters(flowLog.getFlowId(), flowLog.getFlowTriggerUserCrn(), flowLog.getOperationType().name(), span.context()), flowLog.getFlowChainId(), flowLog.getNextEvent(), payload);
                return;
            }
        } catch (RuntimeException e) {
            String message = String.format("Flow could not be restarted with id: '%s', flow chain id: '%s' and flow type: '%s'", flowLog.getFlowId(), flowLog.getFlowChainId(), flowLog.getFlowType().getClassValue().getSimpleName());
            LOGGER.error(message, e);
        }
        try {
            flowLogService.terminate(flowLog.getResourceId(), flowLog.getFlowId());
        } catch (TransactionExecutionException e) {
            throw new TransactionRuntimeExecutionException(e);
        }
    }
}
Also used : FLOW_ID(com.sequenceiq.flow.core.FlowConstants.FLOW_ID) AcceptResult(com.sequenceiq.cloudbreak.common.event.AcceptResult) LoggerFactory(org.slf4j.LoggerFactory) JsonReader(com.cedarsoftware.util.io.JsonReader) IdempotentEvent(com.sequenceiq.cloudbreak.common.event.IdempotentEvent) TransactionRuntimeExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionRuntimeExecutionException) Benchmark(com.sequenceiq.cloudbreak.util.Benchmark) FlowChains(com.sequenceiq.flow.core.chain.FlowChains) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) Pair(org.apache.commons.lang3.tuple.Pair) InMemoryCleanup(com.sequenceiq.flow.cleanup.InMemoryCleanup) FLOW_CANCEL(com.sequenceiq.flow.core.FlowConstants.FLOW_CANCEL) Map(java.util.Map) BadRequestException(javax.ws.rs.BadRequestException) FLOW_CHAIN_ID(com.sequenceiq.flow.core.FlowConstants.FLOW_CHAIN_ID) FlowLog(com.sequenceiq.flow.domain.FlowLog) FlowStatCache(com.sequenceiq.flow.core.cache.FlowStatCache) FLOW_CHAIN_TYPE(com.sequenceiq.flow.core.FlowConstants.FLOW_CHAIN_TYPE) NullUtil(com.sequenceiq.cloudbreak.util.NullUtil) Resource(javax.annotation.Resource) Set(java.util.Set) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) UUID(java.util.UUID) FLOW_FINAL(com.sequenceiq.flow.core.FlowConstants.FLOW_FINAL) FLOW_OPERATION_TYPE(com.sequenceiq.flow.core.FlowConstants.FLOW_OPERATION_TYPE) List(java.util.List) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) Optional(java.util.Optional) FlowStateConstants(com.sequenceiq.flow.core.FlowState.FlowStateConstants) Span(io.opentracing.Span) Consumer(reactor.fn.Consumer) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) Scope(io.opentracing.Scope) Payload(com.sequenceiq.cloudbreak.common.event.Payload) MDCBuilder(com.sequenceiq.cloudbreak.logger.MDCBuilder) FlowLogIdWithTypeAndTimestamp(com.sequenceiq.flow.domain.FlowLogIdWithTypeAndTimestamp) Inject(javax.inject.Inject) Acceptable(com.sequenceiq.cloudbreak.common.event.Acceptable) FlowFinalizerCallback(com.sequenceiq.flow.core.config.FlowFinalizerCallback) FlowChainLogService(com.sequenceiq.flow.service.flowlog.FlowChainLogService) Event(reactor.bus.Event) FlowIdentifier(com.sequenceiq.flow.api.model.FlowIdentifier) FlowLogUtil(com.sequenceiq.cloudbreak.service.flowlog.FlowLogUtil) FlowNotFoundException(com.sequenceiq.flow.core.exception.FlowNotFoundException) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) FlowNotTriggerableException(com.sequenceiq.flow.core.exception.FlowNotTriggerableException) SpanContext(io.opentracing.SpanContext) Component(org.springframework.stereotype.Component) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) FlowType(com.sequenceiq.flow.api.model.FlowType) NodeConfig(com.sequenceiq.flow.ha.NodeConfig) FlowConfiguration(com.sequenceiq.flow.core.config.FlowConfiguration) Comparator(java.util.Comparator) FlowChainHandler(com.sequenceiq.flow.core.chain.FlowChainHandler) FlowAcceptResult(com.sequenceiq.flow.core.model.FlowAcceptResult) FlowConfiguration(com.sequenceiq.flow.core.config.FlowConfiguration) Span(io.opentracing.Span) TransactionRuntimeExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionRuntimeExecutionException) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) Payload(com.sequenceiq.cloudbreak.common.event.Payload) Map(java.util.Map)

Example 4 with FlowConfiguration

use of com.sequenceiq.flow.core.config.FlowConfiguration in project cloudbreak by hortonworks.

the class Flow2HandlerTest method testRestartFlowNoRestartActionNoFlowChainId.

@Test
public void testRestartFlowNoRestartActionNoFlowChainId() throws TransactionExecutionException {
    FlowLog flowLog = createFlowLog(null);
    Payload payload = new TestPayload(STACK_ID);
    flowLog.setPayloadType(ClassValue.of(TestPayload.class));
    flowLog.setPayload(JsonWriter.objectToJson(payload));
    when(flowLogService.findFirstByFlowIdOrderByCreatedDesc(FLOW_ID)).thenReturn(Optional.of(flowLog));
    HelloWorldFlowConfig helloWorldFlowConfig = new HelloWorldFlowConfig();
    setUpFlowConfigCreateFlow(helloWorldFlowConfig);
    List<FlowConfiguration<?>> flowConfigs = Lists.newArrayList(helloWorldFlowConfig);
    ReflectionTestUtils.setField(underTest, "flowConfigs", flowConfigs);
    underTest.restartFlow(FLOW_ID);
    verify(flowChainHandler, never()).restoreFlowChain(FLOW_CHAIN_ID);
    verify(flowLogService, times(1)).terminate(STACK_ID, FLOW_ID);
    verify(defaultRestartAction, never()).restart(any(), any(), any(), any());
}
Also used : FlowConfiguration(com.sequenceiq.flow.core.config.FlowConfiguration) FlowLog(com.sequenceiq.flow.domain.FlowLog) Payload(com.sequenceiq.cloudbreak.common.event.Payload) HelloWorldFlowConfig(com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig) Test(org.junit.jupiter.api.Test)

Aggregations

Payload (com.sequenceiq.cloudbreak.common.event.Payload)4 FlowConfiguration (com.sequenceiq.flow.core.config.FlowConfiguration)4 FlowLog (com.sequenceiq.flow.domain.FlowLog)4 HelloWorldFlowConfig (com.sequenceiq.flow.core.helloworld.config.HelloWorldFlowConfig)3 Test (org.junit.jupiter.api.Test)2 JsonReader (com.cedarsoftware.util.io.JsonReader)1 AcceptResult (com.sequenceiq.cloudbreak.common.event.AcceptResult)1 Acceptable (com.sequenceiq.cloudbreak.common.event.Acceptable)1 IdempotentEvent (com.sequenceiq.cloudbreak.common.event.IdempotentEvent)1 CloudbreakServiceException (com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException)1 TransactionService (com.sequenceiq.cloudbreak.common.service.TransactionService)1 TransactionExecutionException (com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException)1 TransactionRuntimeExecutionException (com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionRuntimeExecutionException)1 MDCBuilder (com.sequenceiq.cloudbreak.logger.MDCBuilder)1 FlowLogUtil (com.sequenceiq.cloudbreak.service.flowlog.FlowLogUtil)1 Benchmark (com.sequenceiq.cloudbreak.util.Benchmark)1 NullUtil (com.sequenceiq.cloudbreak.util.NullUtil)1 FlowIdentifier (com.sequenceiq.flow.api.model.FlowIdentifier)1 FlowType (com.sequenceiq.flow.api.model.FlowType)1 InMemoryCleanup (com.sequenceiq.flow.cleanup.InMemoryCleanup)1