Search in sources :

Example 26 with Payload

use of com.sequenceiq.cloudbreak.common.event.Payload 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 27 with Payload

use of com.sequenceiq.cloudbreak.common.event.Payload in project cloudbreak by hortonworks.

the class EnvironmentFillInMemoryStateStoreRestartAction method restart.

@Override
public void restart(FlowParameters flowParameters, String flowChainId, String event, Object payload) {
    LOGGER.debug("Restoring MDC context and InMemoryStateStore entry for flow: '{}', flow chain: '{}', event: '{}'", flowParameters.getFlowId(), flowChainId, event);
    Payload envPayload = (Payload) payload;
    Optional<Environment> environment = environmentService.getById(envPayload.getResourceId());
    environment.ifPresent(env -> restart(flowParameters, flowChainId, event, payload, env));
}
Also used : Environment(com.sequenceiq.environment.environment.domain.Environment) Payload(com.sequenceiq.cloudbreak.common.event.Payload)

Example 28 with Payload

use of com.sequenceiq.cloudbreak.common.event.Payload in project cloudbreak by hortonworks.

the class ExceptionCatcherEventHandlerTest method testAcceptWhenNoEventNotification.

@Test
public void testAcceptWhenNoEventNotification() {
    Event<Payload> event = mock(Event.class);
    Event.Headers headers = mock(Event.Headers.class);
    Payload payload = mock(Payload.class);
    Selectable failureEvent = mock(Selectable.class);
    underTest.initialize(SELECTOR, failureEvent, false, null);
    when(event.getData()).thenReturn(payload);
    when(payload.getResourceId()).thenReturn(RESOURCE_ID);
    when(event.getHeaders()).thenReturn(headers);
    when(failureEvent.selector()).thenReturn(SELECTOR);
    underTest.accept(event);
    ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(eventBus).notify(eq(SELECTOR), eventCaptor.capture());
    Event eventBusEvent = eventCaptor.getValue();
    assertNotNull(eventBusEvent);
    assertEquals(failureEvent, eventBusEvent.getData());
}
Also used : Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) Event(reactor.bus.Event) Payload(com.sequenceiq.cloudbreak.common.event.Payload) Test(org.junit.jupiter.api.Test)

Example 29 with Payload

use of com.sequenceiq.cloudbreak.common.event.Payload in project cloudbreak by hortonworks.

the class ExceptionCatcherEventHandlerTest method testAcceptWhenException.

@Test
public void testAcceptWhenException() {
    Event<Payload> event = mock(Event.class);
    Event.Headers headers = mock(Event.Headers.class);
    Payload payload = mock(Payload.class);
    Selectable failureEvent = mock(Selectable.class);
    underTest.initialize(SELECTOR, failureEvent, true, null);
    when(event.getData()).thenReturn(payload);
    when(payload.getResourceId()).thenReturn(RESOURCE_ID);
    when(event.getHeaders()).thenReturn(headers);
    when(failureEvent.selector()).thenReturn(SELECTOR);
    underTest.accept(event);
    ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(eventBus).notify(eq(SELECTOR), eventCaptor.capture());
    Event eventBusEvent = eventCaptor.getValue();
    assertNotNull(eventBusEvent);
    assertEquals(failureEvent, eventBusEvent.getData());
}
Also used : Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) Event(reactor.bus.Event) Payload(com.sequenceiq.cloudbreak.common.event.Payload) Test(org.junit.jupiter.api.Test)

Example 30 with Payload

use of com.sequenceiq.cloudbreak.common.event.Payload in project cloudbreak by hortonworks.

the class FlowCancelServiceTest method testCancelRunningFlows.

@Test
public void testCancelRunningFlows() {
    Map<String, Object> parameters = Map.of();
    when(eventParameterFactory.createEventParameters(1L)).thenReturn(parameters);
    when(eventFactory.createEventWithErrHandler(eq(parameters), any(Payload.class))).thenAnswer(invocation -> {
        Payload payload = invocation.getArgument(1, Payload.class);
        Assert.assertEquals(Long.valueOf(1L), payload.getResourceId());
        return new Event<>(payload);
    });
    underTest.cancelRunningFlows(1L);
    verify(reactor, times(1)).notify(eq(FlowConstants.FLOW_CANCEL), any(Event.class));
}
Also used : Event(reactor.bus.Event) Payload(com.sequenceiq.cloudbreak.common.event.Payload) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

Payload (com.sequenceiq.cloudbreak.common.event.Payload)36 Test (org.junit.jupiter.api.Test)11 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)10 Optional (java.util.Optional)10 Inject (javax.inject.Inject)10 Event (reactor.bus.Event)10 Selectable (com.sequenceiq.cloudbreak.common.event.Selectable)9 MDCBuilder (com.sequenceiq.cloudbreak.logger.MDCBuilder)8 List (java.util.List)8 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)7 AvailabilityZone.availabilityZone (com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone.availabilityZone)7 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)7 Location (com.sequenceiq.cloudbreak.cloud.model.Location)7 Location.location (com.sequenceiq.cloudbreak.cloud.model.Location.location)7 Region.region (com.sequenceiq.cloudbreak.cloud.model.Region.region)7 FlowParameters (com.sequenceiq.flow.core.FlowParameters)7 Map (java.util.Map)7 Collectors (java.util.stream.Collectors)7 StateContext (org.springframework.statemachine.StateContext)7 Crn (com.sequenceiq.cloudbreak.auth.crn.Crn)6